blob: 6b4bbb3b15d2aded9acf412c7b3c4342157552b0 [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 /**
7132 * Utility method to retrieve the inverse of the current mMatrix property.
7133 * We cache the matrix to avoid recalculating it when transform properties
7134 * have not changed.
7135 *
7136 * @return The inverse of the current matrix of this view.
7137 */
Jeff Brown86671742010-09-30 20:00:15 -07007138 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007139 final TransformationInfo info = mTransformationInfo;
7140 if (info != null) {
7141 updateMatrix();
7142 if (info.mInverseMatrixDirty) {
7143 if (info.mInverseMatrix == null) {
7144 info.mInverseMatrix = new Matrix();
7145 }
7146 info.mMatrix.invert(info.mInverseMatrix);
7147 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007148 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007149 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007150 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007151 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 }
7153
7154 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007155 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7156 * views are drawn) from the camera to this view. The camera's distance
7157 * affects 3D transformations, for instance rotations around the X and Y
7158 * axis. If the rotationX or rotationY properties are changed and this view is
7159 * large (more than half the size of the screen), it is recommended to always
7160 * use a camera distance that's greater than the height (X axis rotation) or
7161 * the width (Y axis rotation) of this view.</p>
7162 *
7163 * <p>The distance of the camera from the view plane can have an affect on the
7164 * perspective distortion of the view when it is rotated around the x or y axis.
7165 * For example, a large distance will result in a large viewing angle, and there
7166 * will not be much perspective distortion of the view as it rotates. A short
7167 * distance may cause much more perspective distortion upon rotation, and can
7168 * also result in some drawing artifacts if the rotated view ends up partially
7169 * behind the camera (which is why the recommendation is to use a distance at
7170 * least as far as the size of the view, if the view is to be rotated.)</p>
7171 *
7172 * <p>The distance is expressed in "depth pixels." The default distance depends
7173 * on the screen density. For instance, on a medium density display, the
7174 * default distance is 1280. On a high density display, the default distance
7175 * is 1920.</p>
7176 *
7177 * <p>If you want to specify a distance that leads to visually consistent
7178 * results across various densities, use the following formula:</p>
7179 * <pre>
7180 * float scale = context.getResources().getDisplayMetrics().density;
7181 * view.setCameraDistance(distance * scale);
7182 * </pre>
7183 *
7184 * <p>The density scale factor of a high density display is 1.5,
7185 * and 1920 = 1280 * 1.5.</p>
7186 *
7187 * @param distance The distance in "depth pixels", if negative the opposite
7188 * value is used
7189 *
7190 * @see #setRotationX(float)
7191 * @see #setRotationY(float)
7192 */
7193 public void setCameraDistance(float distance) {
7194 invalidateParentCaches();
7195 invalidate(false);
7196
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007197 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007198 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007199 final TransformationInfo info = mTransformationInfo;
7200 if (info.mCamera == null) {
7201 info.mCamera = new Camera();
7202 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007203 }
7204
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007205 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7206 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007207
7208 invalidate(false);
7209 }
7210
7211 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007212 * The degrees that the view is rotated around the pivot point.
7213 *
Romain Guya5364ee2011-02-24 14:46:04 -08007214 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007215 * @see #getPivotX()
7216 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007217 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007218 * @return The degrees of rotation.
7219 */
7220 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007221 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007222 }
7223
7224 /**
Chet Haase897247b2010-09-09 14:54:47 -07007225 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7226 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007227 *
7228 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007229 *
7230 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007231 * @see #getPivotX()
7232 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007233 * @see #setRotationX(float)
7234 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007235 *
7236 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007237 */
7238 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007239 ensureTransformationInfo();
7240 final TransformationInfo info = mTransformationInfo;
7241 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007242 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007243 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007244 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007245 info.mRotation = rotation;
7246 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007248 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007249 }
7250 }
7251
7252 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007253 * The degrees that the view is rotated around the vertical axis through the pivot point.
7254 *
7255 * @see #getPivotX()
7256 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007257 * @see #setRotationY(float)
7258 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007259 * @return The degrees of Y rotation.
7260 */
7261 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007262 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007263 }
7264
7265 /**
Chet Haase897247b2010-09-09 14:54:47 -07007266 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7267 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7268 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007269 *
7270 * When rotating large views, it is recommended to adjust the camera distance
7271 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007272 *
7273 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007274 *
7275 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007276 * @see #getPivotX()
7277 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007278 * @see #setRotation(float)
7279 * @see #setRotationX(float)
7280 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007281 *
7282 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007283 */
7284 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007285 ensureTransformationInfo();
7286 final TransformationInfo info = mTransformationInfo;
7287 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007288 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007289 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007290 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007291 info.mRotationY = rotationY;
7292 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007293 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007294 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007295 }
7296 }
7297
7298 /**
7299 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7300 *
7301 * @see #getPivotX()
7302 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007303 * @see #setRotationX(float)
7304 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007305 * @return The degrees of X rotation.
7306 */
7307 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007308 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007309 }
7310
7311 /**
Chet Haase897247b2010-09-09 14:54:47 -07007312 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7313 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7314 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007315 *
7316 * When rotating large views, it is recommended to adjust the camera distance
7317 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007318 *
7319 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007320 *
7321 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007322 * @see #getPivotX()
7323 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007324 * @see #setRotation(float)
7325 * @see #setRotationY(float)
7326 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007327 *
7328 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007329 */
7330 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007331 ensureTransformationInfo();
7332 final TransformationInfo info = mTransformationInfo;
7333 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007334 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007335 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007336 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007337 info.mRotationX = rotationX;
7338 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007339 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007340 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007341 }
7342 }
7343
7344 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007345 * The amount that the view is scaled in x around the pivot point, as a proportion of
7346 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7347 *
Joe Onorato93162322010-09-16 15:42:01 -04007348 * <p>By default, this is 1.0f.
7349 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007350 * @see #getPivotX()
7351 * @see #getPivotY()
7352 * @return The scaling factor.
7353 */
7354 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007355 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007356 }
7357
7358 /**
7359 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7360 * the view's unscaled width. A value of 1 means that no scaling is applied.
7361 *
7362 * @param scaleX The scaling factor.
7363 * @see #getPivotX()
7364 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007365 *
7366 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007367 */
7368 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007369 ensureTransformationInfo();
7370 final TransformationInfo info = mTransformationInfo;
7371 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007372 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007373 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007374 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007375 info.mScaleX = scaleX;
7376 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007377 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007378 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007379 }
7380 }
7381
7382 /**
7383 * The amount that the view is scaled in y around the pivot point, as a proportion of
7384 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7385 *
Joe Onorato93162322010-09-16 15:42:01 -04007386 * <p>By default, this is 1.0f.
7387 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007388 * @see #getPivotX()
7389 * @see #getPivotY()
7390 * @return The scaling factor.
7391 */
7392 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007393 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007394 }
7395
7396 /**
7397 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7398 * the view's unscaled width. A value of 1 means that no scaling is applied.
7399 *
7400 * @param scaleY The scaling factor.
7401 * @see #getPivotX()
7402 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007403 *
7404 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007405 */
7406 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007407 ensureTransformationInfo();
7408 final TransformationInfo info = mTransformationInfo;
7409 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007410 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007411 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007412 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007413 info.mScaleY = scaleY;
7414 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007415 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007416 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007417 }
7418 }
7419
7420 /**
7421 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7422 * and {@link #setScaleX(float) scaled}.
7423 *
7424 * @see #getRotation()
7425 * @see #getScaleX()
7426 * @see #getScaleY()
7427 * @see #getPivotY()
7428 * @return The x location of the pivot point.
7429 */
7430 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007431 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007432 }
7433
7434 /**
7435 * Sets the x location of the point around which the view is
7436 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007437 * By default, the pivot point is centered on the object.
7438 * Setting this property disables this behavior and causes the view to use only the
7439 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007440 *
7441 * @param pivotX The x location of the pivot point.
7442 * @see #getRotation()
7443 * @see #getScaleX()
7444 * @see #getScaleY()
7445 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007446 *
7447 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007448 */
7449 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007450 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007451 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007452 final TransformationInfo info = mTransformationInfo;
7453 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007454 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007455 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007456 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007457 info.mPivotX = pivotX;
7458 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007459 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007460 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007461 }
7462 }
7463
7464 /**
7465 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7466 * and {@link #setScaleY(float) scaled}.
7467 *
7468 * @see #getRotation()
7469 * @see #getScaleX()
7470 * @see #getScaleY()
7471 * @see #getPivotY()
7472 * @return The y location of the pivot point.
7473 */
7474 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007475 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007476 }
7477
7478 /**
7479 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007480 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7481 * Setting this property disables this behavior and causes the view to use only the
7482 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007483 *
7484 * @param pivotY The y location of the pivot point.
7485 * @see #getRotation()
7486 * @see #getScaleX()
7487 * @see #getScaleY()
7488 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007489 *
7490 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 */
7492 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007493 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007494 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007495 final TransformationInfo info = mTransformationInfo;
7496 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007497 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007498 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007499 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007500 info.mPivotY = pivotY;
7501 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007503 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 }
7505 }
7506
7507 /**
7508 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7509 * completely transparent and 1 means the view is completely opaque.
7510 *
Joe Onorato93162322010-09-16 15:42:01 -04007511 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007512 * @return The opacity of the view.
7513 */
7514 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007515 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007516 }
7517
7518 /**
Romain Guy171c5922011-01-06 10:04:23 -08007519 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7520 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007521 *
Romain Guy171c5922011-01-06 10:04:23 -08007522 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7523 * responsible for applying the opacity itself. Otherwise, calling this method is
7524 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007525 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007526 *
7527 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007528 *
Joe Malin32736f02011-01-19 16:14:20 -08007529 * @see #setLayerType(int, android.graphics.Paint)
7530 *
Chet Haase73066682010-11-29 15:55:32 -08007531 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 */
7533 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007534 ensureTransformationInfo();
7535 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007536 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007537 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007538 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007539 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007540 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007541 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007542 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007543 invalidate(false);
7544 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007545 }
7546
7547 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007548 * Faster version of setAlpha() which performs the same steps except there are
7549 * no calls to invalidate(). The caller of this function should perform proper invalidation
7550 * on the parent and this object. The return value indicates whether the subclass handles
7551 * alpha (the return value for onSetAlpha()).
7552 *
7553 * @param alpha The new value for the alpha property
7554 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7555 */
7556 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007557 ensureTransformationInfo();
7558 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007559 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7560 if (subclassHandlesAlpha) {
7561 mPrivateFlags |= ALPHA_SET;
7562 } else {
7563 mPrivateFlags &= ~ALPHA_SET;
7564 }
7565 return subclassHandlesAlpha;
7566 }
7567
7568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007569 * Top position of this view relative to its parent.
7570 *
7571 * @return The top of this view, in pixels.
7572 */
7573 @ViewDebug.CapturedViewProperty
7574 public final int getTop() {
7575 return mTop;
7576 }
7577
7578 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007579 * Sets the top position of this view relative to its parent. This method is meant to be called
7580 * by the layout system and should not generally be called otherwise, because the property
7581 * may be changed at any time by the layout.
7582 *
7583 * @param top The top of this view, in pixels.
7584 */
7585 public final void setTop(int top) {
7586 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007587 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007588 final boolean matrixIsIdentity = mTransformationInfo == null
7589 || mTransformationInfo.mMatrixIsIdentity;
7590 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007591 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007592 int minTop;
7593 int yLoc;
7594 if (top < mTop) {
7595 minTop = top;
7596 yLoc = top - mTop;
7597 } else {
7598 minTop = mTop;
7599 yLoc = 0;
7600 }
Chet Haasee9140a72011-02-16 16:23:29 -08007601 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007602 }
7603 } else {
7604 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007605 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007606 }
7607
Chet Haaseed032702010-10-01 14:05:54 -07007608 int width = mRight - mLeft;
7609 int oldHeight = mBottom - mTop;
7610
Chet Haase21cd1382010-09-01 17:42:29 -07007611 mTop = top;
7612
Chet Haaseed032702010-10-01 14:05:54 -07007613 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7614
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007615 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007616 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7617 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007618 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007619 }
Chet Haase21cd1382010-09-01 17:42:29 -07007620 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007621 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007622 }
Chet Haase55dbb652010-12-21 20:15:08 -08007623 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007624 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007625 }
7626 }
7627
7628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 * Bottom position of this view relative to its parent.
7630 *
7631 * @return The bottom of this view, in pixels.
7632 */
7633 @ViewDebug.CapturedViewProperty
7634 public final int getBottom() {
7635 return mBottom;
7636 }
7637
7638 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007639 * True if this view has changed since the last time being drawn.
7640 *
7641 * @return The dirty state of this view.
7642 */
7643 public boolean isDirty() {
7644 return (mPrivateFlags & DIRTY_MASK) != 0;
7645 }
7646
7647 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007648 * Sets the bottom position of this view relative to its parent. This method is meant to be
7649 * called by the layout system and should not generally be called otherwise, because the
7650 * property may be changed at any time by the layout.
7651 *
7652 * @param bottom The bottom of this view, in pixels.
7653 */
7654 public final void setBottom(int bottom) {
7655 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007656 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007657 final boolean matrixIsIdentity = mTransformationInfo == null
7658 || mTransformationInfo.mMatrixIsIdentity;
7659 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007660 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007661 int maxBottom;
7662 if (bottom < mBottom) {
7663 maxBottom = mBottom;
7664 } else {
7665 maxBottom = bottom;
7666 }
Chet Haasee9140a72011-02-16 16:23:29 -08007667 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007668 }
7669 } else {
7670 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007671 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007672 }
7673
Chet Haaseed032702010-10-01 14:05:54 -07007674 int width = mRight - mLeft;
7675 int oldHeight = mBottom - mTop;
7676
Chet Haase21cd1382010-09-01 17:42:29 -07007677 mBottom = bottom;
7678
Chet Haaseed032702010-10-01 14:05:54 -07007679 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7680
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007681 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007682 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7683 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007684 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007685 }
Chet Haase21cd1382010-09-01 17:42:29 -07007686 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007687 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007688 }
Chet Haase55dbb652010-12-21 20:15:08 -08007689 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007690 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007691 }
7692 }
7693
7694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 * Left position of this view relative to its parent.
7696 *
7697 * @return The left edge of this view, in pixels.
7698 */
7699 @ViewDebug.CapturedViewProperty
7700 public final int getLeft() {
7701 return mLeft;
7702 }
7703
7704 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007705 * Sets the left position of this view relative to its parent. This method is meant to be called
7706 * by the layout system and should not generally be called otherwise, because the property
7707 * may be changed at any time by the layout.
7708 *
7709 * @param left The bottom of this view, in pixels.
7710 */
7711 public final void setLeft(int left) {
7712 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007713 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007714 final boolean matrixIsIdentity = mTransformationInfo == null
7715 || mTransformationInfo.mMatrixIsIdentity;
7716 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007717 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007718 int minLeft;
7719 int xLoc;
7720 if (left < mLeft) {
7721 minLeft = left;
7722 xLoc = left - mLeft;
7723 } else {
7724 minLeft = mLeft;
7725 xLoc = 0;
7726 }
Chet Haasee9140a72011-02-16 16:23:29 -08007727 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007728 }
7729 } else {
7730 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007731 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007732 }
7733
Chet Haaseed032702010-10-01 14:05:54 -07007734 int oldWidth = mRight - mLeft;
7735 int height = mBottom - mTop;
7736
Chet Haase21cd1382010-09-01 17:42:29 -07007737 mLeft = left;
7738
Chet Haaseed032702010-10-01 14:05:54 -07007739 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7740
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007741 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007742 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7743 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007744 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007745 }
Chet Haase21cd1382010-09-01 17:42:29 -07007746 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007747 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007748 }
Chet Haase55dbb652010-12-21 20:15:08 -08007749 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007750 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007751 }
7752 }
7753
7754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 * Right position of this view relative to its parent.
7756 *
7757 * @return The right edge of this view, in pixels.
7758 */
7759 @ViewDebug.CapturedViewProperty
7760 public final int getRight() {
7761 return mRight;
7762 }
7763
7764 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007765 * Sets the right position of this view relative to its parent. This method is meant to be called
7766 * by the layout system and should not generally be called otherwise, because the property
7767 * may be changed at any time by the layout.
7768 *
7769 * @param right The bottom of this view, in pixels.
7770 */
7771 public final void setRight(int right) {
7772 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007773 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007774 final boolean matrixIsIdentity = mTransformationInfo == null
7775 || mTransformationInfo.mMatrixIsIdentity;
7776 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007777 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007778 int maxRight;
7779 if (right < mRight) {
7780 maxRight = mRight;
7781 } else {
7782 maxRight = right;
7783 }
Chet Haasee9140a72011-02-16 16:23:29 -08007784 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007785 }
7786 } else {
7787 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007788 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007789 }
7790
Chet Haaseed032702010-10-01 14:05:54 -07007791 int oldWidth = mRight - mLeft;
7792 int height = mBottom - mTop;
7793
Chet Haase21cd1382010-09-01 17:42:29 -07007794 mRight = right;
7795
Chet Haaseed032702010-10-01 14:05:54 -07007796 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7797
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007798 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007799 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7800 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007801 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007802 }
Chet Haase21cd1382010-09-01 17:42:29 -07007803 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007804 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007805 }
Chet Haase55dbb652010-12-21 20:15:08 -08007806 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007807 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007808 }
7809 }
7810
7811 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007812 * The visual x position of this view, in pixels. This is equivalent to the
7813 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007814 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007815 *
Chet Haasedf030d22010-07-30 17:22:38 -07007816 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007817 */
Chet Haasedf030d22010-07-30 17:22:38 -07007818 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007819 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007820 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007821
Chet Haasedf030d22010-07-30 17:22:38 -07007822 /**
7823 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7824 * {@link #setTranslationX(float) translationX} property to be the difference between
7825 * the x value passed in and the current {@link #getLeft() left} property.
7826 *
7827 * @param x The visual x position of this view, in pixels.
7828 */
7829 public void setX(float x) {
7830 setTranslationX(x - mLeft);
7831 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007832
Chet Haasedf030d22010-07-30 17:22:38 -07007833 /**
7834 * The visual y position of this view, in pixels. This is equivalent to the
7835 * {@link #setTranslationY(float) translationY} property plus the current
7836 * {@link #getTop() top} property.
7837 *
7838 * @return The visual y position of this view, in pixels.
7839 */
7840 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007841 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007842 }
7843
7844 /**
7845 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7846 * {@link #setTranslationY(float) translationY} property to be the difference between
7847 * the y value passed in and the current {@link #getTop() top} property.
7848 *
7849 * @param y The visual y position of this view, in pixels.
7850 */
7851 public void setY(float y) {
7852 setTranslationY(y - mTop);
7853 }
7854
7855
7856 /**
7857 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7858 * This position is post-layout, in addition to wherever the object's
7859 * layout placed it.
7860 *
7861 * @return The horizontal position of this view relative to its left position, in pixels.
7862 */
7863 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007864 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007865 }
7866
7867 /**
7868 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7869 * This effectively positions the object post-layout, in addition to wherever the object's
7870 * layout placed it.
7871 *
7872 * @param translationX The horizontal position of this view relative to its left position,
7873 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007874 *
7875 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007876 */
7877 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007878 ensureTransformationInfo();
7879 final TransformationInfo info = mTransformationInfo;
7880 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007881 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007882 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007883 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007884 info.mTranslationX = translationX;
7885 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007886 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007887 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007888 }
7889 }
7890
7891 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007892 * The horizontal location of this view relative to its {@link #getTop() top} position.
7893 * This position is post-layout, in addition to wherever the object's
7894 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007895 *
Chet Haasedf030d22010-07-30 17:22:38 -07007896 * @return The vertical position of this view relative to its top position,
7897 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007898 */
Chet Haasedf030d22010-07-30 17:22:38 -07007899 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007900 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007901 }
7902
7903 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007904 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7905 * This effectively positions the object 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 * @param translationY The vertical position of this view relative to its top position,
7909 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007910 *
7911 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007912 */
Chet Haasedf030d22010-07-30 17:22:38 -07007913 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007914 ensureTransformationInfo();
7915 final TransformationInfo info = mTransformationInfo;
7916 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007917 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007918 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007919 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007920 info.mTranslationY = translationY;
7921 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007922 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007923 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007924 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007925 }
7926
7927 /**
Romain Guyda489792011-02-03 01:05:15 -08007928 * @hide
7929 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007930 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007931 ensureTransformationInfo();
7932 final TransformationInfo info = mTransformationInfo;
7933 info.mTranslationX = x;
7934 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007935 }
7936
7937 /**
7938 * @hide
7939 */
7940 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007941 ensureTransformationInfo();
7942 final TransformationInfo info = mTransformationInfo;
7943 info.mTranslationY = y;
7944 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007945 }
7946
7947 /**
7948 * @hide
7949 */
Romain Guyda489792011-02-03 01:05:15 -08007950 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007951 ensureTransformationInfo();
7952 final TransformationInfo info = mTransformationInfo;
7953 info.mTranslationX = x - mLeft;
7954 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007955 }
7956
7957 /**
7958 * @hide
7959 */
7960 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007961 ensureTransformationInfo();
7962 final TransformationInfo info = mTransformationInfo;
7963 info.mTranslationY = y - mTop;
7964 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007965 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007966
Romain Guyda489792011-02-03 01:05:15 -08007967 /**
7968 * @hide
7969 */
7970 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007971 ensureTransformationInfo();
7972 final TransformationInfo info = mTransformationInfo;
7973 info.mScaleX = x;
7974 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007975 }
7976
7977 /**
7978 * @hide
7979 */
7980 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007981 ensureTransformationInfo();
7982 final TransformationInfo info = mTransformationInfo;
7983 info.mScaleY = y;
7984 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007985 }
7986
7987 /**
7988 * @hide
7989 */
7990 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007991 ensureTransformationInfo();
7992 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007993 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007994
Romain Guyda489792011-02-03 01:05:15 -08007995 /**
7996 * @hide
7997 */
7998 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007999 ensureTransformationInfo();
8000 final TransformationInfo info = mTransformationInfo;
8001 info.mRotationY = y;
8002 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08008003 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008004
Romain Guyda489792011-02-03 01:05:15 -08008005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 * Hit rectangle in parent's coordinates
8007 *
8008 * @param outRect The hit rectangle of the view.
8009 */
8010 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008011 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008012 final TransformationInfo info = mTransformationInfo;
8013 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008014 outRect.set(mLeft, mTop, mRight, mBottom);
8015 } else {
8016 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008017 tmpRect.set(-info.mPivotX, -info.mPivotY,
8018 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8019 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008020 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8021 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008022 }
8023 }
8024
8025 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008026 * Determines whether the given point, in local coordinates is inside the view.
8027 */
8028 /*package*/ final boolean pointInView(float localX, float localY) {
8029 return localX >= 0 && localX < (mRight - mLeft)
8030 && localY >= 0 && localY < (mBottom - mTop);
8031 }
8032
8033 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008034 * Utility method to determine whether the given point, in local coordinates,
8035 * is inside the view, where the area of the view is expanded by the slop factor.
8036 * This method is called while processing touch-move events to determine if the event
8037 * is still within the view.
8038 */
8039 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008040 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008041 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 }
8043
8044 /**
8045 * When a view has focus and the user navigates away from it, the next view is searched for
8046 * starting from the rectangle filled in by this method.
8047 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008048 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8049 * of the view. However, if your view maintains some idea of internal selection,
8050 * such as a cursor, or a selected row or column, you should override this method and
8051 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 *
8053 * @param r The rectangle to fill in, in this view's coordinates.
8054 */
8055 public void getFocusedRect(Rect r) {
8056 getDrawingRect(r);
8057 }
8058
8059 /**
8060 * If some part of this view is not clipped by any of its parents, then
8061 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008062 * coordinates (without taking possible View rotations into account), offset
8063 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8064 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065 *
8066 * @param r If true is returned, r holds the global coordinates of the
8067 * visible portion of this view.
8068 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8069 * between this view and its root. globalOffet may be null.
8070 * @return true if r is non-empty (i.e. part of the view is visible at the
8071 * root level.
8072 */
8073 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8074 int width = mRight - mLeft;
8075 int height = mBottom - mTop;
8076 if (width > 0 && height > 0) {
8077 r.set(0, 0, width, height);
8078 if (globalOffset != null) {
8079 globalOffset.set(-mScrollX, -mScrollY);
8080 }
8081 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8082 }
8083 return false;
8084 }
8085
8086 public final boolean getGlobalVisibleRect(Rect r) {
8087 return getGlobalVisibleRect(r, null);
8088 }
8089
8090 public final boolean getLocalVisibleRect(Rect r) {
8091 Point offset = new Point();
8092 if (getGlobalVisibleRect(r, offset)) {
8093 r.offset(-offset.x, -offset.y); // make r local
8094 return true;
8095 }
8096 return false;
8097 }
8098
8099 /**
8100 * Offset this view's vertical location by the specified number of pixels.
8101 *
8102 * @param offset the number of pixels to offset the view by
8103 */
8104 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008105 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008106 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008107 final boolean matrixIsIdentity = mTransformationInfo == null
8108 || mTransformationInfo.mMatrixIsIdentity;
8109 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008110 final ViewParent p = mParent;
8111 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008112 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008113 int minTop;
8114 int maxBottom;
8115 int yLoc;
8116 if (offset < 0) {
8117 minTop = mTop + offset;
8118 maxBottom = mBottom;
8119 yLoc = offset;
8120 } else {
8121 minTop = mTop;
8122 maxBottom = mBottom + offset;
8123 yLoc = 0;
8124 }
8125 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8126 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008127 }
8128 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008129 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008130 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008131
Chet Haasec3aa3612010-06-17 08:50:37 -07008132 mTop += offset;
8133 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008134
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008135 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008136 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008137 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008138 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008139 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 }
8142
8143 /**
8144 * Offset this view's horizontal location by the specified amount of pixels.
8145 *
8146 * @param offset the numer of pixels to offset the view by
8147 */
8148 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008149 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008150 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008151 final boolean matrixIsIdentity = mTransformationInfo == null
8152 || mTransformationInfo.mMatrixIsIdentity;
8153 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008154 final ViewParent p = mParent;
8155 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008156 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008157 int minLeft;
8158 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008159 if (offset < 0) {
8160 minLeft = mLeft + offset;
8161 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008162 } else {
8163 minLeft = mLeft;
8164 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008165 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008166 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008167 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008168 }
8169 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008170 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008171 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008172
Chet Haasec3aa3612010-06-17 08:50:37 -07008173 mLeft += offset;
8174 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008175
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008176 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008177 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008178 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008179 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008180 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 }
8183
8184 /**
8185 * Get the LayoutParams associated with this view. All views should have
8186 * layout parameters. These supply parameters to the <i>parent</i> of this
8187 * view specifying how it should be arranged. There are many subclasses of
8188 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8189 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008190 *
8191 * This method may return null if this View is not attached to a parent
8192 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8193 * was not invoked successfully. When a View is attached to a parent
8194 * ViewGroup, this method must not return null.
8195 *
8196 * @return The LayoutParams associated with this view, or null if no
8197 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008199 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 public ViewGroup.LayoutParams getLayoutParams() {
8201 return mLayoutParams;
8202 }
8203
8204 /**
8205 * Set the layout parameters associated with this view. These supply
8206 * parameters to the <i>parent</i> of this view specifying how it should be
8207 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8208 * correspond to the different subclasses of ViewGroup that are responsible
8209 * for arranging their children.
8210 *
Romain Guy01c174b2011-02-22 11:51:06 -08008211 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 */
8213 public void setLayoutParams(ViewGroup.LayoutParams params) {
8214 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008215 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 }
8217 mLayoutParams = params;
8218 requestLayout();
8219 }
8220
8221 /**
8222 * Set the scrolled position of your view. This will cause a call to
8223 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8224 * invalidated.
8225 * @param x the x position to scroll to
8226 * @param y the y position to scroll to
8227 */
8228 public void scrollTo(int x, int y) {
8229 if (mScrollX != x || mScrollY != y) {
8230 int oldX = mScrollX;
8231 int oldY = mScrollY;
8232 mScrollX = x;
8233 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008234 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008236 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008237 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 }
8240 }
8241
8242 /**
8243 * Move the scrolled position of your view. This will cause a call to
8244 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8245 * invalidated.
8246 * @param x the amount of pixels to scroll by horizontally
8247 * @param y the amount of pixels to scroll by vertically
8248 */
8249 public void scrollBy(int x, int y) {
8250 scrollTo(mScrollX + x, mScrollY + y);
8251 }
8252
8253 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008254 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8255 * animation to fade the scrollbars out after a default delay. If a subclass
8256 * provides animated scrolling, the start delay should equal the duration
8257 * of the scrolling animation.</p>
8258 *
8259 * <p>The animation starts only if at least one of the scrollbars is
8260 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8261 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8262 * this method returns true, and false otherwise. If the animation is
8263 * started, this method calls {@link #invalidate()}; in that case the
8264 * caller should not call {@link #invalidate()}.</p>
8265 *
8266 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008267 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008268 *
8269 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8270 * and {@link #scrollTo(int, int)}.</p>
8271 *
8272 * @return true if the animation is played, false otherwise
8273 *
8274 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008275 * @see #scrollBy(int, int)
8276 * @see #scrollTo(int, int)
8277 * @see #isHorizontalScrollBarEnabled()
8278 * @see #isVerticalScrollBarEnabled()
8279 * @see #setHorizontalScrollBarEnabled(boolean)
8280 * @see #setVerticalScrollBarEnabled(boolean)
8281 */
8282 protected boolean awakenScrollBars() {
8283 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008284 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008285 }
8286
8287 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008288 * Trigger the scrollbars to draw.
8289 * This method differs from awakenScrollBars() only in its default duration.
8290 * initialAwakenScrollBars() will show the scroll bars for longer than
8291 * usual to give the user more of a chance to notice them.
8292 *
8293 * @return true if the animation is played, false otherwise.
8294 */
8295 private boolean initialAwakenScrollBars() {
8296 return mScrollCache != null &&
8297 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8298 }
8299
8300 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008301 * <p>
8302 * Trigger the scrollbars to draw. When invoked this method starts an
8303 * animation to fade the scrollbars out after a fixed delay. If a subclass
8304 * provides animated scrolling, the start delay should equal the duration of
8305 * the scrolling animation.
8306 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008307 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008308 * <p>
8309 * The animation starts only if at least one of the scrollbars is enabled,
8310 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8311 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8312 * this method returns true, and false otherwise. If the animation is
8313 * started, this method calls {@link #invalidate()}; in that case the caller
8314 * should not call {@link #invalidate()}.
8315 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008316 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008317 * <p>
8318 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008319 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008320 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008321 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008322 * @param startDelay the delay, in milliseconds, after which the animation
8323 * should start; when the delay is 0, the animation starts
8324 * immediately
8325 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008326 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008327 * @see #scrollBy(int, int)
8328 * @see #scrollTo(int, int)
8329 * @see #isHorizontalScrollBarEnabled()
8330 * @see #isVerticalScrollBarEnabled()
8331 * @see #setHorizontalScrollBarEnabled(boolean)
8332 * @see #setVerticalScrollBarEnabled(boolean)
8333 */
8334 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008335 return awakenScrollBars(startDelay, true);
8336 }
Joe Malin32736f02011-01-19 16:14:20 -08008337
Mike Cleron290947b2009-09-29 18:34:32 -07008338 /**
8339 * <p>
8340 * Trigger the scrollbars to draw. When invoked this method starts an
8341 * animation to fade the scrollbars out after a fixed delay. If a subclass
8342 * provides animated scrolling, the start delay should equal the duration of
8343 * the scrolling animation.
8344 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008345 *
Mike Cleron290947b2009-09-29 18:34:32 -07008346 * <p>
8347 * The animation starts only if at least one of the scrollbars is enabled,
8348 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8349 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8350 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008351 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008352 * is set to true; in that case the caller
8353 * should not call {@link #invalidate()}.
8354 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008355 *
Mike Cleron290947b2009-09-29 18:34:32 -07008356 * <p>
8357 * This method should be invoked everytime a subclass directly updates the
8358 * scroll parameters.
8359 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008360 *
Mike Cleron290947b2009-09-29 18:34:32 -07008361 * @param startDelay the delay, in milliseconds, after which the animation
8362 * should start; when the delay is 0, the animation starts
8363 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008364 *
Mike Cleron290947b2009-09-29 18:34:32 -07008365 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008366 *
Mike Cleron290947b2009-09-29 18:34:32 -07008367 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008368 *
Mike Cleron290947b2009-09-29 18:34:32 -07008369 * @see #scrollBy(int, int)
8370 * @see #scrollTo(int, int)
8371 * @see #isHorizontalScrollBarEnabled()
8372 * @see #isVerticalScrollBarEnabled()
8373 * @see #setHorizontalScrollBarEnabled(boolean)
8374 * @see #setVerticalScrollBarEnabled(boolean)
8375 */
8376 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008377 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008378
Mike Cleronf116bf82009-09-27 19:14:12 -07008379 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8380 return false;
8381 }
8382
8383 if (scrollCache.scrollBar == null) {
8384 scrollCache.scrollBar = new ScrollBarDrawable();
8385 }
8386
8387 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8388
Mike Cleron290947b2009-09-29 18:34:32 -07008389 if (invalidate) {
8390 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008391 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008392 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008393
8394 if (scrollCache.state == ScrollabilityCache.OFF) {
8395 // FIXME: this is copied from WindowManagerService.
8396 // We should get this value from the system when it
8397 // is possible to do so.
8398 final int KEY_REPEAT_FIRST_DELAY = 750;
8399 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8400 }
8401
8402 // Tell mScrollCache when we should start fading. This may
8403 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008404 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008405 scrollCache.fadeStartTime = fadeStartTime;
8406 scrollCache.state = ScrollabilityCache.ON;
8407
8408 // Schedule our fader to run, unscheduling any old ones first
8409 if (mAttachInfo != null) {
8410 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8411 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8412 }
8413
8414 return true;
8415 }
8416
8417 return false;
8418 }
8419
8420 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008421 * Do not invalidate views which are not visible and which are not running an animation. They
8422 * will not get drawn and they should not set dirty flags as if they will be drawn
8423 */
8424 private boolean skipInvalidate() {
8425 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8426 (!(mParent instanceof ViewGroup) ||
8427 !((ViewGroup) mParent).isViewTransitioning(this));
8428 }
8429 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008430 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008431 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8432 * in the future. This must be called from a UI thread. To call from a non-UI
8433 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008434 *
8435 * WARNING: This method is destructive to dirty.
8436 * @param dirty the rectangle representing the bounds of the dirty region
8437 */
8438 public void invalidate(Rect dirty) {
8439 if (ViewDebug.TRACE_HIERARCHY) {
8440 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8441 }
8442
Chet Haaseaceafe62011-08-26 15:44:33 -07008443 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008444 return;
8445 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008446 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008447 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8448 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008450 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008451 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 final ViewParent p = mParent;
8453 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008454 //noinspection PointlessBooleanExpression,ConstantConditions
8455 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8456 if (p != null && ai != null && ai.mHardwareAccelerated) {
8457 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008458 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008459 p.invalidateChild(this, null);
8460 return;
8461 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008463 if (p != null && ai != null) {
8464 final int scrollX = mScrollX;
8465 final int scrollY = mScrollY;
8466 final Rect r = ai.mTmpInvalRect;
8467 r.set(dirty.left - scrollX, dirty.top - scrollY,
8468 dirty.right - scrollX, dirty.bottom - scrollY);
8469 mParent.invalidateChild(this, r);
8470 }
8471 }
8472 }
8473
8474 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008475 * 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 -08008476 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008477 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8478 * will be called at some point in the future. This must be called from
8479 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 * @param l the left position of the dirty region
8481 * @param t the top position of the dirty region
8482 * @param r the right position of the dirty region
8483 * @param b the bottom position of the dirty region
8484 */
8485 public void invalidate(int l, int t, int r, int b) {
8486 if (ViewDebug.TRACE_HIERARCHY) {
8487 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8488 }
8489
Chet Haaseaceafe62011-08-26 15:44:33 -07008490 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008491 return;
8492 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008493 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008494 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8495 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008497 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008498 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 final ViewParent p = mParent;
8500 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008501 //noinspection PointlessBooleanExpression,ConstantConditions
8502 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8503 if (p != null && ai != null && ai.mHardwareAccelerated) {
8504 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008505 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008506 p.invalidateChild(this, null);
8507 return;
8508 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008510 if (p != null && ai != null && l < r && t < b) {
8511 final int scrollX = mScrollX;
8512 final int scrollY = mScrollY;
8513 final Rect tmpr = ai.mTmpInvalRect;
8514 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8515 p.invalidateChild(this, tmpr);
8516 }
8517 }
8518 }
8519
8520 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008521 * Invalidate the whole view. If the view is visible,
8522 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8523 * the future. This must be called from a UI thread. To call from a non-UI thread,
8524 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 */
8526 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008527 invalidate(true);
8528 }
Joe Malin32736f02011-01-19 16:14:20 -08008529
Chet Haaseed032702010-10-01 14:05:54 -07008530 /**
8531 * This is where the invalidate() work actually happens. A full invalidate()
8532 * causes the drawing cache to be invalidated, but this function can be called with
8533 * invalidateCache set to false to skip that invalidation step for cases that do not
8534 * need it (for example, a component that remains at the same dimensions with the same
8535 * content).
8536 *
8537 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8538 * well. This is usually true for a full invalidate, but may be set to false if the
8539 * View's contents or dimensions have not changed.
8540 */
Romain Guy849d0a32011-02-01 17:20:48 -08008541 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 if (ViewDebug.TRACE_HIERARCHY) {
8543 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8544 }
8545
Chet Haaseaceafe62011-08-26 15:44:33 -07008546 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008547 return;
8548 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008549 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008550 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008551 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8552 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008553 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008554 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008555 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008556 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008557 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008560 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008561 //noinspection PointlessBooleanExpression,ConstantConditions
8562 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8563 if (p != null && ai != null && ai.mHardwareAccelerated) {
8564 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008565 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008566 p.invalidateChild(this, null);
8567 return;
8568 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008569 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 if (p != null && ai != null) {
8572 final Rect r = ai.mTmpInvalRect;
8573 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8574 // Don't call invalidate -- we don't want to internally scroll
8575 // our own bounds
8576 p.invalidateChild(this, r);
8577 }
8578 }
8579 }
8580
8581 /**
Romain Guyda489792011-02-03 01:05:15 -08008582 * @hide
8583 */
8584 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008585 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008586 return;
8587 }
Romain Guyda489792011-02-03 01:05:15 -08008588 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8589 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8590 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8591 if (mParent instanceof View) {
8592 ((View) mParent).mPrivateFlags |= INVALIDATED;
8593 }
8594 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008595 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008596 mPrivateFlags |= INVALIDATED;
8597 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008598 if (mParent != null && mAttachInfo != null) {
8599 if (mAttachInfo.mHardwareAccelerated) {
8600 mParent.invalidateChild(this, null);
8601 } else {
8602 final Rect r = mAttachInfo.mTmpInvalRect;
8603 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8604 // Don't call invalidate -- we don't want to internally scroll
8605 // our own bounds
8606 mParent.invalidateChild(this, r);
8607 }
Romain Guyda489792011-02-03 01:05:15 -08008608 }
8609 }
8610 }
8611
8612 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008613 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008614 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8615 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008616 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8617 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008618 *
8619 * @hide
8620 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008621 protected void invalidateParentCaches() {
8622 if (mParent instanceof View) {
8623 ((View) mParent).mPrivateFlags |= INVALIDATED;
8624 }
8625 }
Joe Malin32736f02011-01-19 16:14:20 -08008626
Romain Guy0fd89bf2011-01-26 15:41:30 -08008627 /**
8628 * Used to indicate that the parent of this view should be invalidated. This functionality
8629 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8630 * which is necessary when various parent-managed properties of the view change, such as
8631 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8632 * an invalidation event to the parent.
8633 *
8634 * @hide
8635 */
8636 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008637 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008638 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008639 }
8640 }
8641
8642 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008643 * Indicates whether this View is opaque. An opaque View guarantees that it will
8644 * draw all the pixels overlapping its bounds using a fully opaque color.
8645 *
8646 * Subclasses of View should override this method whenever possible to indicate
8647 * whether an instance is opaque. Opaque Views are treated in a special way by
8648 * the View hierarchy, possibly allowing it to perform optimizations during
8649 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008650 *
Romain Guy24443ea2009-05-11 11:56:30 -07008651 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008652 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008653 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008654 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008655 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008656 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8657 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008658 }
8659
Adam Powell20232d02010-12-08 21:08:53 -08008660 /**
8661 * @hide
8662 */
8663 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008664 // Opaque if:
8665 // - Has a background
8666 // - Background is opaque
8667 // - Doesn't have scrollbars or scrollbars are inside overlay
8668
8669 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8670 mPrivateFlags |= OPAQUE_BACKGROUND;
8671 } else {
8672 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8673 }
8674
8675 final int flags = mViewFlags;
8676 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8677 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8678 mPrivateFlags |= OPAQUE_SCROLLBARS;
8679 } else {
8680 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8681 }
8682 }
8683
8684 /**
8685 * @hide
8686 */
8687 protected boolean hasOpaqueScrollbars() {
8688 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008689 }
8690
8691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 * @return A handler associated with the thread running the View. This
8693 * handler can be used to pump events in the UI events queue.
8694 */
8695 public Handler getHandler() {
8696 if (mAttachInfo != null) {
8697 return mAttachInfo.mHandler;
8698 }
8699 return null;
8700 }
8701
8702 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008703 * <p>Causes the Runnable to be added to the message queue.
8704 * The runnable will be run on the user interface thread.</p>
8705 *
8706 * <p>This method can be invoked from outside of the UI thread
8707 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708 *
8709 * @param action The Runnable that will be executed.
8710 *
8711 * @return Returns true if the Runnable was successfully placed in to the
8712 * message queue. Returns false on failure, usually because the
8713 * looper processing the message queue is exiting.
8714 */
8715 public boolean post(Runnable action) {
8716 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008717 AttachInfo attachInfo = mAttachInfo;
8718 if (attachInfo != null) {
8719 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 } else {
8721 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008722 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008723 return true;
8724 }
8725
8726 return handler.post(action);
8727 }
8728
8729 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008730 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008732 * The runnable will be run on the user interface thread.</p>
8733 *
8734 * <p>This method can be invoked from outside of the UI thread
8735 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008736 *
8737 * @param action The Runnable that will be executed.
8738 * @param delayMillis The delay (in milliseconds) until the Runnable
8739 * will be executed.
8740 *
8741 * @return true if the Runnable was successfully placed in to the
8742 * message queue. Returns false on failure, usually because the
8743 * looper processing the message queue is exiting. Note that a
8744 * result of true does not mean the Runnable will be processed --
8745 * if the looper is quit before the delivery time of the message
8746 * occurs then the message will be dropped.
8747 */
8748 public boolean postDelayed(Runnable action, long delayMillis) {
8749 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008750 AttachInfo attachInfo = mAttachInfo;
8751 if (attachInfo != null) {
8752 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 } else {
8754 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008755 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 return true;
8757 }
8758
8759 return handler.postDelayed(action, delayMillis);
8760 }
8761
8762 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008763 * <p>Removes the specified Runnable from the message queue.</p>
8764 *
8765 * <p>This method can be invoked from outside of the UI thread
8766 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008767 *
8768 * @param action The Runnable to remove from the message handling queue
8769 *
8770 * @return true if this view could ask the Handler to remove the Runnable,
8771 * false otherwise. When the returned value is true, the Runnable
8772 * may or may not have been actually removed from the message queue
8773 * (for instance, if the Runnable was not in the queue already.)
8774 */
8775 public boolean removeCallbacks(Runnable action) {
8776 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008777 AttachInfo attachInfo = mAttachInfo;
8778 if (attachInfo != null) {
8779 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 } else {
8781 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008782 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 return true;
8784 }
8785
8786 handler.removeCallbacks(action);
8787 return true;
8788 }
8789
8790 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008791 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8792 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 *
Romain Guye63a4f32011-08-11 11:33:31 -07008794 * <p>This method can be invoked from outside of the UI thread
8795 * only when this View is attached to a window.</p>
8796 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 * @see #invalidate()
8798 */
8799 public void postInvalidate() {
8800 postInvalidateDelayed(0);
8801 }
8802
8803 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008804 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8805 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8806 *
8807 * <p>This method can be invoked from outside of the UI thread
8808 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 *
8810 * @param left The left coordinate of the rectangle to invalidate.
8811 * @param top The top coordinate of the rectangle to invalidate.
8812 * @param right The right coordinate of the rectangle to invalidate.
8813 * @param bottom The bottom coordinate of the rectangle to invalidate.
8814 *
8815 * @see #invalidate(int, int, int, int)
8816 * @see #invalidate(Rect)
8817 */
8818 public void postInvalidate(int left, int top, int right, int bottom) {
8819 postInvalidateDelayed(0, left, top, right, bottom);
8820 }
8821
8822 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008823 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8824 * loop. Waits for the specified amount of time.</p>
8825 *
8826 * <p>This method can be invoked from outside of the UI thread
8827 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 *
8829 * @param delayMilliseconds the duration in milliseconds to delay the
8830 * invalidation by
8831 */
8832 public void postInvalidateDelayed(long delayMilliseconds) {
8833 // We try only with the AttachInfo because there's no point in invalidating
8834 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008835 AttachInfo attachInfo = mAttachInfo;
8836 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008837 Message msg = Message.obtain();
8838 msg.what = AttachInfo.INVALIDATE_MSG;
8839 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008840 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 }
8842 }
8843
8844 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008845 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8846 * through the event loop. Waits for the specified amount of time.</p>
8847 *
8848 * <p>This method can be invoked from outside of the UI thread
8849 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 *
8851 * @param delayMilliseconds the duration in milliseconds to delay the
8852 * invalidation by
8853 * @param left The left coordinate of the rectangle to invalidate.
8854 * @param top The top coordinate of the rectangle to invalidate.
8855 * @param right The right coordinate of the rectangle to invalidate.
8856 * @param bottom The bottom coordinate of the rectangle to invalidate.
8857 */
8858 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8859 int right, int bottom) {
8860
8861 // We try only with the AttachInfo because there's no point in invalidating
8862 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008863 AttachInfo attachInfo = mAttachInfo;
8864 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8866 info.target = this;
8867 info.left = left;
8868 info.top = top;
8869 info.right = right;
8870 info.bottom = bottom;
8871
8872 final Message msg = Message.obtain();
8873 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8874 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008875 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 }
8877 }
8878
8879 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008880 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8881 * This event is sent at most once every
8882 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8883 */
8884 private void postSendViewScrolledAccessibilityEventCallback() {
8885 if (mSendViewScrolledAccessibilityEvent == null) {
8886 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8887 }
8888 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8889 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8890 postDelayed(mSendViewScrolledAccessibilityEvent,
8891 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8892 }
8893 }
8894
8895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 * Called by a parent to request that a child update its values for mScrollX
8897 * and mScrollY if necessary. This will typically be done if the child is
8898 * animating a scroll using a {@link android.widget.Scroller Scroller}
8899 * object.
8900 */
8901 public void computeScroll() {
8902 }
8903
8904 /**
8905 * <p>Indicate whether the horizontal edges are faded when the view is
8906 * scrolled horizontally.</p>
8907 *
8908 * @return true if the horizontal edges should are faded on scroll, false
8909 * otherwise
8910 *
8911 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008912 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 */
8914 public boolean isHorizontalFadingEdgeEnabled() {
8915 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8916 }
8917
8918 /**
8919 * <p>Define whether the horizontal edges should be faded when this view
8920 * is scrolled horizontally.</p>
8921 *
8922 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8923 * be faded when the view is scrolled
8924 * horizontally
8925 *
8926 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008927 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 */
8929 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8930 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8931 if (horizontalFadingEdgeEnabled) {
8932 initScrollCache();
8933 }
8934
8935 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8936 }
8937 }
8938
8939 /**
8940 * <p>Indicate whether the vertical edges are faded when the view is
8941 * scrolled horizontally.</p>
8942 *
8943 * @return true if the vertical edges should are faded on scroll, false
8944 * otherwise
8945 *
8946 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008947 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 */
8949 public boolean isVerticalFadingEdgeEnabled() {
8950 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8951 }
8952
8953 /**
8954 * <p>Define whether the vertical edges should be faded when this view
8955 * is scrolled vertically.</p>
8956 *
8957 * @param verticalFadingEdgeEnabled true if the vertical edges should
8958 * be faded when the view is scrolled
8959 * vertically
8960 *
8961 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008962 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 */
8964 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8965 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8966 if (verticalFadingEdgeEnabled) {
8967 initScrollCache();
8968 }
8969
8970 mViewFlags ^= FADING_EDGE_VERTICAL;
8971 }
8972 }
8973
8974 /**
8975 * Returns the strength, or intensity, of the top faded edge. The strength is
8976 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8977 * returns 0.0 or 1.0 but no value in between.
8978 *
8979 * Subclasses should override this method to provide a smoother fade transition
8980 * when scrolling occurs.
8981 *
8982 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8983 */
8984 protected float getTopFadingEdgeStrength() {
8985 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8986 }
8987
8988 /**
8989 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8990 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8991 * returns 0.0 or 1.0 but no value in between.
8992 *
8993 * Subclasses should override this method to provide a smoother fade transition
8994 * when scrolling occurs.
8995 *
8996 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8997 */
8998 protected float getBottomFadingEdgeStrength() {
8999 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9000 computeVerticalScrollRange() ? 1.0f : 0.0f;
9001 }
9002
9003 /**
9004 * Returns the strength, or intensity, of the left faded edge. The strength is
9005 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9006 * returns 0.0 or 1.0 but no value in between.
9007 *
9008 * Subclasses should override this method to provide a smoother fade transition
9009 * when scrolling occurs.
9010 *
9011 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9012 */
9013 protected float getLeftFadingEdgeStrength() {
9014 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9015 }
9016
9017 /**
9018 * Returns the strength, or intensity, of the right faded edge. The strength is
9019 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9020 * returns 0.0 or 1.0 but no value in between.
9021 *
9022 * Subclasses should override this method to provide a smoother fade transition
9023 * when scrolling occurs.
9024 *
9025 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9026 */
9027 protected float getRightFadingEdgeStrength() {
9028 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9029 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9030 }
9031
9032 /**
9033 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9034 * scrollbar is not drawn by default.</p>
9035 *
9036 * @return true if the horizontal scrollbar should be painted, false
9037 * otherwise
9038 *
9039 * @see #setHorizontalScrollBarEnabled(boolean)
9040 */
9041 public boolean isHorizontalScrollBarEnabled() {
9042 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9043 }
9044
9045 /**
9046 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9047 * scrollbar is not drawn by default.</p>
9048 *
9049 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9050 * be painted
9051 *
9052 * @see #isHorizontalScrollBarEnabled()
9053 */
9054 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9055 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9056 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009057 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009058 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009059 }
9060 }
9061
9062 /**
9063 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9064 * scrollbar is not drawn by default.</p>
9065 *
9066 * @return true if the vertical scrollbar should be painted, false
9067 * otherwise
9068 *
9069 * @see #setVerticalScrollBarEnabled(boolean)
9070 */
9071 public boolean isVerticalScrollBarEnabled() {
9072 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9073 }
9074
9075 /**
9076 * <p>Define whether the vertical scrollbar should be drawn or not. The
9077 * scrollbar is not drawn by default.</p>
9078 *
9079 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9080 * be painted
9081 *
9082 * @see #isVerticalScrollBarEnabled()
9083 */
9084 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9085 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9086 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009087 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009088 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089 }
9090 }
9091
Adam Powell20232d02010-12-08 21:08:53 -08009092 /**
9093 * @hide
9094 */
9095 protected void recomputePadding() {
9096 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 }
Joe Malin32736f02011-01-19 16:14:20 -08009098
Mike Cleronfe81d382009-09-28 14:22:16 -07009099 /**
9100 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009101 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009102 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009103 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009104 */
9105 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9106 initScrollCache();
9107 final ScrollabilityCache scrollabilityCache = mScrollCache;
9108 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009109 if (fadeScrollbars) {
9110 scrollabilityCache.state = ScrollabilityCache.OFF;
9111 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009112 scrollabilityCache.state = ScrollabilityCache.ON;
9113 }
9114 }
Joe Malin32736f02011-01-19 16:14:20 -08009115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116 /**
Joe Malin32736f02011-01-19 16:14:20 -08009117 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009118 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009119 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009120 * @return true if scrollbar fading is enabled
9121 */
9122 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009123 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009124 }
Joe Malin32736f02011-01-19 16:14:20 -08009125
Mike Cleron52f0a642009-09-28 18:21:37 -07009126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009127 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9128 * inset. When inset, they add to the padding of the view. And the scrollbars
9129 * can be drawn inside the padding area or on the edge of the view. For example,
9130 * if a view has a background drawable and you want to draw the scrollbars
9131 * inside the padding specified by the drawable, you can use
9132 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9133 * appear at the edge of the view, ignoring the padding, then you can use
9134 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9135 * @param style the style of the scrollbars. Should be one of
9136 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9137 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9138 * @see #SCROLLBARS_INSIDE_OVERLAY
9139 * @see #SCROLLBARS_INSIDE_INSET
9140 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9141 * @see #SCROLLBARS_OUTSIDE_INSET
9142 */
9143 public void setScrollBarStyle(int style) {
9144 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9145 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009146 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009147 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009148 }
9149 }
9150
9151 /**
9152 * <p>Returns the current scrollbar style.</p>
9153 * @return the current scrollbar style
9154 * @see #SCROLLBARS_INSIDE_OVERLAY
9155 * @see #SCROLLBARS_INSIDE_INSET
9156 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9157 * @see #SCROLLBARS_OUTSIDE_INSET
9158 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009159 @ViewDebug.ExportedProperty(mapping = {
9160 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9161 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9162 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9163 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9164 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009165 public int getScrollBarStyle() {
9166 return mViewFlags & SCROLLBARS_STYLE_MASK;
9167 }
9168
9169 /**
9170 * <p>Compute the horizontal range that the horizontal scrollbar
9171 * represents.</p>
9172 *
9173 * <p>The range is expressed in arbitrary units that must be the same as the
9174 * units used by {@link #computeHorizontalScrollExtent()} and
9175 * {@link #computeHorizontalScrollOffset()}.</p>
9176 *
9177 * <p>The default range is the drawing width of this view.</p>
9178 *
9179 * @return the total horizontal range represented by the horizontal
9180 * scrollbar
9181 *
9182 * @see #computeHorizontalScrollExtent()
9183 * @see #computeHorizontalScrollOffset()
9184 * @see android.widget.ScrollBarDrawable
9185 */
9186 protected int computeHorizontalScrollRange() {
9187 return getWidth();
9188 }
9189
9190 /**
9191 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9192 * within the horizontal range. This value is used to compute the position
9193 * of the thumb within the scrollbar's track.</p>
9194 *
9195 * <p>The range is expressed in arbitrary units that must be the same as the
9196 * units used by {@link #computeHorizontalScrollRange()} and
9197 * {@link #computeHorizontalScrollExtent()}.</p>
9198 *
9199 * <p>The default offset is the scroll offset of this view.</p>
9200 *
9201 * @return the horizontal offset of the scrollbar's thumb
9202 *
9203 * @see #computeHorizontalScrollRange()
9204 * @see #computeHorizontalScrollExtent()
9205 * @see android.widget.ScrollBarDrawable
9206 */
9207 protected int computeHorizontalScrollOffset() {
9208 return mScrollX;
9209 }
9210
9211 /**
9212 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9213 * within the horizontal range. This value is used to compute the length
9214 * of the thumb within the scrollbar's track.</p>
9215 *
9216 * <p>The range is expressed in arbitrary units that must be the same as the
9217 * units used by {@link #computeHorizontalScrollRange()} and
9218 * {@link #computeHorizontalScrollOffset()}.</p>
9219 *
9220 * <p>The default extent is the drawing width of this view.</p>
9221 *
9222 * @return the horizontal extent of the scrollbar's thumb
9223 *
9224 * @see #computeHorizontalScrollRange()
9225 * @see #computeHorizontalScrollOffset()
9226 * @see android.widget.ScrollBarDrawable
9227 */
9228 protected int computeHorizontalScrollExtent() {
9229 return getWidth();
9230 }
9231
9232 /**
9233 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9234 *
9235 * <p>The range is expressed in arbitrary units that must be the same as the
9236 * units used by {@link #computeVerticalScrollExtent()} and
9237 * {@link #computeVerticalScrollOffset()}.</p>
9238 *
9239 * @return the total vertical range represented by the vertical scrollbar
9240 *
9241 * <p>The default range is the drawing height of this view.</p>
9242 *
9243 * @see #computeVerticalScrollExtent()
9244 * @see #computeVerticalScrollOffset()
9245 * @see android.widget.ScrollBarDrawable
9246 */
9247 protected int computeVerticalScrollRange() {
9248 return getHeight();
9249 }
9250
9251 /**
9252 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9253 * within the horizontal range. This value is used to compute the position
9254 * of the thumb within the scrollbar's track.</p>
9255 *
9256 * <p>The range is expressed in arbitrary units that must be the same as the
9257 * units used by {@link #computeVerticalScrollRange()} and
9258 * {@link #computeVerticalScrollExtent()}.</p>
9259 *
9260 * <p>The default offset is the scroll offset of this view.</p>
9261 *
9262 * @return the vertical offset of the scrollbar's thumb
9263 *
9264 * @see #computeVerticalScrollRange()
9265 * @see #computeVerticalScrollExtent()
9266 * @see android.widget.ScrollBarDrawable
9267 */
9268 protected int computeVerticalScrollOffset() {
9269 return mScrollY;
9270 }
9271
9272 /**
9273 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9274 * within the vertical range. This value is used to compute the length
9275 * of the thumb within the scrollbar's track.</p>
9276 *
9277 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009278 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 * {@link #computeVerticalScrollOffset()}.</p>
9280 *
9281 * <p>The default extent is the drawing height of this view.</p>
9282 *
9283 * @return the vertical extent of the scrollbar's thumb
9284 *
9285 * @see #computeVerticalScrollRange()
9286 * @see #computeVerticalScrollOffset()
9287 * @see android.widget.ScrollBarDrawable
9288 */
9289 protected int computeVerticalScrollExtent() {
9290 return getHeight();
9291 }
9292
9293 /**
Adam Powell69159442011-06-13 17:53:06 -07009294 * Check if this view can be scrolled horizontally in a certain direction.
9295 *
9296 * @param direction Negative to check scrolling left, positive to check scrolling right.
9297 * @return true if this view can be scrolled in the specified direction, false otherwise.
9298 */
9299 public boolean canScrollHorizontally(int direction) {
9300 final int offset = computeHorizontalScrollOffset();
9301 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9302 if (range == 0) return false;
9303 if (direction < 0) {
9304 return offset > 0;
9305 } else {
9306 return offset < range - 1;
9307 }
9308 }
9309
9310 /**
9311 * Check if this view can be scrolled vertically in a certain direction.
9312 *
9313 * @param direction Negative to check scrolling up, positive to check scrolling down.
9314 * @return true if this view can be scrolled in the specified direction, false otherwise.
9315 */
9316 public boolean canScrollVertically(int direction) {
9317 final int offset = computeVerticalScrollOffset();
9318 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9319 if (range == 0) return false;
9320 if (direction < 0) {
9321 return offset > 0;
9322 } else {
9323 return offset < range - 1;
9324 }
9325 }
9326
9327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9329 * scrollbars are painted only if they have been awakened first.</p>
9330 *
9331 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009332 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009333 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009335 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009336 // scrollbars are drawn only when the animation is running
9337 final ScrollabilityCache cache = mScrollCache;
9338 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009339
Mike Cleronf116bf82009-09-27 19:14:12 -07009340 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009341
Mike Cleronf116bf82009-09-27 19:14:12 -07009342 if (state == ScrollabilityCache.OFF) {
9343 return;
9344 }
Joe Malin32736f02011-01-19 16:14:20 -08009345
Mike Cleronf116bf82009-09-27 19:14:12 -07009346 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009347
Mike Cleronf116bf82009-09-27 19:14:12 -07009348 if (state == ScrollabilityCache.FADING) {
9349 // We're fading -- get our fade interpolation
9350 if (cache.interpolatorValues == null) {
9351 cache.interpolatorValues = new float[1];
9352 }
Joe Malin32736f02011-01-19 16:14:20 -08009353
Mike Cleronf116bf82009-09-27 19:14:12 -07009354 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009355
Mike Cleronf116bf82009-09-27 19:14:12 -07009356 // Stops the animation if we're done
9357 if (cache.scrollBarInterpolator.timeToValues(values) ==
9358 Interpolator.Result.FREEZE_END) {
9359 cache.state = ScrollabilityCache.OFF;
9360 } else {
9361 cache.scrollBar.setAlpha(Math.round(values[0]));
9362 }
Joe Malin32736f02011-01-19 16:14:20 -08009363
9364 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009365 // drawing. We only want this when we're fading so that
9366 // we prevent excessive redraws
9367 invalidate = true;
9368 } else {
9369 // We're just on -- but we may have been fading before so
9370 // reset alpha
9371 cache.scrollBar.setAlpha(255);
9372 }
9373
Joe Malin32736f02011-01-19 16:14:20 -08009374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 final int viewFlags = mViewFlags;
9376
9377 final boolean drawHorizontalScrollBar =
9378 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9379 final boolean drawVerticalScrollBar =
9380 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9381 && !isVerticalScrollBarHidden();
9382
9383 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9384 final int width = mRight - mLeft;
9385 final int height = mBottom - mTop;
9386
9387 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388
Mike Reede8853fc2009-09-04 14:01:48 -04009389 final int scrollX = mScrollX;
9390 final int scrollY = mScrollY;
9391 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9392
Mike Cleronf116bf82009-09-27 19:14:12 -07009393 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009396 int size = scrollBar.getSize(false);
9397 if (size <= 0) {
9398 size = cache.scrollBarSize;
9399 }
9400
Mike Cleronf116bf82009-09-27 19:14:12 -07009401 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009402 computeHorizontalScrollOffset(),
9403 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009404 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009405 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009406 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009407 left = scrollX + (mPaddingLeft & inside);
9408 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9409 bottom = top + size;
9410 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9411 if (invalidate) {
9412 invalidate(left, top, right, bottom);
9413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009414 }
9415
9416 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009417 int size = scrollBar.getSize(true);
9418 if (size <= 0) {
9419 size = cache.scrollBarSize;
9420 }
9421
Mike Reede8853fc2009-09-04 14:01:48 -04009422 scrollBar.setParameters(computeVerticalScrollRange(),
9423 computeVerticalScrollOffset(),
9424 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009425 switch (mVerticalScrollbarPosition) {
9426 default:
9427 case SCROLLBAR_POSITION_DEFAULT:
9428 case SCROLLBAR_POSITION_RIGHT:
9429 left = scrollX + width - size - (mUserPaddingRight & inside);
9430 break;
9431 case SCROLLBAR_POSITION_LEFT:
9432 left = scrollX + (mUserPaddingLeft & inside);
9433 break;
9434 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009435 top = scrollY + (mPaddingTop & inside);
9436 right = left + size;
9437 bottom = scrollY + height - (mUserPaddingBottom & inside);
9438 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9439 if (invalidate) {
9440 invalidate(left, top, right, bottom);
9441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 }
9443 }
9444 }
9445 }
Romain Guy8506ab42009-06-11 17:35:47 -07009446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009448 * 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 -08009449 * FastScroller is visible.
9450 * @return whether to temporarily hide the vertical scrollbar
9451 * @hide
9452 */
9453 protected boolean isVerticalScrollBarHidden() {
9454 return false;
9455 }
9456
9457 /**
9458 * <p>Draw the horizontal scrollbar if
9459 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 * @param canvas the canvas on which to draw the scrollbar
9462 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 *
9464 * @see #isHorizontalScrollBarEnabled()
9465 * @see #computeHorizontalScrollRange()
9466 * @see #computeHorizontalScrollExtent()
9467 * @see #computeHorizontalScrollOffset()
9468 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009469 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009470 */
Romain Guy8fb95422010-08-17 18:38:51 -07009471 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9472 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009473 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009474 scrollBar.draw(canvas);
9475 }
Mike Reede8853fc2009-09-04 14:01:48 -04009476
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009478 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9479 * returns true.</p>
9480 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 * @param canvas the canvas on which to draw the scrollbar
9482 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 *
9484 * @see #isVerticalScrollBarEnabled()
9485 * @see #computeVerticalScrollRange()
9486 * @see #computeVerticalScrollExtent()
9487 * @see #computeVerticalScrollOffset()
9488 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009489 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 */
Romain Guy8fb95422010-08-17 18:38:51 -07009491 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9492 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009493 scrollBar.setBounds(l, t, r, b);
9494 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009495 }
9496
9497 /**
9498 * Implement this to do your drawing.
9499 *
9500 * @param canvas the canvas on which the background will be drawn
9501 */
9502 protected void onDraw(Canvas canvas) {
9503 }
9504
9505 /*
9506 * Caller is responsible for calling requestLayout if necessary.
9507 * (This allows addViewInLayout to not request a new layout.)
9508 */
9509 void assignParent(ViewParent parent) {
9510 if (mParent == null) {
9511 mParent = parent;
9512 } else if (parent == null) {
9513 mParent = null;
9514 } else {
9515 throw new RuntimeException("view " + this + " being added, but"
9516 + " it already has a parent");
9517 }
9518 }
9519
9520 /**
9521 * This is called when the view is attached to a window. At this point it
9522 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009523 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9524 * however it may be called any time before the first onDraw -- including
9525 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009526 *
9527 * @see #onDetachedFromWindow()
9528 */
9529 protected void onAttachedToWindow() {
9530 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9531 mParent.requestTransparentRegion(this);
9532 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009533 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9534 initialAwakenScrollBars();
9535 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9536 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009537 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009538 // Order is important here: LayoutDirection MUST be resolved before Padding
9539 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009540 resolveLayoutDirectionIfNeeded();
9541 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009542 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009543 if (isFocused()) {
9544 InputMethodManager imm = InputMethodManager.peekInstance();
9545 imm.focusIn(this);
9546 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009547 }
Cibu Johny86666632010-02-22 13:01:02 -08009548
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009549 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009550 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9551 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009552 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009553 private void resolveLayoutDirectionIfNeeded() {
9554 // Do not resolve if it is not needed
9555 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9556
9557 // Clear any previous layout direction resolution
9558 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9559
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009560 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9561 // TextDirectionHeuristic
9562 resetResolvedTextDirection();
9563
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009564 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009565 switch (getLayoutDirection()) {
9566 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009567 // We cannot do the resolution if there is no parent
9568 if (mParent == null) return;
9569
Cibu Johny86666632010-02-22 13:01:02 -08009570 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009571 if (mParent instanceof ViewGroup) {
9572 ViewGroup viewGroup = ((ViewGroup) mParent);
9573
9574 // Check if the parent view group can resolve
9575 if (! viewGroup.canResolveLayoutDirection()) {
9576 return;
9577 }
9578 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9579 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9580 }
Cibu Johny86666632010-02-22 13:01:02 -08009581 }
9582 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009583 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009584 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009585 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009586 case LAYOUT_DIRECTION_LOCALE:
9587 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009588 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009589 }
9590 break;
9591 default:
9592 // Nothing to do, LTR by default
9593 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009594
9595 // Set to resolved
9596 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9597 }
9598
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009599 /**
9600 * @hide
9601 */
9602 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009603 // If the user specified the absolute padding (either with android:padding or
9604 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9605 // use the default padding or the padding from the background drawable
9606 // (stored at this point in mPadding*)
9607 switch (getResolvedLayoutDirection()) {
9608 case LAYOUT_DIRECTION_RTL:
9609 // Start user padding override Right user padding. Otherwise, if Right user
9610 // padding is not defined, use the default Right padding. If Right user padding
9611 // is defined, just use it.
9612 if (mUserPaddingStart >= 0) {
9613 mUserPaddingRight = mUserPaddingStart;
9614 } else if (mUserPaddingRight < 0) {
9615 mUserPaddingRight = mPaddingRight;
9616 }
9617 if (mUserPaddingEnd >= 0) {
9618 mUserPaddingLeft = mUserPaddingEnd;
9619 } else if (mUserPaddingLeft < 0) {
9620 mUserPaddingLeft = mPaddingLeft;
9621 }
9622 break;
9623 case LAYOUT_DIRECTION_LTR:
9624 default:
9625 // Start user padding override Left user padding. Otherwise, if Left user
9626 // padding is not defined, use the default left padding. If Left user padding
9627 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009628 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009629 mUserPaddingLeft = mUserPaddingStart;
9630 } else if (mUserPaddingLeft < 0) {
9631 mUserPaddingLeft = mPaddingLeft;
9632 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009633 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009634 mUserPaddingRight = mUserPaddingEnd;
9635 } else if (mUserPaddingRight < 0) {
9636 mUserPaddingRight = mPaddingRight;
9637 }
9638 }
9639
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009640 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9641
9642 recomputePadding();
9643 }
9644
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009645 /**
9646 * Return true if layout direction resolution can be done
9647 *
9648 * @hide
9649 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009650 protected boolean canResolveLayoutDirection() {
9651 switch (getLayoutDirection()) {
9652 case LAYOUT_DIRECTION_INHERIT:
9653 return (mParent != null);
9654 default:
9655 return true;
9656 }
9657 }
9658
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009659 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009660 * Reset the resolved layout direction.
9661 *
9662 * Subclasses need to override this method to clear cached information that depends on the
9663 * resolved layout direction, or to inform child views that inherit their layout direction.
9664 * Overrides must also call the superclass implementation at the start of their implementation.
9665 *
9666 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009667 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009668 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009669 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009670 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009671 }
9672
9673 /**
9674 * Check if a Locale is corresponding to a RTL script.
9675 *
9676 * @param locale Locale to check
9677 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009678 *
9679 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009680 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009681 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009682 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9683 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009684 }
9685
9686 /**
9687 * This is called when the view is detached from a window. At this point it
9688 * no longer has a surface for drawing.
9689 *
9690 * @see #onAttachedToWindow()
9691 */
9692 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009693 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009694
Romain Guya440b002010-02-24 15:57:54 -08009695 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009696 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009697 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009698 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009701
Romain Guy6d7475d2011-07-27 16:28:21 -07009702 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009703
Chet Haasedaf98e92011-01-10 14:10:36 -08009704 if (mDisplayList != null) {
9705 mDisplayList.invalidate();
9706 }
9707
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009708 if (mAttachInfo != null) {
9709 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009710 }
9711
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009712 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009713
9714 resetResolvedLayoutDirection();
9715 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 }
9717
9718 /**
9719 * @return The number of times this view has been attached to a window
9720 */
9721 protected int getWindowAttachCount() {
9722 return mWindowAttachCount;
9723 }
9724
9725 /**
9726 * Retrieve a unique token identifying the window this view is attached to.
9727 * @return Return the window's token for use in
9728 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9729 */
9730 public IBinder getWindowToken() {
9731 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9732 }
9733
9734 /**
9735 * Retrieve a unique token identifying the top-level "real" window of
9736 * the window that this view is attached to. That is, this is like
9737 * {@link #getWindowToken}, except if the window this view in is a panel
9738 * window (attached to another containing window), then the token of
9739 * the containing window is returned instead.
9740 *
9741 * @return Returns the associated window token, either
9742 * {@link #getWindowToken()} or the containing window's token.
9743 */
9744 public IBinder getApplicationWindowToken() {
9745 AttachInfo ai = mAttachInfo;
9746 if (ai != null) {
9747 IBinder appWindowToken = ai.mPanelParentWindowToken;
9748 if (appWindowToken == null) {
9749 appWindowToken = ai.mWindowToken;
9750 }
9751 return appWindowToken;
9752 }
9753 return null;
9754 }
9755
9756 /**
9757 * Retrieve private session object this view hierarchy is using to
9758 * communicate with the window manager.
9759 * @return the session object to communicate with the window manager
9760 */
9761 /*package*/ IWindowSession getWindowSession() {
9762 return mAttachInfo != null ? mAttachInfo.mSession : null;
9763 }
9764
9765 /**
9766 * @param info the {@link android.view.View.AttachInfo} to associated with
9767 * this view
9768 */
9769 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9770 //System.out.println("Attached! " + this);
9771 mAttachInfo = info;
9772 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009773 // We will need to evaluate the drawable state at least once.
9774 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 if (mFloatingTreeObserver != null) {
9776 info.mTreeObserver.merge(mFloatingTreeObserver);
9777 mFloatingTreeObserver = null;
9778 }
9779 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9780 mAttachInfo.mScrollContainers.add(this);
9781 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9782 }
9783 performCollectViewAttributes(visibility);
9784 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009785
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009786 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009787 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009788 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009789 if (listeners != null && listeners.size() > 0) {
9790 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9791 // perform the dispatching. The iterator is a safe guard against listeners that
9792 // could mutate the list by calling the various add/remove methods. This prevents
9793 // the array from being modified while we iterate it.
9794 for (OnAttachStateChangeListener listener : listeners) {
9795 listener.onViewAttachedToWindow(this);
9796 }
9797 }
9798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 int vis = info.mWindowVisibility;
9800 if (vis != GONE) {
9801 onWindowVisibilityChanged(vis);
9802 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009803 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9804 // If nobody has evaluated the drawable state yet, then do it now.
9805 refreshDrawableState();
9806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 }
9808
9809 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 AttachInfo info = mAttachInfo;
9811 if (info != null) {
9812 int vis = info.mWindowVisibility;
9813 if (vis != GONE) {
9814 onWindowVisibilityChanged(GONE);
9815 }
9816 }
9817
9818 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009819
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009820 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009821 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009822 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009823 if (listeners != null && listeners.size() > 0) {
9824 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9825 // perform the dispatching. The iterator is a safe guard against listeners that
9826 // could mutate the list by calling the various add/remove methods. This prevents
9827 // the array from being modified while we iterate it.
9828 for (OnAttachStateChangeListener listener : listeners) {
9829 listener.onViewDetachedFromWindow(this);
9830 }
9831 }
9832
Romain Guy01d5edc2011-01-28 11:28:53 -08009833 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 mAttachInfo.mScrollContainers.remove(this);
9835 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9836 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 mAttachInfo = null;
9839 }
9840
9841 /**
9842 * Store this view hierarchy's frozen state into the given container.
9843 *
9844 * @param container The SparseArray in which to save the view's state.
9845 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009846 * @see #restoreHierarchyState(android.util.SparseArray)
9847 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9848 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 */
9850 public void saveHierarchyState(SparseArray<Parcelable> container) {
9851 dispatchSaveInstanceState(container);
9852 }
9853
9854 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009855 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9856 * this view and its children. May be overridden to modify how freezing happens to a
9857 * 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 -08009858 *
9859 * @param container The SparseArray in which to save the view's state.
9860 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009861 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9862 * @see #saveHierarchyState(android.util.SparseArray)
9863 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864 */
9865 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9866 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9867 mPrivateFlags &= ~SAVE_STATE_CALLED;
9868 Parcelable state = onSaveInstanceState();
9869 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9870 throw new IllegalStateException(
9871 "Derived class did not call super.onSaveInstanceState()");
9872 }
9873 if (state != null) {
9874 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9875 // + ": " + state);
9876 container.put(mID, state);
9877 }
9878 }
9879 }
9880
9881 /**
9882 * Hook allowing a view to generate a representation of its internal state
9883 * that can later be used to create a new instance with that same state.
9884 * This state should only contain information that is not persistent or can
9885 * not be reconstructed later. For example, you will never store your
9886 * current position on screen because that will be computed again when a
9887 * new instance of the view is placed in its view hierarchy.
9888 * <p>
9889 * Some examples of things you may store here: the current cursor position
9890 * in a text view (but usually not the text itself since that is stored in a
9891 * content provider or other persistent storage), the currently selected
9892 * item in a list view.
9893 *
9894 * @return Returns a Parcelable object containing the view's current dynamic
9895 * state, or null if there is nothing interesting to save. The
9896 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009897 * @see #onRestoreInstanceState(android.os.Parcelable)
9898 * @see #saveHierarchyState(android.util.SparseArray)
9899 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 * @see #setSaveEnabled(boolean)
9901 */
9902 protected Parcelable onSaveInstanceState() {
9903 mPrivateFlags |= SAVE_STATE_CALLED;
9904 return BaseSavedState.EMPTY_STATE;
9905 }
9906
9907 /**
9908 * Restore this view hierarchy's frozen state from the given container.
9909 *
9910 * @param container The SparseArray which holds previously frozen states.
9911 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009912 * @see #saveHierarchyState(android.util.SparseArray)
9913 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9914 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 */
9916 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9917 dispatchRestoreInstanceState(container);
9918 }
9919
9920 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009921 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9922 * state for this view and its children. May be overridden to modify how restoring
9923 * happens to a view's children; for example, some views may want to not store state
9924 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 *
9926 * @param container The SparseArray which holds previously saved state.
9927 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009928 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9929 * @see #restoreHierarchyState(android.util.SparseArray)
9930 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 */
9932 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9933 if (mID != NO_ID) {
9934 Parcelable state = container.get(mID);
9935 if (state != null) {
9936 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9937 // + ": " + state);
9938 mPrivateFlags &= ~SAVE_STATE_CALLED;
9939 onRestoreInstanceState(state);
9940 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9941 throw new IllegalStateException(
9942 "Derived class did not call super.onRestoreInstanceState()");
9943 }
9944 }
9945 }
9946 }
9947
9948 /**
9949 * Hook allowing a view to re-apply a representation of its internal state that had previously
9950 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9951 * null state.
9952 *
9953 * @param state The frozen state that had previously been returned by
9954 * {@link #onSaveInstanceState}.
9955 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009956 * @see #onSaveInstanceState()
9957 * @see #restoreHierarchyState(android.util.SparseArray)
9958 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 */
9960 protected void onRestoreInstanceState(Parcelable state) {
9961 mPrivateFlags |= SAVE_STATE_CALLED;
9962 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009963 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9964 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009965 + "when two views of different type have the same id in the same hierarchy. "
9966 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009967 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 }
9969 }
9970
9971 /**
9972 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9973 *
9974 * @return the drawing start time in milliseconds
9975 */
9976 public long getDrawingTime() {
9977 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9978 }
9979
9980 /**
9981 * <p>Enables or disables the duplication of the parent's state into this view. When
9982 * duplication is enabled, this view gets its drawable state from its parent rather
9983 * than from its own internal properties.</p>
9984 *
9985 * <p>Note: in the current implementation, setting this property to true after the
9986 * view was added to a ViewGroup might have no effect at all. This property should
9987 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9988 *
9989 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9990 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009991 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009992 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9993 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009994 *
9995 * @param enabled True to enable duplication of the parent's drawable state, false
9996 * to disable it.
9997 *
9998 * @see #getDrawableState()
9999 * @see #isDuplicateParentStateEnabled()
10000 */
10001 public void setDuplicateParentStateEnabled(boolean enabled) {
10002 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10003 }
10004
10005 /**
10006 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10007 *
10008 * @return True if this view's drawable state is duplicated from the parent,
10009 * false otherwise
10010 *
10011 * @see #getDrawableState()
10012 * @see #setDuplicateParentStateEnabled(boolean)
10013 */
10014 public boolean isDuplicateParentStateEnabled() {
10015 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10016 }
10017
10018 /**
Romain Guy171c5922011-01-06 10:04:23 -080010019 * <p>Specifies the type of layer backing this view. The layer can be
10020 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10021 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010022 *
Romain Guy171c5922011-01-06 10:04:23 -080010023 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10024 * instance that controls how the layer is composed on screen. The following
10025 * properties of the paint are taken into account when composing the layer:</p>
10026 * <ul>
10027 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10028 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10029 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10030 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010031 *
Romain Guy171c5922011-01-06 10:04:23 -080010032 * <p>If this view has an alpha value set to < 1.0 by calling
10033 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10034 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10035 * equivalent to setting a hardware layer on this view and providing a paint with
10036 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010037 *
Romain Guy171c5922011-01-06 10:04:23 -080010038 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10039 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10040 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010041 *
Romain Guy171c5922011-01-06 10:04:23 -080010042 * @param layerType The ype of layer to use with this view, must be one of
10043 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10044 * {@link #LAYER_TYPE_HARDWARE}
10045 * @param paint The paint used to compose the layer. This argument is optional
10046 * and can be null. It is ignored when the layer type is
10047 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010048 *
10049 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010050 * @see #LAYER_TYPE_NONE
10051 * @see #LAYER_TYPE_SOFTWARE
10052 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010053 * @see #setAlpha(float)
10054 *
Romain Guy171c5922011-01-06 10:04:23 -080010055 * @attr ref android.R.styleable#View_layerType
10056 */
10057 public void setLayerType(int layerType, Paint paint) {
10058 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010059 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010060 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10061 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010062
Romain Guyd6cd5722011-01-17 14:42:41 -080010063 if (layerType == mLayerType) {
10064 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10065 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010066 invalidateParentCaches();
10067 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010068 }
10069 return;
10070 }
Romain Guy171c5922011-01-06 10:04:23 -080010071
10072 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010073 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010074 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010075 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010076 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010077 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010078 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010079 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010080 default:
10081 break;
Romain Guy171c5922011-01-06 10:04:23 -080010082 }
10083
10084 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010085 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10086 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10087 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010088
Romain Guy0fd89bf2011-01-26 15:41:30 -080010089 invalidateParentCaches();
10090 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010091 }
10092
10093 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010094 * Indicates whether this view has a static layer. A view with layer type
10095 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10096 * dynamic.
10097 */
10098 boolean hasStaticLayer() {
10099 return mLayerType == LAYER_TYPE_NONE;
10100 }
10101
10102 /**
Romain Guy171c5922011-01-06 10:04:23 -080010103 * Indicates what type of layer is currently associated with this view. By default
10104 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10105 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10106 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010107 *
Romain Guy171c5922011-01-06 10:04:23 -080010108 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10109 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010110 *
10111 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010112 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010113 * @see #LAYER_TYPE_NONE
10114 * @see #LAYER_TYPE_SOFTWARE
10115 * @see #LAYER_TYPE_HARDWARE
10116 */
10117 public int getLayerType() {
10118 return mLayerType;
10119 }
Joe Malin32736f02011-01-19 16:14:20 -080010120
Romain Guy6c319ca2011-01-11 14:29:25 -080010121 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010122 * Forces this view's layer to be created and this view to be rendered
10123 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10124 * invoking this method will have no effect.
10125 *
10126 * This method can for instance be used to render a view into its layer before
10127 * starting an animation. If this view is complex, rendering into the layer
10128 * before starting the animation will avoid skipping frames.
10129 *
10130 * @throws IllegalStateException If this view is not attached to a window
10131 *
10132 * @see #setLayerType(int, android.graphics.Paint)
10133 */
10134 public void buildLayer() {
10135 if (mLayerType == LAYER_TYPE_NONE) return;
10136
10137 if (mAttachInfo == null) {
10138 throw new IllegalStateException("This view must be attached to a window first");
10139 }
10140
10141 switch (mLayerType) {
10142 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010143 if (mAttachInfo.mHardwareRenderer != null &&
10144 mAttachInfo.mHardwareRenderer.isEnabled() &&
10145 mAttachInfo.mHardwareRenderer.validate()) {
10146 getHardwareLayer();
10147 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010148 break;
10149 case LAYER_TYPE_SOFTWARE:
10150 buildDrawingCache(true);
10151 break;
10152 }
10153 }
10154
10155 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010156 * <p>Returns a hardware layer that can be used to draw this view again
10157 * without executing its draw method.</p>
10158 *
10159 * @return A HardwareLayer ready to render, or null if an error occurred.
10160 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010161 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010162 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10163 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010164 return null;
10165 }
10166
10167 final int width = mRight - mLeft;
10168 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010169
Romain Guy6c319ca2011-01-11 14:29:25 -080010170 if (width == 0 || height == 0) {
10171 return null;
10172 }
10173
10174 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10175 if (mHardwareLayer == null) {
10176 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10177 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010178 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010179 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10180 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010181 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010182 }
10183
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010184 // The layer is not valid if the underlying GPU resources cannot be allocated
10185 if (!mHardwareLayer.isValid()) {
10186 return null;
10187 }
10188
Romain Guy59a12ca2011-06-09 17:48:21 -070010189 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010190 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010191
10192 // Make sure all the GPU resources have been properly allocated
10193 if (canvas == null) {
10194 mHardwareLayer.end(currentCanvas);
10195 return null;
10196 }
10197
Romain Guy5e7f7662011-01-24 22:35:56 -080010198 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010199 try {
10200 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010201 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010202 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010203
Chet Haase88172fe2011-03-07 17:36:33 -080010204 final int restoreCount = canvas.save();
10205
Romain Guy6c319ca2011-01-11 14:29:25 -080010206 computeScroll();
10207 canvas.translate(-mScrollX, -mScrollY);
10208
Romain Guy6c319ca2011-01-11 14:29:25 -080010209 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010210
Romain Guy6c319ca2011-01-11 14:29:25 -080010211 // Fast path for layouts with no backgrounds
10212 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10213 mPrivateFlags &= ~DIRTY_MASK;
10214 dispatchDraw(canvas);
10215 } else {
10216 draw(canvas);
10217 }
Joe Malin32736f02011-01-19 16:14:20 -080010218
Chet Haase88172fe2011-03-07 17:36:33 -080010219 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010220 } finally {
10221 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010222 mHardwareLayer.end(currentCanvas);
10223 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010224 }
10225 }
10226
10227 return mHardwareLayer;
10228 }
Romain Guy171c5922011-01-06 10:04:23 -080010229
Romain Guy589b0bb2011-10-10 13:57:47 -070010230 /**
10231 * Destroys this View's hardware layer if possible.
10232 *
10233 * @return True if the layer was destroyed, false otherwise.
10234 *
10235 * @see #setLayerType(int, android.graphics.Paint)
10236 * @see #LAYER_TYPE_HARDWARE
10237 */
Romain Guy65b345f2011-07-27 18:51:50 -070010238 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010239 if (mHardwareLayer != null) {
Dave Burke70775062011-11-14 11:39:30 -080010240 mHardwareLayer.destroy();
10241 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010242
10243 invalidate(true);
10244 invalidateParentCaches();
10245
Romain Guy65b345f2011-07-27 18:51:50 -070010246 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010247 }
Romain Guy65b345f2011-07-27 18:51:50 -070010248 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010249 }
10250
Romain Guy171c5922011-01-06 10:04:23 -080010251 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010252 * Destroys all hardware rendering resources. This method is invoked
10253 * when the system needs to reclaim resources. Upon execution of this
10254 * method, you should free any OpenGL resources created by the view.
10255 *
10256 * Note: you <strong>must</strong> call
10257 * <code>super.destroyHardwareResources()</code> when overriding
10258 * this method.
10259 *
10260 * @hide
10261 */
10262 protected void destroyHardwareResources() {
10263 destroyLayer();
10264 }
10265
10266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10268 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10269 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10270 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10271 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10272 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010273 *
Romain Guy171c5922011-01-06 10:04:23 -080010274 * <p>Enabling the drawing cache is similar to
10275 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010276 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10277 * drawing cache has no effect on rendering because the system uses a different mechanism
10278 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10279 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10280 * for information on how to enable software and hardware layers.</p>
10281 *
10282 * <p>This API can be used to manually generate
10283 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10284 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 *
10286 * @param enabled true to enable the drawing cache, false otherwise
10287 *
10288 * @see #isDrawingCacheEnabled()
10289 * @see #getDrawingCache()
10290 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010291 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010292 */
10293 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010294 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10296 }
10297
10298 /**
10299 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10300 *
10301 * @return true if the drawing cache is enabled
10302 *
10303 * @see #setDrawingCacheEnabled(boolean)
10304 * @see #getDrawingCache()
10305 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010306 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 public boolean isDrawingCacheEnabled() {
10308 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10309 }
10310
10311 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010312 * Debugging utility which recursively outputs the dirty state of a view and its
10313 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010314 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010315 * @hide
10316 */
Romain Guy676b1732011-02-14 14:45:33 -080010317 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010318 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10319 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10320 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10321 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10322 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10323 if (clear) {
10324 mPrivateFlags &= clearMask;
10325 }
10326 if (this instanceof ViewGroup) {
10327 ViewGroup parent = (ViewGroup) this;
10328 final int count = parent.getChildCount();
10329 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010330 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010331 child.outputDirtyFlags(indent + " ", clear, clearMask);
10332 }
10333 }
10334 }
10335
10336 /**
10337 * This method is used by ViewGroup to cause its children to restore or recreate their
10338 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10339 * to recreate its own display list, which would happen if it went through the normal
10340 * draw/dispatchDraw mechanisms.
10341 *
10342 * @hide
10343 */
10344 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010345
10346 /**
10347 * A view that is not attached or hardware accelerated cannot create a display list.
10348 * This method checks these conditions and returns the appropriate result.
10349 *
10350 * @return true if view has the ability to create a display list, false otherwise.
10351 *
10352 * @hide
10353 */
10354 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010355 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010356 }
Joe Malin32736f02011-01-19 16:14:20 -080010357
Chet Haasedaf98e92011-01-10 14:10:36 -080010358 /**
Romain Guyb051e892010-09-28 19:09:36 -070010359 * <p>Returns a display list that can be used to draw this view again
10360 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010361 *
Romain Guyb051e892010-09-28 19:09:36 -070010362 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010363 *
10364 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010365 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010366 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010367 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010368 return null;
10369 }
10370
Chet Haasedaf98e92011-01-10 14:10:36 -080010371 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10372 mDisplayList == null || !mDisplayList.isValid() ||
10373 mRecreateDisplayList)) {
10374 // Don't need to recreate the display list, just need to tell our
10375 // children to restore/recreate theirs
10376 if (mDisplayList != null && mDisplayList.isValid() &&
10377 !mRecreateDisplayList) {
10378 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10379 mPrivateFlags &= ~DIRTY_MASK;
10380 dispatchGetDisplayList();
10381
10382 return mDisplayList;
10383 }
10384
10385 // If we got here, we're recreating it. Mark it as such to ensure that
10386 // we copy in child display lists into ours in drawChild()
10387 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010388 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010389 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010390 // If we're creating a new display list, make sure our parent gets invalidated
10391 // since they will need to recreate their display list to account for this
10392 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010393 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010394 }
Romain Guyb051e892010-09-28 19:09:36 -070010395
10396 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010397 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010398 try {
10399 int width = mRight - mLeft;
10400 int height = mBottom - mTop;
10401
10402 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010403 // The dirty rect should always be null for a display list
10404 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010405
Chet Haasedaf98e92011-01-10 14:10:36 -080010406 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010407
10408 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010409 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010410 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010411 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010412
Romain Guyb051e892010-09-28 19:09:36 -070010413 // Fast path for layouts with no backgrounds
10414 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010415 dispatchDraw(canvas);
10416 } else {
10417 draw(canvas);
10418 }
Romain Guyb051e892010-09-28 19:09:36 -070010419 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010420 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010421 canvas.onPostDraw();
10422
10423 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010424 }
Chet Haase77785f92011-01-25 23:22:09 -080010425 } else {
10426 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10427 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010428 }
10429
10430 return mDisplayList;
10431 }
10432
10433 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010434 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010435 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010436 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010437 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010438 * @see #getDrawingCache(boolean)
10439 */
10440 public Bitmap getDrawingCache() {
10441 return getDrawingCache(false);
10442 }
10443
10444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10446 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10447 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10448 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10449 * request the drawing cache by calling this method and draw it on screen if the
10450 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010451 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010452 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10453 * this method will create a bitmap of the same size as this view. Because this bitmap
10454 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10455 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10456 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10457 * size than the view. This implies that your application must be able to handle this
10458 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010459 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010460 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10461 * the current density of the screen when the application is in compatibility
10462 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010464 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 * @see #setDrawingCacheEnabled(boolean)
10467 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010468 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 * @see #destroyDrawingCache()
10470 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010471 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10473 return null;
10474 }
10475 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010476 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 }
Romain Guy02890fd2010-08-06 17:58:44 -070010478 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 }
10480
10481 /**
10482 * <p>Frees the resources used by the drawing cache. If you call
10483 * {@link #buildDrawingCache()} manually without calling
10484 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10485 * should cleanup the cache with this method afterwards.</p>
10486 *
10487 * @see #setDrawingCacheEnabled(boolean)
10488 * @see #buildDrawingCache()
10489 * @see #getDrawingCache()
10490 */
10491 public void destroyDrawingCache() {
10492 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010493 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 mDrawingCache = null;
10495 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010496 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010497 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010498 mUnscaledDrawingCache = null;
10499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 }
10501
10502 /**
10503 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010504 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 * view will always be drawn on top of a solid color.
10506 *
10507 * @param color The background color to use for the drawing cache's bitmap
10508 *
10509 * @see #setDrawingCacheEnabled(boolean)
10510 * @see #buildDrawingCache()
10511 * @see #getDrawingCache()
10512 */
10513 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010514 if (color != mDrawingCacheBackgroundColor) {
10515 mDrawingCacheBackgroundColor = color;
10516 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010518 }
10519
10520 /**
10521 * @see #setDrawingCacheBackgroundColor(int)
10522 *
10523 * @return The background color to used for the drawing cache's bitmap
10524 */
10525 public int getDrawingCacheBackgroundColor() {
10526 return mDrawingCacheBackgroundColor;
10527 }
10528
10529 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010530 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010531 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010532 * @see #buildDrawingCache(boolean)
10533 */
10534 public void buildDrawingCache() {
10535 buildDrawingCache(false);
10536 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010537
Romain Guyfbd8f692009-06-26 14:51:58 -070010538 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10540 *
10541 * <p>If you call {@link #buildDrawingCache()} manually without calling
10542 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10543 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010544 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010545 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10546 * this method will create a bitmap of the same size as this view. Because this bitmap
10547 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10548 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10549 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10550 * size than the view. This implies that your application must be able to handle this
10551 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010552 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010553 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10554 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010555 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010556 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 *
10558 * @see #getDrawingCache()
10559 * @see #destroyDrawingCache()
10560 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010561 public void buildDrawingCache(boolean autoScale) {
10562 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010563 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010564 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565
10566 if (ViewDebug.TRACE_HIERARCHY) {
10567 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569
Romain Guy8506ab42009-06-11 17:35:47 -070010570 int width = mRight - mLeft;
10571 int height = mBottom - mTop;
10572
10573 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010574 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010575
Romain Guye1123222009-06-29 14:24:56 -070010576 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010577 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10578 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580
10581 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010582 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010583 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010584
10585 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010586 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010587 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010588 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10589 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010590 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 return;
10592 }
10593
10594 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010595 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596
10597 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 Bitmap.Config quality;
10599 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010600 // Never pick ARGB_4444 because it looks awful
10601 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10603 case DRAWING_CACHE_QUALITY_AUTO:
10604 quality = Bitmap.Config.ARGB_8888;
10605 break;
10606 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010607 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 break;
10609 case DRAWING_CACHE_QUALITY_HIGH:
10610 quality = Bitmap.Config.ARGB_8888;
10611 break;
10612 default:
10613 quality = Bitmap.Config.ARGB_8888;
10614 break;
10615 }
10616 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010617 // Optimization for translucent windows
10618 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010619 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 }
10621
10622 // Try to cleanup memory
10623 if (bitmap != null) bitmap.recycle();
10624
10625 try {
10626 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010627 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010628 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010629 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010630 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010631 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010632 }
Adam Powell26153a32010-11-08 15:22:27 -080010633 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 } catch (OutOfMemoryError e) {
10635 // If there is not enough memory to create the bitmap cache, just
10636 // ignore the issue as bitmap caches are not required to draw the
10637 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010638 if (autoScale) {
10639 mDrawingCache = null;
10640 } else {
10641 mUnscaledDrawingCache = null;
10642 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010643 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 return;
10645 }
10646
10647 clear = drawingCacheBackgroundColor != 0;
10648 }
10649
10650 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 if (attachInfo != null) {
10652 canvas = attachInfo.mCanvas;
10653 if (canvas == null) {
10654 canvas = new Canvas();
10655 }
10656 canvas.setBitmap(bitmap);
10657 // Temporarily clobber the cached Canvas in case one of our children
10658 // is also using a drawing cache. Without this, the children would
10659 // steal the canvas by attaching their own bitmap to it and bad, bad
10660 // thing would happen (invisible views, corrupted drawings, etc.)
10661 attachInfo.mCanvas = null;
10662 } else {
10663 // This case should hopefully never or seldom happen
10664 canvas = new Canvas(bitmap);
10665 }
10666
10667 if (clear) {
10668 bitmap.eraseColor(drawingCacheBackgroundColor);
10669 }
10670
10671 computeScroll();
10672 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010673
Romain Guye1123222009-06-29 14:24:56 -070010674 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010675 final float scale = attachInfo.mApplicationScale;
10676 canvas.scale(scale, scale);
10677 }
Joe Malin32736f02011-01-19 16:14:20 -080010678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 canvas.translate(-mScrollX, -mScrollY);
10680
Romain Guy5bcdff42009-05-14 21:27:18 -070010681 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010682 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10683 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010684 mPrivateFlags |= DRAWING_CACHE_VALID;
10685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686
10687 // Fast path for layouts with no backgrounds
10688 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10689 if (ViewDebug.TRACE_HIERARCHY) {
10690 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10691 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010692 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 dispatchDraw(canvas);
10694 } else {
10695 draw(canvas);
10696 }
10697
10698 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010699 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700
10701 if (attachInfo != null) {
10702 // Restore the cached Canvas for our siblings
10703 attachInfo.mCanvas = canvas;
10704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 }
10706 }
10707
10708 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010709 * Create a snapshot of the view into a bitmap. We should probably make
10710 * some form of this public, but should think about the API.
10711 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010712 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010713 int width = mRight - mLeft;
10714 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010715
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010716 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010717 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010718 width = (int) ((width * scale) + 0.5f);
10719 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010720
Romain Guy8c11e312009-09-14 15:15:30 -070010721 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010722 if (bitmap == null) {
10723 throw new OutOfMemoryError();
10724 }
10725
Romain Guyc529d8d2011-09-06 15:01:39 -070010726 Resources resources = getResources();
10727 if (resources != null) {
10728 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10729 }
Joe Malin32736f02011-01-19 16:14:20 -080010730
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010731 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010732 if (attachInfo != null) {
10733 canvas = attachInfo.mCanvas;
10734 if (canvas == null) {
10735 canvas = new Canvas();
10736 }
10737 canvas.setBitmap(bitmap);
10738 // Temporarily clobber the cached Canvas in case one of our children
10739 // is also using a drawing cache. Without this, the children would
10740 // steal the canvas by attaching their own bitmap to it and bad, bad
10741 // things would happen (invisible views, corrupted drawings, etc.)
10742 attachInfo.mCanvas = null;
10743 } else {
10744 // This case should hopefully never or seldom happen
10745 canvas = new Canvas(bitmap);
10746 }
10747
Romain Guy5bcdff42009-05-14 21:27:18 -070010748 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010749 bitmap.eraseColor(backgroundColor);
10750 }
10751
10752 computeScroll();
10753 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010754 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010755 canvas.translate(-mScrollX, -mScrollY);
10756
Romain Guy5bcdff42009-05-14 21:27:18 -070010757 // Temporarily remove the dirty mask
10758 int flags = mPrivateFlags;
10759 mPrivateFlags &= ~DIRTY_MASK;
10760
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010761 // Fast path for layouts with no backgrounds
10762 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10763 dispatchDraw(canvas);
10764 } else {
10765 draw(canvas);
10766 }
10767
Romain Guy5bcdff42009-05-14 21:27:18 -070010768 mPrivateFlags = flags;
10769
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010770 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010771 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010772
10773 if (attachInfo != null) {
10774 // Restore the cached Canvas for our siblings
10775 attachInfo.mCanvas = canvas;
10776 }
Romain Guy8506ab42009-06-11 17:35:47 -070010777
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010778 return bitmap;
10779 }
10780
10781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 * Indicates whether this View is currently in edit mode. A View is usually
10783 * in edit mode when displayed within a developer tool. For instance, if
10784 * this View is being drawn by a visual user interface builder, this method
10785 * should return true.
10786 *
10787 * Subclasses should check the return value of this method to provide
10788 * different behaviors if their normal behavior might interfere with the
10789 * host environment. For instance: the class spawns a thread in its
10790 * constructor, the drawing code relies on device-specific features, etc.
10791 *
10792 * This method is usually checked in the drawing code of custom widgets.
10793 *
10794 * @return True if this View is in edit mode, false otherwise.
10795 */
10796 public boolean isInEditMode() {
10797 return false;
10798 }
10799
10800 /**
10801 * If the View draws content inside its padding and enables fading edges,
10802 * it needs to support padding offsets. Padding offsets are added to the
10803 * fading edges to extend the length of the fade so that it covers pixels
10804 * drawn inside the padding.
10805 *
10806 * Subclasses of this class should override this method if they need
10807 * to draw content inside the padding.
10808 *
10809 * @return True if padding offset must be applied, false otherwise.
10810 *
10811 * @see #getLeftPaddingOffset()
10812 * @see #getRightPaddingOffset()
10813 * @see #getTopPaddingOffset()
10814 * @see #getBottomPaddingOffset()
10815 *
10816 * @since CURRENT
10817 */
10818 protected boolean isPaddingOffsetRequired() {
10819 return false;
10820 }
10821
10822 /**
10823 * Amount by which to extend the left fading region. Called only when
10824 * {@link #isPaddingOffsetRequired()} returns true.
10825 *
10826 * @return The left padding offset in pixels.
10827 *
10828 * @see #isPaddingOffsetRequired()
10829 *
10830 * @since CURRENT
10831 */
10832 protected int getLeftPaddingOffset() {
10833 return 0;
10834 }
10835
10836 /**
10837 * Amount by which to extend the right fading region. Called only when
10838 * {@link #isPaddingOffsetRequired()} returns true.
10839 *
10840 * @return The right padding offset in pixels.
10841 *
10842 * @see #isPaddingOffsetRequired()
10843 *
10844 * @since CURRENT
10845 */
10846 protected int getRightPaddingOffset() {
10847 return 0;
10848 }
10849
10850 /**
10851 * Amount by which to extend the top fading region. Called only when
10852 * {@link #isPaddingOffsetRequired()} returns true.
10853 *
10854 * @return The top padding offset in pixels.
10855 *
10856 * @see #isPaddingOffsetRequired()
10857 *
10858 * @since CURRENT
10859 */
10860 protected int getTopPaddingOffset() {
10861 return 0;
10862 }
10863
10864 /**
10865 * Amount by which to extend the bottom fading region. Called only when
10866 * {@link #isPaddingOffsetRequired()} returns true.
10867 *
10868 * @return The bottom padding offset in pixels.
10869 *
10870 * @see #isPaddingOffsetRequired()
10871 *
10872 * @since CURRENT
10873 */
10874 protected int getBottomPaddingOffset() {
10875 return 0;
10876 }
10877
10878 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010879 * @hide
10880 * @param offsetRequired
10881 */
10882 protected int getFadeTop(boolean offsetRequired) {
10883 int top = mPaddingTop;
10884 if (offsetRequired) top += getTopPaddingOffset();
10885 return top;
10886 }
10887
10888 /**
10889 * @hide
10890 * @param offsetRequired
10891 */
10892 protected int getFadeHeight(boolean offsetRequired) {
10893 int padding = mPaddingTop;
10894 if (offsetRequired) padding += getTopPaddingOffset();
10895 return mBottom - mTop - mPaddingBottom - padding;
10896 }
10897
10898 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010899 * <p>Indicates whether this view is attached to an hardware accelerated
10900 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010901 *
Romain Guy2bffd262010-09-12 17:40:02 -070010902 * <p>Even if this method returns true, it does not mean that every call
10903 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10904 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10905 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10906 * window is hardware accelerated,
10907 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10908 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010909 *
Romain Guy2bffd262010-09-12 17:40:02 -070010910 * @return True if the view is attached to a window and the window is
10911 * hardware accelerated; false in any other case.
10912 */
10913 public boolean isHardwareAccelerated() {
10914 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10915 }
Joe Malin32736f02011-01-19 16:14:20 -080010916
Romain Guy2bffd262010-09-12 17:40:02 -070010917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010918 * Manually render this view (and all of its children) to the given Canvas.
10919 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010920 * called. When implementing a view, implement
10921 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10922 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010923 *
10924 * @param canvas The Canvas to which the View is rendered.
10925 */
10926 public void draw(Canvas canvas) {
10927 if (ViewDebug.TRACE_HIERARCHY) {
10928 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10929 }
10930
Romain Guy5bcdff42009-05-14 21:27:18 -070010931 final int privateFlags = mPrivateFlags;
10932 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10933 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10934 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010936 /*
10937 * Draw traversal performs several drawing steps which must be executed
10938 * in the appropriate order:
10939 *
10940 * 1. Draw the background
10941 * 2. If necessary, save the canvas' layers to prepare for fading
10942 * 3. Draw view's content
10943 * 4. Draw children
10944 * 5. If necessary, draw the fading edges and restore layers
10945 * 6. Draw decorations (scrollbars for instance)
10946 */
10947
10948 // Step 1, draw the background, if needed
10949 int saveCount;
10950
Romain Guy24443ea2009-05-11 11:56:30 -070010951 if (!dirtyOpaque) {
10952 final Drawable background = mBGDrawable;
10953 if (background != null) {
10954 final int scrollX = mScrollX;
10955 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956
Romain Guy24443ea2009-05-11 11:56:30 -070010957 if (mBackgroundSizeChanged) {
10958 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10959 mBackgroundSizeChanged = false;
10960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961
Romain Guy24443ea2009-05-11 11:56:30 -070010962 if ((scrollX | scrollY) == 0) {
10963 background.draw(canvas);
10964 } else {
10965 canvas.translate(scrollX, scrollY);
10966 background.draw(canvas);
10967 canvas.translate(-scrollX, -scrollY);
10968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010969 }
10970 }
10971
10972 // skip step 2 & 5 if possible (common case)
10973 final int viewFlags = mViewFlags;
10974 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10975 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10976 if (!verticalEdges && !horizontalEdges) {
10977 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010978 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010979
10980 // Step 4, draw the children
10981 dispatchDraw(canvas);
10982
10983 // Step 6, draw decorations (scrollbars)
10984 onDrawScrollBars(canvas);
10985
10986 // we're done...
10987 return;
10988 }
10989
10990 /*
10991 * Here we do the full fledged routine...
10992 * (this is an uncommon case where speed matters less,
10993 * this is why we repeat some of the tests that have been
10994 * done above)
10995 */
10996
10997 boolean drawTop = false;
10998 boolean drawBottom = false;
10999 boolean drawLeft = false;
11000 boolean drawRight = false;
11001
11002 float topFadeStrength = 0.0f;
11003 float bottomFadeStrength = 0.0f;
11004 float leftFadeStrength = 0.0f;
11005 float rightFadeStrength = 0.0f;
11006
11007 // Step 2, save the canvas' layers
11008 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009
11010 final boolean offsetRequired = isPaddingOffsetRequired();
11011 if (offsetRequired) {
11012 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 }
11014
11015 int left = mScrollX + paddingLeft;
11016 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011017 int top = mScrollY + getFadeTop(offsetRequired);
11018 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011019
11020 if (offsetRequired) {
11021 right += getRightPaddingOffset();
11022 bottom += getBottomPaddingOffset();
11023 }
11024
11025 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011026 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11027 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028
11029 // clip the fade length if top and bottom fades overlap
11030 // overlapping fades produce odd-looking artifacts
11031 if (verticalEdges && (top + length > bottom - length)) {
11032 length = (bottom - top) / 2;
11033 }
11034
11035 // also clip horizontal fades if necessary
11036 if (horizontalEdges && (left + length > right - length)) {
11037 length = (right - left) / 2;
11038 }
11039
11040 if (verticalEdges) {
11041 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011042 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011043 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011044 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011045 }
11046
11047 if (horizontalEdges) {
11048 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011049 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011050 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011051 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052 }
11053
11054 saveCount = canvas.getSaveCount();
11055
11056 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011057 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011058 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11059
11060 if (drawTop) {
11061 canvas.saveLayer(left, top, right, top + length, null, flags);
11062 }
11063
11064 if (drawBottom) {
11065 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11066 }
11067
11068 if (drawLeft) {
11069 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11070 }
11071
11072 if (drawRight) {
11073 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11074 }
11075 } else {
11076 scrollabilityCache.setFadeColor(solidColor);
11077 }
11078
11079 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011080 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081
11082 // Step 4, draw the children
11083 dispatchDraw(canvas);
11084
11085 // Step 5, draw the fade effect and restore layers
11086 final Paint p = scrollabilityCache.paint;
11087 final Matrix matrix = scrollabilityCache.matrix;
11088 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089
11090 if (drawTop) {
11091 matrix.setScale(1, fadeHeight * topFadeStrength);
11092 matrix.postTranslate(left, top);
11093 fade.setLocalMatrix(matrix);
11094 canvas.drawRect(left, top, right, top + length, p);
11095 }
11096
11097 if (drawBottom) {
11098 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11099 matrix.postRotate(180);
11100 matrix.postTranslate(left, bottom);
11101 fade.setLocalMatrix(matrix);
11102 canvas.drawRect(left, bottom - length, right, bottom, p);
11103 }
11104
11105 if (drawLeft) {
11106 matrix.setScale(1, fadeHeight * leftFadeStrength);
11107 matrix.postRotate(-90);
11108 matrix.postTranslate(left, top);
11109 fade.setLocalMatrix(matrix);
11110 canvas.drawRect(left, top, left + length, bottom, p);
11111 }
11112
11113 if (drawRight) {
11114 matrix.setScale(1, fadeHeight * rightFadeStrength);
11115 matrix.postRotate(90);
11116 matrix.postTranslate(right, top);
11117 fade.setLocalMatrix(matrix);
11118 canvas.drawRect(right - length, top, right, bottom, p);
11119 }
11120
11121 canvas.restoreToCount(saveCount);
11122
11123 // Step 6, draw decorations (scrollbars)
11124 onDrawScrollBars(canvas);
11125 }
11126
11127 /**
11128 * Override this if your view is known to always be drawn on top of a solid color background,
11129 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11130 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11131 * should be set to 0xFF.
11132 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011133 * @see #setVerticalFadingEdgeEnabled(boolean)
11134 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011135 *
11136 * @return The known solid color background for this view, or 0 if the color may vary
11137 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011138 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 public int getSolidColor() {
11140 return 0;
11141 }
11142
11143 /**
11144 * Build a human readable string representation of the specified view flags.
11145 *
11146 * @param flags the view flags to convert to a string
11147 * @return a String representing the supplied flags
11148 */
11149 private static String printFlags(int flags) {
11150 String output = "";
11151 int numFlags = 0;
11152 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11153 output += "TAKES_FOCUS";
11154 numFlags++;
11155 }
11156
11157 switch (flags & VISIBILITY_MASK) {
11158 case INVISIBLE:
11159 if (numFlags > 0) {
11160 output += " ";
11161 }
11162 output += "INVISIBLE";
11163 // USELESS HERE numFlags++;
11164 break;
11165 case GONE:
11166 if (numFlags > 0) {
11167 output += " ";
11168 }
11169 output += "GONE";
11170 // USELESS HERE numFlags++;
11171 break;
11172 default:
11173 break;
11174 }
11175 return output;
11176 }
11177
11178 /**
11179 * Build a human readable string representation of the specified private
11180 * view flags.
11181 *
11182 * @param privateFlags the private view flags to convert to a string
11183 * @return a String representing the supplied flags
11184 */
11185 private static String printPrivateFlags(int privateFlags) {
11186 String output = "";
11187 int numFlags = 0;
11188
11189 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11190 output += "WANTS_FOCUS";
11191 numFlags++;
11192 }
11193
11194 if ((privateFlags & FOCUSED) == FOCUSED) {
11195 if (numFlags > 0) {
11196 output += " ";
11197 }
11198 output += "FOCUSED";
11199 numFlags++;
11200 }
11201
11202 if ((privateFlags & SELECTED) == SELECTED) {
11203 if (numFlags > 0) {
11204 output += " ";
11205 }
11206 output += "SELECTED";
11207 numFlags++;
11208 }
11209
11210 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11211 if (numFlags > 0) {
11212 output += " ";
11213 }
11214 output += "IS_ROOT_NAMESPACE";
11215 numFlags++;
11216 }
11217
11218 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11219 if (numFlags > 0) {
11220 output += " ";
11221 }
11222 output += "HAS_BOUNDS";
11223 numFlags++;
11224 }
11225
11226 if ((privateFlags & DRAWN) == DRAWN) {
11227 if (numFlags > 0) {
11228 output += " ";
11229 }
11230 output += "DRAWN";
11231 // USELESS HERE numFlags++;
11232 }
11233 return output;
11234 }
11235
11236 /**
11237 * <p>Indicates whether or not this view's layout will be requested during
11238 * the next hierarchy layout pass.</p>
11239 *
11240 * @return true if the layout will be forced during next layout pass
11241 */
11242 public boolean isLayoutRequested() {
11243 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11244 }
11245
11246 /**
11247 * Assign a size and position to a view and all of its
11248 * descendants
11249 *
11250 * <p>This is the second phase of the layout mechanism.
11251 * (The first is measuring). In this phase, each parent calls
11252 * layout on all of its children to position them.
11253 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011254 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 *
Chet Haase9c087442011-01-12 16:20:16 -080011256 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 * Derived classes with children should override
11258 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011259 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 *
11261 * @param l Left position, relative to parent
11262 * @param t Top position, relative to parent
11263 * @param r Right position, relative to parent
11264 * @param b Bottom position, relative to parent
11265 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011266 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011267 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011268 int oldL = mLeft;
11269 int oldT = mTop;
11270 int oldB = mBottom;
11271 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011272 boolean changed = setFrame(l, t, r, b);
11273 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11274 if (ViewDebug.TRACE_HIERARCHY) {
11275 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11276 }
11277
11278 onLayout(changed, l, t, r, b);
11279 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011280
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011281 ListenerInfo li = mListenerInfo;
11282 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011283 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011284 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011285 int numListeners = listenersCopy.size();
11286 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011287 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011288 }
11289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 }
11291 mPrivateFlags &= ~FORCE_LAYOUT;
11292 }
11293
11294 /**
11295 * Called from layout when this view should
11296 * assign a size and position to each of its children.
11297 *
11298 * Derived classes with children should override
11299 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011300 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011301 * @param changed This is a new size or position for this view
11302 * @param left Left position, relative to parent
11303 * @param top Top position, relative to parent
11304 * @param right Right position, relative to parent
11305 * @param bottom Bottom position, relative to parent
11306 */
11307 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11308 }
11309
11310 /**
11311 * Assign a size and position to this view.
11312 *
11313 * This is called from layout.
11314 *
11315 * @param left Left position, relative to parent
11316 * @param top Top position, relative to parent
11317 * @param right Right position, relative to parent
11318 * @param bottom Bottom position, relative to parent
11319 * @return true if the new size and position are different than the
11320 * previous ones
11321 * {@hide}
11322 */
11323 protected boolean setFrame(int left, int top, int right, int bottom) {
11324 boolean changed = false;
11325
11326 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011327 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 + right + "," + bottom + ")");
11329 }
11330
11331 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11332 changed = true;
11333
11334 // Remember our drawn bit
11335 int drawn = mPrivateFlags & DRAWN;
11336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 int oldWidth = mRight - mLeft;
11338 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011339 int newWidth = right - left;
11340 int newHeight = bottom - top;
11341 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11342
11343 // Invalidate our old position
11344 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345
11346 mLeft = left;
11347 mTop = top;
11348 mRight = right;
11349 mBottom = bottom;
11350
11351 mPrivateFlags |= HAS_BOUNDS;
11352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353
Chet Haase75755e22011-07-18 17:48:25 -070011354 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011355 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11356 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011357 if (mTransformationInfo != null) {
11358 mTransformationInfo.mMatrixDirty = true;
11359 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11362 }
11363
11364 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11365 // If we are visible, force the DRAWN bit to on so that
11366 // this invalidate will go through (at least to our parent).
11367 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011368 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 // the DRAWN bit.
11370 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011371 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011372 // parent display list may need to be recreated based on a change in the bounds
11373 // of any child
11374 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011375 }
11376
11377 // Reset drawn bit to original value (invalidate turns it off)
11378 mPrivateFlags |= drawn;
11379
11380 mBackgroundSizeChanged = true;
11381 }
11382 return changed;
11383 }
11384
11385 /**
11386 * Finalize inflating a view from XML. This is called as the last phase
11387 * of inflation, after all child views have been added.
11388 *
11389 * <p>Even if the subclass overrides onFinishInflate, they should always be
11390 * sure to call the super method, so that we get called.
11391 */
11392 protected void onFinishInflate() {
11393 }
11394
11395 /**
11396 * Returns the resources associated with this view.
11397 *
11398 * @return Resources object.
11399 */
11400 public Resources getResources() {
11401 return mResources;
11402 }
11403
11404 /**
11405 * Invalidates the specified Drawable.
11406 *
11407 * @param drawable the drawable to invalidate
11408 */
11409 public void invalidateDrawable(Drawable drawable) {
11410 if (verifyDrawable(drawable)) {
11411 final Rect dirty = drawable.getBounds();
11412 final int scrollX = mScrollX;
11413 final int scrollY = mScrollY;
11414
11415 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11416 dirty.right + scrollX, dirty.bottom + scrollY);
11417 }
11418 }
11419
11420 /**
11421 * Schedules an action on a drawable to occur at a specified time.
11422 *
11423 * @param who the recipient of the action
11424 * @param what the action to run on the drawable
11425 * @param when the time at which the action must occur. Uses the
11426 * {@link SystemClock#uptimeMillis} timebase.
11427 */
11428 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011429 if (verifyDrawable(who) && what != null) {
11430 if (mAttachInfo != null) {
11431 mAttachInfo.mHandler.postAtTime(what, who, when);
11432 } else {
11433 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011435 }
11436 }
11437
11438 /**
11439 * Cancels a scheduled action on a drawable.
11440 *
11441 * @param who the recipient of the action
11442 * @param what the action to cancel
11443 */
11444 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011445 if (verifyDrawable(who) && what != null) {
11446 if (mAttachInfo != null) {
11447 mAttachInfo.mHandler.removeCallbacks(what, who);
11448 } else {
11449 ViewRootImpl.getRunQueue().removeCallbacks(what);
11450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011451 }
11452 }
11453
11454 /**
11455 * Unschedule any events associated with the given Drawable. This can be
11456 * used when selecting a new Drawable into a view, so that the previous
11457 * one is completely unscheduled.
11458 *
11459 * @param who The Drawable to unschedule.
11460 *
11461 * @see #drawableStateChanged
11462 */
11463 public void unscheduleDrawable(Drawable who) {
11464 if (mAttachInfo != null) {
11465 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11466 }
11467 }
11468
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011469 /**
11470 * Return the layout direction of a given Drawable.
11471 *
11472 * @param who the Drawable to query
11473 *
11474 * @hide
11475 */
11476 public int getResolvedLayoutDirection(Drawable who) {
11477 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011478 }
11479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011480 /**
11481 * If your view subclass is displaying its own Drawable objects, it should
11482 * override this function and return true for any Drawable it is
11483 * displaying. This allows animations for those drawables to be
11484 * scheduled.
11485 *
11486 * <p>Be sure to call through to the super class when overriding this
11487 * function.
11488 *
11489 * @param who The Drawable to verify. Return true if it is one you are
11490 * displaying, else return the result of calling through to the
11491 * super class.
11492 *
11493 * @return boolean If true than the Drawable is being displayed in the
11494 * view; else false and it is not allowed to animate.
11495 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011496 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11497 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 */
11499 protected boolean verifyDrawable(Drawable who) {
11500 return who == mBGDrawable;
11501 }
11502
11503 /**
11504 * This function is called whenever the state of the view changes in such
11505 * a way that it impacts the state of drawables being shown.
11506 *
11507 * <p>Be sure to call through to the superclass when overriding this
11508 * function.
11509 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011510 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 */
11512 protected void drawableStateChanged() {
11513 Drawable d = mBGDrawable;
11514 if (d != null && d.isStateful()) {
11515 d.setState(getDrawableState());
11516 }
11517 }
11518
11519 /**
11520 * Call this to force a view to update its drawable state. This will cause
11521 * drawableStateChanged to be called on this view. Views that are interested
11522 * in the new state should call getDrawableState.
11523 *
11524 * @see #drawableStateChanged
11525 * @see #getDrawableState
11526 */
11527 public void refreshDrawableState() {
11528 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11529 drawableStateChanged();
11530
11531 ViewParent parent = mParent;
11532 if (parent != null) {
11533 parent.childDrawableStateChanged(this);
11534 }
11535 }
11536
11537 /**
11538 * Return an array of resource IDs of the drawable states representing the
11539 * current state of the view.
11540 *
11541 * @return The current drawable state
11542 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011543 * @see Drawable#setState(int[])
11544 * @see #drawableStateChanged()
11545 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011546 */
11547 public final int[] getDrawableState() {
11548 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11549 return mDrawableState;
11550 } else {
11551 mDrawableState = onCreateDrawableState(0);
11552 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11553 return mDrawableState;
11554 }
11555 }
11556
11557 /**
11558 * Generate the new {@link android.graphics.drawable.Drawable} state for
11559 * this view. This is called by the view
11560 * system when the cached Drawable state is determined to be invalid. To
11561 * retrieve the current state, you should use {@link #getDrawableState}.
11562 *
11563 * @param extraSpace if non-zero, this is the number of extra entries you
11564 * would like in the returned array in which you can place your own
11565 * states.
11566 *
11567 * @return Returns an array holding the current {@link Drawable} state of
11568 * the view.
11569 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011570 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011571 */
11572 protected int[] onCreateDrawableState(int extraSpace) {
11573 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11574 mParent instanceof View) {
11575 return ((View) mParent).onCreateDrawableState(extraSpace);
11576 }
11577
11578 int[] drawableState;
11579
11580 int privateFlags = mPrivateFlags;
11581
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011582 int viewStateIndex = 0;
11583 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11584 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11585 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011586 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011587 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11588 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011589 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11590 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011591 // This is set if HW acceleration is requested, even if the current
11592 // process doesn't allow it. This is just to allow app preview
11593 // windows to better match their app.
11594 viewStateIndex |= VIEW_STATE_ACCELERATED;
11595 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011596 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011597
Christopher Tate3d4bf172011-03-28 16:16:46 -070011598 final int privateFlags2 = mPrivateFlags2;
11599 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11600 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 drawableState = VIEW_STATE_SETS[viewStateIndex];
11603
11604 //noinspection ConstantIfStatement
11605 if (false) {
11606 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11607 Log.i("View", toString()
11608 + " pressed=" + ((privateFlags & PRESSED) != 0)
11609 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11610 + " fo=" + hasFocus()
11611 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011612 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011613 + ": " + Arrays.toString(drawableState));
11614 }
11615
11616 if (extraSpace == 0) {
11617 return drawableState;
11618 }
11619
11620 final int[] fullState;
11621 if (drawableState != null) {
11622 fullState = new int[drawableState.length + extraSpace];
11623 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11624 } else {
11625 fullState = new int[extraSpace];
11626 }
11627
11628 return fullState;
11629 }
11630
11631 /**
11632 * Merge your own state values in <var>additionalState</var> into the base
11633 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011634 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011635 *
11636 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011637 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011638 * own additional state values.
11639 *
11640 * @param additionalState The additional state values you would like
11641 * added to <var>baseState</var>; this array is not modified.
11642 *
11643 * @return As a convenience, the <var>baseState</var> array you originally
11644 * passed into the function is returned.
11645 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011646 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 */
11648 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11649 final int N = baseState.length;
11650 int i = N - 1;
11651 while (i >= 0 && baseState[i] == 0) {
11652 i--;
11653 }
11654 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11655 return baseState;
11656 }
11657
11658 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011659 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11660 * on all Drawable objects associated with this view.
11661 */
11662 public void jumpDrawablesToCurrentState() {
11663 if (mBGDrawable != null) {
11664 mBGDrawable.jumpToCurrentState();
11665 }
11666 }
11667
11668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 * Sets the background color for this view.
11670 * @param color the color of the background
11671 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011672 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011673 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011674 if (mBGDrawable instanceof ColorDrawable) {
11675 ((ColorDrawable) mBGDrawable).setColor(color);
11676 } else {
11677 setBackgroundDrawable(new ColorDrawable(color));
11678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011679 }
11680
11681 /**
11682 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011683 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 * @param resid The identifier of the resource.
11685 * @attr ref android.R.styleable#View_background
11686 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011687 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011688 public void setBackgroundResource(int resid) {
11689 if (resid != 0 && resid == mBackgroundResource) {
11690 return;
11691 }
11692
11693 Drawable d= null;
11694 if (resid != 0) {
11695 d = mResources.getDrawable(resid);
11696 }
11697 setBackgroundDrawable(d);
11698
11699 mBackgroundResource = resid;
11700 }
11701
11702 /**
11703 * Set the background to a given Drawable, or remove the background. If the
11704 * background has padding, this View's padding is set to the background's
11705 * padding. However, when a background is removed, this View's padding isn't
11706 * touched. If setting the padding is desired, please use
11707 * {@link #setPadding(int, int, int, int)}.
11708 *
11709 * @param d The Drawable to use as the background, or null to remove the
11710 * background
11711 */
11712 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011713 if (d == mBGDrawable) {
11714 return;
11715 }
11716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011717 boolean requestLayout = false;
11718
11719 mBackgroundResource = 0;
11720
11721 /*
11722 * Regardless of whether we're setting a new background or not, we want
11723 * to clear the previous drawable.
11724 */
11725 if (mBGDrawable != null) {
11726 mBGDrawable.setCallback(null);
11727 unscheduleDrawable(mBGDrawable);
11728 }
11729
11730 if (d != null) {
11731 Rect padding = sThreadLocal.get();
11732 if (padding == null) {
11733 padding = new Rect();
11734 sThreadLocal.set(padding);
11735 }
11736 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011737 switch (d.getResolvedLayoutDirectionSelf()) {
11738 case LAYOUT_DIRECTION_RTL:
11739 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11740 break;
11741 case LAYOUT_DIRECTION_LTR:
11742 default:
11743 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745 }
11746
11747 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11748 // if it has a different minimum size, we should layout again
11749 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11750 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11751 requestLayout = true;
11752 }
11753
11754 d.setCallback(this);
11755 if (d.isStateful()) {
11756 d.setState(getDrawableState());
11757 }
11758 d.setVisible(getVisibility() == VISIBLE, false);
11759 mBGDrawable = d;
11760
11761 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11762 mPrivateFlags &= ~SKIP_DRAW;
11763 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11764 requestLayout = true;
11765 }
11766 } else {
11767 /* Remove the background */
11768 mBGDrawable = null;
11769
11770 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11771 /*
11772 * This view ONLY drew the background before and we're removing
11773 * the background, so now it won't draw anything
11774 * (hence we SKIP_DRAW)
11775 */
11776 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11777 mPrivateFlags |= SKIP_DRAW;
11778 }
11779
11780 /*
11781 * When the background is set, we try to apply its padding to this
11782 * View. When the background is removed, we don't touch this View's
11783 * padding. This is noted in the Javadocs. Hence, we don't need to
11784 * requestLayout(), the invalidate() below is sufficient.
11785 */
11786
11787 // The old background's minimum size could have affected this
11788 // View's layout, so let's requestLayout
11789 requestLayout = true;
11790 }
11791
Romain Guy8f1344f52009-05-15 16:03:59 -070011792 computeOpaqueFlags();
11793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011794 if (requestLayout) {
11795 requestLayout();
11796 }
11797
11798 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011799 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011800 }
11801
11802 /**
11803 * Gets the background drawable
11804 * @return The drawable used as the background for this view, if any.
11805 */
11806 public Drawable getBackground() {
11807 return mBGDrawable;
11808 }
11809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011810 /**
11811 * Sets the padding. The view may add on the space required to display
11812 * the scrollbars, depending on the style and visibility of the scrollbars.
11813 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11814 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11815 * from the values set in this call.
11816 *
11817 * @attr ref android.R.styleable#View_padding
11818 * @attr ref android.R.styleable#View_paddingBottom
11819 * @attr ref android.R.styleable#View_paddingLeft
11820 * @attr ref android.R.styleable#View_paddingRight
11821 * @attr ref android.R.styleable#View_paddingTop
11822 * @param left the left padding in pixels
11823 * @param top the top padding in pixels
11824 * @param right the right padding in pixels
11825 * @param bottom the bottom padding in pixels
11826 */
11827 public void setPadding(int left, int top, int right, int bottom) {
11828 boolean changed = false;
11829
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011830 mUserPaddingRelative = false;
11831
Adam Powell20232d02010-12-08 21:08:53 -080011832 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011833 mUserPaddingRight = right;
11834 mUserPaddingBottom = bottom;
11835
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011836 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011837
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011838 // Common case is there are no scroll bars.
11839 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011840 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011841 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011842 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011843 switch (mVerticalScrollbarPosition) {
11844 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011845 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11846 left += offset;
11847 } else {
11848 right += offset;
11849 }
11850 break;
Adam Powell20232d02010-12-08 21:08:53 -080011851 case SCROLLBAR_POSITION_RIGHT:
11852 right += offset;
11853 break;
11854 case SCROLLBAR_POSITION_LEFT:
11855 left += offset;
11856 break;
11857 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011858 }
Adam Powell20232d02010-12-08 21:08:53 -080011859 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011860 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11861 ? 0 : getHorizontalScrollbarHeight();
11862 }
11863 }
Romain Guy8506ab42009-06-11 17:35:47 -070011864
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011865 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866 changed = true;
11867 mPaddingLeft = left;
11868 }
11869 if (mPaddingTop != top) {
11870 changed = true;
11871 mPaddingTop = top;
11872 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011873 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011875 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011876 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011877 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011878 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011879 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011880 }
11881
11882 if (changed) {
11883 requestLayout();
11884 }
11885 }
11886
11887 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011888 * Sets the relative padding. The view may add on the space required to display
11889 * the scrollbars, depending on the style and visibility of the scrollbars.
11890 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11891 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11892 * from the values set in this call.
11893 *
11894 * @attr ref android.R.styleable#View_padding
11895 * @attr ref android.R.styleable#View_paddingBottom
11896 * @attr ref android.R.styleable#View_paddingStart
11897 * @attr ref android.R.styleable#View_paddingEnd
11898 * @attr ref android.R.styleable#View_paddingTop
11899 * @param start the start padding in pixels
11900 * @param top the top padding in pixels
11901 * @param end the end padding in pixels
11902 * @param bottom the bottom padding in pixels
11903 *
11904 * @hide
11905 */
11906 public void setPaddingRelative(int start, int top, int end, int bottom) {
11907 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011908
11909 mUserPaddingStart = start;
11910 mUserPaddingEnd = end;
11911
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011912 switch(getResolvedLayoutDirection()) {
11913 case LAYOUT_DIRECTION_RTL:
11914 setPadding(end, top, start, bottom);
11915 break;
11916 case LAYOUT_DIRECTION_LTR:
11917 default:
11918 setPadding(start, top, end, bottom);
11919 }
11920 }
11921
11922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011923 * Returns the top padding of this view.
11924 *
11925 * @return the top padding in pixels
11926 */
11927 public int getPaddingTop() {
11928 return mPaddingTop;
11929 }
11930
11931 /**
11932 * Returns the bottom padding of this view. If there are inset and enabled
11933 * scrollbars, this value may include the space required to display the
11934 * scrollbars as well.
11935 *
11936 * @return the bottom padding in pixels
11937 */
11938 public int getPaddingBottom() {
11939 return mPaddingBottom;
11940 }
11941
11942 /**
11943 * Returns the left padding of this view. If there are inset and enabled
11944 * scrollbars, this value may include the space required to display the
11945 * scrollbars as well.
11946 *
11947 * @return the left padding in pixels
11948 */
11949 public int getPaddingLeft() {
11950 return mPaddingLeft;
11951 }
11952
11953 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011954 * Returns the start padding of this view. If there are inset and enabled
11955 * scrollbars, this value may include the space required to display the
11956 * scrollbars as well.
11957 *
11958 * @return the start padding in pixels
11959 *
11960 * @hide
11961 */
11962 public int getPaddingStart() {
11963 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11964 mPaddingRight : mPaddingLeft;
11965 }
11966
11967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011968 * Returns the right padding of this view. If there are inset and enabled
11969 * scrollbars, this value may include the space required to display the
11970 * scrollbars as well.
11971 *
11972 * @return the right padding in pixels
11973 */
11974 public int getPaddingRight() {
11975 return mPaddingRight;
11976 }
11977
11978 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011979 * Returns the end padding of this view. If there are inset and enabled
11980 * scrollbars, this value may include the space required to display the
11981 * scrollbars as well.
11982 *
11983 * @return the end padding in pixels
11984 *
11985 * @hide
11986 */
11987 public int getPaddingEnd() {
11988 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11989 mPaddingLeft : mPaddingRight;
11990 }
11991
11992 /**
11993 * Return if the padding as been set thru relative values
11994 * {@link #setPaddingRelative(int, int, int, int)} or thru
11995 * @attr ref android.R.styleable#View_paddingStart or
11996 * @attr ref android.R.styleable#View_paddingEnd
11997 *
11998 * @return true if the padding is relative or false if it is not.
11999 *
12000 * @hide
12001 */
12002 public boolean isPaddingRelative() {
12003 return mUserPaddingRelative;
12004 }
12005
12006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012007 * Changes the selection state of this view. A view can be selected or not.
12008 * Note that selection is not the same as focus. Views are typically
12009 * selected in the context of an AdapterView like ListView or GridView;
12010 * the selected view is the view that is highlighted.
12011 *
12012 * @param selected true if the view must be selected, false otherwise
12013 */
12014 public void setSelected(boolean selected) {
12015 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12016 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012017 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012018 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012019 refreshDrawableState();
12020 dispatchSetSelected(selected);
12021 }
12022 }
12023
12024 /**
12025 * Dispatch setSelected to all of this View's children.
12026 *
12027 * @see #setSelected(boolean)
12028 *
12029 * @param selected The new selected state
12030 */
12031 protected void dispatchSetSelected(boolean selected) {
12032 }
12033
12034 /**
12035 * Indicates the selection state of this view.
12036 *
12037 * @return true if the view is selected, false otherwise
12038 */
12039 @ViewDebug.ExportedProperty
12040 public boolean isSelected() {
12041 return (mPrivateFlags & SELECTED) != 0;
12042 }
12043
12044 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012045 * Changes the activated state of this view. A view can be activated or not.
12046 * Note that activation is not the same as selection. Selection is
12047 * a transient property, representing the view (hierarchy) the user is
12048 * currently interacting with. Activation is a longer-term state that the
12049 * user can move views in and out of. For example, in a list view with
12050 * single or multiple selection enabled, the views in the current selection
12051 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12052 * here.) The activated state is propagated down to children of the view it
12053 * is set on.
12054 *
12055 * @param activated true if the view must be activated, false otherwise
12056 */
12057 public void setActivated(boolean activated) {
12058 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12059 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012060 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012061 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012062 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012063 }
12064 }
12065
12066 /**
12067 * Dispatch setActivated to all of this View's children.
12068 *
12069 * @see #setActivated(boolean)
12070 *
12071 * @param activated The new activated state
12072 */
12073 protected void dispatchSetActivated(boolean activated) {
12074 }
12075
12076 /**
12077 * Indicates the activation state of this view.
12078 *
12079 * @return true if the view is activated, false otherwise
12080 */
12081 @ViewDebug.ExportedProperty
12082 public boolean isActivated() {
12083 return (mPrivateFlags & ACTIVATED) != 0;
12084 }
12085
12086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12088 * observer can be used to get notifications when global events, like
12089 * layout, happen.
12090 *
12091 * The returned ViewTreeObserver observer is not guaranteed to remain
12092 * valid for the lifetime of this View. If the caller of this method keeps
12093 * a long-lived reference to ViewTreeObserver, it should always check for
12094 * the return value of {@link ViewTreeObserver#isAlive()}.
12095 *
12096 * @return The ViewTreeObserver for this view's hierarchy.
12097 */
12098 public ViewTreeObserver getViewTreeObserver() {
12099 if (mAttachInfo != null) {
12100 return mAttachInfo.mTreeObserver;
12101 }
12102 if (mFloatingTreeObserver == null) {
12103 mFloatingTreeObserver = new ViewTreeObserver();
12104 }
12105 return mFloatingTreeObserver;
12106 }
12107
12108 /**
12109 * <p>Finds the topmost view in the current view hierarchy.</p>
12110 *
12111 * @return the topmost view containing this view
12112 */
12113 public View getRootView() {
12114 if (mAttachInfo != null) {
12115 final View v = mAttachInfo.mRootView;
12116 if (v != null) {
12117 return v;
12118 }
12119 }
Romain Guy8506ab42009-06-11 17:35:47 -070012120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012121 View parent = this;
12122
12123 while (parent.mParent != null && parent.mParent instanceof View) {
12124 parent = (View) parent.mParent;
12125 }
12126
12127 return parent;
12128 }
12129
12130 /**
12131 * <p>Computes the coordinates of this view on the screen. The argument
12132 * must be an array of two integers. After the method returns, the array
12133 * contains the x and y location in that order.</p>
12134 *
12135 * @param location an array of two integers in which to hold the coordinates
12136 */
12137 public void getLocationOnScreen(int[] location) {
12138 getLocationInWindow(location);
12139
12140 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012141 if (info != null) {
12142 location[0] += info.mWindowLeft;
12143 location[1] += info.mWindowTop;
12144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012145 }
12146
12147 /**
12148 * <p>Computes the coordinates of this view in its window. The argument
12149 * must be an array of two integers. After the method returns, the array
12150 * contains the x and y location in that order.</p>
12151 *
12152 * @param location an array of two integers in which to hold the coordinates
12153 */
12154 public void getLocationInWindow(int[] location) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012155 // When the view is not attached to a window, this method does not make sense
12156 if (mAttachInfo == null) return;
12157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012158 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012159 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012160 }
12161
Gilles Debunnecea45132011-11-24 02:19:27 +010012162 float[] position = mAttachInfo.mTmpTransformLocation;
12163 position[0] = position[1] = 0.0f;
12164
12165 if (!hasIdentityMatrix()) {
12166 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168
Gilles Debunnecea45132011-11-24 02:19:27 +010012169 position[0] += mLeft;
12170 position[1] += mTop;
12171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 ViewParent viewParent = mParent;
12173 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012174 final View view = (View) viewParent;
12175
12176 position[0] -= view.mScrollX;
12177 position[1] -= view.mScrollY;
12178
12179 if (!view.hasIdentityMatrix()) {
12180 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012181 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012182
12183 position[0] += view.mLeft;
12184 position[1] += view.mTop;
12185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186 viewParent = view.mParent;
12187 }
Romain Guy8506ab42009-06-11 17:35:47 -070012188
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012189 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012191 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12192 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012194
12195 location[0] = (int) (position[0] + 0.5f);
12196 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012197 }
12198
12199 /**
12200 * {@hide}
12201 * @param id the id of the view to be found
12202 * @return the view of the specified id, null if cannot be found
12203 */
12204 protected View findViewTraversal(int id) {
12205 if (id == mID) {
12206 return this;
12207 }
12208 return null;
12209 }
12210
12211 /**
12212 * {@hide}
12213 * @param tag the tag of the view to be found
12214 * @return the view of specified tag, null if cannot be found
12215 */
12216 protected View findViewWithTagTraversal(Object tag) {
12217 if (tag != null && tag.equals(mTag)) {
12218 return this;
12219 }
12220 return null;
12221 }
12222
12223 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012224 * {@hide}
12225 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012226 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012227 * @return The first view that matches the predicate or null.
12228 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012229 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012230 if (predicate.apply(this)) {
12231 return this;
12232 }
12233 return null;
12234 }
12235
12236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012237 * Look for a child view with the given id. If this view has the given
12238 * id, return this view.
12239 *
12240 * @param id The id to search for.
12241 * @return The view that has the given id in the hierarchy or null
12242 */
12243 public final View findViewById(int id) {
12244 if (id < 0) {
12245 return null;
12246 }
12247 return findViewTraversal(id);
12248 }
12249
12250 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012251 * Finds a view by its unuque and stable accessibility id.
12252 *
12253 * @param accessibilityId The searched accessibility id.
12254 * @return The found view.
12255 */
12256 final View findViewByAccessibilityId(int accessibilityId) {
12257 if (accessibilityId < 0) {
12258 return null;
12259 }
12260 return findViewByAccessibilityIdTraversal(accessibilityId);
12261 }
12262
12263 /**
12264 * Performs the traversal to find a view by its unuque and stable accessibility id.
12265 *
12266 * <strong>Note:</strong>This method does not stop at the root namespace
12267 * boundary since the user can touch the screen at an arbitrary location
12268 * potentially crossing the root namespace bounday which will send an
12269 * accessibility event to accessibility services and they should be able
12270 * to obtain the event source. Also accessibility ids are guaranteed to be
12271 * unique in the window.
12272 *
12273 * @param accessibilityId The accessibility id.
12274 * @return The found view.
12275 */
12276 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12277 if (getAccessibilityViewId() == accessibilityId) {
12278 return this;
12279 }
12280 return null;
12281 }
12282
12283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 * Look for a child view with the given tag. If this view has the given
12285 * tag, return this view.
12286 *
12287 * @param tag The tag to search for, using "tag.equals(getTag())".
12288 * @return The View that has the given tag in the hierarchy or null
12289 */
12290 public final View findViewWithTag(Object tag) {
12291 if (tag == null) {
12292 return null;
12293 }
12294 return findViewWithTagTraversal(tag);
12295 }
12296
12297 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012298 * {@hide}
12299 * Look for a child view that matches the specified predicate.
12300 * If this view matches the predicate, return this view.
12301 *
12302 * @param predicate The predicate to evaluate.
12303 * @return The first view that matches the predicate or null.
12304 */
12305 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012306 return findViewByPredicateTraversal(predicate, null);
12307 }
12308
12309 /**
12310 * {@hide}
12311 * Look for a child view that matches the specified predicate,
12312 * starting with the specified view and its descendents and then
12313 * recusively searching the ancestors and siblings of that view
12314 * until this view is reached.
12315 *
12316 * This method is useful in cases where the predicate does not match
12317 * a single unique view (perhaps multiple views use the same id)
12318 * and we are trying to find the view that is "closest" in scope to the
12319 * starting view.
12320 *
12321 * @param start The view to start from.
12322 * @param predicate The predicate to evaluate.
12323 * @return The first view that matches the predicate or null.
12324 */
12325 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12326 View childToSkip = null;
12327 for (;;) {
12328 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12329 if (view != null || start == this) {
12330 return view;
12331 }
12332
12333 ViewParent parent = start.getParent();
12334 if (parent == null || !(parent instanceof View)) {
12335 return null;
12336 }
12337
12338 childToSkip = start;
12339 start = (View) parent;
12340 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012341 }
12342
12343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012344 * Sets the identifier for this view. The identifier does not have to be
12345 * unique in this view's hierarchy. The identifier should be a positive
12346 * number.
12347 *
12348 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012349 * @see #getId()
12350 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012351 *
12352 * @param id a number used to identify the view
12353 *
12354 * @attr ref android.R.styleable#View_id
12355 */
12356 public void setId(int id) {
12357 mID = id;
12358 }
12359
12360 /**
12361 * {@hide}
12362 *
12363 * @param isRoot true if the view belongs to the root namespace, false
12364 * otherwise
12365 */
12366 public void setIsRootNamespace(boolean isRoot) {
12367 if (isRoot) {
12368 mPrivateFlags |= IS_ROOT_NAMESPACE;
12369 } else {
12370 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12371 }
12372 }
12373
12374 /**
12375 * {@hide}
12376 *
12377 * @return true if the view belongs to the root namespace, false otherwise
12378 */
12379 public boolean isRootNamespace() {
12380 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12381 }
12382
12383 /**
12384 * Returns this view's identifier.
12385 *
12386 * @return a positive integer used to identify the view or {@link #NO_ID}
12387 * if the view has no ID
12388 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012389 * @see #setId(int)
12390 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012391 * @attr ref android.R.styleable#View_id
12392 */
12393 @ViewDebug.CapturedViewProperty
12394 public int getId() {
12395 return mID;
12396 }
12397
12398 /**
12399 * Returns this view's tag.
12400 *
12401 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012402 *
12403 * @see #setTag(Object)
12404 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 */
12406 @ViewDebug.ExportedProperty
12407 public Object getTag() {
12408 return mTag;
12409 }
12410
12411 /**
12412 * Sets the tag associated with this view. A tag can be used to mark
12413 * a view in its hierarchy and does not have to be unique within the
12414 * hierarchy. Tags can also be used to store data within a view without
12415 * resorting to another data structure.
12416 *
12417 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012418 *
12419 * @see #getTag()
12420 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 */
12422 public void setTag(final Object tag) {
12423 mTag = tag;
12424 }
12425
12426 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012427 * Returns the tag associated with this view and the specified key.
12428 *
12429 * @param key The key identifying the tag
12430 *
12431 * @return the Object stored in this view as a tag
12432 *
12433 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012434 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012435 */
12436 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012437 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012438 return null;
12439 }
12440
12441 /**
12442 * Sets a tag associated with this view and a key. A tag can be used
12443 * to mark a view in its hierarchy and does not have to be unique within
12444 * the hierarchy. Tags can also be used to store data within a view
12445 * without resorting to another data structure.
12446 *
12447 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012448 * application to ensure it is unique (see the <a
12449 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12450 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012451 * the Android framework or not associated with any package will cause
12452 * an {@link IllegalArgumentException} to be thrown.
12453 *
12454 * @param key The key identifying the tag
12455 * @param tag An Object to tag the view with
12456 *
12457 * @throws IllegalArgumentException If they specified key is not valid
12458 *
12459 * @see #setTag(Object)
12460 * @see #getTag(int)
12461 */
12462 public void setTag(int key, final Object tag) {
12463 // If the package id is 0x00 or 0x01, it's either an undefined package
12464 // or a framework id
12465 if ((key >>> 24) < 2) {
12466 throw new IllegalArgumentException("The key must be an application-specific "
12467 + "resource id.");
12468 }
12469
Adam Powell2b2f6d62011-09-23 11:15:39 -070012470 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012471 }
12472
12473 /**
12474 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12475 * framework id.
12476 *
12477 * @hide
12478 */
12479 public void setTagInternal(int key, Object tag) {
12480 if ((key >>> 24) != 0x1) {
12481 throw new IllegalArgumentException("The key must be a framework-specific "
12482 + "resource id.");
12483 }
12484
Adam Powell2b2f6d62011-09-23 11:15:39 -070012485 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012486 }
12487
Adam Powell2b2f6d62011-09-23 11:15:39 -070012488 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012489 if (mKeyedTags == null) {
12490 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012491 }
12492
Adam Powell7db82ac2011-09-22 19:44:04 -070012493 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012494 }
12495
12496 /**
Romain Guy13922e02009-05-12 17:56:14 -070012497 * @param consistency The type of consistency. See ViewDebug for more information.
12498 *
12499 * @hide
12500 */
12501 protected boolean dispatchConsistencyCheck(int consistency) {
12502 return onConsistencyCheck(consistency);
12503 }
12504
12505 /**
12506 * Method that subclasses should implement to check their consistency. The type of
12507 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012508 *
Romain Guy13922e02009-05-12 17:56:14 -070012509 * @param consistency The type of consistency. See ViewDebug for more information.
12510 *
12511 * @throws IllegalStateException if the view is in an inconsistent state.
12512 *
12513 * @hide
12514 */
12515 protected boolean onConsistencyCheck(int consistency) {
12516 boolean result = true;
12517
12518 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12519 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12520
12521 if (checkLayout) {
12522 if (getParent() == null) {
12523 result = false;
12524 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12525 "View " + this + " does not have a parent.");
12526 }
12527
12528 if (mAttachInfo == null) {
12529 result = false;
12530 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12531 "View " + this + " is not attached to a window.");
12532 }
12533 }
12534
12535 if (checkDrawing) {
12536 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12537 // from their draw() method
12538
12539 if ((mPrivateFlags & DRAWN) != DRAWN &&
12540 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12541 result = false;
12542 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12543 "View " + this + " was invalidated but its drawing cache is valid.");
12544 }
12545 }
12546
12547 return result;
12548 }
12549
12550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012551 * Prints information about this view in the log output, with the tag
12552 * {@link #VIEW_LOG_TAG}.
12553 *
12554 * @hide
12555 */
12556 public void debug() {
12557 debug(0);
12558 }
12559
12560 /**
12561 * Prints information about this view in the log output, with the tag
12562 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12563 * indentation defined by the <code>depth</code>.
12564 *
12565 * @param depth the indentation level
12566 *
12567 * @hide
12568 */
12569 protected void debug(int depth) {
12570 String output = debugIndent(depth - 1);
12571
12572 output += "+ " + this;
12573 int id = getId();
12574 if (id != -1) {
12575 output += " (id=" + id + ")";
12576 }
12577 Object tag = getTag();
12578 if (tag != null) {
12579 output += " (tag=" + tag + ")";
12580 }
12581 Log.d(VIEW_LOG_TAG, output);
12582
12583 if ((mPrivateFlags & FOCUSED) != 0) {
12584 output = debugIndent(depth) + " FOCUSED";
12585 Log.d(VIEW_LOG_TAG, output);
12586 }
12587
12588 output = debugIndent(depth);
12589 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12590 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12591 + "} ";
12592 Log.d(VIEW_LOG_TAG, output);
12593
12594 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12595 || mPaddingBottom != 0) {
12596 output = debugIndent(depth);
12597 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12598 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12599 Log.d(VIEW_LOG_TAG, output);
12600 }
12601
12602 output = debugIndent(depth);
12603 output += "mMeasureWidth=" + mMeasuredWidth +
12604 " mMeasureHeight=" + mMeasuredHeight;
12605 Log.d(VIEW_LOG_TAG, output);
12606
12607 output = debugIndent(depth);
12608 if (mLayoutParams == null) {
12609 output += "BAD! no layout params";
12610 } else {
12611 output = mLayoutParams.debug(output);
12612 }
12613 Log.d(VIEW_LOG_TAG, output);
12614
12615 output = debugIndent(depth);
12616 output += "flags={";
12617 output += View.printFlags(mViewFlags);
12618 output += "}";
12619 Log.d(VIEW_LOG_TAG, output);
12620
12621 output = debugIndent(depth);
12622 output += "privateFlags={";
12623 output += View.printPrivateFlags(mPrivateFlags);
12624 output += "}";
12625 Log.d(VIEW_LOG_TAG, output);
12626 }
12627
12628 /**
12629 * Creates an string of whitespaces used for indentation.
12630 *
12631 * @param depth the indentation level
12632 * @return a String containing (depth * 2 + 3) * 2 white spaces
12633 *
12634 * @hide
12635 */
12636 protected static String debugIndent(int depth) {
12637 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12638 for (int i = 0; i < (depth * 2) + 3; i++) {
12639 spaces.append(' ').append(' ');
12640 }
12641 return spaces.toString();
12642 }
12643
12644 /**
12645 * <p>Return the offset of the widget's text baseline from the widget's top
12646 * boundary. If this widget does not support baseline alignment, this
12647 * method returns -1. </p>
12648 *
12649 * @return the offset of the baseline within the widget's bounds or -1
12650 * if baseline alignment is not supported
12651 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012652 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012653 public int getBaseline() {
12654 return -1;
12655 }
12656
12657 /**
12658 * Call this when something has changed which has invalidated the
12659 * layout of this view. This will schedule a layout pass of the view
12660 * tree.
12661 */
12662 public void requestLayout() {
12663 if (ViewDebug.TRACE_HIERARCHY) {
12664 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12665 }
12666
12667 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012668 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012669
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012670 if (mParent != null) {
12671 if (mLayoutParams != null) {
12672 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12673 }
12674 if (!mParent.isLayoutRequested()) {
12675 mParent.requestLayout();
12676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012677 }
12678 }
12679
12680 /**
12681 * Forces this view to be laid out during the next layout pass.
12682 * This method does not call requestLayout() or forceLayout()
12683 * on the parent.
12684 */
12685 public void forceLayout() {
12686 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012687 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012688 }
12689
12690 /**
12691 * <p>
12692 * This is called to find out how big a view should be. The parent
12693 * supplies constraint information in the width and height parameters.
12694 * </p>
12695 *
12696 * <p>
12697 * The actual mesurement work of a view is performed in
12698 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12699 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12700 * </p>
12701 *
12702 *
12703 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12704 * parent
12705 * @param heightMeasureSpec Vertical space requirements as imposed by the
12706 * parent
12707 *
12708 * @see #onMeasure(int, int)
12709 */
12710 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12711 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12712 widthMeasureSpec != mOldWidthMeasureSpec ||
12713 heightMeasureSpec != mOldHeightMeasureSpec) {
12714
12715 // first clears the measured dimension flag
12716 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12717
12718 if (ViewDebug.TRACE_HIERARCHY) {
12719 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12720 }
12721
12722 // measure ourselves, this should set the measured dimension flag back
12723 onMeasure(widthMeasureSpec, heightMeasureSpec);
12724
12725 // flag not set, setMeasuredDimension() was not invoked, we raise
12726 // an exception to warn the developer
12727 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12728 throw new IllegalStateException("onMeasure() did not set the"
12729 + " measured dimension by calling"
12730 + " setMeasuredDimension()");
12731 }
12732
12733 mPrivateFlags |= LAYOUT_REQUIRED;
12734 }
12735
12736 mOldWidthMeasureSpec = widthMeasureSpec;
12737 mOldHeightMeasureSpec = heightMeasureSpec;
12738 }
12739
12740 /**
12741 * <p>
12742 * Measure the view and its content to determine the measured width and the
12743 * measured height. This method is invoked by {@link #measure(int, int)} and
12744 * should be overriden by subclasses to provide accurate and efficient
12745 * measurement of their contents.
12746 * </p>
12747 *
12748 * <p>
12749 * <strong>CONTRACT:</strong> When overriding this method, you
12750 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12751 * measured width and height of this view. Failure to do so will trigger an
12752 * <code>IllegalStateException</code>, thrown by
12753 * {@link #measure(int, int)}. Calling the superclass'
12754 * {@link #onMeasure(int, int)} is a valid use.
12755 * </p>
12756 *
12757 * <p>
12758 * The base class implementation of measure defaults to the background size,
12759 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12760 * override {@link #onMeasure(int, int)} to provide better measurements of
12761 * their content.
12762 * </p>
12763 *
12764 * <p>
12765 * If this method is overridden, it is the subclass's responsibility to make
12766 * sure the measured height and width are at least the view's minimum height
12767 * and width ({@link #getSuggestedMinimumHeight()} and
12768 * {@link #getSuggestedMinimumWidth()}).
12769 * </p>
12770 *
12771 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12772 * The requirements are encoded with
12773 * {@link android.view.View.MeasureSpec}.
12774 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12775 * The requirements are encoded with
12776 * {@link android.view.View.MeasureSpec}.
12777 *
12778 * @see #getMeasuredWidth()
12779 * @see #getMeasuredHeight()
12780 * @see #setMeasuredDimension(int, int)
12781 * @see #getSuggestedMinimumHeight()
12782 * @see #getSuggestedMinimumWidth()
12783 * @see android.view.View.MeasureSpec#getMode(int)
12784 * @see android.view.View.MeasureSpec#getSize(int)
12785 */
12786 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12787 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12788 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12789 }
12790
12791 /**
12792 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12793 * measured width and measured height. Failing to do so will trigger an
12794 * exception at measurement time.</p>
12795 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012796 * @param measuredWidth The measured width of this view. May be a complex
12797 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12798 * {@link #MEASURED_STATE_TOO_SMALL}.
12799 * @param measuredHeight The measured height of this view. May be a complex
12800 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12801 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012802 */
12803 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12804 mMeasuredWidth = measuredWidth;
12805 mMeasuredHeight = measuredHeight;
12806
12807 mPrivateFlags |= MEASURED_DIMENSION_SET;
12808 }
12809
12810 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012811 * Merge two states as returned by {@link #getMeasuredState()}.
12812 * @param curState The current state as returned from a view or the result
12813 * of combining multiple views.
12814 * @param newState The new view state to combine.
12815 * @return Returns a new integer reflecting the combination of the two
12816 * states.
12817 */
12818 public static int combineMeasuredStates(int curState, int newState) {
12819 return curState | newState;
12820 }
12821
12822 /**
12823 * Version of {@link #resolveSizeAndState(int, int, int)}
12824 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12825 */
12826 public static int resolveSize(int size, int measureSpec) {
12827 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12828 }
12829
12830 /**
12831 * Utility to reconcile a desired size and state, with constraints imposed
12832 * by a MeasureSpec. Will take the desired size, unless a different size
12833 * is imposed by the constraints. The returned value is a compound integer,
12834 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12835 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12836 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012837 *
12838 * @param size How big the view wants to be
12839 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012840 * @return Size information bit mask as defined by
12841 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012843 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012844 int result = size;
12845 int specMode = MeasureSpec.getMode(measureSpec);
12846 int specSize = MeasureSpec.getSize(measureSpec);
12847 switch (specMode) {
12848 case MeasureSpec.UNSPECIFIED:
12849 result = size;
12850 break;
12851 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012852 if (specSize < size) {
12853 result = specSize | MEASURED_STATE_TOO_SMALL;
12854 } else {
12855 result = size;
12856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 break;
12858 case MeasureSpec.EXACTLY:
12859 result = specSize;
12860 break;
12861 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012862 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012863 }
12864
12865 /**
12866 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012867 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012868 * by the MeasureSpec.
12869 *
12870 * @param size Default size for this view
12871 * @param measureSpec Constraints imposed by the parent
12872 * @return The size this view should be.
12873 */
12874 public static int getDefaultSize(int size, int measureSpec) {
12875 int result = size;
12876 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012877 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012878
12879 switch (specMode) {
12880 case MeasureSpec.UNSPECIFIED:
12881 result = size;
12882 break;
12883 case MeasureSpec.AT_MOST:
12884 case MeasureSpec.EXACTLY:
12885 result = specSize;
12886 break;
12887 }
12888 return result;
12889 }
12890
12891 /**
12892 * Returns the suggested minimum height that the view should use. This
12893 * returns the maximum of the view's minimum height
12894 * and the background's minimum height
12895 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12896 * <p>
12897 * When being used in {@link #onMeasure(int, int)}, the caller should still
12898 * ensure the returned height is within the requirements of the parent.
12899 *
12900 * @return The suggested minimum height of the view.
12901 */
12902 protected int getSuggestedMinimumHeight() {
12903 int suggestedMinHeight = mMinHeight;
12904
12905 if (mBGDrawable != null) {
12906 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12907 if (suggestedMinHeight < bgMinHeight) {
12908 suggestedMinHeight = bgMinHeight;
12909 }
12910 }
12911
12912 return suggestedMinHeight;
12913 }
12914
12915 /**
12916 * Returns the suggested minimum width that the view should use. This
12917 * returns the maximum of the view's minimum width)
12918 * and the background's minimum width
12919 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12920 * <p>
12921 * When being used in {@link #onMeasure(int, int)}, the caller should still
12922 * ensure the returned width is within the requirements of the parent.
12923 *
12924 * @return The suggested minimum width of the view.
12925 */
12926 protected int getSuggestedMinimumWidth() {
12927 int suggestedMinWidth = mMinWidth;
12928
12929 if (mBGDrawable != null) {
12930 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12931 if (suggestedMinWidth < bgMinWidth) {
12932 suggestedMinWidth = bgMinWidth;
12933 }
12934 }
12935
12936 return suggestedMinWidth;
12937 }
12938
12939 /**
12940 * Sets the minimum height of the view. It is not guaranteed the view will
12941 * be able to achieve this minimum height (for example, if its parent layout
12942 * constrains it with less available height).
12943 *
12944 * @param minHeight The minimum height the view will try to be.
12945 */
12946 public void setMinimumHeight(int minHeight) {
12947 mMinHeight = minHeight;
12948 }
12949
12950 /**
12951 * Sets the minimum width of the view. It is not guaranteed the view will
12952 * be able to achieve this minimum width (for example, if its parent layout
12953 * constrains it with less available width).
12954 *
12955 * @param minWidth The minimum width the view will try to be.
12956 */
12957 public void setMinimumWidth(int minWidth) {
12958 mMinWidth = minWidth;
12959 }
12960
12961 /**
12962 * Get the animation currently associated with this view.
12963 *
12964 * @return The animation that is currently playing or
12965 * scheduled to play for this view.
12966 */
12967 public Animation getAnimation() {
12968 return mCurrentAnimation;
12969 }
12970
12971 /**
12972 * Start the specified animation now.
12973 *
12974 * @param animation the animation to start now
12975 */
12976 public void startAnimation(Animation animation) {
12977 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12978 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012979 invalidateParentCaches();
12980 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012981 }
12982
12983 /**
12984 * Cancels any animations for this view.
12985 */
12986 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012987 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012988 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012990 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012991 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012992 }
12993
12994 /**
12995 * Sets the next animation to play for this view.
12996 * If you want the animation to play immediately, use
12997 * startAnimation. This method provides allows fine-grained
12998 * control over the start time and invalidation, but you
12999 * must make sure that 1) the animation has a start time set, and
13000 * 2) the view will be invalidated when the animation is supposed to
13001 * start.
13002 *
13003 * @param animation The next animation, or null.
13004 */
13005 public void setAnimation(Animation animation) {
13006 mCurrentAnimation = animation;
13007 if (animation != null) {
13008 animation.reset();
13009 }
13010 }
13011
13012 /**
13013 * Invoked by a parent ViewGroup to notify the start of the animation
13014 * currently associated with this view. If you override this method,
13015 * always call super.onAnimationStart();
13016 *
13017 * @see #setAnimation(android.view.animation.Animation)
13018 * @see #getAnimation()
13019 */
13020 protected void onAnimationStart() {
13021 mPrivateFlags |= ANIMATION_STARTED;
13022 }
13023
13024 /**
13025 * Invoked by a parent ViewGroup to notify the end of the animation
13026 * currently associated with this view. If you override this method,
13027 * always call super.onAnimationEnd();
13028 *
13029 * @see #setAnimation(android.view.animation.Animation)
13030 * @see #getAnimation()
13031 */
13032 protected void onAnimationEnd() {
13033 mPrivateFlags &= ~ANIMATION_STARTED;
13034 }
13035
13036 /**
13037 * Invoked if there is a Transform that involves alpha. Subclass that can
13038 * draw themselves with the specified alpha should return true, and then
13039 * respect that alpha when their onDraw() is called. If this returns false
13040 * then the view may be redirected to draw into an offscreen buffer to
13041 * fulfill the request, which will look fine, but may be slower than if the
13042 * subclass handles it internally. The default implementation returns false.
13043 *
13044 * @param alpha The alpha (0..255) to apply to the view's drawing
13045 * @return true if the view can draw with the specified alpha.
13046 */
13047 protected boolean onSetAlpha(int alpha) {
13048 return false;
13049 }
13050
13051 /**
13052 * This is used by the RootView to perform an optimization when
13053 * the view hierarchy contains one or several SurfaceView.
13054 * SurfaceView is always considered transparent, but its children are not,
13055 * therefore all View objects remove themselves from the global transparent
13056 * region (passed as a parameter to this function).
13057 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013058 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013059 *
13060 * @return Returns true if the effective visibility of the view at this
13061 * point is opaque, regardless of the transparent region; returns false
13062 * if it is possible for underlying windows to be seen behind the view.
13063 *
13064 * {@hide}
13065 */
13066 public boolean gatherTransparentRegion(Region region) {
13067 final AttachInfo attachInfo = mAttachInfo;
13068 if (region != null && attachInfo != null) {
13069 final int pflags = mPrivateFlags;
13070 if ((pflags & SKIP_DRAW) == 0) {
13071 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13072 // remove it from the transparent region.
13073 final int[] location = attachInfo.mTransparentLocation;
13074 getLocationInWindow(location);
13075 region.op(location[0], location[1], location[0] + mRight - mLeft,
13076 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13077 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13078 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13079 // exists, so we remove the background drawable's non-transparent
13080 // parts from this transparent region.
13081 applyDrawableToTransparentRegion(mBGDrawable, region);
13082 }
13083 }
13084 return true;
13085 }
13086
13087 /**
13088 * Play a sound effect for this view.
13089 *
13090 * <p>The framework will play sound effects for some built in actions, such as
13091 * clicking, but you may wish to play these effects in your widget,
13092 * for instance, for internal navigation.
13093 *
13094 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13095 * {@link #isSoundEffectsEnabled()} is true.
13096 *
13097 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13098 */
13099 public void playSoundEffect(int soundConstant) {
13100 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13101 return;
13102 }
13103 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13104 }
13105
13106 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013107 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013108 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013109 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013110 *
13111 * <p>The framework will provide haptic feedback for some built in actions,
13112 * such as long presses, but you may wish to provide feedback for your
13113 * own widget.
13114 *
13115 * <p>The feedback will only be performed if
13116 * {@link #isHapticFeedbackEnabled()} is true.
13117 *
13118 * @param feedbackConstant One of the constants defined in
13119 * {@link HapticFeedbackConstants}
13120 */
13121 public boolean performHapticFeedback(int feedbackConstant) {
13122 return performHapticFeedback(feedbackConstant, 0);
13123 }
13124
13125 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013126 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013127 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013128 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013129 *
13130 * @param feedbackConstant One of the constants defined in
13131 * {@link HapticFeedbackConstants}
13132 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13133 */
13134 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13135 if (mAttachInfo == null) {
13136 return false;
13137 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013138 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013139 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013140 && !isHapticFeedbackEnabled()) {
13141 return false;
13142 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013143 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13144 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013145 }
13146
13147 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013148 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013149 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13150 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013151 */
13152 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013153 if (visibility != mSystemUiVisibility) {
13154 mSystemUiVisibility = visibility;
13155 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13156 mParent.recomputeViewAttributes(this);
13157 }
Joe Onorato664644d2011-01-23 17:53:23 -080013158 }
13159 }
13160
13161 /**
13162 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013163 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13164 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013165 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013166 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013167 return mSystemUiVisibility;
13168 }
13169
Scott Mainec6331b2011-05-24 16:55:56 -070013170 /**
13171 * Set a listener to receive callbacks when the visibility of the system bar changes.
13172 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13173 */
Joe Onorato664644d2011-01-23 17:53:23 -080013174 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013175 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013176 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13177 mParent.recomputeViewAttributes(this);
13178 }
13179 }
13180
13181 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013182 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13183 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013184 */
13185 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013186 ListenerInfo li = mListenerInfo;
13187 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13188 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013189 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013190 }
13191 }
13192
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013193 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13194 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13195 if (val != mSystemUiVisibility) {
13196 setSystemUiVisibility(val);
13197 }
13198 }
13199
Joe Onorato664644d2011-01-23 17:53:23 -080013200 /**
Joe Malin32736f02011-01-19 16:14:20 -080013201 * Creates an image that the system displays during the drag and drop
13202 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13203 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13204 * appearance as the given View. The default also positions the center of the drag shadow
13205 * directly under the touch point. If no View is provided (the constructor with no parameters
13206 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13207 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13208 * default is an invisible drag shadow.
13209 * <p>
13210 * You are not required to use the View you provide to the constructor as the basis of the
13211 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13212 * anything you want as the drag shadow.
13213 * </p>
13214 * <p>
13215 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13216 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13217 * size and position of the drag shadow. It uses this data to construct a
13218 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13219 * so that your application can draw the shadow image in the Canvas.
13220 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013221 *
13222 * <div class="special reference">
13223 * <h3>Developer Guides</h3>
13224 * <p>For a guide to implementing drag and drop features, read the
13225 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13226 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013227 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013228 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013229 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013230
13231 /**
Joe Malin32736f02011-01-19 16:14:20 -080013232 * Constructs a shadow image builder based on a View. By default, the resulting drag
13233 * shadow will have the same appearance and dimensions as the View, with the touch point
13234 * over the center of the View.
13235 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013236 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013237 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013238 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013239 }
13240
Christopher Tate17ed60c2011-01-18 12:50:26 -080013241 /**
13242 * Construct a shadow builder object with no associated View. This
13243 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13244 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13245 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013246 * reference to any View object. If they are not overridden, then the result is an
13247 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013248 */
13249 public DragShadowBuilder() {
13250 mView = new WeakReference<View>(null);
13251 }
13252
13253 /**
13254 * Returns the View object that had been passed to the
13255 * {@link #View.DragShadowBuilder(View)}
13256 * constructor. If that View parameter was {@code null} or if the
13257 * {@link #View.DragShadowBuilder()}
13258 * constructor was used to instantiate the builder object, this method will return
13259 * null.
13260 *
13261 * @return The View object associate with this builder object.
13262 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013263 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013264 final public View getView() {
13265 return mView.get();
13266 }
13267
Christopher Tate2c095f32010-10-04 14:13:40 -070013268 /**
Joe Malin32736f02011-01-19 16:14:20 -080013269 * Provides the metrics for the shadow image. These include the dimensions of
13270 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013271 * be centered under the touch location while dragging.
13272 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013273 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013274 * same as the dimensions of the View itself and centers the shadow under
13275 * the touch point.
13276 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013277 *
Joe Malin32736f02011-01-19 16:14:20 -080013278 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13279 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13280 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13281 * image.
13282 *
13283 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13284 * shadow image that should be underneath the touch point during the drag and drop
13285 * operation. Your application must set {@link android.graphics.Point#x} to the
13286 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013287 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013288 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013289 final View view = mView.get();
13290 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013291 shadowSize.set(view.getWidth(), view.getHeight());
13292 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013293 } else {
13294 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13295 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013296 }
13297
13298 /**
Joe Malin32736f02011-01-19 16:14:20 -080013299 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13300 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013301 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013302 *
Joe Malin32736f02011-01-19 16:14:20 -080013303 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013304 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013305 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013306 final View view = mView.get();
13307 if (view != null) {
13308 view.draw(canvas);
13309 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013310 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013311 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013312 }
13313 }
13314
13315 /**
Joe Malin32736f02011-01-19 16:14:20 -080013316 * Starts a drag and drop operation. When your application calls this method, it passes a
13317 * {@link android.view.View.DragShadowBuilder} object to the system. The
13318 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13319 * to get metrics for the drag shadow, and then calls the object's
13320 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13321 * <p>
13322 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13323 * drag events to all the View objects in your application that are currently visible. It does
13324 * this either by calling the View object's drag listener (an implementation of
13325 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13326 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13327 * Both are passed a {@link android.view.DragEvent} object that has a
13328 * {@link android.view.DragEvent#getAction()} value of
13329 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13330 * </p>
13331 * <p>
13332 * Your application can invoke startDrag() on any attached View object. The View object does not
13333 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13334 * be related to the View the user selected for dragging.
13335 * </p>
13336 * @param data A {@link android.content.ClipData} object pointing to the data to be
13337 * transferred by the drag and drop operation.
13338 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13339 * drag shadow.
13340 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13341 * drop operation. This Object is put into every DragEvent object sent by the system during the
13342 * current drag.
13343 * <p>
13344 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13345 * to the target Views. For example, it can contain flags that differentiate between a
13346 * a copy operation and a move operation.
13347 * </p>
13348 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13349 * so the parameter should be set to 0.
13350 * @return {@code true} if the method completes successfully, or
13351 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13352 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013353 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013354 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013355 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013356 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013357 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013358 }
13359 boolean okay = false;
13360
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013361 Point shadowSize = new Point();
13362 Point shadowTouchPoint = new Point();
13363 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013364
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013365 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13366 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13367 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013368 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013369
Chris Tatea32dcf72010-10-14 12:13:50 -070013370 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013371 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13372 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013373 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013374 Surface surface = new Surface();
13375 try {
13376 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013377 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013378 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013379 + " surface=" + surface);
13380 if (token != null) {
13381 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013382 try {
Chris Tate6b391282010-10-14 15:48:59 -070013383 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013384 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013385 } finally {
13386 surface.unlockCanvasAndPost(canvas);
13387 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013388
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013389 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013390
13391 // Cache the local state object for delivery with DragEvents
13392 root.setLocalDragState(myLocalState);
13393
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013394 // repurpose 'shadowSize' for the last touch point
13395 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013396
Christopher Tatea53146c2010-09-07 11:57:52 -070013397 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013398 shadowSize.x, shadowSize.y,
13399 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013400 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013401
13402 // Off and running! Release our local surface instance; the drag
13403 // shadow surface is now managed by the system process.
13404 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013405 }
13406 } catch (Exception e) {
13407 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13408 surface.destroy();
13409 }
13410
13411 return okay;
13412 }
13413
Christopher Tatea53146c2010-09-07 11:57:52 -070013414 /**
Joe Malin32736f02011-01-19 16:14:20 -080013415 * Handles drag events sent by the system following a call to
13416 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13417 *<p>
13418 * When the system calls this method, it passes a
13419 * {@link android.view.DragEvent} object. A call to
13420 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13421 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13422 * operation.
13423 * @param event The {@link android.view.DragEvent} sent by the system.
13424 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13425 * in DragEvent, indicating the type of drag event represented by this object.
13426 * @return {@code true} if the method was successful, otherwise {@code false}.
13427 * <p>
13428 * The method should return {@code true} in response to an action type of
13429 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13430 * operation.
13431 * </p>
13432 * <p>
13433 * The method should also return {@code true} in response to an action type of
13434 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13435 * {@code false} if it didn't.
13436 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013437 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013438 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013439 return false;
13440 }
13441
13442 /**
Joe Malin32736f02011-01-19 16:14:20 -080013443 * Detects if this View is enabled and has a drag event listener.
13444 * If both are true, then it calls the drag event listener with the
13445 * {@link android.view.DragEvent} it received. If the drag event listener returns
13446 * {@code true}, then dispatchDragEvent() returns {@code true}.
13447 * <p>
13448 * For all other cases, the method calls the
13449 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13450 * method and returns its result.
13451 * </p>
13452 * <p>
13453 * This ensures that a drag event is always consumed, even if the View does not have a drag
13454 * event listener. However, if the View has a listener and the listener returns true, then
13455 * onDragEvent() is not called.
13456 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013457 */
13458 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013459 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013460 ListenerInfo li = mListenerInfo;
13461 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13462 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013463 return true;
13464 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013465 return onDragEvent(event);
13466 }
13467
Christopher Tate3d4bf172011-03-28 16:16:46 -070013468 boolean canAcceptDrag() {
13469 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13470 }
13471
Christopher Tatea53146c2010-09-07 11:57:52 -070013472 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013473 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13474 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013475 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013476 */
13477 public void onCloseSystemDialogs(String reason) {
13478 }
Joe Malin32736f02011-01-19 16:14:20 -080013479
Dianne Hackbornffa42482009-09-23 22:20:11 -070013480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013481 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013482 * update a Region being computed for
13483 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013484 * that any non-transparent parts of the Drawable are removed from the
13485 * given transparent region.
13486 *
13487 * @param dr The Drawable whose transparency is to be applied to the region.
13488 * @param region A Region holding the current transparency information,
13489 * where any parts of the region that are set are considered to be
13490 * transparent. On return, this region will be modified to have the
13491 * transparency information reduced by the corresponding parts of the
13492 * Drawable that are not transparent.
13493 * {@hide}
13494 */
13495 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13496 if (DBG) {
13497 Log.i("View", "Getting transparent region for: " + this);
13498 }
13499 final Region r = dr.getTransparentRegion();
13500 final Rect db = dr.getBounds();
13501 final AttachInfo attachInfo = mAttachInfo;
13502 if (r != null && attachInfo != null) {
13503 final int w = getRight()-getLeft();
13504 final int h = getBottom()-getTop();
13505 if (db.left > 0) {
13506 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13507 r.op(0, 0, db.left, h, Region.Op.UNION);
13508 }
13509 if (db.right < w) {
13510 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13511 r.op(db.right, 0, w, h, Region.Op.UNION);
13512 }
13513 if (db.top > 0) {
13514 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13515 r.op(0, 0, w, db.top, Region.Op.UNION);
13516 }
13517 if (db.bottom < h) {
13518 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13519 r.op(0, db.bottom, w, h, Region.Op.UNION);
13520 }
13521 final int[] location = attachInfo.mTransparentLocation;
13522 getLocationInWindow(location);
13523 r.translate(location[0], location[1]);
13524 region.op(r, Region.Op.INTERSECT);
13525 } else {
13526 region.op(db, Region.Op.DIFFERENCE);
13527 }
13528 }
13529
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013530 private void checkForLongClick(int delayOffset) {
13531 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13532 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013533
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013534 if (mPendingCheckForLongPress == null) {
13535 mPendingCheckForLongPress = new CheckForLongPress();
13536 }
13537 mPendingCheckForLongPress.rememberWindowAttachCount();
13538 postDelayed(mPendingCheckForLongPress,
13539 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013541 }
13542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013543 /**
13544 * Inflate a view from an XML resource. This convenience method wraps the {@link
13545 * LayoutInflater} class, which provides a full range of options for view inflation.
13546 *
13547 * @param context The Context object for your activity or application.
13548 * @param resource The resource ID to inflate
13549 * @param root A view group that will be the parent. Used to properly inflate the
13550 * layout_* parameters.
13551 * @see LayoutInflater
13552 */
13553 public static View inflate(Context context, int resource, ViewGroup root) {
13554 LayoutInflater factory = LayoutInflater.from(context);
13555 return factory.inflate(resource, root);
13556 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558 /**
Adam Powell637d3372010-08-25 14:37:03 -070013559 * Scroll the view with standard behavior for scrolling beyond the normal
13560 * content boundaries. Views that call this method should override
13561 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13562 * results of an over-scroll operation.
13563 *
13564 * Views can use this method to handle any touch or fling-based scrolling.
13565 *
13566 * @param deltaX Change in X in pixels
13567 * @param deltaY Change in Y in pixels
13568 * @param scrollX Current X scroll value in pixels before applying deltaX
13569 * @param scrollY Current Y scroll value in pixels before applying deltaY
13570 * @param scrollRangeX Maximum content scroll range along the X axis
13571 * @param scrollRangeY Maximum content scroll range along the Y axis
13572 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13573 * along the X axis.
13574 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13575 * along the Y axis.
13576 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13577 * @return true if scrolling was clamped to an over-scroll boundary along either
13578 * axis, false otherwise.
13579 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013580 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013581 protected boolean overScrollBy(int deltaX, int deltaY,
13582 int scrollX, int scrollY,
13583 int scrollRangeX, int scrollRangeY,
13584 int maxOverScrollX, int maxOverScrollY,
13585 boolean isTouchEvent) {
13586 final int overScrollMode = mOverScrollMode;
13587 final boolean canScrollHorizontal =
13588 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13589 final boolean canScrollVertical =
13590 computeVerticalScrollRange() > computeVerticalScrollExtent();
13591 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13592 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13593 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13594 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13595
13596 int newScrollX = scrollX + deltaX;
13597 if (!overScrollHorizontal) {
13598 maxOverScrollX = 0;
13599 }
13600
13601 int newScrollY = scrollY + deltaY;
13602 if (!overScrollVertical) {
13603 maxOverScrollY = 0;
13604 }
13605
13606 // Clamp values if at the limits and record
13607 final int left = -maxOverScrollX;
13608 final int right = maxOverScrollX + scrollRangeX;
13609 final int top = -maxOverScrollY;
13610 final int bottom = maxOverScrollY + scrollRangeY;
13611
13612 boolean clampedX = false;
13613 if (newScrollX > right) {
13614 newScrollX = right;
13615 clampedX = true;
13616 } else if (newScrollX < left) {
13617 newScrollX = left;
13618 clampedX = true;
13619 }
13620
13621 boolean clampedY = false;
13622 if (newScrollY > bottom) {
13623 newScrollY = bottom;
13624 clampedY = true;
13625 } else if (newScrollY < top) {
13626 newScrollY = top;
13627 clampedY = true;
13628 }
13629
13630 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13631
13632 return clampedX || clampedY;
13633 }
13634
13635 /**
13636 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13637 * respond to the results of an over-scroll operation.
13638 *
13639 * @param scrollX New X scroll value in pixels
13640 * @param scrollY New Y scroll value in pixels
13641 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13642 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13643 */
13644 protected void onOverScrolled(int scrollX, int scrollY,
13645 boolean clampedX, boolean clampedY) {
13646 // Intentionally empty.
13647 }
13648
13649 /**
13650 * Returns the over-scroll mode for this view. The result will be
13651 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13652 * (allow over-scrolling only if the view content is larger than the container),
13653 * or {@link #OVER_SCROLL_NEVER}.
13654 *
13655 * @return This view's over-scroll mode.
13656 */
13657 public int getOverScrollMode() {
13658 return mOverScrollMode;
13659 }
13660
13661 /**
13662 * Set the over-scroll mode for this view. Valid over-scroll modes are
13663 * {@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 * Setting the over-scroll mode of a view will have an effect only if the
13668 * view is capable of scrolling.
13669 *
13670 * @param overScrollMode The new over-scroll mode for this view.
13671 */
13672 public void setOverScrollMode(int overScrollMode) {
13673 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13674 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13675 overScrollMode != OVER_SCROLL_NEVER) {
13676 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13677 }
13678 mOverScrollMode = overScrollMode;
13679 }
13680
13681 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013682 * Gets a scale factor that determines the distance the view should scroll
13683 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13684 * @return The vertical scroll scale factor.
13685 * @hide
13686 */
13687 protected float getVerticalScrollFactor() {
13688 if (mVerticalScrollFactor == 0) {
13689 TypedValue outValue = new TypedValue();
13690 if (!mContext.getTheme().resolveAttribute(
13691 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13692 throw new IllegalStateException(
13693 "Expected theme to define listPreferredItemHeight.");
13694 }
13695 mVerticalScrollFactor = outValue.getDimension(
13696 mContext.getResources().getDisplayMetrics());
13697 }
13698 return mVerticalScrollFactor;
13699 }
13700
13701 /**
13702 * Gets a scale factor that determines the distance the view should scroll
13703 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13704 * @return The horizontal scroll scale factor.
13705 * @hide
13706 */
13707 protected float getHorizontalScrollFactor() {
13708 // TODO: Should use something else.
13709 return getVerticalScrollFactor();
13710 }
13711
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013712 /**
13713 * Return the value specifying the text direction or policy that was set with
13714 * {@link #setTextDirection(int)}.
13715 *
13716 * @return the defined text direction. It can be one of:
13717 *
13718 * {@link #TEXT_DIRECTION_INHERIT},
13719 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13720 * {@link #TEXT_DIRECTION_ANY_RTL},
13721 * {@link #TEXT_DIRECTION_LTR},
13722 * {@link #TEXT_DIRECTION_RTL},
13723 *
13724 * @hide
13725 */
13726 public int getTextDirection() {
13727 return mTextDirection;
13728 }
13729
13730 /**
13731 * Set the text direction.
13732 *
13733 * @param textDirection the direction to set. Should be one of:
13734 *
13735 * {@link #TEXT_DIRECTION_INHERIT},
13736 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13737 * {@link #TEXT_DIRECTION_ANY_RTL},
13738 * {@link #TEXT_DIRECTION_LTR},
13739 * {@link #TEXT_DIRECTION_RTL},
13740 *
13741 * @hide
13742 */
13743 public void setTextDirection(int textDirection) {
13744 if (textDirection != mTextDirection) {
13745 mTextDirection = textDirection;
13746 resetResolvedTextDirection();
13747 requestLayout();
13748 }
13749 }
13750
13751 /**
13752 * Return the resolved text direction.
13753 *
13754 * @return the resolved text direction. Return one of:
13755 *
Doug Feltcb3791202011-07-07 11:57:48 -070013756 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13757 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013758 * {@link #TEXT_DIRECTION_LTR},
13759 * {@link #TEXT_DIRECTION_RTL},
13760 *
13761 * @hide
13762 */
13763 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013764 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013765 resolveTextDirection();
13766 }
13767 return mResolvedTextDirection;
13768 }
13769
13770 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013771 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013772 *
13773 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013774 */
13775 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013776 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13777 mResolvedTextDirection = mTextDirection;
13778 return;
13779 }
13780 if (mParent != null && mParent instanceof ViewGroup) {
13781 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13782 return;
13783 }
13784 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013785 }
13786
13787 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013788 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013789 *
13790 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013791 */
13792 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013793 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013794 }
13795
Chet Haaseb39f0512011-05-24 14:36:40 -070013796 //
13797 // Properties
13798 //
13799 /**
13800 * A Property wrapper around the <code>alpha</code> functionality handled by the
13801 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13802 */
Romain Guy02ccac62011-06-24 13:20:23 -070013803 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013804 @Override
13805 public void setValue(View object, float value) {
13806 object.setAlpha(value);
13807 }
13808
13809 @Override
13810 public Float get(View object) {
13811 return object.getAlpha();
13812 }
13813 };
13814
13815 /**
13816 * A Property wrapper around the <code>translationX</code> functionality handled by the
13817 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13818 */
13819 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13820 @Override
13821 public void setValue(View object, float value) {
13822 object.setTranslationX(value);
13823 }
13824
13825 @Override
13826 public Float get(View object) {
13827 return object.getTranslationX();
13828 }
13829 };
13830
13831 /**
13832 * A Property wrapper around the <code>translationY</code> functionality handled by the
13833 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13834 */
13835 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13836 @Override
13837 public void setValue(View object, float value) {
13838 object.setTranslationY(value);
13839 }
13840
13841 @Override
13842 public Float get(View object) {
13843 return object.getTranslationY();
13844 }
13845 };
13846
13847 /**
13848 * A Property wrapper around the <code>x</code> functionality handled by the
13849 * {@link View#setX(float)} and {@link View#getX()} methods.
13850 */
13851 public static Property<View, Float> X = new FloatProperty<View>("x") {
13852 @Override
13853 public void setValue(View object, float value) {
13854 object.setX(value);
13855 }
13856
13857 @Override
13858 public Float get(View object) {
13859 return object.getX();
13860 }
13861 };
13862
13863 /**
13864 * A Property wrapper around the <code>y</code> functionality handled by the
13865 * {@link View#setY(float)} and {@link View#getY()} methods.
13866 */
13867 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13868 @Override
13869 public void setValue(View object, float value) {
13870 object.setY(value);
13871 }
13872
13873 @Override
13874 public Float get(View object) {
13875 return object.getY();
13876 }
13877 };
13878
13879 /**
13880 * A Property wrapper around the <code>rotation</code> functionality handled by the
13881 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13882 */
13883 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13884 @Override
13885 public void setValue(View object, float value) {
13886 object.setRotation(value);
13887 }
13888
13889 @Override
13890 public Float get(View object) {
13891 return object.getRotation();
13892 }
13893 };
13894
13895 /**
13896 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13897 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13898 */
13899 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13900 @Override
13901 public void setValue(View object, float value) {
13902 object.setRotationX(value);
13903 }
13904
13905 @Override
13906 public Float get(View object) {
13907 return object.getRotationX();
13908 }
13909 };
13910
13911 /**
13912 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13913 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13914 */
13915 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13916 @Override
13917 public void setValue(View object, float value) {
13918 object.setRotationY(value);
13919 }
13920
13921 @Override
13922 public Float get(View object) {
13923 return object.getRotationY();
13924 }
13925 };
13926
13927 /**
13928 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13929 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13930 */
13931 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13932 @Override
13933 public void setValue(View object, float value) {
13934 object.setScaleX(value);
13935 }
13936
13937 @Override
13938 public Float get(View object) {
13939 return object.getScaleX();
13940 }
13941 };
13942
13943 /**
13944 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13945 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13946 */
13947 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13948 @Override
13949 public void setValue(View object, float value) {
13950 object.setScaleY(value);
13951 }
13952
13953 @Override
13954 public Float get(View object) {
13955 return object.getScaleY();
13956 }
13957 };
13958
Jeff Brown33bbfd22011-02-24 20:55:35 -080013959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13961 * Each MeasureSpec represents a requirement for either the width or the height.
13962 * A MeasureSpec is comprised of a size and a mode. There are three possible
13963 * modes:
13964 * <dl>
13965 * <dt>UNSPECIFIED</dt>
13966 * <dd>
13967 * The parent has not imposed any constraint on the child. It can be whatever size
13968 * it wants.
13969 * </dd>
13970 *
13971 * <dt>EXACTLY</dt>
13972 * <dd>
13973 * The parent has determined an exact size for the child. The child is going to be
13974 * given those bounds regardless of how big it wants to be.
13975 * </dd>
13976 *
13977 * <dt>AT_MOST</dt>
13978 * <dd>
13979 * The child can be as large as it wants up to the specified size.
13980 * </dd>
13981 * </dl>
13982 *
13983 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13984 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13985 */
13986 public static class MeasureSpec {
13987 private static final int MODE_SHIFT = 30;
13988 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13989
13990 /**
13991 * Measure specification mode: The parent has not imposed any constraint
13992 * on the child. It can be whatever size it wants.
13993 */
13994 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13995
13996 /**
13997 * Measure specification mode: The parent has determined an exact size
13998 * for the child. The child is going to be given those bounds regardless
13999 * of how big it wants to be.
14000 */
14001 public static final int EXACTLY = 1 << MODE_SHIFT;
14002
14003 /**
14004 * Measure specification mode: The child can be as large as it wants up
14005 * to the specified size.
14006 */
14007 public static final int AT_MOST = 2 << MODE_SHIFT;
14008
14009 /**
14010 * Creates a measure specification based on the supplied size and mode.
14011 *
14012 * The mode must always be one of the following:
14013 * <ul>
14014 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14015 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14016 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14017 * </ul>
14018 *
14019 * @param size the size of the measure specification
14020 * @param mode the mode of the measure specification
14021 * @return the measure specification based on size and mode
14022 */
14023 public static int makeMeasureSpec(int size, int mode) {
14024 return size + mode;
14025 }
14026
14027 /**
14028 * Extracts the mode from the supplied measure specification.
14029 *
14030 * @param measureSpec the measure specification to extract the mode from
14031 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14032 * {@link android.view.View.MeasureSpec#AT_MOST} or
14033 * {@link android.view.View.MeasureSpec#EXACTLY}
14034 */
14035 public static int getMode(int measureSpec) {
14036 return (measureSpec & MODE_MASK);
14037 }
14038
14039 /**
14040 * Extracts the size from the supplied measure specification.
14041 *
14042 * @param measureSpec the measure specification to extract the size from
14043 * @return the size in pixels defined in the supplied measure specification
14044 */
14045 public static int getSize(int measureSpec) {
14046 return (measureSpec & ~MODE_MASK);
14047 }
14048
14049 /**
14050 * Returns a String representation of the specified measure
14051 * specification.
14052 *
14053 * @param measureSpec the measure specification to convert to a String
14054 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14055 */
14056 public static String toString(int measureSpec) {
14057 int mode = getMode(measureSpec);
14058 int size = getSize(measureSpec);
14059
14060 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14061
14062 if (mode == UNSPECIFIED)
14063 sb.append("UNSPECIFIED ");
14064 else if (mode == EXACTLY)
14065 sb.append("EXACTLY ");
14066 else if (mode == AT_MOST)
14067 sb.append("AT_MOST ");
14068 else
14069 sb.append(mode).append(" ");
14070
14071 sb.append(size);
14072 return sb.toString();
14073 }
14074 }
14075
14076 class CheckForLongPress implements Runnable {
14077
14078 private int mOriginalWindowAttachCount;
14079
14080 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014081 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 && mOriginalWindowAttachCount == mWindowAttachCount) {
14083 if (performLongClick()) {
14084 mHasPerformedLongPress = true;
14085 }
14086 }
14087 }
14088
14089 public void rememberWindowAttachCount() {
14090 mOriginalWindowAttachCount = mWindowAttachCount;
14091 }
14092 }
Joe Malin32736f02011-01-19 16:14:20 -080014093
Adam Powelle14579b2009-12-16 18:39:52 -080014094 private final class CheckForTap implements Runnable {
14095 public void run() {
14096 mPrivateFlags &= ~PREPRESSED;
14097 mPrivateFlags |= PRESSED;
14098 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014099 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014100 }
14101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014102
Adam Powella35d7682010-03-12 14:48:13 -080014103 private final class PerformClick implements Runnable {
14104 public void run() {
14105 performClick();
14106 }
14107 }
14108
Dianne Hackborn63042d62011-01-26 18:56:29 -080014109 /** @hide */
14110 public void hackTurnOffWindowResizeAnim(boolean off) {
14111 mAttachInfo.mTurnOffWindowResizeAnim = off;
14112 }
Joe Malin32736f02011-01-19 16:14:20 -080014113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014115 * This method returns a ViewPropertyAnimator object, which can be used to animate
14116 * specific properties on this View.
14117 *
14118 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14119 */
14120 public ViewPropertyAnimator animate() {
14121 if (mAnimator == null) {
14122 mAnimator = new ViewPropertyAnimator(this);
14123 }
14124 return mAnimator;
14125 }
14126
14127 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 * Interface definition for a callback to be invoked when a key event is
14129 * dispatched to this view. The callback will be invoked before the key
14130 * event is given to the view.
14131 */
14132 public interface OnKeyListener {
14133 /**
14134 * Called when a key is dispatched to a view. This allows listeners to
14135 * get a chance to respond before the target view.
14136 *
14137 * @param v The view the key has been dispatched to.
14138 * @param keyCode The code for the physical key that was pressed
14139 * @param event The KeyEvent object containing full information about
14140 * the event.
14141 * @return True if the listener has consumed the event, false otherwise.
14142 */
14143 boolean onKey(View v, int keyCode, KeyEvent event);
14144 }
14145
14146 /**
14147 * Interface definition for a callback to be invoked when a touch event is
14148 * dispatched to this view. The callback will be invoked before the touch
14149 * event is given to the view.
14150 */
14151 public interface OnTouchListener {
14152 /**
14153 * Called when a touch event is dispatched to a view. This allows listeners to
14154 * get a chance to respond before the target view.
14155 *
14156 * @param v The view the touch event has been dispatched to.
14157 * @param event The MotionEvent object containing full information about
14158 * the event.
14159 * @return True if the listener has consumed the event, false otherwise.
14160 */
14161 boolean onTouch(View v, MotionEvent event);
14162 }
14163
14164 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014165 * Interface definition for a callback to be invoked when a hover event is
14166 * dispatched to this view. The callback will be invoked before the hover
14167 * event is given to the view.
14168 */
14169 public interface OnHoverListener {
14170 /**
14171 * Called when a hover event is dispatched to a view. This allows listeners to
14172 * get a chance to respond before the target view.
14173 *
14174 * @param v The view the hover event has been dispatched to.
14175 * @param event The MotionEvent object containing full information about
14176 * the event.
14177 * @return True if the listener has consumed the event, false otherwise.
14178 */
14179 boolean onHover(View v, MotionEvent event);
14180 }
14181
14182 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014183 * Interface definition for a callback to be invoked when a generic motion event is
14184 * dispatched to this view. The callback will be invoked before the generic motion
14185 * event is given to the view.
14186 */
14187 public interface OnGenericMotionListener {
14188 /**
14189 * Called when a generic motion event is dispatched to a view. This allows listeners to
14190 * get a chance to respond before the target view.
14191 *
14192 * @param v The view the generic motion event has been dispatched to.
14193 * @param event The MotionEvent object containing full information about
14194 * the event.
14195 * @return True if the listener has consumed the event, false otherwise.
14196 */
14197 boolean onGenericMotion(View v, MotionEvent event);
14198 }
14199
14200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014201 * Interface definition for a callback to be invoked when a view has been clicked and held.
14202 */
14203 public interface OnLongClickListener {
14204 /**
14205 * Called when a view has been clicked and held.
14206 *
14207 * @param v The view that was clicked and held.
14208 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014209 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014210 */
14211 boolean onLongClick(View v);
14212 }
14213
14214 /**
Chris Tate32affef2010-10-18 15:29:21 -070014215 * Interface definition for a callback to be invoked when a drag is being dispatched
14216 * to this view. The callback will be invoked before the hosting view's own
14217 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14218 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014219 *
14220 * <div class="special reference">
14221 * <h3>Developer Guides</h3>
14222 * <p>For a guide to implementing drag and drop features, read the
14223 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14224 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014225 */
14226 public interface OnDragListener {
14227 /**
14228 * Called when a drag event is dispatched to a view. This allows listeners
14229 * to get a chance to override base View behavior.
14230 *
Joe Malin32736f02011-01-19 16:14:20 -080014231 * @param v The View that received the drag event.
14232 * @param event The {@link android.view.DragEvent} object for the drag event.
14233 * @return {@code true} if the drag event was handled successfully, or {@code false}
14234 * if the drag event was not handled. Note that {@code false} will trigger the View
14235 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014236 */
14237 boolean onDrag(View v, DragEvent event);
14238 }
14239
14240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014241 * Interface definition for a callback to be invoked when the focus state of
14242 * a view changed.
14243 */
14244 public interface OnFocusChangeListener {
14245 /**
14246 * Called when the focus state of a view has changed.
14247 *
14248 * @param v The view whose state has changed.
14249 * @param hasFocus The new focus state of v.
14250 */
14251 void onFocusChange(View v, boolean hasFocus);
14252 }
14253
14254 /**
14255 * Interface definition for a callback to be invoked when a view is clicked.
14256 */
14257 public interface OnClickListener {
14258 /**
14259 * Called when a view has been clicked.
14260 *
14261 * @param v The view that was clicked.
14262 */
14263 void onClick(View v);
14264 }
14265
14266 /**
14267 * Interface definition for a callback to be invoked when the context menu
14268 * for this view is being built.
14269 */
14270 public interface OnCreateContextMenuListener {
14271 /**
14272 * Called when the context menu for this view is being built. It is not
14273 * safe to hold onto the menu after this method returns.
14274 *
14275 * @param menu The context menu that is being built
14276 * @param v The view for which the context menu is being built
14277 * @param menuInfo Extra information about the item for which the
14278 * context menu should be shown. This information will vary
14279 * depending on the class of v.
14280 */
14281 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14282 }
14283
Joe Onorato664644d2011-01-23 17:53:23 -080014284 /**
14285 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014286 * visibility. This reports <strong>global</strong> changes to the system UI
14287 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014288 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014289 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014290 */
14291 public interface OnSystemUiVisibilityChangeListener {
14292 /**
14293 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014294 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014295 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014296 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014297 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14298 * <strong>global</strong> state of the UI visibility flags, not what your
14299 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014300 */
14301 public void onSystemUiVisibilityChange(int visibility);
14302 }
14303
Adam Powell4afd62b2011-02-18 15:02:18 -080014304 /**
14305 * Interface definition for a callback to be invoked when this view is attached
14306 * or detached from its window.
14307 */
14308 public interface OnAttachStateChangeListener {
14309 /**
14310 * Called when the view is attached to a window.
14311 * @param v The view that was attached
14312 */
14313 public void onViewAttachedToWindow(View v);
14314 /**
14315 * Called when the view is detached from a window.
14316 * @param v The view that was detached
14317 */
14318 public void onViewDetachedFromWindow(View v);
14319 }
14320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014321 private final class UnsetPressedState implements Runnable {
14322 public void run() {
14323 setPressed(false);
14324 }
14325 }
14326
14327 /**
14328 * Base class for derived classes that want to save and restore their own
14329 * state in {@link android.view.View#onSaveInstanceState()}.
14330 */
14331 public static class BaseSavedState extends AbsSavedState {
14332 /**
14333 * Constructor used when reading from a parcel. Reads the state of the superclass.
14334 *
14335 * @param source
14336 */
14337 public BaseSavedState(Parcel source) {
14338 super(source);
14339 }
14340
14341 /**
14342 * Constructor called by derived classes when creating their SavedState objects
14343 *
14344 * @param superState The state of the superclass of this view
14345 */
14346 public BaseSavedState(Parcelable superState) {
14347 super(superState);
14348 }
14349
14350 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14351 new Parcelable.Creator<BaseSavedState>() {
14352 public BaseSavedState createFromParcel(Parcel in) {
14353 return new BaseSavedState(in);
14354 }
14355
14356 public BaseSavedState[] newArray(int size) {
14357 return new BaseSavedState[size];
14358 }
14359 };
14360 }
14361
14362 /**
14363 * A set of information given to a view when it is attached to its parent
14364 * window.
14365 */
14366 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367 interface Callbacks {
14368 void playSoundEffect(int effectId);
14369 boolean performHapticFeedback(int effectId, boolean always);
14370 }
14371
14372 /**
14373 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14374 * to a Handler. This class contains the target (View) to invalidate and
14375 * the coordinates of the dirty rectangle.
14376 *
14377 * For performance purposes, this class also implements a pool of up to
14378 * POOL_LIMIT objects that get reused. This reduces memory allocations
14379 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014380 */
Romain Guyd928d682009-03-31 17:52:16 -070014381 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014382 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014383 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14384 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014385 public InvalidateInfo newInstance() {
14386 return new InvalidateInfo();
14387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014388
Romain Guyd928d682009-03-31 17:52:16 -070014389 public void onAcquired(InvalidateInfo element) {
14390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014391
Romain Guyd928d682009-03-31 17:52:16 -070014392 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014393 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014394 }
14395 }, POOL_LIMIT)
14396 );
14397
14398 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014399 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014400
14401 View target;
14402
14403 int left;
14404 int top;
14405 int right;
14406 int bottom;
14407
Romain Guyd928d682009-03-31 17:52:16 -070014408 public void setNextPoolable(InvalidateInfo element) {
14409 mNext = element;
14410 }
14411
14412 public InvalidateInfo getNextPoolable() {
14413 return mNext;
14414 }
14415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014416 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014417 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014418 }
14419
14420 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014421 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014423
14424 public boolean isPooled() {
14425 return mIsPooled;
14426 }
14427
14428 public void setPooled(boolean isPooled) {
14429 mIsPooled = isPooled;
14430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014431 }
14432
14433 final IWindowSession mSession;
14434
14435 final IWindow mWindow;
14436
14437 final IBinder mWindowToken;
14438
14439 final Callbacks mRootCallbacks;
14440
Romain Guy59a12ca2011-06-09 17:48:21 -070014441 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014443 /**
14444 * The top view of the hierarchy.
14445 */
14446 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448 IBinder mPanelParentWindowToken;
14449 Surface mSurface;
14450
Romain Guyb051e892010-09-28 19:09:36 -070014451 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014452 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014453 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014455 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014456 * Scale factor used by the compatibility mode
14457 */
14458 float mApplicationScale;
14459
14460 /**
14461 * Indicates whether the application is in compatibility mode
14462 */
14463 boolean mScalingRequired;
14464
14465 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014466 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014467 */
14468 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014469
Dianne Hackborn63042d62011-01-26 18:56:29 -080014470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014471 * Left position of this view's window
14472 */
14473 int mWindowLeft;
14474
14475 /**
14476 * Top position of this view's window
14477 */
14478 int mWindowTop;
14479
14480 /**
Adam Powell26153a32010-11-08 15:22:27 -080014481 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014482 */
Adam Powell26153a32010-11-08 15:22:27 -080014483 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014484
14485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014486 * For windows that are full-screen but using insets to layout inside
14487 * of the screen decorations, these are the current insets for the
14488 * content of the window.
14489 */
14490 final Rect mContentInsets = new Rect();
14491
14492 /**
14493 * For windows that are full-screen but using insets to layout inside
14494 * of the screen decorations, these are the current insets for the
14495 * actual visible parts of the window.
14496 */
14497 final Rect mVisibleInsets = new Rect();
14498
14499 /**
14500 * The internal insets given by this window. This value is
14501 * supplied by the client (through
14502 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14503 * be given to the window manager when changed to be used in laying
14504 * out windows behind it.
14505 */
14506 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14507 = new ViewTreeObserver.InternalInsetsInfo();
14508
14509 /**
14510 * All views in the window's hierarchy that serve as scroll containers,
14511 * used to determine if the window can be resized or must be panned
14512 * to adjust for a soft input area.
14513 */
14514 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14515
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014516 final KeyEvent.DispatcherState mKeyDispatchState
14517 = new KeyEvent.DispatcherState();
14518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014519 /**
14520 * Indicates whether the view's window currently has the focus.
14521 */
14522 boolean mHasWindowFocus;
14523
14524 /**
14525 * The current visibility of the window.
14526 */
14527 int mWindowVisibility;
14528
14529 /**
14530 * Indicates the time at which drawing started to occur.
14531 */
14532 long mDrawingTime;
14533
14534 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014535 * Indicates whether or not ignoring the DIRTY_MASK flags.
14536 */
14537 boolean mIgnoreDirtyState;
14538
14539 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014540 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14541 * to avoid clearing that flag prematurely.
14542 */
14543 boolean mSetIgnoreDirtyState = false;
14544
14545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 * Indicates whether the view's window is currently in touch mode.
14547 */
14548 boolean mInTouchMode;
14549
14550 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014551 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 * the next time it performs a traversal
14553 */
14554 boolean mRecomputeGlobalAttributes;
14555
14556 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014557 * Always report new attributes at next traversal.
14558 */
14559 boolean mForceReportNewAttributes;
14560
14561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 * Set during a traveral if any views want to keep the screen on.
14563 */
14564 boolean mKeepScreenOn;
14565
14566 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014567 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14568 */
14569 int mSystemUiVisibility;
14570
14571 /**
14572 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14573 * attached.
14574 */
14575 boolean mHasSystemUiListeners;
14576
14577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014578 * Set if the visibility of any views has changed.
14579 */
14580 boolean mViewVisibilityChanged;
14581
14582 /**
14583 * Set to true if a view has been scrolled.
14584 */
14585 boolean mViewScrollChanged;
14586
14587 /**
14588 * Global to the view hierarchy used as a temporary for dealing with
14589 * x/y points in the transparent region computations.
14590 */
14591 final int[] mTransparentLocation = new int[2];
14592
14593 /**
14594 * Global to the view hierarchy used as a temporary for dealing with
14595 * x/y points in the ViewGroup.invalidateChild implementation.
14596 */
14597 final int[] mInvalidateChildLocation = new int[2];
14598
Chet Haasec3aa3612010-06-17 08:50:37 -070014599
14600 /**
14601 * Global to the view hierarchy used as a temporary for dealing with
14602 * x/y location when view is transformed.
14603 */
14604 final float[] mTmpTransformLocation = new float[2];
14605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014606 /**
14607 * The view tree observer used to dispatch global events like
14608 * layout, pre-draw, touch mode change, etc.
14609 */
14610 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14611
14612 /**
14613 * A Canvas used by the view hierarchy to perform bitmap caching.
14614 */
14615 Canvas mCanvas;
14616
14617 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014618 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014619 * handler can be used to pump events in the UI events queue.
14620 */
14621 final Handler mHandler;
14622
14623 /**
14624 * Identifier for messages requesting the view to be invalidated.
14625 * Such messages should be sent to {@link #mHandler}.
14626 */
14627 static final int INVALIDATE_MSG = 0x1;
14628
14629 /**
14630 * Identifier for messages requesting the view to invalidate a region.
14631 * Such messages should be sent to {@link #mHandler}.
14632 */
14633 static final int INVALIDATE_RECT_MSG = 0x2;
14634
14635 /**
14636 * Temporary for use in computing invalidate rectangles while
14637 * calling up the hierarchy.
14638 */
14639 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014640
14641 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014642 * Temporary for use in computing hit areas with transformed views
14643 */
14644 final RectF mTmpTransformRect = new RectF();
14645
14646 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014647 * Temporary list for use in collecting focusable descendents of a view.
14648 */
14649 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014651 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014652 * The id of the window for accessibility purposes.
14653 */
14654 int mAccessibilityWindowId = View.NO_ID;
14655
14656 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014657 * Creates a new set of attachment information with the specified
14658 * events handler and thread.
14659 *
14660 * @param handler the events handler the view must use
14661 */
14662 AttachInfo(IWindowSession session, IWindow window,
14663 Handler handler, Callbacks effectPlayer) {
14664 mSession = session;
14665 mWindow = window;
14666 mWindowToken = window.asBinder();
14667 mHandler = handler;
14668 mRootCallbacks = effectPlayer;
14669 }
14670 }
14671
14672 /**
14673 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14674 * is supported. This avoids keeping too many unused fields in most
14675 * instances of View.</p>
14676 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014677 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014678
Mike Cleronf116bf82009-09-27 19:14:12 -070014679 /**
14680 * Scrollbars are not visible
14681 */
14682 public static final int OFF = 0;
14683
14684 /**
14685 * Scrollbars are visible
14686 */
14687 public static final int ON = 1;
14688
14689 /**
14690 * Scrollbars are fading away
14691 */
14692 public static final int FADING = 2;
14693
14694 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014696 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014697 public int scrollBarDefaultDelayBeforeFade;
14698 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014699
14700 public int scrollBarSize;
14701 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014702 public float[] interpolatorValues;
14703 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014704
14705 public final Paint paint;
14706 public final Matrix matrix;
14707 public Shader shader;
14708
Mike Cleronf116bf82009-09-27 19:14:12 -070014709 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14710
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014711 private static final float[] OPAQUE = { 255 };
14712 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014713
Mike Cleronf116bf82009-09-27 19:14:12 -070014714 /**
14715 * When fading should start. This time moves into the future every time
14716 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14717 */
14718 public long fadeStartTime;
14719
14720
14721 /**
14722 * The current state of the scrollbars: ON, OFF, or FADING
14723 */
14724 public int state = OFF;
14725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014726 private int mLastColor;
14727
Mike Cleronf116bf82009-09-27 19:14:12 -070014728 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014729 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14730 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014731 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14732 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014733
14734 paint = new Paint();
14735 matrix = new Matrix();
14736 // use use a height of 1, and then wack the matrix each time we
14737 // actually use it.
14738 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014740 paint.setShader(shader);
14741 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014742 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014743 }
Romain Guy8506ab42009-06-11 17:35:47 -070014744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014745 public void setFadeColor(int color) {
14746 if (color != 0 && color != mLastColor) {
14747 mLastColor = color;
14748 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014749
Romain Guye55e1a72009-08-27 10:42:26 -070014750 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14751 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014753 paint.setShader(shader);
14754 // Restore the default transfer mode (src_over)
14755 paint.setXfermode(null);
14756 }
14757 }
Joe Malin32736f02011-01-19 16:14:20 -080014758
Mike Cleronf116bf82009-09-27 19:14:12 -070014759 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014760 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014761 if (now >= fadeStartTime) {
14762
14763 // the animation fades the scrollbars out by changing
14764 // the opacity (alpha) from fully opaque to fully
14765 // transparent
14766 int nextFrame = (int) now;
14767 int framesCount = 0;
14768
14769 Interpolator interpolator = scrollBarInterpolator;
14770
14771 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014772 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014773
14774 // End transparent
14775 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014776 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014777
14778 state = FADING;
14779
14780 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014781 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014782 }
14783 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014784 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014785
Svetoslav Ganova0156172011-06-26 17:55:44 -070014786 /**
14787 * Resuable callback for sending
14788 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14789 */
14790 private class SendViewScrolledAccessibilityEvent implements Runnable {
14791 public volatile boolean mIsPending;
14792
14793 public void run() {
14794 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14795 mIsPending = false;
14796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014798
14799 /**
14800 * <p>
14801 * This class represents a delegate that can be registered in a {@link View}
14802 * to enhance accessibility support via composition rather via inheritance.
14803 * It is specifically targeted to widget developers that extend basic View
14804 * classes i.e. classes in package android.view, that would like their
14805 * applications to be backwards compatible.
14806 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080014807 * <div class="special reference">
14808 * <h3>Developer Guides</h3>
14809 * <p>For more information about making applications accessible, read the
14810 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
14811 * developer guide.</p>
14812 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014813 * <p>
14814 * A scenario in which a developer would like to use an accessibility delegate
14815 * is overriding a method introduced in a later API version then the minimal API
14816 * version supported by the application. For example, the method
14817 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14818 * in API version 4 when the accessibility APIs were first introduced. If a
14819 * developer would like his application to run on API version 4 devices (assuming
14820 * all other APIs used by the application are version 4 or lower) and take advantage
14821 * of this method, instead of overriding the method which would break the application's
14822 * backwards compatibility, he can override the corresponding method in this
14823 * delegate and register the delegate in the target View if the API version of
14824 * the system is high enough i.e. the API version is same or higher to the API
14825 * version that introduced
14826 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14827 * </p>
14828 * <p>
14829 * Here is an example implementation:
14830 * </p>
14831 * <code><pre><p>
14832 * if (Build.VERSION.SDK_INT >= 14) {
14833 * // If the API version is equal of higher than the version in
14834 * // which onInitializeAccessibilityNodeInfo was introduced we
14835 * // register a delegate with a customized implementation.
14836 * View view = findViewById(R.id.view_id);
14837 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14838 * public void onInitializeAccessibilityNodeInfo(View host,
14839 * AccessibilityNodeInfo info) {
14840 * // Let the default implementation populate the info.
14841 * super.onInitializeAccessibilityNodeInfo(host, info);
14842 * // Set some other information.
14843 * info.setEnabled(host.isEnabled());
14844 * }
14845 * });
14846 * }
14847 * </code></pre></p>
14848 * <p>
14849 * This delegate contains methods that correspond to the accessibility methods
14850 * in View. If a delegate has been specified the implementation in View hands
14851 * off handling to the corresponding method in this delegate. The default
14852 * implementation the delegate methods behaves exactly as the corresponding
14853 * method in View for the case of no accessibility delegate been set. Hence,
14854 * to customize the behavior of a View method, clients can override only the
14855 * corresponding delegate method without altering the behavior of the rest
14856 * accessibility related methods of the host view.
14857 * </p>
14858 */
14859 public static class AccessibilityDelegate {
14860
14861 /**
14862 * Sends an accessibility event of the given type. If accessibility is not
14863 * enabled this method has no effect.
14864 * <p>
14865 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14866 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14867 * been set.
14868 * </p>
14869 *
14870 * @param host The View hosting the delegate.
14871 * @param eventType The type of the event to send.
14872 *
14873 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14874 */
14875 public void sendAccessibilityEvent(View host, int eventType) {
14876 host.sendAccessibilityEventInternal(eventType);
14877 }
14878
14879 /**
14880 * Sends an accessibility event. This method behaves exactly as
14881 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14882 * empty {@link AccessibilityEvent} and does not perform a check whether
14883 * accessibility is enabled.
14884 * <p>
14885 * The default implementation behaves as
14886 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14887 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14888 * the case of no accessibility delegate been set.
14889 * </p>
14890 *
14891 * @param host The View hosting the delegate.
14892 * @param event The event to send.
14893 *
14894 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14895 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14896 */
14897 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14898 host.sendAccessibilityEventUncheckedInternal(event);
14899 }
14900
14901 /**
14902 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14903 * to its children for adding their text content to the event.
14904 * <p>
14905 * The default implementation behaves as
14906 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14907 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14908 * the case of no accessibility delegate been set.
14909 * </p>
14910 *
14911 * @param host The View hosting the delegate.
14912 * @param event The event.
14913 * @return True if the event population was completed.
14914 *
14915 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14916 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14917 */
14918 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14919 return host.dispatchPopulateAccessibilityEventInternal(event);
14920 }
14921
14922 /**
14923 * Gives a chance to the host View to populate the accessibility event with its
14924 * text content.
14925 * <p>
14926 * The default implementation behaves as
14927 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14928 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14929 * the case of no accessibility delegate been set.
14930 * </p>
14931 *
14932 * @param host The View hosting the delegate.
14933 * @param event The accessibility event which to populate.
14934 *
14935 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14936 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14937 */
14938 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14939 host.onPopulateAccessibilityEventInternal(event);
14940 }
14941
14942 /**
14943 * Initializes an {@link AccessibilityEvent} with information about the
14944 * the host View which is the event source.
14945 * <p>
14946 * The default implementation behaves as
14947 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14948 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14949 * the case of no accessibility delegate been set.
14950 * </p>
14951 *
14952 * @param host The View hosting the delegate.
14953 * @param event The event to initialize.
14954 *
14955 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14956 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14957 */
14958 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14959 host.onInitializeAccessibilityEventInternal(event);
14960 }
14961
14962 /**
14963 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14964 * <p>
14965 * The default implementation behaves as
14966 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14967 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14968 * the case of no accessibility delegate been set.
14969 * </p>
14970 *
14971 * @param host The View hosting the delegate.
14972 * @param info The instance to initialize.
14973 *
14974 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14975 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14976 */
14977 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14978 host.onInitializeAccessibilityNodeInfoInternal(info);
14979 }
14980
14981 /**
14982 * Called when a child of the host View has requested sending an
14983 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14984 * to augment the event.
14985 * <p>
14986 * The default implementation behaves as
14987 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14988 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14989 * the case of no accessibility delegate been set.
14990 * </p>
14991 *
14992 * @param host The View hosting the delegate.
14993 * @param child The child which requests sending the event.
14994 * @param event The event to be sent.
14995 * @return True if the event should be sent
14996 *
14997 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14998 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14999 */
15000 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15001 AccessibilityEvent event) {
15002 return host.onRequestSendAccessibilityEventInternal(child, event);
15003 }
15004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015005}