blob: 7d756c27fadc07d0111f9177ed28a49dbebf5215 [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
Joe Fernandez558459f2011-10-13 16:47:36 -0700421 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * 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 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001480 * Accessibility event types that are dispatched for text population.
1481 */
1482 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1483 AccessibilityEvent.TYPE_VIEW_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1485 | AccessibilityEvent.TYPE_VIEW_SELECTED
1486 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1487 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1488 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001489 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001490 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1491 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001492
1493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * Temporary Rect currently for use in setBackground(). This will probably
1495 * be extended in the future to hold our own class with more than just
1496 * a Rect. :)
1497 */
1498 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001499
1500 /**
1501 * Map used to store views' tags.
1502 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001503 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001506 * The next available accessiiblity id.
1507 */
1508 private static int sNextAccessibilityViewId;
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * The animation currently associated with this view.
1512 * @hide
1513 */
1514 protected Animation mCurrentAnimation = null;
1515
1516 /**
1517 * Width as measured during measure pass.
1518 * {@hide}
1519 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001520 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001521 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
1523 /**
1524 * Height as measured during measure pass.
1525 * {@hide}
1526 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001527 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001528 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529
1530 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001531 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1532 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1533 * its display list. This flag, used only when hw accelerated, allows us to clear the
1534 * flag while retaining this information until it's needed (at getDisplayList() time and
1535 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1536 *
1537 * {@hide}
1538 */
1539 boolean mRecreateDisplayList = false;
1540
1541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * The view's identifier.
1543 * {@hide}
1544 *
1545 * @see #setId(int)
1546 * @see #getId()
1547 */
1548 @ViewDebug.ExportedProperty(resolveId = true)
1549 int mID = NO_ID;
1550
1551 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001552 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001553 */
1554 int mAccessibilityViewId = NO_ID;
1555
1556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 * The view's tag.
1558 * {@hide}
1559 *
1560 * @see #setTag(Object)
1561 * @see #getTag()
1562 */
1563 protected Object mTag;
1564
1565 // for mPrivateFlags:
1566 /** {@hide} */
1567 static final int WANTS_FOCUS = 0x00000001;
1568 /** {@hide} */
1569 static final int FOCUSED = 0x00000002;
1570 /** {@hide} */
1571 static final int SELECTED = 0x00000004;
1572 /** {@hide} */
1573 static final int IS_ROOT_NAMESPACE = 0x00000008;
1574 /** {@hide} */
1575 static final int HAS_BOUNDS = 0x00000010;
1576 /** {@hide} */
1577 static final int DRAWN = 0x00000020;
1578 /**
1579 * When this flag is set, this view is running an animation on behalf of its
1580 * children and should therefore not cancel invalidate requests, even if they
1581 * lie outside of this view's bounds.
1582 *
1583 * {@hide}
1584 */
1585 static final int DRAW_ANIMATION = 0x00000040;
1586 /** {@hide} */
1587 static final int SKIP_DRAW = 0x00000080;
1588 /** {@hide} */
1589 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1590 /** {@hide} */
1591 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1592 /** {@hide} */
1593 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1594 /** {@hide} */
1595 static final int MEASURED_DIMENSION_SET = 0x00000800;
1596 /** {@hide} */
1597 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001598 /** {@hide} */
1599 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600
1601 private static final int PRESSED = 0x00004000;
1602
1603 /** {@hide} */
1604 static final int DRAWING_CACHE_VALID = 0x00008000;
1605 /**
1606 * Flag used to indicate that this view should be drawn once more (and only once
1607 * more) after its animation has completed.
1608 * {@hide}
1609 */
1610 static final int ANIMATION_STARTED = 0x00010000;
1611
1612 private static final int SAVE_STATE_CALLED = 0x00020000;
1613
1614 /**
1615 * Indicates that the View returned true when onSetAlpha() was called and that
1616 * the alpha must be restored.
1617 * {@hide}
1618 */
1619 static final int ALPHA_SET = 0x00040000;
1620
1621 /**
1622 * Set by {@link #setScrollContainer(boolean)}.
1623 */
1624 static final int SCROLL_CONTAINER = 0x00080000;
1625
1626 /**
1627 * Set by {@link #setScrollContainer(boolean)}.
1628 */
1629 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1630
1631 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001632 * View flag indicating whether this view was invalidated (fully or partially.)
1633 *
1634 * @hide
1635 */
1636 static final int DIRTY = 0x00200000;
1637
1638 /**
1639 * View flag indicating whether this view was invalidated by an opaque
1640 * invalidate request.
1641 *
1642 * @hide
1643 */
1644 static final int DIRTY_OPAQUE = 0x00400000;
1645
1646 /**
1647 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1648 *
1649 * @hide
1650 */
1651 static final int DIRTY_MASK = 0x00600000;
1652
1653 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001654 * Indicates whether the background is opaque.
1655 *
1656 * @hide
1657 */
1658 static final int OPAQUE_BACKGROUND = 0x00800000;
1659
1660 /**
1661 * Indicates whether the scrollbars are opaque.
1662 *
1663 * @hide
1664 */
1665 static final int OPAQUE_SCROLLBARS = 0x01000000;
1666
1667 /**
1668 * Indicates whether the view is opaque.
1669 *
1670 * @hide
1671 */
1672 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001673
Adam Powelle14579b2009-12-16 18:39:52 -08001674 /**
1675 * Indicates a prepressed state;
1676 * the short time between ACTION_DOWN and recognizing
1677 * a 'real' press. Prepressed is used to recognize quick taps
1678 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001679 *
Adam Powelle14579b2009-12-16 18:39:52 -08001680 * @hide
1681 */
1682 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001683
Adam Powellc9fbaab2010-02-16 17:16:19 -08001684 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001685 * Indicates whether the view is temporarily detached.
1686 *
1687 * @hide
1688 */
1689 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001690
Adam Powell8568c3a2010-04-19 14:26:11 -07001691 /**
1692 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001693 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001694 * @hide
1695 */
1696 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001697
1698 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001699 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1700 * @hide
1701 */
1702 private static final int HOVERED = 0x10000000;
1703
1704 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001705 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1706 * for transform operations
1707 *
1708 * @hide
1709 */
Adam Powellf37df072010-09-17 16:22:49 -07001710 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001711
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001712 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001713 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001714
Chet Haasefd2b0022010-08-06 13:08:56 -07001715 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001716 * Indicates that this view was specifically invalidated, not just dirtied because some
1717 * child view was invalidated. The flag is used to determine when we need to recreate
1718 * a view's display list (as opposed to just returning a reference to its existing
1719 * display list).
1720 *
1721 * @hide
1722 */
1723 static final int INVALIDATED = 0x80000000;
1724
Christopher Tate3d4bf172011-03-28 16:16:46 -07001725 /* Masks for mPrivateFlags2 */
1726
1727 /**
1728 * Indicates that this view has reported that it can accept the current drag's content.
1729 * Cleared when the drag operation concludes.
1730 * @hide
1731 */
1732 static final int DRAG_CAN_ACCEPT = 0x00000001;
1733
1734 /**
1735 * Indicates that this view is currently directly under the drag location in a
1736 * drag-and-drop operation involving content that it can accept. Cleared when
1737 * the drag exits the view, or when the drag operation concludes.
1738 * @hide
1739 */
1740 static final int DRAG_HOVERED = 0x00000002;
1741
Cibu Johny86666632010-02-22 13:01:02 -08001742 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001743 * Indicates whether the view layout direction has been resolved and drawn to the
1744 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001745 *
1746 * @hide
1747 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001748 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1749
1750 /**
1751 * Indicates whether the view layout direction has been resolved.
1752 *
1753 * @hide
1754 */
1755 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1756
Cibu Johny86666632010-02-22 13:01:02 -08001757
Christopher Tate3d4bf172011-03-28 16:16:46 -07001758 /* End of masks for mPrivateFlags2 */
1759
1760 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1761
Chet Haasedaf98e92011-01-10 14:10:36 -08001762 /**
Adam Powell637d3372010-08-25 14:37:03 -07001763 * Always allow a user to over-scroll this view, provided it is a
1764 * view that can scroll.
1765 *
1766 * @see #getOverScrollMode()
1767 * @see #setOverScrollMode(int)
1768 */
1769 public static final int OVER_SCROLL_ALWAYS = 0;
1770
1771 /**
1772 * Allow a user to over-scroll this view only if the content is large
1773 * enough to meaningfully scroll, provided it is a view that can scroll.
1774 *
1775 * @see #getOverScrollMode()
1776 * @see #setOverScrollMode(int)
1777 */
1778 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1779
1780 /**
1781 * Never allow a user to over-scroll this view.
1782 *
1783 * @see #getOverScrollMode()
1784 * @see #setOverScrollMode(int)
1785 */
1786 public static final int OVER_SCROLL_NEVER = 2;
1787
1788 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001789 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001790 *
Joe Malin32736f02011-01-19 16:14:20 -08001791 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001792 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001793 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001794
1795 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001796 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1797 *
1798 * This is for use in games, book readers, video players, or any other "immersive" application
1799 * where the usual system chrome is deemed too distracting.
1800 *
1801 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001802 *
Joe Malin32736f02011-01-19 16:14:20 -08001803 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001804 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001805 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1806
1807 /**
1808 * View has requested that the system navigation be temporarily hidden.
1809 *
1810 * This is an even less obtrusive state than that called for by
1811 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1812 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1813 * those to disappear. This is useful (in conjunction with the
1814 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1815 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1816 * window flags) for displaying content using every last pixel on the display.
1817 *
1818 * There is a limitation: because navigation controls are so important, the least user
1819 * interaction will cause them to reappear immediately.
1820 *
1821 * @see #setSystemUiVisibility(int)
1822 */
1823 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1824
1825 /**
1826 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1827 */
1828 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1829
1830 /**
1831 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1832 */
1833 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001834
1835 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001836 * @hide
1837 *
1838 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1839 * out of the public fields to keep the undefined bits out of the developer's way.
1840 *
1841 * Flag to make the status bar not expandable. Unless you also
1842 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1843 */
1844 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1845
1846 /**
1847 * @hide
1848 *
1849 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1850 * out of the public fields to keep the undefined bits out of the developer's way.
1851 *
1852 * Flag to hide notification icons and scrolling ticker text.
1853 */
1854 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1855
1856 /**
1857 * @hide
1858 *
1859 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1860 * out of the public fields to keep the undefined bits out of the developer's way.
1861 *
1862 * Flag to disable incoming notification alerts. This will not block
1863 * icons, but it will block sound, vibrating and other visual or aural notifications.
1864 */
1865 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1866
1867 /**
1868 * @hide
1869 *
1870 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1871 * out of the public fields to keep the undefined bits out of the developer's way.
1872 *
1873 * Flag to hide only the scrolling ticker. Note that
1874 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1875 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1876 */
1877 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1878
1879 /**
1880 * @hide
1881 *
1882 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1883 * out of the public fields to keep the undefined bits out of the developer's way.
1884 *
1885 * Flag to hide the center system info area.
1886 */
1887 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1888
1889 /**
1890 * @hide
1891 *
1892 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1893 * out of the public fields to keep the undefined bits out of the developer's way.
1894 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001895 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001896 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1897 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001898 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001899
1900 /**
1901 * @hide
1902 *
1903 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1904 * out of the public fields to keep the undefined bits out of the developer's way.
1905 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001906 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001907 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1908 */
1909 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1910
1911 /**
1912 * @hide
1913 *
1914 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1915 * out of the public fields to keep the undefined bits out of the developer's way.
1916 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001917 * Flag to hide only the clock. You might use this if your activity has
1918 * its own clock making the status bar's clock redundant.
1919 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001920 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1921
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001922 /**
1923 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001924 *
1925 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1926 * out of the public fields to keep the undefined bits out of the developer's way.
1927 *
1928 * Flag to hide only the recent apps button. Don't use this
1929 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1930 */
1931 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1932
1933 /**
1934 * @hide
1935 *
1936 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1937 *
1938 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1939 */
1940 @Deprecated
1941 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1942 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1943
1944 /**
1945 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001946 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001947 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001948
1949 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001950 * These are the system UI flags that can be cleared by events outside
1951 * of an application. Currently this is just the ability to tap on the
1952 * screen while hiding the navigation bar to have it return.
1953 * @hide
1954 */
1955 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1956 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1957
1958 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001959 * Find views that render the specified text.
1960 *
1961 * @see #findViewsWithText(ArrayList, CharSequence, int)
1962 */
1963 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1964
1965 /**
1966 * Find find views that contain the specified content description.
1967 *
1968 * @see #findViewsWithText(ArrayList, CharSequence, int)
1969 */
1970 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1971
1972 /**
Adam Powell637d3372010-08-25 14:37:03 -07001973 * Controls the over-scroll mode for this view.
1974 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1975 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1976 * and {@link #OVER_SCROLL_NEVER}.
1977 */
1978 private int mOverScrollMode;
1979
1980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 * The parent this view is attached to.
1982 * {@hide}
1983 *
1984 * @see #getParent()
1985 */
1986 protected ViewParent mParent;
1987
1988 /**
1989 * {@hide}
1990 */
1991 AttachInfo mAttachInfo;
1992
1993 /**
1994 * {@hide}
1995 */
Romain Guy809a7f62009-05-14 15:44:42 -07001996 @ViewDebug.ExportedProperty(flagMapping = {
1997 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1998 name = "FORCE_LAYOUT"),
1999 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2000 name = "LAYOUT_REQUIRED"),
2001 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002002 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002003 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2004 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2005 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2006 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2007 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002009 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010
2011 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002012 * This view's request for the visibility of the status bar.
2013 * @hide
2014 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002015 @ViewDebug.ExportedProperty(flagMapping = {
2016 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2017 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2018 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2019 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2020 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2021 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2022 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2023 equals = SYSTEM_UI_FLAG_VISIBLE,
2024 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2025 })
Joe Onorato664644d2011-01-23 17:53:23 -08002026 int mSystemUiVisibility;
2027
2028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 * Count of how many windows this view has been attached to.
2030 */
2031 int mWindowAttachCount;
2032
2033 /**
2034 * The layout parameters associated with this view and used by the parent
2035 * {@link android.view.ViewGroup} to determine how this view should be
2036 * laid out.
2037 * {@hide}
2038 */
2039 protected ViewGroup.LayoutParams mLayoutParams;
2040
2041 /**
2042 * The view flags hold various views states.
2043 * {@hide}
2044 */
2045 @ViewDebug.ExportedProperty
2046 int mViewFlags;
2047
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002048 static class TransformationInfo {
2049 /**
2050 * The transform matrix for the View. This transform is calculated internally
2051 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2052 * is used by default. Do *not* use this variable directly; instead call
2053 * getMatrix(), which will automatically recalculate the matrix if necessary
2054 * to get the correct matrix based on the latest rotation and scale properties.
2055 */
2056 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002057
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002058 /**
2059 * The transform matrix for the View. This transform is calculated internally
2060 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2061 * is used by default. Do *not* use this variable directly; instead call
2062 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2063 * to get the correct matrix based on the latest rotation and scale properties.
2064 */
2065 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002066
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002067 /**
2068 * An internal variable that tracks whether we need to recalculate the
2069 * transform matrix, based on whether the rotation or scaleX/Y properties
2070 * have changed since the matrix was last calculated.
2071 */
2072 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002073
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002074 /**
2075 * An internal variable that tracks whether we need to recalculate the
2076 * transform matrix, based on whether the rotation or scaleX/Y properties
2077 * have changed since the matrix was last calculated.
2078 */
2079 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002080
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002081 /**
2082 * A variable that tracks whether we need to recalculate the
2083 * transform matrix, based on whether the rotation or scaleX/Y properties
2084 * have changed since the matrix was last calculated. This variable
2085 * is only valid after a call to updateMatrix() or to a function that
2086 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2087 */
2088 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002089
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002090 /**
2091 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2092 */
2093 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002094
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002095 /**
2096 * This matrix is used when computing the matrix for 3D rotations.
2097 */
2098 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002099
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002100 /**
2101 * These prev values are used to recalculate a centered pivot point when necessary. The
2102 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2103 * set), so thes values are only used then as well.
2104 */
2105 private int mPrevWidth = -1;
2106 private int mPrevHeight = -1;
2107
2108 /**
2109 * The degrees rotation around the vertical axis through the pivot point.
2110 */
2111 @ViewDebug.ExportedProperty
2112 float mRotationY = 0f;
2113
2114 /**
2115 * The degrees rotation around the horizontal axis through the pivot point.
2116 */
2117 @ViewDebug.ExportedProperty
2118 float mRotationX = 0f;
2119
2120 /**
2121 * The degrees rotation around the pivot point.
2122 */
2123 @ViewDebug.ExportedProperty
2124 float mRotation = 0f;
2125
2126 /**
2127 * The amount of translation of the object away from its left property (post-layout).
2128 */
2129 @ViewDebug.ExportedProperty
2130 float mTranslationX = 0f;
2131
2132 /**
2133 * The amount of translation of the object away from its top property (post-layout).
2134 */
2135 @ViewDebug.ExportedProperty
2136 float mTranslationY = 0f;
2137
2138 /**
2139 * The amount of scale in the x direction around the pivot point. A
2140 * value of 1 means no scaling is applied.
2141 */
2142 @ViewDebug.ExportedProperty
2143 float mScaleX = 1f;
2144
2145 /**
2146 * The amount of scale in the y direction around the pivot point. A
2147 * value of 1 means no scaling is applied.
2148 */
2149 @ViewDebug.ExportedProperty
2150 float mScaleY = 1f;
2151
2152 /**
2153 * The amount of scale in the x direction around the pivot point. A
2154 * value of 1 means no scaling is applied.
2155 */
2156 @ViewDebug.ExportedProperty
2157 float mPivotX = 0f;
2158
2159 /**
2160 * The amount of scale in the y direction around the pivot point. A
2161 * value of 1 means no scaling is applied.
2162 */
2163 @ViewDebug.ExportedProperty
2164 float mPivotY = 0f;
2165
2166 /**
2167 * The opacity of the View. This is a value from 0 to 1, where 0 means
2168 * completely transparent and 1 means completely opaque.
2169 */
2170 @ViewDebug.ExportedProperty
2171 float mAlpha = 1f;
2172 }
2173
2174 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002175
Joe Malin32736f02011-01-19 16:14:20 -08002176 private boolean mLastIsOpaque;
2177
Chet Haasefd2b0022010-08-06 13:08:56 -07002178 /**
2179 * Convenience value to check for float values that are close enough to zero to be considered
2180 * zero.
2181 */
Romain Guy2542d192010-08-18 11:47:12 -07002182 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002183
2184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 * The distance in pixels from the left edge of this view's parent
2186 * to the left edge of this view.
2187 * {@hide}
2188 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002189 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 protected int mLeft;
2191 /**
2192 * The distance in pixels from the left edge of this view's parent
2193 * to the right edge of this view.
2194 * {@hide}
2195 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002196 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 protected int mRight;
2198 /**
2199 * The distance in pixels from the top edge of this view's parent
2200 * to the top edge of this view.
2201 * {@hide}
2202 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002203 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 protected int mTop;
2205 /**
2206 * The distance in pixels from the top edge of this view's parent
2207 * to the bottom edge of this view.
2208 * {@hide}
2209 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002210 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 protected int mBottom;
2212
2213 /**
2214 * The offset, in pixels, by which the content of this view is scrolled
2215 * horizontally.
2216 * {@hide}
2217 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002218 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 protected int mScrollX;
2220 /**
2221 * The offset, in pixels, by which the content of this view is scrolled
2222 * vertically.
2223 * {@hide}
2224 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002225 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 protected int mScrollY;
2227
2228 /**
2229 * The left padding in pixels, that is the distance in pixels between the
2230 * left edge of this view and the left edge of its content.
2231 * {@hide}
2232 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002233 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 protected int mPaddingLeft;
2235 /**
2236 * The right padding in pixels, that is the distance in pixels between the
2237 * right edge of this view and the right edge of its content.
2238 * {@hide}
2239 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002240 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 protected int mPaddingRight;
2242 /**
2243 * The top padding in pixels, that is the distance in pixels between the
2244 * top edge of this view and the top edge of its content.
2245 * {@hide}
2246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002247 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 protected int mPaddingTop;
2249 /**
2250 * The bottom padding in pixels, that is the distance in pixels between the
2251 * bottom edge of this view and the bottom edge of its content.
2252 * {@hide}
2253 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002254 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 protected int mPaddingBottom;
2256
2257 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002258 * Briefly describes the view and is primarily used for accessibility support.
2259 */
2260 private CharSequence mContentDescription;
2261
2262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002264 *
2265 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002267 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002268 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269
2270 /**
2271 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002272 *
2273 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002275 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002276 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002278 /**
Adam Powell20232d02010-12-08 21:08:53 -08002279 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002280 *
2281 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002282 */
2283 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002284 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002285
2286 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002287 * Cache if the user padding is relative.
2288 *
2289 */
2290 @ViewDebug.ExportedProperty(category = "padding")
2291 boolean mUserPaddingRelative;
2292
2293 /**
2294 * Cache the paddingStart set by the user to append to the scrollbar's size.
2295 *
2296 */
2297 @ViewDebug.ExportedProperty(category = "padding")
2298 int mUserPaddingStart;
2299
2300 /**
2301 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2302 *
2303 */
2304 @ViewDebug.ExportedProperty(category = "padding")
2305 int mUserPaddingEnd;
2306
2307 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002308 * @hide
2309 */
2310 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2311 /**
2312 * @hide
2313 */
2314 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 private Drawable mBGDrawable;
2317
2318 private int mBackgroundResource;
2319 private boolean mBackgroundSizeChanged;
2320
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002321 static class ListenerInfo {
2322 /**
2323 * Listener used to dispatch focus change events.
2324 * This field should be made private, so it is hidden from the SDK.
2325 * {@hide}
2326 */
2327 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002329 /**
2330 * Listeners for layout change events.
2331 */
2332 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002333
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002334 /**
2335 * Listeners for attach events.
2336 */
2337 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002338
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002339 /**
2340 * Listener used to dispatch click events.
2341 * This field should be made private, so it is hidden from the SDK.
2342 * {@hide}
2343 */
2344 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002346 /**
2347 * Listener used to dispatch long click events.
2348 * This field should be made private, so it is hidden from the SDK.
2349 * {@hide}
2350 */
2351 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002353 /**
2354 * Listener used to build the context menu.
2355 * This field should be made private, so it is hidden from the SDK.
2356 * {@hide}
2357 */
2358 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002360 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002362 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002364 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002365
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002366 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002367
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002368 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002369
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002370 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2371 }
2372
2373 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 /**
2376 * The application environment this view lives in.
2377 * This field should be made private, so it is hidden from the SDK.
2378 * {@hide}
2379 */
2380 protected Context mContext;
2381
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002382 private final Resources mResources;
2383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 private ScrollabilityCache mScrollCache;
2385
2386 private int[] mDrawableState = null;
2387
Romain Guy0211a0a2011-02-14 16:34:59 -08002388 /**
2389 * Set to true when drawing cache is enabled and cannot be created.
2390 *
2391 * @hide
2392 */
2393 public boolean mCachingFailed;
2394
Romain Guy02890fd2010-08-06 17:58:44 -07002395 private Bitmap mDrawingCache;
2396 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002397 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002398 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399
2400 /**
2401 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2402 * the user may specify which view to go to next.
2403 */
2404 private int mNextFocusLeftId = View.NO_ID;
2405
2406 /**
2407 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2408 * the user may specify which view to go to next.
2409 */
2410 private int mNextFocusRightId = View.NO_ID;
2411
2412 /**
2413 * When this view has focus and the next focus is {@link #FOCUS_UP},
2414 * the user may specify which view to go to next.
2415 */
2416 private int mNextFocusUpId = View.NO_ID;
2417
2418 /**
2419 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2420 * the user may specify which view to go to next.
2421 */
2422 private int mNextFocusDownId = View.NO_ID;
2423
Jeff Brown4e6319b2010-12-13 10:36:51 -08002424 /**
2425 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2426 * the user may specify which view to go to next.
2427 */
2428 int mNextFocusForwardId = View.NO_ID;
2429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002431 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002432 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002433 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 private UnsetPressedState mUnsetPressedState;
2436
2437 /**
2438 * Whether the long press's action has been invoked. The tap's action is invoked on the
2439 * up event while a long press is invoked as soon as the long press duration is reached, so
2440 * a long press could be performed before the tap is checked, in which case the tap's action
2441 * should not be invoked.
2442 */
2443 private boolean mHasPerformedLongPress;
2444
2445 /**
2446 * The minimum height of the view. We'll try our best to have the height
2447 * of this view to at least this amount.
2448 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002449 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 private int mMinHeight;
2451
2452 /**
2453 * The minimum width of the view. We'll try our best to have the width
2454 * of this view to at least this amount.
2455 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002456 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 private int mMinWidth;
2458
2459 /**
2460 * The delegate to handle touch events that are physically in this view
2461 * but should be handled by another view.
2462 */
2463 private TouchDelegate mTouchDelegate = null;
2464
2465 /**
2466 * Solid color to use as a background when creating the drawing cache. Enables
2467 * the cache to use 16 bit bitmaps instead of 32 bit.
2468 */
2469 private int mDrawingCacheBackgroundColor = 0;
2470
2471 /**
2472 * Special tree observer used when mAttachInfo is null.
2473 */
2474 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002475
Adam Powelle14579b2009-12-16 18:39:52 -08002476 /**
2477 * Cache the touch slop from the context that created the view.
2478 */
2479 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002482 * Object that handles automatic animation of view properties.
2483 */
2484 private ViewPropertyAnimator mAnimator = null;
2485
2486 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002487 * Flag indicating that a drag can cross window boundaries. When
2488 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2489 * with this flag set, all visible applications will be able to participate
2490 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002491 *
2492 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002493 */
2494 public static final int DRAG_FLAG_GLOBAL = 1;
2495
2496 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002497 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2498 */
2499 private float mVerticalScrollFactor;
2500
2501 /**
Adam Powell20232d02010-12-08 21:08:53 -08002502 * Position of the vertical scroll bar.
2503 */
2504 private int mVerticalScrollbarPosition;
2505
2506 /**
2507 * Position the scroll bar at the default position as determined by the system.
2508 */
2509 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2510
2511 /**
2512 * Position the scroll bar along the left edge.
2513 */
2514 public static final int SCROLLBAR_POSITION_LEFT = 1;
2515
2516 /**
2517 * Position the scroll bar along the right edge.
2518 */
2519 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2520
2521 /**
Romain Guy171c5922011-01-06 10:04:23 -08002522 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002523 *
2524 * @see #getLayerType()
2525 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002526 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002527 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002528 */
2529 public static final int LAYER_TYPE_NONE = 0;
2530
2531 /**
2532 * <p>Indicates that the view has a software layer. A software layer is backed
2533 * by a bitmap and causes the view to be rendered using Android's software
2534 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002535 *
Romain Guy171c5922011-01-06 10:04:23 -08002536 * <p>Software layers have various usages:</p>
2537 * <p>When the application is not using hardware acceleration, a software layer
2538 * is useful to apply a specific color filter and/or blending mode and/or
2539 * translucency to a view and all its children.</p>
2540 * <p>When the application is using hardware acceleration, a software layer
2541 * is useful to render drawing primitives not supported by the hardware
2542 * accelerated pipeline. It can also be used to cache a complex view tree
2543 * into a texture and reduce the complexity of drawing operations. For instance,
2544 * when animating a complex view tree with a translation, a software layer can
2545 * be used to render the view tree only once.</p>
2546 * <p>Software layers should be avoided when the affected view tree updates
2547 * often. Every update will require to re-render the software layer, which can
2548 * potentially be slow (particularly when hardware acceleration is turned on
2549 * since the layer will have to be uploaded into a hardware texture after every
2550 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002551 *
2552 * @see #getLayerType()
2553 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002554 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002555 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002556 */
2557 public static final int LAYER_TYPE_SOFTWARE = 1;
2558
2559 /**
2560 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2561 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2562 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2563 * rendering pipeline, but only if hardware acceleration is turned on for the
2564 * view hierarchy. When hardware acceleration is turned off, hardware layers
2565 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002566 *
Romain Guy171c5922011-01-06 10:04:23 -08002567 * <p>A hardware layer is useful to apply a specific color filter and/or
2568 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002569 * <p>A hardware layer can be used to cache a complex view tree into a
2570 * texture and reduce the complexity of drawing operations. For instance,
2571 * when animating a complex view tree with a translation, a hardware layer can
2572 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002573 * <p>A hardware layer can also be used to increase the rendering quality when
2574 * rotation transformations are applied on a view. It can also be used to
2575 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002576 *
2577 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002578 * @see #setLayerType(int, android.graphics.Paint)
2579 * @see #LAYER_TYPE_NONE
2580 * @see #LAYER_TYPE_SOFTWARE
2581 */
2582 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002583
Romain Guy3aaff3a2011-01-12 14:18:47 -08002584 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2585 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2586 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2587 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2588 })
Romain Guy171c5922011-01-06 10:04:23 -08002589 int mLayerType = LAYER_TYPE_NONE;
2590 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002591 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002592
2593 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002594 * Set to true when the view is sending hover accessibility events because it
2595 * is the innermost hovered view.
2596 */
2597 private boolean mSendingHoverAccessibilityEvents;
2598
2599 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002600 * Delegate for injecting accessiblity functionality.
2601 */
2602 AccessibilityDelegate mAccessibilityDelegate;
2603
2604 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002605 * Text direction is inherited thru {@link ViewGroup}
2606 * @hide
2607 */
2608 public static final int TEXT_DIRECTION_INHERIT = 0;
2609
2610 /**
2611 * Text direction is using "first strong algorithm". The first strong directional character
2612 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002613 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002614 *
2615 * @hide
2616 */
2617 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2618
2619 /**
2620 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2621 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002622 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002623 *
2624 * @hide
2625 */
2626 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2627
2628 /**
2629 * Text direction is forced to LTR.
2630 *
2631 * @hide
2632 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002633 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002634
2635 /**
2636 * Text direction is forced to RTL.
2637 *
2638 * @hide
2639 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002640 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002641
2642 /**
2643 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002644 *
2645 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002646 */
2647 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2648
2649 /**
2650 * The text direction that has been defined by {@link #setTextDirection(int)}.
2651 *
2652 * {@hide}
2653 */
2654 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002655 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2656 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2657 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2658 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2659 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2660 })
Doug Feltcb3791202011-07-07 11:57:48 -07002661 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002662
2663 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002664 * The resolved text direction. This needs resolution if the value is
2665 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2666 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2667 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002668 *
2669 * {@hide}
2670 */
2671 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002672 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2673 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2674 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002675 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2676 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2677 })
Doug Feltcb3791202011-07-07 11:57:48 -07002678 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002679
2680 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002681 * Consistency verifier for debugging purposes.
2682 * @hide
2683 */
2684 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2685 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2686 new InputEventConsistencyVerifier(this, 0) : null;
2687
2688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 * Simple constructor to use when creating a view from code.
2690 *
2691 * @param context The Context the view is running in, through which it can
2692 * access the current theme, resources, etc.
2693 */
2694 public View(Context context) {
2695 mContext = context;
2696 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002697 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002698 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002699 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002700 mUserPaddingStart = -1;
2701 mUserPaddingEnd = -1;
2702 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 }
2704
2705 /**
2706 * Constructor that is called when inflating a view from XML. This is called
2707 * when a view is being constructed from an XML file, supplying attributes
2708 * that were specified in the XML file. This version uses a default style of
2709 * 0, so the only attribute values applied are those in the Context's Theme
2710 * and the given AttributeSet.
2711 *
2712 * <p>
2713 * The method onFinishInflate() will be called after all children have been
2714 * added.
2715 *
2716 * @param context The Context the view is running in, through which it can
2717 * access the current theme, resources, etc.
2718 * @param attrs The attributes of the XML tag that is inflating the view.
2719 * @see #View(Context, AttributeSet, int)
2720 */
2721 public View(Context context, AttributeSet attrs) {
2722 this(context, attrs, 0);
2723 }
2724
2725 /**
2726 * Perform inflation from XML and apply a class-specific base style. This
2727 * constructor of View allows subclasses to use their own base style when
2728 * they are inflating. For example, a Button class's constructor would call
2729 * this version of the super class constructor and supply
2730 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2731 * the theme's button style to modify all of the base view attributes (in
2732 * particular its background) as well as the Button class's attributes.
2733 *
2734 * @param context The Context the view is running in, through which it can
2735 * access the current theme, resources, etc.
2736 * @param attrs The attributes of the XML tag that is inflating the view.
2737 * @param defStyle The default style to apply to this view. If 0, no style
2738 * will be applied (beyond what is included in the theme). This may
2739 * either be an attribute resource, whose value will be retrieved
2740 * from the current theme, or an explicit style resource.
2741 * @see #View(Context, AttributeSet)
2742 */
2743 public View(Context context, AttributeSet attrs, int defStyle) {
2744 this(context);
2745
2746 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2747 defStyle, 0);
2748
2749 Drawable background = null;
2750
2751 int leftPadding = -1;
2752 int topPadding = -1;
2753 int rightPadding = -1;
2754 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002755 int startPadding = -1;
2756 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757
2758 int padding = -1;
2759
2760 int viewFlagValues = 0;
2761 int viewFlagMasks = 0;
2762
2763 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 int x = 0;
2766 int y = 0;
2767
Chet Haase73066682010-11-29 15:55:32 -08002768 float tx = 0;
2769 float ty = 0;
2770 float rotation = 0;
2771 float rotationX = 0;
2772 float rotationY = 0;
2773 float sx = 1f;
2774 float sy = 1f;
2775 boolean transformSet = false;
2776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2778
Adam Powell637d3372010-08-25 14:37:03 -07002779 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 final int N = a.getIndexCount();
2781 for (int i = 0; i < N; i++) {
2782 int attr = a.getIndex(i);
2783 switch (attr) {
2784 case com.android.internal.R.styleable.View_background:
2785 background = a.getDrawable(attr);
2786 break;
2787 case com.android.internal.R.styleable.View_padding:
2788 padding = a.getDimensionPixelSize(attr, -1);
2789 break;
2790 case com.android.internal.R.styleable.View_paddingLeft:
2791 leftPadding = a.getDimensionPixelSize(attr, -1);
2792 break;
2793 case com.android.internal.R.styleable.View_paddingTop:
2794 topPadding = a.getDimensionPixelSize(attr, -1);
2795 break;
2796 case com.android.internal.R.styleable.View_paddingRight:
2797 rightPadding = a.getDimensionPixelSize(attr, -1);
2798 break;
2799 case com.android.internal.R.styleable.View_paddingBottom:
2800 bottomPadding = a.getDimensionPixelSize(attr, -1);
2801 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002802 case com.android.internal.R.styleable.View_paddingStart:
2803 startPadding = a.getDimensionPixelSize(attr, -1);
2804 break;
2805 case com.android.internal.R.styleable.View_paddingEnd:
2806 endPadding = a.getDimensionPixelSize(attr, -1);
2807 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 case com.android.internal.R.styleable.View_scrollX:
2809 x = a.getDimensionPixelOffset(attr, 0);
2810 break;
2811 case com.android.internal.R.styleable.View_scrollY:
2812 y = a.getDimensionPixelOffset(attr, 0);
2813 break;
Chet Haase73066682010-11-29 15:55:32 -08002814 case com.android.internal.R.styleable.View_alpha:
2815 setAlpha(a.getFloat(attr, 1f));
2816 break;
2817 case com.android.internal.R.styleable.View_transformPivotX:
2818 setPivotX(a.getDimensionPixelOffset(attr, 0));
2819 break;
2820 case com.android.internal.R.styleable.View_transformPivotY:
2821 setPivotY(a.getDimensionPixelOffset(attr, 0));
2822 break;
2823 case com.android.internal.R.styleable.View_translationX:
2824 tx = a.getDimensionPixelOffset(attr, 0);
2825 transformSet = true;
2826 break;
2827 case com.android.internal.R.styleable.View_translationY:
2828 ty = a.getDimensionPixelOffset(attr, 0);
2829 transformSet = true;
2830 break;
2831 case com.android.internal.R.styleable.View_rotation:
2832 rotation = a.getFloat(attr, 0);
2833 transformSet = true;
2834 break;
2835 case com.android.internal.R.styleable.View_rotationX:
2836 rotationX = a.getFloat(attr, 0);
2837 transformSet = true;
2838 break;
2839 case com.android.internal.R.styleable.View_rotationY:
2840 rotationY = a.getFloat(attr, 0);
2841 transformSet = true;
2842 break;
2843 case com.android.internal.R.styleable.View_scaleX:
2844 sx = a.getFloat(attr, 1f);
2845 transformSet = true;
2846 break;
2847 case com.android.internal.R.styleable.View_scaleY:
2848 sy = a.getFloat(attr, 1f);
2849 transformSet = true;
2850 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 case com.android.internal.R.styleable.View_id:
2852 mID = a.getResourceId(attr, NO_ID);
2853 break;
2854 case com.android.internal.R.styleable.View_tag:
2855 mTag = a.getText(attr);
2856 break;
2857 case com.android.internal.R.styleable.View_fitsSystemWindows:
2858 if (a.getBoolean(attr, false)) {
2859 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2860 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2861 }
2862 break;
2863 case com.android.internal.R.styleable.View_focusable:
2864 if (a.getBoolean(attr, false)) {
2865 viewFlagValues |= FOCUSABLE;
2866 viewFlagMasks |= FOCUSABLE_MASK;
2867 }
2868 break;
2869 case com.android.internal.R.styleable.View_focusableInTouchMode:
2870 if (a.getBoolean(attr, false)) {
2871 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2872 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2873 }
2874 break;
2875 case com.android.internal.R.styleable.View_clickable:
2876 if (a.getBoolean(attr, false)) {
2877 viewFlagValues |= CLICKABLE;
2878 viewFlagMasks |= CLICKABLE;
2879 }
2880 break;
2881 case com.android.internal.R.styleable.View_longClickable:
2882 if (a.getBoolean(attr, false)) {
2883 viewFlagValues |= LONG_CLICKABLE;
2884 viewFlagMasks |= LONG_CLICKABLE;
2885 }
2886 break;
2887 case com.android.internal.R.styleable.View_saveEnabled:
2888 if (!a.getBoolean(attr, true)) {
2889 viewFlagValues |= SAVE_DISABLED;
2890 viewFlagMasks |= SAVE_DISABLED_MASK;
2891 }
2892 break;
2893 case com.android.internal.R.styleable.View_duplicateParentState:
2894 if (a.getBoolean(attr, false)) {
2895 viewFlagValues |= DUPLICATE_PARENT_STATE;
2896 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2897 }
2898 break;
2899 case com.android.internal.R.styleable.View_visibility:
2900 final int visibility = a.getInt(attr, 0);
2901 if (visibility != 0) {
2902 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2903 viewFlagMasks |= VISIBILITY_MASK;
2904 }
2905 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002906 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002907 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002908 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002909 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002910 final int layoutDirection = a.getInt(attr, -1);
2911 if (layoutDirection != -1) {
2912 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002913 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002914 // Set to default (LAYOUT_DIRECTION_INHERIT)
2915 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002916 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002917 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002918 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 case com.android.internal.R.styleable.View_drawingCacheQuality:
2920 final int cacheQuality = a.getInt(attr, 0);
2921 if (cacheQuality != 0) {
2922 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2923 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2924 }
2925 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002926 case com.android.internal.R.styleable.View_contentDescription:
2927 mContentDescription = a.getString(attr);
2928 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2930 if (!a.getBoolean(attr, true)) {
2931 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2932 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2933 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002934 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2936 if (!a.getBoolean(attr, true)) {
2937 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2938 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2939 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002940 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 case R.styleable.View_scrollbars:
2942 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2943 if (scrollbars != SCROLLBARS_NONE) {
2944 viewFlagValues |= scrollbars;
2945 viewFlagMasks |= SCROLLBARS_MASK;
2946 initializeScrollbars(a);
2947 }
2948 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002949 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002951 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2952 // Ignore the attribute starting with ICS
2953 break;
2954 }
2955 // With builds < ICS, fall through and apply fading edges
2956 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2958 if (fadingEdge != FADING_EDGE_NONE) {
2959 viewFlagValues |= fadingEdge;
2960 viewFlagMasks |= FADING_EDGE_MASK;
2961 initializeFadingEdge(a);
2962 }
2963 break;
2964 case R.styleable.View_scrollbarStyle:
2965 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2966 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2967 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2968 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2969 }
2970 break;
2971 case R.styleable.View_isScrollContainer:
2972 setScrollContainer = true;
2973 if (a.getBoolean(attr, false)) {
2974 setScrollContainer(true);
2975 }
2976 break;
2977 case com.android.internal.R.styleable.View_keepScreenOn:
2978 if (a.getBoolean(attr, false)) {
2979 viewFlagValues |= KEEP_SCREEN_ON;
2980 viewFlagMasks |= KEEP_SCREEN_ON;
2981 }
2982 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002983 case R.styleable.View_filterTouchesWhenObscured:
2984 if (a.getBoolean(attr, false)) {
2985 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2986 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2987 }
2988 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 case R.styleable.View_nextFocusLeft:
2990 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2991 break;
2992 case R.styleable.View_nextFocusRight:
2993 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2994 break;
2995 case R.styleable.View_nextFocusUp:
2996 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2997 break;
2998 case R.styleable.View_nextFocusDown:
2999 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3000 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003001 case R.styleable.View_nextFocusForward:
3002 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3003 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 case R.styleable.View_minWidth:
3005 mMinWidth = a.getDimensionPixelSize(attr, 0);
3006 break;
3007 case R.styleable.View_minHeight:
3008 mMinHeight = a.getDimensionPixelSize(attr, 0);
3009 break;
Romain Guy9a817362009-05-01 10:57:14 -07003010 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003011 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003012 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003013 + "be used within a restricted context");
3014 }
3015
Romain Guy9a817362009-05-01 10:57:14 -07003016 final String handlerName = a.getString(attr);
3017 if (handlerName != null) {
3018 setOnClickListener(new OnClickListener() {
3019 private Method mHandler;
3020
3021 public void onClick(View v) {
3022 if (mHandler == null) {
3023 try {
3024 mHandler = getContext().getClass().getMethod(handlerName,
3025 View.class);
3026 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003027 int id = getId();
3028 String idText = id == NO_ID ? "" : " with id '"
3029 + getContext().getResources().getResourceEntryName(
3030 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003031 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003032 handlerName + "(View) in the activity "
3033 + getContext().getClass() + " for onClick handler"
3034 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003035 }
3036 }
3037
3038 try {
3039 mHandler.invoke(getContext(), View.this);
3040 } catch (IllegalAccessException e) {
3041 throw new IllegalStateException("Could not execute non "
3042 + "public method of the activity", e);
3043 } catch (InvocationTargetException e) {
3044 throw new IllegalStateException("Could not execute "
3045 + "method of the activity", e);
3046 }
3047 }
3048 });
3049 }
3050 break;
Adam Powell637d3372010-08-25 14:37:03 -07003051 case R.styleable.View_overScrollMode:
3052 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3053 break;
Adam Powell20232d02010-12-08 21:08:53 -08003054 case R.styleable.View_verticalScrollbarPosition:
3055 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3056 break;
Romain Guy171c5922011-01-06 10:04:23 -08003057 case R.styleable.View_layerType:
3058 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3059 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003060 case R.styleable.View_textDirection:
3061 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3062 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 }
3064 }
3065
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003066 a.recycle();
3067
Adam Powell637d3372010-08-25 14:37:03 -07003068 setOverScrollMode(overScrollMode);
3069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 if (background != null) {
3071 setBackgroundDrawable(background);
3072 }
3073
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003074 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3075
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003076 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3077 // layout direction). Those cached values will be used later during padding resolution.
3078 mUserPaddingStart = startPadding;
3079 mUserPaddingEnd = endPadding;
3080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 if (padding >= 0) {
3082 leftPadding = padding;
3083 topPadding = padding;
3084 rightPadding = padding;
3085 bottomPadding = padding;
3086 }
3087
3088 // If the user specified the padding (either with android:padding or
3089 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3090 // use the default padding or the padding from the background drawable
3091 // (stored at this point in mPadding*)
3092 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3093 topPadding >= 0 ? topPadding : mPaddingTop,
3094 rightPadding >= 0 ? rightPadding : mPaddingRight,
3095 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3096
3097 if (viewFlagMasks != 0) {
3098 setFlags(viewFlagValues, viewFlagMasks);
3099 }
3100
3101 // Needs to be called after mViewFlags is set
3102 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3103 recomputePadding();
3104 }
3105
3106 if (x != 0 || y != 0) {
3107 scrollTo(x, y);
3108 }
3109
Chet Haase73066682010-11-29 15:55:32 -08003110 if (transformSet) {
3111 setTranslationX(tx);
3112 setTranslationY(ty);
3113 setRotation(rotation);
3114 setRotationX(rotationX);
3115 setRotationY(rotationY);
3116 setScaleX(sx);
3117 setScaleY(sy);
3118 }
3119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3121 setScrollContainer(true);
3122 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003123
3124 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
3126
3127 /**
3128 * Non-public constructor for use in testing
3129 */
3130 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003131 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 }
3133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 /**
3135 * <p>
3136 * Initializes the fading edges from a given set of styled attributes. This
3137 * method should be called by subclasses that need fading edges and when an
3138 * instance of these subclasses is created programmatically rather than
3139 * being inflated from XML. This method is automatically called when the XML
3140 * is inflated.
3141 * </p>
3142 *
3143 * @param a the styled attributes set to initialize the fading edges from
3144 */
3145 protected void initializeFadingEdge(TypedArray a) {
3146 initScrollCache();
3147
3148 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3149 R.styleable.View_fadingEdgeLength,
3150 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3151 }
3152
3153 /**
3154 * Returns the size of the vertical faded edges used to indicate that more
3155 * content in this view is visible.
3156 *
3157 * @return The size in pixels of the vertical faded edge or 0 if vertical
3158 * faded edges are not enabled for this view.
3159 * @attr ref android.R.styleable#View_fadingEdgeLength
3160 */
3161 public int getVerticalFadingEdgeLength() {
3162 if (isVerticalFadingEdgeEnabled()) {
3163 ScrollabilityCache cache = mScrollCache;
3164 if (cache != null) {
3165 return cache.fadingEdgeLength;
3166 }
3167 }
3168 return 0;
3169 }
3170
3171 /**
3172 * Set the size of the faded edge used to indicate that more content in this
3173 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003174 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3175 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3176 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 *
3178 * @param length The size in pixels of the faded edge used to indicate that more
3179 * content in this view is visible.
3180 */
3181 public void setFadingEdgeLength(int length) {
3182 initScrollCache();
3183 mScrollCache.fadingEdgeLength = length;
3184 }
3185
3186 /**
3187 * Returns the size of the horizontal faded edges used to indicate that more
3188 * content in this view is visible.
3189 *
3190 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3191 * faded edges are not enabled for this view.
3192 * @attr ref android.R.styleable#View_fadingEdgeLength
3193 */
3194 public int getHorizontalFadingEdgeLength() {
3195 if (isHorizontalFadingEdgeEnabled()) {
3196 ScrollabilityCache cache = mScrollCache;
3197 if (cache != null) {
3198 return cache.fadingEdgeLength;
3199 }
3200 }
3201 return 0;
3202 }
3203
3204 /**
3205 * Returns the width of the vertical scrollbar.
3206 *
3207 * @return The width in pixels of the vertical scrollbar or 0 if there
3208 * is no vertical scrollbar.
3209 */
3210 public int getVerticalScrollbarWidth() {
3211 ScrollabilityCache cache = mScrollCache;
3212 if (cache != null) {
3213 ScrollBarDrawable scrollBar = cache.scrollBar;
3214 if (scrollBar != null) {
3215 int size = scrollBar.getSize(true);
3216 if (size <= 0) {
3217 size = cache.scrollBarSize;
3218 }
3219 return size;
3220 }
3221 return 0;
3222 }
3223 return 0;
3224 }
3225
3226 /**
3227 * Returns the height of the horizontal scrollbar.
3228 *
3229 * @return The height in pixels of the horizontal scrollbar or 0 if
3230 * there is no horizontal scrollbar.
3231 */
3232 protected int getHorizontalScrollbarHeight() {
3233 ScrollabilityCache cache = mScrollCache;
3234 if (cache != null) {
3235 ScrollBarDrawable scrollBar = cache.scrollBar;
3236 if (scrollBar != null) {
3237 int size = scrollBar.getSize(false);
3238 if (size <= 0) {
3239 size = cache.scrollBarSize;
3240 }
3241 return size;
3242 }
3243 return 0;
3244 }
3245 return 0;
3246 }
3247
3248 /**
3249 * <p>
3250 * Initializes the scrollbars from a given set of styled attributes. This
3251 * method should be called by subclasses that need scrollbars and when an
3252 * instance of these subclasses is created programmatically rather than
3253 * being inflated from XML. This method is automatically called when the XML
3254 * is inflated.
3255 * </p>
3256 *
3257 * @param a the styled attributes set to initialize the scrollbars from
3258 */
3259 protected void initializeScrollbars(TypedArray a) {
3260 initScrollCache();
3261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003263
Mike Cleronf116bf82009-09-27 19:14:12 -07003264 if (scrollabilityCache.scrollBar == null) {
3265 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3266 }
Joe Malin32736f02011-01-19 16:14:20 -08003267
Romain Guy8bda2482010-03-02 11:42:11 -08003268 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269
Mike Cleronf116bf82009-09-27 19:14:12 -07003270 if (!fadeScrollbars) {
3271 scrollabilityCache.state = ScrollabilityCache.ON;
3272 }
3273 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003274
3275
Mike Cleronf116bf82009-09-27 19:14:12 -07003276 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3277 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3278 .getScrollBarFadeDuration());
3279 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3280 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3281 ViewConfiguration.getScrollDefaultDelay());
3282
Joe Malin32736f02011-01-19 16:14:20 -08003283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3285 com.android.internal.R.styleable.View_scrollbarSize,
3286 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3287
3288 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3289 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3290
3291 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3292 if (thumb != null) {
3293 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3294 }
3295
3296 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3297 false);
3298 if (alwaysDraw) {
3299 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3300 }
3301
3302 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3303 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3304
3305 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3306 if (thumb != null) {
3307 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3308 }
3309
3310 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3311 false);
3312 if (alwaysDraw) {
3313 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3314 }
3315
3316 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003317 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 }
3319
3320 /**
3321 * <p>
3322 * Initalizes the scrollability cache if necessary.
3323 * </p>
3324 */
3325 private void initScrollCache() {
3326 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003327 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 }
3329 }
3330
3331 /**
Adam Powell20232d02010-12-08 21:08:53 -08003332 * Set the position of the vertical scroll bar. Should be one of
3333 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3334 * {@link #SCROLLBAR_POSITION_RIGHT}.
3335 *
3336 * @param position Where the vertical scroll bar should be positioned.
3337 */
3338 public void setVerticalScrollbarPosition(int position) {
3339 if (mVerticalScrollbarPosition != position) {
3340 mVerticalScrollbarPosition = position;
3341 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003342 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003343 }
3344 }
3345
3346 /**
3347 * @return The position where the vertical scroll bar will show, if applicable.
3348 * @see #setVerticalScrollbarPosition(int)
3349 */
3350 public int getVerticalScrollbarPosition() {
3351 return mVerticalScrollbarPosition;
3352 }
3353
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003354 ListenerInfo getListenerInfo() {
3355 if (mListenerInfo != null) {
3356 return mListenerInfo;
3357 }
3358 mListenerInfo = new ListenerInfo();
3359 return mListenerInfo;
3360 }
3361
Adam Powell20232d02010-12-08 21:08:53 -08003362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 * Register a callback to be invoked when focus of this view changed.
3364 *
3365 * @param l The callback that will run.
3366 */
3367 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003368 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 }
3370
3371 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003372 * Add a listener that will be called when the bounds of the view change due to
3373 * layout processing.
3374 *
3375 * @param listener The listener that will be called when layout bounds change.
3376 */
3377 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003378 ListenerInfo li = getListenerInfo();
3379 if (li.mOnLayoutChangeListeners == null) {
3380 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003381 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003382 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3383 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003384 }
Chet Haase21cd1382010-09-01 17:42:29 -07003385 }
3386
3387 /**
3388 * Remove a listener for layout changes.
3389 *
3390 * @param listener The listener for layout bounds change.
3391 */
3392 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003393 ListenerInfo li = mListenerInfo;
3394 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003395 return;
3396 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003397 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003398 }
3399
3400 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003401 * Add a listener for attach state changes.
3402 *
3403 * This listener will be called whenever this view is attached or detached
3404 * from a window. Remove the listener using
3405 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3406 *
3407 * @param listener Listener to attach
3408 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3409 */
3410 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003411 ListenerInfo li = getListenerInfo();
3412 if (li.mOnAttachStateChangeListeners == null) {
3413 li.mOnAttachStateChangeListeners
3414 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003415 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003416 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003417 }
3418
3419 /**
3420 * Remove a listener for attach state changes. The listener will receive no further
3421 * notification of window attach/detach events.
3422 *
3423 * @param listener Listener to remove
3424 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3425 */
3426 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003427 ListenerInfo li = mListenerInfo;
3428 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003429 return;
3430 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003431 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003432 }
3433
3434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 * Returns the focus-change callback registered for this view.
3436 *
3437 * @return The callback, or null if one is not registered.
3438 */
3439 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003440 ListenerInfo li = mListenerInfo;
3441 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 }
3443
3444 /**
3445 * Register a callback to be invoked when this view is clicked. If this view is not
3446 * clickable, it becomes clickable.
3447 *
3448 * @param l The callback that will run
3449 *
3450 * @see #setClickable(boolean)
3451 */
3452 public void setOnClickListener(OnClickListener l) {
3453 if (!isClickable()) {
3454 setClickable(true);
3455 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003456 getListenerInfo().mOnClickListener = l;
3457 }
3458
3459 /**
3460 * Return whether this view has an attached OnClickListener. Returns
3461 * true if there is a listener, false if there is none.
3462 */
3463 public boolean hasOnClickListeners() {
3464 ListenerInfo li = mListenerInfo;
3465 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 }
3467
3468 /**
3469 * Register a callback to be invoked when this view is clicked and held. If this view is not
3470 * long clickable, it becomes long clickable.
3471 *
3472 * @param l The callback that will run
3473 *
3474 * @see #setLongClickable(boolean)
3475 */
3476 public void setOnLongClickListener(OnLongClickListener l) {
3477 if (!isLongClickable()) {
3478 setLongClickable(true);
3479 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003480 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 }
3482
3483 /**
3484 * Register a callback to be invoked when the context menu for this view is
3485 * being built. If this view is not long clickable, it becomes long clickable.
3486 *
3487 * @param l The callback that will run
3488 *
3489 */
3490 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3491 if (!isLongClickable()) {
3492 setLongClickable(true);
3493 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003494 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 }
3496
3497 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003498 * Call this view's OnClickListener, if it is defined. Performs all normal
3499 * actions associated with clicking: reporting accessibility event, playing
3500 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 *
3502 * @return True there was an assigned OnClickListener that was called, false
3503 * otherwise is returned.
3504 */
3505 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003506 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3507
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003508 ListenerInfo li = mListenerInfo;
3509 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003511 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 return true;
3513 }
3514
3515 return false;
3516 }
3517
3518 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003519 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3520 * this only calls the listener, and does not do any associated clicking
3521 * actions like reporting an accessibility event.
3522 *
3523 * @return True there was an assigned OnClickListener that was called, false
3524 * otherwise is returned.
3525 */
3526 public boolean callOnClick() {
3527 ListenerInfo li = mListenerInfo;
3528 if (li != null && li.mOnClickListener != null) {
3529 li.mOnClickListener.onClick(this);
3530 return true;
3531 }
3532 return false;
3533 }
3534
3535 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003536 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3537 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003539 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 */
3541 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003542 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003545 ListenerInfo li = mListenerInfo;
3546 if (li != null && li.mOnLongClickListener != null) {
3547 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 }
3549 if (!handled) {
3550 handled = showContextMenu();
3551 }
3552 if (handled) {
3553 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3554 }
3555 return handled;
3556 }
3557
3558 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003559 * Performs button-related actions during a touch down event.
3560 *
3561 * @param event The event.
3562 * @return True if the down was consumed.
3563 *
3564 * @hide
3565 */
3566 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3567 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3568 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3569 return true;
3570 }
3571 }
3572 return false;
3573 }
3574
3575 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 * Bring up the context menu for this view.
3577 *
3578 * @return Whether a context menu was displayed.
3579 */
3580 public boolean showContextMenu() {
3581 return getParent().showContextMenuForChild(this);
3582 }
3583
3584 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003585 * Bring up the context menu for this view, referring to the item under the specified point.
3586 *
3587 * @param x The referenced x coordinate.
3588 * @param y The referenced y coordinate.
3589 * @param metaState The keyboard modifiers that were pressed.
3590 * @return Whether a context menu was displayed.
3591 *
3592 * @hide
3593 */
3594 public boolean showContextMenu(float x, float y, int metaState) {
3595 return showContextMenu();
3596 }
3597
3598 /**
Adam Powell6e346362010-07-23 10:18:23 -07003599 * Start an action mode.
3600 *
3601 * @param callback Callback that will control the lifecycle of the action mode
3602 * @return The new action mode if it is started, null otherwise
3603 *
3604 * @see ActionMode
3605 */
3606 public ActionMode startActionMode(ActionMode.Callback callback) {
3607 return getParent().startActionModeForChild(this, callback);
3608 }
3609
3610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 * Register a callback to be invoked when a key is pressed in this view.
3612 * @param l the key listener to attach to this view
3613 */
3614 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003615 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 }
3617
3618 /**
3619 * Register a callback to be invoked when a touch event is sent to this view.
3620 * @param l the touch listener to attach to this view
3621 */
3622 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003623 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 }
3625
3626 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003627 * Register a callback to be invoked when a generic motion event is sent to this view.
3628 * @param l the generic motion listener to attach to this view
3629 */
3630 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003631 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003632 }
3633
3634 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003635 * Register a callback to be invoked when a hover event is sent to this view.
3636 * @param l the hover listener to attach to this view
3637 */
3638 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003639 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003640 }
3641
3642 /**
Joe Malin32736f02011-01-19 16:14:20 -08003643 * Register a drag event listener callback object for this View. The parameter is
3644 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3645 * View, the system calls the
3646 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3647 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003648 */
3649 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003650 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003651 }
3652
3653 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003654 * Give this view focus. This will cause
3655 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 *
3657 * Note: this does not check whether this {@link View} should get focus, it just
3658 * gives it focus no matter what. It should only be called internally by framework
3659 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3660 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003661 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3662 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 * focus moved when requestFocus() is called. It may not always
3664 * apply, in which case use the default View.FOCUS_DOWN.
3665 * @param previouslyFocusedRect The rectangle of the view that had focus
3666 * prior in this View's coordinate system.
3667 */
3668 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3669 if (DBG) {
3670 System.out.println(this + " requestFocus()");
3671 }
3672
3673 if ((mPrivateFlags & FOCUSED) == 0) {
3674 mPrivateFlags |= FOCUSED;
3675
3676 if (mParent != null) {
3677 mParent.requestChildFocus(this, this);
3678 }
3679
3680 onFocusChanged(true, direction, previouslyFocusedRect);
3681 refreshDrawableState();
3682 }
3683 }
3684
3685 /**
3686 * Request that a rectangle of this view be visible on the screen,
3687 * scrolling if necessary just enough.
3688 *
3689 * <p>A View should call this if it maintains some notion of which part
3690 * of its content is interesting. For example, a text editing view
3691 * should call this when its cursor moves.
3692 *
3693 * @param rectangle The rectangle.
3694 * @return Whether any parent scrolled.
3695 */
3696 public boolean requestRectangleOnScreen(Rect rectangle) {
3697 return requestRectangleOnScreen(rectangle, false);
3698 }
3699
3700 /**
3701 * Request that a rectangle of this view be visible on the screen,
3702 * scrolling if necessary just enough.
3703 *
3704 * <p>A View should call this if it maintains some notion of which part
3705 * of its content is interesting. For example, a text editing view
3706 * should call this when its cursor moves.
3707 *
3708 * <p>When <code>immediate</code> is set to true, scrolling will not be
3709 * animated.
3710 *
3711 * @param rectangle The rectangle.
3712 * @param immediate True to forbid animated scrolling, false otherwise
3713 * @return Whether any parent scrolled.
3714 */
3715 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3716 View child = this;
3717 ViewParent parent = mParent;
3718 boolean scrolled = false;
3719 while (parent != null) {
3720 scrolled |= parent.requestChildRectangleOnScreen(child,
3721 rectangle, immediate);
3722
3723 // offset rect so next call has the rectangle in the
3724 // coordinate system of its direct child.
3725 rectangle.offset(child.getLeft(), child.getTop());
3726 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3727
3728 if (!(parent instanceof View)) {
3729 break;
3730 }
Romain Guy8506ab42009-06-11 17:35:47 -07003731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 child = (View) parent;
3733 parent = child.getParent();
3734 }
3735 return scrolled;
3736 }
3737
3738 /**
3739 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003740 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 */
3742 public void clearFocus() {
3743 if (DBG) {
3744 System.out.println(this + " clearFocus()");
3745 }
3746
3747 if ((mPrivateFlags & FOCUSED) != 0) {
3748 mPrivateFlags &= ~FOCUSED;
3749
3750 if (mParent != null) {
3751 mParent.clearChildFocus(this);
3752 }
3753
3754 onFocusChanged(false, 0, null);
3755 refreshDrawableState();
3756 }
3757 }
3758
3759 /**
3760 * Called to clear the focus of a view that is about to be removed.
3761 * Doesn't call clearChildFocus, which prevents this view from taking
3762 * focus again before it has been removed from the parent
3763 */
3764 void clearFocusForRemoval() {
3765 if ((mPrivateFlags & FOCUSED) != 0) {
3766 mPrivateFlags &= ~FOCUSED;
3767
3768 onFocusChanged(false, 0, null);
3769 refreshDrawableState();
3770 }
3771 }
3772
3773 /**
3774 * Called internally by the view system when a new view is getting focus.
3775 * This is what clears the old focus.
3776 */
3777 void unFocus() {
3778 if (DBG) {
3779 System.out.println(this + " unFocus()");
3780 }
3781
3782 if ((mPrivateFlags & FOCUSED) != 0) {
3783 mPrivateFlags &= ~FOCUSED;
3784
3785 onFocusChanged(false, 0, null);
3786 refreshDrawableState();
3787 }
3788 }
3789
3790 /**
3791 * Returns true if this view has focus iteself, or is the ancestor of the
3792 * view that has focus.
3793 *
3794 * @return True if this view has or contains focus, false otherwise.
3795 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003796 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 public boolean hasFocus() {
3798 return (mPrivateFlags & FOCUSED) != 0;
3799 }
3800
3801 /**
3802 * Returns true if this view is focusable or if it contains a reachable View
3803 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3804 * is a View whose parents do not block descendants focus.
3805 *
3806 * Only {@link #VISIBLE} views are considered focusable.
3807 *
3808 * @return True if the view is focusable or if the view contains a focusable
3809 * View, false otherwise.
3810 *
3811 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3812 */
3813 public boolean hasFocusable() {
3814 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3815 }
3816
3817 /**
3818 * Called by the view system when the focus state of this view changes.
3819 * When the focus change event is caused by directional navigation, direction
3820 * and previouslyFocusedRect provide insight into where the focus is coming from.
3821 * When overriding, be sure to call up through to the super class so that
3822 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003823 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 * @param gainFocus True if the View has focus; false otherwise.
3825 * @param direction The direction focus has moved when requestFocus()
3826 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003827 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3828 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3829 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3831 * system, of the previously focused view. If applicable, this will be
3832 * passed in as finer grained information about where the focus is coming
3833 * from (in addition to direction). Will be <code>null</code> otherwise.
3834 */
3835 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003836 if (gainFocus) {
3837 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3838 }
3839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 InputMethodManager imm = InputMethodManager.peekInstance();
3841 if (!gainFocus) {
3842 if (isPressed()) {
3843 setPressed(false);
3844 }
3845 if (imm != null && mAttachInfo != null
3846 && mAttachInfo.mHasWindowFocus) {
3847 imm.focusOut(this);
3848 }
Romain Guya2431d02009-04-30 16:30:00 -07003849 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 } else if (imm != null && mAttachInfo != null
3851 && mAttachInfo.mHasWindowFocus) {
3852 imm.focusIn(this);
3853 }
Romain Guy8506ab42009-06-11 17:35:47 -07003854
Romain Guy0fd89bf2011-01-26 15:41:30 -08003855 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003856 ListenerInfo li = mListenerInfo;
3857 if (li != null && li.mOnFocusChangeListener != null) {
3858 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 }
Joe Malin32736f02011-01-19 16:14:20 -08003860
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003861 if (mAttachInfo != null) {
3862 mAttachInfo.mKeyDispatchState.reset(this);
3863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 }
3865
3866 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003867 * Sends an accessibility event of the given type. If accessiiblity is
3868 * not enabled this method has no effect. The default implementation calls
3869 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3870 * to populate information about the event source (this View), then calls
3871 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3872 * populate the text content of the event source including its descendants,
3873 * and last calls
3874 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3875 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003876 * <p>
3877 * If an {@link AccessibilityDelegate} has been specified via calling
3878 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3879 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3880 * responsible for handling this call.
3881 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003882 *
Scott Mainb303d832011-10-12 16:45:18 -07003883 * @param eventType The type of the event to send, as defined by several types from
3884 * {@link android.view.accessibility.AccessibilityEvent}, such as
3885 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3886 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003887 *
3888 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3889 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3890 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003891 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003892 */
3893 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003894 if (mAccessibilityDelegate != null) {
3895 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3896 } else {
3897 sendAccessibilityEventInternal(eventType);
3898 }
3899 }
3900
3901 /**
3902 * @see #sendAccessibilityEvent(int)
3903 *
3904 * Note: Called from the default {@link AccessibilityDelegate}.
3905 */
3906 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003907 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3908 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3909 }
3910 }
3911
3912 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003913 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3914 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003915 * perform a check whether accessibility is enabled.
3916 * <p>
3917 * If an {@link AccessibilityDelegate} has been specified via calling
3918 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3919 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3920 * is responsible for handling this call.
3921 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003922 *
3923 * @param event The event to send.
3924 *
3925 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003926 */
3927 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003928 if (mAccessibilityDelegate != null) {
3929 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3930 } else {
3931 sendAccessibilityEventUncheckedInternal(event);
3932 }
3933 }
3934
3935 /**
3936 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3937 *
3938 * Note: Called from the default {@link AccessibilityDelegate}.
3939 */
3940 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003941 if (!isShown()) {
3942 return;
3943 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003944 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003945 // Only a subset of accessibility events populates text content.
3946 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3947 dispatchPopulateAccessibilityEvent(event);
3948 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003949 // In the beginning we called #isShown(), so we know that getParent() is not null.
3950 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003951 }
3952
3953 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003954 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3955 * to its children for adding their text content to the event. Note that the
3956 * event text is populated in a separate dispatch path since we add to the
3957 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003958 * A typical implementation will call
3959 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3960 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3961 * on each child. Override this method if custom population of the event text
3962 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003963 * <p>
3964 * If an {@link AccessibilityDelegate} has been specified via calling
3965 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3966 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3967 * is responsible for handling this call.
3968 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003969 * <p>
3970 * <em>Note:</em> Accessibility events of certain types are not dispatched for
3971 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
3972 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003973 *
3974 * @param event The event.
3975 *
3976 * @return True if the event population was completed.
3977 */
3978 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003979 if (mAccessibilityDelegate != null) {
3980 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3981 } else {
3982 return dispatchPopulateAccessibilityEventInternal(event);
3983 }
3984 }
3985
3986 /**
3987 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3988 *
3989 * Note: Called from the default {@link AccessibilityDelegate}.
3990 */
3991 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003992 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003993 return false;
3994 }
3995
3996 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003997 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003998 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07003999 * text content. While this method is free to modify event
4000 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004001 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4002 * <p>
4003 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004004 * to the text added by the super implementation:
4005 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004006 * super.onPopulateAccessibilityEvent(event);
4007 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4008 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4009 * mCurrentDate.getTimeInMillis(), flags);
4010 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004011 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004012 * <p>
4013 * If an {@link AccessibilityDelegate} has been specified via calling
4014 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4015 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4016 * is responsible for handling this call.
4017 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004018 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4019 * information to the event, in case the default implementation has basic information to add.
4020 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004021 *
4022 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004023 *
4024 * @see #sendAccessibilityEvent(int)
4025 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004026 */
4027 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004028 if (mAccessibilityDelegate != null) {
4029 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4030 } else {
4031 onPopulateAccessibilityEventInternal(event);
4032 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004033 }
4034
4035 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004036 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4037 *
4038 * Note: Called from the default {@link AccessibilityDelegate}.
4039 */
4040 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4041
4042 }
4043
4044 /**
4045 * Initializes an {@link AccessibilityEvent} with information about
4046 * this View which is the event source. In other words, the source of
4047 * an accessibility event is the view whose state change triggered firing
4048 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004049 * <p>
4050 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004051 * to properties set by the super implementation:
4052 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4053 * super.onInitializeAccessibilityEvent(event);
4054 * event.setPassword(true);
4055 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004056 * <p>
4057 * If an {@link AccessibilityDelegate} has been specified via calling
4058 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4059 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4060 * is responsible for handling this call.
4061 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004062 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4063 * information to the event, in case the default implementation has basic information to add.
4064 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004065 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004066 *
4067 * @see #sendAccessibilityEvent(int)
4068 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4069 */
4070 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004071 if (mAccessibilityDelegate != null) {
4072 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4073 } else {
4074 onInitializeAccessibilityEventInternal(event);
4075 }
4076 }
4077
4078 /**
4079 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4080 *
4081 * Note: Called from the default {@link AccessibilityDelegate}.
4082 */
4083 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004084 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07004085 event.setClassName(getClass().getName());
4086 event.setPackageName(getContext().getPackageName());
4087 event.setEnabled(isEnabled());
4088 event.setContentDescription(mContentDescription);
4089
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004090 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4091 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4092 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4093 FOCUSABLES_ALL);
4094 event.setItemCount(focusablesTempList.size());
4095 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4096 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004097 }
4098 }
4099
4100 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004101 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4102 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4103 * This method is responsible for obtaining an accessibility node info from a
4104 * pool of reusable instances and calling
4105 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4106 * initialize the former.
4107 * <p>
4108 * Note: The client is responsible for recycling the obtained instance by calling
4109 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4110 * </p>
4111 * @return A populated {@link AccessibilityNodeInfo}.
4112 *
4113 * @see AccessibilityNodeInfo
4114 */
4115 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4116 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4117 onInitializeAccessibilityNodeInfo(info);
4118 return info;
4119 }
4120
4121 /**
4122 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4123 * The base implementation sets:
4124 * <ul>
4125 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004126 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4127 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004128 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4129 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4130 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4131 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4132 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4133 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4134 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4135 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4136 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4137 * </ul>
4138 * <p>
4139 * Subclasses should override this method, call the super implementation,
4140 * and set additional attributes.
4141 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004142 * <p>
4143 * If an {@link AccessibilityDelegate} has been specified via calling
4144 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4145 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4146 * is responsible for handling this call.
4147 * </p>
4148 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004149 * @param info The instance to initialize.
4150 */
4151 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004152 if (mAccessibilityDelegate != null) {
4153 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4154 } else {
4155 onInitializeAccessibilityNodeInfoInternal(info);
4156 }
4157 }
4158
4159 /**
4160 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4161 *
4162 * Note: Called from the default {@link AccessibilityDelegate}.
4163 */
4164 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004165 Rect bounds = mAttachInfo.mTmpInvalRect;
4166 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004167 info.setBoundsInParent(bounds);
4168
4169 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4170 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004171 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004172 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4173 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004174
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004175 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4176 ViewParent parent = getParent();
4177 if (parent instanceof View) {
4178 View parentView = (View) parent;
4179 info.setParent(parentView);
4180 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004181 }
4182
4183 info.setPackageName(mContext.getPackageName());
4184 info.setClassName(getClass().getName());
4185 info.setContentDescription(getContentDescription());
4186
4187 info.setEnabled(isEnabled());
4188 info.setClickable(isClickable());
4189 info.setFocusable(isFocusable());
4190 info.setFocused(isFocused());
4191 info.setSelected(isSelected());
4192 info.setLongClickable(isLongClickable());
4193
4194 // TODO: These make sense only if we are in an AdapterView but all
4195 // views can be selected. Maybe from accessiiblity perspective
4196 // we should report as selectable view in an AdapterView.
4197 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4198 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4199
4200 if (isFocusable()) {
4201 if (isFocused()) {
4202 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4203 } else {
4204 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4205 }
4206 }
4207 }
4208
4209 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004210 * Sets a delegate for implementing accessibility support via compositon as
4211 * opposed to inheritance. The delegate's primary use is for implementing
4212 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4213 *
4214 * @param delegate The delegate instance.
4215 *
4216 * @see AccessibilityDelegate
4217 */
4218 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4219 mAccessibilityDelegate = delegate;
4220 }
4221
4222 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004223 * Gets the unique identifier of this view on the screen for accessibility purposes.
4224 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4225 *
4226 * @return The view accessibility id.
4227 *
4228 * @hide
4229 */
4230 public int getAccessibilityViewId() {
4231 if (mAccessibilityViewId == NO_ID) {
4232 mAccessibilityViewId = sNextAccessibilityViewId++;
4233 }
4234 return mAccessibilityViewId;
4235 }
4236
4237 /**
4238 * Gets the unique identifier of the window in which this View reseides.
4239 *
4240 * @return The window accessibility id.
4241 *
4242 * @hide
4243 */
4244 public int getAccessibilityWindowId() {
4245 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4246 }
4247
4248 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004249 * Gets the {@link View} description. It briefly describes the view and is
4250 * primarily used for accessibility support. Set this property to enable
4251 * better accessibility support for your application. This is especially
4252 * true for views that do not have textual representation (For example,
4253 * ImageButton).
4254 *
4255 * @return The content descriptiopn.
4256 *
4257 * @attr ref android.R.styleable#View_contentDescription
4258 */
4259 public CharSequence getContentDescription() {
4260 return mContentDescription;
4261 }
4262
4263 /**
4264 * Sets the {@link View} description. It briefly describes the view and is
4265 * primarily used for accessibility support. Set this property to enable
4266 * better accessibility support for your application. This is especially
4267 * true for views that do not have textual representation (For example,
4268 * ImageButton).
4269 *
4270 * @param contentDescription The content description.
4271 *
4272 * @attr ref android.R.styleable#View_contentDescription
4273 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004274 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004275 public void setContentDescription(CharSequence contentDescription) {
4276 mContentDescription = contentDescription;
4277 }
4278
4279 /**
Romain Guya2431d02009-04-30 16:30:00 -07004280 * Invoked whenever this view loses focus, either by losing window focus or by losing
4281 * focus within its window. This method can be used to clear any state tied to the
4282 * focus. For instance, if a button is held pressed with the trackball and the window
4283 * loses focus, this method can be used to cancel the press.
4284 *
4285 * Subclasses of View overriding this method should always call super.onFocusLost().
4286 *
4287 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004288 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004289 *
4290 * @hide pending API council approval
4291 */
4292 protected void onFocusLost() {
4293 resetPressedState();
4294 }
4295
4296 private void resetPressedState() {
4297 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4298 return;
4299 }
4300
4301 if (isPressed()) {
4302 setPressed(false);
4303
4304 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004305 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004306 }
4307 }
4308 }
4309
4310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 * Returns true if this view has focus
4312 *
4313 * @return True if this view has focus, false otherwise.
4314 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004315 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 public boolean isFocused() {
4317 return (mPrivateFlags & FOCUSED) != 0;
4318 }
4319
4320 /**
4321 * Find the view in the hierarchy rooted at this view that currently has
4322 * focus.
4323 *
4324 * @return The view that currently has focus, or null if no focused view can
4325 * be found.
4326 */
4327 public View findFocus() {
4328 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4329 }
4330
4331 /**
4332 * Change whether this view is one of the set of scrollable containers in
4333 * its window. This will be used to determine whether the window can
4334 * resize or must pan when a soft input area is open -- scrollable
4335 * containers allow the window to use resize mode since the container
4336 * will appropriately shrink.
4337 */
4338 public void setScrollContainer(boolean isScrollContainer) {
4339 if (isScrollContainer) {
4340 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4341 mAttachInfo.mScrollContainers.add(this);
4342 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4343 }
4344 mPrivateFlags |= SCROLL_CONTAINER;
4345 } else {
4346 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4347 mAttachInfo.mScrollContainers.remove(this);
4348 }
4349 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4350 }
4351 }
4352
4353 /**
4354 * Returns the quality of the drawing cache.
4355 *
4356 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4357 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4358 *
4359 * @see #setDrawingCacheQuality(int)
4360 * @see #setDrawingCacheEnabled(boolean)
4361 * @see #isDrawingCacheEnabled()
4362 *
4363 * @attr ref android.R.styleable#View_drawingCacheQuality
4364 */
4365 public int getDrawingCacheQuality() {
4366 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4367 }
4368
4369 /**
4370 * Set the drawing cache quality of this view. This value is used only when the
4371 * drawing cache is enabled
4372 *
4373 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4374 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4375 *
4376 * @see #getDrawingCacheQuality()
4377 * @see #setDrawingCacheEnabled(boolean)
4378 * @see #isDrawingCacheEnabled()
4379 *
4380 * @attr ref android.R.styleable#View_drawingCacheQuality
4381 */
4382 public void setDrawingCacheQuality(int quality) {
4383 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4384 }
4385
4386 /**
4387 * Returns whether the screen should remain on, corresponding to the current
4388 * value of {@link #KEEP_SCREEN_ON}.
4389 *
4390 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4391 *
4392 * @see #setKeepScreenOn(boolean)
4393 *
4394 * @attr ref android.R.styleable#View_keepScreenOn
4395 */
4396 public boolean getKeepScreenOn() {
4397 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4398 }
4399
4400 /**
4401 * Controls whether the screen should remain on, modifying the
4402 * value of {@link #KEEP_SCREEN_ON}.
4403 *
4404 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4405 *
4406 * @see #getKeepScreenOn()
4407 *
4408 * @attr ref android.R.styleable#View_keepScreenOn
4409 */
4410 public void setKeepScreenOn(boolean keepScreenOn) {
4411 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4412 }
4413
4414 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004415 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4416 * @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 -08004417 *
4418 * @attr ref android.R.styleable#View_nextFocusLeft
4419 */
4420 public int getNextFocusLeftId() {
4421 return mNextFocusLeftId;
4422 }
4423
4424 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004425 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4426 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4427 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 *
4429 * @attr ref android.R.styleable#View_nextFocusLeft
4430 */
4431 public void setNextFocusLeftId(int nextFocusLeftId) {
4432 mNextFocusLeftId = nextFocusLeftId;
4433 }
4434
4435 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004436 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4437 * @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 -08004438 *
4439 * @attr ref android.R.styleable#View_nextFocusRight
4440 */
4441 public int getNextFocusRightId() {
4442 return mNextFocusRightId;
4443 }
4444
4445 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004446 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4447 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4448 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 *
4450 * @attr ref android.R.styleable#View_nextFocusRight
4451 */
4452 public void setNextFocusRightId(int nextFocusRightId) {
4453 mNextFocusRightId = nextFocusRightId;
4454 }
4455
4456 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004457 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4458 * @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 -08004459 *
4460 * @attr ref android.R.styleable#View_nextFocusUp
4461 */
4462 public int getNextFocusUpId() {
4463 return mNextFocusUpId;
4464 }
4465
4466 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004467 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4468 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4469 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 *
4471 * @attr ref android.R.styleable#View_nextFocusUp
4472 */
4473 public void setNextFocusUpId(int nextFocusUpId) {
4474 mNextFocusUpId = nextFocusUpId;
4475 }
4476
4477 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004478 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4479 * @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 -08004480 *
4481 * @attr ref android.R.styleable#View_nextFocusDown
4482 */
4483 public int getNextFocusDownId() {
4484 return mNextFocusDownId;
4485 }
4486
4487 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004488 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4489 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4490 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 *
4492 * @attr ref android.R.styleable#View_nextFocusDown
4493 */
4494 public void setNextFocusDownId(int nextFocusDownId) {
4495 mNextFocusDownId = nextFocusDownId;
4496 }
4497
4498 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004499 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4500 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4501 *
4502 * @attr ref android.R.styleable#View_nextFocusForward
4503 */
4504 public int getNextFocusForwardId() {
4505 return mNextFocusForwardId;
4506 }
4507
4508 /**
4509 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4510 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4511 * decide automatically.
4512 *
4513 * @attr ref android.R.styleable#View_nextFocusForward
4514 */
4515 public void setNextFocusForwardId(int nextFocusForwardId) {
4516 mNextFocusForwardId = nextFocusForwardId;
4517 }
4518
4519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 * Returns the visibility of this view and all of its ancestors
4521 *
4522 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4523 */
4524 public boolean isShown() {
4525 View current = this;
4526 //noinspection ConstantConditions
4527 do {
4528 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4529 return false;
4530 }
4531 ViewParent parent = current.mParent;
4532 if (parent == null) {
4533 return false; // We are not attached to the view root
4534 }
4535 if (!(parent instanceof View)) {
4536 return true;
4537 }
4538 current = (View) parent;
4539 } while (current != null);
4540
4541 return false;
4542 }
4543
4544 /**
4545 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4546 * is set
4547 *
4548 * @param insets Insets for system windows
4549 *
4550 * @return True if this view applied the insets, false otherwise
4551 */
4552 protected boolean fitSystemWindows(Rect insets) {
4553 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4554 mPaddingLeft = insets.left;
4555 mPaddingTop = insets.top;
4556 mPaddingRight = insets.right;
4557 mPaddingBottom = insets.bottom;
4558 requestLayout();
4559 return true;
4560 }
4561 return false;
4562 }
4563
4564 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004565 * Set whether or not this view should account for system screen decorations
4566 * such as the status bar and inset its content. This allows this view to be
4567 * positioned in absolute screen coordinates and remain visible to the user.
4568 *
4569 * <p>This should only be used by top-level window decor views.
4570 *
4571 * @param fitSystemWindows true to inset content for system screen decorations, false for
4572 * default behavior.
4573 *
4574 * @attr ref android.R.styleable#View_fitsSystemWindows
4575 */
4576 public void setFitsSystemWindows(boolean fitSystemWindows) {
4577 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4578 }
4579
4580 /**
4581 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4582 * will account for system screen decorations such as the status bar and inset its
4583 * content. This allows the view to be positioned in absolute screen coordinates
4584 * and remain visible to the user.
4585 *
4586 * @return true if this view will adjust its content bounds for system screen decorations.
4587 *
4588 * @attr ref android.R.styleable#View_fitsSystemWindows
4589 */
4590 public boolean fitsSystemWindows() {
4591 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4592 }
4593
4594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 * Returns the visibility status for this view.
4596 *
4597 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4598 * @attr ref android.R.styleable#View_visibility
4599 */
4600 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004601 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4602 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4603 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 })
4605 public int getVisibility() {
4606 return mViewFlags & VISIBILITY_MASK;
4607 }
4608
4609 /**
4610 * Set the enabled state of this view.
4611 *
4612 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4613 * @attr ref android.R.styleable#View_visibility
4614 */
4615 @RemotableViewMethod
4616 public void setVisibility(int visibility) {
4617 setFlags(visibility, VISIBILITY_MASK);
4618 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4619 }
4620
4621 /**
4622 * Returns the enabled status for this view. The interpretation of the
4623 * enabled state varies by subclass.
4624 *
4625 * @return True if this view is enabled, false otherwise.
4626 */
4627 @ViewDebug.ExportedProperty
4628 public boolean isEnabled() {
4629 return (mViewFlags & ENABLED_MASK) == ENABLED;
4630 }
4631
4632 /**
4633 * Set the enabled state of this view. The interpretation of the enabled
4634 * state varies by subclass.
4635 *
4636 * @param enabled True if this view is enabled, false otherwise.
4637 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004638 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004640 if (enabled == isEnabled()) return;
4641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4643
4644 /*
4645 * The View most likely has to change its appearance, so refresh
4646 * the drawable state.
4647 */
4648 refreshDrawableState();
4649
4650 // Invalidate too, since the default behavior for views is to be
4651 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004652 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 }
4654
4655 /**
4656 * Set whether this view can receive the focus.
4657 *
4658 * Setting this to false will also ensure that this view is not focusable
4659 * in touch mode.
4660 *
4661 * @param focusable If true, this view can receive the focus.
4662 *
4663 * @see #setFocusableInTouchMode(boolean)
4664 * @attr ref android.R.styleable#View_focusable
4665 */
4666 public void setFocusable(boolean focusable) {
4667 if (!focusable) {
4668 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4669 }
4670 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4671 }
4672
4673 /**
4674 * Set whether this view can receive focus while in touch mode.
4675 *
4676 * Setting this to true will also ensure that this view is focusable.
4677 *
4678 * @param focusableInTouchMode If true, this view can receive the focus while
4679 * in touch mode.
4680 *
4681 * @see #setFocusable(boolean)
4682 * @attr ref android.R.styleable#View_focusableInTouchMode
4683 */
4684 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4685 // Focusable in touch mode should always be set before the focusable flag
4686 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4687 // which, in touch mode, will not successfully request focus on this view
4688 // because the focusable in touch mode flag is not set
4689 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4690 if (focusableInTouchMode) {
4691 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4692 }
4693 }
4694
4695 /**
4696 * Set whether this view should have sound effects enabled for events such as
4697 * clicking and touching.
4698 *
4699 * <p>You may wish to disable sound effects for a view if you already play sounds,
4700 * for instance, a dial key that plays dtmf tones.
4701 *
4702 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4703 * @see #isSoundEffectsEnabled()
4704 * @see #playSoundEffect(int)
4705 * @attr ref android.R.styleable#View_soundEffectsEnabled
4706 */
4707 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4708 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4709 }
4710
4711 /**
4712 * @return whether this view should have sound effects enabled for events such as
4713 * clicking and touching.
4714 *
4715 * @see #setSoundEffectsEnabled(boolean)
4716 * @see #playSoundEffect(int)
4717 * @attr ref android.R.styleable#View_soundEffectsEnabled
4718 */
4719 @ViewDebug.ExportedProperty
4720 public boolean isSoundEffectsEnabled() {
4721 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4722 }
4723
4724 /**
4725 * Set whether this view should have haptic feedback for events such as
4726 * long presses.
4727 *
4728 * <p>You may wish to disable haptic feedback if your view already controls
4729 * its own haptic feedback.
4730 *
4731 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4732 * @see #isHapticFeedbackEnabled()
4733 * @see #performHapticFeedback(int)
4734 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4735 */
4736 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4737 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4738 }
4739
4740 /**
4741 * @return whether this view should have haptic feedback enabled for events
4742 * long presses.
4743 *
4744 * @see #setHapticFeedbackEnabled(boolean)
4745 * @see #performHapticFeedback(int)
4746 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4747 */
4748 @ViewDebug.ExportedProperty
4749 public boolean isHapticFeedbackEnabled() {
4750 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4751 }
4752
4753 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004754 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004755 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004756 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4757 * {@link #LAYOUT_DIRECTION_RTL},
4758 * {@link #LAYOUT_DIRECTION_INHERIT} or
4759 * {@link #LAYOUT_DIRECTION_LOCALE}.
4760 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004761 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004762 * @hide
4763 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004764 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004765 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4766 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4767 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4768 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004769 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004770 public int getLayoutDirection() {
4771 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004772 }
4773
4774 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004775 * Set the layout direction for this view. This will propagate a reset of layout direction
4776 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004777 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004778 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4779 * {@link #LAYOUT_DIRECTION_RTL},
4780 * {@link #LAYOUT_DIRECTION_INHERIT} or
4781 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004782 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004783 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004784 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004785 * @hide
4786 */
4787 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004788 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004789 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004790 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004791 // Setting the flag will also request a layout.
4792 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4793 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004794 }
4795
4796 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004797 * Returns the resolved layout direction for this view.
4798 *
4799 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4800 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4801 *
4802 * @hide
4803 */
4804 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4805 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4806 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4807 })
4808 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004809 resolveLayoutDirectionIfNeeded();
4810 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004811 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4812 }
4813
4814 /**
4815 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4816 * layout attribute and/or the inherited value from the parent.</p>
4817 *
4818 * @return true if the layout is right-to-left.
4819 *
4820 * @hide
4821 */
4822 @ViewDebug.ExportedProperty(category = "layout")
4823 public boolean isLayoutRtl() {
4824 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4825 }
4826
4827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 * If this view doesn't do any drawing on its own, set this flag to
4829 * allow further optimizations. By default, this flag is not set on
4830 * View, but could be set on some View subclasses such as ViewGroup.
4831 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004832 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4833 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 *
4835 * @param willNotDraw whether or not this View draw on its own
4836 */
4837 public void setWillNotDraw(boolean willNotDraw) {
4838 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4839 }
4840
4841 /**
4842 * Returns whether or not this View draws on its own.
4843 *
4844 * @return true if this view has nothing to draw, false otherwise
4845 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004846 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004847 public boolean willNotDraw() {
4848 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4849 }
4850
4851 /**
4852 * When a View's drawing cache is enabled, drawing is redirected to an
4853 * offscreen bitmap. Some views, like an ImageView, must be able to
4854 * bypass this mechanism if they already draw a single bitmap, to avoid
4855 * unnecessary usage of the memory.
4856 *
4857 * @param willNotCacheDrawing true if this view does not cache its
4858 * drawing, false otherwise
4859 */
4860 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4861 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4862 }
4863
4864 /**
4865 * Returns whether or not this View can cache its drawing or not.
4866 *
4867 * @return true if this view does not cache its drawing, false otherwise
4868 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004869 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 public boolean willNotCacheDrawing() {
4871 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4872 }
4873
4874 /**
4875 * Indicates whether this view reacts to click events or not.
4876 *
4877 * @return true if the view is clickable, false otherwise
4878 *
4879 * @see #setClickable(boolean)
4880 * @attr ref android.R.styleable#View_clickable
4881 */
4882 @ViewDebug.ExportedProperty
4883 public boolean isClickable() {
4884 return (mViewFlags & CLICKABLE) == CLICKABLE;
4885 }
4886
4887 /**
4888 * Enables or disables click events for this view. When a view
4889 * is clickable it will change its state to "pressed" on every click.
4890 * Subclasses should set the view clickable to visually react to
4891 * user's clicks.
4892 *
4893 * @param clickable true to make the view clickable, false otherwise
4894 *
4895 * @see #isClickable()
4896 * @attr ref android.R.styleable#View_clickable
4897 */
4898 public void setClickable(boolean clickable) {
4899 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4900 }
4901
4902 /**
4903 * Indicates whether this view reacts to long click events or not.
4904 *
4905 * @return true if the view is long clickable, false otherwise
4906 *
4907 * @see #setLongClickable(boolean)
4908 * @attr ref android.R.styleable#View_longClickable
4909 */
4910 public boolean isLongClickable() {
4911 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4912 }
4913
4914 /**
4915 * Enables or disables long click events for this view. When a view is long
4916 * clickable it reacts to the user holding down the button for a longer
4917 * duration than a tap. This event can either launch the listener or a
4918 * context menu.
4919 *
4920 * @param longClickable true to make the view long clickable, false otherwise
4921 * @see #isLongClickable()
4922 * @attr ref android.R.styleable#View_longClickable
4923 */
4924 public void setLongClickable(boolean longClickable) {
4925 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4926 }
4927
4928 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004929 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 *
4931 * @see #isClickable()
4932 * @see #setClickable(boolean)
4933 *
4934 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4935 * the View's internal state from a previously set "pressed" state.
4936 */
4937 public void setPressed(boolean pressed) {
4938 if (pressed) {
4939 mPrivateFlags |= PRESSED;
4940 } else {
4941 mPrivateFlags &= ~PRESSED;
4942 }
4943 refreshDrawableState();
4944 dispatchSetPressed(pressed);
4945 }
4946
4947 /**
4948 * Dispatch setPressed to all of this View's children.
4949 *
4950 * @see #setPressed(boolean)
4951 *
4952 * @param pressed The new pressed state
4953 */
4954 protected void dispatchSetPressed(boolean pressed) {
4955 }
4956
4957 /**
4958 * Indicates whether the view is currently in pressed state. Unless
4959 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4960 * the pressed state.
4961 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004962 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 * @see #isClickable()
4964 * @see #setClickable(boolean)
4965 *
4966 * @return true if the view is currently pressed, false otherwise
4967 */
4968 public boolean isPressed() {
4969 return (mPrivateFlags & PRESSED) == PRESSED;
4970 }
4971
4972 /**
4973 * Indicates whether this view will save its state (that is,
4974 * whether its {@link #onSaveInstanceState} method will be called).
4975 *
4976 * @return Returns true if the view state saving is enabled, else false.
4977 *
4978 * @see #setSaveEnabled(boolean)
4979 * @attr ref android.R.styleable#View_saveEnabled
4980 */
4981 public boolean isSaveEnabled() {
4982 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4983 }
4984
4985 /**
4986 * Controls whether the saving of this view's state is
4987 * enabled (that is, whether its {@link #onSaveInstanceState} method
4988 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004989 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 * for its state to be saved. This flag can only disable the
4991 * saving of this view; any child views may still have their state saved.
4992 *
4993 * @param enabled Set to false to <em>disable</em> state saving, or true
4994 * (the default) to allow it.
4995 *
4996 * @see #isSaveEnabled()
4997 * @see #setId(int)
4998 * @see #onSaveInstanceState()
4999 * @attr ref android.R.styleable#View_saveEnabled
5000 */
5001 public void setSaveEnabled(boolean enabled) {
5002 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5003 }
5004
Jeff Brown85a31762010-09-01 17:01:00 -07005005 /**
5006 * Gets whether the framework should discard touches when the view's
5007 * window is obscured by another visible window.
5008 * Refer to the {@link View} security documentation for more details.
5009 *
5010 * @return True if touch filtering is enabled.
5011 *
5012 * @see #setFilterTouchesWhenObscured(boolean)
5013 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5014 */
5015 @ViewDebug.ExportedProperty
5016 public boolean getFilterTouchesWhenObscured() {
5017 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5018 }
5019
5020 /**
5021 * Sets whether the framework should discard touches when the view's
5022 * window is obscured by another visible window.
5023 * Refer to the {@link View} security documentation for more details.
5024 *
5025 * @param enabled True if touch filtering should be enabled.
5026 *
5027 * @see #getFilterTouchesWhenObscured
5028 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5029 */
5030 public void setFilterTouchesWhenObscured(boolean enabled) {
5031 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5032 FILTER_TOUCHES_WHEN_OBSCURED);
5033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034
5035 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005036 * Indicates whether the entire hierarchy under this view will save its
5037 * state when a state saving traversal occurs from its parent. The default
5038 * is true; if false, these views will not be saved unless
5039 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5040 *
5041 * @return Returns true if the view state saving from parent is enabled, else false.
5042 *
5043 * @see #setSaveFromParentEnabled(boolean)
5044 */
5045 public boolean isSaveFromParentEnabled() {
5046 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5047 }
5048
5049 /**
5050 * Controls whether the entire hierarchy under this view will save its
5051 * state when a state saving traversal occurs from its parent. The default
5052 * is true; if false, these views will not be saved unless
5053 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5054 *
5055 * @param enabled Set to false to <em>disable</em> state saving, or true
5056 * (the default) to allow it.
5057 *
5058 * @see #isSaveFromParentEnabled()
5059 * @see #setId(int)
5060 * @see #onSaveInstanceState()
5061 */
5062 public void setSaveFromParentEnabled(boolean enabled) {
5063 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5064 }
5065
5066
5067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 * Returns whether this View is able to take focus.
5069 *
5070 * @return True if this view can take focus, or false otherwise.
5071 * @attr ref android.R.styleable#View_focusable
5072 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005073 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 public final boolean isFocusable() {
5075 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5076 }
5077
5078 /**
5079 * When a view is focusable, it may not want to take focus when in touch mode.
5080 * For example, a button would like focus when the user is navigating via a D-pad
5081 * so that the user can click on it, but once the user starts touching the screen,
5082 * the button shouldn't take focus
5083 * @return Whether the view is focusable in touch mode.
5084 * @attr ref android.R.styleable#View_focusableInTouchMode
5085 */
5086 @ViewDebug.ExportedProperty
5087 public final boolean isFocusableInTouchMode() {
5088 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5089 }
5090
5091 /**
5092 * Find the nearest view in the specified direction that can take focus.
5093 * This does not actually give focus to that view.
5094 *
5095 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5096 *
5097 * @return The nearest focusable in the specified direction, or null if none
5098 * can be found.
5099 */
5100 public View focusSearch(int direction) {
5101 if (mParent != null) {
5102 return mParent.focusSearch(this, direction);
5103 } else {
5104 return null;
5105 }
5106 }
5107
5108 /**
5109 * This method is the last chance for the focused view and its ancestors to
5110 * respond to an arrow key. This is called when the focused view did not
5111 * consume the key internally, nor could the view system find a new view in
5112 * the requested direction to give focus to.
5113 *
5114 * @param focused The currently focused view.
5115 * @param direction The direction focus wants to move. One of FOCUS_UP,
5116 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5117 * @return True if the this view consumed this unhandled move.
5118 */
5119 public boolean dispatchUnhandledMove(View focused, int direction) {
5120 return false;
5121 }
5122
5123 /**
5124 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005125 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005127 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5128 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 * @return The user specified next view, or null if there is none.
5130 */
5131 View findUserSetNextFocus(View root, int direction) {
5132 switch (direction) {
5133 case FOCUS_LEFT:
5134 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005135 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 case FOCUS_RIGHT:
5137 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005138 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 case FOCUS_UP:
5140 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005141 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 case FOCUS_DOWN:
5143 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005144 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005145 case FOCUS_FORWARD:
5146 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005147 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005148 case FOCUS_BACKWARD: {
5149 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005150 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005151 @Override
5152 public boolean apply(View t) {
5153 return t.mNextFocusForwardId == id;
5154 }
5155 });
5156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 }
5158 return null;
5159 }
5160
Jeff Brown4dfbec22011-08-15 14:55:37 -07005161 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5162 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5163 @Override
5164 public boolean apply(View t) {
5165 return t.mID == childViewId;
5166 }
5167 });
5168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 if (result == null) {
5170 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5171 + "by user for id " + childViewId);
5172 }
5173 return result;
5174 }
5175
5176 /**
5177 * Find and return all focusable views that are descendants of this view,
5178 * possibly including this view if it is focusable itself.
5179 *
5180 * @param direction The direction of the focus
5181 * @return A list of focusable views
5182 */
5183 public ArrayList<View> getFocusables(int direction) {
5184 ArrayList<View> result = new ArrayList<View>(24);
5185 addFocusables(result, direction);
5186 return result;
5187 }
5188
5189 /**
5190 * Add any focusable views that are descendants of this view (possibly
5191 * including this view if it is focusable itself) to views. If we are in touch mode,
5192 * only add views that are also focusable in touch mode.
5193 *
5194 * @param views Focusable views found so far
5195 * @param direction The direction of the focus
5196 */
5197 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005198 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200
svetoslavganov75986cf2009-05-14 22:28:01 -07005201 /**
5202 * Adds any focusable views that are descendants of this view (possibly
5203 * including this view if it is focusable itself) to views. This method
5204 * adds all focusable views regardless if we are in touch mode or
5205 * only views focusable in touch mode if we are in touch mode depending on
5206 * the focusable mode paramater.
5207 *
5208 * @param views Focusable views found so far or null if all we are interested is
5209 * the number of focusables.
5210 * @param direction The direction of the focus.
5211 * @param focusableMode The type of focusables to be added.
5212 *
5213 * @see #FOCUSABLES_ALL
5214 * @see #FOCUSABLES_TOUCH_MODE
5215 */
5216 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5217 if (!isFocusable()) {
5218 return;
5219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220
svetoslavganov75986cf2009-05-14 22:28:01 -07005221 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5222 isInTouchMode() && !isFocusableInTouchMode()) {
5223 return;
5224 }
5225
5226 if (views != null) {
5227 views.add(this);
5228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 }
5230
5231 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005232 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005233 * The search is performed by either the text that the View renders or the content
5234 * description that describes the view for accessibility purposes and the view does
5235 * not render or both. Clients can specify how the search is to be performed via
5236 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5237 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005238 *
5239 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005240 * @param searched The text to match against.
5241 *
5242 * @see #FIND_VIEWS_WITH_TEXT
5243 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5244 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005245 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005246 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5247 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5248 && !TextUtils.isEmpty(mContentDescription)) {
5249 String searchedLowerCase = searched.toString().toLowerCase();
5250 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5251 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5252 outViews.add(this);
5253 }
5254 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005255 }
5256
5257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 * Find and return all touchable views that are descendants of this view,
5259 * possibly including this view if it is touchable itself.
5260 *
5261 * @return A list of touchable views
5262 */
5263 public ArrayList<View> getTouchables() {
5264 ArrayList<View> result = new ArrayList<View>();
5265 addTouchables(result);
5266 return result;
5267 }
5268
5269 /**
5270 * Add any touchable views that are descendants of this view (possibly
5271 * including this view if it is touchable itself) to views.
5272 *
5273 * @param views Touchable views found so far
5274 */
5275 public void addTouchables(ArrayList<View> views) {
5276 final int viewFlags = mViewFlags;
5277
5278 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5279 && (viewFlags & ENABLED_MASK) == ENABLED) {
5280 views.add(this);
5281 }
5282 }
5283
5284 /**
5285 * Call this to try to give focus to a specific view or to one of its
5286 * descendants.
5287 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005288 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5289 * false), or if it is focusable and it is not focusable in touch mode
5290 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005292 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 * have focus, and you want your parent to look for the next one.
5294 *
5295 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5296 * {@link #FOCUS_DOWN} and <code>null</code>.
5297 *
5298 * @return Whether this view or one of its descendants actually took focus.
5299 */
5300 public final boolean requestFocus() {
5301 return requestFocus(View.FOCUS_DOWN);
5302 }
5303
5304
5305 /**
5306 * Call this to try to give focus to a specific view or to one of its
5307 * descendants and give it a hint about what direction focus is heading.
5308 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005309 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5310 * false), or if it is focusable and it is not focusable in touch mode
5311 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005313 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 * have focus, and you want your parent to look for the next one.
5315 *
5316 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5317 * <code>null</code> set for the previously focused rectangle.
5318 *
5319 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5320 * @return Whether this view or one of its descendants actually took focus.
5321 */
5322 public final boolean requestFocus(int direction) {
5323 return requestFocus(direction, null);
5324 }
5325
5326 /**
5327 * Call this to try to give focus to a specific view or to one of its descendants
5328 * and give it hints about the direction and a specific rectangle that the focus
5329 * is coming from. The rectangle can help give larger views a finer grained hint
5330 * about where focus is coming from, and therefore, where to show selection, or
5331 * forward focus change internally.
5332 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005333 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5334 * false), or if it is focusable and it is not focusable in touch mode
5335 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 *
5337 * A View will not take focus if it is not visible.
5338 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005339 * A View will not take focus if one of its parents has
5340 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5341 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005343 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 * have focus, and you want your parent to look for the next one.
5345 *
5346 * You may wish to override this method if your custom {@link View} has an internal
5347 * {@link View} that it wishes to forward the request to.
5348 *
5349 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5350 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5351 * to give a finer grained hint about where focus is coming from. May be null
5352 * if there is no hint.
5353 * @return Whether this view or one of its descendants actually took focus.
5354 */
5355 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5356 // need to be focusable
5357 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5358 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5359 return false;
5360 }
5361
5362 // need to be focusable in touch mode if in touch mode
5363 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005364 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5365 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 }
5367
5368 // need to not have any parents blocking us
5369 if (hasAncestorThatBlocksDescendantFocus()) {
5370 return false;
5371 }
5372
5373 handleFocusGainInternal(direction, previouslyFocusedRect);
5374 return true;
5375 }
5376
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005377 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005378 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005379 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005380 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005381 }
5382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 /**
5384 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5385 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5386 * touch mode to request focus when they are touched.
5387 *
5388 * @return Whether this view or one of its descendants actually took focus.
5389 *
5390 * @see #isInTouchMode()
5391 *
5392 */
5393 public final boolean requestFocusFromTouch() {
5394 // Leave touch mode if we need to
5395 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005396 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005397 if (viewRoot != null) {
5398 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 }
5400 }
5401 return requestFocus(View.FOCUS_DOWN);
5402 }
5403
5404 /**
5405 * @return Whether any ancestor of this view blocks descendant focus.
5406 */
5407 private boolean hasAncestorThatBlocksDescendantFocus() {
5408 ViewParent ancestor = mParent;
5409 while (ancestor instanceof ViewGroup) {
5410 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5411 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5412 return true;
5413 } else {
5414 ancestor = vgAncestor.getParent();
5415 }
5416 }
5417 return false;
5418 }
5419
5420 /**
Romain Guya440b002010-02-24 15:57:54 -08005421 * @hide
5422 */
5423 public void dispatchStartTemporaryDetach() {
5424 onStartTemporaryDetach();
5425 }
5426
5427 /**
5428 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5430 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005431 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 */
5433 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005434 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005435 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005436 }
5437
5438 /**
5439 * @hide
5440 */
5441 public void dispatchFinishTemporaryDetach() {
5442 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 }
Romain Guy8506ab42009-06-11 17:35:47 -07005444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 /**
5446 * Called after {@link #onStartTemporaryDetach} when the container is done
5447 * changing the view.
5448 */
5449 public void onFinishTemporaryDetach() {
5450 }
Romain Guy8506ab42009-06-11 17:35:47 -07005451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005453 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5454 * for this view's window. Returns null if the view is not currently attached
5455 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005456 * just use the standard high-level event callbacks like
5457 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005458 */
5459 public KeyEvent.DispatcherState getKeyDispatcherState() {
5460 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5461 }
Joe Malin32736f02011-01-19 16:14:20 -08005462
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 * Dispatch a key event before it is processed by any input method
5465 * associated with the view hierarchy. This can be used to intercept
5466 * key events in special situations before the IME consumes them; a
5467 * typical example would be handling the BACK key to update the application's
5468 * UI instead of allowing the IME to see it and close itself.
5469 *
5470 * @param event The key event to be dispatched.
5471 * @return True if the event was handled, false otherwise.
5472 */
5473 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5474 return onKeyPreIme(event.getKeyCode(), event);
5475 }
5476
5477 /**
5478 * Dispatch a key event to the next view on the focus path. This path runs
5479 * from the top of the view tree down to the currently focused view. If this
5480 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5481 * the next node down the focus path. This method also fires any key
5482 * listeners.
5483 *
5484 * @param event The key event to be dispatched.
5485 * @return True if the event was handled, false otherwise.
5486 */
5487 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005488 if (mInputEventConsistencyVerifier != null) {
5489 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491
Jeff Brown21bc5c92011-02-28 18:27:14 -08005492 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005493 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005494 ListenerInfo li = mListenerInfo;
5495 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5496 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 return true;
5498 }
5499
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005500 if (event.dispatch(this, mAttachInfo != null
5501 ? mAttachInfo.mKeyDispatchState : null, this)) {
5502 return true;
5503 }
5504
5505 if (mInputEventConsistencyVerifier != null) {
5506 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5507 }
5508 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 }
5510
5511 /**
5512 * Dispatches a key shortcut event.
5513 *
5514 * @param event The key event to be dispatched.
5515 * @return True if the event was handled by the view, false otherwise.
5516 */
5517 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5518 return onKeyShortcut(event.getKeyCode(), event);
5519 }
5520
5521 /**
5522 * Pass the touch screen motion event down to the target view, or this
5523 * view if it is the target.
5524 *
5525 * @param event The motion event to be dispatched.
5526 * @return True if the event was handled by the view, false otherwise.
5527 */
5528 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005529 if (mInputEventConsistencyVerifier != null) {
5530 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5531 }
5532
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005533 if (onFilterTouchEventForSecurity(event)) {
5534 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005535 ListenerInfo li = mListenerInfo;
5536 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5537 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005538 return true;
5539 }
5540
5541 if (onTouchEvent(event)) {
5542 return true;
5543 }
Jeff Brown85a31762010-09-01 17:01:00 -07005544 }
5545
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005546 if (mInputEventConsistencyVerifier != null) {
5547 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005549 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 }
5551
5552 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005553 * Filter the touch event to apply security policies.
5554 *
5555 * @param event The motion event to be filtered.
5556 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005557 *
Jeff Brown85a31762010-09-01 17:01:00 -07005558 * @see #getFilterTouchesWhenObscured
5559 */
5560 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005561 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005562 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5563 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5564 // Window is obscured, drop this touch.
5565 return false;
5566 }
5567 return true;
5568 }
5569
5570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 * Pass a trackball motion event down to the focused view.
5572 *
5573 * @param event The motion event to be dispatched.
5574 * @return True if the event was handled by the view, false otherwise.
5575 */
5576 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005577 if (mInputEventConsistencyVerifier != null) {
5578 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5579 }
5580
Romain Guy02ccac62011-06-24 13:20:23 -07005581 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 }
5583
5584 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005585 * Dispatch a generic motion event.
5586 * <p>
5587 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5588 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005589 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005590 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005591 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005592 *
5593 * @param event The motion event to be dispatched.
5594 * @return True if the event was handled by the view, false otherwise.
5595 */
5596 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005597 if (mInputEventConsistencyVerifier != null) {
5598 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5599 }
5600
Jeff Browna032cc02011-03-07 16:56:21 -08005601 final int source = event.getSource();
5602 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5603 final int action = event.getAction();
5604 if (action == MotionEvent.ACTION_HOVER_ENTER
5605 || action == MotionEvent.ACTION_HOVER_MOVE
5606 || action == MotionEvent.ACTION_HOVER_EXIT) {
5607 if (dispatchHoverEvent(event)) {
5608 return true;
5609 }
5610 } else if (dispatchGenericPointerEvent(event)) {
5611 return true;
5612 }
5613 } else if (dispatchGenericFocusedEvent(event)) {
5614 return true;
5615 }
5616
Jeff Brown10b62902011-06-20 16:40:37 -07005617 if (dispatchGenericMotionEventInternal(event)) {
5618 return true;
5619 }
5620
5621 if (mInputEventConsistencyVerifier != null) {
5622 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5623 }
5624 return false;
5625 }
5626
5627 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005628 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005629 ListenerInfo li = mListenerInfo;
5630 if (li != null && li.mOnGenericMotionListener != null
5631 && (mViewFlags & ENABLED_MASK) == ENABLED
5632 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005633 return true;
5634 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005635
5636 if (onGenericMotionEvent(event)) {
5637 return true;
5638 }
5639
5640 if (mInputEventConsistencyVerifier != null) {
5641 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5642 }
5643 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005644 }
5645
5646 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005647 * Dispatch a hover event.
5648 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005649 * Do not call this method directly.
5650 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005651 * </p>
5652 *
5653 * @param event The motion event to be dispatched.
5654 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005655 */
5656 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005657 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005658 ListenerInfo li = mListenerInfo;
5659 if (li != null && li.mOnHoverListener != null
5660 && (mViewFlags & ENABLED_MASK) == ENABLED
5661 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005662 return true;
5663 }
5664
Jeff Browna032cc02011-03-07 16:56:21 -08005665 return onHoverEvent(event);
5666 }
5667
5668 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005669 * Returns true if the view has a child to which it has recently sent
5670 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5671 * it does not have a hovered child, then it must be the innermost hovered view.
5672 * @hide
5673 */
5674 protected boolean hasHoveredChild() {
5675 return false;
5676 }
5677
5678 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005679 * Dispatch a generic motion event to the view under the first pointer.
5680 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005681 * Do not call this method directly.
5682 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005683 * </p>
5684 *
5685 * @param event The motion event to be dispatched.
5686 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005687 */
5688 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5689 return false;
5690 }
5691
5692 /**
5693 * Dispatch a generic motion event to the currently focused view.
5694 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005695 * Do not call this method directly.
5696 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005697 * </p>
5698 *
5699 * @param event The motion event to be dispatched.
5700 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005701 */
5702 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5703 return false;
5704 }
5705
5706 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005707 * Dispatch a pointer event.
5708 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005709 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5710 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5711 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005712 * and should not be expected to handle other pointing device features.
5713 * </p>
5714 *
5715 * @param event The motion event to be dispatched.
5716 * @return True if the event was handled by the view, false otherwise.
5717 * @hide
5718 */
5719 public final boolean dispatchPointerEvent(MotionEvent event) {
5720 if (event.isTouchEvent()) {
5721 return dispatchTouchEvent(event);
5722 } else {
5723 return dispatchGenericMotionEvent(event);
5724 }
5725 }
5726
5727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 * Called when the window containing this view gains or loses window focus.
5729 * ViewGroups should override to route to their children.
5730 *
5731 * @param hasFocus True if the window containing this view now has focus,
5732 * false otherwise.
5733 */
5734 public void dispatchWindowFocusChanged(boolean hasFocus) {
5735 onWindowFocusChanged(hasFocus);
5736 }
5737
5738 /**
5739 * Called when the window containing this view gains or loses focus. Note
5740 * that this is separate from view focus: to receive key events, both
5741 * your view and its window must have focus. If a window is displayed
5742 * on top of yours that takes input focus, then your own window will lose
5743 * focus but the view focus will remain unchanged.
5744 *
5745 * @param hasWindowFocus True if the window containing this view now has
5746 * focus, false otherwise.
5747 */
5748 public void onWindowFocusChanged(boolean hasWindowFocus) {
5749 InputMethodManager imm = InputMethodManager.peekInstance();
5750 if (!hasWindowFocus) {
5751 if (isPressed()) {
5752 setPressed(false);
5753 }
5754 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5755 imm.focusOut(this);
5756 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005757 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005758 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005759 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5761 imm.focusIn(this);
5762 }
5763 refreshDrawableState();
5764 }
5765
5766 /**
5767 * Returns true if this view is in a window that currently has window focus.
5768 * Note that this is not the same as the view itself having focus.
5769 *
5770 * @return True if this view is in a window that currently has window focus.
5771 */
5772 public boolean hasWindowFocus() {
5773 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5774 }
5775
5776 /**
Adam Powell326d8082009-12-09 15:10:07 -08005777 * Dispatch a view visibility change down the view hierarchy.
5778 * ViewGroups should override to route to their children.
5779 * @param changedView The view whose visibility changed. Could be 'this' or
5780 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005781 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5782 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005783 */
5784 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5785 onVisibilityChanged(changedView, visibility);
5786 }
5787
5788 /**
5789 * Called when the visibility of the view or an ancestor of the view is changed.
5790 * @param changedView The view whose visibility changed. Could be 'this' or
5791 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005792 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5793 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005794 */
5795 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005796 if (visibility == VISIBLE) {
5797 if (mAttachInfo != null) {
5798 initialAwakenScrollBars();
5799 } else {
5800 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5801 }
5802 }
Adam Powell326d8082009-12-09 15:10:07 -08005803 }
5804
5805 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005806 * Dispatch a hint about whether this view is displayed. For instance, when
5807 * a View moves out of the screen, it might receives a display hint indicating
5808 * the view is not displayed. Applications should not <em>rely</em> on this hint
5809 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005810 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005811 * @param hint A hint about whether or not this view is displayed:
5812 * {@link #VISIBLE} or {@link #INVISIBLE}.
5813 */
5814 public void dispatchDisplayHint(int hint) {
5815 onDisplayHint(hint);
5816 }
5817
5818 /**
5819 * Gives this view a hint about whether is displayed or not. For instance, when
5820 * a View moves out of the screen, it might receives a display hint indicating
5821 * the view is not displayed. Applications should not <em>rely</em> on this hint
5822 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005823 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005824 * @param hint A hint about whether or not this view is displayed:
5825 * {@link #VISIBLE} or {@link #INVISIBLE}.
5826 */
5827 protected void onDisplayHint(int hint) {
5828 }
5829
5830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 * Dispatch a window visibility change down the view hierarchy.
5832 * ViewGroups should override to route to their children.
5833 *
5834 * @param visibility The new visibility of the window.
5835 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005836 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 */
5838 public void dispatchWindowVisibilityChanged(int visibility) {
5839 onWindowVisibilityChanged(visibility);
5840 }
5841
5842 /**
5843 * Called when the window containing has change its visibility
5844 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5845 * that this tells you whether or not your window is being made visible
5846 * to the window manager; this does <em>not</em> tell you whether or not
5847 * your window is obscured by other windows on the screen, even if it
5848 * is itself visible.
5849 *
5850 * @param visibility The new visibility of the window.
5851 */
5852 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005853 if (visibility == VISIBLE) {
5854 initialAwakenScrollBars();
5855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 }
5857
5858 /**
5859 * Returns the current visibility of the window this view is attached to
5860 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5861 *
5862 * @return Returns the current visibility of the view's window.
5863 */
5864 public int getWindowVisibility() {
5865 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5866 }
5867
5868 /**
5869 * Retrieve the overall visible display size in which the window this view is
5870 * attached to has been positioned in. This takes into account screen
5871 * decorations above the window, for both cases where the window itself
5872 * is being position inside of them or the window is being placed under
5873 * then and covered insets are used for the window to position its content
5874 * inside. In effect, this tells you the available area where content can
5875 * be placed and remain visible to users.
5876 *
5877 * <p>This function requires an IPC back to the window manager to retrieve
5878 * the requested information, so should not be used in performance critical
5879 * code like drawing.
5880 *
5881 * @param outRect Filled in with the visible display frame. If the view
5882 * is not attached to a window, this is simply the raw display size.
5883 */
5884 public void getWindowVisibleDisplayFrame(Rect outRect) {
5885 if (mAttachInfo != null) {
5886 try {
5887 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5888 } catch (RemoteException e) {
5889 return;
5890 }
5891 // XXX This is really broken, and probably all needs to be done
5892 // in the window manager, and we need to know more about whether
5893 // we want the area behind or in front of the IME.
5894 final Rect insets = mAttachInfo.mVisibleInsets;
5895 outRect.left += insets.left;
5896 outRect.top += insets.top;
5897 outRect.right -= insets.right;
5898 outRect.bottom -= insets.bottom;
5899 return;
5900 }
5901 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005902 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 }
5904
5905 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005906 * Dispatch a notification about a resource configuration change down
5907 * the view hierarchy.
5908 * ViewGroups should override to route to their children.
5909 *
5910 * @param newConfig The new resource configuration.
5911 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005912 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005913 */
5914 public void dispatchConfigurationChanged(Configuration newConfig) {
5915 onConfigurationChanged(newConfig);
5916 }
5917
5918 /**
5919 * Called when the current configuration of the resources being used
5920 * by the application have changed. You can use this to decide when
5921 * to reload resources that can changed based on orientation and other
5922 * configuration characterstics. You only need to use this if you are
5923 * not relying on the normal {@link android.app.Activity} mechanism of
5924 * recreating the activity instance upon a configuration change.
5925 *
5926 * @param newConfig The new resource configuration.
5927 */
5928 protected void onConfigurationChanged(Configuration newConfig) {
5929 }
5930
5931 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 * Private function to aggregate all per-view attributes in to the view
5933 * root.
5934 */
5935 void dispatchCollectViewAttributes(int visibility) {
5936 performCollectViewAttributes(visibility);
5937 }
5938
5939 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005940 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005941 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5942 mAttachInfo.mKeepScreenOn = true;
5943 }
5944 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005945 ListenerInfo li = mListenerInfo;
5946 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005947 mAttachInfo.mHasSystemUiListeners = true;
5948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 }
5950 }
5951
5952 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005953 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005955 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5956 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 ai.mRecomputeGlobalAttributes = true;
5958 }
5959 }
5960 }
5961
5962 /**
5963 * Returns whether the device is currently in touch mode. Touch mode is entered
5964 * once the user begins interacting with the device by touch, and affects various
5965 * things like whether focus is always visible to the user.
5966 *
5967 * @return Whether the device is in touch mode.
5968 */
5969 @ViewDebug.ExportedProperty
5970 public boolean isInTouchMode() {
5971 if (mAttachInfo != null) {
5972 return mAttachInfo.mInTouchMode;
5973 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005974 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 }
5976 }
5977
5978 /**
5979 * Returns the context the view is running in, through which it can
5980 * access the current theme, resources, etc.
5981 *
5982 * @return The view's Context.
5983 */
5984 @ViewDebug.CapturedViewProperty
5985 public final Context getContext() {
5986 return mContext;
5987 }
5988
5989 /**
5990 * Handle a key event before it is processed by any input method
5991 * associated with the view hierarchy. This can be used to intercept
5992 * key events in special situations before the IME consumes them; a
5993 * typical example would be handling the BACK key to update the application's
5994 * UI instead of allowing the IME to see it and close itself.
5995 *
5996 * @param keyCode The value in event.getKeyCode().
5997 * @param event Description of the key event.
5998 * @return If you handled the event, return true. If you want to allow the
5999 * event to be handled by the next receiver, return false.
6000 */
6001 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6002 return false;
6003 }
6004
6005 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006006 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6007 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6009 * is released, if the view is enabled and clickable.
6010 *
6011 * @param keyCode A key code that represents the button pressed, from
6012 * {@link android.view.KeyEvent}.
6013 * @param event The KeyEvent object that defines the button action.
6014 */
6015 public boolean onKeyDown(int keyCode, KeyEvent event) {
6016 boolean result = false;
6017
6018 switch (keyCode) {
6019 case KeyEvent.KEYCODE_DPAD_CENTER:
6020 case KeyEvent.KEYCODE_ENTER: {
6021 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6022 return true;
6023 }
6024 // Long clickable items don't necessarily have to be clickable
6025 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6026 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6027 (event.getRepeatCount() == 0)) {
6028 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006029 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 return true;
6031 }
6032 break;
6033 }
6034 }
6035 return result;
6036 }
6037
6038 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006039 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6040 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6041 * the event).
6042 */
6043 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6044 return false;
6045 }
6046
6047 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006048 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6049 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6051 * {@link KeyEvent#KEYCODE_ENTER} is released.
6052 *
6053 * @param keyCode A key code that represents the button pressed, from
6054 * {@link android.view.KeyEvent}.
6055 * @param event The KeyEvent object that defines the button action.
6056 */
6057 public boolean onKeyUp(int keyCode, KeyEvent event) {
6058 boolean result = false;
6059
6060 switch (keyCode) {
6061 case KeyEvent.KEYCODE_DPAD_CENTER:
6062 case KeyEvent.KEYCODE_ENTER: {
6063 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6064 return true;
6065 }
6066 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6067 setPressed(false);
6068
6069 if (!mHasPerformedLongPress) {
6070 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006071 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072
6073 result = performClick();
6074 }
6075 }
6076 break;
6077 }
6078 }
6079 return result;
6080 }
6081
6082 /**
6083 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6084 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6085 * the event).
6086 *
6087 * @param keyCode A key code that represents the button pressed, from
6088 * {@link android.view.KeyEvent}.
6089 * @param repeatCount The number of times the action was made.
6090 * @param event The KeyEvent object that defines the button action.
6091 */
6092 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6093 return false;
6094 }
6095
6096 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006097 * Called on the focused view when a key shortcut event is not handled.
6098 * Override this method to implement local key shortcuts for the View.
6099 * Key shortcuts can also be implemented by setting the
6100 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 *
6102 * @param keyCode The value in event.getKeyCode().
6103 * @param event Description of the key event.
6104 * @return If you handled the event, return true. If you want to allow the
6105 * event to be handled by the next receiver, return false.
6106 */
6107 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6108 return false;
6109 }
6110
6111 /**
6112 * Check whether the called view is a text editor, in which case it
6113 * would make sense to automatically display a soft input window for
6114 * it. Subclasses should override this if they implement
6115 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006116 * a call on that method would return a non-null InputConnection, and
6117 * they are really a first-class editor that the user would normally
6118 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006119 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006120 * <p>The default implementation always returns false. This does
6121 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6122 * will not be called or the user can not otherwise perform edits on your
6123 * view; it is just a hint to the system that this is not the primary
6124 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 * @return Returns true if this view is a text editor, else false.
6127 */
6128 public boolean onCheckIsTextEditor() {
6129 return false;
6130 }
Romain Guy8506ab42009-06-11 17:35:47 -07006131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 /**
6133 * Create a new InputConnection for an InputMethod to interact
6134 * with the view. The default implementation returns null, since it doesn't
6135 * support input methods. You can override this to implement such support.
6136 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 * <p>When implementing this, you probably also want to implement
6139 * {@link #onCheckIsTextEditor()} to indicate you will return a
6140 * non-null InputConnection.
6141 *
6142 * @param outAttrs Fill in with attribute information about the connection.
6143 */
6144 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6145 return null;
6146 }
6147
6148 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006149 * Called by the {@link android.view.inputmethod.InputMethodManager}
6150 * when a view who is not the current
6151 * input connection target is trying to make a call on the manager. The
6152 * default implementation returns false; you can override this to return
6153 * true for certain views if you are performing InputConnection proxying
6154 * to them.
6155 * @param view The View that is making the InputMethodManager call.
6156 * @return Return true to allow the call, false to reject.
6157 */
6158 public boolean checkInputConnectionProxy(View view) {
6159 return false;
6160 }
Romain Guy8506ab42009-06-11 17:35:47 -07006161
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 * Show the context menu for this view. It is not safe to hold on to the
6164 * menu after returning from this method.
6165 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006166 * You should normally not overload this method. Overload
6167 * {@link #onCreateContextMenu(ContextMenu)} or define an
6168 * {@link OnCreateContextMenuListener} to add items to the context menu.
6169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 * @param menu The context menu to populate
6171 */
6172 public void createContextMenu(ContextMenu menu) {
6173 ContextMenuInfo menuInfo = getContextMenuInfo();
6174
6175 // Sets the current menu info so all items added to menu will have
6176 // my extra info set.
6177 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6178
6179 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006180 ListenerInfo li = mListenerInfo;
6181 if (li != null && li.mOnCreateContextMenuListener != null) {
6182 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 }
6184
6185 // Clear the extra information so subsequent items that aren't mine don't
6186 // have my extra info.
6187 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6188
6189 if (mParent != null) {
6190 mParent.createContextMenu(menu);
6191 }
6192 }
6193
6194 /**
6195 * Views should implement this if they have extra information to associate
6196 * with the context menu. The return result is supplied as a parameter to
6197 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6198 * callback.
6199 *
6200 * @return Extra information about the item for which the context menu
6201 * should be shown. This information will vary across different
6202 * subclasses of View.
6203 */
6204 protected ContextMenuInfo getContextMenuInfo() {
6205 return null;
6206 }
6207
6208 /**
6209 * Views should implement this if the view itself is going to add items to
6210 * the context menu.
6211 *
6212 * @param menu the context menu to populate
6213 */
6214 protected void onCreateContextMenu(ContextMenu menu) {
6215 }
6216
6217 /**
6218 * Implement this method to handle trackball motion events. The
6219 * <em>relative</em> movement of the trackball since the last event
6220 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6221 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6222 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6223 * they will often be fractional values, representing the more fine-grained
6224 * movement information available from a trackball).
6225 *
6226 * @param event The motion event.
6227 * @return True if the event was handled, false otherwise.
6228 */
6229 public boolean onTrackballEvent(MotionEvent event) {
6230 return false;
6231 }
6232
6233 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006234 * Implement this method to handle generic motion events.
6235 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006236 * Generic motion events describe joystick movements, mouse hovers, track pad
6237 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006238 * {@link MotionEvent#getSource() source} of the motion event specifies
6239 * the class of input that was received. Implementations of this method
6240 * must examine the bits in the source before processing the event.
6241 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006242 * </p><p>
6243 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6244 * are delivered to the view under the pointer. All other generic motion events are
6245 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006246 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006247 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006248 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006249 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6250 * // process the joystick movement...
6251 * return true;
6252 * }
6253 * }
6254 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6255 * switch (event.getAction()) {
6256 * case MotionEvent.ACTION_HOVER_MOVE:
6257 * // process the mouse hover movement...
6258 * return true;
6259 * case MotionEvent.ACTION_SCROLL:
6260 * // process the scroll wheel movement...
6261 * return true;
6262 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006263 * }
6264 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006265 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006266 *
6267 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006268 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006269 */
6270 public boolean onGenericMotionEvent(MotionEvent event) {
6271 return false;
6272 }
6273
6274 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006275 * Implement this method to handle hover events.
6276 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006277 * This method is called whenever a pointer is hovering into, over, or out of the
6278 * bounds of a view and the view is not currently being touched.
6279 * Hover events are represented as pointer events with action
6280 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6281 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6282 * </p>
6283 * <ul>
6284 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6285 * when the pointer enters the bounds of the view.</li>
6286 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6287 * when the pointer has already entered the bounds of the view and has moved.</li>
6288 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6289 * when the pointer has exited the bounds of the view or when the pointer is
6290 * about to go down due to a button click, tap, or similar user action that
6291 * causes the view to be touched.</li>
6292 * </ul>
6293 * <p>
6294 * The view should implement this method to return true to indicate that it is
6295 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006296 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006297 * The default implementation calls {@link #setHovered} to update the hovered state
6298 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006299 * is enabled and is clickable. The default implementation also sends hover
6300 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006301 * </p>
6302 *
6303 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006304 * @return True if the view handled the hover event.
6305 *
6306 * @see #isHovered
6307 * @see #setHovered
6308 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006309 */
6310 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006311 // The root view may receive hover (or touch) events that are outside the bounds of
6312 // the window. This code ensures that we only send accessibility events for
6313 // hovers that are actually within the bounds of the root view.
6314 final int action = event.getAction();
6315 if (!mSendingHoverAccessibilityEvents) {
6316 if ((action == MotionEvent.ACTION_HOVER_ENTER
6317 || action == MotionEvent.ACTION_HOVER_MOVE)
6318 && !hasHoveredChild()
6319 && pointInView(event.getX(), event.getY())) {
6320 mSendingHoverAccessibilityEvents = true;
6321 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6322 }
6323 } else {
6324 if (action == MotionEvent.ACTION_HOVER_EXIT
6325 || (action == MotionEvent.ACTION_HOVER_MOVE
6326 && !pointInView(event.getX(), event.getY()))) {
6327 mSendingHoverAccessibilityEvents = false;
6328 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6329 }
Jeff Browna1b24182011-07-28 13:38:24 -07006330 }
6331
Jeff Brown87b7f802011-06-21 18:35:45 -07006332 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006333 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006334 case MotionEvent.ACTION_HOVER_ENTER:
6335 setHovered(true);
6336 break;
6337 case MotionEvent.ACTION_HOVER_EXIT:
6338 setHovered(false);
6339 break;
6340 }
Jeff Browna1b24182011-07-28 13:38:24 -07006341
6342 // Dispatch the event to onGenericMotionEvent before returning true.
6343 // This is to provide compatibility with existing applications that
6344 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6345 // break because of the new default handling for hoverable views
6346 // in onHoverEvent.
6347 // Note that onGenericMotionEvent will be called by default when
6348 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6349 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006350 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006351 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006352 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006353 }
6354
6355 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006356 * Returns true if the view should handle {@link #onHoverEvent}
6357 * by calling {@link #setHovered} to change its hovered state.
6358 *
6359 * @return True if the view is hoverable.
6360 */
6361 private boolean isHoverable() {
6362 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006363 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006364 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6365 return false;
6366 }
6367
6368 return (viewFlags & CLICKABLE) == CLICKABLE
6369 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6370 }
6371
6372 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006373 * Returns true if the view is currently hovered.
6374 *
6375 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006376 *
6377 * @see #setHovered
6378 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006379 */
Jeff Brown10b62902011-06-20 16:40:37 -07006380 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006381 public boolean isHovered() {
6382 return (mPrivateFlags & HOVERED) != 0;
6383 }
6384
6385 /**
6386 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006387 * <p>
6388 * Calling this method also changes the drawable state of the view. This
6389 * enables the view to react to hover by using different drawable resources
6390 * to change its appearance.
6391 * </p><p>
6392 * The {@link #onHoverChanged} method is called when the hovered state changes.
6393 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006394 *
6395 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006396 *
6397 * @see #isHovered
6398 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006399 */
6400 public void setHovered(boolean hovered) {
6401 if (hovered) {
6402 if ((mPrivateFlags & HOVERED) == 0) {
6403 mPrivateFlags |= HOVERED;
6404 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006405 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006406 }
6407 } else {
6408 if ((mPrivateFlags & HOVERED) != 0) {
6409 mPrivateFlags &= ~HOVERED;
6410 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006411 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006412 }
6413 }
6414 }
6415
6416 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006417 * Implement this method to handle hover state changes.
6418 * <p>
6419 * This method is called whenever the hover state changes as a result of a
6420 * call to {@link #setHovered}.
6421 * </p>
6422 *
6423 * @param hovered The current hover state, as returned by {@link #isHovered}.
6424 *
6425 * @see #isHovered
6426 * @see #setHovered
6427 */
6428 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006429 }
6430
6431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 * Implement this method to handle touch screen motion events.
6433 *
6434 * @param event The motion event.
6435 * @return True if the event was handled, false otherwise.
6436 */
6437 public boolean onTouchEvent(MotionEvent event) {
6438 final int viewFlags = mViewFlags;
6439
6440 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006441 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6442 mPrivateFlags &= ~PRESSED;
6443 refreshDrawableState();
6444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 // A disabled view that is clickable still consumes the touch
6446 // events, it just doesn't respond to them.
6447 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6448 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6449 }
6450
6451 if (mTouchDelegate != null) {
6452 if (mTouchDelegate.onTouchEvent(event)) {
6453 return true;
6454 }
6455 }
6456
6457 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6458 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6459 switch (event.getAction()) {
6460 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006461 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6462 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 // take focus if we don't have it already and we should in
6464 // touch mode.
6465 boolean focusTaken = false;
6466 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6467 focusTaken = requestFocus();
6468 }
6469
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006470 if (prepressed) {
6471 // The button is being released before we actually
6472 // showed it as pressed. Make it show the pressed
6473 // state now (before scheduling the click) to ensure
6474 // the user sees it.
6475 mPrivateFlags |= PRESSED;
6476 refreshDrawableState();
6477 }
Joe Malin32736f02011-01-19 16:14:20 -08006478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 if (!mHasPerformedLongPress) {
6480 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006481 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482
6483 // Only perform take click actions if we were in the pressed state
6484 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006485 // Use a Runnable and post this rather than calling
6486 // performClick directly. This lets other visual state
6487 // of the view update before click actions start.
6488 if (mPerformClick == null) {
6489 mPerformClick = new PerformClick();
6490 }
6491 if (!post(mPerformClick)) {
6492 performClick();
6493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 }
6495 }
6496
6497 if (mUnsetPressedState == null) {
6498 mUnsetPressedState = new UnsetPressedState();
6499 }
6500
Adam Powelle14579b2009-12-16 18:39:52 -08006501 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006502 postDelayed(mUnsetPressedState,
6503 ViewConfiguration.getPressedStateDuration());
6504 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 // If the post failed, unpress right now
6506 mUnsetPressedState.run();
6507 }
Adam Powelle14579b2009-12-16 18:39:52 -08006508 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 }
6510 break;
6511
6512 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006513 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006514
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006515 if (performButtonActionOnTouchDown(event)) {
6516 break;
6517 }
6518
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006519 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006520 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006521
6522 // For views inside a scrolling container, delay the pressed feedback for
6523 // a short period in case this is a scroll.
6524 if (isInScrollingContainer) {
6525 mPrivateFlags |= PREPRESSED;
6526 if (mPendingCheckForTap == null) {
6527 mPendingCheckForTap = new CheckForTap();
6528 }
6529 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6530 } else {
6531 // Not inside a scrolling container, so show the feedback right away
6532 mPrivateFlags |= PRESSED;
6533 refreshDrawableState();
6534 checkForLongClick(0);
6535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 break;
6537
6538 case MotionEvent.ACTION_CANCEL:
6539 mPrivateFlags &= ~PRESSED;
6540 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006541 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 break;
6543
6544 case MotionEvent.ACTION_MOVE:
6545 final int x = (int) event.getX();
6546 final int y = (int) event.getY();
6547
6548 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006549 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006551 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006552 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006553 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006554 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555
6556 // Need to switch from pressed to not pressed
6557 mPrivateFlags &= ~PRESSED;
6558 refreshDrawableState();
6559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 }
6561 break;
6562 }
6563 return true;
6564 }
6565
6566 return false;
6567 }
6568
6569 /**
Adam Powell10298662011-08-14 18:26:30 -07006570 * @hide
6571 */
6572 public boolean isInScrollingContainer() {
6573 ViewParent p = getParent();
6574 while (p != null && p instanceof ViewGroup) {
6575 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6576 return true;
6577 }
6578 p = p.getParent();
6579 }
6580 return false;
6581 }
6582
6583 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006584 * Remove the longpress detection timer.
6585 */
6586 private void removeLongPressCallback() {
6587 if (mPendingCheckForLongPress != null) {
6588 removeCallbacks(mPendingCheckForLongPress);
6589 }
6590 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006591
6592 /**
6593 * Remove the pending click action
6594 */
6595 private void removePerformClickCallback() {
6596 if (mPerformClick != null) {
6597 removeCallbacks(mPerformClick);
6598 }
6599 }
6600
Adam Powelle14579b2009-12-16 18:39:52 -08006601 /**
Romain Guya440b002010-02-24 15:57:54 -08006602 * Remove the prepress detection timer.
6603 */
6604 private void removeUnsetPressCallback() {
6605 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6606 setPressed(false);
6607 removeCallbacks(mUnsetPressedState);
6608 }
6609 }
6610
6611 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006612 * Remove the tap detection timer.
6613 */
6614 private void removeTapCallback() {
6615 if (mPendingCheckForTap != null) {
6616 mPrivateFlags &= ~PREPRESSED;
6617 removeCallbacks(mPendingCheckForTap);
6618 }
6619 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006620
6621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 * Cancels a pending long press. Your subclass can use this if you
6623 * want the context menu to come up if the user presses and holds
6624 * at the same place, but you don't want it to come up if they press
6625 * and then move around enough to cause scrolling.
6626 */
6627 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006628 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006629
6630 /*
6631 * The prepressed state handled by the tap callback is a display
6632 * construct, but the tap callback will post a long press callback
6633 * less its own timeout. Remove it here.
6634 */
6635 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 }
6637
6638 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006639 * Remove the pending callback for sending a
6640 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6641 */
6642 private void removeSendViewScrolledAccessibilityEventCallback() {
6643 if (mSendViewScrolledAccessibilityEvent != null) {
6644 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6645 }
6646 }
6647
6648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 * Sets the TouchDelegate for this View.
6650 */
6651 public void setTouchDelegate(TouchDelegate delegate) {
6652 mTouchDelegate = delegate;
6653 }
6654
6655 /**
6656 * Gets the TouchDelegate for this View.
6657 */
6658 public TouchDelegate getTouchDelegate() {
6659 return mTouchDelegate;
6660 }
6661
6662 /**
6663 * Set flags controlling behavior of this view.
6664 *
6665 * @param flags Constant indicating the value which should be set
6666 * @param mask Constant indicating the bit range that should be changed
6667 */
6668 void setFlags(int flags, int mask) {
6669 int old = mViewFlags;
6670 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6671
6672 int changed = mViewFlags ^ old;
6673 if (changed == 0) {
6674 return;
6675 }
6676 int privateFlags = mPrivateFlags;
6677
6678 /* Check if the FOCUSABLE bit has changed */
6679 if (((changed & FOCUSABLE_MASK) != 0) &&
6680 ((privateFlags & HAS_BOUNDS) !=0)) {
6681 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6682 && ((privateFlags & FOCUSED) != 0)) {
6683 /* Give up focus if we are no longer focusable */
6684 clearFocus();
6685 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6686 && ((privateFlags & FOCUSED) == 0)) {
6687 /*
6688 * Tell the view system that we are now available to take focus
6689 * if no one else already has it.
6690 */
6691 if (mParent != null) mParent.focusableViewAvailable(this);
6692 }
6693 }
6694
6695 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6696 if ((changed & VISIBILITY_MASK) != 0) {
6697 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006698 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006699 * it was not visible. Marking it drawn ensures that the invalidation will
6700 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006701 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006702 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006703 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704
6705 needGlobalAttributesUpdate(true);
6706
6707 // a view becoming visible is worth notifying the parent
6708 // about in case nothing has focus. even if this specific view
6709 // isn't focusable, it may contain something that is, so let
6710 // the root view try to give this focus if nothing else does.
6711 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6712 mParent.focusableViewAvailable(this);
6713 }
6714 }
6715 }
6716
6717 /* Check if the GONE bit has changed */
6718 if ((changed & GONE) != 0) {
6719 needGlobalAttributesUpdate(false);
6720 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006721
Romain Guyecd80ee2009-12-03 17:13:02 -08006722 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6723 if (hasFocus()) clearFocus();
6724 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006725 if (mParent instanceof View) {
6726 // GONE views noop invalidation, so invalidate the parent
6727 ((View) mParent).invalidate(true);
6728 }
6729 // Mark the view drawn to ensure that it gets invalidated properly the next
6730 // time it is visible and gets invalidated
6731 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006732 }
6733 if (mAttachInfo != null) {
6734 mAttachInfo.mViewVisibilityChanged = true;
6735 }
6736 }
6737
6738 /* Check if the VISIBLE bit has changed */
6739 if ((changed & INVISIBLE) != 0) {
6740 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006741 /*
6742 * If this view is becoming invisible, set the DRAWN flag so that
6743 * the next invalidate() will not be skipped.
6744 */
6745 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746
6747 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6748 // root view becoming invisible shouldn't clear focus
6749 if (getRootView() != this) {
6750 clearFocus();
6751 }
6752 }
6753 if (mAttachInfo != null) {
6754 mAttachInfo.mViewVisibilityChanged = true;
6755 }
6756 }
6757
Adam Powell326d8082009-12-09 15:10:07 -08006758 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006759 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006760 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6761 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006762 } else if (mParent != null) {
6763 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006764 }
Adam Powell326d8082009-12-09 15:10:07 -08006765 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6766 }
6767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6769 destroyDrawingCache();
6770 }
6771
6772 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6773 destroyDrawingCache();
6774 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006775 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 }
6777
6778 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6779 destroyDrawingCache();
6780 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6781 }
6782
6783 if ((changed & DRAW_MASK) != 0) {
6784 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6785 if (mBGDrawable != null) {
6786 mPrivateFlags &= ~SKIP_DRAW;
6787 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6788 } else {
6789 mPrivateFlags |= SKIP_DRAW;
6790 }
6791 } else {
6792 mPrivateFlags &= ~SKIP_DRAW;
6793 }
6794 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006795 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 }
6797
6798 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006799 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 mParent.recomputeViewAttributes(this);
6801 }
6802 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006803
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006804 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006805 requestLayout();
6806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 }
6808
6809 /**
6810 * Change the view's z order in the tree, so it's on top of other sibling
6811 * views
6812 */
6813 public void bringToFront() {
6814 if (mParent != null) {
6815 mParent.bringChildToFront(this);
6816 }
6817 }
6818
6819 /**
6820 * This is called in response to an internal scroll in this view (i.e., the
6821 * view scrolled its own contents). This is typically as a result of
6822 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6823 * called.
6824 *
6825 * @param l Current horizontal scroll origin.
6826 * @param t Current vertical scroll origin.
6827 * @param oldl Previous horizontal scroll origin.
6828 * @param oldt Previous vertical scroll origin.
6829 */
6830 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006831 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6832 postSendViewScrolledAccessibilityEventCallback();
6833 }
6834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 mBackgroundSizeChanged = true;
6836
6837 final AttachInfo ai = mAttachInfo;
6838 if (ai != null) {
6839 ai.mViewScrollChanged = true;
6840 }
6841 }
6842
6843 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006844 * Interface definition for a callback to be invoked when the layout bounds of a view
6845 * changes due to layout processing.
6846 */
6847 public interface OnLayoutChangeListener {
6848 /**
6849 * Called when the focus state of a view has changed.
6850 *
6851 * @param v The view whose state has changed.
6852 * @param left The new value of the view's left property.
6853 * @param top The new value of the view's top property.
6854 * @param right The new value of the view's right property.
6855 * @param bottom The new value of the view's bottom property.
6856 * @param oldLeft The previous value of the view's left property.
6857 * @param oldTop The previous value of the view's top property.
6858 * @param oldRight The previous value of the view's right property.
6859 * @param oldBottom The previous value of the view's bottom property.
6860 */
6861 void onLayoutChange(View v, int left, int top, int right, int bottom,
6862 int oldLeft, int oldTop, int oldRight, int oldBottom);
6863 }
6864
6865 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 * This is called during layout when the size of this view has changed. If
6867 * you were just added to the view hierarchy, you're called with the old
6868 * values of 0.
6869 *
6870 * @param w Current width of this view.
6871 * @param h Current height of this view.
6872 * @param oldw Old width of this view.
6873 * @param oldh Old height of this view.
6874 */
6875 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6876 }
6877
6878 /**
6879 * Called by draw to draw the child views. This may be overridden
6880 * by derived classes to gain control just before its children are drawn
6881 * (but after its own view has been drawn).
6882 * @param canvas the canvas on which to draw the view
6883 */
6884 protected void dispatchDraw(Canvas canvas) {
6885 }
6886
6887 /**
6888 * Gets the parent of this view. Note that the parent is a
6889 * ViewParent and not necessarily a View.
6890 *
6891 * @return Parent of this view.
6892 */
6893 public final ViewParent getParent() {
6894 return mParent;
6895 }
6896
6897 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006898 * Set the horizontal scrolled position of your view. This will cause a call to
6899 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6900 * invalidated.
6901 * @param value the x position to scroll to
6902 */
6903 public void setScrollX(int value) {
6904 scrollTo(value, mScrollY);
6905 }
6906
6907 /**
6908 * Set the vertical scrolled position of your view. This will cause a call to
6909 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6910 * invalidated.
6911 * @param value the y position to scroll to
6912 */
6913 public void setScrollY(int value) {
6914 scrollTo(mScrollX, value);
6915 }
6916
6917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 * Return the scrolled left position of this view. This is the left edge of
6919 * the displayed part of your view. You do not need to draw any pixels
6920 * farther left, since those are outside of the frame of your view on
6921 * screen.
6922 *
6923 * @return The left edge of the displayed part of your view, in pixels.
6924 */
6925 public final int getScrollX() {
6926 return mScrollX;
6927 }
6928
6929 /**
6930 * Return the scrolled top position of this view. This is the top edge of
6931 * the displayed part of your view. You do not need to draw any pixels above
6932 * it, since those are outside of the frame of your view on screen.
6933 *
6934 * @return The top edge of the displayed part of your view, in pixels.
6935 */
6936 public final int getScrollY() {
6937 return mScrollY;
6938 }
6939
6940 /**
6941 * Return the width of the your view.
6942 *
6943 * @return The width of your view, in pixels.
6944 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006945 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 public final int getWidth() {
6947 return mRight - mLeft;
6948 }
6949
6950 /**
6951 * Return the height of your view.
6952 *
6953 * @return The height of your view, in pixels.
6954 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006955 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 public final int getHeight() {
6957 return mBottom - mTop;
6958 }
6959
6960 /**
6961 * Return the visible drawing bounds of your view. Fills in the output
6962 * rectangle with the values from getScrollX(), getScrollY(),
6963 * getWidth(), and getHeight().
6964 *
6965 * @param outRect The (scrolled) drawing bounds of the view.
6966 */
6967 public void getDrawingRect(Rect outRect) {
6968 outRect.left = mScrollX;
6969 outRect.top = mScrollY;
6970 outRect.right = mScrollX + (mRight - mLeft);
6971 outRect.bottom = mScrollY + (mBottom - mTop);
6972 }
6973
6974 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006975 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6976 * raw width component (that is the result is masked by
6977 * {@link #MEASURED_SIZE_MASK}).
6978 *
6979 * @return The raw measured width of this view.
6980 */
6981 public final int getMeasuredWidth() {
6982 return mMeasuredWidth & MEASURED_SIZE_MASK;
6983 }
6984
6985 /**
6986 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006987 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006988 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 * This should be used during measurement and layout calculations only. Use
6990 * {@link #getWidth()} to see how wide a view is after layout.
6991 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006992 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006994 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 return mMeasuredWidth;
6996 }
6997
6998 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006999 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7000 * raw width component (that is the result is masked by
7001 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007003 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 */
7005 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007006 return mMeasuredHeight & MEASURED_SIZE_MASK;
7007 }
7008
7009 /**
7010 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007011 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007012 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7013 * This should be used during measurement and layout calculations only. Use
7014 * {@link #getHeight()} to see how wide a view is after layout.
7015 *
7016 * @return The measured width of this view as a bit mask.
7017 */
7018 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 return mMeasuredHeight;
7020 }
7021
7022 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007023 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7024 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7025 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7026 * and the height component is at the shifted bits
7027 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7028 */
7029 public final int getMeasuredState() {
7030 return (mMeasuredWidth&MEASURED_STATE_MASK)
7031 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7032 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7033 }
7034
7035 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007036 * The transform matrix of this view, which is calculated based on the current
7037 * roation, scale, and pivot properties.
7038 *
7039 * @see #getRotation()
7040 * @see #getScaleX()
7041 * @see #getScaleY()
7042 * @see #getPivotX()
7043 * @see #getPivotY()
7044 * @return The current transform matrix for the view
7045 */
7046 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007047 if (mTransformationInfo != null) {
7048 updateMatrix();
7049 return mTransformationInfo.mMatrix;
7050 }
7051 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007052 }
7053
7054 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007055 * Utility function to determine if the value is far enough away from zero to be
7056 * considered non-zero.
7057 * @param value A floating point value to check for zero-ness
7058 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7059 */
7060 private static boolean nonzero(float value) {
7061 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7062 }
7063
7064 /**
Jeff Brown86671742010-09-30 20:00:15 -07007065 * Returns true if the transform matrix is the identity matrix.
7066 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007067 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007068 * @return True if the transform matrix is the identity matrix, false otherwise.
7069 */
Jeff Brown86671742010-09-30 20:00:15 -07007070 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007071 if (mTransformationInfo != null) {
7072 updateMatrix();
7073 return mTransformationInfo.mMatrixIsIdentity;
7074 }
7075 return true;
7076 }
7077
7078 void ensureTransformationInfo() {
7079 if (mTransformationInfo == null) {
7080 mTransformationInfo = new TransformationInfo();
7081 }
Jeff Brown86671742010-09-30 20:00:15 -07007082 }
7083
7084 /**
7085 * Recomputes the transform matrix if necessary.
7086 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007087 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007088 final TransformationInfo info = mTransformationInfo;
7089 if (info == null) {
7090 return;
7091 }
7092 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007093 // transform-related properties have changed since the last time someone
7094 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007095
7096 // Figure out if we need to update the pivot point
7097 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007098 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7099 info.mPrevWidth = mRight - mLeft;
7100 info.mPrevHeight = mBottom - mTop;
7101 info.mPivotX = info.mPrevWidth / 2f;
7102 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007103 }
7104 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007105 info.mMatrix.reset();
7106 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7107 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7108 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7109 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007110 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007111 if (info.mCamera == null) {
7112 info.mCamera = new Camera();
7113 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007114 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007115 info.mCamera.save();
7116 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7117 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7118 info.mCamera.getMatrix(info.matrix3D);
7119 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7120 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7121 info.mPivotY + info.mTranslationY);
7122 info.mMatrix.postConcat(info.matrix3D);
7123 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007124 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007125 info.mMatrixDirty = false;
7126 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7127 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007128 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007129 }
7130
7131 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01007132 * When searching for a view to focus this rectangle is used when considering if this view is
7133 * a good candidate for receiving focus.
7134 *
7135 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
7136 *
7137 * @param r The rectangle to fill in, in this view's coordinates.
7138 */
7139 public void getFocusRect(Rect r) {
7140 getDrawingRect(r);
7141 }
7142
7143 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007144 * Utility method to retrieve the inverse of the current mMatrix property.
7145 * We cache the matrix to avoid recalculating it when transform properties
7146 * have not changed.
7147 *
7148 * @return The inverse of the current matrix of this view.
7149 */
Jeff Brown86671742010-09-30 20:00:15 -07007150 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007151 final TransformationInfo info = mTransformationInfo;
7152 if (info != null) {
7153 updateMatrix();
7154 if (info.mInverseMatrixDirty) {
7155 if (info.mInverseMatrix == null) {
7156 info.mInverseMatrix = new Matrix();
7157 }
7158 info.mMatrix.invert(info.mInverseMatrix);
7159 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007160 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007161 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007162 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007163 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007164 }
7165
7166 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007167 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7168 * views are drawn) from the camera to this view. The camera's distance
7169 * affects 3D transformations, for instance rotations around the X and Y
7170 * axis. If the rotationX or rotationY properties are changed and this view is
7171 * large (more than half the size of the screen), it is recommended to always
7172 * use a camera distance that's greater than the height (X axis rotation) or
7173 * the width (Y axis rotation) of this view.</p>
7174 *
7175 * <p>The distance of the camera from the view plane can have an affect on the
7176 * perspective distortion of the view when it is rotated around the x or y axis.
7177 * For example, a large distance will result in a large viewing angle, and there
7178 * will not be much perspective distortion of the view as it rotates. A short
7179 * distance may cause much more perspective distortion upon rotation, and can
7180 * also result in some drawing artifacts if the rotated view ends up partially
7181 * behind the camera (which is why the recommendation is to use a distance at
7182 * least as far as the size of the view, if the view is to be rotated.)</p>
7183 *
7184 * <p>The distance is expressed in "depth pixels." The default distance depends
7185 * on the screen density. For instance, on a medium density display, the
7186 * default distance is 1280. On a high density display, the default distance
7187 * is 1920.</p>
7188 *
7189 * <p>If you want to specify a distance that leads to visually consistent
7190 * results across various densities, use the following formula:</p>
7191 * <pre>
7192 * float scale = context.getResources().getDisplayMetrics().density;
7193 * view.setCameraDistance(distance * scale);
7194 * </pre>
7195 *
7196 * <p>The density scale factor of a high density display is 1.5,
7197 * and 1920 = 1280 * 1.5.</p>
7198 *
7199 * @param distance The distance in "depth pixels", if negative the opposite
7200 * value is used
7201 *
7202 * @see #setRotationX(float)
7203 * @see #setRotationY(float)
7204 */
7205 public void setCameraDistance(float distance) {
7206 invalidateParentCaches();
7207 invalidate(false);
7208
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007209 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007210 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007211 final TransformationInfo info = mTransformationInfo;
7212 if (info.mCamera == null) {
7213 info.mCamera = new Camera();
7214 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007215 }
7216
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007217 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7218 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007219
7220 invalidate(false);
7221 }
7222
7223 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007224 * The degrees that the view is rotated around the pivot point.
7225 *
Romain Guya5364ee2011-02-24 14:46:04 -08007226 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007227 * @see #getPivotX()
7228 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007229 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007230 * @return The degrees of rotation.
7231 */
7232 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007233 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007234 }
7235
7236 /**
Chet Haase897247b2010-09-09 14:54:47 -07007237 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7238 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007239 *
7240 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007241 *
7242 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007243 * @see #getPivotX()
7244 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007245 * @see #setRotationX(float)
7246 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007247 *
7248 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007249 */
7250 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007251 ensureTransformationInfo();
7252 final TransformationInfo info = mTransformationInfo;
7253 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007254 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007255 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007256 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007257 info.mRotation = rotation;
7258 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007259 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007260 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007261 }
7262 }
7263
7264 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007265 * The degrees that the view is rotated around the vertical axis through the pivot point.
7266 *
7267 * @see #getPivotX()
7268 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007269 * @see #setRotationY(float)
7270 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007271 * @return The degrees of Y rotation.
7272 */
7273 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007274 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007275 }
7276
7277 /**
Chet Haase897247b2010-09-09 14:54:47 -07007278 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7279 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7280 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007281 *
7282 * When rotating large views, it is recommended to adjust the camera distance
7283 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007284 *
7285 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007286 *
7287 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007288 * @see #getPivotX()
7289 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007290 * @see #setRotation(float)
7291 * @see #setRotationX(float)
7292 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007293 *
7294 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007295 */
7296 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007297 ensureTransformationInfo();
7298 final TransformationInfo info = mTransformationInfo;
7299 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007300 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007301 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007302 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007303 info.mRotationY = rotationY;
7304 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007305 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007306 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007307 }
7308 }
7309
7310 /**
7311 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7312 *
7313 * @see #getPivotX()
7314 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007315 * @see #setRotationX(float)
7316 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007317 * @return The degrees of X rotation.
7318 */
7319 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007320 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007321 }
7322
7323 /**
Chet Haase897247b2010-09-09 14:54:47 -07007324 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7325 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7326 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007327 *
7328 * When rotating large views, it is recommended to adjust the camera distance
7329 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007330 *
7331 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007332 *
7333 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007334 * @see #getPivotX()
7335 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007336 * @see #setRotation(float)
7337 * @see #setRotationY(float)
7338 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007339 *
7340 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007341 */
7342 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007343 ensureTransformationInfo();
7344 final TransformationInfo info = mTransformationInfo;
7345 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007346 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007347 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007348 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007349 info.mRotationX = rotationX;
7350 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007351 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007352 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007353 }
7354 }
7355
7356 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007357 * The amount that the view is scaled in x around the pivot point, as a proportion of
7358 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7359 *
Joe Onorato93162322010-09-16 15:42:01 -04007360 * <p>By default, this is 1.0f.
7361 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007362 * @see #getPivotX()
7363 * @see #getPivotY()
7364 * @return The scaling factor.
7365 */
7366 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007367 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007368 }
7369
7370 /**
7371 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7372 * the view's unscaled width. A value of 1 means that no scaling is applied.
7373 *
7374 * @param scaleX The scaling factor.
7375 * @see #getPivotX()
7376 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007377 *
7378 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007379 */
7380 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007381 ensureTransformationInfo();
7382 final TransformationInfo info = mTransformationInfo;
7383 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007384 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007385 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007386 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007387 info.mScaleX = scaleX;
7388 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007389 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007390 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007391 }
7392 }
7393
7394 /**
7395 * The amount that the view is scaled in y around the pivot point, as a proportion of
7396 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7397 *
Joe Onorato93162322010-09-16 15:42:01 -04007398 * <p>By default, this is 1.0f.
7399 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007400 * @see #getPivotX()
7401 * @see #getPivotY()
7402 * @return The scaling factor.
7403 */
7404 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007406 }
7407
7408 /**
7409 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7410 * the view's unscaled width. A value of 1 means that no scaling is applied.
7411 *
7412 * @param scaleY The scaling factor.
7413 * @see #getPivotX()
7414 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007415 *
7416 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007417 */
7418 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007419 ensureTransformationInfo();
7420 final TransformationInfo info = mTransformationInfo;
7421 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007422 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007423 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007424 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007425 info.mScaleY = scaleY;
7426 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007427 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007428 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007429 }
7430 }
7431
7432 /**
7433 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7434 * and {@link #setScaleX(float) scaled}.
7435 *
7436 * @see #getRotation()
7437 * @see #getScaleX()
7438 * @see #getScaleY()
7439 * @see #getPivotY()
7440 * @return The x location of the pivot point.
7441 */
7442 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007443 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007444 }
7445
7446 /**
7447 * Sets the x location of the point around which the view is
7448 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007449 * By default, the pivot point is centered on the object.
7450 * Setting this property disables this behavior and causes the view to use only the
7451 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007452 *
7453 * @param pivotX The x location of the pivot point.
7454 * @see #getRotation()
7455 * @see #getScaleX()
7456 * @see #getScaleY()
7457 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007458 *
7459 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007460 */
7461 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007462 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007463 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007464 final TransformationInfo info = mTransformationInfo;
7465 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007466 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007467 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007468 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007469 info.mPivotX = pivotX;
7470 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007472 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007473 }
7474 }
7475
7476 /**
7477 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7478 * and {@link #setScaleY(float) scaled}.
7479 *
7480 * @see #getRotation()
7481 * @see #getScaleX()
7482 * @see #getScaleY()
7483 * @see #getPivotY()
7484 * @return The y location of the pivot point.
7485 */
7486 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007487 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007488 }
7489
7490 /**
7491 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007492 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7493 * Setting this property disables this behavior and causes the view to use only the
7494 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007495 *
7496 * @param pivotY The y location of the pivot point.
7497 * @see #getRotation()
7498 * @see #getScaleX()
7499 * @see #getScaleY()
7500 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007501 *
7502 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007503 */
7504 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007505 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007506 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007507 final TransformationInfo info = mTransformationInfo;
7508 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007509 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007510 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007511 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007512 info.mPivotY = pivotY;
7513 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007514 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007515 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007516 }
7517 }
7518
7519 /**
7520 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7521 * completely transparent and 1 means the view is completely opaque.
7522 *
Joe Onorato93162322010-09-16 15:42:01 -04007523 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007524 * @return The opacity of the view.
7525 */
7526 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007527 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007528 }
7529
7530 /**
Romain Guy171c5922011-01-06 10:04:23 -08007531 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7532 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007533 *
Romain Guy171c5922011-01-06 10:04:23 -08007534 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7535 * responsible for applying the opacity itself. Otherwise, calling this method is
7536 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007537 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007538 *
7539 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007540 *
Joe Malin32736f02011-01-19 16:14:20 -08007541 * @see #setLayerType(int, android.graphics.Paint)
7542 *
Chet Haase73066682010-11-29 15:55:32 -08007543 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007544 */
7545 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007546 ensureTransformationInfo();
7547 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007548 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007549 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007550 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007551 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007552 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007553 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007554 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007555 invalidate(false);
7556 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007557 }
7558
7559 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007560 * Faster version of setAlpha() which performs the same steps except there are
7561 * no calls to invalidate(). The caller of this function should perform proper invalidation
7562 * on the parent and this object. The return value indicates whether the subclass handles
7563 * alpha (the return value for onSetAlpha()).
7564 *
7565 * @param alpha The new value for the alpha property
7566 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7567 */
7568 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 ensureTransformationInfo();
7570 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007571 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7572 if (subclassHandlesAlpha) {
7573 mPrivateFlags |= ALPHA_SET;
7574 } else {
7575 mPrivateFlags &= ~ALPHA_SET;
7576 }
7577 return subclassHandlesAlpha;
7578 }
7579
7580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 * Top position of this view relative to its parent.
7582 *
7583 * @return The top of this view, in pixels.
7584 */
7585 @ViewDebug.CapturedViewProperty
7586 public final int getTop() {
7587 return mTop;
7588 }
7589
7590 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007591 * Sets the top position of this view relative to its parent. This method is meant to be called
7592 * by the layout system and should not generally be called otherwise, because the property
7593 * may be changed at any time by the layout.
7594 *
7595 * @param top The top of this view, in pixels.
7596 */
7597 public final void setTop(int top) {
7598 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007599 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007600 final boolean matrixIsIdentity = mTransformationInfo == null
7601 || mTransformationInfo.mMatrixIsIdentity;
7602 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007603 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007604 int minTop;
7605 int yLoc;
7606 if (top < mTop) {
7607 minTop = top;
7608 yLoc = top - mTop;
7609 } else {
7610 minTop = mTop;
7611 yLoc = 0;
7612 }
Chet Haasee9140a72011-02-16 16:23:29 -08007613 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007614 }
7615 } else {
7616 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007617 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007618 }
7619
Chet Haaseed032702010-10-01 14:05:54 -07007620 int width = mRight - mLeft;
7621 int oldHeight = mBottom - mTop;
7622
Chet Haase21cd1382010-09-01 17:42:29 -07007623 mTop = top;
7624
Chet Haaseed032702010-10-01 14:05:54 -07007625 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7626
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007627 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007628 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7629 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007630 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007631 }
Chet Haase21cd1382010-09-01 17:42:29 -07007632 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007633 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007634 }
Chet Haase55dbb652010-12-21 20:15:08 -08007635 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007636 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007637 }
7638 }
7639
7640 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 * Bottom position of this view relative to its parent.
7642 *
7643 * @return The bottom of this view, in pixels.
7644 */
7645 @ViewDebug.CapturedViewProperty
7646 public final int getBottom() {
7647 return mBottom;
7648 }
7649
7650 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007651 * True if this view has changed since the last time being drawn.
7652 *
7653 * @return The dirty state of this view.
7654 */
7655 public boolean isDirty() {
7656 return (mPrivateFlags & DIRTY_MASK) != 0;
7657 }
7658
7659 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007660 * Sets the bottom position of this view relative to its parent. This method is meant to be
7661 * called by the layout system and should not generally be called otherwise, because the
7662 * property may be changed at any time by the layout.
7663 *
7664 * @param bottom The bottom of this view, in pixels.
7665 */
7666 public final void setBottom(int bottom) {
7667 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007668 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007669 final boolean matrixIsIdentity = mTransformationInfo == null
7670 || mTransformationInfo.mMatrixIsIdentity;
7671 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007672 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007673 int maxBottom;
7674 if (bottom < mBottom) {
7675 maxBottom = mBottom;
7676 } else {
7677 maxBottom = bottom;
7678 }
Chet Haasee9140a72011-02-16 16:23:29 -08007679 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007680 }
7681 } else {
7682 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007683 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007684 }
7685
Chet Haaseed032702010-10-01 14:05:54 -07007686 int width = mRight - mLeft;
7687 int oldHeight = mBottom - mTop;
7688
Chet Haase21cd1382010-09-01 17:42:29 -07007689 mBottom = bottom;
7690
Chet Haaseed032702010-10-01 14:05:54 -07007691 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7692
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007693 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007694 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7695 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007696 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007697 }
Chet Haase21cd1382010-09-01 17:42:29 -07007698 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007699 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007700 }
Chet Haase55dbb652010-12-21 20:15:08 -08007701 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007702 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007703 }
7704 }
7705
7706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 * Left position of this view relative to its parent.
7708 *
7709 * @return The left edge of this view, in pixels.
7710 */
7711 @ViewDebug.CapturedViewProperty
7712 public final int getLeft() {
7713 return mLeft;
7714 }
7715
7716 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007717 * Sets the left position of this view relative to its parent. This method is meant to be called
7718 * by the layout system and should not generally be called otherwise, because the property
7719 * may be changed at any time by the layout.
7720 *
7721 * @param left The bottom of this view, in pixels.
7722 */
7723 public final void setLeft(int left) {
7724 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007725 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007726 final boolean matrixIsIdentity = mTransformationInfo == null
7727 || mTransformationInfo.mMatrixIsIdentity;
7728 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007729 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007730 int minLeft;
7731 int xLoc;
7732 if (left < mLeft) {
7733 minLeft = left;
7734 xLoc = left - mLeft;
7735 } else {
7736 minLeft = mLeft;
7737 xLoc = 0;
7738 }
Chet Haasee9140a72011-02-16 16:23:29 -08007739 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007740 }
7741 } else {
7742 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007743 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007744 }
7745
Chet Haaseed032702010-10-01 14:05:54 -07007746 int oldWidth = mRight - mLeft;
7747 int height = mBottom - mTop;
7748
Chet Haase21cd1382010-09-01 17:42:29 -07007749 mLeft = left;
7750
Chet Haaseed032702010-10-01 14:05:54 -07007751 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7752
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007753 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007754 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7755 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007756 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007757 }
Chet Haase21cd1382010-09-01 17:42:29 -07007758 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007759 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007760 }
Chet Haase55dbb652010-12-21 20:15:08 -08007761 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007762 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007763 }
7764 }
7765
7766 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 * Right position of this view relative to its parent.
7768 *
7769 * @return The right edge of this view, in pixels.
7770 */
7771 @ViewDebug.CapturedViewProperty
7772 public final int getRight() {
7773 return mRight;
7774 }
7775
7776 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007777 * Sets the right position of this view relative to its parent. This method is meant to be called
7778 * by the layout system and should not generally be called otherwise, because the property
7779 * may be changed at any time by the layout.
7780 *
7781 * @param right The bottom of this view, in pixels.
7782 */
7783 public final void setRight(int right) {
7784 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007785 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007786 final boolean matrixIsIdentity = mTransformationInfo == null
7787 || mTransformationInfo.mMatrixIsIdentity;
7788 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007789 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007790 int maxRight;
7791 if (right < mRight) {
7792 maxRight = mRight;
7793 } else {
7794 maxRight = right;
7795 }
Chet Haasee9140a72011-02-16 16:23:29 -08007796 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007797 }
7798 } else {
7799 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007800 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007801 }
7802
Chet Haaseed032702010-10-01 14:05:54 -07007803 int oldWidth = mRight - mLeft;
7804 int height = mBottom - mTop;
7805
Chet Haase21cd1382010-09-01 17:42:29 -07007806 mRight = right;
7807
Chet Haaseed032702010-10-01 14:05:54 -07007808 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7809
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007810 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007811 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7812 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007813 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007814 }
Chet Haase21cd1382010-09-01 17:42:29 -07007815 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007816 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007817 }
Chet Haase55dbb652010-12-21 20:15:08 -08007818 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007819 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007820 }
7821 }
7822
7823 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007824 * The visual x position of this view, in pixels. This is equivalent to the
7825 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007826 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007827 *
Chet Haasedf030d22010-07-30 17:22:38 -07007828 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007829 */
Chet Haasedf030d22010-07-30 17:22:38 -07007830 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007831 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007832 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007833
Chet Haasedf030d22010-07-30 17:22:38 -07007834 /**
7835 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7836 * {@link #setTranslationX(float) translationX} property to be the difference between
7837 * the x value passed in and the current {@link #getLeft() left} property.
7838 *
7839 * @param x The visual x position of this view, in pixels.
7840 */
7841 public void setX(float x) {
7842 setTranslationX(x - mLeft);
7843 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007844
Chet Haasedf030d22010-07-30 17:22:38 -07007845 /**
7846 * The visual y position of this view, in pixels. This is equivalent to the
7847 * {@link #setTranslationY(float) translationY} property plus the current
7848 * {@link #getTop() top} property.
7849 *
7850 * @return The visual y position of this view, in pixels.
7851 */
7852 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007853 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007854 }
7855
7856 /**
7857 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7858 * {@link #setTranslationY(float) translationY} property to be the difference between
7859 * the y value passed in and the current {@link #getTop() top} property.
7860 *
7861 * @param y The visual y position of this view, in pixels.
7862 */
7863 public void setY(float y) {
7864 setTranslationY(y - mTop);
7865 }
7866
7867
7868 /**
7869 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7870 * This position is post-layout, in addition to wherever the object's
7871 * layout placed it.
7872 *
7873 * @return The horizontal position of this view relative to its left position, in pixels.
7874 */
7875 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007876 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007877 }
7878
7879 /**
7880 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7881 * This effectively positions the object post-layout, in addition to wherever the object's
7882 * layout placed it.
7883 *
7884 * @param translationX The horizontal position of this view relative to its left position,
7885 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007886 *
7887 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007888 */
7889 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007890 ensureTransformationInfo();
7891 final TransformationInfo info = mTransformationInfo;
7892 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007893 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007894 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007895 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007896 info.mTranslationX = translationX;
7897 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007898 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007899 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007900 }
7901 }
7902
7903 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007904 * The horizontal location of this view relative to its {@link #getTop() top} position.
7905 * This position is post-layout, in addition to wherever the object's
7906 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007907 *
Chet Haasedf030d22010-07-30 17:22:38 -07007908 * @return The vertical position of this view relative to its top position,
7909 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007910 */
Chet Haasedf030d22010-07-30 17:22:38 -07007911 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007912 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007913 }
7914
7915 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007916 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7917 * This effectively positions the object post-layout, in addition to wherever the object's
7918 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007919 *
Chet Haasedf030d22010-07-30 17:22:38 -07007920 * @param translationY The vertical position of this view relative to its top position,
7921 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007922 *
7923 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007924 */
Chet Haasedf030d22010-07-30 17:22:38 -07007925 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007926 ensureTransformationInfo();
7927 final TransformationInfo info = mTransformationInfo;
7928 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007929 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007930 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007931 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007932 info.mTranslationY = translationY;
7933 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007934 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007935 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007936 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007937 }
7938
7939 /**
Romain Guyda489792011-02-03 01:05:15 -08007940 * @hide
7941 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007942 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007943 ensureTransformationInfo();
7944 final TransformationInfo info = mTransformationInfo;
7945 info.mTranslationX = x;
7946 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007947 }
7948
7949 /**
7950 * @hide
7951 */
7952 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007953 ensureTransformationInfo();
7954 final TransformationInfo info = mTransformationInfo;
7955 info.mTranslationY = y;
7956 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007957 }
7958
7959 /**
7960 * @hide
7961 */
Romain Guyda489792011-02-03 01:05:15 -08007962 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007963 ensureTransformationInfo();
7964 final TransformationInfo info = mTransformationInfo;
7965 info.mTranslationX = x - mLeft;
7966 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007967 }
7968
7969 /**
7970 * @hide
7971 */
7972 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007973 ensureTransformationInfo();
7974 final TransformationInfo info = mTransformationInfo;
7975 info.mTranslationY = y - mTop;
7976 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007977 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007978
Romain Guyda489792011-02-03 01:05:15 -08007979 /**
7980 * @hide
7981 */
7982 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007983 ensureTransformationInfo();
7984 final TransformationInfo info = mTransformationInfo;
7985 info.mScaleX = x;
7986 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007987 }
7988
7989 /**
7990 * @hide
7991 */
7992 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007993 ensureTransformationInfo();
7994 final TransformationInfo info = mTransformationInfo;
7995 info.mScaleY = y;
7996 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007997 }
7998
7999 /**
8000 * @hide
8001 */
8002 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008003 ensureTransformationInfo();
8004 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08008005 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008006
Romain Guyda489792011-02-03 01:05:15 -08008007 /**
8008 * @hide
8009 */
8010 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008011 ensureTransformationInfo();
8012 final TransformationInfo info = mTransformationInfo;
8013 info.mRotationY = y;
8014 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008015 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008016
Romain Guyda489792011-02-03 01:05:15 -08008017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018 * Hit rectangle in parent's coordinates
8019 *
8020 * @param outRect The hit rectangle of the view.
8021 */
8022 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008023 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008024 final TransformationInfo info = mTransformationInfo;
8025 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008026 outRect.set(mLeft, mTop, mRight, mBottom);
8027 } else {
8028 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008029 tmpRect.set(-info.mPivotX, -info.mPivotY,
8030 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8031 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008032 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8033 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008034 }
8035 }
8036
8037 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008038 * Determines whether the given point, in local coordinates is inside the view.
8039 */
8040 /*package*/ final boolean pointInView(float localX, float localY) {
8041 return localX >= 0 && localX < (mRight - mLeft)
8042 && localY >= 0 && localY < (mBottom - mTop);
8043 }
8044
8045 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008046 * Utility method to determine whether the given point, in local coordinates,
8047 * is inside the view, where the area of the view is expanded by the slop factor.
8048 * This method is called while processing touch-move events to determine if the event
8049 * is still within the view.
8050 */
8051 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008052 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008053 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 }
8055
8056 /**
8057 * When a view has focus and the user navigates away from it, the next view is searched for
8058 * starting from the rectangle filled in by this method.
8059 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008060 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8061 * of the view. However, if your view maintains some idea of internal selection,
8062 * such as a cursor, or a selected row or column, you should override this method and
8063 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008064 *
8065 * @param r The rectangle to fill in, in this view's coordinates.
8066 */
8067 public void getFocusedRect(Rect r) {
8068 getDrawingRect(r);
8069 }
8070
8071 /**
8072 * If some part of this view is not clipped by any of its parents, then
8073 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008074 * coordinates (without taking possible View rotations into account), offset
8075 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8076 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 *
8078 * @param r If true is returned, r holds the global coordinates of the
8079 * visible portion of this view.
8080 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8081 * between this view and its root. globalOffet may be null.
8082 * @return true if r is non-empty (i.e. part of the view is visible at the
8083 * root level.
8084 */
8085 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8086 int width = mRight - mLeft;
8087 int height = mBottom - mTop;
8088 if (width > 0 && height > 0) {
8089 r.set(0, 0, width, height);
8090 if (globalOffset != null) {
8091 globalOffset.set(-mScrollX, -mScrollY);
8092 }
8093 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8094 }
8095 return false;
8096 }
8097
8098 public final boolean getGlobalVisibleRect(Rect r) {
8099 return getGlobalVisibleRect(r, null);
8100 }
8101
8102 public final boolean getLocalVisibleRect(Rect r) {
8103 Point offset = new Point();
8104 if (getGlobalVisibleRect(r, offset)) {
8105 r.offset(-offset.x, -offset.y); // make r local
8106 return true;
8107 }
8108 return false;
8109 }
8110
8111 /**
8112 * Offset this view's vertical location by the specified number of pixels.
8113 *
8114 * @param offset the number of pixels to offset the view by
8115 */
8116 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008117 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008118 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008119 final boolean matrixIsIdentity = mTransformationInfo == null
8120 || mTransformationInfo.mMatrixIsIdentity;
8121 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008122 final ViewParent p = mParent;
8123 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008124 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008125 int minTop;
8126 int maxBottom;
8127 int yLoc;
8128 if (offset < 0) {
8129 minTop = mTop + offset;
8130 maxBottom = mBottom;
8131 yLoc = offset;
8132 } else {
8133 minTop = mTop;
8134 maxBottom = mBottom + offset;
8135 yLoc = 0;
8136 }
8137 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8138 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008139 }
8140 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008141 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008142 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008143
Chet Haasec3aa3612010-06-17 08:50:37 -07008144 mTop += offset;
8145 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008146
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008147 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008149 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008150 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008151 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153 }
8154
8155 /**
8156 * Offset this view's horizontal location by the specified amount of pixels.
8157 *
8158 * @param offset the numer of pixels to offset the view by
8159 */
8160 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008161 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008162 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008163 final boolean matrixIsIdentity = mTransformationInfo == null
8164 || mTransformationInfo.mMatrixIsIdentity;
8165 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008166 final ViewParent p = mParent;
8167 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008168 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008169 int minLeft;
8170 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008171 if (offset < 0) {
8172 minLeft = mLeft + offset;
8173 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008174 } else {
8175 minLeft = mLeft;
8176 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008177 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008179 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008180 }
8181 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008182 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008183 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008184
Chet Haasec3aa3612010-06-17 08:50:37 -07008185 mLeft += offset;
8186 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008187
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008188 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008189 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008190 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008191 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008192 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008194 }
8195
8196 /**
8197 * Get the LayoutParams associated with this view. All views should have
8198 * layout parameters. These supply parameters to the <i>parent</i> of this
8199 * view specifying how it should be arranged. There are many subclasses of
8200 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8201 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008202 *
8203 * This method may return null if this View is not attached to a parent
8204 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8205 * was not invoked successfully. When a View is attached to a parent
8206 * ViewGroup, this method must not return null.
8207 *
8208 * @return The LayoutParams associated with this view, or null if no
8209 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008211 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 public ViewGroup.LayoutParams getLayoutParams() {
8213 return mLayoutParams;
8214 }
8215
8216 /**
8217 * Set the layout parameters associated with this view. These supply
8218 * parameters to the <i>parent</i> of this view specifying how it should be
8219 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8220 * correspond to the different subclasses of ViewGroup that are responsible
8221 * for arranging their children.
8222 *
Romain Guy01c174b2011-02-22 11:51:06 -08008223 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 */
8225 public void setLayoutParams(ViewGroup.LayoutParams params) {
8226 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008227 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 }
8229 mLayoutParams = params;
8230 requestLayout();
8231 }
8232
8233 /**
8234 * Set the scrolled position of your view. This will cause a call to
8235 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8236 * invalidated.
8237 * @param x the x position to scroll to
8238 * @param y the y position to scroll to
8239 */
8240 public void scrollTo(int x, int y) {
8241 if (mScrollX != x || mScrollY != y) {
8242 int oldX = mScrollX;
8243 int oldY = mScrollY;
8244 mScrollX = x;
8245 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008246 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008248 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008249 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 }
8252 }
8253
8254 /**
8255 * Move the scrolled position of your view. This will cause a call to
8256 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8257 * invalidated.
8258 * @param x the amount of pixels to scroll by horizontally
8259 * @param y the amount of pixels to scroll by vertically
8260 */
8261 public void scrollBy(int x, int y) {
8262 scrollTo(mScrollX + x, mScrollY + y);
8263 }
8264
8265 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008266 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8267 * animation to fade the scrollbars out after a default delay. If a subclass
8268 * provides animated scrolling, the start delay should equal the duration
8269 * of the scrolling animation.</p>
8270 *
8271 * <p>The animation starts only if at least one of the scrollbars is
8272 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8273 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8274 * this method returns true, and false otherwise. If the animation is
8275 * started, this method calls {@link #invalidate()}; in that case the
8276 * caller should not call {@link #invalidate()}.</p>
8277 *
8278 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008279 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008280 *
8281 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8282 * and {@link #scrollTo(int, int)}.</p>
8283 *
8284 * @return true if the animation is played, false otherwise
8285 *
8286 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008287 * @see #scrollBy(int, int)
8288 * @see #scrollTo(int, int)
8289 * @see #isHorizontalScrollBarEnabled()
8290 * @see #isVerticalScrollBarEnabled()
8291 * @see #setHorizontalScrollBarEnabled(boolean)
8292 * @see #setVerticalScrollBarEnabled(boolean)
8293 */
8294 protected boolean awakenScrollBars() {
8295 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008296 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008297 }
8298
8299 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008300 * Trigger the scrollbars to draw.
8301 * This method differs from awakenScrollBars() only in its default duration.
8302 * initialAwakenScrollBars() will show the scroll bars for longer than
8303 * usual to give the user more of a chance to notice them.
8304 *
8305 * @return true if the animation is played, false otherwise.
8306 */
8307 private boolean initialAwakenScrollBars() {
8308 return mScrollCache != null &&
8309 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8310 }
8311
8312 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008313 * <p>
8314 * Trigger the scrollbars to draw. When invoked this method starts an
8315 * animation to fade the scrollbars out after a fixed delay. If a subclass
8316 * provides animated scrolling, the start delay should equal the duration of
8317 * the scrolling animation.
8318 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008319 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008320 * <p>
8321 * The animation starts only if at least one of the scrollbars is enabled,
8322 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8323 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8324 * this method returns true, and false otherwise. If the animation is
8325 * started, this method calls {@link #invalidate()}; in that case the caller
8326 * should not call {@link #invalidate()}.
8327 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008328 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008329 * <p>
8330 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008331 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008332 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008333 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008334 * @param startDelay the delay, in milliseconds, after which the animation
8335 * should start; when the delay is 0, the animation starts
8336 * immediately
8337 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008338 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008339 * @see #scrollBy(int, int)
8340 * @see #scrollTo(int, int)
8341 * @see #isHorizontalScrollBarEnabled()
8342 * @see #isVerticalScrollBarEnabled()
8343 * @see #setHorizontalScrollBarEnabled(boolean)
8344 * @see #setVerticalScrollBarEnabled(boolean)
8345 */
8346 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008347 return awakenScrollBars(startDelay, true);
8348 }
Joe Malin32736f02011-01-19 16:14:20 -08008349
Mike Cleron290947b2009-09-29 18:34:32 -07008350 /**
8351 * <p>
8352 * Trigger the scrollbars to draw. When invoked this method starts an
8353 * animation to fade the scrollbars out after a fixed delay. If a subclass
8354 * provides animated scrolling, the start delay should equal the duration of
8355 * the scrolling animation.
8356 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008357 *
Mike Cleron290947b2009-09-29 18:34:32 -07008358 * <p>
8359 * The animation starts only if at least one of the scrollbars is enabled,
8360 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8361 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8362 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008363 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008364 * is set to true; in that case the caller
8365 * should not call {@link #invalidate()}.
8366 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008367 *
Mike Cleron290947b2009-09-29 18:34:32 -07008368 * <p>
8369 * This method should be invoked everytime a subclass directly updates the
8370 * scroll parameters.
8371 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008372 *
Mike Cleron290947b2009-09-29 18:34:32 -07008373 * @param startDelay the delay, in milliseconds, after which the animation
8374 * should start; when the delay is 0, the animation starts
8375 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008376 *
Mike Cleron290947b2009-09-29 18:34:32 -07008377 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008378 *
Mike Cleron290947b2009-09-29 18:34:32 -07008379 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008380 *
Mike Cleron290947b2009-09-29 18:34:32 -07008381 * @see #scrollBy(int, int)
8382 * @see #scrollTo(int, int)
8383 * @see #isHorizontalScrollBarEnabled()
8384 * @see #isVerticalScrollBarEnabled()
8385 * @see #setHorizontalScrollBarEnabled(boolean)
8386 * @see #setVerticalScrollBarEnabled(boolean)
8387 */
8388 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008389 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008390
Mike Cleronf116bf82009-09-27 19:14:12 -07008391 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8392 return false;
8393 }
8394
8395 if (scrollCache.scrollBar == null) {
8396 scrollCache.scrollBar = new ScrollBarDrawable();
8397 }
8398
8399 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8400
Mike Cleron290947b2009-09-29 18:34:32 -07008401 if (invalidate) {
8402 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008403 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008404 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008405
8406 if (scrollCache.state == ScrollabilityCache.OFF) {
8407 // FIXME: this is copied from WindowManagerService.
8408 // We should get this value from the system when it
8409 // is possible to do so.
8410 final int KEY_REPEAT_FIRST_DELAY = 750;
8411 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8412 }
8413
8414 // Tell mScrollCache when we should start fading. This may
8415 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008416 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008417 scrollCache.fadeStartTime = fadeStartTime;
8418 scrollCache.state = ScrollabilityCache.ON;
8419
8420 // Schedule our fader to run, unscheduling any old ones first
8421 if (mAttachInfo != null) {
8422 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8423 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8424 }
8425
8426 return true;
8427 }
8428
8429 return false;
8430 }
8431
8432 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008433 * Do not invalidate views which are not visible and which are not running an animation. They
8434 * will not get drawn and they should not set dirty flags as if they will be drawn
8435 */
8436 private boolean skipInvalidate() {
8437 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8438 (!(mParent instanceof ViewGroup) ||
8439 !((ViewGroup) mParent).isViewTransitioning(this));
8440 }
8441 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008442 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008443 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8444 * in the future. This must be called from a UI thread. To call from a non-UI
8445 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 *
8447 * WARNING: This method is destructive to dirty.
8448 * @param dirty the rectangle representing the bounds of the dirty region
8449 */
8450 public void invalidate(Rect dirty) {
8451 if (ViewDebug.TRACE_HIERARCHY) {
8452 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8453 }
8454
Chet Haaseaceafe62011-08-26 15:44:33 -07008455 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008456 return;
8457 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008458 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008459 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8460 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008462 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008463 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 final ViewParent p = mParent;
8465 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008466 //noinspection PointlessBooleanExpression,ConstantConditions
8467 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8468 if (p != null && ai != null && ai.mHardwareAccelerated) {
8469 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008470 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008471 p.invalidateChild(this, null);
8472 return;
8473 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 if (p != null && ai != null) {
8476 final int scrollX = mScrollX;
8477 final int scrollY = mScrollY;
8478 final Rect r = ai.mTmpInvalRect;
8479 r.set(dirty.left - scrollX, dirty.top - scrollY,
8480 dirty.right - scrollX, dirty.bottom - scrollY);
8481 mParent.invalidateChild(this, r);
8482 }
8483 }
8484 }
8485
8486 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008487 * Mark the area defined by the rect (l,t,r,b) as needing to be drawn.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008489 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8490 * will be called at some point in the future. This must be called from
8491 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 * @param l the left position of the dirty region
8493 * @param t the top position of the dirty region
8494 * @param r the right position of the dirty region
8495 * @param b the bottom position of the dirty region
8496 */
8497 public void invalidate(int l, int t, int r, int b) {
8498 if (ViewDebug.TRACE_HIERARCHY) {
8499 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8500 }
8501
Chet Haaseaceafe62011-08-26 15:44:33 -07008502 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008503 return;
8504 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008505 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008506 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8507 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008509 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008510 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 final ViewParent p = mParent;
8512 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008513 //noinspection PointlessBooleanExpression,ConstantConditions
8514 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8515 if (p != null && ai != null && ai.mHardwareAccelerated) {
8516 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008517 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008518 p.invalidateChild(this, null);
8519 return;
8520 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008522 if (p != null && ai != null && l < r && t < b) {
8523 final int scrollX = mScrollX;
8524 final int scrollY = mScrollY;
8525 final Rect tmpr = ai.mTmpInvalRect;
8526 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8527 p.invalidateChild(this, tmpr);
8528 }
8529 }
8530 }
8531
8532 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008533 * Invalidate the whole view. If the view is visible,
8534 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8535 * the future. This must be called from a UI thread. To call from a non-UI thread,
8536 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 */
8538 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008539 invalidate(true);
8540 }
Joe Malin32736f02011-01-19 16:14:20 -08008541
Chet Haaseed032702010-10-01 14:05:54 -07008542 /**
8543 * This is where the invalidate() work actually happens. A full invalidate()
8544 * causes the drawing cache to be invalidated, but this function can be called with
8545 * invalidateCache set to false to skip that invalidation step for cases that do not
8546 * need it (for example, a component that remains at the same dimensions with the same
8547 * content).
8548 *
8549 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8550 * well. This is usually true for a full invalidate, but may be set to false if the
8551 * View's contents or dimensions have not changed.
8552 */
Romain Guy849d0a32011-02-01 17:20:48 -08008553 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 if (ViewDebug.TRACE_HIERARCHY) {
8555 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8556 }
8557
Chet Haaseaceafe62011-08-26 15:44:33 -07008558 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008559 return;
8560 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008561 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008562 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008563 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8564 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008565 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008566 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008567 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008568 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008569 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008572 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008573 //noinspection PointlessBooleanExpression,ConstantConditions
8574 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8575 if (p != null && ai != null && ai.mHardwareAccelerated) {
8576 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008577 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008578 p.invalidateChild(this, null);
8579 return;
8580 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008581 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 if (p != null && ai != null) {
8584 final Rect r = ai.mTmpInvalRect;
8585 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8586 // Don't call invalidate -- we don't want to internally scroll
8587 // our own bounds
8588 p.invalidateChild(this, r);
8589 }
8590 }
8591 }
8592
8593 /**
Romain Guyda489792011-02-03 01:05:15 -08008594 * @hide
8595 */
8596 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008597 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008598 return;
8599 }
Romain Guyda489792011-02-03 01:05:15 -08008600 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8601 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8602 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8603 if (mParent instanceof View) {
8604 ((View) mParent).mPrivateFlags |= INVALIDATED;
8605 }
8606 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008607 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008608 mPrivateFlags |= INVALIDATED;
8609 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008610 if (mParent != null && mAttachInfo != null) {
8611 if (mAttachInfo.mHardwareAccelerated) {
8612 mParent.invalidateChild(this, null);
8613 } else {
8614 final Rect r = mAttachInfo.mTmpInvalRect;
8615 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8616 // Don't call invalidate -- we don't want to internally scroll
8617 // our own bounds
8618 mParent.invalidateChild(this, r);
8619 }
Romain Guyda489792011-02-03 01:05:15 -08008620 }
8621 }
8622 }
8623
8624 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008625 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008626 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8627 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008628 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8629 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008630 *
8631 * @hide
8632 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008633 protected void invalidateParentCaches() {
8634 if (mParent instanceof View) {
8635 ((View) mParent).mPrivateFlags |= INVALIDATED;
8636 }
8637 }
Joe Malin32736f02011-01-19 16:14:20 -08008638
Romain Guy0fd89bf2011-01-26 15:41:30 -08008639 /**
8640 * Used to indicate that the parent of this view should be invalidated. This functionality
8641 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8642 * which is necessary when various parent-managed properties of the view change, such as
8643 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8644 * an invalidation event to the parent.
8645 *
8646 * @hide
8647 */
8648 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008649 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008650 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008651 }
8652 }
8653
8654 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008655 * Indicates whether this View is opaque. An opaque View guarantees that it will
8656 * draw all the pixels overlapping its bounds using a fully opaque color.
8657 *
8658 * Subclasses of View should override this method whenever possible to indicate
8659 * whether an instance is opaque. Opaque Views are treated in a special way by
8660 * the View hierarchy, possibly allowing it to perform optimizations during
8661 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008662 *
Romain Guy24443ea2009-05-11 11:56:30 -07008663 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008664 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008665 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008666 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008667 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008668 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8669 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008670 }
8671
Adam Powell20232d02010-12-08 21:08:53 -08008672 /**
8673 * @hide
8674 */
8675 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008676 // Opaque if:
8677 // - Has a background
8678 // - Background is opaque
8679 // - Doesn't have scrollbars or scrollbars are inside overlay
8680
8681 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8682 mPrivateFlags |= OPAQUE_BACKGROUND;
8683 } else {
8684 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8685 }
8686
8687 final int flags = mViewFlags;
8688 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8689 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8690 mPrivateFlags |= OPAQUE_SCROLLBARS;
8691 } else {
8692 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8693 }
8694 }
8695
8696 /**
8697 * @hide
8698 */
8699 protected boolean hasOpaqueScrollbars() {
8700 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008701 }
8702
8703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008704 * @return A handler associated with the thread running the View. This
8705 * handler can be used to pump events in the UI events queue.
8706 */
8707 public Handler getHandler() {
8708 if (mAttachInfo != null) {
8709 return mAttachInfo.mHandler;
8710 }
8711 return null;
8712 }
8713
8714 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008715 * <p>Causes the Runnable to be added to the message queue.
8716 * The runnable will be run on the user interface thread.</p>
8717 *
8718 * <p>This method can be invoked from outside of the UI thread
8719 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 *
8721 * @param action The Runnable that will be executed.
8722 *
8723 * @return Returns true if the Runnable was successfully placed in to the
8724 * message queue. Returns false on failure, usually because the
8725 * looper processing the message queue is exiting.
8726 */
8727 public boolean post(Runnable action) {
8728 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008729 AttachInfo attachInfo = mAttachInfo;
8730 if (attachInfo != null) {
8731 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008732 } else {
8733 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008734 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 return true;
8736 }
8737
8738 return handler.post(action);
8739 }
8740
8741 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008742 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008743 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008744 * The runnable will be run on the user interface thread.</p>
8745 *
8746 * <p>This method can be invoked from outside of the UI thread
8747 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 *
8749 * @param action The Runnable that will be executed.
8750 * @param delayMillis The delay (in milliseconds) until the Runnable
8751 * will be executed.
8752 *
8753 * @return true if the Runnable was successfully placed in to the
8754 * message queue. Returns false on failure, usually because the
8755 * looper processing the message queue is exiting. Note that a
8756 * result of true does not mean the Runnable will be processed --
8757 * if the looper is quit before the delivery time of the message
8758 * occurs then the message will be dropped.
8759 */
8760 public boolean postDelayed(Runnable action, long delayMillis) {
8761 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008762 AttachInfo attachInfo = mAttachInfo;
8763 if (attachInfo != null) {
8764 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 } else {
8766 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008767 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 return true;
8769 }
8770
8771 return handler.postDelayed(action, delayMillis);
8772 }
8773
8774 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008775 * <p>Removes the specified Runnable from the message queue.</p>
8776 *
8777 * <p>This method can be invoked from outside of the UI thread
8778 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 *
8780 * @param action The Runnable to remove from the message handling queue
8781 *
8782 * @return true if this view could ask the Handler to remove the Runnable,
8783 * false otherwise. When the returned value is true, the Runnable
8784 * may or may not have been actually removed from the message queue
8785 * (for instance, if the Runnable was not in the queue already.)
8786 */
8787 public boolean removeCallbacks(Runnable action) {
8788 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008789 AttachInfo attachInfo = mAttachInfo;
8790 if (attachInfo != null) {
8791 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 } else {
8793 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008794 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 return true;
8796 }
8797
8798 handler.removeCallbacks(action);
8799 return true;
8800 }
8801
8802 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008803 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8804 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008805 *
Romain Guye63a4f32011-08-11 11:33:31 -07008806 * <p>This method can be invoked from outside of the UI thread
8807 * only when this View is attached to a window.</p>
8808 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 * @see #invalidate()
8810 */
8811 public void postInvalidate() {
8812 postInvalidateDelayed(0);
8813 }
8814
8815 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008816 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8817 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8818 *
8819 * <p>This method can be invoked from outside of the UI thread
8820 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 *
8822 * @param left The left coordinate of the rectangle to invalidate.
8823 * @param top The top coordinate of the rectangle to invalidate.
8824 * @param right The right coordinate of the rectangle to invalidate.
8825 * @param bottom The bottom coordinate of the rectangle to invalidate.
8826 *
8827 * @see #invalidate(int, int, int, int)
8828 * @see #invalidate(Rect)
8829 */
8830 public void postInvalidate(int left, int top, int right, int bottom) {
8831 postInvalidateDelayed(0, left, top, right, bottom);
8832 }
8833
8834 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008835 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8836 * loop. Waits for the specified amount of time.</p>
8837 *
8838 * <p>This method can be invoked from outside of the UI thread
8839 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008840 *
8841 * @param delayMilliseconds the duration in milliseconds to delay the
8842 * invalidation by
8843 */
8844 public void postInvalidateDelayed(long delayMilliseconds) {
8845 // We try only with the AttachInfo because there's no point in invalidating
8846 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008847 AttachInfo attachInfo = mAttachInfo;
8848 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008849 Message msg = Message.obtain();
8850 msg.what = AttachInfo.INVALIDATE_MSG;
8851 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008852 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 }
8854 }
8855
8856 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008857 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8858 * through the event loop. Waits for the specified amount of time.</p>
8859 *
8860 * <p>This method can be invoked from outside of the UI thread
8861 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008862 *
8863 * @param delayMilliseconds the duration in milliseconds to delay the
8864 * invalidation by
8865 * @param left The left coordinate of the rectangle to invalidate.
8866 * @param top The top coordinate of the rectangle to invalidate.
8867 * @param right The right coordinate of the rectangle to invalidate.
8868 * @param bottom The bottom coordinate of the rectangle to invalidate.
8869 */
8870 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8871 int right, int bottom) {
8872
8873 // We try only with the AttachInfo because there's no point in invalidating
8874 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008875 AttachInfo attachInfo = mAttachInfo;
8876 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008877 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8878 info.target = this;
8879 info.left = left;
8880 info.top = top;
8881 info.right = right;
8882 info.bottom = bottom;
8883
8884 final Message msg = Message.obtain();
8885 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8886 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008887 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 }
8889 }
8890
8891 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008892 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8893 * This event is sent at most once every
8894 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8895 */
8896 private void postSendViewScrolledAccessibilityEventCallback() {
8897 if (mSendViewScrolledAccessibilityEvent == null) {
8898 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8899 }
8900 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8901 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8902 postDelayed(mSendViewScrolledAccessibilityEvent,
8903 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8904 }
8905 }
8906
8907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 * Called by a parent to request that a child update its values for mScrollX
8909 * and mScrollY if necessary. This will typically be done if the child is
8910 * animating a scroll using a {@link android.widget.Scroller Scroller}
8911 * object.
8912 */
8913 public void computeScroll() {
8914 }
8915
8916 /**
8917 * <p>Indicate whether the horizontal edges are faded when the view is
8918 * scrolled horizontally.</p>
8919 *
8920 * @return true if the horizontal edges should are faded on scroll, false
8921 * otherwise
8922 *
8923 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008924 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008925 */
8926 public boolean isHorizontalFadingEdgeEnabled() {
8927 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8928 }
8929
8930 /**
8931 * <p>Define whether the horizontal edges should be faded when this view
8932 * is scrolled horizontally.</p>
8933 *
8934 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8935 * be faded when the view is scrolled
8936 * horizontally
8937 *
8938 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008939 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008940 */
8941 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8942 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8943 if (horizontalFadingEdgeEnabled) {
8944 initScrollCache();
8945 }
8946
8947 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8948 }
8949 }
8950
8951 /**
8952 * <p>Indicate whether the vertical edges are faded when the view is
8953 * scrolled horizontally.</p>
8954 *
8955 * @return true if the vertical edges should are faded on scroll, false
8956 * otherwise
8957 *
8958 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008959 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 */
8961 public boolean isVerticalFadingEdgeEnabled() {
8962 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8963 }
8964
8965 /**
8966 * <p>Define whether the vertical edges should be faded when this view
8967 * is scrolled vertically.</p>
8968 *
8969 * @param verticalFadingEdgeEnabled true if the vertical edges should
8970 * be faded when the view is scrolled
8971 * vertically
8972 *
8973 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008974 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 */
8976 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8977 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8978 if (verticalFadingEdgeEnabled) {
8979 initScrollCache();
8980 }
8981
8982 mViewFlags ^= FADING_EDGE_VERTICAL;
8983 }
8984 }
8985
8986 /**
8987 * Returns the strength, or intensity, of the top faded edge. The strength is
8988 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8989 * returns 0.0 or 1.0 but no value in between.
8990 *
8991 * Subclasses should override this method to provide a smoother fade transition
8992 * when scrolling occurs.
8993 *
8994 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8995 */
8996 protected float getTopFadingEdgeStrength() {
8997 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8998 }
8999
9000 /**
9001 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9002 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9003 * returns 0.0 or 1.0 but no value in between.
9004 *
9005 * Subclasses should override this method to provide a smoother fade transition
9006 * when scrolling occurs.
9007 *
9008 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9009 */
9010 protected float getBottomFadingEdgeStrength() {
9011 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9012 computeVerticalScrollRange() ? 1.0f : 0.0f;
9013 }
9014
9015 /**
9016 * Returns the strength, or intensity, of the left faded edge. The strength is
9017 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9018 * returns 0.0 or 1.0 but no value in between.
9019 *
9020 * Subclasses should override this method to provide a smoother fade transition
9021 * when scrolling occurs.
9022 *
9023 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9024 */
9025 protected float getLeftFadingEdgeStrength() {
9026 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9027 }
9028
9029 /**
9030 * Returns the strength, or intensity, of the right faded edge. The strength is
9031 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9032 * returns 0.0 or 1.0 but no value in between.
9033 *
9034 * Subclasses should override this method to provide a smoother fade transition
9035 * when scrolling occurs.
9036 *
9037 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9038 */
9039 protected float getRightFadingEdgeStrength() {
9040 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9041 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9042 }
9043
9044 /**
9045 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9046 * scrollbar is not drawn by default.</p>
9047 *
9048 * @return true if the horizontal scrollbar should be painted, false
9049 * otherwise
9050 *
9051 * @see #setHorizontalScrollBarEnabled(boolean)
9052 */
9053 public boolean isHorizontalScrollBarEnabled() {
9054 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9055 }
9056
9057 /**
9058 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9059 * scrollbar is not drawn by default.</p>
9060 *
9061 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9062 * be painted
9063 *
9064 * @see #isHorizontalScrollBarEnabled()
9065 */
9066 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9067 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9068 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009069 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009070 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009071 }
9072 }
9073
9074 /**
9075 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9076 * scrollbar is not drawn by default.</p>
9077 *
9078 * @return true if the vertical scrollbar should be painted, false
9079 * otherwise
9080 *
9081 * @see #setVerticalScrollBarEnabled(boolean)
9082 */
9083 public boolean isVerticalScrollBarEnabled() {
9084 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9085 }
9086
9087 /**
9088 * <p>Define whether the vertical scrollbar should be drawn or not. The
9089 * scrollbar is not drawn by default.</p>
9090 *
9091 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9092 * be painted
9093 *
9094 * @see #isVerticalScrollBarEnabled()
9095 */
9096 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9097 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9098 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009099 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009100 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 }
9102 }
9103
Adam Powell20232d02010-12-08 21:08:53 -08009104 /**
9105 * @hide
9106 */
9107 protected void recomputePadding() {
9108 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 }
Joe Malin32736f02011-01-19 16:14:20 -08009110
Mike Cleronfe81d382009-09-28 14:22:16 -07009111 /**
9112 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009113 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009114 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009115 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009116 */
9117 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9118 initScrollCache();
9119 final ScrollabilityCache scrollabilityCache = mScrollCache;
9120 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009121 if (fadeScrollbars) {
9122 scrollabilityCache.state = ScrollabilityCache.OFF;
9123 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009124 scrollabilityCache.state = ScrollabilityCache.ON;
9125 }
9126 }
Joe Malin32736f02011-01-19 16:14:20 -08009127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009128 /**
Joe Malin32736f02011-01-19 16:14:20 -08009129 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009130 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009131 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009132 * @return true if scrollbar fading is enabled
9133 */
9134 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009135 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009136 }
Joe Malin32736f02011-01-19 16:14:20 -08009137
Mike Cleron52f0a642009-09-28 18:21:37 -07009138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9140 * inset. When inset, they add to the padding of the view. And the scrollbars
9141 * can be drawn inside the padding area or on the edge of the view. For example,
9142 * if a view has a background drawable and you want to draw the scrollbars
9143 * inside the padding specified by the drawable, you can use
9144 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9145 * appear at the edge of the view, ignoring the padding, then you can use
9146 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9147 * @param style the style of the scrollbars. Should be one of
9148 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9149 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9150 * @see #SCROLLBARS_INSIDE_OVERLAY
9151 * @see #SCROLLBARS_INSIDE_INSET
9152 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9153 * @see #SCROLLBARS_OUTSIDE_INSET
9154 */
9155 public void setScrollBarStyle(int style) {
9156 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9157 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009158 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009159 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 }
9161 }
9162
9163 /**
9164 * <p>Returns the current scrollbar style.</p>
9165 * @return the current scrollbar style
9166 * @see #SCROLLBARS_INSIDE_OVERLAY
9167 * @see #SCROLLBARS_INSIDE_INSET
9168 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9169 * @see #SCROLLBARS_OUTSIDE_INSET
9170 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009171 @ViewDebug.ExportedProperty(mapping = {
9172 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9173 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9174 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9175 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9176 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009177 public int getScrollBarStyle() {
9178 return mViewFlags & SCROLLBARS_STYLE_MASK;
9179 }
9180
9181 /**
9182 * <p>Compute the horizontal range that the horizontal scrollbar
9183 * represents.</p>
9184 *
9185 * <p>The range is expressed in arbitrary units that must be the same as the
9186 * units used by {@link #computeHorizontalScrollExtent()} and
9187 * {@link #computeHorizontalScrollOffset()}.</p>
9188 *
9189 * <p>The default range is the drawing width of this view.</p>
9190 *
9191 * @return the total horizontal range represented by the horizontal
9192 * scrollbar
9193 *
9194 * @see #computeHorizontalScrollExtent()
9195 * @see #computeHorizontalScrollOffset()
9196 * @see android.widget.ScrollBarDrawable
9197 */
9198 protected int computeHorizontalScrollRange() {
9199 return getWidth();
9200 }
9201
9202 /**
9203 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9204 * within the horizontal range. This value is used to compute the position
9205 * of the thumb within the scrollbar's track.</p>
9206 *
9207 * <p>The range is expressed in arbitrary units that must be the same as the
9208 * units used by {@link #computeHorizontalScrollRange()} and
9209 * {@link #computeHorizontalScrollExtent()}.</p>
9210 *
9211 * <p>The default offset is the scroll offset of this view.</p>
9212 *
9213 * @return the horizontal offset of the scrollbar's thumb
9214 *
9215 * @see #computeHorizontalScrollRange()
9216 * @see #computeHorizontalScrollExtent()
9217 * @see android.widget.ScrollBarDrawable
9218 */
9219 protected int computeHorizontalScrollOffset() {
9220 return mScrollX;
9221 }
9222
9223 /**
9224 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9225 * within the horizontal range. This value is used to compute the length
9226 * of the thumb within the scrollbar's track.</p>
9227 *
9228 * <p>The range is expressed in arbitrary units that must be the same as the
9229 * units used by {@link #computeHorizontalScrollRange()} and
9230 * {@link #computeHorizontalScrollOffset()}.</p>
9231 *
9232 * <p>The default extent is the drawing width of this view.</p>
9233 *
9234 * @return the horizontal extent of the scrollbar's thumb
9235 *
9236 * @see #computeHorizontalScrollRange()
9237 * @see #computeHorizontalScrollOffset()
9238 * @see android.widget.ScrollBarDrawable
9239 */
9240 protected int computeHorizontalScrollExtent() {
9241 return getWidth();
9242 }
9243
9244 /**
9245 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9246 *
9247 * <p>The range is expressed in arbitrary units that must be the same as the
9248 * units used by {@link #computeVerticalScrollExtent()} and
9249 * {@link #computeVerticalScrollOffset()}.</p>
9250 *
9251 * @return the total vertical range represented by the vertical scrollbar
9252 *
9253 * <p>The default range is the drawing height of this view.</p>
9254 *
9255 * @see #computeVerticalScrollExtent()
9256 * @see #computeVerticalScrollOffset()
9257 * @see android.widget.ScrollBarDrawable
9258 */
9259 protected int computeVerticalScrollRange() {
9260 return getHeight();
9261 }
9262
9263 /**
9264 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9265 * within the horizontal range. This value is used to compute the position
9266 * of the thumb within the scrollbar's track.</p>
9267 *
9268 * <p>The range is expressed in arbitrary units that must be the same as the
9269 * units used by {@link #computeVerticalScrollRange()} and
9270 * {@link #computeVerticalScrollExtent()}.</p>
9271 *
9272 * <p>The default offset is the scroll offset of this view.</p>
9273 *
9274 * @return the vertical offset of the scrollbar's thumb
9275 *
9276 * @see #computeVerticalScrollRange()
9277 * @see #computeVerticalScrollExtent()
9278 * @see android.widget.ScrollBarDrawable
9279 */
9280 protected int computeVerticalScrollOffset() {
9281 return mScrollY;
9282 }
9283
9284 /**
9285 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9286 * within the vertical range. This value is used to compute the length
9287 * of the thumb within the scrollbar's track.</p>
9288 *
9289 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009290 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009291 * {@link #computeVerticalScrollOffset()}.</p>
9292 *
9293 * <p>The default extent is the drawing height of this view.</p>
9294 *
9295 * @return the vertical extent of the scrollbar's thumb
9296 *
9297 * @see #computeVerticalScrollRange()
9298 * @see #computeVerticalScrollOffset()
9299 * @see android.widget.ScrollBarDrawable
9300 */
9301 protected int computeVerticalScrollExtent() {
9302 return getHeight();
9303 }
9304
9305 /**
Adam Powell69159442011-06-13 17:53:06 -07009306 * Check if this view can be scrolled horizontally in a certain direction.
9307 *
9308 * @param direction Negative to check scrolling left, positive to check scrolling right.
9309 * @return true if this view can be scrolled in the specified direction, false otherwise.
9310 */
9311 public boolean canScrollHorizontally(int direction) {
9312 final int offset = computeHorizontalScrollOffset();
9313 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9314 if (range == 0) return false;
9315 if (direction < 0) {
9316 return offset > 0;
9317 } else {
9318 return offset < range - 1;
9319 }
9320 }
9321
9322 /**
9323 * Check if this view can be scrolled vertically in a certain direction.
9324 *
9325 * @param direction Negative to check scrolling up, positive to check scrolling down.
9326 * @return true if this view can be scrolled in the specified direction, false otherwise.
9327 */
9328 public boolean canScrollVertically(int direction) {
9329 final int offset = computeVerticalScrollOffset();
9330 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9331 if (range == 0) return false;
9332 if (direction < 0) {
9333 return offset > 0;
9334 } else {
9335 return offset < range - 1;
9336 }
9337 }
9338
9339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9341 * scrollbars are painted only if they have been awakened first.</p>
9342 *
9343 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009344 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009345 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009347 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009348 // scrollbars are drawn only when the animation is running
9349 final ScrollabilityCache cache = mScrollCache;
9350 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009351
Mike Cleronf116bf82009-09-27 19:14:12 -07009352 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009353
Mike Cleronf116bf82009-09-27 19:14:12 -07009354 if (state == ScrollabilityCache.OFF) {
9355 return;
9356 }
Joe Malin32736f02011-01-19 16:14:20 -08009357
Mike Cleronf116bf82009-09-27 19:14:12 -07009358 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009359
Mike Cleronf116bf82009-09-27 19:14:12 -07009360 if (state == ScrollabilityCache.FADING) {
9361 // We're fading -- get our fade interpolation
9362 if (cache.interpolatorValues == null) {
9363 cache.interpolatorValues = new float[1];
9364 }
Joe Malin32736f02011-01-19 16:14:20 -08009365
Mike Cleronf116bf82009-09-27 19:14:12 -07009366 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009367
Mike Cleronf116bf82009-09-27 19:14:12 -07009368 // Stops the animation if we're done
9369 if (cache.scrollBarInterpolator.timeToValues(values) ==
9370 Interpolator.Result.FREEZE_END) {
9371 cache.state = ScrollabilityCache.OFF;
9372 } else {
9373 cache.scrollBar.setAlpha(Math.round(values[0]));
9374 }
Joe Malin32736f02011-01-19 16:14:20 -08009375
9376 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009377 // drawing. We only want this when we're fading so that
9378 // we prevent excessive redraws
9379 invalidate = true;
9380 } else {
9381 // We're just on -- but we may have been fading before so
9382 // reset alpha
9383 cache.scrollBar.setAlpha(255);
9384 }
9385
Joe Malin32736f02011-01-19 16:14:20 -08009386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 final int viewFlags = mViewFlags;
9388
9389 final boolean drawHorizontalScrollBar =
9390 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9391 final boolean drawVerticalScrollBar =
9392 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9393 && !isVerticalScrollBarHidden();
9394
9395 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9396 final int width = mRight - mLeft;
9397 final int height = mBottom - mTop;
9398
9399 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009400
Mike Reede8853fc2009-09-04 14:01:48 -04009401 final int scrollX = mScrollX;
9402 final int scrollY = mScrollY;
9403 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9404
Mike Cleronf116bf82009-09-27 19:14:12 -07009405 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009408 int size = scrollBar.getSize(false);
9409 if (size <= 0) {
9410 size = cache.scrollBarSize;
9411 }
9412
Mike Cleronf116bf82009-09-27 19:14:12 -07009413 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009414 computeHorizontalScrollOffset(),
9415 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009416 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009417 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009418 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009419 left = scrollX + (mPaddingLeft & inside);
9420 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9421 bottom = top + size;
9422 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9423 if (invalidate) {
9424 invalidate(left, top, right, bottom);
9425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 }
9427
9428 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009429 int size = scrollBar.getSize(true);
9430 if (size <= 0) {
9431 size = cache.scrollBarSize;
9432 }
9433
Mike Reede8853fc2009-09-04 14:01:48 -04009434 scrollBar.setParameters(computeVerticalScrollRange(),
9435 computeVerticalScrollOffset(),
9436 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009437 switch (mVerticalScrollbarPosition) {
9438 default:
9439 case SCROLLBAR_POSITION_DEFAULT:
9440 case SCROLLBAR_POSITION_RIGHT:
9441 left = scrollX + width - size - (mUserPaddingRight & inside);
9442 break;
9443 case SCROLLBAR_POSITION_LEFT:
9444 left = scrollX + (mUserPaddingLeft & inside);
9445 break;
9446 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009447 top = scrollY + (mPaddingTop & inside);
9448 right = left + size;
9449 bottom = scrollY + height - (mUserPaddingBottom & inside);
9450 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9451 if (invalidate) {
9452 invalidate(left, top, right, bottom);
9453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009454 }
9455 }
9456 }
9457 }
Romain Guy8506ab42009-06-11 17:35:47 -07009458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009459 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009460 * 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 -08009461 * FastScroller is visible.
9462 * @return whether to temporarily hide the vertical scrollbar
9463 * @hide
9464 */
9465 protected boolean isVerticalScrollBarHidden() {
9466 return false;
9467 }
9468
9469 /**
9470 * <p>Draw the horizontal scrollbar if
9471 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9472 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 * @param canvas the canvas on which to draw the scrollbar
9474 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 *
9476 * @see #isHorizontalScrollBarEnabled()
9477 * @see #computeHorizontalScrollRange()
9478 * @see #computeHorizontalScrollExtent()
9479 * @see #computeHorizontalScrollOffset()
9480 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009481 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009482 */
Romain Guy8fb95422010-08-17 18:38:51 -07009483 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9484 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009485 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009486 scrollBar.draw(canvas);
9487 }
Mike Reede8853fc2009-09-04 14:01:48 -04009488
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9491 * returns true.</p>
9492 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 * @param canvas the canvas on which to draw the scrollbar
9494 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009495 *
9496 * @see #isVerticalScrollBarEnabled()
9497 * @see #computeVerticalScrollRange()
9498 * @see #computeVerticalScrollExtent()
9499 * @see #computeVerticalScrollOffset()
9500 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009501 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 */
Romain Guy8fb95422010-08-17 18:38:51 -07009503 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9504 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009505 scrollBar.setBounds(l, t, r, b);
9506 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009507 }
9508
9509 /**
9510 * Implement this to do your drawing.
9511 *
9512 * @param canvas the canvas on which the background will be drawn
9513 */
9514 protected void onDraw(Canvas canvas) {
9515 }
9516
9517 /*
9518 * Caller is responsible for calling requestLayout if necessary.
9519 * (This allows addViewInLayout to not request a new layout.)
9520 */
9521 void assignParent(ViewParent parent) {
9522 if (mParent == null) {
9523 mParent = parent;
9524 } else if (parent == null) {
9525 mParent = null;
9526 } else {
9527 throw new RuntimeException("view " + this + " being added, but"
9528 + " it already has a parent");
9529 }
9530 }
9531
9532 /**
9533 * This is called when the view is attached to a window. At this point it
9534 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009535 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9536 * however it may be called any time before the first onDraw -- including
9537 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009538 *
9539 * @see #onDetachedFromWindow()
9540 */
9541 protected void onAttachedToWindow() {
9542 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9543 mParent.requestTransparentRegion(this);
9544 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009545 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9546 initialAwakenScrollBars();
9547 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9548 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009549 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009550 // Order is important here: LayoutDirection MUST be resolved before Padding
9551 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009552 resolveLayoutDirectionIfNeeded();
9553 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009554 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009555 if (isFocused()) {
9556 InputMethodManager imm = InputMethodManager.peekInstance();
9557 imm.focusIn(this);
9558 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009559 }
Cibu Johny86666632010-02-22 13:01:02 -08009560
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009561 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009562 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9563 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009564 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009565 private void resolveLayoutDirectionIfNeeded() {
9566 // Do not resolve if it is not needed
9567 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9568
9569 // Clear any previous layout direction resolution
9570 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9571
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009572 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9573 // TextDirectionHeuristic
9574 resetResolvedTextDirection();
9575
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009576 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009577 switch (getLayoutDirection()) {
9578 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009579 // We cannot do the resolution if there is no parent
9580 if (mParent == null) return;
9581
Cibu Johny86666632010-02-22 13:01:02 -08009582 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009583 if (mParent instanceof ViewGroup) {
9584 ViewGroup viewGroup = ((ViewGroup) mParent);
9585
9586 // Check if the parent view group can resolve
9587 if (! viewGroup.canResolveLayoutDirection()) {
9588 return;
9589 }
9590 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9591 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9592 }
Cibu Johny86666632010-02-22 13:01:02 -08009593 }
9594 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009595 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009596 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009597 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009598 case LAYOUT_DIRECTION_LOCALE:
9599 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009600 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009601 }
9602 break;
9603 default:
9604 // Nothing to do, LTR by default
9605 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009606
9607 // Set to resolved
9608 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9609 }
9610
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009611 /**
9612 * @hide
9613 */
9614 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009615 // If the user specified the absolute padding (either with android:padding or
9616 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9617 // use the default padding or the padding from the background drawable
9618 // (stored at this point in mPadding*)
9619 switch (getResolvedLayoutDirection()) {
9620 case LAYOUT_DIRECTION_RTL:
9621 // Start user padding override Right user padding. Otherwise, if Right user
9622 // padding is not defined, use the default Right padding. If Right user padding
9623 // is defined, just use it.
9624 if (mUserPaddingStart >= 0) {
9625 mUserPaddingRight = mUserPaddingStart;
9626 } else if (mUserPaddingRight < 0) {
9627 mUserPaddingRight = mPaddingRight;
9628 }
9629 if (mUserPaddingEnd >= 0) {
9630 mUserPaddingLeft = mUserPaddingEnd;
9631 } else if (mUserPaddingLeft < 0) {
9632 mUserPaddingLeft = mPaddingLeft;
9633 }
9634 break;
9635 case LAYOUT_DIRECTION_LTR:
9636 default:
9637 // Start user padding override Left user padding. Otherwise, if Left user
9638 // padding is not defined, use the default left padding. If Left user padding
9639 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009640 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009641 mUserPaddingLeft = mUserPaddingStart;
9642 } else if (mUserPaddingLeft < 0) {
9643 mUserPaddingLeft = mPaddingLeft;
9644 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009645 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009646 mUserPaddingRight = mUserPaddingEnd;
9647 } else if (mUserPaddingRight < 0) {
9648 mUserPaddingRight = mPaddingRight;
9649 }
9650 }
9651
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009652 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9653
9654 recomputePadding();
9655 }
9656
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009657 /**
9658 * Return true if layout direction resolution can be done
9659 *
9660 * @hide
9661 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009662 protected boolean canResolveLayoutDirection() {
9663 switch (getLayoutDirection()) {
9664 case LAYOUT_DIRECTION_INHERIT:
9665 return (mParent != null);
9666 default:
9667 return true;
9668 }
9669 }
9670
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009671 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009672 * Reset the resolved layout direction.
9673 *
9674 * Subclasses need to override this method to clear cached information that depends on the
9675 * resolved layout direction, or to inform child views that inherit their layout direction.
9676 * Overrides must also call the superclass implementation at the start of their implementation.
9677 *
9678 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009679 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009680 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009681 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009682 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009683 }
9684
9685 /**
9686 * Check if a Locale is corresponding to a RTL script.
9687 *
9688 * @param locale Locale to check
9689 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009690 *
9691 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009692 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009693 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009694 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9695 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 }
9697
9698 /**
9699 * This is called when the view is detached from a window. At this point it
9700 * no longer has a surface for drawing.
9701 *
9702 * @see #onAttachedToWindow()
9703 */
9704 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009705 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009706
Romain Guya440b002010-02-24 15:57:54 -08009707 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009708 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009709 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009710 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009713
Romain Guy6d7475d2011-07-27 16:28:21 -07009714 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009715
Chet Haasedaf98e92011-01-10 14:10:36 -08009716 if (mDisplayList != null) {
9717 mDisplayList.invalidate();
9718 }
9719
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009720 if (mAttachInfo != null) {
9721 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009722 }
9723
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009724 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009725
9726 resetResolvedLayoutDirection();
9727 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009728 }
9729
9730 /**
9731 * @return The number of times this view has been attached to a window
9732 */
9733 protected int getWindowAttachCount() {
9734 return mWindowAttachCount;
9735 }
9736
9737 /**
9738 * Retrieve a unique token identifying the window this view is attached to.
9739 * @return Return the window's token for use in
9740 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9741 */
9742 public IBinder getWindowToken() {
9743 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9744 }
9745
9746 /**
9747 * Retrieve a unique token identifying the top-level "real" window of
9748 * the window that this view is attached to. That is, this is like
9749 * {@link #getWindowToken}, except if the window this view in is a panel
9750 * window (attached to another containing window), then the token of
9751 * the containing window is returned instead.
9752 *
9753 * @return Returns the associated window token, either
9754 * {@link #getWindowToken()} or the containing window's token.
9755 */
9756 public IBinder getApplicationWindowToken() {
9757 AttachInfo ai = mAttachInfo;
9758 if (ai != null) {
9759 IBinder appWindowToken = ai.mPanelParentWindowToken;
9760 if (appWindowToken == null) {
9761 appWindowToken = ai.mWindowToken;
9762 }
9763 return appWindowToken;
9764 }
9765 return null;
9766 }
9767
9768 /**
9769 * Retrieve private session object this view hierarchy is using to
9770 * communicate with the window manager.
9771 * @return the session object to communicate with the window manager
9772 */
9773 /*package*/ IWindowSession getWindowSession() {
9774 return mAttachInfo != null ? mAttachInfo.mSession : null;
9775 }
9776
9777 /**
9778 * @param info the {@link android.view.View.AttachInfo} to associated with
9779 * this view
9780 */
9781 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9782 //System.out.println("Attached! " + this);
9783 mAttachInfo = info;
9784 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009785 // We will need to evaluate the drawable state at least once.
9786 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 if (mFloatingTreeObserver != null) {
9788 info.mTreeObserver.merge(mFloatingTreeObserver);
9789 mFloatingTreeObserver = null;
9790 }
9791 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9792 mAttachInfo.mScrollContainers.add(this);
9793 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9794 }
9795 performCollectViewAttributes(visibility);
9796 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009797
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009798 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009799 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009800 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009801 if (listeners != null && listeners.size() > 0) {
9802 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9803 // perform the dispatching. The iterator is a safe guard against listeners that
9804 // could mutate the list by calling the various add/remove methods. This prevents
9805 // the array from being modified while we iterate it.
9806 for (OnAttachStateChangeListener listener : listeners) {
9807 listener.onViewAttachedToWindow(this);
9808 }
9809 }
9810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 int vis = info.mWindowVisibility;
9812 if (vis != GONE) {
9813 onWindowVisibilityChanged(vis);
9814 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009815 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9816 // If nobody has evaluated the drawable state yet, then do it now.
9817 refreshDrawableState();
9818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 }
9820
9821 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 AttachInfo info = mAttachInfo;
9823 if (info != null) {
9824 int vis = info.mWindowVisibility;
9825 if (vis != GONE) {
9826 onWindowVisibilityChanged(GONE);
9827 }
9828 }
9829
9830 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009831
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009832 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009833 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009834 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009835 if (listeners != null && listeners.size() > 0) {
9836 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9837 // perform the dispatching. The iterator is a safe guard against listeners that
9838 // could mutate the list by calling the various add/remove methods. This prevents
9839 // the array from being modified while we iterate it.
9840 for (OnAttachStateChangeListener listener : listeners) {
9841 listener.onViewDetachedFromWindow(this);
9842 }
9843 }
9844
Romain Guy01d5edc2011-01-28 11:28:53 -08009845 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 mAttachInfo.mScrollContainers.remove(this);
9847 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9848 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 mAttachInfo = null;
9851 }
9852
9853 /**
9854 * Store this view hierarchy's frozen state into the given container.
9855 *
9856 * @param container The SparseArray in which to save the view's state.
9857 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009858 * @see #restoreHierarchyState(android.util.SparseArray)
9859 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9860 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 */
9862 public void saveHierarchyState(SparseArray<Parcelable> container) {
9863 dispatchSaveInstanceState(container);
9864 }
9865
9866 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009867 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9868 * this view and its children. May be overridden to modify how freezing happens to a
9869 * 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 -08009870 *
9871 * @param container The SparseArray in which to save the view's state.
9872 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009873 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9874 * @see #saveHierarchyState(android.util.SparseArray)
9875 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 */
9877 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9878 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9879 mPrivateFlags &= ~SAVE_STATE_CALLED;
9880 Parcelable state = onSaveInstanceState();
9881 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9882 throw new IllegalStateException(
9883 "Derived class did not call super.onSaveInstanceState()");
9884 }
9885 if (state != null) {
9886 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9887 // + ": " + state);
9888 container.put(mID, state);
9889 }
9890 }
9891 }
9892
9893 /**
9894 * Hook allowing a view to generate a representation of its internal state
9895 * that can later be used to create a new instance with that same state.
9896 * This state should only contain information that is not persistent or can
9897 * not be reconstructed later. For example, you will never store your
9898 * current position on screen because that will be computed again when a
9899 * new instance of the view is placed in its view hierarchy.
9900 * <p>
9901 * Some examples of things you may store here: the current cursor position
9902 * in a text view (but usually not the text itself since that is stored in a
9903 * content provider or other persistent storage), the currently selected
9904 * item in a list view.
9905 *
9906 * @return Returns a Parcelable object containing the view's current dynamic
9907 * state, or null if there is nothing interesting to save. The
9908 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009909 * @see #onRestoreInstanceState(android.os.Parcelable)
9910 * @see #saveHierarchyState(android.util.SparseArray)
9911 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 * @see #setSaveEnabled(boolean)
9913 */
9914 protected Parcelable onSaveInstanceState() {
9915 mPrivateFlags |= SAVE_STATE_CALLED;
9916 return BaseSavedState.EMPTY_STATE;
9917 }
9918
9919 /**
9920 * Restore this view hierarchy's frozen state from the given container.
9921 *
9922 * @param container The SparseArray which holds previously frozen states.
9923 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009924 * @see #saveHierarchyState(android.util.SparseArray)
9925 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9926 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 */
9928 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9929 dispatchRestoreInstanceState(container);
9930 }
9931
9932 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009933 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9934 * state for this view and its children. May be overridden to modify how restoring
9935 * happens to a view's children; for example, some views may want to not store state
9936 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009937 *
9938 * @param container The SparseArray which holds previously saved state.
9939 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009940 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9941 * @see #restoreHierarchyState(android.util.SparseArray)
9942 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 */
9944 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9945 if (mID != NO_ID) {
9946 Parcelable state = container.get(mID);
9947 if (state != null) {
9948 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9949 // + ": " + state);
9950 mPrivateFlags &= ~SAVE_STATE_CALLED;
9951 onRestoreInstanceState(state);
9952 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9953 throw new IllegalStateException(
9954 "Derived class did not call super.onRestoreInstanceState()");
9955 }
9956 }
9957 }
9958 }
9959
9960 /**
9961 * Hook allowing a view to re-apply a representation of its internal state that had previously
9962 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9963 * null state.
9964 *
9965 * @param state The frozen state that had previously been returned by
9966 * {@link #onSaveInstanceState}.
9967 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009968 * @see #onSaveInstanceState()
9969 * @see #restoreHierarchyState(android.util.SparseArray)
9970 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 */
9972 protected void onRestoreInstanceState(Parcelable state) {
9973 mPrivateFlags |= SAVE_STATE_CALLED;
9974 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009975 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9976 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009977 + "when two views of different type have the same id in the same hierarchy. "
9978 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009979 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 }
9981 }
9982
9983 /**
9984 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9985 *
9986 * @return the drawing start time in milliseconds
9987 */
9988 public long getDrawingTime() {
9989 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9990 }
9991
9992 /**
9993 * <p>Enables or disables the duplication of the parent's state into this view. When
9994 * duplication is enabled, this view gets its drawable state from its parent rather
9995 * than from its own internal properties.</p>
9996 *
9997 * <p>Note: in the current implementation, setting this property to true after the
9998 * view was added to a ViewGroup might have no effect at all. This property should
9999 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10000 *
10001 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10002 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010003 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010004 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10005 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006 *
10007 * @param enabled True to enable duplication of the parent's drawable state, false
10008 * to disable it.
10009 *
10010 * @see #getDrawableState()
10011 * @see #isDuplicateParentStateEnabled()
10012 */
10013 public void setDuplicateParentStateEnabled(boolean enabled) {
10014 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10015 }
10016
10017 /**
10018 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10019 *
10020 * @return True if this view's drawable state is duplicated from the parent,
10021 * false otherwise
10022 *
10023 * @see #getDrawableState()
10024 * @see #setDuplicateParentStateEnabled(boolean)
10025 */
10026 public boolean isDuplicateParentStateEnabled() {
10027 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10028 }
10029
10030 /**
Romain Guy171c5922011-01-06 10:04:23 -080010031 * <p>Specifies the type of layer backing this view. The layer can be
10032 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10033 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010034 *
Romain Guy171c5922011-01-06 10:04:23 -080010035 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10036 * instance that controls how the layer is composed on screen. The following
10037 * properties of the paint are taken into account when composing the layer:</p>
10038 * <ul>
10039 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10040 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10041 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10042 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010043 *
Romain Guy171c5922011-01-06 10:04:23 -080010044 * <p>If this view has an alpha value set to < 1.0 by calling
10045 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10046 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10047 * equivalent to setting a hardware layer on this view and providing a paint with
10048 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010049 *
Romain Guy171c5922011-01-06 10:04:23 -080010050 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10051 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10052 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010053 *
Romain Guy171c5922011-01-06 10:04:23 -080010054 * @param layerType The ype of layer to use with this view, must be one of
10055 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10056 * {@link #LAYER_TYPE_HARDWARE}
10057 * @param paint The paint used to compose the layer. This argument is optional
10058 * and can be null. It is ignored when the layer type is
10059 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010060 *
10061 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010062 * @see #LAYER_TYPE_NONE
10063 * @see #LAYER_TYPE_SOFTWARE
10064 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010065 * @see #setAlpha(float)
10066 *
Romain Guy171c5922011-01-06 10:04:23 -080010067 * @attr ref android.R.styleable#View_layerType
10068 */
10069 public void setLayerType(int layerType, Paint paint) {
10070 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010071 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010072 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10073 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010074
Romain Guyd6cd5722011-01-17 14:42:41 -080010075 if (layerType == mLayerType) {
10076 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10077 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010078 invalidateParentCaches();
10079 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010080 }
10081 return;
10082 }
Romain Guy171c5922011-01-06 10:04:23 -080010083
10084 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010085 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010086 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010087 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010088 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010089 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010090 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010091 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010092 default:
10093 break;
Romain Guy171c5922011-01-06 10:04:23 -080010094 }
10095
10096 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010097 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10098 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10099 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010100
Romain Guy0fd89bf2011-01-26 15:41:30 -080010101 invalidateParentCaches();
10102 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010103 }
10104
10105 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010106 * Indicates whether this view has a static layer. A view with layer type
10107 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10108 * dynamic.
10109 */
10110 boolean hasStaticLayer() {
10111 return mLayerType == LAYER_TYPE_NONE;
10112 }
10113
10114 /**
Romain Guy171c5922011-01-06 10:04:23 -080010115 * Indicates what type of layer is currently associated with this view. By default
10116 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10117 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10118 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010119 *
Romain Guy171c5922011-01-06 10:04:23 -080010120 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10121 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010122 *
10123 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010124 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010125 * @see #LAYER_TYPE_NONE
10126 * @see #LAYER_TYPE_SOFTWARE
10127 * @see #LAYER_TYPE_HARDWARE
10128 */
10129 public int getLayerType() {
10130 return mLayerType;
10131 }
Joe Malin32736f02011-01-19 16:14:20 -080010132
Romain Guy6c319ca2011-01-11 14:29:25 -080010133 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010134 * Forces this view's layer to be created and this view to be rendered
10135 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10136 * invoking this method will have no effect.
10137 *
10138 * This method can for instance be used to render a view into its layer before
10139 * starting an animation. If this view is complex, rendering into the layer
10140 * before starting the animation will avoid skipping frames.
10141 *
10142 * @throws IllegalStateException If this view is not attached to a window
10143 *
10144 * @see #setLayerType(int, android.graphics.Paint)
10145 */
10146 public void buildLayer() {
10147 if (mLayerType == LAYER_TYPE_NONE) return;
10148
10149 if (mAttachInfo == null) {
10150 throw new IllegalStateException("This view must be attached to a window first");
10151 }
10152
10153 switch (mLayerType) {
10154 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010155 if (mAttachInfo.mHardwareRenderer != null &&
10156 mAttachInfo.mHardwareRenderer.isEnabled() &&
10157 mAttachInfo.mHardwareRenderer.validate()) {
10158 getHardwareLayer();
10159 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010160 break;
10161 case LAYER_TYPE_SOFTWARE:
10162 buildDrawingCache(true);
10163 break;
10164 }
10165 }
10166
10167 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010168 * <p>Returns a hardware layer that can be used to draw this view again
10169 * without executing its draw method.</p>
10170 *
10171 * @return A HardwareLayer ready to render, or null if an error occurred.
10172 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010173 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010174 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10175 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010176 return null;
10177 }
10178
10179 final int width = mRight - mLeft;
10180 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010181
Romain Guy6c319ca2011-01-11 14:29:25 -080010182 if (width == 0 || height == 0) {
10183 return null;
10184 }
10185
10186 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10187 if (mHardwareLayer == null) {
10188 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10189 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010190 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010191 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10192 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010193 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010194 }
10195
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010196 // The layer is not valid if the underlying GPU resources cannot be allocated
10197 if (!mHardwareLayer.isValid()) {
10198 return null;
10199 }
10200
Romain Guy59a12ca2011-06-09 17:48:21 -070010201 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010202 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010203
10204 // Make sure all the GPU resources have been properly allocated
10205 if (canvas == null) {
10206 mHardwareLayer.end(currentCanvas);
10207 return null;
10208 }
10209
Romain Guy5e7f7662011-01-24 22:35:56 -080010210 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010211 try {
10212 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010213 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010214 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010215
Chet Haase88172fe2011-03-07 17:36:33 -080010216 final int restoreCount = canvas.save();
10217
Romain Guy6c319ca2011-01-11 14:29:25 -080010218 computeScroll();
10219 canvas.translate(-mScrollX, -mScrollY);
10220
Romain Guy6c319ca2011-01-11 14:29:25 -080010221 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010222
Romain Guy6c319ca2011-01-11 14:29:25 -080010223 // Fast path for layouts with no backgrounds
10224 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10225 mPrivateFlags &= ~DIRTY_MASK;
10226 dispatchDraw(canvas);
10227 } else {
10228 draw(canvas);
10229 }
Joe Malin32736f02011-01-19 16:14:20 -080010230
Chet Haase88172fe2011-03-07 17:36:33 -080010231 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010232 } finally {
10233 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010234 mHardwareLayer.end(currentCanvas);
10235 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010236 }
10237 }
10238
10239 return mHardwareLayer;
10240 }
Romain Guy171c5922011-01-06 10:04:23 -080010241
Romain Guy589b0bb2011-10-10 13:57:47 -070010242 /**
10243 * Destroys this View's hardware layer if possible.
10244 *
10245 * @return True if the layer was destroyed, false otherwise.
10246 *
10247 * @see #setLayerType(int, android.graphics.Paint)
10248 * @see #LAYER_TYPE_HARDWARE
10249 */
Romain Guy65b345f2011-07-27 18:51:50 -070010250 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010251 if (mHardwareLayer != null) {
Dave Burke70775062011-11-14 11:39:30 -080010252 mHardwareLayer.destroy();
10253 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010254
10255 invalidate(true);
10256 invalidateParentCaches();
10257
Romain Guy65b345f2011-07-27 18:51:50 -070010258 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010259 }
Romain Guy65b345f2011-07-27 18:51:50 -070010260 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010261 }
10262
Romain Guy171c5922011-01-06 10:04:23 -080010263 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010264 * Destroys all hardware rendering resources. This method is invoked
10265 * when the system needs to reclaim resources. Upon execution of this
10266 * method, you should free any OpenGL resources created by the view.
10267 *
10268 * Note: you <strong>must</strong> call
10269 * <code>super.destroyHardwareResources()</code> when overriding
10270 * this method.
10271 *
10272 * @hide
10273 */
10274 protected void destroyHardwareResources() {
10275 destroyLayer();
10276 }
10277
10278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10280 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10281 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10282 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10283 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10284 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010285 *
Romain Guy171c5922011-01-06 10:04:23 -080010286 * <p>Enabling the drawing cache is similar to
10287 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010288 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10289 * drawing cache has no effect on rendering because the system uses a different mechanism
10290 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10291 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10292 * for information on how to enable software and hardware layers.</p>
10293 *
10294 * <p>This API can be used to manually generate
10295 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10296 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 *
10298 * @param enabled true to enable the drawing cache, false otherwise
10299 *
10300 * @see #isDrawingCacheEnabled()
10301 * @see #getDrawingCache()
10302 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010303 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 */
10305 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010306 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10308 }
10309
10310 /**
10311 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10312 *
10313 * @return true if the drawing cache is enabled
10314 *
10315 * @see #setDrawingCacheEnabled(boolean)
10316 * @see #getDrawingCache()
10317 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010318 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 public boolean isDrawingCacheEnabled() {
10320 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10321 }
10322
10323 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010324 * Debugging utility which recursively outputs the dirty state of a view and its
10325 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010326 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010327 * @hide
10328 */
Romain Guy676b1732011-02-14 14:45:33 -080010329 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010330 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10331 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10332 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10333 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10334 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10335 if (clear) {
10336 mPrivateFlags &= clearMask;
10337 }
10338 if (this instanceof ViewGroup) {
10339 ViewGroup parent = (ViewGroup) this;
10340 final int count = parent.getChildCount();
10341 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010342 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010343 child.outputDirtyFlags(indent + " ", clear, clearMask);
10344 }
10345 }
10346 }
10347
10348 /**
10349 * This method is used by ViewGroup to cause its children to restore or recreate their
10350 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10351 * to recreate its own display list, which would happen if it went through the normal
10352 * draw/dispatchDraw mechanisms.
10353 *
10354 * @hide
10355 */
10356 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010357
10358 /**
10359 * A view that is not attached or hardware accelerated cannot create a display list.
10360 * This method checks these conditions and returns the appropriate result.
10361 *
10362 * @return true if view has the ability to create a display list, false otherwise.
10363 *
10364 * @hide
10365 */
10366 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010367 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010368 }
Joe Malin32736f02011-01-19 16:14:20 -080010369
Chet Haasedaf98e92011-01-10 14:10:36 -080010370 /**
Romain Guyb051e892010-09-28 19:09:36 -070010371 * <p>Returns a display list that can be used to draw this view again
10372 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010373 *
Romain Guyb051e892010-09-28 19:09:36 -070010374 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010375 *
10376 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010377 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010378 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010379 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010380 return null;
10381 }
10382
Chet Haasedaf98e92011-01-10 14:10:36 -080010383 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10384 mDisplayList == null || !mDisplayList.isValid() ||
10385 mRecreateDisplayList)) {
10386 // Don't need to recreate the display list, just need to tell our
10387 // children to restore/recreate theirs
10388 if (mDisplayList != null && mDisplayList.isValid() &&
10389 !mRecreateDisplayList) {
10390 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10391 mPrivateFlags &= ~DIRTY_MASK;
10392 dispatchGetDisplayList();
10393
10394 return mDisplayList;
10395 }
10396
10397 // If we got here, we're recreating it. Mark it as such to ensure that
10398 // we copy in child display lists into ours in drawChild()
10399 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010400 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010401 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010402 // If we're creating a new display list, make sure our parent gets invalidated
10403 // since they will need to recreate their display list to account for this
10404 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010405 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010406 }
Romain Guyb051e892010-09-28 19:09:36 -070010407
10408 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010409 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010410 try {
10411 int width = mRight - mLeft;
10412 int height = mBottom - mTop;
10413
10414 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010415 // The dirty rect should always be null for a display list
10416 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010417
Chet Haasedaf98e92011-01-10 14:10:36 -080010418 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010419
10420 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010421 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010422 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010423 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010424
Romain Guyb051e892010-09-28 19:09:36 -070010425 // Fast path for layouts with no backgrounds
10426 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010427 dispatchDraw(canvas);
10428 } else {
10429 draw(canvas);
10430 }
Romain Guyb051e892010-09-28 19:09:36 -070010431 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010432 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010433 canvas.onPostDraw();
10434
10435 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010436 }
Chet Haase77785f92011-01-25 23:22:09 -080010437 } else {
10438 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10439 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010440 }
10441
10442 return mDisplayList;
10443 }
10444
10445 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010446 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010447 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010448 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010449 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010450 * @see #getDrawingCache(boolean)
10451 */
10452 public Bitmap getDrawingCache() {
10453 return getDrawingCache(false);
10454 }
10455
10456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10458 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10459 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10460 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10461 * request the drawing cache by calling this method and draw it on screen if the
10462 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010463 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010464 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10465 * this method will create a bitmap of the same size as this view. Because this bitmap
10466 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10467 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10468 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10469 * size than the view. This implies that your application must be able to handle this
10470 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010471 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010472 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10473 * the current density of the screen when the application is in compatibility
10474 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010476 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010477 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010478 * @see #setDrawingCacheEnabled(boolean)
10479 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010480 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 * @see #destroyDrawingCache()
10482 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010483 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010484 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10485 return null;
10486 }
10487 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010488 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 }
Romain Guy02890fd2010-08-06 17:58:44 -070010490 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 }
10492
10493 /**
10494 * <p>Frees the resources used by the drawing cache. If you call
10495 * {@link #buildDrawingCache()} manually without calling
10496 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10497 * should cleanup the cache with this method afterwards.</p>
10498 *
10499 * @see #setDrawingCacheEnabled(boolean)
10500 * @see #buildDrawingCache()
10501 * @see #getDrawingCache()
10502 */
10503 public void destroyDrawingCache() {
10504 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010505 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 mDrawingCache = null;
10507 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010508 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010509 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010510 mUnscaledDrawingCache = null;
10511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 }
10513
10514 /**
10515 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010516 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 * view will always be drawn on top of a solid color.
10518 *
10519 * @param color The background color to use for the drawing cache's bitmap
10520 *
10521 * @see #setDrawingCacheEnabled(boolean)
10522 * @see #buildDrawingCache()
10523 * @see #getDrawingCache()
10524 */
10525 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010526 if (color != mDrawingCacheBackgroundColor) {
10527 mDrawingCacheBackgroundColor = color;
10528 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 }
10531
10532 /**
10533 * @see #setDrawingCacheBackgroundColor(int)
10534 *
10535 * @return The background color to used for the drawing cache's bitmap
10536 */
10537 public int getDrawingCacheBackgroundColor() {
10538 return mDrawingCacheBackgroundColor;
10539 }
10540
10541 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010542 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010543 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010544 * @see #buildDrawingCache(boolean)
10545 */
10546 public void buildDrawingCache() {
10547 buildDrawingCache(false);
10548 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010549
Romain Guyfbd8f692009-06-26 14:51:58 -070010550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10552 *
10553 * <p>If you call {@link #buildDrawingCache()} manually without calling
10554 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10555 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010556 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010557 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10558 * this method will create a bitmap of the same size as this view. Because this bitmap
10559 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10560 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10561 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10562 * size than the view. This implies that your application must be able to handle this
10563 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010564 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010565 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10566 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010567 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010568 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 *
10570 * @see #getDrawingCache()
10571 * @see #destroyDrawingCache()
10572 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010573 public void buildDrawingCache(boolean autoScale) {
10574 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010575 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010576 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577
10578 if (ViewDebug.TRACE_HIERARCHY) {
10579 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581
Romain Guy8506ab42009-06-11 17:35:47 -070010582 int width = mRight - mLeft;
10583 int height = mBottom - mTop;
10584
10585 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010586 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010587
Romain Guye1123222009-06-29 14:24:56 -070010588 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010589 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10590 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592
10593 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010594 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010595 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596
10597 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010598 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010599 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10601 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010602 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 return;
10604 }
10605
10606 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010607 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608
10609 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 Bitmap.Config quality;
10611 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010612 // Never pick ARGB_4444 because it looks awful
10613 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10615 case DRAWING_CACHE_QUALITY_AUTO:
10616 quality = Bitmap.Config.ARGB_8888;
10617 break;
10618 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010619 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 break;
10621 case DRAWING_CACHE_QUALITY_HIGH:
10622 quality = Bitmap.Config.ARGB_8888;
10623 break;
10624 default:
10625 quality = Bitmap.Config.ARGB_8888;
10626 break;
10627 }
10628 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010629 // Optimization for translucent windows
10630 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010631 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 }
10633
10634 // Try to cleanup memory
10635 if (bitmap != null) bitmap.recycle();
10636
10637 try {
10638 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010639 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010640 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010641 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010642 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010643 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010644 }
Adam Powell26153a32010-11-08 15:22:27 -080010645 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 } catch (OutOfMemoryError e) {
10647 // If there is not enough memory to create the bitmap cache, just
10648 // ignore the issue as bitmap caches are not required to draw the
10649 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010650 if (autoScale) {
10651 mDrawingCache = null;
10652 } else {
10653 mUnscaledDrawingCache = null;
10654 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010655 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 return;
10657 }
10658
10659 clear = drawingCacheBackgroundColor != 0;
10660 }
10661
10662 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 if (attachInfo != null) {
10664 canvas = attachInfo.mCanvas;
10665 if (canvas == null) {
10666 canvas = new Canvas();
10667 }
10668 canvas.setBitmap(bitmap);
10669 // Temporarily clobber the cached Canvas in case one of our children
10670 // is also using a drawing cache. Without this, the children would
10671 // steal the canvas by attaching their own bitmap to it and bad, bad
10672 // thing would happen (invisible views, corrupted drawings, etc.)
10673 attachInfo.mCanvas = null;
10674 } else {
10675 // This case should hopefully never or seldom happen
10676 canvas = new Canvas(bitmap);
10677 }
10678
10679 if (clear) {
10680 bitmap.eraseColor(drawingCacheBackgroundColor);
10681 }
10682
10683 computeScroll();
10684 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010685
Romain Guye1123222009-06-29 14:24:56 -070010686 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010687 final float scale = attachInfo.mApplicationScale;
10688 canvas.scale(scale, scale);
10689 }
Joe Malin32736f02011-01-19 16:14:20 -080010690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 canvas.translate(-mScrollX, -mScrollY);
10692
Romain Guy5bcdff42009-05-14 21:27:18 -070010693 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010694 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10695 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010696 mPrivateFlags |= DRAWING_CACHE_VALID;
10697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698
10699 // Fast path for layouts with no backgrounds
10700 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10701 if (ViewDebug.TRACE_HIERARCHY) {
10702 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10703 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010704 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 dispatchDraw(canvas);
10706 } else {
10707 draw(canvas);
10708 }
10709
10710 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010711 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712
10713 if (attachInfo != null) {
10714 // Restore the cached Canvas for our siblings
10715 attachInfo.mCanvas = canvas;
10716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 }
10718 }
10719
10720 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010721 * Create a snapshot of the view into a bitmap. We should probably make
10722 * some form of this public, but should think about the API.
10723 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010724 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010725 int width = mRight - mLeft;
10726 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010727
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010728 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010729 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010730 width = (int) ((width * scale) + 0.5f);
10731 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010732
Romain Guy8c11e312009-09-14 15:15:30 -070010733 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010734 if (bitmap == null) {
10735 throw new OutOfMemoryError();
10736 }
10737
Romain Guyc529d8d2011-09-06 15:01:39 -070010738 Resources resources = getResources();
10739 if (resources != null) {
10740 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10741 }
Joe Malin32736f02011-01-19 16:14:20 -080010742
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010743 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010744 if (attachInfo != null) {
10745 canvas = attachInfo.mCanvas;
10746 if (canvas == null) {
10747 canvas = new Canvas();
10748 }
10749 canvas.setBitmap(bitmap);
10750 // Temporarily clobber the cached Canvas in case one of our children
10751 // is also using a drawing cache. Without this, the children would
10752 // steal the canvas by attaching their own bitmap to it and bad, bad
10753 // things would happen (invisible views, corrupted drawings, etc.)
10754 attachInfo.mCanvas = null;
10755 } else {
10756 // This case should hopefully never or seldom happen
10757 canvas = new Canvas(bitmap);
10758 }
10759
Romain Guy5bcdff42009-05-14 21:27:18 -070010760 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010761 bitmap.eraseColor(backgroundColor);
10762 }
10763
10764 computeScroll();
10765 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010766 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010767 canvas.translate(-mScrollX, -mScrollY);
10768
Romain Guy5bcdff42009-05-14 21:27:18 -070010769 // Temporarily remove the dirty mask
10770 int flags = mPrivateFlags;
10771 mPrivateFlags &= ~DIRTY_MASK;
10772
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010773 // Fast path for layouts with no backgrounds
10774 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10775 dispatchDraw(canvas);
10776 } else {
10777 draw(canvas);
10778 }
10779
Romain Guy5bcdff42009-05-14 21:27:18 -070010780 mPrivateFlags = flags;
10781
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010782 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010783 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010784
10785 if (attachInfo != null) {
10786 // Restore the cached Canvas for our siblings
10787 attachInfo.mCanvas = canvas;
10788 }
Romain Guy8506ab42009-06-11 17:35:47 -070010789
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010790 return bitmap;
10791 }
10792
10793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 * Indicates whether this View is currently in edit mode. A View is usually
10795 * in edit mode when displayed within a developer tool. For instance, if
10796 * this View is being drawn by a visual user interface builder, this method
10797 * should return true.
10798 *
10799 * Subclasses should check the return value of this method to provide
10800 * different behaviors if their normal behavior might interfere with the
10801 * host environment. For instance: the class spawns a thread in its
10802 * constructor, the drawing code relies on device-specific features, etc.
10803 *
10804 * This method is usually checked in the drawing code of custom widgets.
10805 *
10806 * @return True if this View is in edit mode, false otherwise.
10807 */
10808 public boolean isInEditMode() {
10809 return false;
10810 }
10811
10812 /**
10813 * If the View draws content inside its padding and enables fading edges,
10814 * it needs to support padding offsets. Padding offsets are added to the
10815 * fading edges to extend the length of the fade so that it covers pixels
10816 * drawn inside the padding.
10817 *
10818 * Subclasses of this class should override this method if they need
10819 * to draw content inside the padding.
10820 *
10821 * @return True if padding offset must be applied, false otherwise.
10822 *
10823 * @see #getLeftPaddingOffset()
10824 * @see #getRightPaddingOffset()
10825 * @see #getTopPaddingOffset()
10826 * @see #getBottomPaddingOffset()
10827 *
10828 * @since CURRENT
10829 */
10830 protected boolean isPaddingOffsetRequired() {
10831 return false;
10832 }
10833
10834 /**
10835 * Amount by which to extend the left fading region. Called only when
10836 * {@link #isPaddingOffsetRequired()} returns true.
10837 *
10838 * @return The left padding offset in pixels.
10839 *
10840 * @see #isPaddingOffsetRequired()
10841 *
10842 * @since CURRENT
10843 */
10844 protected int getLeftPaddingOffset() {
10845 return 0;
10846 }
10847
10848 /**
10849 * Amount by which to extend the right fading region. Called only when
10850 * {@link #isPaddingOffsetRequired()} returns true.
10851 *
10852 * @return The right padding offset in pixels.
10853 *
10854 * @see #isPaddingOffsetRequired()
10855 *
10856 * @since CURRENT
10857 */
10858 protected int getRightPaddingOffset() {
10859 return 0;
10860 }
10861
10862 /**
10863 * Amount by which to extend the top fading region. Called only when
10864 * {@link #isPaddingOffsetRequired()} returns true.
10865 *
10866 * @return The top padding offset in pixels.
10867 *
10868 * @see #isPaddingOffsetRequired()
10869 *
10870 * @since CURRENT
10871 */
10872 protected int getTopPaddingOffset() {
10873 return 0;
10874 }
10875
10876 /**
10877 * Amount by which to extend the bottom fading region. Called only when
10878 * {@link #isPaddingOffsetRequired()} returns true.
10879 *
10880 * @return The bottom padding offset in pixels.
10881 *
10882 * @see #isPaddingOffsetRequired()
10883 *
10884 * @since CURRENT
10885 */
10886 protected int getBottomPaddingOffset() {
10887 return 0;
10888 }
10889
10890 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010891 * @hide
10892 * @param offsetRequired
10893 */
10894 protected int getFadeTop(boolean offsetRequired) {
10895 int top = mPaddingTop;
10896 if (offsetRequired) top += getTopPaddingOffset();
10897 return top;
10898 }
10899
10900 /**
10901 * @hide
10902 * @param offsetRequired
10903 */
10904 protected int getFadeHeight(boolean offsetRequired) {
10905 int padding = mPaddingTop;
10906 if (offsetRequired) padding += getTopPaddingOffset();
10907 return mBottom - mTop - mPaddingBottom - padding;
10908 }
10909
10910 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010911 * <p>Indicates whether this view is attached to an hardware accelerated
10912 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010913 *
Romain Guy2bffd262010-09-12 17:40:02 -070010914 * <p>Even if this method returns true, it does not mean that every call
10915 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10916 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10917 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10918 * window is hardware accelerated,
10919 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10920 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010921 *
Romain Guy2bffd262010-09-12 17:40:02 -070010922 * @return True if the view is attached to a window and the window is
10923 * hardware accelerated; false in any other case.
10924 */
10925 public boolean isHardwareAccelerated() {
10926 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10927 }
Joe Malin32736f02011-01-19 16:14:20 -080010928
Romain Guy2bffd262010-09-12 17:40:02 -070010929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010930 * Manually render this view (and all of its children) to the given Canvas.
10931 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010932 * called. When implementing a view, implement
10933 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10934 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010935 *
10936 * @param canvas The Canvas to which the View is rendered.
10937 */
10938 public void draw(Canvas canvas) {
10939 if (ViewDebug.TRACE_HIERARCHY) {
10940 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10941 }
10942
Romain Guy5bcdff42009-05-14 21:27:18 -070010943 final int privateFlags = mPrivateFlags;
10944 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10945 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10946 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 /*
10949 * Draw traversal performs several drawing steps which must be executed
10950 * in the appropriate order:
10951 *
10952 * 1. Draw the background
10953 * 2. If necessary, save the canvas' layers to prepare for fading
10954 * 3. Draw view's content
10955 * 4. Draw children
10956 * 5. If necessary, draw the fading edges and restore layers
10957 * 6. Draw decorations (scrollbars for instance)
10958 */
10959
10960 // Step 1, draw the background, if needed
10961 int saveCount;
10962
Romain Guy24443ea2009-05-11 11:56:30 -070010963 if (!dirtyOpaque) {
10964 final Drawable background = mBGDrawable;
10965 if (background != null) {
10966 final int scrollX = mScrollX;
10967 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968
Romain Guy24443ea2009-05-11 11:56:30 -070010969 if (mBackgroundSizeChanged) {
10970 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10971 mBackgroundSizeChanged = false;
10972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010973
Romain Guy24443ea2009-05-11 11:56:30 -070010974 if ((scrollX | scrollY) == 0) {
10975 background.draw(canvas);
10976 } else {
10977 canvas.translate(scrollX, scrollY);
10978 background.draw(canvas);
10979 canvas.translate(-scrollX, -scrollY);
10980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 }
10982 }
10983
10984 // skip step 2 & 5 if possible (common case)
10985 final int viewFlags = mViewFlags;
10986 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10987 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10988 if (!verticalEdges && !horizontalEdges) {
10989 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010990 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991
10992 // Step 4, draw the children
10993 dispatchDraw(canvas);
10994
10995 // Step 6, draw decorations (scrollbars)
10996 onDrawScrollBars(canvas);
10997
10998 // we're done...
10999 return;
11000 }
11001
11002 /*
11003 * Here we do the full fledged routine...
11004 * (this is an uncommon case where speed matters less,
11005 * this is why we repeat some of the tests that have been
11006 * done above)
11007 */
11008
11009 boolean drawTop = false;
11010 boolean drawBottom = false;
11011 boolean drawLeft = false;
11012 boolean drawRight = false;
11013
11014 float topFadeStrength = 0.0f;
11015 float bottomFadeStrength = 0.0f;
11016 float leftFadeStrength = 0.0f;
11017 float rightFadeStrength = 0.0f;
11018
11019 // Step 2, save the canvas' layers
11020 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011021
11022 final boolean offsetRequired = isPaddingOffsetRequired();
11023 if (offsetRequired) {
11024 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011025 }
11026
11027 int left = mScrollX + paddingLeft;
11028 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011029 int top = mScrollY + getFadeTop(offsetRequired);
11030 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011031
11032 if (offsetRequired) {
11033 right += getRightPaddingOffset();
11034 bottom += getBottomPaddingOffset();
11035 }
11036
11037 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011038 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11039 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040
11041 // clip the fade length if top and bottom fades overlap
11042 // overlapping fades produce odd-looking artifacts
11043 if (verticalEdges && (top + length > bottom - length)) {
11044 length = (bottom - top) / 2;
11045 }
11046
11047 // also clip horizontal fades if necessary
11048 if (horizontalEdges && (left + length > right - length)) {
11049 length = (right - left) / 2;
11050 }
11051
11052 if (verticalEdges) {
11053 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011054 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011056 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 }
11058
11059 if (horizontalEdges) {
11060 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011061 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011063 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 }
11065
11066 saveCount = canvas.getSaveCount();
11067
11068 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011069 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11071
11072 if (drawTop) {
11073 canvas.saveLayer(left, top, right, top + length, null, flags);
11074 }
11075
11076 if (drawBottom) {
11077 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11078 }
11079
11080 if (drawLeft) {
11081 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11082 }
11083
11084 if (drawRight) {
11085 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11086 }
11087 } else {
11088 scrollabilityCache.setFadeColor(solidColor);
11089 }
11090
11091 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011092 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093
11094 // Step 4, draw the children
11095 dispatchDraw(canvas);
11096
11097 // Step 5, draw the fade effect and restore layers
11098 final Paint p = scrollabilityCache.paint;
11099 final Matrix matrix = scrollabilityCache.matrix;
11100 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011101
11102 if (drawTop) {
11103 matrix.setScale(1, fadeHeight * topFadeStrength);
11104 matrix.postTranslate(left, top);
11105 fade.setLocalMatrix(matrix);
11106 canvas.drawRect(left, top, right, top + length, p);
11107 }
11108
11109 if (drawBottom) {
11110 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11111 matrix.postRotate(180);
11112 matrix.postTranslate(left, bottom);
11113 fade.setLocalMatrix(matrix);
11114 canvas.drawRect(left, bottom - length, right, bottom, p);
11115 }
11116
11117 if (drawLeft) {
11118 matrix.setScale(1, fadeHeight * leftFadeStrength);
11119 matrix.postRotate(-90);
11120 matrix.postTranslate(left, top);
11121 fade.setLocalMatrix(matrix);
11122 canvas.drawRect(left, top, left + length, bottom, p);
11123 }
11124
11125 if (drawRight) {
11126 matrix.setScale(1, fadeHeight * rightFadeStrength);
11127 matrix.postRotate(90);
11128 matrix.postTranslate(right, top);
11129 fade.setLocalMatrix(matrix);
11130 canvas.drawRect(right - length, top, right, bottom, p);
11131 }
11132
11133 canvas.restoreToCount(saveCount);
11134
11135 // Step 6, draw decorations (scrollbars)
11136 onDrawScrollBars(canvas);
11137 }
11138
11139 /**
11140 * Override this if your view is known to always be drawn on top of a solid color background,
11141 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11142 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11143 * should be set to 0xFF.
11144 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011145 * @see #setVerticalFadingEdgeEnabled(boolean)
11146 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011147 *
11148 * @return The known solid color background for this view, or 0 if the color may vary
11149 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011150 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 public int getSolidColor() {
11152 return 0;
11153 }
11154
11155 /**
11156 * Build a human readable string representation of the specified view flags.
11157 *
11158 * @param flags the view flags to convert to a string
11159 * @return a String representing the supplied flags
11160 */
11161 private static String printFlags(int flags) {
11162 String output = "";
11163 int numFlags = 0;
11164 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11165 output += "TAKES_FOCUS";
11166 numFlags++;
11167 }
11168
11169 switch (flags & VISIBILITY_MASK) {
11170 case INVISIBLE:
11171 if (numFlags > 0) {
11172 output += " ";
11173 }
11174 output += "INVISIBLE";
11175 // USELESS HERE numFlags++;
11176 break;
11177 case GONE:
11178 if (numFlags > 0) {
11179 output += " ";
11180 }
11181 output += "GONE";
11182 // USELESS HERE numFlags++;
11183 break;
11184 default:
11185 break;
11186 }
11187 return output;
11188 }
11189
11190 /**
11191 * Build a human readable string representation of the specified private
11192 * view flags.
11193 *
11194 * @param privateFlags the private view flags to convert to a string
11195 * @return a String representing the supplied flags
11196 */
11197 private static String printPrivateFlags(int privateFlags) {
11198 String output = "";
11199 int numFlags = 0;
11200
11201 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11202 output += "WANTS_FOCUS";
11203 numFlags++;
11204 }
11205
11206 if ((privateFlags & FOCUSED) == FOCUSED) {
11207 if (numFlags > 0) {
11208 output += " ";
11209 }
11210 output += "FOCUSED";
11211 numFlags++;
11212 }
11213
11214 if ((privateFlags & SELECTED) == SELECTED) {
11215 if (numFlags > 0) {
11216 output += " ";
11217 }
11218 output += "SELECTED";
11219 numFlags++;
11220 }
11221
11222 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11223 if (numFlags > 0) {
11224 output += " ";
11225 }
11226 output += "IS_ROOT_NAMESPACE";
11227 numFlags++;
11228 }
11229
11230 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11231 if (numFlags > 0) {
11232 output += " ";
11233 }
11234 output += "HAS_BOUNDS";
11235 numFlags++;
11236 }
11237
11238 if ((privateFlags & DRAWN) == DRAWN) {
11239 if (numFlags > 0) {
11240 output += " ";
11241 }
11242 output += "DRAWN";
11243 // USELESS HERE numFlags++;
11244 }
11245 return output;
11246 }
11247
11248 /**
11249 * <p>Indicates whether or not this view's layout will be requested during
11250 * the next hierarchy layout pass.</p>
11251 *
11252 * @return true if the layout will be forced during next layout pass
11253 */
11254 public boolean isLayoutRequested() {
11255 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11256 }
11257
11258 /**
11259 * Assign a size and position to a view and all of its
11260 * descendants
11261 *
11262 * <p>This is the second phase of the layout mechanism.
11263 * (The first is measuring). In this phase, each parent calls
11264 * layout on all of its children to position them.
11265 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011266 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 *
Chet Haase9c087442011-01-12 16:20:16 -080011268 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 * Derived classes with children should override
11270 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011271 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011272 *
11273 * @param l Left position, relative to parent
11274 * @param t Top position, relative to parent
11275 * @param r Right position, relative to parent
11276 * @param b Bottom position, relative to parent
11277 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011278 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011279 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011280 int oldL = mLeft;
11281 int oldT = mTop;
11282 int oldB = mBottom;
11283 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284 boolean changed = setFrame(l, t, r, b);
11285 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11286 if (ViewDebug.TRACE_HIERARCHY) {
11287 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11288 }
11289
11290 onLayout(changed, l, t, r, b);
11291 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011292
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011293 ListenerInfo li = mListenerInfo;
11294 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011295 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011296 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011297 int numListeners = listenersCopy.size();
11298 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011299 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011300 }
11301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011302 }
11303 mPrivateFlags &= ~FORCE_LAYOUT;
11304 }
11305
11306 /**
11307 * Called from layout when this view should
11308 * assign a size and position to each of its children.
11309 *
11310 * Derived classes with children should override
11311 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011312 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 * @param changed This is a new size or position for this view
11314 * @param left Left position, relative to parent
11315 * @param top Top position, relative to parent
11316 * @param right Right position, relative to parent
11317 * @param bottom Bottom position, relative to parent
11318 */
11319 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11320 }
11321
11322 /**
11323 * Assign a size and position to this view.
11324 *
11325 * This is called from layout.
11326 *
11327 * @param left Left position, relative to parent
11328 * @param top Top position, relative to parent
11329 * @param right Right position, relative to parent
11330 * @param bottom Bottom position, relative to parent
11331 * @return true if the new size and position are different than the
11332 * previous ones
11333 * {@hide}
11334 */
11335 protected boolean setFrame(int left, int top, int right, int bottom) {
11336 boolean changed = false;
11337
11338 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011339 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 + right + "," + bottom + ")");
11341 }
11342
11343 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11344 changed = true;
11345
11346 // Remember our drawn bit
11347 int drawn = mPrivateFlags & DRAWN;
11348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349 int oldWidth = mRight - mLeft;
11350 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011351 int newWidth = right - left;
11352 int newHeight = bottom - top;
11353 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11354
11355 // Invalidate our old position
11356 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011357
11358 mLeft = left;
11359 mTop = top;
11360 mRight = right;
11361 mBottom = bottom;
11362
11363 mPrivateFlags |= HAS_BOUNDS;
11364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365
Chet Haase75755e22011-07-18 17:48:25 -070011366 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011367 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11368 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011369 if (mTransformationInfo != null) {
11370 mTransformationInfo.mMatrixDirty = true;
11371 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011373 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11374 }
11375
11376 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11377 // If we are visible, force the DRAWN bit to on so that
11378 // this invalidate will go through (at least to our parent).
11379 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011380 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381 // the DRAWN bit.
11382 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011383 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011384 // parent display list may need to be recreated based on a change in the bounds
11385 // of any child
11386 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 }
11388
11389 // Reset drawn bit to original value (invalidate turns it off)
11390 mPrivateFlags |= drawn;
11391
11392 mBackgroundSizeChanged = true;
11393 }
11394 return changed;
11395 }
11396
11397 /**
11398 * Finalize inflating a view from XML. This is called as the last phase
11399 * of inflation, after all child views have been added.
11400 *
11401 * <p>Even if the subclass overrides onFinishInflate, they should always be
11402 * sure to call the super method, so that we get called.
11403 */
11404 protected void onFinishInflate() {
11405 }
11406
11407 /**
11408 * Returns the resources associated with this view.
11409 *
11410 * @return Resources object.
11411 */
11412 public Resources getResources() {
11413 return mResources;
11414 }
11415
11416 /**
11417 * Invalidates the specified Drawable.
11418 *
11419 * @param drawable the drawable to invalidate
11420 */
11421 public void invalidateDrawable(Drawable drawable) {
11422 if (verifyDrawable(drawable)) {
11423 final Rect dirty = drawable.getBounds();
11424 final int scrollX = mScrollX;
11425 final int scrollY = mScrollY;
11426
11427 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11428 dirty.right + scrollX, dirty.bottom + scrollY);
11429 }
11430 }
11431
11432 /**
11433 * Schedules an action on a drawable to occur at a specified time.
11434 *
11435 * @param who the recipient of the action
11436 * @param what the action to run on the drawable
11437 * @param when the time at which the action must occur. Uses the
11438 * {@link SystemClock#uptimeMillis} timebase.
11439 */
11440 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011441 if (verifyDrawable(who) && what != null) {
11442 if (mAttachInfo != null) {
11443 mAttachInfo.mHandler.postAtTime(what, who, when);
11444 } else {
11445 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011447 }
11448 }
11449
11450 /**
11451 * Cancels a scheduled action on a drawable.
11452 *
11453 * @param who the recipient of the action
11454 * @param what the action to cancel
11455 */
11456 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011457 if (verifyDrawable(who) && what != null) {
11458 if (mAttachInfo != null) {
11459 mAttachInfo.mHandler.removeCallbacks(what, who);
11460 } else {
11461 ViewRootImpl.getRunQueue().removeCallbacks(what);
11462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 }
11464 }
11465
11466 /**
11467 * Unschedule any events associated with the given Drawable. This can be
11468 * used when selecting a new Drawable into a view, so that the previous
11469 * one is completely unscheduled.
11470 *
11471 * @param who The Drawable to unschedule.
11472 *
11473 * @see #drawableStateChanged
11474 */
11475 public void unscheduleDrawable(Drawable who) {
11476 if (mAttachInfo != null) {
11477 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11478 }
11479 }
11480
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011481 /**
11482 * Return the layout direction of a given Drawable.
11483 *
11484 * @param who the Drawable to query
11485 *
11486 * @hide
11487 */
11488 public int getResolvedLayoutDirection(Drawable who) {
11489 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011490 }
11491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011492 /**
11493 * If your view subclass is displaying its own Drawable objects, it should
11494 * override this function and return true for any Drawable it is
11495 * displaying. This allows animations for those drawables to be
11496 * scheduled.
11497 *
11498 * <p>Be sure to call through to the super class when overriding this
11499 * function.
11500 *
11501 * @param who The Drawable to verify. Return true if it is one you are
11502 * displaying, else return the result of calling through to the
11503 * super class.
11504 *
11505 * @return boolean If true than the Drawable is being displayed in the
11506 * view; else false and it is not allowed to animate.
11507 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011508 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11509 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011510 */
11511 protected boolean verifyDrawable(Drawable who) {
11512 return who == mBGDrawable;
11513 }
11514
11515 /**
11516 * This function is called whenever the state of the view changes in such
11517 * a way that it impacts the state of drawables being shown.
11518 *
11519 * <p>Be sure to call through to the superclass when overriding this
11520 * function.
11521 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011522 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011523 */
11524 protected void drawableStateChanged() {
11525 Drawable d = mBGDrawable;
11526 if (d != null && d.isStateful()) {
11527 d.setState(getDrawableState());
11528 }
11529 }
11530
11531 /**
11532 * Call this to force a view to update its drawable state. This will cause
11533 * drawableStateChanged to be called on this view. Views that are interested
11534 * in the new state should call getDrawableState.
11535 *
11536 * @see #drawableStateChanged
11537 * @see #getDrawableState
11538 */
11539 public void refreshDrawableState() {
11540 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11541 drawableStateChanged();
11542
11543 ViewParent parent = mParent;
11544 if (parent != null) {
11545 parent.childDrawableStateChanged(this);
11546 }
11547 }
11548
11549 /**
11550 * Return an array of resource IDs of the drawable states representing the
11551 * current state of the view.
11552 *
11553 * @return The current drawable state
11554 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011555 * @see Drawable#setState(int[])
11556 * @see #drawableStateChanged()
11557 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011558 */
11559 public final int[] getDrawableState() {
11560 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11561 return mDrawableState;
11562 } else {
11563 mDrawableState = onCreateDrawableState(0);
11564 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11565 return mDrawableState;
11566 }
11567 }
11568
11569 /**
11570 * Generate the new {@link android.graphics.drawable.Drawable} state for
11571 * this view. This is called by the view
11572 * system when the cached Drawable state is determined to be invalid. To
11573 * retrieve the current state, you should use {@link #getDrawableState}.
11574 *
11575 * @param extraSpace if non-zero, this is the number of extra entries you
11576 * would like in the returned array in which you can place your own
11577 * states.
11578 *
11579 * @return Returns an array holding the current {@link Drawable} state of
11580 * the view.
11581 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011582 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011583 */
11584 protected int[] onCreateDrawableState(int extraSpace) {
11585 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11586 mParent instanceof View) {
11587 return ((View) mParent).onCreateDrawableState(extraSpace);
11588 }
11589
11590 int[] drawableState;
11591
11592 int privateFlags = mPrivateFlags;
11593
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011594 int viewStateIndex = 0;
11595 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11596 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11597 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011598 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011599 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11600 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011601 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11602 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011603 // This is set if HW acceleration is requested, even if the current
11604 // process doesn't allow it. This is just to allow app preview
11605 // windows to better match their app.
11606 viewStateIndex |= VIEW_STATE_ACCELERATED;
11607 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011608 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011609
Christopher Tate3d4bf172011-03-28 16:16:46 -070011610 final int privateFlags2 = mPrivateFlags2;
11611 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11612 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 drawableState = VIEW_STATE_SETS[viewStateIndex];
11615
11616 //noinspection ConstantIfStatement
11617 if (false) {
11618 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11619 Log.i("View", toString()
11620 + " pressed=" + ((privateFlags & PRESSED) != 0)
11621 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11622 + " fo=" + hasFocus()
11623 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011624 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011625 + ": " + Arrays.toString(drawableState));
11626 }
11627
11628 if (extraSpace == 0) {
11629 return drawableState;
11630 }
11631
11632 final int[] fullState;
11633 if (drawableState != null) {
11634 fullState = new int[drawableState.length + extraSpace];
11635 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11636 } else {
11637 fullState = new int[extraSpace];
11638 }
11639
11640 return fullState;
11641 }
11642
11643 /**
11644 * Merge your own state values in <var>additionalState</var> into the base
11645 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011646 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 *
11648 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011649 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 * own additional state values.
11651 *
11652 * @param additionalState The additional state values you would like
11653 * added to <var>baseState</var>; this array is not modified.
11654 *
11655 * @return As a convenience, the <var>baseState</var> array you originally
11656 * passed into the function is returned.
11657 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011658 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 */
11660 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11661 final int N = baseState.length;
11662 int i = N - 1;
11663 while (i >= 0 && baseState[i] == 0) {
11664 i--;
11665 }
11666 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11667 return baseState;
11668 }
11669
11670 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011671 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11672 * on all Drawable objects associated with this view.
11673 */
11674 public void jumpDrawablesToCurrentState() {
11675 if (mBGDrawable != null) {
11676 mBGDrawable.jumpToCurrentState();
11677 }
11678 }
11679
11680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 * Sets the background color for this view.
11682 * @param color the color of the background
11683 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011684 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011686 if (mBGDrawable instanceof ColorDrawable) {
11687 ((ColorDrawable) mBGDrawable).setColor(color);
11688 } else {
11689 setBackgroundDrawable(new ColorDrawable(color));
11690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011691 }
11692
11693 /**
11694 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011695 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011696 * @param resid The identifier of the resource.
11697 * @attr ref android.R.styleable#View_background
11698 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011699 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011700 public void setBackgroundResource(int resid) {
11701 if (resid != 0 && resid == mBackgroundResource) {
11702 return;
11703 }
11704
11705 Drawable d= null;
11706 if (resid != 0) {
11707 d = mResources.getDrawable(resid);
11708 }
11709 setBackgroundDrawable(d);
11710
11711 mBackgroundResource = resid;
11712 }
11713
11714 /**
11715 * Set the background to a given Drawable, or remove the background. If the
11716 * background has padding, this View's padding is set to the background's
11717 * padding. However, when a background is removed, this View's padding isn't
11718 * touched. If setting the padding is desired, please use
11719 * {@link #setPadding(int, int, int, int)}.
11720 *
11721 * @param d The Drawable to use as the background, or null to remove the
11722 * background
11723 */
11724 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011725 if (d == mBGDrawable) {
11726 return;
11727 }
11728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011729 boolean requestLayout = false;
11730
11731 mBackgroundResource = 0;
11732
11733 /*
11734 * Regardless of whether we're setting a new background or not, we want
11735 * to clear the previous drawable.
11736 */
11737 if (mBGDrawable != null) {
11738 mBGDrawable.setCallback(null);
11739 unscheduleDrawable(mBGDrawable);
11740 }
11741
11742 if (d != null) {
11743 Rect padding = sThreadLocal.get();
11744 if (padding == null) {
11745 padding = new Rect();
11746 sThreadLocal.set(padding);
11747 }
11748 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011749 switch (d.getResolvedLayoutDirectionSelf()) {
11750 case LAYOUT_DIRECTION_RTL:
11751 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11752 break;
11753 case LAYOUT_DIRECTION_LTR:
11754 default:
11755 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 }
11758
11759 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11760 // if it has a different minimum size, we should layout again
11761 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11762 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11763 requestLayout = true;
11764 }
11765
11766 d.setCallback(this);
11767 if (d.isStateful()) {
11768 d.setState(getDrawableState());
11769 }
11770 d.setVisible(getVisibility() == VISIBLE, false);
11771 mBGDrawable = d;
11772
11773 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11774 mPrivateFlags &= ~SKIP_DRAW;
11775 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11776 requestLayout = true;
11777 }
11778 } else {
11779 /* Remove the background */
11780 mBGDrawable = null;
11781
11782 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11783 /*
11784 * This view ONLY drew the background before and we're removing
11785 * the background, so now it won't draw anything
11786 * (hence we SKIP_DRAW)
11787 */
11788 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11789 mPrivateFlags |= SKIP_DRAW;
11790 }
11791
11792 /*
11793 * When the background is set, we try to apply its padding to this
11794 * View. When the background is removed, we don't touch this View's
11795 * padding. This is noted in the Javadocs. Hence, we don't need to
11796 * requestLayout(), the invalidate() below is sufficient.
11797 */
11798
11799 // The old background's minimum size could have affected this
11800 // View's layout, so let's requestLayout
11801 requestLayout = true;
11802 }
11803
Romain Guy8f1344f52009-05-15 16:03:59 -070011804 computeOpaqueFlags();
11805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011806 if (requestLayout) {
11807 requestLayout();
11808 }
11809
11810 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011811 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011812 }
11813
11814 /**
11815 * Gets the background drawable
11816 * @return The drawable used as the background for this view, if any.
11817 */
11818 public Drawable getBackground() {
11819 return mBGDrawable;
11820 }
11821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822 /**
11823 * Sets the padding. The view may add on the space required to display
11824 * the scrollbars, depending on the style and visibility of the scrollbars.
11825 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11826 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11827 * from the values set in this call.
11828 *
11829 * @attr ref android.R.styleable#View_padding
11830 * @attr ref android.R.styleable#View_paddingBottom
11831 * @attr ref android.R.styleable#View_paddingLeft
11832 * @attr ref android.R.styleable#View_paddingRight
11833 * @attr ref android.R.styleable#View_paddingTop
11834 * @param left the left padding in pixels
11835 * @param top the top padding in pixels
11836 * @param right the right padding in pixels
11837 * @param bottom the bottom padding in pixels
11838 */
11839 public void setPadding(int left, int top, int right, int bottom) {
11840 boolean changed = false;
11841
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011842 mUserPaddingRelative = false;
11843
Adam Powell20232d02010-12-08 21:08:53 -080011844 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011845 mUserPaddingRight = right;
11846 mUserPaddingBottom = bottom;
11847
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011848 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011849
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011850 // Common case is there are no scroll bars.
11851 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011852 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011853 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011854 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011855 switch (mVerticalScrollbarPosition) {
11856 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011857 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11858 left += offset;
11859 } else {
11860 right += offset;
11861 }
11862 break;
Adam Powell20232d02010-12-08 21:08:53 -080011863 case SCROLLBAR_POSITION_RIGHT:
11864 right += offset;
11865 break;
11866 case SCROLLBAR_POSITION_LEFT:
11867 left += offset;
11868 break;
11869 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011870 }
Adam Powell20232d02010-12-08 21:08:53 -080011871 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011872 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11873 ? 0 : getHorizontalScrollbarHeight();
11874 }
11875 }
Romain Guy8506ab42009-06-11 17:35:47 -070011876
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011877 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011878 changed = true;
11879 mPaddingLeft = left;
11880 }
11881 if (mPaddingTop != top) {
11882 changed = true;
11883 mPaddingTop = top;
11884 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011885 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011886 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011887 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011888 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011889 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011890 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011891 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 }
11893
11894 if (changed) {
11895 requestLayout();
11896 }
11897 }
11898
11899 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011900 * Sets the relative padding. The view may add on the space required to display
11901 * the scrollbars, depending on the style and visibility of the scrollbars.
11902 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11903 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11904 * from the values set in this call.
11905 *
11906 * @attr ref android.R.styleable#View_padding
11907 * @attr ref android.R.styleable#View_paddingBottom
11908 * @attr ref android.R.styleable#View_paddingStart
11909 * @attr ref android.R.styleable#View_paddingEnd
11910 * @attr ref android.R.styleable#View_paddingTop
11911 * @param start the start padding in pixels
11912 * @param top the top padding in pixels
11913 * @param end the end padding in pixels
11914 * @param bottom the bottom padding in pixels
11915 *
11916 * @hide
11917 */
11918 public void setPaddingRelative(int start, int top, int end, int bottom) {
11919 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011920
11921 mUserPaddingStart = start;
11922 mUserPaddingEnd = end;
11923
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011924 switch(getResolvedLayoutDirection()) {
11925 case LAYOUT_DIRECTION_RTL:
11926 setPadding(end, top, start, bottom);
11927 break;
11928 case LAYOUT_DIRECTION_LTR:
11929 default:
11930 setPadding(start, top, end, bottom);
11931 }
11932 }
11933
11934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 * Returns the top padding of this view.
11936 *
11937 * @return the top padding in pixels
11938 */
11939 public int getPaddingTop() {
11940 return mPaddingTop;
11941 }
11942
11943 /**
11944 * Returns the bottom padding of this view. If there are inset and enabled
11945 * scrollbars, this value may include the space required to display the
11946 * scrollbars as well.
11947 *
11948 * @return the bottom padding in pixels
11949 */
11950 public int getPaddingBottom() {
11951 return mPaddingBottom;
11952 }
11953
11954 /**
11955 * Returns the left padding of this view. If there are inset and enabled
11956 * scrollbars, this value may include the space required to display the
11957 * scrollbars as well.
11958 *
11959 * @return the left padding in pixels
11960 */
11961 public int getPaddingLeft() {
11962 return mPaddingLeft;
11963 }
11964
11965 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011966 * Returns the start padding of this view. If there are inset and enabled
11967 * scrollbars, this value may include the space required to display the
11968 * scrollbars as well.
11969 *
11970 * @return the start padding in pixels
11971 *
11972 * @hide
11973 */
11974 public int getPaddingStart() {
11975 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11976 mPaddingRight : mPaddingLeft;
11977 }
11978
11979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011980 * Returns the right padding of this view. If there are inset and enabled
11981 * scrollbars, this value may include the space required to display the
11982 * scrollbars as well.
11983 *
11984 * @return the right padding in pixels
11985 */
11986 public int getPaddingRight() {
11987 return mPaddingRight;
11988 }
11989
11990 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011991 * Returns the end padding of this view. If there are inset and enabled
11992 * scrollbars, this value may include the space required to display the
11993 * scrollbars as well.
11994 *
11995 * @return the end padding in pixels
11996 *
11997 * @hide
11998 */
11999 public int getPaddingEnd() {
12000 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12001 mPaddingLeft : mPaddingRight;
12002 }
12003
12004 /**
12005 * Return if the padding as been set thru relative values
12006 * {@link #setPaddingRelative(int, int, int, int)} or thru
12007 * @attr ref android.R.styleable#View_paddingStart or
12008 * @attr ref android.R.styleable#View_paddingEnd
12009 *
12010 * @return true if the padding is relative or false if it is not.
12011 *
12012 * @hide
12013 */
12014 public boolean isPaddingRelative() {
12015 return mUserPaddingRelative;
12016 }
12017
12018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012019 * Changes the selection state of this view. A view can be selected or not.
12020 * Note that selection is not the same as focus. Views are typically
12021 * selected in the context of an AdapterView like ListView or GridView;
12022 * the selected view is the view that is highlighted.
12023 *
12024 * @param selected true if the view must be selected, false otherwise
12025 */
12026 public void setSelected(boolean selected) {
12027 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12028 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012029 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012030 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 refreshDrawableState();
12032 dispatchSetSelected(selected);
12033 }
12034 }
12035
12036 /**
12037 * Dispatch setSelected to all of this View's children.
12038 *
12039 * @see #setSelected(boolean)
12040 *
12041 * @param selected The new selected state
12042 */
12043 protected void dispatchSetSelected(boolean selected) {
12044 }
12045
12046 /**
12047 * Indicates the selection state of this view.
12048 *
12049 * @return true if the view is selected, false otherwise
12050 */
12051 @ViewDebug.ExportedProperty
12052 public boolean isSelected() {
12053 return (mPrivateFlags & SELECTED) != 0;
12054 }
12055
12056 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012057 * Changes the activated state of this view. A view can be activated or not.
12058 * Note that activation is not the same as selection. Selection is
12059 * a transient property, representing the view (hierarchy) the user is
12060 * currently interacting with. Activation is a longer-term state that the
12061 * user can move views in and out of. For example, in a list view with
12062 * single or multiple selection enabled, the views in the current selection
12063 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12064 * here.) The activated state is propagated down to children of the view it
12065 * is set on.
12066 *
12067 * @param activated true if the view must be activated, false otherwise
12068 */
12069 public void setActivated(boolean activated) {
12070 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12071 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012072 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012073 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012074 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012075 }
12076 }
12077
12078 /**
12079 * Dispatch setActivated to all of this View's children.
12080 *
12081 * @see #setActivated(boolean)
12082 *
12083 * @param activated The new activated state
12084 */
12085 protected void dispatchSetActivated(boolean activated) {
12086 }
12087
12088 /**
12089 * Indicates the activation state of this view.
12090 *
12091 * @return true if the view is activated, false otherwise
12092 */
12093 @ViewDebug.ExportedProperty
12094 public boolean isActivated() {
12095 return (mPrivateFlags & ACTIVATED) != 0;
12096 }
12097
12098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12100 * observer can be used to get notifications when global events, like
12101 * layout, happen.
12102 *
12103 * The returned ViewTreeObserver observer is not guaranteed to remain
12104 * valid for the lifetime of this View. If the caller of this method keeps
12105 * a long-lived reference to ViewTreeObserver, it should always check for
12106 * the return value of {@link ViewTreeObserver#isAlive()}.
12107 *
12108 * @return The ViewTreeObserver for this view's hierarchy.
12109 */
12110 public ViewTreeObserver getViewTreeObserver() {
12111 if (mAttachInfo != null) {
12112 return mAttachInfo.mTreeObserver;
12113 }
12114 if (mFloatingTreeObserver == null) {
12115 mFloatingTreeObserver = new ViewTreeObserver();
12116 }
12117 return mFloatingTreeObserver;
12118 }
12119
12120 /**
12121 * <p>Finds the topmost view in the current view hierarchy.</p>
12122 *
12123 * @return the topmost view containing this view
12124 */
12125 public View getRootView() {
12126 if (mAttachInfo != null) {
12127 final View v = mAttachInfo.mRootView;
12128 if (v != null) {
12129 return v;
12130 }
12131 }
Romain Guy8506ab42009-06-11 17:35:47 -070012132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012133 View parent = this;
12134
12135 while (parent.mParent != null && parent.mParent instanceof View) {
12136 parent = (View) parent.mParent;
12137 }
12138
12139 return parent;
12140 }
12141
12142 /**
12143 * <p>Computes the coordinates of this view on the screen. The argument
12144 * must be an array of two integers. After the method returns, the array
12145 * contains the x and y location in that order.</p>
12146 *
12147 * @param location an array of two integers in which to hold the coordinates
12148 */
12149 public void getLocationOnScreen(int[] location) {
12150 getLocationInWindow(location);
12151
12152 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012153 if (info != null) {
12154 location[0] += info.mWindowLeft;
12155 location[1] += info.mWindowTop;
12156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 }
12158
12159 /**
12160 * <p>Computes the coordinates of this view in its window. The argument
12161 * must be an array of two integers. After the method returns, the array
12162 * contains the x and y location in that order.</p>
12163 *
12164 * @param location an array of two integers in which to hold the coordinates
12165 */
12166 public void getLocationInWindow(int[] location) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012167 // When the view is not attached to a window, this method does not make sense
12168 if (mAttachInfo == null) return;
12169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012170 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012171 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 }
12173
Gilles Debunnecea45132011-11-24 02:19:27 +010012174 float[] position = mAttachInfo.mTmpTransformLocation;
12175 position[0] = position[1] = 0.0f;
12176
12177 if (!hasIdentityMatrix()) {
12178 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180
Gilles Debunnecea45132011-11-24 02:19:27 +010012181 position[0] += mLeft;
12182 position[1] += mTop;
12183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184 ViewParent viewParent = mParent;
12185 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012186 final View view = (View) viewParent;
12187
12188 position[0] -= view.mScrollX;
12189 position[1] -= view.mScrollY;
12190
12191 if (!view.hasIdentityMatrix()) {
12192 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012193 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012194
12195 position[0] += view.mLeft;
12196 position[1] += view.mTop;
12197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012198 viewParent = view.mParent;
12199 }
Romain Guy8506ab42009-06-11 17:35:47 -070012200
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012201 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012203 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12204 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012205 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012206
12207 location[0] = (int) (position[0] + 0.5f);
12208 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012209 }
12210
12211 /**
12212 * {@hide}
12213 * @param id the id of the view to be found
12214 * @return the view of the specified id, null if cannot be found
12215 */
12216 protected View findViewTraversal(int id) {
12217 if (id == mID) {
12218 return this;
12219 }
12220 return null;
12221 }
12222
12223 /**
12224 * {@hide}
12225 * @param tag the tag of the view to be found
12226 * @return the view of specified tag, null if cannot be found
12227 */
12228 protected View findViewWithTagTraversal(Object tag) {
12229 if (tag != null && tag.equals(mTag)) {
12230 return this;
12231 }
12232 return null;
12233 }
12234
12235 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012236 * {@hide}
12237 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012238 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012239 * @return The first view that matches the predicate or null.
12240 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012241 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012242 if (predicate.apply(this)) {
12243 return this;
12244 }
12245 return null;
12246 }
12247
12248 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012249 * Look for a child view with the given id. If this view has the given
12250 * id, return this view.
12251 *
12252 * @param id The id to search for.
12253 * @return The view that has the given id in the hierarchy or null
12254 */
12255 public final View findViewById(int id) {
12256 if (id < 0) {
12257 return null;
12258 }
12259 return findViewTraversal(id);
12260 }
12261
12262 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012263 * Finds a view by its unuque and stable accessibility id.
12264 *
12265 * @param accessibilityId The searched accessibility id.
12266 * @return The found view.
12267 */
12268 final View findViewByAccessibilityId(int accessibilityId) {
12269 if (accessibilityId < 0) {
12270 return null;
12271 }
12272 return findViewByAccessibilityIdTraversal(accessibilityId);
12273 }
12274
12275 /**
12276 * Performs the traversal to find a view by its unuque and stable accessibility id.
12277 *
12278 * <strong>Note:</strong>This method does not stop at the root namespace
12279 * boundary since the user can touch the screen at an arbitrary location
12280 * potentially crossing the root namespace bounday which will send an
12281 * accessibility event to accessibility services and they should be able
12282 * to obtain the event source. Also accessibility ids are guaranteed to be
12283 * unique in the window.
12284 *
12285 * @param accessibilityId The accessibility id.
12286 * @return The found view.
12287 */
12288 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12289 if (getAccessibilityViewId() == accessibilityId) {
12290 return this;
12291 }
12292 return null;
12293 }
12294
12295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012296 * Look for a child view with the given tag. If this view has the given
12297 * tag, return this view.
12298 *
12299 * @param tag The tag to search for, using "tag.equals(getTag())".
12300 * @return The View that has the given tag in the hierarchy or null
12301 */
12302 public final View findViewWithTag(Object tag) {
12303 if (tag == null) {
12304 return null;
12305 }
12306 return findViewWithTagTraversal(tag);
12307 }
12308
12309 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012310 * {@hide}
12311 * Look for a child view that matches the specified predicate.
12312 * If this view matches the predicate, return this view.
12313 *
12314 * @param predicate The predicate to evaluate.
12315 * @return The first view that matches the predicate or null.
12316 */
12317 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012318 return findViewByPredicateTraversal(predicate, null);
12319 }
12320
12321 /**
12322 * {@hide}
12323 * Look for a child view that matches the specified predicate,
12324 * starting with the specified view and its descendents and then
12325 * recusively searching the ancestors and siblings of that view
12326 * until this view is reached.
12327 *
12328 * This method is useful in cases where the predicate does not match
12329 * a single unique view (perhaps multiple views use the same id)
12330 * and we are trying to find the view that is "closest" in scope to the
12331 * starting view.
12332 *
12333 * @param start The view to start from.
12334 * @param predicate The predicate to evaluate.
12335 * @return The first view that matches the predicate or null.
12336 */
12337 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12338 View childToSkip = null;
12339 for (;;) {
12340 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12341 if (view != null || start == this) {
12342 return view;
12343 }
12344
12345 ViewParent parent = start.getParent();
12346 if (parent == null || !(parent instanceof View)) {
12347 return null;
12348 }
12349
12350 childToSkip = start;
12351 start = (View) parent;
12352 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012353 }
12354
12355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012356 * Sets the identifier for this view. The identifier does not have to be
12357 * unique in this view's hierarchy. The identifier should be a positive
12358 * number.
12359 *
12360 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012361 * @see #getId()
12362 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012363 *
12364 * @param id a number used to identify the view
12365 *
12366 * @attr ref android.R.styleable#View_id
12367 */
12368 public void setId(int id) {
12369 mID = id;
12370 }
12371
12372 /**
12373 * {@hide}
12374 *
12375 * @param isRoot true if the view belongs to the root namespace, false
12376 * otherwise
12377 */
12378 public void setIsRootNamespace(boolean isRoot) {
12379 if (isRoot) {
12380 mPrivateFlags |= IS_ROOT_NAMESPACE;
12381 } else {
12382 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12383 }
12384 }
12385
12386 /**
12387 * {@hide}
12388 *
12389 * @return true if the view belongs to the root namespace, false otherwise
12390 */
12391 public boolean isRootNamespace() {
12392 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12393 }
12394
12395 /**
12396 * Returns this view's identifier.
12397 *
12398 * @return a positive integer used to identify the view or {@link #NO_ID}
12399 * if the view has no ID
12400 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012401 * @see #setId(int)
12402 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 * @attr ref android.R.styleable#View_id
12404 */
12405 @ViewDebug.CapturedViewProperty
12406 public int getId() {
12407 return mID;
12408 }
12409
12410 /**
12411 * Returns this view's tag.
12412 *
12413 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012414 *
12415 * @see #setTag(Object)
12416 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 */
12418 @ViewDebug.ExportedProperty
12419 public Object getTag() {
12420 return mTag;
12421 }
12422
12423 /**
12424 * Sets the tag associated with this view. A tag can be used to mark
12425 * a view in its hierarchy and does not have to be unique within the
12426 * hierarchy. Tags can also be used to store data within a view without
12427 * resorting to another data structure.
12428 *
12429 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012430 *
12431 * @see #getTag()
12432 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012433 */
12434 public void setTag(final Object tag) {
12435 mTag = tag;
12436 }
12437
12438 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012439 * Returns the tag associated with this view and the specified key.
12440 *
12441 * @param key The key identifying the tag
12442 *
12443 * @return the Object stored in this view as a tag
12444 *
12445 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012446 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012447 */
12448 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012449 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012450 return null;
12451 }
12452
12453 /**
12454 * Sets a tag associated with this view and a key. A tag can be used
12455 * to mark a view in its hierarchy and does not have to be unique within
12456 * the hierarchy. Tags can also be used to store data within a view
12457 * without resorting to another data structure.
12458 *
12459 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012460 * application to ensure it is unique (see the <a
12461 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12462 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012463 * the Android framework or not associated with any package will cause
12464 * an {@link IllegalArgumentException} to be thrown.
12465 *
12466 * @param key The key identifying the tag
12467 * @param tag An Object to tag the view with
12468 *
12469 * @throws IllegalArgumentException If they specified key is not valid
12470 *
12471 * @see #setTag(Object)
12472 * @see #getTag(int)
12473 */
12474 public void setTag(int key, final Object tag) {
12475 // If the package id is 0x00 or 0x01, it's either an undefined package
12476 // or a framework id
12477 if ((key >>> 24) < 2) {
12478 throw new IllegalArgumentException("The key must be an application-specific "
12479 + "resource id.");
12480 }
12481
Adam Powell2b2f6d62011-09-23 11:15:39 -070012482 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012483 }
12484
12485 /**
12486 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12487 * framework id.
12488 *
12489 * @hide
12490 */
12491 public void setTagInternal(int key, Object tag) {
12492 if ((key >>> 24) != 0x1) {
12493 throw new IllegalArgumentException("The key must be a framework-specific "
12494 + "resource id.");
12495 }
12496
Adam Powell2b2f6d62011-09-23 11:15:39 -070012497 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012498 }
12499
Adam Powell2b2f6d62011-09-23 11:15:39 -070012500 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012501 if (mKeyedTags == null) {
12502 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012503 }
12504
Adam Powell7db82ac2011-09-22 19:44:04 -070012505 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012506 }
12507
12508 /**
Romain Guy13922e02009-05-12 17:56:14 -070012509 * @param consistency The type of consistency. See ViewDebug for more information.
12510 *
12511 * @hide
12512 */
12513 protected boolean dispatchConsistencyCheck(int consistency) {
12514 return onConsistencyCheck(consistency);
12515 }
12516
12517 /**
12518 * Method that subclasses should implement to check their consistency. The type of
12519 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012520 *
Romain Guy13922e02009-05-12 17:56:14 -070012521 * @param consistency The type of consistency. See ViewDebug for more information.
12522 *
12523 * @throws IllegalStateException if the view is in an inconsistent state.
12524 *
12525 * @hide
12526 */
12527 protected boolean onConsistencyCheck(int consistency) {
12528 boolean result = true;
12529
12530 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12531 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12532
12533 if (checkLayout) {
12534 if (getParent() == null) {
12535 result = false;
12536 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12537 "View " + this + " does not have a parent.");
12538 }
12539
12540 if (mAttachInfo == null) {
12541 result = false;
12542 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12543 "View " + this + " is not attached to a window.");
12544 }
12545 }
12546
12547 if (checkDrawing) {
12548 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12549 // from their draw() method
12550
12551 if ((mPrivateFlags & DRAWN) != DRAWN &&
12552 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12553 result = false;
12554 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12555 "View " + this + " was invalidated but its drawing cache is valid.");
12556 }
12557 }
12558
12559 return result;
12560 }
12561
12562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012563 * Prints information about this view in the log output, with the tag
12564 * {@link #VIEW_LOG_TAG}.
12565 *
12566 * @hide
12567 */
12568 public void debug() {
12569 debug(0);
12570 }
12571
12572 /**
12573 * Prints information about this view in the log output, with the tag
12574 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12575 * indentation defined by the <code>depth</code>.
12576 *
12577 * @param depth the indentation level
12578 *
12579 * @hide
12580 */
12581 protected void debug(int depth) {
12582 String output = debugIndent(depth - 1);
12583
12584 output += "+ " + this;
12585 int id = getId();
12586 if (id != -1) {
12587 output += " (id=" + id + ")";
12588 }
12589 Object tag = getTag();
12590 if (tag != null) {
12591 output += " (tag=" + tag + ")";
12592 }
12593 Log.d(VIEW_LOG_TAG, output);
12594
12595 if ((mPrivateFlags & FOCUSED) != 0) {
12596 output = debugIndent(depth) + " FOCUSED";
12597 Log.d(VIEW_LOG_TAG, output);
12598 }
12599
12600 output = debugIndent(depth);
12601 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12602 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12603 + "} ";
12604 Log.d(VIEW_LOG_TAG, output);
12605
12606 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12607 || mPaddingBottom != 0) {
12608 output = debugIndent(depth);
12609 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12610 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12611 Log.d(VIEW_LOG_TAG, output);
12612 }
12613
12614 output = debugIndent(depth);
12615 output += "mMeasureWidth=" + mMeasuredWidth +
12616 " mMeasureHeight=" + mMeasuredHeight;
12617 Log.d(VIEW_LOG_TAG, output);
12618
12619 output = debugIndent(depth);
12620 if (mLayoutParams == null) {
12621 output += "BAD! no layout params";
12622 } else {
12623 output = mLayoutParams.debug(output);
12624 }
12625 Log.d(VIEW_LOG_TAG, output);
12626
12627 output = debugIndent(depth);
12628 output += "flags={";
12629 output += View.printFlags(mViewFlags);
12630 output += "}";
12631 Log.d(VIEW_LOG_TAG, output);
12632
12633 output = debugIndent(depth);
12634 output += "privateFlags={";
12635 output += View.printPrivateFlags(mPrivateFlags);
12636 output += "}";
12637 Log.d(VIEW_LOG_TAG, output);
12638 }
12639
12640 /**
12641 * Creates an string of whitespaces used for indentation.
12642 *
12643 * @param depth the indentation level
12644 * @return a String containing (depth * 2 + 3) * 2 white spaces
12645 *
12646 * @hide
12647 */
12648 protected static String debugIndent(int depth) {
12649 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12650 for (int i = 0; i < (depth * 2) + 3; i++) {
12651 spaces.append(' ').append(' ');
12652 }
12653 return spaces.toString();
12654 }
12655
12656 /**
12657 * <p>Return the offset of the widget's text baseline from the widget's top
12658 * boundary. If this widget does not support baseline alignment, this
12659 * method returns -1. </p>
12660 *
12661 * @return the offset of the baseline within the widget's bounds or -1
12662 * if baseline alignment is not supported
12663 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012664 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012665 public int getBaseline() {
12666 return -1;
12667 }
12668
12669 /**
12670 * Call this when something has changed which has invalidated the
12671 * layout of this view. This will schedule a layout pass of the view
12672 * tree.
12673 */
12674 public void requestLayout() {
12675 if (ViewDebug.TRACE_HIERARCHY) {
12676 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12677 }
12678
12679 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012680 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012681
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012682 if (mParent != null) {
12683 if (mLayoutParams != null) {
12684 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12685 }
12686 if (!mParent.isLayoutRequested()) {
12687 mParent.requestLayout();
12688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012689 }
12690 }
12691
12692 /**
12693 * Forces this view to be laid out during the next layout pass.
12694 * This method does not call requestLayout() or forceLayout()
12695 * on the parent.
12696 */
12697 public void forceLayout() {
12698 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012699 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700 }
12701
12702 /**
12703 * <p>
12704 * This is called to find out how big a view should be. The parent
12705 * supplies constraint information in the width and height parameters.
12706 * </p>
12707 *
12708 * <p>
12709 * The actual mesurement work of a view is performed in
12710 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12711 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12712 * </p>
12713 *
12714 *
12715 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12716 * parent
12717 * @param heightMeasureSpec Vertical space requirements as imposed by the
12718 * parent
12719 *
12720 * @see #onMeasure(int, int)
12721 */
12722 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12723 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12724 widthMeasureSpec != mOldWidthMeasureSpec ||
12725 heightMeasureSpec != mOldHeightMeasureSpec) {
12726
12727 // first clears the measured dimension flag
12728 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12729
12730 if (ViewDebug.TRACE_HIERARCHY) {
12731 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12732 }
12733
12734 // measure ourselves, this should set the measured dimension flag back
12735 onMeasure(widthMeasureSpec, heightMeasureSpec);
12736
12737 // flag not set, setMeasuredDimension() was not invoked, we raise
12738 // an exception to warn the developer
12739 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12740 throw new IllegalStateException("onMeasure() did not set the"
12741 + " measured dimension by calling"
12742 + " setMeasuredDimension()");
12743 }
12744
12745 mPrivateFlags |= LAYOUT_REQUIRED;
12746 }
12747
12748 mOldWidthMeasureSpec = widthMeasureSpec;
12749 mOldHeightMeasureSpec = heightMeasureSpec;
12750 }
12751
12752 /**
12753 * <p>
12754 * Measure the view and its content to determine the measured width and the
12755 * measured height. This method is invoked by {@link #measure(int, int)} and
12756 * should be overriden by subclasses to provide accurate and efficient
12757 * measurement of their contents.
12758 * </p>
12759 *
12760 * <p>
12761 * <strong>CONTRACT:</strong> When overriding this method, you
12762 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12763 * measured width and height of this view. Failure to do so will trigger an
12764 * <code>IllegalStateException</code>, thrown by
12765 * {@link #measure(int, int)}. Calling the superclass'
12766 * {@link #onMeasure(int, int)} is a valid use.
12767 * </p>
12768 *
12769 * <p>
12770 * The base class implementation of measure defaults to the background size,
12771 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12772 * override {@link #onMeasure(int, int)} to provide better measurements of
12773 * their content.
12774 * </p>
12775 *
12776 * <p>
12777 * If this method is overridden, it is the subclass's responsibility to make
12778 * sure the measured height and width are at least the view's minimum height
12779 * and width ({@link #getSuggestedMinimumHeight()} and
12780 * {@link #getSuggestedMinimumWidth()}).
12781 * </p>
12782 *
12783 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12784 * The requirements are encoded with
12785 * {@link android.view.View.MeasureSpec}.
12786 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12787 * The requirements are encoded with
12788 * {@link android.view.View.MeasureSpec}.
12789 *
12790 * @see #getMeasuredWidth()
12791 * @see #getMeasuredHeight()
12792 * @see #setMeasuredDimension(int, int)
12793 * @see #getSuggestedMinimumHeight()
12794 * @see #getSuggestedMinimumWidth()
12795 * @see android.view.View.MeasureSpec#getMode(int)
12796 * @see android.view.View.MeasureSpec#getSize(int)
12797 */
12798 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12799 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12800 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12801 }
12802
12803 /**
12804 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12805 * measured width and measured height. Failing to do so will trigger an
12806 * exception at measurement time.</p>
12807 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012808 * @param measuredWidth The measured width of this view. May be a complex
12809 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12810 * {@link #MEASURED_STATE_TOO_SMALL}.
12811 * @param measuredHeight The measured height of this view. May be a complex
12812 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12813 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012814 */
12815 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12816 mMeasuredWidth = measuredWidth;
12817 mMeasuredHeight = measuredHeight;
12818
12819 mPrivateFlags |= MEASURED_DIMENSION_SET;
12820 }
12821
12822 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012823 * Merge two states as returned by {@link #getMeasuredState()}.
12824 * @param curState The current state as returned from a view or the result
12825 * of combining multiple views.
12826 * @param newState The new view state to combine.
12827 * @return Returns a new integer reflecting the combination of the two
12828 * states.
12829 */
12830 public static int combineMeasuredStates(int curState, int newState) {
12831 return curState | newState;
12832 }
12833
12834 /**
12835 * Version of {@link #resolveSizeAndState(int, int, int)}
12836 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12837 */
12838 public static int resolveSize(int size, int measureSpec) {
12839 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12840 }
12841
12842 /**
12843 * Utility to reconcile a desired size and state, with constraints imposed
12844 * by a MeasureSpec. Will take the desired size, unless a different size
12845 * is imposed by the constraints. The returned value is a compound integer,
12846 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12847 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12848 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849 *
12850 * @param size How big the view wants to be
12851 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012852 * @return Size information bit mask as defined by
12853 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012855 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012856 int result = size;
12857 int specMode = MeasureSpec.getMode(measureSpec);
12858 int specSize = MeasureSpec.getSize(measureSpec);
12859 switch (specMode) {
12860 case MeasureSpec.UNSPECIFIED:
12861 result = size;
12862 break;
12863 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012864 if (specSize < size) {
12865 result = specSize | MEASURED_STATE_TOO_SMALL;
12866 } else {
12867 result = size;
12868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012869 break;
12870 case MeasureSpec.EXACTLY:
12871 result = specSize;
12872 break;
12873 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012874 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012875 }
12876
12877 /**
12878 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012879 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012880 * by the MeasureSpec.
12881 *
12882 * @param size Default size for this view
12883 * @param measureSpec Constraints imposed by the parent
12884 * @return The size this view should be.
12885 */
12886 public static int getDefaultSize(int size, int measureSpec) {
12887 int result = size;
12888 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012889 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012890
12891 switch (specMode) {
12892 case MeasureSpec.UNSPECIFIED:
12893 result = size;
12894 break;
12895 case MeasureSpec.AT_MOST:
12896 case MeasureSpec.EXACTLY:
12897 result = specSize;
12898 break;
12899 }
12900 return result;
12901 }
12902
12903 /**
12904 * Returns the suggested minimum height that the view should use. This
12905 * returns the maximum of the view's minimum height
12906 * and the background's minimum height
12907 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12908 * <p>
12909 * When being used in {@link #onMeasure(int, int)}, the caller should still
12910 * ensure the returned height is within the requirements of the parent.
12911 *
12912 * @return The suggested minimum height of the view.
12913 */
12914 protected int getSuggestedMinimumHeight() {
12915 int suggestedMinHeight = mMinHeight;
12916
12917 if (mBGDrawable != null) {
12918 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12919 if (suggestedMinHeight < bgMinHeight) {
12920 suggestedMinHeight = bgMinHeight;
12921 }
12922 }
12923
12924 return suggestedMinHeight;
12925 }
12926
12927 /**
12928 * Returns the suggested minimum width that the view should use. This
12929 * returns the maximum of the view's minimum width)
12930 * and the background's minimum width
12931 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12932 * <p>
12933 * When being used in {@link #onMeasure(int, int)}, the caller should still
12934 * ensure the returned width is within the requirements of the parent.
12935 *
12936 * @return The suggested minimum width of the view.
12937 */
12938 protected int getSuggestedMinimumWidth() {
12939 int suggestedMinWidth = mMinWidth;
12940
12941 if (mBGDrawable != null) {
12942 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12943 if (suggestedMinWidth < bgMinWidth) {
12944 suggestedMinWidth = bgMinWidth;
12945 }
12946 }
12947
12948 return suggestedMinWidth;
12949 }
12950
12951 /**
12952 * Sets the minimum height of the view. It is not guaranteed the view will
12953 * be able to achieve this minimum height (for example, if its parent layout
12954 * constrains it with less available height).
12955 *
12956 * @param minHeight The minimum height the view will try to be.
12957 */
12958 public void setMinimumHeight(int minHeight) {
12959 mMinHeight = minHeight;
12960 }
12961
12962 /**
12963 * Sets the minimum width of the view. It is not guaranteed the view will
12964 * be able to achieve this minimum width (for example, if its parent layout
12965 * constrains it with less available width).
12966 *
12967 * @param minWidth The minimum width the view will try to be.
12968 */
12969 public void setMinimumWidth(int minWidth) {
12970 mMinWidth = minWidth;
12971 }
12972
12973 /**
12974 * Get the animation currently associated with this view.
12975 *
12976 * @return The animation that is currently playing or
12977 * scheduled to play for this view.
12978 */
12979 public Animation getAnimation() {
12980 return mCurrentAnimation;
12981 }
12982
12983 /**
12984 * Start the specified animation now.
12985 *
12986 * @param animation the animation to start now
12987 */
12988 public void startAnimation(Animation animation) {
12989 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12990 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012991 invalidateParentCaches();
12992 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012993 }
12994
12995 /**
12996 * Cancels any animations for this view.
12997 */
12998 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012999 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013000 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013002 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013003 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004 }
13005
13006 /**
13007 * Sets the next animation to play for this view.
13008 * If you want the animation to play immediately, use
13009 * startAnimation. This method provides allows fine-grained
13010 * control over the start time and invalidation, but you
13011 * must make sure that 1) the animation has a start time set, and
13012 * 2) the view will be invalidated when the animation is supposed to
13013 * start.
13014 *
13015 * @param animation The next animation, or null.
13016 */
13017 public void setAnimation(Animation animation) {
13018 mCurrentAnimation = animation;
13019 if (animation != null) {
13020 animation.reset();
13021 }
13022 }
13023
13024 /**
13025 * Invoked by a parent ViewGroup to notify the start of the animation
13026 * currently associated with this view. If you override this method,
13027 * always call super.onAnimationStart();
13028 *
13029 * @see #setAnimation(android.view.animation.Animation)
13030 * @see #getAnimation()
13031 */
13032 protected void onAnimationStart() {
13033 mPrivateFlags |= ANIMATION_STARTED;
13034 }
13035
13036 /**
13037 * Invoked by a parent ViewGroup to notify the end of the animation
13038 * currently associated with this view. If you override this method,
13039 * always call super.onAnimationEnd();
13040 *
13041 * @see #setAnimation(android.view.animation.Animation)
13042 * @see #getAnimation()
13043 */
13044 protected void onAnimationEnd() {
13045 mPrivateFlags &= ~ANIMATION_STARTED;
13046 }
13047
13048 /**
13049 * Invoked if there is a Transform that involves alpha. Subclass that can
13050 * draw themselves with the specified alpha should return true, and then
13051 * respect that alpha when their onDraw() is called. If this returns false
13052 * then the view may be redirected to draw into an offscreen buffer to
13053 * fulfill the request, which will look fine, but may be slower than if the
13054 * subclass handles it internally. The default implementation returns false.
13055 *
13056 * @param alpha The alpha (0..255) to apply to the view's drawing
13057 * @return true if the view can draw with the specified alpha.
13058 */
13059 protected boolean onSetAlpha(int alpha) {
13060 return false;
13061 }
13062
13063 /**
13064 * This is used by the RootView to perform an optimization when
13065 * the view hierarchy contains one or several SurfaceView.
13066 * SurfaceView is always considered transparent, but its children are not,
13067 * therefore all View objects remove themselves from the global transparent
13068 * region (passed as a parameter to this function).
13069 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013070 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071 *
13072 * @return Returns true if the effective visibility of the view at this
13073 * point is opaque, regardless of the transparent region; returns false
13074 * if it is possible for underlying windows to be seen behind the view.
13075 *
13076 * {@hide}
13077 */
13078 public boolean gatherTransparentRegion(Region region) {
13079 final AttachInfo attachInfo = mAttachInfo;
13080 if (region != null && attachInfo != null) {
13081 final int pflags = mPrivateFlags;
13082 if ((pflags & SKIP_DRAW) == 0) {
13083 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13084 // remove it from the transparent region.
13085 final int[] location = attachInfo.mTransparentLocation;
13086 getLocationInWindow(location);
13087 region.op(location[0], location[1], location[0] + mRight - mLeft,
13088 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13089 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13090 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13091 // exists, so we remove the background drawable's non-transparent
13092 // parts from this transparent region.
13093 applyDrawableToTransparentRegion(mBGDrawable, region);
13094 }
13095 }
13096 return true;
13097 }
13098
13099 /**
13100 * Play a sound effect for this view.
13101 *
13102 * <p>The framework will play sound effects for some built in actions, such as
13103 * clicking, but you may wish to play these effects in your widget,
13104 * for instance, for internal navigation.
13105 *
13106 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13107 * {@link #isSoundEffectsEnabled()} is true.
13108 *
13109 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13110 */
13111 public void playSoundEffect(int soundConstant) {
13112 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13113 return;
13114 }
13115 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13116 }
13117
13118 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013119 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013120 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013121 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013122 *
13123 * <p>The framework will provide haptic feedback for some built in actions,
13124 * such as long presses, but you may wish to provide feedback for your
13125 * own widget.
13126 *
13127 * <p>The feedback will only be performed if
13128 * {@link #isHapticFeedbackEnabled()} is true.
13129 *
13130 * @param feedbackConstant One of the constants defined in
13131 * {@link HapticFeedbackConstants}
13132 */
13133 public boolean performHapticFeedback(int feedbackConstant) {
13134 return performHapticFeedback(feedbackConstant, 0);
13135 }
13136
13137 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013138 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013139 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013140 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013141 *
13142 * @param feedbackConstant One of the constants defined in
13143 * {@link HapticFeedbackConstants}
13144 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13145 */
13146 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13147 if (mAttachInfo == null) {
13148 return false;
13149 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013150 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013151 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013152 && !isHapticFeedbackEnabled()) {
13153 return false;
13154 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013155 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13156 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013157 }
13158
13159 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013160 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013161 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13162 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013163 */
13164 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013165 if (visibility != mSystemUiVisibility) {
13166 mSystemUiVisibility = visibility;
13167 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13168 mParent.recomputeViewAttributes(this);
13169 }
Joe Onorato664644d2011-01-23 17:53:23 -080013170 }
13171 }
13172
13173 /**
13174 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013175 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13176 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013177 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013178 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013179 return mSystemUiVisibility;
13180 }
13181
Scott Mainec6331b2011-05-24 16:55:56 -070013182 /**
13183 * Set a listener to receive callbacks when the visibility of the system bar changes.
13184 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13185 */
Joe Onorato664644d2011-01-23 17:53:23 -080013186 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013187 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013188 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13189 mParent.recomputeViewAttributes(this);
13190 }
13191 }
13192
13193 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013194 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13195 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013196 */
13197 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013198 ListenerInfo li = mListenerInfo;
13199 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13200 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013201 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013202 }
13203 }
13204
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013205 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13206 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13207 if (val != mSystemUiVisibility) {
13208 setSystemUiVisibility(val);
13209 }
13210 }
13211
Joe Onorato664644d2011-01-23 17:53:23 -080013212 /**
Joe Malin32736f02011-01-19 16:14:20 -080013213 * Creates an image that the system displays during the drag and drop
13214 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13215 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13216 * appearance as the given View. The default also positions the center of the drag shadow
13217 * directly under the touch point. If no View is provided (the constructor with no parameters
13218 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13219 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13220 * default is an invisible drag shadow.
13221 * <p>
13222 * You are not required to use the View you provide to the constructor as the basis of the
13223 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13224 * anything you want as the drag shadow.
13225 * </p>
13226 * <p>
13227 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13228 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13229 * size and position of the drag shadow. It uses this data to construct a
13230 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13231 * so that your application can draw the shadow image in the Canvas.
13232 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013233 *
13234 * <div class="special reference">
13235 * <h3>Developer Guides</h3>
13236 * <p>For a guide to implementing drag and drop features, read the
13237 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13238 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013239 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013240 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013241 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013242
13243 /**
Joe Malin32736f02011-01-19 16:14:20 -080013244 * Constructs a shadow image builder based on a View. By default, the resulting drag
13245 * shadow will have the same appearance and dimensions as the View, with the touch point
13246 * over the center of the View.
13247 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013248 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013249 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013250 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013251 }
13252
Christopher Tate17ed60c2011-01-18 12:50:26 -080013253 /**
13254 * Construct a shadow builder object with no associated View. This
13255 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13256 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13257 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013258 * reference to any View object. If they are not overridden, then the result is an
13259 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013260 */
13261 public DragShadowBuilder() {
13262 mView = new WeakReference<View>(null);
13263 }
13264
13265 /**
13266 * Returns the View object that had been passed to the
13267 * {@link #View.DragShadowBuilder(View)}
13268 * constructor. If that View parameter was {@code null} or if the
13269 * {@link #View.DragShadowBuilder()}
13270 * constructor was used to instantiate the builder object, this method will return
13271 * null.
13272 *
13273 * @return The View object associate with this builder object.
13274 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013275 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013276 final public View getView() {
13277 return mView.get();
13278 }
13279
Christopher Tate2c095f32010-10-04 14:13:40 -070013280 /**
Joe Malin32736f02011-01-19 16:14:20 -080013281 * Provides the metrics for the shadow image. These include the dimensions of
13282 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013283 * be centered under the touch location while dragging.
13284 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013285 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013286 * same as the dimensions of the View itself and centers the shadow under
13287 * the touch point.
13288 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013289 *
Joe Malin32736f02011-01-19 16:14:20 -080013290 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13291 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13292 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13293 * image.
13294 *
13295 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13296 * shadow image that should be underneath the touch point during the drag and drop
13297 * operation. Your application must set {@link android.graphics.Point#x} to the
13298 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013299 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013300 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013301 final View view = mView.get();
13302 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013303 shadowSize.set(view.getWidth(), view.getHeight());
13304 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013305 } else {
13306 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13307 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013308 }
13309
13310 /**
Joe Malin32736f02011-01-19 16:14:20 -080013311 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13312 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013313 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013314 *
Joe Malin32736f02011-01-19 16:14:20 -080013315 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013316 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013317 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013318 final View view = mView.get();
13319 if (view != null) {
13320 view.draw(canvas);
13321 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013322 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013323 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013324 }
13325 }
13326
13327 /**
Joe Malin32736f02011-01-19 16:14:20 -080013328 * Starts a drag and drop operation. When your application calls this method, it passes a
13329 * {@link android.view.View.DragShadowBuilder} object to the system. The
13330 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13331 * to get metrics for the drag shadow, and then calls the object's
13332 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13333 * <p>
13334 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13335 * drag events to all the View objects in your application that are currently visible. It does
13336 * this either by calling the View object's drag listener (an implementation of
13337 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13338 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13339 * Both are passed a {@link android.view.DragEvent} object that has a
13340 * {@link android.view.DragEvent#getAction()} value of
13341 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13342 * </p>
13343 * <p>
13344 * Your application can invoke startDrag() on any attached View object. The View object does not
13345 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13346 * be related to the View the user selected for dragging.
13347 * </p>
13348 * @param data A {@link android.content.ClipData} object pointing to the data to be
13349 * transferred by the drag and drop operation.
13350 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13351 * drag shadow.
13352 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13353 * drop operation. This Object is put into every DragEvent object sent by the system during the
13354 * current drag.
13355 * <p>
13356 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13357 * to the target Views. For example, it can contain flags that differentiate between a
13358 * a copy operation and a move operation.
13359 * </p>
13360 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13361 * so the parameter should be set to 0.
13362 * @return {@code true} if the method completes successfully, or
13363 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13364 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013365 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013366 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013367 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013368 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013369 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013370 }
13371 boolean okay = false;
13372
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013373 Point shadowSize = new Point();
13374 Point shadowTouchPoint = new Point();
13375 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013376
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013377 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13378 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13379 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013380 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013381
Chris Tatea32dcf72010-10-14 12:13:50 -070013382 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013383 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13384 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013385 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013386 Surface surface = new Surface();
13387 try {
13388 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013389 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013390 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013391 + " surface=" + surface);
13392 if (token != null) {
13393 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013394 try {
Chris Tate6b391282010-10-14 15:48:59 -070013395 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013396 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013397 } finally {
13398 surface.unlockCanvasAndPost(canvas);
13399 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013400
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013401 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013402
13403 // Cache the local state object for delivery with DragEvents
13404 root.setLocalDragState(myLocalState);
13405
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013406 // repurpose 'shadowSize' for the last touch point
13407 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013408
Christopher Tatea53146c2010-09-07 11:57:52 -070013409 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013410 shadowSize.x, shadowSize.y,
13411 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013412 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013413
13414 // Off and running! Release our local surface instance; the drag
13415 // shadow surface is now managed by the system process.
13416 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013417 }
13418 } catch (Exception e) {
13419 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13420 surface.destroy();
13421 }
13422
13423 return okay;
13424 }
13425
Christopher Tatea53146c2010-09-07 11:57:52 -070013426 /**
Joe Malin32736f02011-01-19 16:14:20 -080013427 * Handles drag events sent by the system following a call to
13428 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13429 *<p>
13430 * When the system calls this method, it passes a
13431 * {@link android.view.DragEvent} object. A call to
13432 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13433 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13434 * operation.
13435 * @param event The {@link android.view.DragEvent} sent by the system.
13436 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13437 * in DragEvent, indicating the type of drag event represented by this object.
13438 * @return {@code true} if the method was successful, otherwise {@code false}.
13439 * <p>
13440 * The method should return {@code true} in response to an action type of
13441 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13442 * operation.
13443 * </p>
13444 * <p>
13445 * The method should also return {@code true} in response to an action type of
13446 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13447 * {@code false} if it didn't.
13448 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013449 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013450 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013451 return false;
13452 }
13453
13454 /**
Joe Malin32736f02011-01-19 16:14:20 -080013455 * Detects if this View is enabled and has a drag event listener.
13456 * If both are true, then it calls the drag event listener with the
13457 * {@link android.view.DragEvent} it received. If the drag event listener returns
13458 * {@code true}, then dispatchDragEvent() returns {@code true}.
13459 * <p>
13460 * For all other cases, the method calls the
13461 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13462 * method and returns its result.
13463 * </p>
13464 * <p>
13465 * This ensures that a drag event is always consumed, even if the View does not have a drag
13466 * event listener. However, if the View has a listener and the listener returns true, then
13467 * onDragEvent() is not called.
13468 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013469 */
13470 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013471 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013472 ListenerInfo li = mListenerInfo;
13473 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13474 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013475 return true;
13476 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013477 return onDragEvent(event);
13478 }
13479
Christopher Tate3d4bf172011-03-28 16:16:46 -070013480 boolean canAcceptDrag() {
13481 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13482 }
13483
Christopher Tatea53146c2010-09-07 11:57:52 -070013484 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013485 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13486 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013487 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013488 */
13489 public void onCloseSystemDialogs(String reason) {
13490 }
Joe Malin32736f02011-01-19 16:14:20 -080013491
Dianne Hackbornffa42482009-09-23 22:20:11 -070013492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013494 * update a Region being computed for
13495 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013496 * that any non-transparent parts of the Drawable are removed from the
13497 * given transparent region.
13498 *
13499 * @param dr The Drawable whose transparency is to be applied to the region.
13500 * @param region A Region holding the current transparency information,
13501 * where any parts of the region that are set are considered to be
13502 * transparent. On return, this region will be modified to have the
13503 * transparency information reduced by the corresponding parts of the
13504 * Drawable that are not transparent.
13505 * {@hide}
13506 */
13507 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13508 if (DBG) {
13509 Log.i("View", "Getting transparent region for: " + this);
13510 }
13511 final Region r = dr.getTransparentRegion();
13512 final Rect db = dr.getBounds();
13513 final AttachInfo attachInfo = mAttachInfo;
13514 if (r != null && attachInfo != null) {
13515 final int w = getRight()-getLeft();
13516 final int h = getBottom()-getTop();
13517 if (db.left > 0) {
13518 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13519 r.op(0, 0, db.left, h, Region.Op.UNION);
13520 }
13521 if (db.right < w) {
13522 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13523 r.op(db.right, 0, w, h, Region.Op.UNION);
13524 }
13525 if (db.top > 0) {
13526 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13527 r.op(0, 0, w, db.top, Region.Op.UNION);
13528 }
13529 if (db.bottom < h) {
13530 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13531 r.op(0, db.bottom, w, h, Region.Op.UNION);
13532 }
13533 final int[] location = attachInfo.mTransparentLocation;
13534 getLocationInWindow(location);
13535 r.translate(location[0], location[1]);
13536 region.op(r, Region.Op.INTERSECT);
13537 } else {
13538 region.op(db, Region.Op.DIFFERENCE);
13539 }
13540 }
13541
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013542 private void checkForLongClick(int delayOffset) {
13543 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13544 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013545
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013546 if (mPendingCheckForLongPress == null) {
13547 mPendingCheckForLongPress = new CheckForLongPress();
13548 }
13549 mPendingCheckForLongPress.rememberWindowAttachCount();
13550 postDelayed(mPendingCheckForLongPress,
13551 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013553 }
13554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013555 /**
13556 * Inflate a view from an XML resource. This convenience method wraps the {@link
13557 * LayoutInflater} class, which provides a full range of options for view inflation.
13558 *
13559 * @param context The Context object for your activity or application.
13560 * @param resource The resource ID to inflate
13561 * @param root A view group that will be the parent. Used to properly inflate the
13562 * layout_* parameters.
13563 * @see LayoutInflater
13564 */
13565 public static View inflate(Context context, int resource, ViewGroup root) {
13566 LayoutInflater factory = LayoutInflater.from(context);
13567 return factory.inflate(resource, root);
13568 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013570 /**
Adam Powell637d3372010-08-25 14:37:03 -070013571 * Scroll the view with standard behavior for scrolling beyond the normal
13572 * content boundaries. Views that call this method should override
13573 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13574 * results of an over-scroll operation.
13575 *
13576 * Views can use this method to handle any touch or fling-based scrolling.
13577 *
13578 * @param deltaX Change in X in pixels
13579 * @param deltaY Change in Y in pixels
13580 * @param scrollX Current X scroll value in pixels before applying deltaX
13581 * @param scrollY Current Y scroll value in pixels before applying deltaY
13582 * @param scrollRangeX Maximum content scroll range along the X axis
13583 * @param scrollRangeY Maximum content scroll range along the Y axis
13584 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13585 * along the X axis.
13586 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13587 * along the Y axis.
13588 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13589 * @return true if scrolling was clamped to an over-scroll boundary along either
13590 * axis, false otherwise.
13591 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013592 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013593 protected boolean overScrollBy(int deltaX, int deltaY,
13594 int scrollX, int scrollY,
13595 int scrollRangeX, int scrollRangeY,
13596 int maxOverScrollX, int maxOverScrollY,
13597 boolean isTouchEvent) {
13598 final int overScrollMode = mOverScrollMode;
13599 final boolean canScrollHorizontal =
13600 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13601 final boolean canScrollVertical =
13602 computeVerticalScrollRange() > computeVerticalScrollExtent();
13603 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13604 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13605 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13606 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13607
13608 int newScrollX = scrollX + deltaX;
13609 if (!overScrollHorizontal) {
13610 maxOverScrollX = 0;
13611 }
13612
13613 int newScrollY = scrollY + deltaY;
13614 if (!overScrollVertical) {
13615 maxOverScrollY = 0;
13616 }
13617
13618 // Clamp values if at the limits and record
13619 final int left = -maxOverScrollX;
13620 final int right = maxOverScrollX + scrollRangeX;
13621 final int top = -maxOverScrollY;
13622 final int bottom = maxOverScrollY + scrollRangeY;
13623
13624 boolean clampedX = false;
13625 if (newScrollX > right) {
13626 newScrollX = right;
13627 clampedX = true;
13628 } else if (newScrollX < left) {
13629 newScrollX = left;
13630 clampedX = true;
13631 }
13632
13633 boolean clampedY = false;
13634 if (newScrollY > bottom) {
13635 newScrollY = bottom;
13636 clampedY = true;
13637 } else if (newScrollY < top) {
13638 newScrollY = top;
13639 clampedY = true;
13640 }
13641
13642 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13643
13644 return clampedX || clampedY;
13645 }
13646
13647 /**
13648 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13649 * respond to the results of an over-scroll operation.
13650 *
13651 * @param scrollX New X scroll value in pixels
13652 * @param scrollY New Y scroll value in pixels
13653 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13654 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13655 */
13656 protected void onOverScrolled(int scrollX, int scrollY,
13657 boolean clampedX, boolean clampedY) {
13658 // Intentionally empty.
13659 }
13660
13661 /**
13662 * Returns the over-scroll mode for this view. The result will be
13663 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13664 * (allow over-scrolling only if the view content is larger than the container),
13665 * or {@link #OVER_SCROLL_NEVER}.
13666 *
13667 * @return This view's over-scroll mode.
13668 */
13669 public int getOverScrollMode() {
13670 return mOverScrollMode;
13671 }
13672
13673 /**
13674 * Set the over-scroll mode for this view. Valid over-scroll modes are
13675 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13676 * (allow over-scrolling only if the view content is larger than the container),
13677 * or {@link #OVER_SCROLL_NEVER}.
13678 *
13679 * Setting the over-scroll mode of a view will have an effect only if the
13680 * view is capable of scrolling.
13681 *
13682 * @param overScrollMode The new over-scroll mode for this view.
13683 */
13684 public void setOverScrollMode(int overScrollMode) {
13685 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13686 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13687 overScrollMode != OVER_SCROLL_NEVER) {
13688 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13689 }
13690 mOverScrollMode = overScrollMode;
13691 }
13692
13693 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013694 * Gets a scale factor that determines the distance the view should scroll
13695 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13696 * @return The vertical scroll scale factor.
13697 * @hide
13698 */
13699 protected float getVerticalScrollFactor() {
13700 if (mVerticalScrollFactor == 0) {
13701 TypedValue outValue = new TypedValue();
13702 if (!mContext.getTheme().resolveAttribute(
13703 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13704 throw new IllegalStateException(
13705 "Expected theme to define listPreferredItemHeight.");
13706 }
13707 mVerticalScrollFactor = outValue.getDimension(
13708 mContext.getResources().getDisplayMetrics());
13709 }
13710 return mVerticalScrollFactor;
13711 }
13712
13713 /**
13714 * Gets a scale factor that determines the distance the view should scroll
13715 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13716 * @return The horizontal scroll scale factor.
13717 * @hide
13718 */
13719 protected float getHorizontalScrollFactor() {
13720 // TODO: Should use something else.
13721 return getVerticalScrollFactor();
13722 }
13723
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013724 /**
13725 * Return the value specifying the text direction or policy that was set with
13726 * {@link #setTextDirection(int)}.
13727 *
13728 * @return the defined text direction. It can be one of:
13729 *
13730 * {@link #TEXT_DIRECTION_INHERIT},
13731 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13732 * {@link #TEXT_DIRECTION_ANY_RTL},
13733 * {@link #TEXT_DIRECTION_LTR},
13734 * {@link #TEXT_DIRECTION_RTL},
13735 *
13736 * @hide
13737 */
13738 public int getTextDirection() {
13739 return mTextDirection;
13740 }
13741
13742 /**
13743 * Set the text direction.
13744 *
13745 * @param textDirection the direction to set. Should be one of:
13746 *
13747 * {@link #TEXT_DIRECTION_INHERIT},
13748 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13749 * {@link #TEXT_DIRECTION_ANY_RTL},
13750 * {@link #TEXT_DIRECTION_LTR},
13751 * {@link #TEXT_DIRECTION_RTL},
13752 *
13753 * @hide
13754 */
13755 public void setTextDirection(int textDirection) {
13756 if (textDirection != mTextDirection) {
13757 mTextDirection = textDirection;
13758 resetResolvedTextDirection();
13759 requestLayout();
13760 }
13761 }
13762
13763 /**
13764 * Return the resolved text direction.
13765 *
13766 * @return the resolved text direction. Return one of:
13767 *
Doug Feltcb3791202011-07-07 11:57:48 -070013768 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13769 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013770 * {@link #TEXT_DIRECTION_LTR},
13771 * {@link #TEXT_DIRECTION_RTL},
13772 *
13773 * @hide
13774 */
13775 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013776 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013777 resolveTextDirection();
13778 }
13779 return mResolvedTextDirection;
13780 }
13781
13782 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013783 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013784 *
13785 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013786 */
13787 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013788 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13789 mResolvedTextDirection = mTextDirection;
13790 return;
13791 }
13792 if (mParent != null && mParent instanceof ViewGroup) {
13793 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13794 return;
13795 }
13796 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013797 }
13798
13799 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013800 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013801 *
13802 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013803 */
13804 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013805 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013806 }
13807
Chet Haaseb39f0512011-05-24 14:36:40 -070013808 //
13809 // Properties
13810 //
13811 /**
13812 * A Property wrapper around the <code>alpha</code> functionality handled by the
13813 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13814 */
Romain Guy02ccac62011-06-24 13:20:23 -070013815 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013816 @Override
13817 public void setValue(View object, float value) {
13818 object.setAlpha(value);
13819 }
13820
13821 @Override
13822 public Float get(View object) {
13823 return object.getAlpha();
13824 }
13825 };
13826
13827 /**
13828 * A Property wrapper around the <code>translationX</code> functionality handled by the
13829 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13830 */
13831 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13832 @Override
13833 public void setValue(View object, float value) {
13834 object.setTranslationX(value);
13835 }
13836
13837 @Override
13838 public Float get(View object) {
13839 return object.getTranslationX();
13840 }
13841 };
13842
13843 /**
13844 * A Property wrapper around the <code>translationY</code> functionality handled by the
13845 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13846 */
13847 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13848 @Override
13849 public void setValue(View object, float value) {
13850 object.setTranslationY(value);
13851 }
13852
13853 @Override
13854 public Float get(View object) {
13855 return object.getTranslationY();
13856 }
13857 };
13858
13859 /**
13860 * A Property wrapper around the <code>x</code> functionality handled by the
13861 * {@link View#setX(float)} and {@link View#getX()} methods.
13862 */
13863 public static Property<View, Float> X = new FloatProperty<View>("x") {
13864 @Override
13865 public void setValue(View object, float value) {
13866 object.setX(value);
13867 }
13868
13869 @Override
13870 public Float get(View object) {
13871 return object.getX();
13872 }
13873 };
13874
13875 /**
13876 * A Property wrapper around the <code>y</code> functionality handled by the
13877 * {@link View#setY(float)} and {@link View#getY()} methods.
13878 */
13879 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13880 @Override
13881 public void setValue(View object, float value) {
13882 object.setY(value);
13883 }
13884
13885 @Override
13886 public Float get(View object) {
13887 return object.getY();
13888 }
13889 };
13890
13891 /**
13892 * A Property wrapper around the <code>rotation</code> functionality handled by the
13893 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13894 */
13895 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13896 @Override
13897 public void setValue(View object, float value) {
13898 object.setRotation(value);
13899 }
13900
13901 @Override
13902 public Float get(View object) {
13903 return object.getRotation();
13904 }
13905 };
13906
13907 /**
13908 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13909 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13910 */
13911 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13912 @Override
13913 public void setValue(View object, float value) {
13914 object.setRotationX(value);
13915 }
13916
13917 @Override
13918 public Float get(View object) {
13919 return object.getRotationX();
13920 }
13921 };
13922
13923 /**
13924 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13925 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13926 */
13927 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13928 @Override
13929 public void setValue(View object, float value) {
13930 object.setRotationY(value);
13931 }
13932
13933 @Override
13934 public Float get(View object) {
13935 return object.getRotationY();
13936 }
13937 };
13938
13939 /**
13940 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13941 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13942 */
13943 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13944 @Override
13945 public void setValue(View object, float value) {
13946 object.setScaleX(value);
13947 }
13948
13949 @Override
13950 public Float get(View object) {
13951 return object.getScaleX();
13952 }
13953 };
13954
13955 /**
13956 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13957 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13958 */
13959 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13960 @Override
13961 public void setValue(View object, float value) {
13962 object.setScaleY(value);
13963 }
13964
13965 @Override
13966 public Float get(View object) {
13967 return object.getScaleY();
13968 }
13969 };
13970
Jeff Brown33bbfd22011-02-24 20:55:35 -080013971 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13973 * Each MeasureSpec represents a requirement for either the width or the height.
13974 * A MeasureSpec is comprised of a size and a mode. There are three possible
13975 * modes:
13976 * <dl>
13977 * <dt>UNSPECIFIED</dt>
13978 * <dd>
13979 * The parent has not imposed any constraint on the child. It can be whatever size
13980 * it wants.
13981 * </dd>
13982 *
13983 * <dt>EXACTLY</dt>
13984 * <dd>
13985 * The parent has determined an exact size for the child. The child is going to be
13986 * given those bounds regardless of how big it wants to be.
13987 * </dd>
13988 *
13989 * <dt>AT_MOST</dt>
13990 * <dd>
13991 * The child can be as large as it wants up to the specified size.
13992 * </dd>
13993 * </dl>
13994 *
13995 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13996 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13997 */
13998 public static class MeasureSpec {
13999 private static final int MODE_SHIFT = 30;
14000 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14001
14002 /**
14003 * Measure specification mode: The parent has not imposed any constraint
14004 * on the child. It can be whatever size it wants.
14005 */
14006 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14007
14008 /**
14009 * Measure specification mode: The parent has determined an exact size
14010 * for the child. The child is going to be given those bounds regardless
14011 * of how big it wants to be.
14012 */
14013 public static final int EXACTLY = 1 << MODE_SHIFT;
14014
14015 /**
14016 * Measure specification mode: The child can be as large as it wants up
14017 * to the specified size.
14018 */
14019 public static final int AT_MOST = 2 << MODE_SHIFT;
14020
14021 /**
14022 * Creates a measure specification based on the supplied size and mode.
14023 *
14024 * The mode must always be one of the following:
14025 * <ul>
14026 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14027 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14028 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14029 * </ul>
14030 *
14031 * @param size the size of the measure specification
14032 * @param mode the mode of the measure specification
14033 * @return the measure specification based on size and mode
14034 */
14035 public static int makeMeasureSpec(int size, int mode) {
14036 return size + mode;
14037 }
14038
14039 /**
14040 * Extracts the mode from the supplied measure specification.
14041 *
14042 * @param measureSpec the measure specification to extract the mode from
14043 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14044 * {@link android.view.View.MeasureSpec#AT_MOST} or
14045 * {@link android.view.View.MeasureSpec#EXACTLY}
14046 */
14047 public static int getMode(int measureSpec) {
14048 return (measureSpec & MODE_MASK);
14049 }
14050
14051 /**
14052 * Extracts the size from the supplied measure specification.
14053 *
14054 * @param measureSpec the measure specification to extract the size from
14055 * @return the size in pixels defined in the supplied measure specification
14056 */
14057 public static int getSize(int measureSpec) {
14058 return (measureSpec & ~MODE_MASK);
14059 }
14060
14061 /**
14062 * Returns a String representation of the specified measure
14063 * specification.
14064 *
14065 * @param measureSpec the measure specification to convert to a String
14066 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14067 */
14068 public static String toString(int measureSpec) {
14069 int mode = getMode(measureSpec);
14070 int size = getSize(measureSpec);
14071
14072 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14073
14074 if (mode == UNSPECIFIED)
14075 sb.append("UNSPECIFIED ");
14076 else if (mode == EXACTLY)
14077 sb.append("EXACTLY ");
14078 else if (mode == AT_MOST)
14079 sb.append("AT_MOST ");
14080 else
14081 sb.append(mode).append(" ");
14082
14083 sb.append(size);
14084 return sb.toString();
14085 }
14086 }
14087
14088 class CheckForLongPress implements Runnable {
14089
14090 private int mOriginalWindowAttachCount;
14091
14092 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014093 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014094 && mOriginalWindowAttachCount == mWindowAttachCount) {
14095 if (performLongClick()) {
14096 mHasPerformedLongPress = true;
14097 }
14098 }
14099 }
14100
14101 public void rememberWindowAttachCount() {
14102 mOriginalWindowAttachCount = mWindowAttachCount;
14103 }
14104 }
Joe Malin32736f02011-01-19 16:14:20 -080014105
Adam Powelle14579b2009-12-16 18:39:52 -080014106 private final class CheckForTap implements Runnable {
14107 public void run() {
14108 mPrivateFlags &= ~PREPRESSED;
14109 mPrivateFlags |= PRESSED;
14110 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014111 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014112 }
14113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114
Adam Powella35d7682010-03-12 14:48:13 -080014115 private final class PerformClick implements Runnable {
14116 public void run() {
14117 performClick();
14118 }
14119 }
14120
Dianne Hackborn63042d62011-01-26 18:56:29 -080014121 /** @hide */
14122 public void hackTurnOffWindowResizeAnim(boolean off) {
14123 mAttachInfo.mTurnOffWindowResizeAnim = off;
14124 }
Joe Malin32736f02011-01-19 16:14:20 -080014125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014126 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014127 * This method returns a ViewPropertyAnimator object, which can be used to animate
14128 * specific properties on this View.
14129 *
14130 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14131 */
14132 public ViewPropertyAnimator animate() {
14133 if (mAnimator == null) {
14134 mAnimator = new ViewPropertyAnimator(this);
14135 }
14136 return mAnimator;
14137 }
14138
14139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014140 * Interface definition for a callback to be invoked when a key event is
14141 * dispatched to this view. The callback will be invoked before the key
14142 * event is given to the view.
14143 */
14144 public interface OnKeyListener {
14145 /**
14146 * Called when a key is dispatched to a view. This allows listeners to
14147 * get a chance to respond before the target view.
14148 *
14149 * @param v The view the key has been dispatched to.
14150 * @param keyCode The code for the physical key that was pressed
14151 * @param event The KeyEvent object containing full information about
14152 * the event.
14153 * @return True if the listener has consumed the event, false otherwise.
14154 */
14155 boolean onKey(View v, int keyCode, KeyEvent event);
14156 }
14157
14158 /**
14159 * Interface definition for a callback to be invoked when a touch event is
14160 * dispatched to this view. The callback will be invoked before the touch
14161 * event is given to the view.
14162 */
14163 public interface OnTouchListener {
14164 /**
14165 * Called when a touch event is dispatched to a view. This allows listeners to
14166 * get a chance to respond before the target view.
14167 *
14168 * @param v The view the touch event has been dispatched to.
14169 * @param event The MotionEvent object containing full information about
14170 * the event.
14171 * @return True if the listener has consumed the event, false otherwise.
14172 */
14173 boolean onTouch(View v, MotionEvent event);
14174 }
14175
14176 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014177 * Interface definition for a callback to be invoked when a hover event is
14178 * dispatched to this view. The callback will be invoked before the hover
14179 * event is given to the view.
14180 */
14181 public interface OnHoverListener {
14182 /**
14183 * Called when a hover event is dispatched to a view. This allows listeners to
14184 * get a chance to respond before the target view.
14185 *
14186 * @param v The view the hover event has been dispatched to.
14187 * @param event The MotionEvent object containing full information about
14188 * the event.
14189 * @return True if the listener has consumed the event, false otherwise.
14190 */
14191 boolean onHover(View v, MotionEvent event);
14192 }
14193
14194 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014195 * Interface definition for a callback to be invoked when a generic motion event is
14196 * dispatched to this view. The callback will be invoked before the generic motion
14197 * event is given to the view.
14198 */
14199 public interface OnGenericMotionListener {
14200 /**
14201 * Called when a generic motion event is dispatched to a view. This allows listeners to
14202 * get a chance to respond before the target view.
14203 *
14204 * @param v The view the generic motion event has been dispatched to.
14205 * @param event The MotionEvent object containing full information about
14206 * the event.
14207 * @return True if the listener has consumed the event, false otherwise.
14208 */
14209 boolean onGenericMotion(View v, MotionEvent event);
14210 }
14211
14212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014213 * Interface definition for a callback to be invoked when a view has been clicked and held.
14214 */
14215 public interface OnLongClickListener {
14216 /**
14217 * Called when a view has been clicked and held.
14218 *
14219 * @param v The view that was clicked and held.
14220 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014221 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014222 */
14223 boolean onLongClick(View v);
14224 }
14225
14226 /**
Chris Tate32affef2010-10-18 15:29:21 -070014227 * Interface definition for a callback to be invoked when a drag is being dispatched
14228 * to this view. The callback will be invoked before the hosting view's own
14229 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14230 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014231 *
14232 * <div class="special reference">
14233 * <h3>Developer Guides</h3>
14234 * <p>For a guide to implementing drag and drop features, read the
14235 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14236 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014237 */
14238 public interface OnDragListener {
14239 /**
14240 * Called when a drag event is dispatched to a view. This allows listeners
14241 * to get a chance to override base View behavior.
14242 *
Joe Malin32736f02011-01-19 16:14:20 -080014243 * @param v The View that received the drag event.
14244 * @param event The {@link android.view.DragEvent} object for the drag event.
14245 * @return {@code true} if the drag event was handled successfully, or {@code false}
14246 * if the drag event was not handled. Note that {@code false} will trigger the View
14247 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014248 */
14249 boolean onDrag(View v, DragEvent event);
14250 }
14251
14252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014253 * Interface definition for a callback to be invoked when the focus state of
14254 * a view changed.
14255 */
14256 public interface OnFocusChangeListener {
14257 /**
14258 * Called when the focus state of a view has changed.
14259 *
14260 * @param v The view whose state has changed.
14261 * @param hasFocus The new focus state of v.
14262 */
14263 void onFocusChange(View v, boolean hasFocus);
14264 }
14265
14266 /**
14267 * Interface definition for a callback to be invoked when a view is clicked.
14268 */
14269 public interface OnClickListener {
14270 /**
14271 * Called when a view has been clicked.
14272 *
14273 * @param v The view that was clicked.
14274 */
14275 void onClick(View v);
14276 }
14277
14278 /**
14279 * Interface definition for a callback to be invoked when the context menu
14280 * for this view is being built.
14281 */
14282 public interface OnCreateContextMenuListener {
14283 /**
14284 * Called when the context menu for this view is being built. It is not
14285 * safe to hold onto the menu after this method returns.
14286 *
14287 * @param menu The context menu that is being built
14288 * @param v The view for which the context menu is being built
14289 * @param menuInfo Extra information about the item for which the
14290 * context menu should be shown. This information will vary
14291 * depending on the class of v.
14292 */
14293 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14294 }
14295
Joe Onorato664644d2011-01-23 17:53:23 -080014296 /**
14297 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014298 * visibility. This reports <strong>global</strong> changes to the system UI
14299 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014300 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014301 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014302 */
14303 public interface OnSystemUiVisibilityChangeListener {
14304 /**
14305 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014306 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014307 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014308 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014309 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14310 * <strong>global</strong> state of the UI visibility flags, not what your
14311 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014312 */
14313 public void onSystemUiVisibilityChange(int visibility);
14314 }
14315
Adam Powell4afd62b2011-02-18 15:02:18 -080014316 /**
14317 * Interface definition for a callback to be invoked when this view is attached
14318 * or detached from its window.
14319 */
14320 public interface OnAttachStateChangeListener {
14321 /**
14322 * Called when the view is attached to a window.
14323 * @param v The view that was attached
14324 */
14325 public void onViewAttachedToWindow(View v);
14326 /**
14327 * Called when the view is detached from a window.
14328 * @param v The view that was detached
14329 */
14330 public void onViewDetachedFromWindow(View v);
14331 }
14332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014333 private final class UnsetPressedState implements Runnable {
14334 public void run() {
14335 setPressed(false);
14336 }
14337 }
14338
14339 /**
14340 * Base class for derived classes that want to save and restore their own
14341 * state in {@link android.view.View#onSaveInstanceState()}.
14342 */
14343 public static class BaseSavedState extends AbsSavedState {
14344 /**
14345 * Constructor used when reading from a parcel. Reads the state of the superclass.
14346 *
14347 * @param source
14348 */
14349 public BaseSavedState(Parcel source) {
14350 super(source);
14351 }
14352
14353 /**
14354 * Constructor called by derived classes when creating their SavedState objects
14355 *
14356 * @param superState The state of the superclass of this view
14357 */
14358 public BaseSavedState(Parcelable superState) {
14359 super(superState);
14360 }
14361
14362 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14363 new Parcelable.Creator<BaseSavedState>() {
14364 public BaseSavedState createFromParcel(Parcel in) {
14365 return new BaseSavedState(in);
14366 }
14367
14368 public BaseSavedState[] newArray(int size) {
14369 return new BaseSavedState[size];
14370 }
14371 };
14372 }
14373
14374 /**
14375 * A set of information given to a view when it is attached to its parent
14376 * window.
14377 */
14378 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 interface Callbacks {
14380 void playSoundEffect(int effectId);
14381 boolean performHapticFeedback(int effectId, boolean always);
14382 }
14383
14384 /**
14385 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14386 * to a Handler. This class contains the target (View) to invalidate and
14387 * the coordinates of the dirty rectangle.
14388 *
14389 * For performance purposes, this class also implements a pool of up to
14390 * POOL_LIMIT objects that get reused. This reduces memory allocations
14391 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014392 */
Romain Guyd928d682009-03-31 17:52:16 -070014393 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014394 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014395 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14396 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014397 public InvalidateInfo newInstance() {
14398 return new InvalidateInfo();
14399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014400
Romain Guyd928d682009-03-31 17:52:16 -070014401 public void onAcquired(InvalidateInfo element) {
14402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014403
Romain Guyd928d682009-03-31 17:52:16 -070014404 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014405 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014406 }
14407 }, POOL_LIMIT)
14408 );
14409
14410 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014411 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014412
14413 View target;
14414
14415 int left;
14416 int top;
14417 int right;
14418 int bottom;
14419
Romain Guyd928d682009-03-31 17:52:16 -070014420 public void setNextPoolable(InvalidateInfo element) {
14421 mNext = element;
14422 }
14423
14424 public InvalidateInfo getNextPoolable() {
14425 return mNext;
14426 }
14427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014428 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014429 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014430 }
14431
14432 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014433 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014434 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014435
14436 public boolean isPooled() {
14437 return mIsPooled;
14438 }
14439
14440 public void setPooled(boolean isPooled) {
14441 mIsPooled = isPooled;
14442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014443 }
14444
14445 final IWindowSession mSession;
14446
14447 final IWindow mWindow;
14448
14449 final IBinder mWindowToken;
14450
14451 final Callbacks mRootCallbacks;
14452
Romain Guy59a12ca2011-06-09 17:48:21 -070014453 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014455 /**
14456 * The top view of the hierarchy.
14457 */
14458 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014460 IBinder mPanelParentWindowToken;
14461 Surface mSurface;
14462
Romain Guyb051e892010-09-28 19:09:36 -070014463 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014464 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014465 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014468 * Scale factor used by the compatibility mode
14469 */
14470 float mApplicationScale;
14471
14472 /**
14473 * Indicates whether the application is in compatibility mode
14474 */
14475 boolean mScalingRequired;
14476
14477 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014478 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014479 */
14480 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014481
Dianne Hackborn63042d62011-01-26 18:56:29 -080014482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014483 * Left position of this view's window
14484 */
14485 int mWindowLeft;
14486
14487 /**
14488 * Top position of this view's window
14489 */
14490 int mWindowTop;
14491
14492 /**
Adam Powell26153a32010-11-08 15:22:27 -080014493 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014494 */
Adam Powell26153a32010-11-08 15:22:27 -080014495 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014496
14497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014498 * For windows that are full-screen but using insets to layout inside
14499 * of the screen decorations, these are the current insets for the
14500 * content of the window.
14501 */
14502 final Rect mContentInsets = new Rect();
14503
14504 /**
14505 * For windows that are full-screen but using insets to layout inside
14506 * of the screen decorations, these are the current insets for the
14507 * actual visible parts of the window.
14508 */
14509 final Rect mVisibleInsets = new Rect();
14510
14511 /**
14512 * The internal insets given by this window. This value is
14513 * supplied by the client (through
14514 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14515 * be given to the window manager when changed to be used in laying
14516 * out windows behind it.
14517 */
14518 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14519 = new ViewTreeObserver.InternalInsetsInfo();
14520
14521 /**
14522 * All views in the window's hierarchy that serve as scroll containers,
14523 * used to determine if the window can be resized or must be panned
14524 * to adjust for a soft input area.
14525 */
14526 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14527
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014528 final KeyEvent.DispatcherState mKeyDispatchState
14529 = new KeyEvent.DispatcherState();
14530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014531 /**
14532 * Indicates whether the view's window currently has the focus.
14533 */
14534 boolean mHasWindowFocus;
14535
14536 /**
14537 * The current visibility of the window.
14538 */
14539 int mWindowVisibility;
14540
14541 /**
14542 * Indicates the time at which drawing started to occur.
14543 */
14544 long mDrawingTime;
14545
14546 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014547 * Indicates whether or not ignoring the DIRTY_MASK flags.
14548 */
14549 boolean mIgnoreDirtyState;
14550
14551 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014552 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14553 * to avoid clearing that flag prematurely.
14554 */
14555 boolean mSetIgnoreDirtyState = false;
14556
14557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014558 * Indicates whether the view's window is currently in touch mode.
14559 */
14560 boolean mInTouchMode;
14561
14562 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014563 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014564 * the next time it performs a traversal
14565 */
14566 boolean mRecomputeGlobalAttributes;
14567
14568 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014569 * Always report new attributes at next traversal.
14570 */
14571 boolean mForceReportNewAttributes;
14572
14573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014574 * Set during a traveral if any views want to keep the screen on.
14575 */
14576 boolean mKeepScreenOn;
14577
14578 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014579 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14580 */
14581 int mSystemUiVisibility;
14582
14583 /**
14584 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14585 * attached.
14586 */
14587 boolean mHasSystemUiListeners;
14588
14589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 * Set if the visibility of any views has changed.
14591 */
14592 boolean mViewVisibilityChanged;
14593
14594 /**
14595 * Set to true if a view has been scrolled.
14596 */
14597 boolean mViewScrollChanged;
14598
14599 /**
14600 * Global to the view hierarchy used as a temporary for dealing with
14601 * x/y points in the transparent region computations.
14602 */
14603 final int[] mTransparentLocation = new int[2];
14604
14605 /**
14606 * Global to the view hierarchy used as a temporary for dealing with
14607 * x/y points in the ViewGroup.invalidateChild implementation.
14608 */
14609 final int[] mInvalidateChildLocation = new int[2];
14610
Chet Haasec3aa3612010-06-17 08:50:37 -070014611
14612 /**
14613 * Global to the view hierarchy used as a temporary for dealing with
14614 * x/y location when view is transformed.
14615 */
14616 final float[] mTmpTransformLocation = new float[2];
14617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014618 /**
14619 * The view tree observer used to dispatch global events like
14620 * layout, pre-draw, touch mode change, etc.
14621 */
14622 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14623
14624 /**
14625 * A Canvas used by the view hierarchy to perform bitmap caching.
14626 */
14627 Canvas mCanvas;
14628
14629 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014630 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014631 * handler can be used to pump events in the UI events queue.
14632 */
14633 final Handler mHandler;
14634
14635 /**
14636 * Identifier for messages requesting the view to be invalidated.
14637 * Such messages should be sent to {@link #mHandler}.
14638 */
14639 static final int INVALIDATE_MSG = 0x1;
14640
14641 /**
14642 * Identifier for messages requesting the view to invalidate a region.
14643 * Such messages should be sent to {@link #mHandler}.
14644 */
14645 static final int INVALIDATE_RECT_MSG = 0x2;
14646
14647 /**
14648 * Temporary for use in computing invalidate rectangles while
14649 * calling up the hierarchy.
14650 */
14651 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014652
14653 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014654 * Temporary for use in computing hit areas with transformed views
14655 */
14656 final RectF mTmpTransformRect = new RectF();
14657
14658 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014659 * Temporary list for use in collecting focusable descendents of a view.
14660 */
14661 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014664 * The id of the window for accessibility purposes.
14665 */
14666 int mAccessibilityWindowId = View.NO_ID;
14667
14668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014669 * Creates a new set of attachment information with the specified
14670 * events handler and thread.
14671 *
14672 * @param handler the events handler the view must use
14673 */
14674 AttachInfo(IWindowSession session, IWindow window,
14675 Handler handler, Callbacks effectPlayer) {
14676 mSession = session;
14677 mWindow = window;
14678 mWindowToken = window.asBinder();
14679 mHandler = handler;
14680 mRootCallbacks = effectPlayer;
14681 }
14682 }
14683
14684 /**
14685 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14686 * is supported. This avoids keeping too many unused fields in most
14687 * instances of View.</p>
14688 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014689 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014690
Mike Cleronf116bf82009-09-27 19:14:12 -070014691 /**
14692 * Scrollbars are not visible
14693 */
14694 public static final int OFF = 0;
14695
14696 /**
14697 * Scrollbars are visible
14698 */
14699 public static final int ON = 1;
14700
14701 /**
14702 * Scrollbars are fading away
14703 */
14704 public static final int FADING = 2;
14705
14706 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014708 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014709 public int scrollBarDefaultDelayBeforeFade;
14710 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014711
14712 public int scrollBarSize;
14713 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014714 public float[] interpolatorValues;
14715 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014716
14717 public final Paint paint;
14718 public final Matrix matrix;
14719 public Shader shader;
14720
Mike Cleronf116bf82009-09-27 19:14:12 -070014721 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14722
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014723 private static final float[] OPAQUE = { 255 };
14724 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014725
Mike Cleronf116bf82009-09-27 19:14:12 -070014726 /**
14727 * When fading should start. This time moves into the future every time
14728 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14729 */
14730 public long fadeStartTime;
14731
14732
14733 /**
14734 * The current state of the scrollbars: ON, OFF, or FADING
14735 */
14736 public int state = OFF;
14737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 private int mLastColor;
14739
Mike Cleronf116bf82009-09-27 19:14:12 -070014740 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014741 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14742 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014743 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14744 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014745
14746 paint = new Paint();
14747 matrix = new Matrix();
14748 // use use a height of 1, and then wack the matrix each time we
14749 // actually use it.
14750 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014752 paint.setShader(shader);
14753 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014754 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014755 }
Romain Guy8506ab42009-06-11 17:35:47 -070014756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014757 public void setFadeColor(int color) {
14758 if (color != 0 && color != mLastColor) {
14759 mLastColor = color;
14760 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014761
Romain Guye55e1a72009-08-27 10:42:26 -070014762 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14763 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014765 paint.setShader(shader);
14766 // Restore the default transfer mode (src_over)
14767 paint.setXfermode(null);
14768 }
14769 }
Joe Malin32736f02011-01-19 16:14:20 -080014770
Mike Cleronf116bf82009-09-27 19:14:12 -070014771 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014772 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014773 if (now >= fadeStartTime) {
14774
14775 // the animation fades the scrollbars out by changing
14776 // the opacity (alpha) from fully opaque to fully
14777 // transparent
14778 int nextFrame = (int) now;
14779 int framesCount = 0;
14780
14781 Interpolator interpolator = scrollBarInterpolator;
14782
14783 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014784 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014785
14786 // End transparent
14787 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014788 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014789
14790 state = FADING;
14791
14792 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014793 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014794 }
14795 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014796 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014797
Svetoslav Ganova0156172011-06-26 17:55:44 -070014798 /**
14799 * Resuable callback for sending
14800 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14801 */
14802 private class SendViewScrolledAccessibilityEvent implements Runnable {
14803 public volatile boolean mIsPending;
14804
14805 public void run() {
14806 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14807 mIsPending = false;
14808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014809 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014810
14811 /**
14812 * <p>
14813 * This class represents a delegate that can be registered in a {@link View}
14814 * to enhance accessibility support via composition rather via inheritance.
14815 * It is specifically targeted to widget developers that extend basic View
14816 * classes i.e. classes in package android.view, that would like their
14817 * applications to be backwards compatible.
14818 * </p>
14819 * <p>
14820 * A scenario in which a developer would like to use an accessibility delegate
14821 * is overriding a method introduced in a later API version then the minimal API
14822 * version supported by the application. For example, the method
14823 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14824 * in API version 4 when the accessibility APIs were first introduced. If a
14825 * developer would like his application to run on API version 4 devices (assuming
14826 * all other APIs used by the application are version 4 or lower) and take advantage
14827 * of this method, instead of overriding the method which would break the application's
14828 * backwards compatibility, he can override the corresponding method in this
14829 * delegate and register the delegate in the target View if the API version of
14830 * the system is high enough i.e. the API version is same or higher to the API
14831 * version that introduced
14832 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14833 * </p>
14834 * <p>
14835 * Here is an example implementation:
14836 * </p>
14837 * <code><pre><p>
14838 * if (Build.VERSION.SDK_INT >= 14) {
14839 * // If the API version is equal of higher than the version in
14840 * // which onInitializeAccessibilityNodeInfo was introduced we
14841 * // register a delegate with a customized implementation.
14842 * View view = findViewById(R.id.view_id);
14843 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14844 * public void onInitializeAccessibilityNodeInfo(View host,
14845 * AccessibilityNodeInfo info) {
14846 * // Let the default implementation populate the info.
14847 * super.onInitializeAccessibilityNodeInfo(host, info);
14848 * // Set some other information.
14849 * info.setEnabled(host.isEnabled());
14850 * }
14851 * });
14852 * }
14853 * </code></pre></p>
14854 * <p>
14855 * This delegate contains methods that correspond to the accessibility methods
14856 * in View. If a delegate has been specified the implementation in View hands
14857 * off handling to the corresponding method in this delegate. The default
14858 * implementation the delegate methods behaves exactly as the corresponding
14859 * method in View for the case of no accessibility delegate been set. Hence,
14860 * to customize the behavior of a View method, clients can override only the
14861 * corresponding delegate method without altering the behavior of the rest
14862 * accessibility related methods of the host view.
14863 * </p>
14864 */
14865 public static class AccessibilityDelegate {
14866
14867 /**
14868 * Sends an accessibility event of the given type. If accessibility is not
14869 * enabled this method has no effect.
14870 * <p>
14871 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14872 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14873 * been set.
14874 * </p>
14875 *
14876 * @param host The View hosting the delegate.
14877 * @param eventType The type of the event to send.
14878 *
14879 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14880 */
14881 public void sendAccessibilityEvent(View host, int eventType) {
14882 host.sendAccessibilityEventInternal(eventType);
14883 }
14884
14885 /**
14886 * Sends an accessibility event. This method behaves exactly as
14887 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14888 * empty {@link AccessibilityEvent} and does not perform a check whether
14889 * accessibility is enabled.
14890 * <p>
14891 * The default implementation behaves as
14892 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14893 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14894 * the case of no accessibility delegate been set.
14895 * </p>
14896 *
14897 * @param host The View hosting the delegate.
14898 * @param event The event to send.
14899 *
14900 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14901 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14902 */
14903 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14904 host.sendAccessibilityEventUncheckedInternal(event);
14905 }
14906
14907 /**
14908 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14909 * to its children for adding their text content to the event.
14910 * <p>
14911 * The default implementation behaves as
14912 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14913 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14914 * the case of no accessibility delegate been set.
14915 * </p>
14916 *
14917 * @param host The View hosting the delegate.
14918 * @param event The event.
14919 * @return True if the event population was completed.
14920 *
14921 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14922 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14923 */
14924 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14925 return host.dispatchPopulateAccessibilityEventInternal(event);
14926 }
14927
14928 /**
14929 * Gives a chance to the host View to populate the accessibility event with its
14930 * text content.
14931 * <p>
14932 * The default implementation behaves as
14933 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14934 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14935 * the case of no accessibility delegate been set.
14936 * </p>
14937 *
14938 * @param host The View hosting the delegate.
14939 * @param event The accessibility event which to populate.
14940 *
14941 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14942 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14943 */
14944 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14945 host.onPopulateAccessibilityEventInternal(event);
14946 }
14947
14948 /**
14949 * Initializes an {@link AccessibilityEvent} with information about the
14950 * the host View which is the event source.
14951 * <p>
14952 * The default implementation behaves as
14953 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14954 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14955 * the case of no accessibility delegate been set.
14956 * </p>
14957 *
14958 * @param host The View hosting the delegate.
14959 * @param event The event to initialize.
14960 *
14961 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14962 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14963 */
14964 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14965 host.onInitializeAccessibilityEventInternal(event);
14966 }
14967
14968 /**
14969 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14970 * <p>
14971 * The default implementation behaves as
14972 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14973 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14974 * the case of no accessibility delegate been set.
14975 * </p>
14976 *
14977 * @param host The View hosting the delegate.
14978 * @param info The instance to initialize.
14979 *
14980 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14981 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14982 */
14983 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14984 host.onInitializeAccessibilityNodeInfoInternal(info);
14985 }
14986
14987 /**
14988 * Called when a child of the host View has requested sending an
14989 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14990 * to augment the event.
14991 * <p>
14992 * The default implementation behaves as
14993 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14994 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14995 * the case of no accessibility delegate been set.
14996 * </p>
14997 *
14998 * @param host The View hosting the delegate.
14999 * @param child The child which requests sending the event.
15000 * @param event The event to be sent.
15001 * @return True if the event should be sent
15002 *
15003 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15004 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15005 */
15006 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15007 AccessibilityEvent event) {
15008 return host.onRequestSendAccessibilityEventInternal(child, event);
15009 }
15010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015011}