blob: 9863309c63458d7eaae847db732cb7a63ab013bc [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070064import android.view.accessibility.AccessibilityNodeProvider;
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;
Chet Haase64a48c12012-02-13 16:33:29 -080067import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
74
Doug Feltcb3791202011-07-07 11:57:48 -070075import com.android.internal.R;
76import com.android.internal.util.Predicate;
77import com.android.internal.view.menu.MenuBuilder;
78
Christopher Tatea0374192010-10-05 13:06:41 -070079import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import java.lang.reflect.InvocationTargetException;
81import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070084import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080085import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * <p>
89 * This class represents the basic building block for user interface components. A View
90 * occupies a rectangular area on the screen and is responsible for drawing and
91 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070092 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
94 * are invisible containers that hold other Views (or other ViewGroups) and define
95 * their layout properties.
96 * </p>
97 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For information about using this class to develop your application's user interface,
101 * 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 -0800102 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * <a name="Using"></a>
105 * <h3>Using Views</h3>
106 * <p>
107 * All of the views in a window are arranged in a single tree. You can add views
108 * either from code or by specifying a tree of views in one or more XML layout
109 * files. There are many specialized subclasses of views that act as controls or
110 * are capable of displaying text, images, or other content.
111 * </p>
112 * <p>
113 * Once you have created a tree of views, there are typically a few types of
114 * common operations you may wish to perform:
115 * <ul>
116 * <li><strong>Set properties:</strong> for example setting the text of a
117 * {@link android.widget.TextView}. The available properties and the methods
118 * that set them will vary among the different subclasses of views. Note that
119 * properties that are known at build time can be set in the XML layout
120 * files.</li>
121 * <li><strong>Set focus:</strong> The framework will handled moving focus in
122 * response to user input. To force focus to a specific view, call
123 * {@link #requestFocus}.</li>
124 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
125 * that will be notified when something interesting happens to the view. For
126 * example, all views will let you set a listener to be notified when the view
127 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700128 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
129 * Other view subclasses offer more specialized listeners. For example, a Button
130 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * </ul>
134 * </p>
135 * <p><em>
136 * Note: The Android framework is responsible for measuring, laying out and
137 * drawing views. You should not call methods that perform these actions on
138 * views yourself unless you are actually implementing a
139 * {@link android.view.ViewGroup}.
140 * </em></p>
141 *
142 * <a name="Lifecycle"></a>
143 * <h3>Implementing a Custom View</h3>
144 *
145 * <p>
146 * To implement a custom view, you will usually begin by providing overrides for
147 * some of the standard methods that the framework calls on all views. You do
148 * not need to override all of these methods. In fact, you can start by just
149 * overriding {@link #onDraw(android.graphics.Canvas)}.
150 * <table border="2" width="85%" align="center" cellpadding="5">
151 * <thead>
152 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
153 * </thead>
154 *
155 * <tbody>
156 * <tr>
157 * <td rowspan="2">Creation</td>
158 * <td>Constructors</td>
159 * <td>There is a form of the constructor that are called when the view
160 * is created from code and a form that is called when the view is
161 * inflated from a layout file. The second form should parse and apply
162 * any attributes defined in the layout file.
163 * </td>
164 * </tr>
165 * <tr>
166 * <td><code>{@link #onFinishInflate()}</code></td>
167 * <td>Called after a view and all of its children has been inflated
168 * from XML.</td>
169 * </tr>
170 *
171 * <tr>
172 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700173 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <td>Called to determine the size requirements for this view and all
175 * of its children.
176 * </td>
177 * </tr>
178 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called when this view should assign a size and position to all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when the size of this view has changed.
187 * </td>
188 * </tr>
189 *
190 * <tr>
191 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the view should render its content.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when a new key event occurs.
201 * </td>
202 * </tr>
203 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a key up event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a trackball motion event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a touch screen motion event occurs.
216 * </td>
217 * </tr>
218 *
219 * <tr>
220 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when the view gains or loses focus.
223 * </td>
224 * </tr>
225 *
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the window containing the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="3">Attaching</td>
234 * <td><code>{@link #onAttachedToWindow()}</code></td>
235 * <td>Called when the view is attached to a window.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td><code>{@link #onDetachedFromWindow}</code></td>
241 * <td>Called when the view is detached from its window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700246 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <td>Called when the visibility of the window containing the view
248 * has changed.
249 * </td>
250 * </tr>
251 * </tbody>
252 *
253 * </table>
254 * </p>
255 *
256 * <a name="IDs"></a>
257 * <h3>IDs</h3>
258 * Views may have an integer id associated with them. These ids are typically
259 * assigned in the layout XML files, and are used to find specific views within
260 * the view tree. A common pattern is to:
261 * <ul>
262 * <li>Define a Button in the layout file and assign it a unique ID.
263 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700264 * &lt;Button
265 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700339 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
340 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
342 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <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 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700423 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * 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 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700570 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
571 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700581 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800604 * @attr ref android.R.styleable#View_paddingStart
605 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700635public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
636 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private static final boolean DBG = false;
638
639 /**
640 * The logging tag used by this class with android.util.Log.
641 */
642 protected static final String VIEW_LOG_TAG = "View";
643
644 /**
645 * Used to mark a View that has no ID.
646 */
647 public static final int NO_ID = -1;
648
649 /**
650 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
651 * calling setFlags.
652 */
653 private static final int NOT_FOCUSABLE = 0x00000000;
654
655 /**
656 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
657 * setFlags.
658 */
659 private static final int FOCUSABLE = 0x00000001;
660
661 /**
662 * Mask for use with setFlags indicating bits used for focus.
663 */
664 private static final int FOCUSABLE_MASK = 0x00000001;
665
666 /**
667 * This view will adjust its padding to fit sytem windows (e.g. status bar)
668 */
669 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
670
671 /**
Scott Main812634c22011-07-27 13:22:35 -0700672 * This view is visible.
673 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
674 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public static final int VISIBLE = 0x00000000;
677
678 /**
679 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700680 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
681 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700687 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
688 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public static final int GONE = 0x00000008;
691
692 /**
693 * Mask for use with setFlags indicating bits used for visibility.
694 * {@hide}
695 */
696 static final int VISIBILITY_MASK = 0x0000000C;
697
698 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
699
700 /**
701 * This view is enabled. Intrepretation varies by subclass.
702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int ENABLED = 0x00000000;
706
707 /**
708 * This view is disabled. Intrepretation varies by subclass.
709 * Use with ENABLED_MASK when calling setFlags.
710 * {@hide}
711 */
712 static final int DISABLED = 0x00000020;
713
714 /**
715 * Mask for use with setFlags indicating bits used for indicating whether
716 * this view is enabled
717 * {@hide}
718 */
719 static final int ENABLED_MASK = 0x00000020;
720
721 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700722 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
723 * called and further optimizations will be performed. It is okay to have
724 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 * {@hide}
726 */
727 static final int WILL_NOT_DRAW = 0x00000080;
728
729 /**
730 * Mask for use with setFlags indicating bits used for indicating whether
731 * this view is will draw
732 * {@hide}
733 */
734 static final int DRAW_MASK = 0x00000080;
735
736 /**
737 * <p>This view doesn't show scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_NONE = 0x00000000;
741
742 /**
743 * <p>This view shows horizontal scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
747
748 /**
749 * <p>This view shows vertical scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_VERTICAL = 0x00000200;
753
754 /**
755 * <p>Mask for use with setFlags indicating bits used for indicating which
756 * scrollbars are enabled.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_MASK = 0x00000300;
760
Jeff Brown85a31762010-09-01 17:01:00 -0700761 /**
762 * Indicates that the view should filter touches when its window is obscured.
763 * Refer to the class comments for more information about this security feature.
764 * {@hide}
765 */
766 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
767
768 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
770 /**
771 * <p>This view doesn't show fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_NONE = 0x00000000;
775
776 /**
777 * <p>This view shows horizontal fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
781
782 /**
783 * <p>This view shows vertical fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_VERTICAL = 0x00002000;
787
788 /**
789 * <p>Mask for use with setFlags indicating bits used for indicating which
790 * fading edges are enabled.</p>
791 * {@hide}
792 */
793 static final int FADING_EDGE_MASK = 0x00003000;
794
795 /**
796 * <p>Indicates this view can be clicked. When clickable, a View reacts
797 * to clicks by notifying the OnClickListener.<p>
798 * {@hide}
799 */
800 static final int CLICKABLE = 0x00004000;
801
802 /**
803 * <p>Indicates this view is caching its drawing into a bitmap.</p>
804 * {@hide}
805 */
806 static final int DRAWING_CACHE_ENABLED = 0x00008000;
807
808 /**
809 * <p>Indicates that no icicle should be saved for this view.<p>
810 * {@hide}
811 */
812 static final int SAVE_DISABLED = 0x000010000;
813
814 /**
815 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
816 * property.</p>
817 * {@hide}
818 */
819 static final int SAVE_DISABLED_MASK = 0x000010000;
820
821 /**
822 * <p>Indicates that no drawing cache should ever be created for this view.<p>
823 * {@hide}
824 */
825 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
826
827 /**
828 * <p>Indicates this view can take / keep focus when int touch mode.</p>
829 * {@hide}
830 */
831 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
832
833 /**
834 * <p>Enables low quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
837
838 /**
839 * <p>Enables high quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
842
843 /**
844 * <p>Enables automatic quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
847
848 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
849 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
850 };
851
852 /**
853 * <p>Mask for use with setFlags indicating bits used for the cache
854 * quality property.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
858
859 /**
860 * <p>
861 * Indicates this view can be long clicked. When long clickable, a View
862 * reacts to long clicks by notifying the OnLongClickListener or showing a
863 * context menu.
864 * </p>
865 * {@hide}
866 */
867 static final int LONG_CLICKABLE = 0x00200000;
868
869 /**
870 * <p>Indicates that this view gets its drawable states from its direct parent
871 * and ignores its original internal states.</p>
872 *
873 * @hide
874 */
875 static final int DUPLICATE_PARENT_STATE = 0x00400000;
876
877 /**
878 * The scrollbar style to display the scrollbars inside the content area,
879 * without increasing the padding. The scrollbars will be overlaid with
880 * translucency on the view's content.
881 */
882 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
883
884 /**
885 * The scrollbar style to display the scrollbars inside the padded area,
886 * increasing the padding of the view. The scrollbars will not overlap the
887 * content area of the view.
888 */
889 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
890
891 /**
892 * The scrollbar style to display the scrollbars at the edge of the view,
893 * without increasing the padding. The scrollbars will be overlaid with
894 * translucency.
895 */
896 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
897
898 /**
899 * The scrollbar style to display the scrollbars at the edge of the view,
900 * increasing the padding of the view. The scrollbars will only overlap the
901 * background, if any.
902 */
903 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
904
905 /**
906 * Mask to check if the scrollbar style is overlay or inset.
907 * {@hide}
908 */
909 static final int SCROLLBARS_INSET_MASK = 0x01000000;
910
911 /**
912 * Mask to check if the scrollbar style is inside or outside.
913 * {@hide}
914 */
915 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
916
917 /**
918 * Mask for scrollbar style.
919 * {@hide}
920 */
921 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
922
923 /**
924 * View flag indicating that the screen should remain on while the
925 * window containing this view is visible to the user. This effectively
926 * takes care of automatically setting the WindowManager's
927 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
928 */
929 public static final int KEEP_SCREEN_ON = 0x04000000;
930
931 /**
932 * View flag indicating whether this view should have sound effects enabled
933 * for events such as clicking and touching.
934 */
935 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
936
937 /**
938 * View flag indicating whether this view should have haptic feedback
939 * enabled for events such as long presses.
940 */
941 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
942
943 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700944 * <p>Indicates that the view hierarchy should stop saving state when
945 * it reaches this view. If state saving is initiated immediately at
946 * the view, it will be allowed.
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED = 0x20000000;
950
951 /**
952 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
956
957 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800958 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700959 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800960 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700961 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800962
963 /**
964 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800966 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700967 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800968
969 /**
970 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700971 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800972 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800974
975 /**
976 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800978 */
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 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001501 * Temporary flag, used to enable processing of View properties in the native DisplayList
1502 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1503 * apps.
1504 * @hide
1505 */
Chet Haase9d1992d2012-03-13 11:03:25 -07001506 public static final boolean USE_DISPLAY_LIST_PROPERTIES = false;
Chet Haasea1cff502012-02-21 13:43:44 -08001507
1508 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001509 * Map used to store views' tags.
1510 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001511 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001514 * The next available accessiiblity id.
1515 */
1516 private static int sNextAccessibilityViewId;
1517
1518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 * The animation currently associated with this view.
1520 * @hide
1521 */
1522 protected Animation mCurrentAnimation = null;
1523
1524 /**
1525 * Width as measured during measure pass.
1526 * {@hide}
1527 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001528 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001529 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
1531 /**
1532 * Height as measured during measure pass.
1533 * {@hide}
1534 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001535 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001536 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537
1538 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001539 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1540 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1541 * its display list. This flag, used only when hw accelerated, allows us to clear the
1542 * flag while retaining this information until it's needed (at getDisplayList() time and
1543 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1544 *
1545 * {@hide}
1546 */
1547 boolean mRecreateDisplayList = false;
1548
1549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * The view's identifier.
1551 * {@hide}
1552 *
1553 * @see #setId(int)
1554 * @see #getId()
1555 */
1556 @ViewDebug.ExportedProperty(resolveId = true)
1557 int mID = NO_ID;
1558
1559 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001560 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001561 */
1562 int mAccessibilityViewId = NO_ID;
1563
1564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 * The view's tag.
1566 * {@hide}
1567 *
1568 * @see #setTag(Object)
1569 * @see #getTag()
1570 */
1571 protected Object mTag;
1572
1573 // for mPrivateFlags:
1574 /** {@hide} */
1575 static final int WANTS_FOCUS = 0x00000001;
1576 /** {@hide} */
1577 static final int FOCUSED = 0x00000002;
1578 /** {@hide} */
1579 static final int SELECTED = 0x00000004;
1580 /** {@hide} */
1581 static final int IS_ROOT_NAMESPACE = 0x00000008;
1582 /** {@hide} */
1583 static final int HAS_BOUNDS = 0x00000010;
1584 /** {@hide} */
1585 static final int DRAWN = 0x00000020;
1586 /**
1587 * When this flag is set, this view is running an animation on behalf of its
1588 * children and should therefore not cancel invalidate requests, even if they
1589 * lie outside of this view's bounds.
1590 *
1591 * {@hide}
1592 */
1593 static final int DRAW_ANIMATION = 0x00000040;
1594 /** {@hide} */
1595 static final int SKIP_DRAW = 0x00000080;
1596 /** {@hide} */
1597 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1598 /** {@hide} */
1599 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1600 /** {@hide} */
1601 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1602 /** {@hide} */
1603 static final int MEASURED_DIMENSION_SET = 0x00000800;
1604 /** {@hide} */
1605 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001606 /** {@hide} */
1607 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608
1609 private static final int PRESSED = 0x00004000;
1610
1611 /** {@hide} */
1612 static final int DRAWING_CACHE_VALID = 0x00008000;
1613 /**
1614 * Flag used to indicate that this view should be drawn once more (and only once
1615 * more) after its animation has completed.
1616 * {@hide}
1617 */
1618 static final int ANIMATION_STARTED = 0x00010000;
1619
1620 private static final int SAVE_STATE_CALLED = 0x00020000;
1621
1622 /**
1623 * Indicates that the View returned true when onSetAlpha() was called and that
1624 * the alpha must be restored.
1625 * {@hide}
1626 */
1627 static final int ALPHA_SET = 0x00040000;
1628
1629 /**
1630 * Set by {@link #setScrollContainer(boolean)}.
1631 */
1632 static final int SCROLL_CONTAINER = 0x00080000;
1633
1634 /**
1635 * Set by {@link #setScrollContainer(boolean)}.
1636 */
1637 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1638
1639 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001640 * View flag indicating whether this view was invalidated (fully or partially.)
1641 *
1642 * @hide
1643 */
1644 static final int DIRTY = 0x00200000;
1645
1646 /**
1647 * View flag indicating whether this view was invalidated by an opaque
1648 * invalidate request.
1649 *
1650 * @hide
1651 */
1652 static final int DIRTY_OPAQUE = 0x00400000;
1653
1654 /**
1655 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1656 *
1657 * @hide
1658 */
1659 static final int DIRTY_MASK = 0x00600000;
1660
1661 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001662 * Indicates whether the background is opaque.
1663 *
1664 * @hide
1665 */
1666 static final int OPAQUE_BACKGROUND = 0x00800000;
1667
1668 /**
1669 * Indicates whether the scrollbars are opaque.
1670 *
1671 * @hide
1672 */
1673 static final int OPAQUE_SCROLLBARS = 0x01000000;
1674
1675 /**
1676 * Indicates whether the view is opaque.
1677 *
1678 * @hide
1679 */
1680 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001681
Adam Powelle14579b2009-12-16 18:39:52 -08001682 /**
1683 * Indicates a prepressed state;
1684 * the short time between ACTION_DOWN and recognizing
1685 * a 'real' press. Prepressed is used to recognize quick taps
1686 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001687 *
Adam Powelle14579b2009-12-16 18:39:52 -08001688 * @hide
1689 */
1690 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001691
Adam Powellc9fbaab2010-02-16 17:16:19 -08001692 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001693 * Indicates whether the view is temporarily detached.
1694 *
1695 * @hide
1696 */
1697 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001698
Adam Powell8568c3a2010-04-19 14:26:11 -07001699 /**
1700 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001701 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001702 * @hide
1703 */
1704 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001705
1706 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001707 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1708 * @hide
1709 */
1710 private static final int HOVERED = 0x10000000;
1711
1712 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001713 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1714 * for transform operations
1715 *
1716 * @hide
1717 */
Adam Powellf37df072010-09-17 16:22:49 -07001718 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001719
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001720 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001721 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001722
Chet Haasefd2b0022010-08-06 13:08:56 -07001723 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001724 * Indicates that this view was specifically invalidated, not just dirtied because some
1725 * child view was invalidated. The flag is used to determine when we need to recreate
1726 * a view's display list (as opposed to just returning a reference to its existing
1727 * display list).
1728 *
1729 * @hide
1730 */
1731 static final int INVALIDATED = 0x80000000;
1732
Christopher Tate3d4bf172011-03-28 16:16:46 -07001733 /* Masks for mPrivateFlags2 */
1734
1735 /**
1736 * Indicates that this view has reported that it can accept the current drag's content.
1737 * Cleared when the drag operation concludes.
1738 * @hide
1739 */
1740 static final int DRAG_CAN_ACCEPT = 0x00000001;
1741
1742 /**
1743 * Indicates that this view is currently directly under the drag location in a
1744 * drag-and-drop operation involving content that it can accept. Cleared when
1745 * the drag exits the view, or when the drag operation concludes.
1746 * @hide
1747 */
1748 static final int DRAG_HOVERED = 0x00000002;
1749
Cibu Johny86666632010-02-22 13:01:02 -08001750 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001751 * Indicates whether the view layout direction has been resolved and drawn to the
1752 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001753 *
1754 * @hide
1755 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001756 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1757
1758 /**
1759 * Indicates whether the view layout direction has been resolved.
1760 *
1761 * @hide
1762 */
1763 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1764
Cibu Johny86666632010-02-22 13:01:02 -08001765
Adam Powell539ee872012-02-03 19:00:49 -08001766 /**
1767 * Indicates that the view is tracking some sort of transient state
1768 * that the app should not need to be aware of, but that the framework
1769 * should take special care to preserve.
1770 *
1771 * @hide
1772 */
1773 static final int HAS_TRANSIENT_STATE = 0x00000010;
1774
1775
Christopher Tate3d4bf172011-03-28 16:16:46 -07001776 /* End of masks for mPrivateFlags2 */
1777
1778 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1779
Chet Haasedaf98e92011-01-10 14:10:36 -08001780 /**
Adam Powell637d3372010-08-25 14:37:03 -07001781 * Always allow a user to over-scroll this view, provided it is a
1782 * view that can scroll.
1783 *
1784 * @see #getOverScrollMode()
1785 * @see #setOverScrollMode(int)
1786 */
1787 public static final int OVER_SCROLL_ALWAYS = 0;
1788
1789 /**
1790 * Allow a user to over-scroll this view only if the content is large
1791 * enough to meaningfully scroll, provided it is a view that can scroll.
1792 *
1793 * @see #getOverScrollMode()
1794 * @see #setOverScrollMode(int)
1795 */
1796 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1797
1798 /**
1799 * Never allow a user to over-scroll this view.
1800 *
1801 * @see #getOverScrollMode()
1802 * @see #setOverScrollMode(int)
1803 */
1804 public static final int OVER_SCROLL_NEVER = 2;
1805
1806 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001807 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001808 *
Joe Malin32736f02011-01-19 16:14:20 -08001809 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001810 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001811 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001812
1813 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001814 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1815 *
1816 * This is for use in games, book readers, video players, or any other "immersive" application
1817 * where the usual system chrome is deemed too distracting.
1818 *
1819 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001820 *
Joe Malin32736f02011-01-19 16:14:20 -08001821 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001822 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001823 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1824
1825 /**
1826 * View has requested that the system navigation be temporarily hidden.
1827 *
1828 * This is an even less obtrusive state than that called for by
1829 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1830 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1831 * those to disappear. This is useful (in conjunction with the
1832 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1833 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1834 * window flags) for displaying content using every last pixel on the display.
1835 *
1836 * There is a limitation: because navigation controls are so important, the least user
1837 * interaction will cause them to reappear immediately.
1838 *
1839 * @see #setSystemUiVisibility(int)
1840 */
1841 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1842
1843 /**
1844 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1845 */
1846 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1847
1848 /**
1849 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1850 */
1851 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001852
1853 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001854 * @hide
1855 *
1856 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1857 * out of the public fields to keep the undefined bits out of the developer's way.
1858 *
1859 * Flag to make the status bar not expandable. Unless you also
1860 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1861 */
1862 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1863
1864 /**
1865 * @hide
1866 *
1867 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1868 * out of the public fields to keep the undefined bits out of the developer's way.
1869 *
1870 * Flag to hide notification icons and scrolling ticker text.
1871 */
1872 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1873
1874 /**
1875 * @hide
1876 *
1877 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1878 * out of the public fields to keep the undefined bits out of the developer's way.
1879 *
1880 * Flag to disable incoming notification alerts. This will not block
1881 * icons, but it will block sound, vibrating and other visual or aural notifications.
1882 */
1883 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1884
1885 /**
1886 * @hide
1887 *
1888 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1889 * out of the public fields to keep the undefined bits out of the developer's way.
1890 *
1891 * Flag to hide only the scrolling ticker. Note that
1892 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1893 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1894 */
1895 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1896
1897 /**
1898 * @hide
1899 *
1900 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1901 * out of the public fields to keep the undefined bits out of the developer's way.
1902 *
1903 * Flag to hide the center system info area.
1904 */
1905 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1906
1907 /**
1908 * @hide
1909 *
1910 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1911 * out of the public fields to keep the undefined bits out of the developer's way.
1912 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001913 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001914 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1915 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001916 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001917
1918 /**
1919 * @hide
1920 *
1921 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1922 * out of the public fields to keep the undefined bits out of the developer's way.
1923 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001924 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001925 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1926 */
1927 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1928
1929 /**
1930 * @hide
1931 *
1932 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1933 * out of the public fields to keep the undefined bits out of the developer's way.
1934 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001935 * Flag to hide only the clock. You might use this if your activity has
1936 * its own clock making the status bar's clock redundant.
1937 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001938 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1939
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001940 /**
1941 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001942 *
1943 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1944 * out of the public fields to keep the undefined bits out of the developer's way.
1945 *
1946 * Flag to hide only the recent apps button. Don't use this
1947 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1948 */
1949 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1950
1951 /**
1952 * @hide
1953 *
1954 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1955 *
1956 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1957 */
1958 @Deprecated
1959 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1960 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1961
1962 /**
1963 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001964 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001965 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001966
1967 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001968 * These are the system UI flags that can be cleared by events outside
1969 * of an application. Currently this is just the ability to tap on the
1970 * screen while hiding the navigation bar to have it return.
1971 * @hide
1972 */
1973 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1974 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1975
1976 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001977 * Find views that render the specified text.
1978 *
1979 * @see #findViewsWithText(ArrayList, CharSequence, int)
1980 */
1981 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1982
1983 /**
1984 * Find find views that contain the specified content description.
1985 *
1986 * @see #findViewsWithText(ArrayList, CharSequence, int)
1987 */
1988 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1989
1990 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001991 * Find views that contain {@link AccessibilityNodeProvider}. Such
1992 * a View is a root of virtual view hierarchy and may contain the searched
1993 * text. If this flag is set Views with providers are automatically
1994 * added and it is a responsibility of the client to call the APIs of
1995 * the provider to determine whether the virtual tree rooted at this View
1996 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1997 * represeting the virtual views with this text.
1998 *
1999 * @see #findViewsWithText(ArrayList, CharSequence, int)
2000 *
2001 * @hide
2002 */
2003 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2004
2005 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002006 * Indicates that the screen has changed state and is now off.
2007 *
2008 * @see #onScreenStateChanged(int)
2009 */
2010 public static final int SCREEN_STATE_OFF = 0x0;
2011
2012 /**
2013 * Indicates that the screen has changed state and is now on.
2014 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002015 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002016 */
2017 public static final int SCREEN_STATE_ON = 0x1;
2018
2019 /**
Adam Powell637d3372010-08-25 14:37:03 -07002020 * Controls the over-scroll mode for this view.
2021 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2022 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2023 * and {@link #OVER_SCROLL_NEVER}.
2024 */
2025 private int mOverScrollMode;
2026
2027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 * The parent this view is attached to.
2029 * {@hide}
2030 *
2031 * @see #getParent()
2032 */
2033 protected ViewParent mParent;
2034
2035 /**
2036 * {@hide}
2037 */
2038 AttachInfo mAttachInfo;
2039
2040 /**
2041 * {@hide}
2042 */
Romain Guy809a7f62009-05-14 15:44:42 -07002043 @ViewDebug.ExportedProperty(flagMapping = {
2044 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2045 name = "FORCE_LAYOUT"),
2046 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2047 name = "LAYOUT_REQUIRED"),
2048 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002049 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002050 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2051 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2052 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2053 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2054 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002056 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
2058 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002059 * This view's request for the visibility of the status bar.
2060 * @hide
2061 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002062 @ViewDebug.ExportedProperty(flagMapping = {
2063 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2064 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2065 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2066 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2067 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2068 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2069 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2070 equals = SYSTEM_UI_FLAG_VISIBLE,
2071 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2072 })
Joe Onorato664644d2011-01-23 17:53:23 -08002073 int mSystemUiVisibility;
2074
2075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 * Count of how many windows this view has been attached to.
2077 */
2078 int mWindowAttachCount;
2079
2080 /**
2081 * The layout parameters associated with this view and used by the parent
2082 * {@link android.view.ViewGroup} to determine how this view should be
2083 * laid out.
2084 * {@hide}
2085 */
2086 protected ViewGroup.LayoutParams mLayoutParams;
2087
2088 /**
2089 * The view flags hold various views states.
2090 * {@hide}
2091 */
2092 @ViewDebug.ExportedProperty
2093 int mViewFlags;
2094
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002095 static class TransformationInfo {
2096 /**
2097 * The transform matrix for the View. This transform is calculated internally
2098 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2099 * is used by default. Do *not* use this variable directly; instead call
2100 * getMatrix(), which will automatically recalculate the matrix if necessary
2101 * to get the correct matrix based on the latest rotation and scale properties.
2102 */
2103 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002105 /**
2106 * The transform matrix for the View. This transform is calculated internally
2107 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2108 * is used by default. Do *not* use this variable directly; instead call
2109 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2110 * to get the correct matrix based on the latest rotation and scale properties.
2111 */
2112 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002113
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002114 /**
2115 * An internal variable that tracks whether we need to recalculate the
2116 * transform matrix, based on whether the rotation or scaleX/Y properties
2117 * have changed since the matrix was last calculated.
2118 */
2119 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002120
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002121 /**
2122 * An internal variable that tracks whether we need to recalculate the
2123 * transform matrix, based on whether the rotation or scaleX/Y properties
2124 * have changed since the matrix was last calculated.
2125 */
2126 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002127
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002128 /**
2129 * A variable that tracks whether we need to recalculate the
2130 * transform matrix, based on whether the rotation or scaleX/Y properties
2131 * have changed since the matrix was last calculated. This variable
2132 * is only valid after a call to updateMatrix() or to a function that
2133 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2134 */
2135 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002136
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002137 /**
2138 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2139 */
2140 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002141
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002142 /**
2143 * This matrix is used when computing the matrix for 3D rotations.
2144 */
2145 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002146
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002147 /**
2148 * These prev values are used to recalculate a centered pivot point when necessary. The
2149 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2150 * set), so thes values are only used then as well.
2151 */
2152 private int mPrevWidth = -1;
2153 private int mPrevHeight = -1;
2154
2155 /**
2156 * The degrees rotation around the vertical axis through the pivot point.
2157 */
2158 @ViewDebug.ExportedProperty
2159 float mRotationY = 0f;
2160
2161 /**
2162 * The degrees rotation around the horizontal axis through the pivot point.
2163 */
2164 @ViewDebug.ExportedProperty
2165 float mRotationX = 0f;
2166
2167 /**
2168 * The degrees rotation around the pivot point.
2169 */
2170 @ViewDebug.ExportedProperty
2171 float mRotation = 0f;
2172
2173 /**
2174 * The amount of translation of the object away from its left property (post-layout).
2175 */
2176 @ViewDebug.ExportedProperty
2177 float mTranslationX = 0f;
2178
2179 /**
2180 * The amount of translation of the object away from its top property (post-layout).
2181 */
2182 @ViewDebug.ExportedProperty
2183 float mTranslationY = 0f;
2184
2185 /**
2186 * The amount of scale in the x direction around the pivot point. A
2187 * value of 1 means no scaling is applied.
2188 */
2189 @ViewDebug.ExportedProperty
2190 float mScaleX = 1f;
2191
2192 /**
2193 * The amount of scale in the y direction around the pivot point. A
2194 * value of 1 means no scaling is applied.
2195 */
2196 @ViewDebug.ExportedProperty
2197 float mScaleY = 1f;
2198
2199 /**
Chet Haasea33de552012-02-03 16:28:24 -08002200 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002201 */
2202 @ViewDebug.ExportedProperty
2203 float mPivotX = 0f;
2204
2205 /**
Chet Haasea33de552012-02-03 16:28:24 -08002206 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002207 */
2208 @ViewDebug.ExportedProperty
2209 float mPivotY = 0f;
2210
2211 /**
2212 * The opacity of the View. This is a value from 0 to 1, where 0 means
2213 * completely transparent and 1 means completely opaque.
2214 */
2215 @ViewDebug.ExportedProperty
2216 float mAlpha = 1f;
2217 }
2218
2219 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002220
Joe Malin32736f02011-01-19 16:14:20 -08002221 private boolean mLastIsOpaque;
2222
Chet Haasefd2b0022010-08-06 13:08:56 -07002223 /**
2224 * Convenience value to check for float values that are close enough to zero to be considered
2225 * zero.
2226 */
Romain Guy2542d192010-08-18 11:47:12 -07002227 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002228
2229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 * The distance in pixels from the left edge of this view's parent
2231 * to the left edge of this view.
2232 * {@hide}
2233 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002234 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 protected int mLeft;
2236 /**
2237 * The distance in pixels from the left edge of this view's parent
2238 * to the right edge of this view.
2239 * {@hide}
2240 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002241 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 protected int mRight;
2243 /**
2244 * The distance in pixels from the top edge of this view's parent
2245 * to the top edge of this view.
2246 * {@hide}
2247 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002248 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 protected int mTop;
2250 /**
2251 * The distance in pixels from the top edge of this view's parent
2252 * to the bottom edge of this view.
2253 * {@hide}
2254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002255 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 protected int mBottom;
2257
2258 /**
2259 * The offset, in pixels, by which the content of this view is scrolled
2260 * horizontally.
2261 * {@hide}
2262 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002263 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 protected int mScrollX;
2265 /**
2266 * The offset, in pixels, by which the content of this view is scrolled
2267 * vertically.
2268 * {@hide}
2269 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002270 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 protected int mScrollY;
2272
2273 /**
2274 * The left padding in pixels, that is the distance in pixels between the
2275 * left edge of this view and the left edge of its content.
2276 * {@hide}
2277 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002278 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 protected int mPaddingLeft;
2280 /**
2281 * The right padding in pixels, that is the distance in pixels between the
2282 * right edge of this view and the right edge of its content.
2283 * {@hide}
2284 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002285 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 protected int mPaddingRight;
2287 /**
2288 * The top padding in pixels, that is the distance in pixels between the
2289 * top edge of this view and the top edge of its content.
2290 * {@hide}
2291 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002292 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 protected int mPaddingTop;
2294 /**
2295 * The bottom padding in pixels, that is the distance in pixels between the
2296 * bottom edge of this view and the bottom edge of its content.
2297 * {@hide}
2298 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002299 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 protected int mPaddingBottom;
2301
2302 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002303 * Briefly describes the view and is primarily used for accessibility support.
2304 */
2305 private CharSequence mContentDescription;
2306
2307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002309 *
2310 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002312 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002313 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314
2315 /**
2316 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002317 *
2318 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002320 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002321 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002323 /**
Adam Powell20232d02010-12-08 21:08:53 -08002324 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002325 *
2326 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002327 */
2328 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002329 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002330
2331 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002332 * Cache if the user padding is relative.
2333 *
2334 */
2335 @ViewDebug.ExportedProperty(category = "padding")
2336 boolean mUserPaddingRelative;
2337
2338 /**
2339 * Cache the paddingStart set by the user to append to the scrollbar's size.
2340 *
2341 */
2342 @ViewDebug.ExportedProperty(category = "padding")
2343 int mUserPaddingStart;
2344
2345 /**
2346 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2347 *
2348 */
2349 @ViewDebug.ExportedProperty(category = "padding")
2350 int mUserPaddingEnd;
2351
2352 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002353 * @hide
2354 */
2355 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2356 /**
2357 * @hide
2358 */
2359 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 private Drawable mBGDrawable;
2362
2363 private int mBackgroundResource;
2364 private boolean mBackgroundSizeChanged;
2365
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002366 static class ListenerInfo {
2367 /**
2368 * Listener used to dispatch focus change events.
2369 * This field should be made private, so it is hidden from the SDK.
2370 * {@hide}
2371 */
2372 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002374 /**
2375 * Listeners for layout change events.
2376 */
2377 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002378
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002379 /**
2380 * Listeners for attach events.
2381 */
2382 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002383
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002384 /**
2385 * Listener used to dispatch click events.
2386 * This field should be made private, so it is hidden from the SDK.
2387 * {@hide}
2388 */
2389 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002391 /**
2392 * Listener used to dispatch long click events.
2393 * This field should be made private, so it is hidden from the SDK.
2394 * {@hide}
2395 */
2396 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002398 /**
2399 * Listener used to build the context menu.
2400 * This field should be made private, so it is hidden from the SDK.
2401 * {@hide}
2402 */
2403 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002405 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002407 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002409 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002410
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002411 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002412
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002413 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002414
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002415 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2416 }
2417
2418 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 /**
2421 * The application environment this view lives in.
2422 * This field should be made private, so it is hidden from the SDK.
2423 * {@hide}
2424 */
2425 protected Context mContext;
2426
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002427 private final Resources mResources;
2428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 private ScrollabilityCache mScrollCache;
2430
2431 private int[] mDrawableState = null;
2432
Romain Guy0211a0a2011-02-14 16:34:59 -08002433 /**
2434 * Set to true when drawing cache is enabled and cannot be created.
2435 *
2436 * @hide
2437 */
2438 public boolean mCachingFailed;
2439
Romain Guy02890fd2010-08-06 17:58:44 -07002440 private Bitmap mDrawingCache;
2441 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002442 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002443 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444
2445 /**
2446 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2447 * the user may specify which view to go to next.
2448 */
2449 private int mNextFocusLeftId = View.NO_ID;
2450
2451 /**
2452 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2453 * the user may specify which view to go to next.
2454 */
2455 private int mNextFocusRightId = View.NO_ID;
2456
2457 /**
2458 * When this view has focus and the next focus is {@link #FOCUS_UP},
2459 * the user may specify which view to go to next.
2460 */
2461 private int mNextFocusUpId = View.NO_ID;
2462
2463 /**
2464 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2465 * the user may specify which view to go to next.
2466 */
2467 private int mNextFocusDownId = View.NO_ID;
2468
Jeff Brown4e6319b2010-12-13 10:36:51 -08002469 /**
2470 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2471 * the user may specify which view to go to next.
2472 */
2473 int mNextFocusForwardId = View.NO_ID;
2474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002476 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002477 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002478 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 private UnsetPressedState mUnsetPressedState;
2481
2482 /**
2483 * Whether the long press's action has been invoked. The tap's action is invoked on the
2484 * up event while a long press is invoked as soon as the long press duration is reached, so
2485 * a long press could be performed before the tap is checked, in which case the tap's action
2486 * should not be invoked.
2487 */
2488 private boolean mHasPerformedLongPress;
2489
2490 /**
2491 * The minimum height of the view. We'll try our best to have the height
2492 * of this view to at least this amount.
2493 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002494 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 private int mMinHeight;
2496
2497 /**
2498 * The minimum width of the view. We'll try our best to have the width
2499 * of this view to at least this amount.
2500 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002501 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 private int mMinWidth;
2503
2504 /**
2505 * The delegate to handle touch events that are physically in this view
2506 * but should be handled by another view.
2507 */
2508 private TouchDelegate mTouchDelegate = null;
2509
2510 /**
2511 * Solid color to use as a background when creating the drawing cache. Enables
2512 * the cache to use 16 bit bitmaps instead of 32 bit.
2513 */
2514 private int mDrawingCacheBackgroundColor = 0;
2515
2516 /**
2517 * Special tree observer used when mAttachInfo is null.
2518 */
2519 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002520
Adam Powelle14579b2009-12-16 18:39:52 -08002521 /**
2522 * Cache the touch slop from the context that created the view.
2523 */
2524 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002527 * Object that handles automatic animation of view properties.
2528 */
2529 private ViewPropertyAnimator mAnimator = null;
2530
2531 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002532 * Flag indicating that a drag can cross window boundaries. When
2533 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2534 * with this flag set, all visible applications will be able to participate
2535 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002536 *
2537 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002538 */
2539 public static final int DRAG_FLAG_GLOBAL = 1;
2540
2541 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002542 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2543 */
2544 private float mVerticalScrollFactor;
2545
2546 /**
Adam Powell20232d02010-12-08 21:08:53 -08002547 * Position of the vertical scroll bar.
2548 */
2549 private int mVerticalScrollbarPosition;
2550
2551 /**
2552 * Position the scroll bar at the default position as determined by the system.
2553 */
2554 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2555
2556 /**
2557 * Position the scroll bar along the left edge.
2558 */
2559 public static final int SCROLLBAR_POSITION_LEFT = 1;
2560
2561 /**
2562 * Position the scroll bar along the right edge.
2563 */
2564 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2565
2566 /**
Romain Guy171c5922011-01-06 10:04:23 -08002567 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002568 *
2569 * @see #getLayerType()
2570 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002571 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002572 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002573 */
2574 public static final int LAYER_TYPE_NONE = 0;
2575
2576 /**
2577 * <p>Indicates that the view has a software layer. A software layer is backed
2578 * by a bitmap and causes the view to be rendered using Android's software
2579 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002580 *
Romain Guy171c5922011-01-06 10:04:23 -08002581 * <p>Software layers have various usages:</p>
2582 * <p>When the application is not using hardware acceleration, a software layer
2583 * is useful to apply a specific color filter and/or blending mode and/or
2584 * translucency to a view and all its children.</p>
2585 * <p>When the application is using hardware acceleration, a software layer
2586 * is useful to render drawing primitives not supported by the hardware
2587 * accelerated pipeline. It can also be used to cache a complex view tree
2588 * into a texture and reduce the complexity of drawing operations. For instance,
2589 * when animating a complex view tree with a translation, a software layer can
2590 * be used to render the view tree only once.</p>
2591 * <p>Software layers should be avoided when the affected view tree updates
2592 * often. Every update will require to re-render the software layer, which can
2593 * potentially be slow (particularly when hardware acceleration is turned on
2594 * since the layer will have to be uploaded into a hardware texture after every
2595 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002596 *
2597 * @see #getLayerType()
2598 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002599 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002600 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002601 */
2602 public static final int LAYER_TYPE_SOFTWARE = 1;
2603
2604 /**
2605 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2606 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2607 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2608 * rendering pipeline, but only if hardware acceleration is turned on for the
2609 * view hierarchy. When hardware acceleration is turned off, hardware layers
2610 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002611 *
Romain Guy171c5922011-01-06 10:04:23 -08002612 * <p>A hardware layer is useful to apply a specific color filter and/or
2613 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002614 * <p>A hardware layer can be used to cache a complex view tree into a
2615 * texture and reduce the complexity of drawing operations. For instance,
2616 * when animating a complex view tree with a translation, a hardware layer can
2617 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002618 * <p>A hardware layer can also be used to increase the rendering quality when
2619 * rotation transformations are applied on a view. It can also be used to
2620 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002621 *
2622 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002623 * @see #setLayerType(int, android.graphics.Paint)
2624 * @see #LAYER_TYPE_NONE
2625 * @see #LAYER_TYPE_SOFTWARE
2626 */
2627 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002628
Romain Guy3aaff3a2011-01-12 14:18:47 -08002629 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2630 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2631 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2632 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2633 })
Romain Guy171c5922011-01-06 10:04:23 -08002634 int mLayerType = LAYER_TYPE_NONE;
2635 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002636 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002637
2638 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002639 * Set to true when the view is sending hover accessibility events because it
2640 * is the innermost hovered view.
2641 */
2642 private boolean mSendingHoverAccessibilityEvents;
2643
2644 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002645 * Delegate for injecting accessiblity functionality.
2646 */
2647 AccessibilityDelegate mAccessibilityDelegate;
2648
2649 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002651 */
2652 public static final int TEXT_DIRECTION_INHERIT = 0;
2653
2654 /**
2655 * Text direction is using "first strong algorithm". The first strong directional character
2656 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002657 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002659 */
2660 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2661
2662 /**
2663 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2664 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002665 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002666 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002667 */
2668 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2669
2670 /**
2671 * Text direction is forced to LTR.
2672 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002673 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002674 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002675
2676 /**
2677 * Text direction is forced to RTL.
2678 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002679 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002680 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002681
2682 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002683 * Text direction is coming from the system Locale.
2684 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002685 */
2686 public static final int TEXT_DIRECTION_LOCALE = 5;
2687
2688 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002689 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002690 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002691 */
2692 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2693
2694 /**
2695 * The text direction that has been defined by {@link #setTextDirection(int)}.
2696 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002697 */
2698 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002699 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2700 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2701 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2702 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002703 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2704 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002705 })
Doug Feltcb3791202011-07-07 11:57:48 -07002706 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002707
2708 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002709 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002710 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002711 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2712 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002713 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002714 */
2715 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002716 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2717 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2718 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002719 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002720 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2721 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002722 })
Doug Feltcb3791202011-07-07 11:57:48 -07002723 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002724
2725 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002726 * Consistency verifier for debugging purposes.
2727 * @hide
2728 */
2729 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2730 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2731 new InputEventConsistencyVerifier(this, 0) : null;
2732
2733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 * Simple constructor to use when creating a view from code.
2735 *
2736 * @param context The Context the view is running in, through which it can
2737 * access the current theme, resources, etc.
2738 */
2739 public View(Context context) {
2740 mContext = context;
2741 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002742 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002743 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002744 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002745 mUserPaddingStart = -1;
2746 mUserPaddingEnd = -1;
2747 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
2749
2750 /**
2751 * Constructor that is called when inflating a view from XML. This is called
2752 * when a view is being constructed from an XML file, supplying attributes
2753 * that were specified in the XML file. This version uses a default style of
2754 * 0, so the only attribute values applied are those in the Context's Theme
2755 * and the given AttributeSet.
2756 *
2757 * <p>
2758 * The method onFinishInflate() will be called after all children have been
2759 * added.
2760 *
2761 * @param context The Context the view is running in, through which it can
2762 * access the current theme, resources, etc.
2763 * @param attrs The attributes of the XML tag that is inflating the view.
2764 * @see #View(Context, AttributeSet, int)
2765 */
2766 public View(Context context, AttributeSet attrs) {
2767 this(context, attrs, 0);
2768 }
2769
2770 /**
2771 * Perform inflation from XML and apply a class-specific base style. This
2772 * constructor of View allows subclasses to use their own base style when
2773 * they are inflating. For example, a Button class's constructor would call
2774 * this version of the super class constructor and supply
2775 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2776 * the theme's button style to modify all of the base view attributes (in
2777 * particular its background) as well as the Button class's attributes.
2778 *
2779 * @param context The Context the view is running in, through which it can
2780 * access the current theme, resources, etc.
2781 * @param attrs The attributes of the XML tag that is inflating the view.
2782 * @param defStyle The default style to apply to this view. If 0, no style
2783 * will be applied (beyond what is included in the theme). This may
2784 * either be an attribute resource, whose value will be retrieved
2785 * from the current theme, or an explicit style resource.
2786 * @see #View(Context, AttributeSet)
2787 */
2788 public View(Context context, AttributeSet attrs, int defStyle) {
2789 this(context);
2790
2791 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2792 defStyle, 0);
2793
2794 Drawable background = null;
2795
2796 int leftPadding = -1;
2797 int topPadding = -1;
2798 int rightPadding = -1;
2799 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002800 int startPadding = -1;
2801 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802
2803 int padding = -1;
2804
2805 int viewFlagValues = 0;
2806 int viewFlagMasks = 0;
2807
2808 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 int x = 0;
2811 int y = 0;
2812
Chet Haase73066682010-11-29 15:55:32 -08002813 float tx = 0;
2814 float ty = 0;
2815 float rotation = 0;
2816 float rotationX = 0;
2817 float rotationY = 0;
2818 float sx = 1f;
2819 float sy = 1f;
2820 boolean transformSet = false;
2821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2823
Adam Powell637d3372010-08-25 14:37:03 -07002824 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 final int N = a.getIndexCount();
2826 for (int i = 0; i < N; i++) {
2827 int attr = a.getIndex(i);
2828 switch (attr) {
2829 case com.android.internal.R.styleable.View_background:
2830 background = a.getDrawable(attr);
2831 break;
2832 case com.android.internal.R.styleable.View_padding:
2833 padding = a.getDimensionPixelSize(attr, -1);
2834 break;
2835 case com.android.internal.R.styleable.View_paddingLeft:
2836 leftPadding = a.getDimensionPixelSize(attr, -1);
2837 break;
2838 case com.android.internal.R.styleable.View_paddingTop:
2839 topPadding = a.getDimensionPixelSize(attr, -1);
2840 break;
2841 case com.android.internal.R.styleable.View_paddingRight:
2842 rightPadding = a.getDimensionPixelSize(attr, -1);
2843 break;
2844 case com.android.internal.R.styleable.View_paddingBottom:
2845 bottomPadding = a.getDimensionPixelSize(attr, -1);
2846 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002847 case com.android.internal.R.styleable.View_paddingStart:
2848 startPadding = a.getDimensionPixelSize(attr, -1);
2849 break;
2850 case com.android.internal.R.styleable.View_paddingEnd:
2851 endPadding = a.getDimensionPixelSize(attr, -1);
2852 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 case com.android.internal.R.styleable.View_scrollX:
2854 x = a.getDimensionPixelOffset(attr, 0);
2855 break;
2856 case com.android.internal.R.styleable.View_scrollY:
2857 y = a.getDimensionPixelOffset(attr, 0);
2858 break;
Chet Haase73066682010-11-29 15:55:32 -08002859 case com.android.internal.R.styleable.View_alpha:
2860 setAlpha(a.getFloat(attr, 1f));
2861 break;
2862 case com.android.internal.R.styleable.View_transformPivotX:
2863 setPivotX(a.getDimensionPixelOffset(attr, 0));
2864 break;
2865 case com.android.internal.R.styleable.View_transformPivotY:
2866 setPivotY(a.getDimensionPixelOffset(attr, 0));
2867 break;
2868 case com.android.internal.R.styleable.View_translationX:
2869 tx = a.getDimensionPixelOffset(attr, 0);
2870 transformSet = true;
2871 break;
2872 case com.android.internal.R.styleable.View_translationY:
2873 ty = a.getDimensionPixelOffset(attr, 0);
2874 transformSet = true;
2875 break;
2876 case com.android.internal.R.styleable.View_rotation:
2877 rotation = a.getFloat(attr, 0);
2878 transformSet = true;
2879 break;
2880 case com.android.internal.R.styleable.View_rotationX:
2881 rotationX = a.getFloat(attr, 0);
2882 transformSet = true;
2883 break;
2884 case com.android.internal.R.styleable.View_rotationY:
2885 rotationY = a.getFloat(attr, 0);
2886 transformSet = true;
2887 break;
2888 case com.android.internal.R.styleable.View_scaleX:
2889 sx = a.getFloat(attr, 1f);
2890 transformSet = true;
2891 break;
2892 case com.android.internal.R.styleable.View_scaleY:
2893 sy = a.getFloat(attr, 1f);
2894 transformSet = true;
2895 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 case com.android.internal.R.styleable.View_id:
2897 mID = a.getResourceId(attr, NO_ID);
2898 break;
2899 case com.android.internal.R.styleable.View_tag:
2900 mTag = a.getText(attr);
2901 break;
2902 case com.android.internal.R.styleable.View_fitsSystemWindows:
2903 if (a.getBoolean(attr, false)) {
2904 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2905 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2906 }
2907 break;
2908 case com.android.internal.R.styleable.View_focusable:
2909 if (a.getBoolean(attr, false)) {
2910 viewFlagValues |= FOCUSABLE;
2911 viewFlagMasks |= FOCUSABLE_MASK;
2912 }
2913 break;
2914 case com.android.internal.R.styleable.View_focusableInTouchMode:
2915 if (a.getBoolean(attr, false)) {
2916 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2917 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2918 }
2919 break;
2920 case com.android.internal.R.styleable.View_clickable:
2921 if (a.getBoolean(attr, false)) {
2922 viewFlagValues |= CLICKABLE;
2923 viewFlagMasks |= CLICKABLE;
2924 }
2925 break;
2926 case com.android.internal.R.styleable.View_longClickable:
2927 if (a.getBoolean(attr, false)) {
2928 viewFlagValues |= LONG_CLICKABLE;
2929 viewFlagMasks |= LONG_CLICKABLE;
2930 }
2931 break;
2932 case com.android.internal.R.styleable.View_saveEnabled:
2933 if (!a.getBoolean(attr, true)) {
2934 viewFlagValues |= SAVE_DISABLED;
2935 viewFlagMasks |= SAVE_DISABLED_MASK;
2936 }
2937 break;
2938 case com.android.internal.R.styleable.View_duplicateParentState:
2939 if (a.getBoolean(attr, false)) {
2940 viewFlagValues |= DUPLICATE_PARENT_STATE;
2941 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2942 }
2943 break;
2944 case com.android.internal.R.styleable.View_visibility:
2945 final int visibility = a.getInt(attr, 0);
2946 if (visibility != 0) {
2947 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2948 viewFlagMasks |= VISIBILITY_MASK;
2949 }
2950 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002951 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002952 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002953 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002954 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002955 final int layoutDirection = a.getInt(attr, -1);
2956 if (layoutDirection != -1) {
2957 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002958 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002959 // Set to default (LAYOUT_DIRECTION_INHERIT)
2960 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002961 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002962 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002963 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 case com.android.internal.R.styleable.View_drawingCacheQuality:
2965 final int cacheQuality = a.getInt(attr, 0);
2966 if (cacheQuality != 0) {
2967 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2968 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2969 }
2970 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002971 case com.android.internal.R.styleable.View_contentDescription:
2972 mContentDescription = a.getString(attr);
2973 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2975 if (!a.getBoolean(attr, true)) {
2976 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2977 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2978 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002979 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2981 if (!a.getBoolean(attr, true)) {
2982 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2983 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2984 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002985 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 case R.styleable.View_scrollbars:
2987 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2988 if (scrollbars != SCROLLBARS_NONE) {
2989 viewFlagValues |= scrollbars;
2990 viewFlagMasks |= SCROLLBARS_MASK;
2991 initializeScrollbars(a);
2992 }
2993 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002994 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002996 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2997 // Ignore the attribute starting with ICS
2998 break;
2999 }
3000 // With builds < ICS, fall through and apply fading edges
3001 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3003 if (fadingEdge != FADING_EDGE_NONE) {
3004 viewFlagValues |= fadingEdge;
3005 viewFlagMasks |= FADING_EDGE_MASK;
3006 initializeFadingEdge(a);
3007 }
3008 break;
3009 case R.styleable.View_scrollbarStyle:
3010 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3011 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3012 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3013 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3014 }
3015 break;
3016 case R.styleable.View_isScrollContainer:
3017 setScrollContainer = true;
3018 if (a.getBoolean(attr, false)) {
3019 setScrollContainer(true);
3020 }
3021 break;
3022 case com.android.internal.R.styleable.View_keepScreenOn:
3023 if (a.getBoolean(attr, false)) {
3024 viewFlagValues |= KEEP_SCREEN_ON;
3025 viewFlagMasks |= KEEP_SCREEN_ON;
3026 }
3027 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003028 case R.styleable.View_filterTouchesWhenObscured:
3029 if (a.getBoolean(attr, false)) {
3030 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3031 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3032 }
3033 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 case R.styleable.View_nextFocusLeft:
3035 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3036 break;
3037 case R.styleable.View_nextFocusRight:
3038 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3039 break;
3040 case R.styleable.View_nextFocusUp:
3041 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3042 break;
3043 case R.styleable.View_nextFocusDown:
3044 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3045 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003046 case R.styleable.View_nextFocusForward:
3047 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3048 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 case R.styleable.View_minWidth:
3050 mMinWidth = a.getDimensionPixelSize(attr, 0);
3051 break;
3052 case R.styleable.View_minHeight:
3053 mMinHeight = a.getDimensionPixelSize(attr, 0);
3054 break;
Romain Guy9a817362009-05-01 10:57:14 -07003055 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003056 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003057 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003058 + "be used within a restricted context");
3059 }
3060
Romain Guy9a817362009-05-01 10:57:14 -07003061 final String handlerName = a.getString(attr);
3062 if (handlerName != null) {
3063 setOnClickListener(new OnClickListener() {
3064 private Method mHandler;
3065
3066 public void onClick(View v) {
3067 if (mHandler == null) {
3068 try {
3069 mHandler = getContext().getClass().getMethod(handlerName,
3070 View.class);
3071 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003072 int id = getId();
3073 String idText = id == NO_ID ? "" : " with id '"
3074 + getContext().getResources().getResourceEntryName(
3075 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003076 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003077 handlerName + "(View) in the activity "
3078 + getContext().getClass() + " for onClick handler"
3079 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003080 }
3081 }
3082
3083 try {
3084 mHandler.invoke(getContext(), View.this);
3085 } catch (IllegalAccessException e) {
3086 throw new IllegalStateException("Could not execute non "
3087 + "public method of the activity", e);
3088 } catch (InvocationTargetException e) {
3089 throw new IllegalStateException("Could not execute "
3090 + "method of the activity", e);
3091 }
3092 }
3093 });
3094 }
3095 break;
Adam Powell637d3372010-08-25 14:37:03 -07003096 case R.styleable.View_overScrollMode:
3097 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3098 break;
Adam Powell20232d02010-12-08 21:08:53 -08003099 case R.styleable.View_verticalScrollbarPosition:
3100 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3101 break;
Romain Guy171c5922011-01-06 10:04:23 -08003102 case R.styleable.View_layerType:
3103 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3104 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003105 case R.styleable.View_textDirection:
3106 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3107 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 }
3109 }
3110
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003111 a.recycle();
3112
Adam Powell637d3372010-08-25 14:37:03 -07003113 setOverScrollMode(overScrollMode);
3114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 if (background != null) {
3116 setBackgroundDrawable(background);
3117 }
3118
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003119 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3120 // layout direction). Those cached values will be used later during padding resolution.
3121 mUserPaddingStart = startPadding;
3122 mUserPaddingEnd = endPadding;
3123
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003124 updateUserPaddingRelative();
3125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 if (padding >= 0) {
3127 leftPadding = padding;
3128 topPadding = padding;
3129 rightPadding = padding;
3130 bottomPadding = padding;
3131 }
3132
3133 // If the user specified the padding (either with android:padding or
3134 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3135 // use the default padding or the padding from the background drawable
3136 // (stored at this point in mPadding*)
3137 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3138 topPadding >= 0 ? topPadding : mPaddingTop,
3139 rightPadding >= 0 ? rightPadding : mPaddingRight,
3140 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3141
3142 if (viewFlagMasks != 0) {
3143 setFlags(viewFlagValues, viewFlagMasks);
3144 }
3145
3146 // Needs to be called after mViewFlags is set
3147 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3148 recomputePadding();
3149 }
3150
3151 if (x != 0 || y != 0) {
3152 scrollTo(x, y);
3153 }
3154
Chet Haase73066682010-11-29 15:55:32 -08003155 if (transformSet) {
3156 setTranslationX(tx);
3157 setTranslationY(ty);
3158 setRotation(rotation);
3159 setRotationX(rotationX);
3160 setRotationY(rotationY);
3161 setScaleX(sx);
3162 setScaleY(sy);
3163 }
3164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3166 setScrollContainer(true);
3167 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003168
3169 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
3171
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003172 private void updateUserPaddingRelative() {
3173 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3174 }
3175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 /**
3177 * Non-public constructor for use in testing
3178 */
3179 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003180 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 }
3182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 /**
3184 * <p>
3185 * Initializes the fading edges from a given set of styled attributes. This
3186 * method should be called by subclasses that need fading edges and when an
3187 * instance of these subclasses is created programmatically rather than
3188 * being inflated from XML. This method is automatically called when the XML
3189 * is inflated.
3190 * </p>
3191 *
3192 * @param a the styled attributes set to initialize the fading edges from
3193 */
3194 protected void initializeFadingEdge(TypedArray a) {
3195 initScrollCache();
3196
3197 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3198 R.styleable.View_fadingEdgeLength,
3199 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3200 }
3201
3202 /**
3203 * Returns the size of the vertical faded edges used to indicate that more
3204 * content in this view is visible.
3205 *
3206 * @return The size in pixels of the vertical faded edge or 0 if vertical
3207 * faded edges are not enabled for this view.
3208 * @attr ref android.R.styleable#View_fadingEdgeLength
3209 */
3210 public int getVerticalFadingEdgeLength() {
3211 if (isVerticalFadingEdgeEnabled()) {
3212 ScrollabilityCache cache = mScrollCache;
3213 if (cache != null) {
3214 return cache.fadingEdgeLength;
3215 }
3216 }
3217 return 0;
3218 }
3219
3220 /**
3221 * Set the size of the faded edge used to indicate that more content in this
3222 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003223 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3224 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3225 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 *
3227 * @param length The size in pixels of the faded edge used to indicate that more
3228 * content in this view is visible.
3229 */
3230 public void setFadingEdgeLength(int length) {
3231 initScrollCache();
3232 mScrollCache.fadingEdgeLength = length;
3233 }
3234
3235 /**
3236 * Returns the size of the horizontal faded edges used to indicate that more
3237 * content in this view is visible.
3238 *
3239 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3240 * faded edges are not enabled for this view.
3241 * @attr ref android.R.styleable#View_fadingEdgeLength
3242 */
3243 public int getHorizontalFadingEdgeLength() {
3244 if (isHorizontalFadingEdgeEnabled()) {
3245 ScrollabilityCache cache = mScrollCache;
3246 if (cache != null) {
3247 return cache.fadingEdgeLength;
3248 }
3249 }
3250 return 0;
3251 }
3252
3253 /**
3254 * Returns the width of the vertical scrollbar.
3255 *
3256 * @return The width in pixels of the vertical scrollbar or 0 if there
3257 * is no vertical scrollbar.
3258 */
3259 public int getVerticalScrollbarWidth() {
3260 ScrollabilityCache cache = mScrollCache;
3261 if (cache != null) {
3262 ScrollBarDrawable scrollBar = cache.scrollBar;
3263 if (scrollBar != null) {
3264 int size = scrollBar.getSize(true);
3265 if (size <= 0) {
3266 size = cache.scrollBarSize;
3267 }
3268 return size;
3269 }
3270 return 0;
3271 }
3272 return 0;
3273 }
3274
3275 /**
3276 * Returns the height of the horizontal scrollbar.
3277 *
3278 * @return The height in pixels of the horizontal scrollbar or 0 if
3279 * there is no horizontal scrollbar.
3280 */
3281 protected int getHorizontalScrollbarHeight() {
3282 ScrollabilityCache cache = mScrollCache;
3283 if (cache != null) {
3284 ScrollBarDrawable scrollBar = cache.scrollBar;
3285 if (scrollBar != null) {
3286 int size = scrollBar.getSize(false);
3287 if (size <= 0) {
3288 size = cache.scrollBarSize;
3289 }
3290 return size;
3291 }
3292 return 0;
3293 }
3294 return 0;
3295 }
3296
3297 /**
3298 * <p>
3299 * Initializes the scrollbars from a given set of styled attributes. This
3300 * method should be called by subclasses that need scrollbars and when an
3301 * instance of these subclasses is created programmatically rather than
3302 * being inflated from XML. This method is automatically called when the XML
3303 * is inflated.
3304 * </p>
3305 *
3306 * @param a the styled attributes set to initialize the scrollbars from
3307 */
3308 protected void initializeScrollbars(TypedArray a) {
3309 initScrollCache();
3310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003312
Mike Cleronf116bf82009-09-27 19:14:12 -07003313 if (scrollabilityCache.scrollBar == null) {
3314 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3315 }
Joe Malin32736f02011-01-19 16:14:20 -08003316
Romain Guy8bda2482010-03-02 11:42:11 -08003317 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318
Mike Cleronf116bf82009-09-27 19:14:12 -07003319 if (!fadeScrollbars) {
3320 scrollabilityCache.state = ScrollabilityCache.ON;
3321 }
3322 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003323
3324
Mike Cleronf116bf82009-09-27 19:14:12 -07003325 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3326 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3327 .getScrollBarFadeDuration());
3328 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3329 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3330 ViewConfiguration.getScrollDefaultDelay());
3331
Joe Malin32736f02011-01-19 16:14:20 -08003332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3334 com.android.internal.R.styleable.View_scrollbarSize,
3335 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3336
3337 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3338 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3339
3340 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3341 if (thumb != null) {
3342 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3343 }
3344
3345 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3346 false);
3347 if (alwaysDraw) {
3348 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3349 }
3350
3351 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3352 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3353
3354 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3355 if (thumb != null) {
3356 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3357 }
3358
3359 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3360 false);
3361 if (alwaysDraw) {
3362 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3363 }
3364
3365 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003366 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
3368
3369 /**
3370 * <p>
3371 * Initalizes the scrollability cache if necessary.
3372 * </p>
3373 */
3374 private void initScrollCache() {
3375 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003376 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 }
3378 }
3379
3380 /**
Adam Powell20232d02010-12-08 21:08:53 -08003381 * Set the position of the vertical scroll bar. Should be one of
3382 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3383 * {@link #SCROLLBAR_POSITION_RIGHT}.
3384 *
3385 * @param position Where the vertical scroll bar should be positioned.
3386 */
3387 public void setVerticalScrollbarPosition(int position) {
3388 if (mVerticalScrollbarPosition != position) {
3389 mVerticalScrollbarPosition = position;
3390 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003391 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003392 }
3393 }
3394
3395 /**
3396 * @return The position where the vertical scroll bar will show, if applicable.
3397 * @see #setVerticalScrollbarPosition(int)
3398 */
3399 public int getVerticalScrollbarPosition() {
3400 return mVerticalScrollbarPosition;
3401 }
3402
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003403 ListenerInfo getListenerInfo() {
3404 if (mListenerInfo != null) {
3405 return mListenerInfo;
3406 }
3407 mListenerInfo = new ListenerInfo();
3408 return mListenerInfo;
3409 }
3410
Adam Powell20232d02010-12-08 21:08:53 -08003411 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 * Register a callback to be invoked when focus of this view changed.
3413 *
3414 * @param l The callback that will run.
3415 */
3416 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003417 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 }
3419
3420 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003421 * Add a listener that will be called when the bounds of the view change due to
3422 * layout processing.
3423 *
3424 * @param listener The listener that will be called when layout bounds change.
3425 */
3426 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003427 ListenerInfo li = getListenerInfo();
3428 if (li.mOnLayoutChangeListeners == null) {
3429 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003430 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003431 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3432 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003433 }
Chet Haase21cd1382010-09-01 17:42:29 -07003434 }
3435
3436 /**
3437 * Remove a listener for layout changes.
3438 *
3439 * @param listener The listener for layout bounds change.
3440 */
3441 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003442 ListenerInfo li = mListenerInfo;
3443 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003444 return;
3445 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003446 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003447 }
3448
3449 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003450 * Add a listener for attach state changes.
3451 *
3452 * This listener will be called whenever this view is attached or detached
3453 * from a window. Remove the listener using
3454 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3455 *
3456 * @param listener Listener to attach
3457 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3458 */
3459 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003460 ListenerInfo li = getListenerInfo();
3461 if (li.mOnAttachStateChangeListeners == null) {
3462 li.mOnAttachStateChangeListeners
3463 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003464 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003465 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003466 }
3467
3468 /**
3469 * Remove a listener for attach state changes. The listener will receive no further
3470 * notification of window attach/detach events.
3471 *
3472 * @param listener Listener to remove
3473 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3474 */
3475 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003476 ListenerInfo li = mListenerInfo;
3477 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003478 return;
3479 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003480 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003481 }
3482
3483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 * Returns the focus-change callback registered for this view.
3485 *
3486 * @return The callback, or null if one is not registered.
3487 */
3488 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003489 ListenerInfo li = mListenerInfo;
3490 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 }
3492
3493 /**
3494 * Register a callback to be invoked when this view is clicked. If this view is not
3495 * clickable, it becomes clickable.
3496 *
3497 * @param l The callback that will run
3498 *
3499 * @see #setClickable(boolean)
3500 */
3501 public void setOnClickListener(OnClickListener l) {
3502 if (!isClickable()) {
3503 setClickable(true);
3504 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003505 getListenerInfo().mOnClickListener = l;
3506 }
3507
3508 /**
3509 * Return whether this view has an attached OnClickListener. Returns
3510 * true if there is a listener, false if there is none.
3511 */
3512 public boolean hasOnClickListeners() {
3513 ListenerInfo li = mListenerInfo;
3514 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 }
3516
3517 /**
3518 * Register a callback to be invoked when this view is clicked and held. If this view is not
3519 * long clickable, it becomes long clickable.
3520 *
3521 * @param l The callback that will run
3522 *
3523 * @see #setLongClickable(boolean)
3524 */
3525 public void setOnLongClickListener(OnLongClickListener l) {
3526 if (!isLongClickable()) {
3527 setLongClickable(true);
3528 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003529 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 }
3531
3532 /**
3533 * Register a callback to be invoked when the context menu for this view is
3534 * being built. If this view is not long clickable, it becomes long clickable.
3535 *
3536 * @param l The callback that will run
3537 *
3538 */
3539 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3540 if (!isLongClickable()) {
3541 setLongClickable(true);
3542 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003543 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
3545
3546 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003547 * Call this view's OnClickListener, if it is defined. Performs all normal
3548 * actions associated with clicking: reporting accessibility event, playing
3549 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 *
3551 * @return True there was an assigned OnClickListener that was called, false
3552 * otherwise is returned.
3553 */
3554 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003555 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3556
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003557 ListenerInfo li = mListenerInfo;
3558 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003560 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 return true;
3562 }
3563
3564 return false;
3565 }
3566
3567 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003568 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3569 * this only calls the listener, and does not do any associated clicking
3570 * actions like reporting an accessibility event.
3571 *
3572 * @return True there was an assigned OnClickListener that was called, false
3573 * otherwise is returned.
3574 */
3575 public boolean callOnClick() {
3576 ListenerInfo li = mListenerInfo;
3577 if (li != null && li.mOnClickListener != null) {
3578 li.mOnClickListener.onClick(this);
3579 return true;
3580 }
3581 return false;
3582 }
3583
3584 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003585 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3586 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003588 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 */
3590 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003591 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003594 ListenerInfo li = mListenerInfo;
3595 if (li != null && li.mOnLongClickListener != null) {
3596 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 }
3598 if (!handled) {
3599 handled = showContextMenu();
3600 }
3601 if (handled) {
3602 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3603 }
3604 return handled;
3605 }
3606
3607 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003608 * Performs button-related actions during a touch down event.
3609 *
3610 * @param event The event.
3611 * @return True if the down was consumed.
3612 *
3613 * @hide
3614 */
3615 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3616 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3617 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3618 return true;
3619 }
3620 }
3621 return false;
3622 }
3623
3624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 * Bring up the context menu for this view.
3626 *
3627 * @return Whether a context menu was displayed.
3628 */
3629 public boolean showContextMenu() {
3630 return getParent().showContextMenuForChild(this);
3631 }
3632
3633 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003634 * Bring up the context menu for this view, referring to the item under the specified point.
3635 *
3636 * @param x The referenced x coordinate.
3637 * @param y The referenced y coordinate.
3638 * @param metaState The keyboard modifiers that were pressed.
3639 * @return Whether a context menu was displayed.
3640 *
3641 * @hide
3642 */
3643 public boolean showContextMenu(float x, float y, int metaState) {
3644 return showContextMenu();
3645 }
3646
3647 /**
Adam Powell6e346362010-07-23 10:18:23 -07003648 * Start an action mode.
3649 *
3650 * @param callback Callback that will control the lifecycle of the action mode
3651 * @return The new action mode if it is started, null otherwise
3652 *
3653 * @see ActionMode
3654 */
3655 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003656 ViewParent parent = getParent();
3657 if (parent == null) return null;
3658 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003659 }
3660
3661 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 * Register a callback to be invoked when a key is pressed in this view.
3663 * @param l the key listener to attach to this view
3664 */
3665 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003666 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 }
3668
3669 /**
3670 * Register a callback to be invoked when a touch event is sent to this view.
3671 * @param l the touch listener to attach to this view
3672 */
3673 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 }
3676
3677 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003678 * Register a callback to be invoked when a generic motion event is sent to this view.
3679 * @param l the generic motion listener to attach to this view
3680 */
3681 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003682 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003683 }
3684
3685 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003686 * Register a callback to be invoked when a hover event is sent to this view.
3687 * @param l the hover listener to attach to this view
3688 */
3689 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003690 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003691 }
3692
3693 /**
Joe Malin32736f02011-01-19 16:14:20 -08003694 * Register a drag event listener callback object for this View. The parameter is
3695 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3696 * View, the system calls the
3697 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3698 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003699 */
3700 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003701 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003702 }
3703
3704 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003705 * Give this view focus. This will cause
3706 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 *
3708 * Note: this does not check whether this {@link View} should get focus, it just
3709 * gives it focus no matter what. It should only be called internally by framework
3710 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3711 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003712 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3713 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 * focus moved when requestFocus() is called. It may not always
3715 * apply, in which case use the default View.FOCUS_DOWN.
3716 * @param previouslyFocusedRect The rectangle of the view that had focus
3717 * prior in this View's coordinate system.
3718 */
3719 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3720 if (DBG) {
3721 System.out.println(this + " requestFocus()");
3722 }
3723
3724 if ((mPrivateFlags & FOCUSED) == 0) {
3725 mPrivateFlags |= FOCUSED;
3726
3727 if (mParent != null) {
3728 mParent.requestChildFocus(this, this);
3729 }
3730
3731 onFocusChanged(true, direction, previouslyFocusedRect);
3732 refreshDrawableState();
3733 }
3734 }
3735
3736 /**
3737 * Request that a rectangle of this view be visible on the screen,
3738 * scrolling if necessary just enough.
3739 *
3740 * <p>A View should call this if it maintains some notion of which part
3741 * of its content is interesting. For example, a text editing view
3742 * should call this when its cursor moves.
3743 *
3744 * @param rectangle The rectangle.
3745 * @return Whether any parent scrolled.
3746 */
3747 public boolean requestRectangleOnScreen(Rect rectangle) {
3748 return requestRectangleOnScreen(rectangle, false);
3749 }
3750
3751 /**
3752 * Request that a rectangle of this view be visible on the screen,
3753 * scrolling if necessary just enough.
3754 *
3755 * <p>A View should call this if it maintains some notion of which part
3756 * of its content is interesting. For example, a text editing view
3757 * should call this when its cursor moves.
3758 *
3759 * <p>When <code>immediate</code> is set to true, scrolling will not be
3760 * animated.
3761 *
3762 * @param rectangle The rectangle.
3763 * @param immediate True to forbid animated scrolling, false otherwise
3764 * @return Whether any parent scrolled.
3765 */
3766 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3767 View child = this;
3768 ViewParent parent = mParent;
3769 boolean scrolled = false;
3770 while (parent != null) {
3771 scrolled |= parent.requestChildRectangleOnScreen(child,
3772 rectangle, immediate);
3773
3774 // offset rect so next call has the rectangle in the
3775 // coordinate system of its direct child.
3776 rectangle.offset(child.getLeft(), child.getTop());
3777 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3778
3779 if (!(parent instanceof View)) {
3780 break;
3781 }
Romain Guy8506ab42009-06-11 17:35:47 -07003782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 child = (View) parent;
3784 parent = child.getParent();
3785 }
3786 return scrolled;
3787 }
3788
3789 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003790 * Called when this view wants to give up focus. If focus is cleared
3791 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3792 * <p>
3793 * <strong>Note:</strong> When a View clears focus the framework is trying
3794 * to give focus to the first focusable View from the top. Hence, if this
3795 * View is the first from the top that can take focus, then its focus will
3796 * not be cleared nor will the focus change callback be invoked.
3797 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 */
3799 public void clearFocus() {
3800 if (DBG) {
3801 System.out.println(this + " clearFocus()");
3802 }
3803
3804 if ((mPrivateFlags & FOCUSED) != 0) {
3805 mPrivateFlags &= ~FOCUSED;
3806
3807 if (mParent != null) {
3808 mParent.clearChildFocus(this);
3809 }
3810
3811 onFocusChanged(false, 0, null);
3812 refreshDrawableState();
3813 }
3814 }
3815
3816 /**
3817 * Called to clear the focus of a view that is about to be removed.
3818 * Doesn't call clearChildFocus, which prevents this view from taking
3819 * focus again before it has been removed from the parent
3820 */
3821 void clearFocusForRemoval() {
3822 if ((mPrivateFlags & FOCUSED) != 0) {
3823 mPrivateFlags &= ~FOCUSED;
3824
3825 onFocusChanged(false, 0, null);
3826 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003827
3828 // The view cleared focus and invoked the callbacks, so now is the
3829 // time to give focus to the the first focusable from the top to
3830 // ensure that the gain focus is announced after clear focus.
3831 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 }
3833 }
3834
3835 /**
3836 * Called internally by the view system when a new view is getting focus.
3837 * This is what clears the old focus.
3838 */
3839 void unFocus() {
3840 if (DBG) {
3841 System.out.println(this + " unFocus()");
3842 }
3843
3844 if ((mPrivateFlags & FOCUSED) != 0) {
3845 mPrivateFlags &= ~FOCUSED;
3846
3847 onFocusChanged(false, 0, null);
3848 refreshDrawableState();
3849 }
3850 }
3851
3852 /**
3853 * Returns true if this view has focus iteself, or is the ancestor of the
3854 * view that has focus.
3855 *
3856 * @return True if this view has or contains focus, false otherwise.
3857 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003858 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 public boolean hasFocus() {
3860 return (mPrivateFlags & FOCUSED) != 0;
3861 }
3862
3863 /**
3864 * Returns true if this view is focusable or if it contains a reachable View
3865 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3866 * is a View whose parents do not block descendants focus.
3867 *
3868 * Only {@link #VISIBLE} views are considered focusable.
3869 *
3870 * @return True if the view is focusable or if the view contains a focusable
3871 * View, false otherwise.
3872 *
3873 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3874 */
3875 public boolean hasFocusable() {
3876 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3877 }
3878
3879 /**
3880 * Called by the view system when the focus state of this view changes.
3881 * When the focus change event is caused by directional navigation, direction
3882 * and previouslyFocusedRect provide insight into where the focus is coming from.
3883 * When overriding, be sure to call up through to the super class so that
3884 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003885 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 * @param gainFocus True if the View has focus; false otherwise.
3887 * @param direction The direction focus has moved when requestFocus()
3888 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003889 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3890 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3891 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3893 * system, of the previously focused view. If applicable, this will be
3894 * passed in as finer grained information about where the focus is coming
3895 * from (in addition to direction). Will be <code>null</code> otherwise.
3896 */
3897 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003898 if (gainFocus) {
3899 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3900 }
3901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 InputMethodManager imm = InputMethodManager.peekInstance();
3903 if (!gainFocus) {
3904 if (isPressed()) {
3905 setPressed(false);
3906 }
3907 if (imm != null && mAttachInfo != null
3908 && mAttachInfo.mHasWindowFocus) {
3909 imm.focusOut(this);
3910 }
Romain Guya2431d02009-04-30 16:30:00 -07003911 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 } else if (imm != null && mAttachInfo != null
3913 && mAttachInfo.mHasWindowFocus) {
3914 imm.focusIn(this);
3915 }
Romain Guy8506ab42009-06-11 17:35:47 -07003916
Romain Guy0fd89bf2011-01-26 15:41:30 -08003917 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003918 ListenerInfo li = mListenerInfo;
3919 if (li != null && li.mOnFocusChangeListener != null) {
3920 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 }
Joe Malin32736f02011-01-19 16:14:20 -08003922
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003923 if (mAttachInfo != null) {
3924 mAttachInfo.mKeyDispatchState.reset(this);
3925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 }
3927
3928 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003929 * Sends an accessibility event of the given type. If accessiiblity is
3930 * not enabled this method has no effect. The default implementation calls
3931 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3932 * to populate information about the event source (this View), then calls
3933 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3934 * populate the text content of the event source including its descendants,
3935 * and last calls
3936 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3937 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003938 * <p>
3939 * If an {@link AccessibilityDelegate} has been specified via calling
3940 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3941 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3942 * responsible for handling this call.
3943 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003944 *
Scott Mainb303d832011-10-12 16:45:18 -07003945 * @param eventType The type of the event to send, as defined by several types from
3946 * {@link android.view.accessibility.AccessibilityEvent}, such as
3947 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3948 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003949 *
3950 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3951 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3952 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003953 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003954 */
3955 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003956 if (mAccessibilityDelegate != null) {
3957 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3958 } else {
3959 sendAccessibilityEventInternal(eventType);
3960 }
3961 }
3962
3963 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08003964 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
3965 * {@link AccessibilityEvent} to make an announcement which is related to some
3966 * sort of a context change for which none of the events representing UI transitions
3967 * is a good fit. For example, announcing a new page in a book. If accessibility
3968 * is not enabled this method does nothing.
3969 *
3970 * @param text The announcement text.
3971 */
3972 public void announceForAccessibility(CharSequence text) {
3973 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3974 AccessibilityEvent event = AccessibilityEvent.obtain(
3975 AccessibilityEvent.TYPE_ANNOUNCEMENT);
3976 event.getText().add(text);
3977 sendAccessibilityEventUnchecked(event);
3978 }
3979 }
3980
3981 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003982 * @see #sendAccessibilityEvent(int)
3983 *
3984 * Note: Called from the default {@link AccessibilityDelegate}.
3985 */
3986 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003987 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3988 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3989 }
3990 }
3991
3992 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003993 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3994 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003995 * perform a check whether accessibility is enabled.
3996 * <p>
3997 * If an {@link AccessibilityDelegate} has been specified via calling
3998 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3999 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4000 * is responsible for handling this call.
4001 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004002 *
4003 * @param event The event to send.
4004 *
4005 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004006 */
4007 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004008 if (mAccessibilityDelegate != null) {
4009 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4010 } else {
4011 sendAccessibilityEventUncheckedInternal(event);
4012 }
4013 }
4014
4015 /**
4016 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4017 *
4018 * Note: Called from the default {@link AccessibilityDelegate}.
4019 */
4020 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004021 if (!isShown()) {
4022 return;
4023 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004024 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004025 // Only a subset of accessibility events populates text content.
4026 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4027 dispatchPopulateAccessibilityEvent(event);
4028 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004029 // In the beginning we called #isShown(), so we know that getParent() is not null.
4030 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004031 }
4032
4033 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004034 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4035 * to its children for adding their text content to the event. Note that the
4036 * event text is populated in a separate dispatch path since we add to the
4037 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004038 * A typical implementation will call
4039 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4040 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4041 * on each child. Override this method if custom population of the event text
4042 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004043 * <p>
4044 * If an {@link AccessibilityDelegate} has been specified via calling
4045 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4046 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4047 * is responsible for handling this call.
4048 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004049 * <p>
4050 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4051 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4052 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004053 *
4054 * @param event The event.
4055 *
4056 * @return True if the event population was completed.
4057 */
4058 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004059 if (mAccessibilityDelegate != null) {
4060 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4061 } else {
4062 return dispatchPopulateAccessibilityEventInternal(event);
4063 }
4064 }
4065
4066 /**
4067 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4068 *
4069 * Note: Called from the default {@link AccessibilityDelegate}.
4070 */
4071 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004072 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004073 return false;
4074 }
4075
4076 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004077 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004078 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004079 * text content. While this method is free to modify event
4080 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004081 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4082 * <p>
4083 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004084 * to the text added by the super implementation:
4085 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004086 * super.onPopulateAccessibilityEvent(event);
4087 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4088 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4089 * mCurrentDate.getTimeInMillis(), flags);
4090 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004091 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004092 * <p>
4093 * If an {@link AccessibilityDelegate} has been specified via calling
4094 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4095 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4096 * is responsible for handling this call.
4097 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004098 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4099 * information to the event, in case the default implementation has basic information to add.
4100 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004101 *
4102 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004103 *
4104 * @see #sendAccessibilityEvent(int)
4105 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004106 */
4107 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004108 if (mAccessibilityDelegate != null) {
4109 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4110 } else {
4111 onPopulateAccessibilityEventInternal(event);
4112 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004113 }
4114
4115 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004116 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4117 *
4118 * Note: Called from the default {@link AccessibilityDelegate}.
4119 */
4120 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4121
4122 }
4123
4124 /**
4125 * Initializes an {@link AccessibilityEvent} with information about
4126 * this View which is the event source. In other words, the source of
4127 * an accessibility event is the view whose state change triggered firing
4128 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004129 * <p>
4130 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004131 * to properties set by the super implementation:
4132 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4133 * super.onInitializeAccessibilityEvent(event);
4134 * event.setPassword(true);
4135 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004136 * <p>
4137 * If an {@link AccessibilityDelegate} has been specified via calling
4138 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4139 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4140 * is responsible for handling this call.
4141 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004142 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4143 * information to the event, in case the default implementation has basic information to add.
4144 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004145 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004146 *
4147 * @see #sendAccessibilityEvent(int)
4148 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4149 */
4150 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004151 if (mAccessibilityDelegate != null) {
4152 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4153 } else {
4154 onInitializeAccessibilityEventInternal(event);
4155 }
4156 }
4157
4158 /**
4159 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4160 *
4161 * Note: Called from the default {@link AccessibilityDelegate}.
4162 */
4163 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004164 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004165 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004166 event.setPackageName(getContext().getPackageName());
4167 event.setEnabled(isEnabled());
4168 event.setContentDescription(mContentDescription);
4169
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004170 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4171 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4172 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4173 FOCUSABLES_ALL);
4174 event.setItemCount(focusablesTempList.size());
4175 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4176 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004177 }
4178 }
4179
4180 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004181 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4182 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4183 * This method is responsible for obtaining an accessibility node info from a
4184 * pool of reusable instances and calling
4185 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4186 * initialize the former.
4187 * <p>
4188 * Note: The client is responsible for recycling the obtained instance by calling
4189 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4190 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004191 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004192 * @return A populated {@link AccessibilityNodeInfo}.
4193 *
4194 * @see AccessibilityNodeInfo
4195 */
4196 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004197 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4198 if (provider != null) {
4199 return provider.createAccessibilityNodeInfo(View.NO_ID);
4200 } else {
4201 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4202 onInitializeAccessibilityNodeInfo(info);
4203 return info;
4204 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004205 }
4206
4207 /**
4208 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4209 * The base implementation sets:
4210 * <ul>
4211 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004212 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4213 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004214 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4215 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4216 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4217 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4218 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4219 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4220 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4221 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4222 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4223 * </ul>
4224 * <p>
4225 * Subclasses should override this method, call the super implementation,
4226 * and set additional attributes.
4227 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004228 * <p>
4229 * If an {@link AccessibilityDelegate} has been specified via calling
4230 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4231 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4232 * is responsible for handling this call.
4233 * </p>
4234 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004235 * @param info The instance to initialize.
4236 */
4237 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004238 if (mAccessibilityDelegate != null) {
4239 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4240 } else {
4241 onInitializeAccessibilityNodeInfoInternal(info);
4242 }
4243 }
4244
4245 /**
4246 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4247 *
4248 * Note: Called from the default {@link AccessibilityDelegate}.
4249 */
4250 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004251 Rect bounds = mAttachInfo.mTmpInvalRect;
4252 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004253 info.setBoundsInParent(bounds);
4254
4255 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4256 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004257 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004258 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4259 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004260
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004261 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4262 ViewParent parent = getParent();
4263 if (parent instanceof View) {
4264 View parentView = (View) parent;
4265 info.setParent(parentView);
4266 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004267 }
4268
4269 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004270 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004271 info.setContentDescription(getContentDescription());
4272
4273 info.setEnabled(isEnabled());
4274 info.setClickable(isClickable());
4275 info.setFocusable(isFocusable());
4276 info.setFocused(isFocused());
4277 info.setSelected(isSelected());
4278 info.setLongClickable(isLongClickable());
4279
4280 // TODO: These make sense only if we are in an AdapterView but all
4281 // views can be selected. Maybe from accessiiblity perspective
4282 // we should report as selectable view in an AdapterView.
4283 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4284 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4285
4286 if (isFocusable()) {
4287 if (isFocused()) {
4288 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4289 } else {
4290 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4291 }
4292 }
4293 }
4294
4295 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004296 * Sets a delegate for implementing accessibility support via compositon as
4297 * opposed to inheritance. The delegate's primary use is for implementing
4298 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4299 *
4300 * @param delegate The delegate instance.
4301 *
4302 * @see AccessibilityDelegate
4303 */
4304 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4305 mAccessibilityDelegate = delegate;
4306 }
4307
4308 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004309 * Gets the provider for managing a virtual view hierarchy rooted at this View
4310 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4311 * that explore the window content.
4312 * <p>
4313 * If this method returns an instance, this instance is responsible for managing
4314 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4315 * View including the one representing the View itself. Similarly the returned
4316 * instance is responsible for performing accessibility actions on any virtual
4317 * view or the root view itself.
4318 * </p>
4319 * <p>
4320 * If an {@link AccessibilityDelegate} has been specified via calling
4321 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4322 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4323 * is responsible for handling this call.
4324 * </p>
4325 *
4326 * @return The provider.
4327 *
4328 * @see AccessibilityNodeProvider
4329 */
4330 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4331 if (mAccessibilityDelegate != null) {
4332 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4333 } else {
4334 return null;
4335 }
4336 }
4337
4338 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004339 * Gets the unique identifier of this view on the screen for accessibility purposes.
4340 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4341 *
4342 * @return The view accessibility id.
4343 *
4344 * @hide
4345 */
4346 public int getAccessibilityViewId() {
4347 if (mAccessibilityViewId == NO_ID) {
4348 mAccessibilityViewId = sNextAccessibilityViewId++;
4349 }
4350 return mAccessibilityViewId;
4351 }
4352
4353 /**
4354 * Gets the unique identifier of the window in which this View reseides.
4355 *
4356 * @return The window accessibility id.
4357 *
4358 * @hide
4359 */
4360 public int getAccessibilityWindowId() {
4361 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4362 }
4363
4364 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004365 * Gets the {@link View} description. It briefly describes the view and is
4366 * primarily used for accessibility support. Set this property to enable
4367 * better accessibility support for your application. This is especially
4368 * true for views that do not have textual representation (For example,
4369 * ImageButton).
4370 *
4371 * @return The content descriptiopn.
4372 *
4373 * @attr ref android.R.styleable#View_contentDescription
4374 */
4375 public CharSequence getContentDescription() {
4376 return mContentDescription;
4377 }
4378
4379 /**
4380 * Sets the {@link View} description. It briefly describes the view and is
4381 * primarily used for accessibility support. Set this property to enable
4382 * better accessibility support for your application. This is especially
4383 * true for views that do not have textual representation (For example,
4384 * ImageButton).
4385 *
4386 * @param contentDescription The content description.
4387 *
4388 * @attr ref android.R.styleable#View_contentDescription
4389 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004390 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004391 public void setContentDescription(CharSequence contentDescription) {
4392 mContentDescription = contentDescription;
4393 }
4394
4395 /**
Romain Guya2431d02009-04-30 16:30:00 -07004396 * Invoked whenever this view loses focus, either by losing window focus or by losing
4397 * focus within its window. This method can be used to clear any state tied to the
4398 * focus. For instance, if a button is held pressed with the trackball and the window
4399 * loses focus, this method can be used to cancel the press.
4400 *
4401 * Subclasses of View overriding this method should always call super.onFocusLost().
4402 *
4403 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004404 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004405 *
4406 * @hide pending API council approval
4407 */
4408 protected void onFocusLost() {
4409 resetPressedState();
4410 }
4411
4412 private void resetPressedState() {
4413 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4414 return;
4415 }
4416
4417 if (isPressed()) {
4418 setPressed(false);
4419
4420 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004421 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004422 }
4423 }
4424 }
4425
4426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 * Returns true if this view has focus
4428 *
4429 * @return True if this view has focus, false otherwise.
4430 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004431 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 public boolean isFocused() {
4433 return (mPrivateFlags & FOCUSED) != 0;
4434 }
4435
4436 /**
4437 * Find the view in the hierarchy rooted at this view that currently has
4438 * focus.
4439 *
4440 * @return The view that currently has focus, or null if no focused view can
4441 * be found.
4442 */
4443 public View findFocus() {
4444 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4445 }
4446
4447 /**
4448 * Change whether this view is one of the set of scrollable containers in
4449 * its window. This will be used to determine whether the window can
4450 * resize or must pan when a soft input area is open -- scrollable
4451 * containers allow the window to use resize mode since the container
4452 * will appropriately shrink.
4453 */
4454 public void setScrollContainer(boolean isScrollContainer) {
4455 if (isScrollContainer) {
4456 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4457 mAttachInfo.mScrollContainers.add(this);
4458 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4459 }
4460 mPrivateFlags |= SCROLL_CONTAINER;
4461 } else {
4462 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4463 mAttachInfo.mScrollContainers.remove(this);
4464 }
4465 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4466 }
4467 }
4468
4469 /**
4470 * Returns the quality of the drawing cache.
4471 *
4472 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4473 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4474 *
4475 * @see #setDrawingCacheQuality(int)
4476 * @see #setDrawingCacheEnabled(boolean)
4477 * @see #isDrawingCacheEnabled()
4478 *
4479 * @attr ref android.R.styleable#View_drawingCacheQuality
4480 */
4481 public int getDrawingCacheQuality() {
4482 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4483 }
4484
4485 /**
4486 * Set the drawing cache quality of this view. This value is used only when the
4487 * drawing cache is enabled
4488 *
4489 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4490 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4491 *
4492 * @see #getDrawingCacheQuality()
4493 * @see #setDrawingCacheEnabled(boolean)
4494 * @see #isDrawingCacheEnabled()
4495 *
4496 * @attr ref android.R.styleable#View_drawingCacheQuality
4497 */
4498 public void setDrawingCacheQuality(int quality) {
4499 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4500 }
4501
4502 /**
4503 * Returns whether the screen should remain on, corresponding to the current
4504 * value of {@link #KEEP_SCREEN_ON}.
4505 *
4506 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4507 *
4508 * @see #setKeepScreenOn(boolean)
4509 *
4510 * @attr ref android.R.styleable#View_keepScreenOn
4511 */
4512 public boolean getKeepScreenOn() {
4513 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4514 }
4515
4516 /**
4517 * Controls whether the screen should remain on, modifying the
4518 * value of {@link #KEEP_SCREEN_ON}.
4519 *
4520 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4521 *
4522 * @see #getKeepScreenOn()
4523 *
4524 * @attr ref android.R.styleable#View_keepScreenOn
4525 */
4526 public void setKeepScreenOn(boolean keepScreenOn) {
4527 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4528 }
4529
4530 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004531 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4532 * @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 -08004533 *
4534 * @attr ref android.R.styleable#View_nextFocusLeft
4535 */
4536 public int getNextFocusLeftId() {
4537 return mNextFocusLeftId;
4538 }
4539
4540 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004541 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4542 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4543 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 *
4545 * @attr ref android.R.styleable#View_nextFocusLeft
4546 */
4547 public void setNextFocusLeftId(int nextFocusLeftId) {
4548 mNextFocusLeftId = nextFocusLeftId;
4549 }
4550
4551 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004552 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4553 * @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 -08004554 *
4555 * @attr ref android.R.styleable#View_nextFocusRight
4556 */
4557 public int getNextFocusRightId() {
4558 return mNextFocusRightId;
4559 }
4560
4561 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004562 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4563 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4564 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 *
4566 * @attr ref android.R.styleable#View_nextFocusRight
4567 */
4568 public void setNextFocusRightId(int nextFocusRightId) {
4569 mNextFocusRightId = nextFocusRightId;
4570 }
4571
4572 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004573 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4574 * @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 -08004575 *
4576 * @attr ref android.R.styleable#View_nextFocusUp
4577 */
4578 public int getNextFocusUpId() {
4579 return mNextFocusUpId;
4580 }
4581
4582 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004583 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4584 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4585 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 *
4587 * @attr ref android.R.styleable#View_nextFocusUp
4588 */
4589 public void setNextFocusUpId(int nextFocusUpId) {
4590 mNextFocusUpId = nextFocusUpId;
4591 }
4592
4593 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004594 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4595 * @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 -08004596 *
4597 * @attr ref android.R.styleable#View_nextFocusDown
4598 */
4599 public int getNextFocusDownId() {
4600 return mNextFocusDownId;
4601 }
4602
4603 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004604 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4605 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4606 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 *
4608 * @attr ref android.R.styleable#View_nextFocusDown
4609 */
4610 public void setNextFocusDownId(int nextFocusDownId) {
4611 mNextFocusDownId = nextFocusDownId;
4612 }
4613
4614 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004615 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4616 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4617 *
4618 * @attr ref android.R.styleable#View_nextFocusForward
4619 */
4620 public int getNextFocusForwardId() {
4621 return mNextFocusForwardId;
4622 }
4623
4624 /**
4625 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4626 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4627 * decide automatically.
4628 *
4629 * @attr ref android.R.styleable#View_nextFocusForward
4630 */
4631 public void setNextFocusForwardId(int nextFocusForwardId) {
4632 mNextFocusForwardId = nextFocusForwardId;
4633 }
4634
4635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 * Returns the visibility of this view and all of its ancestors
4637 *
4638 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4639 */
4640 public boolean isShown() {
4641 View current = this;
4642 //noinspection ConstantConditions
4643 do {
4644 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4645 return false;
4646 }
4647 ViewParent parent = current.mParent;
4648 if (parent == null) {
4649 return false; // We are not attached to the view root
4650 }
4651 if (!(parent instanceof View)) {
4652 return true;
4653 }
4654 current = (View) parent;
4655 } while (current != null);
4656
4657 return false;
4658 }
4659
4660 /**
4661 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4662 * is set
4663 *
4664 * @param insets Insets for system windows
4665 *
4666 * @return True if this view applied the insets, false otherwise
4667 */
4668 protected boolean fitSystemWindows(Rect insets) {
4669 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4670 mPaddingLeft = insets.left;
4671 mPaddingTop = insets.top;
4672 mPaddingRight = insets.right;
4673 mPaddingBottom = insets.bottom;
4674 requestLayout();
4675 return true;
4676 }
4677 return false;
4678 }
4679
4680 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004681 * Set whether or not this view should account for system screen decorations
4682 * such as the status bar and inset its content. This allows this view to be
4683 * positioned in absolute screen coordinates and remain visible to the user.
4684 *
4685 * <p>This should only be used by top-level window decor views.
4686 *
4687 * @param fitSystemWindows true to inset content for system screen decorations, false for
4688 * default behavior.
4689 *
4690 * @attr ref android.R.styleable#View_fitsSystemWindows
4691 */
4692 public void setFitsSystemWindows(boolean fitSystemWindows) {
4693 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4694 }
4695
4696 /**
4697 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4698 * will account for system screen decorations such as the status bar and inset its
4699 * content. This allows the view to be positioned in absolute screen coordinates
4700 * and remain visible to the user.
4701 *
4702 * @return true if this view will adjust its content bounds for system screen decorations.
4703 *
4704 * @attr ref android.R.styleable#View_fitsSystemWindows
4705 */
4706 public boolean fitsSystemWindows() {
4707 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4708 }
4709
4710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 * Returns the visibility status for this view.
4712 *
4713 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4714 * @attr ref android.R.styleable#View_visibility
4715 */
4716 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004717 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4718 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4719 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 })
4721 public int getVisibility() {
4722 return mViewFlags & VISIBILITY_MASK;
4723 }
4724
4725 /**
4726 * Set the enabled state of this view.
4727 *
4728 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4729 * @attr ref android.R.styleable#View_visibility
4730 */
4731 @RemotableViewMethod
4732 public void setVisibility(int visibility) {
4733 setFlags(visibility, VISIBILITY_MASK);
4734 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4735 }
4736
4737 /**
4738 * Returns the enabled status for this view. The interpretation of the
4739 * enabled state varies by subclass.
4740 *
4741 * @return True if this view is enabled, false otherwise.
4742 */
4743 @ViewDebug.ExportedProperty
4744 public boolean isEnabled() {
4745 return (mViewFlags & ENABLED_MASK) == ENABLED;
4746 }
4747
4748 /**
4749 * Set the enabled state of this view. The interpretation of the enabled
4750 * state varies by subclass.
4751 *
4752 * @param enabled True if this view is enabled, false otherwise.
4753 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004754 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004756 if (enabled == isEnabled()) return;
4757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4759
4760 /*
4761 * The View most likely has to change its appearance, so refresh
4762 * the drawable state.
4763 */
4764 refreshDrawableState();
4765
4766 // Invalidate too, since the default behavior for views is to be
4767 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004768 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004769 }
4770
4771 /**
4772 * Set whether this view can receive the focus.
4773 *
4774 * Setting this to false will also ensure that this view is not focusable
4775 * in touch mode.
4776 *
4777 * @param focusable If true, this view can receive the focus.
4778 *
4779 * @see #setFocusableInTouchMode(boolean)
4780 * @attr ref android.R.styleable#View_focusable
4781 */
4782 public void setFocusable(boolean focusable) {
4783 if (!focusable) {
4784 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4785 }
4786 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4787 }
4788
4789 /**
4790 * Set whether this view can receive focus while in touch mode.
4791 *
4792 * Setting this to true will also ensure that this view is focusable.
4793 *
4794 * @param focusableInTouchMode If true, this view can receive the focus while
4795 * in touch mode.
4796 *
4797 * @see #setFocusable(boolean)
4798 * @attr ref android.R.styleable#View_focusableInTouchMode
4799 */
4800 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4801 // Focusable in touch mode should always be set before the focusable flag
4802 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4803 // which, in touch mode, will not successfully request focus on this view
4804 // because the focusable in touch mode flag is not set
4805 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4806 if (focusableInTouchMode) {
4807 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4808 }
4809 }
4810
4811 /**
4812 * Set whether this view should have sound effects enabled for events such as
4813 * clicking and touching.
4814 *
4815 * <p>You may wish to disable sound effects for a view if you already play sounds,
4816 * for instance, a dial key that plays dtmf tones.
4817 *
4818 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4819 * @see #isSoundEffectsEnabled()
4820 * @see #playSoundEffect(int)
4821 * @attr ref android.R.styleable#View_soundEffectsEnabled
4822 */
4823 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4824 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4825 }
4826
4827 /**
4828 * @return whether this view should have sound effects enabled for events such as
4829 * clicking and touching.
4830 *
4831 * @see #setSoundEffectsEnabled(boolean)
4832 * @see #playSoundEffect(int)
4833 * @attr ref android.R.styleable#View_soundEffectsEnabled
4834 */
4835 @ViewDebug.ExportedProperty
4836 public boolean isSoundEffectsEnabled() {
4837 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4838 }
4839
4840 /**
4841 * Set whether this view should have haptic feedback for events such as
4842 * long presses.
4843 *
4844 * <p>You may wish to disable haptic feedback if your view already controls
4845 * its own haptic feedback.
4846 *
4847 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4848 * @see #isHapticFeedbackEnabled()
4849 * @see #performHapticFeedback(int)
4850 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4851 */
4852 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4853 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4854 }
4855
4856 /**
4857 * @return whether this view should have haptic feedback enabled for events
4858 * long presses.
4859 *
4860 * @see #setHapticFeedbackEnabled(boolean)
4861 * @see #performHapticFeedback(int)
4862 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4863 */
4864 @ViewDebug.ExportedProperty
4865 public boolean isHapticFeedbackEnabled() {
4866 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4867 }
4868
4869 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004870 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004871 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004872 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4873 * {@link #LAYOUT_DIRECTION_RTL},
4874 * {@link #LAYOUT_DIRECTION_INHERIT} or
4875 * {@link #LAYOUT_DIRECTION_LOCALE}.
4876 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004877 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004878 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004879 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4880 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4881 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4882 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004883 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004884 public int getLayoutDirection() {
4885 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004886 }
4887
4888 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004889 * Set the layout direction for this view. This will propagate a reset of layout direction
4890 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004891 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004892 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4893 * {@link #LAYOUT_DIRECTION_RTL},
4894 * {@link #LAYOUT_DIRECTION_INHERIT} or
4895 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004896 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004897 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004898 */
4899 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004900 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004901 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004902 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004903 // Setting the flag will also request a layout.
4904 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4905 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004906 }
4907
4908 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004909 * Returns the resolved layout direction for this view.
4910 *
4911 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4912 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004913 */
4914 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4915 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4916 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4917 })
4918 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004919 resolveLayoutDirectionIfNeeded();
4920 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004921 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4922 }
4923
4924 /**
4925 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4926 * layout attribute and/or the inherited value from the parent.</p>
4927 *
4928 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004929 */
4930 @ViewDebug.ExportedProperty(category = "layout")
4931 public boolean isLayoutRtl() {
4932 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4933 }
4934
4935 /**
Adam Powell539ee872012-02-03 19:00:49 -08004936 * Indicates whether the view is currently tracking transient state that the
4937 * app should not need to concern itself with saving and restoring, but that
4938 * the framework should take special note to preserve when possible.
4939 *
4940 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004941 */
4942 @ViewDebug.ExportedProperty(category = "layout")
4943 public boolean hasTransientState() {
4944 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4945 }
4946
4947 /**
4948 * Set whether this view is currently tracking transient state that the
4949 * framework should attempt to preserve when possible.
4950 *
4951 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004952 */
4953 public void setHasTransientState(boolean hasTransientState) {
4954 if (hasTransientState() == hasTransientState) return;
4955
4956 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4957 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4958 if (mParent != null) {
4959 try {
4960 mParent.childHasTransientStateChanged(this, hasTransientState);
4961 } catch (AbstractMethodError e) {
4962 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4963 " does not fully implement ViewParent", e);
4964 }
4965 }
4966 }
4967
4968 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 * If this view doesn't do any drawing on its own, set this flag to
4970 * allow further optimizations. By default, this flag is not set on
4971 * View, but could be set on some View subclasses such as ViewGroup.
4972 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004973 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4974 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 *
4976 * @param willNotDraw whether or not this View draw on its own
4977 */
4978 public void setWillNotDraw(boolean willNotDraw) {
4979 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4980 }
4981
4982 /**
4983 * Returns whether or not this View draws on its own.
4984 *
4985 * @return true if this view has nothing to draw, false otherwise
4986 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004987 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 public boolean willNotDraw() {
4989 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4990 }
4991
4992 /**
4993 * When a View's drawing cache is enabled, drawing is redirected to an
4994 * offscreen bitmap. Some views, like an ImageView, must be able to
4995 * bypass this mechanism if they already draw a single bitmap, to avoid
4996 * unnecessary usage of the memory.
4997 *
4998 * @param willNotCacheDrawing true if this view does not cache its
4999 * drawing, false otherwise
5000 */
5001 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5002 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5003 }
5004
5005 /**
5006 * Returns whether or not this View can cache its drawing or not.
5007 *
5008 * @return true if this view does not cache its drawing, false otherwise
5009 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005010 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 public boolean willNotCacheDrawing() {
5012 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5013 }
5014
5015 /**
5016 * Indicates whether this view reacts to click events or not.
5017 *
5018 * @return true if the view is clickable, false otherwise
5019 *
5020 * @see #setClickable(boolean)
5021 * @attr ref android.R.styleable#View_clickable
5022 */
5023 @ViewDebug.ExportedProperty
5024 public boolean isClickable() {
5025 return (mViewFlags & CLICKABLE) == CLICKABLE;
5026 }
5027
5028 /**
5029 * Enables or disables click events for this view. When a view
5030 * is clickable it will change its state to "pressed" on every click.
5031 * Subclasses should set the view clickable to visually react to
5032 * user's clicks.
5033 *
5034 * @param clickable true to make the view clickable, false otherwise
5035 *
5036 * @see #isClickable()
5037 * @attr ref android.R.styleable#View_clickable
5038 */
5039 public void setClickable(boolean clickable) {
5040 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5041 }
5042
5043 /**
5044 * Indicates whether this view reacts to long click events or not.
5045 *
5046 * @return true if the view is long clickable, false otherwise
5047 *
5048 * @see #setLongClickable(boolean)
5049 * @attr ref android.R.styleable#View_longClickable
5050 */
5051 public boolean isLongClickable() {
5052 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5053 }
5054
5055 /**
5056 * Enables or disables long click events for this view. When a view is long
5057 * clickable it reacts to the user holding down the button for a longer
5058 * duration than a tap. This event can either launch the listener or a
5059 * context menu.
5060 *
5061 * @param longClickable true to make the view long clickable, false otherwise
5062 * @see #isLongClickable()
5063 * @attr ref android.R.styleable#View_longClickable
5064 */
5065 public void setLongClickable(boolean longClickable) {
5066 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5067 }
5068
5069 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005070 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 *
5072 * @see #isClickable()
5073 * @see #setClickable(boolean)
5074 *
5075 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5076 * the View's internal state from a previously set "pressed" state.
5077 */
5078 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005079 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 if (pressed) {
5082 mPrivateFlags |= PRESSED;
5083 } else {
5084 mPrivateFlags &= ~PRESSED;
5085 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005086
5087 if (needsRefresh) {
5088 refreshDrawableState();
5089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 dispatchSetPressed(pressed);
5091 }
5092
5093 /**
5094 * Dispatch setPressed to all of this View's children.
5095 *
5096 * @see #setPressed(boolean)
5097 *
5098 * @param pressed The new pressed state
5099 */
5100 protected void dispatchSetPressed(boolean pressed) {
5101 }
5102
5103 /**
5104 * Indicates whether the view is currently in pressed state. Unless
5105 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5106 * the pressed state.
5107 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005108 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 * @see #isClickable()
5110 * @see #setClickable(boolean)
5111 *
5112 * @return true if the view is currently pressed, false otherwise
5113 */
5114 public boolean isPressed() {
5115 return (mPrivateFlags & PRESSED) == PRESSED;
5116 }
5117
5118 /**
5119 * Indicates whether this view will save its state (that is,
5120 * whether its {@link #onSaveInstanceState} method will be called).
5121 *
5122 * @return Returns true if the view state saving is enabled, else false.
5123 *
5124 * @see #setSaveEnabled(boolean)
5125 * @attr ref android.R.styleable#View_saveEnabled
5126 */
5127 public boolean isSaveEnabled() {
5128 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5129 }
5130
5131 /**
5132 * Controls whether the saving of this view's state is
5133 * enabled (that is, whether its {@link #onSaveInstanceState} method
5134 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005135 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 * for its state to be saved. This flag can only disable the
5137 * saving of this view; any child views may still have their state saved.
5138 *
5139 * @param enabled Set to false to <em>disable</em> state saving, or true
5140 * (the default) to allow it.
5141 *
5142 * @see #isSaveEnabled()
5143 * @see #setId(int)
5144 * @see #onSaveInstanceState()
5145 * @attr ref android.R.styleable#View_saveEnabled
5146 */
5147 public void setSaveEnabled(boolean enabled) {
5148 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5149 }
5150
Jeff Brown85a31762010-09-01 17:01:00 -07005151 /**
5152 * Gets whether the framework should discard touches when the view's
5153 * window is obscured by another visible window.
5154 * Refer to the {@link View} security documentation for more details.
5155 *
5156 * @return True if touch filtering is enabled.
5157 *
5158 * @see #setFilterTouchesWhenObscured(boolean)
5159 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5160 */
5161 @ViewDebug.ExportedProperty
5162 public boolean getFilterTouchesWhenObscured() {
5163 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5164 }
5165
5166 /**
5167 * Sets whether the framework should discard touches when the view's
5168 * window is obscured by another visible window.
5169 * Refer to the {@link View} security documentation for more details.
5170 *
5171 * @param enabled True if touch filtering should be enabled.
5172 *
5173 * @see #getFilterTouchesWhenObscured
5174 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5175 */
5176 public void setFilterTouchesWhenObscured(boolean enabled) {
5177 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5178 FILTER_TOUCHES_WHEN_OBSCURED);
5179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180
5181 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005182 * Indicates whether the entire hierarchy under this view will save its
5183 * state when a state saving traversal occurs from its parent. The default
5184 * is true; if false, these views will not be saved unless
5185 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5186 *
5187 * @return Returns true if the view state saving from parent is enabled, else false.
5188 *
5189 * @see #setSaveFromParentEnabled(boolean)
5190 */
5191 public boolean isSaveFromParentEnabled() {
5192 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5193 }
5194
5195 /**
5196 * Controls whether the entire hierarchy under this view will save its
5197 * state when a state saving traversal occurs from its parent. The default
5198 * is true; if false, these views will not be saved unless
5199 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5200 *
5201 * @param enabled Set to false to <em>disable</em> state saving, or true
5202 * (the default) to allow it.
5203 *
5204 * @see #isSaveFromParentEnabled()
5205 * @see #setId(int)
5206 * @see #onSaveInstanceState()
5207 */
5208 public void setSaveFromParentEnabled(boolean enabled) {
5209 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5210 }
5211
5212
5213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 * Returns whether this View is able to take focus.
5215 *
5216 * @return True if this view can take focus, or false otherwise.
5217 * @attr ref android.R.styleable#View_focusable
5218 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005219 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 public final boolean isFocusable() {
5221 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5222 }
5223
5224 /**
5225 * When a view is focusable, it may not want to take focus when in touch mode.
5226 * For example, a button would like focus when the user is navigating via a D-pad
5227 * so that the user can click on it, but once the user starts touching the screen,
5228 * the button shouldn't take focus
5229 * @return Whether the view is focusable in touch mode.
5230 * @attr ref android.R.styleable#View_focusableInTouchMode
5231 */
5232 @ViewDebug.ExportedProperty
5233 public final boolean isFocusableInTouchMode() {
5234 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5235 }
5236
5237 /**
5238 * Find the nearest view in the specified direction that can take focus.
5239 * This does not actually give focus to that view.
5240 *
5241 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5242 *
5243 * @return The nearest focusable in the specified direction, or null if none
5244 * can be found.
5245 */
5246 public View focusSearch(int direction) {
5247 if (mParent != null) {
5248 return mParent.focusSearch(this, direction);
5249 } else {
5250 return null;
5251 }
5252 }
5253
5254 /**
5255 * This method is the last chance for the focused view and its ancestors to
5256 * respond to an arrow key. This is called when the focused view did not
5257 * consume the key internally, nor could the view system find a new view in
5258 * the requested direction to give focus to.
5259 *
5260 * @param focused The currently focused view.
5261 * @param direction The direction focus wants to move. One of FOCUS_UP,
5262 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5263 * @return True if the this view consumed this unhandled move.
5264 */
5265 public boolean dispatchUnhandledMove(View focused, int direction) {
5266 return false;
5267 }
5268
5269 /**
5270 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005271 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005273 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5274 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 * @return The user specified next view, or null if there is none.
5276 */
5277 View findUserSetNextFocus(View root, int direction) {
5278 switch (direction) {
5279 case FOCUS_LEFT:
5280 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005281 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 case FOCUS_RIGHT:
5283 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005284 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 case FOCUS_UP:
5286 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005287 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 case FOCUS_DOWN:
5289 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005290 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005291 case FOCUS_FORWARD:
5292 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005293 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005294 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005295 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005296 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005297 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005298 @Override
5299 public boolean apply(View t) {
5300 return t.mNextFocusForwardId == id;
5301 }
5302 });
5303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 }
5305 return null;
5306 }
5307
Jeff Brown4dfbec22011-08-15 14:55:37 -07005308 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5309 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5310 @Override
5311 public boolean apply(View t) {
5312 return t.mID == childViewId;
5313 }
5314 });
5315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 if (result == null) {
5317 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5318 + "by user for id " + childViewId);
5319 }
5320 return result;
5321 }
5322
5323 /**
5324 * Find and return all focusable views that are descendants of this view,
5325 * possibly including this view if it is focusable itself.
5326 *
5327 * @param direction The direction of the focus
5328 * @return A list of focusable views
5329 */
5330 public ArrayList<View> getFocusables(int direction) {
5331 ArrayList<View> result = new ArrayList<View>(24);
5332 addFocusables(result, direction);
5333 return result;
5334 }
5335
5336 /**
5337 * Add any focusable views that are descendants of this view (possibly
5338 * including this view if it is focusable itself) to views. If we are in touch mode,
5339 * only add views that are also focusable in touch mode.
5340 *
5341 * @param views Focusable views found so far
5342 * @param direction The direction of the focus
5343 */
5344 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005345 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347
svetoslavganov75986cf2009-05-14 22:28:01 -07005348 /**
5349 * Adds any focusable views that are descendants of this view (possibly
5350 * including this view if it is focusable itself) to views. This method
5351 * adds all focusable views regardless if we are in touch mode or
5352 * only views focusable in touch mode if we are in touch mode depending on
5353 * the focusable mode paramater.
5354 *
5355 * @param views Focusable views found so far or null if all we are interested is
5356 * the number of focusables.
5357 * @param direction The direction of the focus.
5358 * @param focusableMode The type of focusables to be added.
5359 *
5360 * @see #FOCUSABLES_ALL
5361 * @see #FOCUSABLES_TOUCH_MODE
5362 */
5363 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5364 if (!isFocusable()) {
5365 return;
5366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367
svetoslavganov75986cf2009-05-14 22:28:01 -07005368 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5369 isInTouchMode() && !isFocusableInTouchMode()) {
5370 return;
5371 }
5372
5373 if (views != null) {
5374 views.add(this);
5375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 }
5377
5378 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005379 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005380 * The search is performed by either the text that the View renders or the content
5381 * description that describes the view for accessibility purposes and the view does
5382 * not render or both. Clients can specify how the search is to be performed via
5383 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5384 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005385 *
5386 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005387 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005388 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005389 * @see #FIND_VIEWS_WITH_TEXT
5390 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5391 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005392 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005393 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005394 if (getAccessibilityNodeProvider() != null) {
5395 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5396 outViews.add(this);
5397 }
5398 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5399 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005400 String searchedLowerCase = searched.toString().toLowerCase();
5401 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5402 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5403 outViews.add(this);
5404 }
5405 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005406 }
5407
5408 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 * Find and return all touchable views that are descendants of this view,
5410 * possibly including this view if it is touchable itself.
5411 *
5412 * @return A list of touchable views
5413 */
5414 public ArrayList<View> getTouchables() {
5415 ArrayList<View> result = new ArrayList<View>();
5416 addTouchables(result);
5417 return result;
5418 }
5419
5420 /**
5421 * Add any touchable views that are descendants of this view (possibly
5422 * including this view if it is touchable itself) to views.
5423 *
5424 * @param views Touchable views found so far
5425 */
5426 public void addTouchables(ArrayList<View> views) {
5427 final int viewFlags = mViewFlags;
5428
5429 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5430 && (viewFlags & ENABLED_MASK) == ENABLED) {
5431 views.add(this);
5432 }
5433 }
5434
5435 /**
5436 * Call this to try to give focus to a specific view or to one of its
5437 * descendants.
5438 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005439 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5440 * false), or if it is focusable and it is not focusable in touch mode
5441 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005443 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 * have focus, and you want your parent to look for the next one.
5445 *
5446 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5447 * {@link #FOCUS_DOWN} and <code>null</code>.
5448 *
5449 * @return Whether this view or one of its descendants actually took focus.
5450 */
5451 public final boolean requestFocus() {
5452 return requestFocus(View.FOCUS_DOWN);
5453 }
5454
5455
5456 /**
5457 * Call this to try to give focus to a specific view or to one of its
5458 * descendants and give it a hint about what direction focus is heading.
5459 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005460 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5461 * false), or if it is focusable and it is not focusable in touch mode
5462 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005464 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 * have focus, and you want your parent to look for the next one.
5466 *
5467 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5468 * <code>null</code> set for the previously focused rectangle.
5469 *
5470 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5471 * @return Whether this view or one of its descendants actually took focus.
5472 */
5473 public final boolean requestFocus(int direction) {
5474 return requestFocus(direction, null);
5475 }
5476
5477 /**
5478 * Call this to try to give focus to a specific view or to one of its descendants
5479 * and give it hints about the direction and a specific rectangle that the focus
5480 * is coming from. The rectangle can help give larger views a finer grained hint
5481 * about where focus is coming from, and therefore, where to show selection, or
5482 * forward focus change internally.
5483 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005484 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5485 * false), or if it is focusable and it is not focusable in touch mode
5486 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 *
5488 * A View will not take focus if it is not visible.
5489 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005490 * A View will not take focus if one of its parents has
5491 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5492 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005494 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 * have focus, and you want your parent to look for the next one.
5496 *
5497 * You may wish to override this method if your custom {@link View} has an internal
5498 * {@link View} that it wishes to forward the request to.
5499 *
5500 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5501 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5502 * to give a finer grained hint about where focus is coming from. May be null
5503 * if there is no hint.
5504 * @return Whether this view or one of its descendants actually took focus.
5505 */
5506 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5507 // need to be focusable
5508 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5509 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5510 return false;
5511 }
5512
5513 // need to be focusable in touch mode if in touch mode
5514 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005515 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5516 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 }
5518
5519 // need to not have any parents blocking us
5520 if (hasAncestorThatBlocksDescendantFocus()) {
5521 return false;
5522 }
5523
5524 handleFocusGainInternal(direction, previouslyFocusedRect);
5525 return true;
5526 }
5527
5528 /**
5529 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5530 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5531 * touch mode to request focus when they are touched.
5532 *
5533 * @return Whether this view or one of its descendants actually took focus.
5534 *
5535 * @see #isInTouchMode()
5536 *
5537 */
5538 public final boolean requestFocusFromTouch() {
5539 // Leave touch mode if we need to
5540 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005541 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005542 if (viewRoot != null) {
5543 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 }
5545 }
5546 return requestFocus(View.FOCUS_DOWN);
5547 }
5548
5549 /**
5550 * @return Whether any ancestor of this view blocks descendant focus.
5551 */
5552 private boolean hasAncestorThatBlocksDescendantFocus() {
5553 ViewParent ancestor = mParent;
5554 while (ancestor instanceof ViewGroup) {
5555 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5556 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5557 return true;
5558 } else {
5559 ancestor = vgAncestor.getParent();
5560 }
5561 }
5562 return false;
5563 }
5564
5565 /**
Romain Guya440b002010-02-24 15:57:54 -08005566 * @hide
5567 */
5568 public void dispatchStartTemporaryDetach() {
5569 onStartTemporaryDetach();
5570 }
5571
5572 /**
5573 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5575 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005576 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 */
5578 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005579 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005580 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005581 }
5582
5583 /**
5584 * @hide
5585 */
5586 public void dispatchFinishTemporaryDetach() {
5587 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 }
Romain Guy8506ab42009-06-11 17:35:47 -07005589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 /**
5591 * Called after {@link #onStartTemporaryDetach} when the container is done
5592 * changing the view.
5593 */
5594 public void onFinishTemporaryDetach() {
5595 }
Romain Guy8506ab42009-06-11 17:35:47 -07005596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005598 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5599 * for this view's window. Returns null if the view is not currently attached
5600 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005601 * just use the standard high-level event callbacks like
5602 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005603 */
5604 public KeyEvent.DispatcherState getKeyDispatcherState() {
5605 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5606 }
Joe Malin32736f02011-01-19 16:14:20 -08005607
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 * Dispatch a key event before it is processed by any input method
5610 * associated with the view hierarchy. This can be used to intercept
5611 * key events in special situations before the IME consumes them; a
5612 * typical example would be handling the BACK key to update the application's
5613 * UI instead of allowing the IME to see it and close itself.
5614 *
5615 * @param event The key event to be dispatched.
5616 * @return True if the event was handled, false otherwise.
5617 */
5618 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5619 return onKeyPreIme(event.getKeyCode(), event);
5620 }
5621
5622 /**
5623 * Dispatch a key event to the next view on the focus path. This path runs
5624 * from the top of the view tree down to the currently focused view. If this
5625 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5626 * the next node down the focus path. This method also fires any key
5627 * listeners.
5628 *
5629 * @param event The key event to be dispatched.
5630 * @return True if the event was handled, false otherwise.
5631 */
5632 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005633 if (mInputEventConsistencyVerifier != null) {
5634 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636
Jeff Brown21bc5c92011-02-28 18:27:14 -08005637 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005638 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005639 ListenerInfo li = mListenerInfo;
5640 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5641 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 return true;
5643 }
5644
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005645 if (event.dispatch(this, mAttachInfo != null
5646 ? mAttachInfo.mKeyDispatchState : null, this)) {
5647 return true;
5648 }
5649
5650 if (mInputEventConsistencyVerifier != null) {
5651 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5652 }
5653 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 }
5655
5656 /**
5657 * Dispatches a key shortcut event.
5658 *
5659 * @param event The key event to be dispatched.
5660 * @return True if the event was handled by the view, false otherwise.
5661 */
5662 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5663 return onKeyShortcut(event.getKeyCode(), event);
5664 }
5665
5666 /**
5667 * Pass the touch screen motion event down to the target view, or this
5668 * view if it is the target.
5669 *
5670 * @param event The motion event to be dispatched.
5671 * @return True if the event was handled by the view, false otherwise.
5672 */
5673 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005674 if (mInputEventConsistencyVerifier != null) {
5675 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5676 }
5677
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005678 if (onFilterTouchEventForSecurity(event)) {
5679 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005680 ListenerInfo li = mListenerInfo;
5681 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5682 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005683 return true;
5684 }
5685
5686 if (onTouchEvent(event)) {
5687 return true;
5688 }
Jeff Brown85a31762010-09-01 17:01:00 -07005689 }
5690
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005691 if (mInputEventConsistencyVerifier != null) {
5692 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005694 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 }
5696
5697 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005698 * Filter the touch event to apply security policies.
5699 *
5700 * @param event The motion event to be filtered.
5701 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005702 *
Jeff Brown85a31762010-09-01 17:01:00 -07005703 * @see #getFilterTouchesWhenObscured
5704 */
5705 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005706 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005707 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5708 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5709 // Window is obscured, drop this touch.
5710 return false;
5711 }
5712 return true;
5713 }
5714
5715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 * Pass a trackball motion event down to the focused view.
5717 *
5718 * @param event The motion event to be dispatched.
5719 * @return True if the event was handled by the view, false otherwise.
5720 */
5721 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005722 if (mInputEventConsistencyVerifier != null) {
5723 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5724 }
5725
Romain Guy02ccac62011-06-24 13:20:23 -07005726 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 }
5728
5729 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005730 * Dispatch a generic motion event.
5731 * <p>
5732 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5733 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005734 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005735 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005736 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005737 *
5738 * @param event The motion event to be dispatched.
5739 * @return True if the event was handled by the view, false otherwise.
5740 */
5741 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005742 if (mInputEventConsistencyVerifier != null) {
5743 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5744 }
5745
Jeff Browna032cc02011-03-07 16:56:21 -08005746 final int source = event.getSource();
5747 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5748 final int action = event.getAction();
5749 if (action == MotionEvent.ACTION_HOVER_ENTER
5750 || action == MotionEvent.ACTION_HOVER_MOVE
5751 || action == MotionEvent.ACTION_HOVER_EXIT) {
5752 if (dispatchHoverEvent(event)) {
5753 return true;
5754 }
5755 } else if (dispatchGenericPointerEvent(event)) {
5756 return true;
5757 }
5758 } else if (dispatchGenericFocusedEvent(event)) {
5759 return true;
5760 }
5761
Jeff Brown10b62902011-06-20 16:40:37 -07005762 if (dispatchGenericMotionEventInternal(event)) {
5763 return true;
5764 }
5765
5766 if (mInputEventConsistencyVerifier != null) {
5767 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5768 }
5769 return false;
5770 }
5771
5772 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005773 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005774 ListenerInfo li = mListenerInfo;
5775 if (li != null && li.mOnGenericMotionListener != null
5776 && (mViewFlags & ENABLED_MASK) == ENABLED
5777 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005778 return true;
5779 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005780
5781 if (onGenericMotionEvent(event)) {
5782 return true;
5783 }
5784
5785 if (mInputEventConsistencyVerifier != null) {
5786 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5787 }
5788 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005789 }
5790
5791 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005792 * Dispatch a hover event.
5793 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005794 * Do not call this method directly.
5795 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005796 * </p>
5797 *
5798 * @param event The motion event to be dispatched.
5799 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005800 */
5801 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005802 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005803 ListenerInfo li = mListenerInfo;
5804 if (li != null && li.mOnHoverListener != null
5805 && (mViewFlags & ENABLED_MASK) == ENABLED
5806 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005807 return true;
5808 }
5809
Jeff Browna032cc02011-03-07 16:56:21 -08005810 return onHoverEvent(event);
5811 }
5812
5813 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005814 * Returns true if the view has a child to which it has recently sent
5815 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5816 * it does not have a hovered child, then it must be the innermost hovered view.
5817 * @hide
5818 */
5819 protected boolean hasHoveredChild() {
5820 return false;
5821 }
5822
5823 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005824 * Dispatch a generic motion event to the view under the first pointer.
5825 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005826 * Do not call this method directly.
5827 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005828 * </p>
5829 *
5830 * @param event The motion event to be dispatched.
5831 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005832 */
5833 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5834 return false;
5835 }
5836
5837 /**
5838 * Dispatch a generic motion event to the currently focused view.
5839 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005840 * Do not call this method directly.
5841 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005842 * </p>
5843 *
5844 * @param event The motion event to be dispatched.
5845 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005846 */
5847 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5848 return false;
5849 }
5850
5851 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005852 * Dispatch a pointer event.
5853 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005854 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5855 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5856 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005857 * and should not be expected to handle other pointing device features.
5858 * </p>
5859 *
5860 * @param event The motion event to be dispatched.
5861 * @return True if the event was handled by the view, false otherwise.
5862 * @hide
5863 */
5864 public final boolean dispatchPointerEvent(MotionEvent event) {
5865 if (event.isTouchEvent()) {
5866 return dispatchTouchEvent(event);
5867 } else {
5868 return dispatchGenericMotionEvent(event);
5869 }
5870 }
5871
5872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 * Called when the window containing this view gains or loses window focus.
5874 * ViewGroups should override to route to their children.
5875 *
5876 * @param hasFocus True if the window containing this view now has focus,
5877 * false otherwise.
5878 */
5879 public void dispatchWindowFocusChanged(boolean hasFocus) {
5880 onWindowFocusChanged(hasFocus);
5881 }
5882
5883 /**
5884 * Called when the window containing this view gains or loses focus. Note
5885 * that this is separate from view focus: to receive key events, both
5886 * your view and its window must have focus. If a window is displayed
5887 * on top of yours that takes input focus, then your own window will lose
5888 * focus but the view focus will remain unchanged.
5889 *
5890 * @param hasWindowFocus True if the window containing this view now has
5891 * focus, false otherwise.
5892 */
5893 public void onWindowFocusChanged(boolean hasWindowFocus) {
5894 InputMethodManager imm = InputMethodManager.peekInstance();
5895 if (!hasWindowFocus) {
5896 if (isPressed()) {
5897 setPressed(false);
5898 }
5899 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5900 imm.focusOut(this);
5901 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005902 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005903 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005904 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5906 imm.focusIn(this);
5907 }
5908 refreshDrawableState();
5909 }
5910
5911 /**
5912 * Returns true if this view is in a window that currently has window focus.
5913 * Note that this is not the same as the view itself having focus.
5914 *
5915 * @return True if this view is in a window that currently has window focus.
5916 */
5917 public boolean hasWindowFocus() {
5918 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5919 }
5920
5921 /**
Adam Powell326d8082009-12-09 15:10:07 -08005922 * Dispatch a view visibility change down the view hierarchy.
5923 * ViewGroups should override to route to their children.
5924 * @param changedView The view whose visibility changed. Could be 'this' or
5925 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005926 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5927 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005928 */
5929 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5930 onVisibilityChanged(changedView, visibility);
5931 }
5932
5933 /**
5934 * Called when the visibility of the view or an ancestor of the view is changed.
5935 * @param changedView The view whose visibility changed. Could be 'this' or
5936 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005937 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5938 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005939 */
5940 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005941 if (visibility == VISIBLE) {
5942 if (mAttachInfo != null) {
5943 initialAwakenScrollBars();
5944 } else {
5945 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5946 }
5947 }
Adam Powell326d8082009-12-09 15:10:07 -08005948 }
5949
5950 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005951 * Dispatch a hint about whether this view is displayed. For instance, when
5952 * a View moves out of the screen, it might receives a display hint indicating
5953 * the view is not displayed. Applications should not <em>rely</em> on this hint
5954 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005955 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005956 * @param hint A hint about whether or not this view is displayed:
5957 * {@link #VISIBLE} or {@link #INVISIBLE}.
5958 */
5959 public void dispatchDisplayHint(int hint) {
5960 onDisplayHint(hint);
5961 }
5962
5963 /**
5964 * Gives this view a hint about whether is displayed or not. For instance, when
5965 * a View moves out of the screen, it might receives a display hint indicating
5966 * the view is not displayed. Applications should not <em>rely</em> on this hint
5967 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005968 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005969 * @param hint A hint about whether or not this view is displayed:
5970 * {@link #VISIBLE} or {@link #INVISIBLE}.
5971 */
5972 protected void onDisplayHint(int hint) {
5973 }
5974
5975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 * Dispatch a window visibility change down the view hierarchy.
5977 * ViewGroups should override to route to their children.
5978 *
5979 * @param visibility The new visibility of the window.
5980 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005981 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 */
5983 public void dispatchWindowVisibilityChanged(int visibility) {
5984 onWindowVisibilityChanged(visibility);
5985 }
5986
5987 /**
5988 * Called when the window containing has change its visibility
5989 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5990 * that this tells you whether or not your window is being made visible
5991 * to the window manager; this does <em>not</em> tell you whether or not
5992 * your window is obscured by other windows on the screen, even if it
5993 * is itself visible.
5994 *
5995 * @param visibility The new visibility of the window.
5996 */
5997 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005998 if (visibility == VISIBLE) {
5999 initialAwakenScrollBars();
6000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 }
6002
6003 /**
6004 * Returns the current visibility of the window this view is attached to
6005 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6006 *
6007 * @return Returns the current visibility of the view's window.
6008 */
6009 public int getWindowVisibility() {
6010 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6011 }
6012
6013 /**
6014 * Retrieve the overall visible display size in which the window this view is
6015 * attached to has been positioned in. This takes into account screen
6016 * decorations above the window, for both cases where the window itself
6017 * is being position inside of them or the window is being placed under
6018 * then and covered insets are used for the window to position its content
6019 * inside. In effect, this tells you the available area where content can
6020 * be placed and remain visible to users.
6021 *
6022 * <p>This function requires an IPC back to the window manager to retrieve
6023 * the requested information, so should not be used in performance critical
6024 * code like drawing.
6025 *
6026 * @param outRect Filled in with the visible display frame. If the view
6027 * is not attached to a window, this is simply the raw display size.
6028 */
6029 public void getWindowVisibleDisplayFrame(Rect outRect) {
6030 if (mAttachInfo != null) {
6031 try {
6032 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6033 } catch (RemoteException e) {
6034 return;
6035 }
6036 // XXX This is really broken, and probably all needs to be done
6037 // in the window manager, and we need to know more about whether
6038 // we want the area behind or in front of the IME.
6039 final Rect insets = mAttachInfo.mVisibleInsets;
6040 outRect.left += insets.left;
6041 outRect.top += insets.top;
6042 outRect.right -= insets.right;
6043 outRect.bottom -= insets.bottom;
6044 return;
6045 }
6046 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006047 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 }
6049
6050 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006051 * Dispatch a notification about a resource configuration change down
6052 * the view hierarchy.
6053 * ViewGroups should override to route to their children.
6054 *
6055 * @param newConfig The new resource configuration.
6056 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006057 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006058 */
6059 public void dispatchConfigurationChanged(Configuration newConfig) {
6060 onConfigurationChanged(newConfig);
6061 }
6062
6063 /**
6064 * Called when the current configuration of the resources being used
6065 * by the application have changed. You can use this to decide when
6066 * to reload resources that can changed based on orientation and other
6067 * configuration characterstics. You only need to use this if you are
6068 * not relying on the normal {@link android.app.Activity} mechanism of
6069 * recreating the activity instance upon a configuration change.
6070 *
6071 * @param newConfig The new resource configuration.
6072 */
6073 protected void onConfigurationChanged(Configuration newConfig) {
6074 }
6075
6076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 * Private function to aggregate all per-view attributes in to the view
6078 * root.
6079 */
6080 void dispatchCollectViewAttributes(int visibility) {
6081 performCollectViewAttributes(visibility);
6082 }
6083
6084 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006085 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006086 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6087 mAttachInfo.mKeepScreenOn = true;
6088 }
6089 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006090 ListenerInfo li = mListenerInfo;
6091 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006092 mAttachInfo.mHasSystemUiListeners = true;
6093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 }
6095 }
6096
6097 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006098 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006100 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6101 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 ai.mRecomputeGlobalAttributes = true;
6103 }
6104 }
6105 }
6106
6107 /**
6108 * Returns whether the device is currently in touch mode. Touch mode is entered
6109 * once the user begins interacting with the device by touch, and affects various
6110 * things like whether focus is always visible to the user.
6111 *
6112 * @return Whether the device is in touch mode.
6113 */
6114 @ViewDebug.ExportedProperty
6115 public boolean isInTouchMode() {
6116 if (mAttachInfo != null) {
6117 return mAttachInfo.mInTouchMode;
6118 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006119 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 }
6121 }
6122
6123 /**
6124 * Returns the context the view is running in, through which it can
6125 * access the current theme, resources, etc.
6126 *
6127 * @return The view's Context.
6128 */
6129 @ViewDebug.CapturedViewProperty
6130 public final Context getContext() {
6131 return mContext;
6132 }
6133
6134 /**
6135 * Handle a key event before it is processed by any input method
6136 * associated with the view hierarchy. This can be used to intercept
6137 * key events in special situations before the IME consumes them; a
6138 * typical example would be handling the BACK key to update the application's
6139 * UI instead of allowing the IME to see it and close itself.
6140 *
6141 * @param keyCode The value in event.getKeyCode().
6142 * @param event Description of the key event.
6143 * @return If you handled the event, return true. If you want to allow the
6144 * event to be handled by the next receiver, return false.
6145 */
6146 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6147 return false;
6148 }
6149
6150 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006151 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6152 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6154 * is released, if the view is enabled and clickable.
6155 *
6156 * @param keyCode A key code that represents the button pressed, from
6157 * {@link android.view.KeyEvent}.
6158 * @param event The KeyEvent object that defines the button action.
6159 */
6160 public boolean onKeyDown(int keyCode, KeyEvent event) {
6161 boolean result = false;
6162
6163 switch (keyCode) {
6164 case KeyEvent.KEYCODE_DPAD_CENTER:
6165 case KeyEvent.KEYCODE_ENTER: {
6166 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6167 return true;
6168 }
6169 // Long clickable items don't necessarily have to be clickable
6170 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6171 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6172 (event.getRepeatCount() == 0)) {
6173 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006174 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 return true;
6176 }
6177 break;
6178 }
6179 }
6180 return result;
6181 }
6182
6183 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006184 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6185 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6186 * the event).
6187 */
6188 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6189 return false;
6190 }
6191
6192 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006193 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6194 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6196 * {@link KeyEvent#KEYCODE_ENTER} is released.
6197 *
6198 * @param keyCode A key code that represents the button pressed, from
6199 * {@link android.view.KeyEvent}.
6200 * @param event The KeyEvent object that defines the button action.
6201 */
6202 public boolean onKeyUp(int keyCode, KeyEvent event) {
6203 boolean result = false;
6204
6205 switch (keyCode) {
6206 case KeyEvent.KEYCODE_DPAD_CENTER:
6207 case KeyEvent.KEYCODE_ENTER: {
6208 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6209 return true;
6210 }
6211 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6212 setPressed(false);
6213
6214 if (!mHasPerformedLongPress) {
6215 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006216 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217
6218 result = performClick();
6219 }
6220 }
6221 break;
6222 }
6223 }
6224 return result;
6225 }
6226
6227 /**
6228 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6229 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6230 * the event).
6231 *
6232 * @param keyCode A key code that represents the button pressed, from
6233 * {@link android.view.KeyEvent}.
6234 * @param repeatCount The number of times the action was made.
6235 * @param event The KeyEvent object that defines the button action.
6236 */
6237 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6238 return false;
6239 }
6240
6241 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006242 * Called on the focused view when a key shortcut event is not handled.
6243 * Override this method to implement local key shortcuts for the View.
6244 * Key shortcuts can also be implemented by setting the
6245 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006246 *
6247 * @param keyCode The value in event.getKeyCode().
6248 * @param event Description of the key event.
6249 * @return If you handled the event, return true. If you want to allow the
6250 * event to be handled by the next receiver, return false.
6251 */
6252 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6253 return false;
6254 }
6255
6256 /**
6257 * Check whether the called view is a text editor, in which case it
6258 * would make sense to automatically display a soft input window for
6259 * it. Subclasses should override this if they implement
6260 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006261 * a call on that method would return a non-null InputConnection, and
6262 * they are really a first-class editor that the user would normally
6263 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006264 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006265 * <p>The default implementation always returns false. This does
6266 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6267 * will not be called or the user can not otherwise perform edits on your
6268 * view; it is just a hint to the system that this is not the primary
6269 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 * @return Returns true if this view is a text editor, else false.
6272 */
6273 public boolean onCheckIsTextEditor() {
6274 return false;
6275 }
Romain Guy8506ab42009-06-11 17:35:47 -07006276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006277 /**
6278 * Create a new InputConnection for an InputMethod to interact
6279 * with the view. The default implementation returns null, since it doesn't
6280 * support input methods. You can override this to implement such support.
6281 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 * <p>When implementing this, you probably also want to implement
6284 * {@link #onCheckIsTextEditor()} to indicate you will return a
6285 * non-null InputConnection.
6286 *
6287 * @param outAttrs Fill in with attribute information about the connection.
6288 */
6289 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6290 return null;
6291 }
6292
6293 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006294 * Called by the {@link android.view.inputmethod.InputMethodManager}
6295 * when a view who is not the current
6296 * input connection target is trying to make a call on the manager. The
6297 * default implementation returns false; you can override this to return
6298 * true for certain views if you are performing InputConnection proxying
6299 * to them.
6300 * @param view The View that is making the InputMethodManager call.
6301 * @return Return true to allow the call, false to reject.
6302 */
6303 public boolean checkInputConnectionProxy(View view) {
6304 return false;
6305 }
Romain Guy8506ab42009-06-11 17:35:47 -07006306
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 * Show the context menu for this view. It is not safe to hold on to the
6309 * menu after returning from this method.
6310 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006311 * You should normally not overload this method. Overload
6312 * {@link #onCreateContextMenu(ContextMenu)} or define an
6313 * {@link OnCreateContextMenuListener} to add items to the context menu.
6314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 * @param menu The context menu to populate
6316 */
6317 public void createContextMenu(ContextMenu menu) {
6318 ContextMenuInfo menuInfo = getContextMenuInfo();
6319
6320 // Sets the current menu info so all items added to menu will have
6321 // my extra info set.
6322 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6323
6324 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006325 ListenerInfo li = mListenerInfo;
6326 if (li != null && li.mOnCreateContextMenuListener != null) {
6327 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 }
6329
6330 // Clear the extra information so subsequent items that aren't mine don't
6331 // have my extra info.
6332 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6333
6334 if (mParent != null) {
6335 mParent.createContextMenu(menu);
6336 }
6337 }
6338
6339 /**
6340 * Views should implement this if they have extra information to associate
6341 * with the context menu. The return result is supplied as a parameter to
6342 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6343 * callback.
6344 *
6345 * @return Extra information about the item for which the context menu
6346 * should be shown. This information will vary across different
6347 * subclasses of View.
6348 */
6349 protected ContextMenuInfo getContextMenuInfo() {
6350 return null;
6351 }
6352
6353 /**
6354 * Views should implement this if the view itself is going to add items to
6355 * the context menu.
6356 *
6357 * @param menu the context menu to populate
6358 */
6359 protected void onCreateContextMenu(ContextMenu menu) {
6360 }
6361
6362 /**
6363 * Implement this method to handle trackball motion events. The
6364 * <em>relative</em> movement of the trackball since the last event
6365 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6366 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6367 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6368 * they will often be fractional values, representing the more fine-grained
6369 * movement information available from a trackball).
6370 *
6371 * @param event The motion event.
6372 * @return True if the event was handled, false otherwise.
6373 */
6374 public boolean onTrackballEvent(MotionEvent event) {
6375 return false;
6376 }
6377
6378 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006379 * Implement this method to handle generic motion events.
6380 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006381 * Generic motion events describe joystick movements, mouse hovers, track pad
6382 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006383 * {@link MotionEvent#getSource() source} of the motion event specifies
6384 * the class of input that was received. Implementations of this method
6385 * must examine the bits in the source before processing the event.
6386 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006387 * </p><p>
6388 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6389 * are delivered to the view under the pointer. All other generic motion events are
6390 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006391 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006392 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006393 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006394 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6395 * // process the joystick movement...
6396 * return true;
6397 * }
6398 * }
6399 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6400 * switch (event.getAction()) {
6401 * case MotionEvent.ACTION_HOVER_MOVE:
6402 * // process the mouse hover movement...
6403 * return true;
6404 * case MotionEvent.ACTION_SCROLL:
6405 * // process the scroll wheel movement...
6406 * return true;
6407 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006408 * }
6409 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006410 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006411 *
6412 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006413 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006414 */
6415 public boolean onGenericMotionEvent(MotionEvent event) {
6416 return false;
6417 }
6418
6419 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006420 * Implement this method to handle hover events.
6421 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006422 * This method is called whenever a pointer is hovering into, over, or out of the
6423 * bounds of a view and the view is not currently being touched.
6424 * Hover events are represented as pointer events with action
6425 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6426 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6427 * </p>
6428 * <ul>
6429 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6430 * when the pointer enters the bounds of the view.</li>
6431 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6432 * when the pointer has already entered the bounds of the view and has moved.</li>
6433 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6434 * when the pointer has exited the bounds of the view or when the pointer is
6435 * about to go down due to a button click, tap, or similar user action that
6436 * causes the view to be touched.</li>
6437 * </ul>
6438 * <p>
6439 * The view should implement this method to return true to indicate that it is
6440 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006441 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006442 * The default implementation calls {@link #setHovered} to update the hovered state
6443 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006444 * is enabled and is clickable. The default implementation also sends hover
6445 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006446 * </p>
6447 *
6448 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006449 * @return True if the view handled the hover event.
6450 *
6451 * @see #isHovered
6452 * @see #setHovered
6453 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006454 */
6455 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006456 // The root view may receive hover (or touch) events that are outside the bounds of
6457 // the window. This code ensures that we only send accessibility events for
6458 // hovers that are actually within the bounds of the root view.
6459 final int action = event.getAction();
6460 if (!mSendingHoverAccessibilityEvents) {
6461 if ((action == MotionEvent.ACTION_HOVER_ENTER
6462 || action == MotionEvent.ACTION_HOVER_MOVE)
6463 && !hasHoveredChild()
6464 && pointInView(event.getX(), event.getY())) {
6465 mSendingHoverAccessibilityEvents = true;
6466 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6467 }
6468 } else {
6469 if (action == MotionEvent.ACTION_HOVER_EXIT
6470 || (action == MotionEvent.ACTION_HOVER_MOVE
6471 && !pointInView(event.getX(), event.getY()))) {
6472 mSendingHoverAccessibilityEvents = false;
6473 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6474 }
Jeff Browna1b24182011-07-28 13:38:24 -07006475 }
6476
Jeff Brown87b7f802011-06-21 18:35:45 -07006477 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006478 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006479 case MotionEvent.ACTION_HOVER_ENTER:
6480 setHovered(true);
6481 break;
6482 case MotionEvent.ACTION_HOVER_EXIT:
6483 setHovered(false);
6484 break;
6485 }
Jeff Browna1b24182011-07-28 13:38:24 -07006486
6487 // Dispatch the event to onGenericMotionEvent before returning true.
6488 // This is to provide compatibility with existing applications that
6489 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6490 // break because of the new default handling for hoverable views
6491 // in onHoverEvent.
6492 // Note that onGenericMotionEvent will be called by default when
6493 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6494 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006495 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006496 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006497 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006498 }
6499
6500 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006501 * Returns true if the view should handle {@link #onHoverEvent}
6502 * by calling {@link #setHovered} to change its hovered state.
6503 *
6504 * @return True if the view is hoverable.
6505 */
6506 private boolean isHoverable() {
6507 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006508 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006509 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6510 return false;
6511 }
6512
6513 return (viewFlags & CLICKABLE) == CLICKABLE
6514 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6515 }
6516
6517 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006518 * Returns true if the view is currently hovered.
6519 *
6520 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006521 *
6522 * @see #setHovered
6523 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006524 */
Jeff Brown10b62902011-06-20 16:40:37 -07006525 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006526 public boolean isHovered() {
6527 return (mPrivateFlags & HOVERED) != 0;
6528 }
6529
6530 /**
6531 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006532 * <p>
6533 * Calling this method also changes the drawable state of the view. This
6534 * enables the view to react to hover by using different drawable resources
6535 * to change its appearance.
6536 * </p><p>
6537 * The {@link #onHoverChanged} method is called when the hovered state changes.
6538 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006539 *
6540 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006541 *
6542 * @see #isHovered
6543 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006544 */
6545 public void setHovered(boolean hovered) {
6546 if (hovered) {
6547 if ((mPrivateFlags & HOVERED) == 0) {
6548 mPrivateFlags |= HOVERED;
6549 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006550 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006551 }
6552 } else {
6553 if ((mPrivateFlags & HOVERED) != 0) {
6554 mPrivateFlags &= ~HOVERED;
6555 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006556 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006557 }
6558 }
6559 }
6560
6561 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006562 * Implement this method to handle hover state changes.
6563 * <p>
6564 * This method is called whenever the hover state changes as a result of a
6565 * call to {@link #setHovered}.
6566 * </p>
6567 *
6568 * @param hovered The current hover state, as returned by {@link #isHovered}.
6569 *
6570 * @see #isHovered
6571 * @see #setHovered
6572 */
6573 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006574 }
6575
6576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 * Implement this method to handle touch screen motion events.
6578 *
6579 * @param event The motion event.
6580 * @return True if the event was handled, false otherwise.
6581 */
6582 public boolean onTouchEvent(MotionEvent event) {
6583 final int viewFlags = mViewFlags;
6584
6585 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006586 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006587 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 // A disabled view that is clickable still consumes the touch
6590 // events, it just doesn't respond to them.
6591 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6592 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6593 }
6594
6595 if (mTouchDelegate != null) {
6596 if (mTouchDelegate.onTouchEvent(event)) {
6597 return true;
6598 }
6599 }
6600
6601 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6602 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6603 switch (event.getAction()) {
6604 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006605 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6606 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 // take focus if we don't have it already and we should in
6608 // touch mode.
6609 boolean focusTaken = false;
6610 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6611 focusTaken = requestFocus();
6612 }
6613
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006614 if (prepressed) {
6615 // The button is being released before we actually
6616 // showed it as pressed. Make it show the pressed
6617 // state now (before scheduling the click) to ensure
6618 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006619 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006620 }
Joe Malin32736f02011-01-19 16:14:20 -08006621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 if (!mHasPerformedLongPress) {
6623 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006624 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625
6626 // Only perform take click actions if we were in the pressed state
6627 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006628 // Use a Runnable and post this rather than calling
6629 // performClick directly. This lets other visual state
6630 // of the view update before click actions start.
6631 if (mPerformClick == null) {
6632 mPerformClick = new PerformClick();
6633 }
6634 if (!post(mPerformClick)) {
6635 performClick();
6636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 }
6638 }
6639
6640 if (mUnsetPressedState == null) {
6641 mUnsetPressedState = new UnsetPressedState();
6642 }
6643
Adam Powelle14579b2009-12-16 18:39:52 -08006644 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006645 postDelayed(mUnsetPressedState,
6646 ViewConfiguration.getPressedStateDuration());
6647 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 // If the post failed, unpress right now
6649 mUnsetPressedState.run();
6650 }
Adam Powelle14579b2009-12-16 18:39:52 -08006651 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
6653 break;
6654
6655 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006656 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006657
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006658 if (performButtonActionOnTouchDown(event)) {
6659 break;
6660 }
6661
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006662 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006663 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006664
6665 // For views inside a scrolling container, delay the pressed feedback for
6666 // a short period in case this is a scroll.
6667 if (isInScrollingContainer) {
6668 mPrivateFlags |= PREPRESSED;
6669 if (mPendingCheckForTap == null) {
6670 mPendingCheckForTap = new CheckForTap();
6671 }
6672 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6673 } else {
6674 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006675 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006676 checkForLongClick(0);
6677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 break;
6679
6680 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006681 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006682 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 break;
6684
6685 case MotionEvent.ACTION_MOVE:
6686 final int x = (int) event.getX();
6687 final int y = (int) event.getY();
6688
6689 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006690 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006692 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006693 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006694 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006695 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006696
Adam Powell4d6f0662012-02-21 15:11:11 -08006697 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 }
6700 break;
6701 }
6702 return true;
6703 }
6704
6705 return false;
6706 }
6707
6708 /**
Adam Powell10298662011-08-14 18:26:30 -07006709 * @hide
6710 */
6711 public boolean isInScrollingContainer() {
6712 ViewParent p = getParent();
6713 while (p != null && p instanceof ViewGroup) {
6714 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6715 return true;
6716 }
6717 p = p.getParent();
6718 }
6719 return false;
6720 }
6721
6722 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006723 * Remove the longpress detection timer.
6724 */
6725 private void removeLongPressCallback() {
6726 if (mPendingCheckForLongPress != null) {
6727 removeCallbacks(mPendingCheckForLongPress);
6728 }
6729 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006730
6731 /**
6732 * Remove the pending click action
6733 */
6734 private void removePerformClickCallback() {
6735 if (mPerformClick != null) {
6736 removeCallbacks(mPerformClick);
6737 }
6738 }
6739
Adam Powelle14579b2009-12-16 18:39:52 -08006740 /**
Romain Guya440b002010-02-24 15:57:54 -08006741 * Remove the prepress detection timer.
6742 */
6743 private void removeUnsetPressCallback() {
6744 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6745 setPressed(false);
6746 removeCallbacks(mUnsetPressedState);
6747 }
6748 }
6749
6750 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006751 * Remove the tap detection timer.
6752 */
6753 private void removeTapCallback() {
6754 if (mPendingCheckForTap != null) {
6755 mPrivateFlags &= ~PREPRESSED;
6756 removeCallbacks(mPendingCheckForTap);
6757 }
6758 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006759
6760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 * Cancels a pending long press. Your subclass can use this if you
6762 * want the context menu to come up if the user presses and holds
6763 * at the same place, but you don't want it to come up if they press
6764 * and then move around enough to cause scrolling.
6765 */
6766 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006767 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006768
6769 /*
6770 * The prepressed state handled by the tap callback is a display
6771 * construct, but the tap callback will post a long press callback
6772 * less its own timeout. Remove it here.
6773 */
6774 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 }
6776
6777 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006778 * Remove the pending callback for sending a
6779 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6780 */
6781 private void removeSendViewScrolledAccessibilityEventCallback() {
6782 if (mSendViewScrolledAccessibilityEvent != null) {
6783 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6784 }
6785 }
6786
6787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 * Sets the TouchDelegate for this View.
6789 */
6790 public void setTouchDelegate(TouchDelegate delegate) {
6791 mTouchDelegate = delegate;
6792 }
6793
6794 /**
6795 * Gets the TouchDelegate for this View.
6796 */
6797 public TouchDelegate getTouchDelegate() {
6798 return mTouchDelegate;
6799 }
6800
6801 /**
6802 * Set flags controlling behavior of this view.
6803 *
6804 * @param flags Constant indicating the value which should be set
6805 * @param mask Constant indicating the bit range that should be changed
6806 */
6807 void setFlags(int flags, int mask) {
6808 int old = mViewFlags;
6809 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6810
6811 int changed = mViewFlags ^ old;
6812 if (changed == 0) {
6813 return;
6814 }
6815 int privateFlags = mPrivateFlags;
6816
6817 /* Check if the FOCUSABLE bit has changed */
6818 if (((changed & FOCUSABLE_MASK) != 0) &&
6819 ((privateFlags & HAS_BOUNDS) !=0)) {
6820 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6821 && ((privateFlags & FOCUSED) != 0)) {
6822 /* Give up focus if we are no longer focusable */
6823 clearFocus();
6824 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6825 && ((privateFlags & FOCUSED) == 0)) {
6826 /*
6827 * Tell the view system that we are now available to take focus
6828 * if no one else already has it.
6829 */
6830 if (mParent != null) mParent.focusableViewAvailable(this);
6831 }
6832 }
6833
6834 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6835 if ((changed & VISIBILITY_MASK) != 0) {
6836 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006837 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006838 * it was not visible. Marking it drawn ensures that the invalidation will
6839 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006841 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006842 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843
6844 needGlobalAttributesUpdate(true);
6845
6846 // a view becoming visible is worth notifying the parent
6847 // about in case nothing has focus. even if this specific view
6848 // isn't focusable, it may contain something that is, so let
6849 // the root view try to give this focus if nothing else does.
6850 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6851 mParent.focusableViewAvailable(this);
6852 }
6853 }
6854 }
6855
6856 /* Check if the GONE bit has changed */
6857 if ((changed & GONE) != 0) {
6858 needGlobalAttributesUpdate(false);
6859 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860
Romain Guyecd80ee2009-12-03 17:13:02 -08006861 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6862 if (hasFocus()) clearFocus();
6863 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006864 if (mParent instanceof View) {
6865 // GONE views noop invalidation, so invalidate the parent
6866 ((View) mParent).invalidate(true);
6867 }
6868 // Mark the view drawn to ensure that it gets invalidated properly the next
6869 // time it is visible and gets invalidated
6870 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 }
6872 if (mAttachInfo != null) {
6873 mAttachInfo.mViewVisibilityChanged = true;
6874 }
6875 }
6876
6877 /* Check if the VISIBLE bit has changed */
6878 if ((changed & INVISIBLE) != 0) {
6879 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006880 /*
6881 * If this view is becoming invisible, set the DRAWN flag so that
6882 * the next invalidate() will not be skipped.
6883 */
6884 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885
6886 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6887 // root view becoming invisible shouldn't clear focus
6888 if (getRootView() != this) {
6889 clearFocus();
6890 }
6891 }
6892 if (mAttachInfo != null) {
6893 mAttachInfo.mViewVisibilityChanged = true;
6894 }
6895 }
6896
Adam Powell326d8082009-12-09 15:10:07 -08006897 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006898 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006899 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6900 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006901 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006902 } else if (mParent != null) {
6903 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006904 }
Adam Powell326d8082009-12-09 15:10:07 -08006905 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6906 }
6907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6909 destroyDrawingCache();
6910 }
6911
6912 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6913 destroyDrawingCache();
6914 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006915 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 }
6917
6918 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6919 destroyDrawingCache();
6920 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6921 }
6922
6923 if ((changed & DRAW_MASK) != 0) {
6924 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6925 if (mBGDrawable != null) {
6926 mPrivateFlags &= ~SKIP_DRAW;
6927 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6928 } else {
6929 mPrivateFlags |= SKIP_DRAW;
6930 }
6931 } else {
6932 mPrivateFlags &= ~SKIP_DRAW;
6933 }
6934 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006935 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 }
6937
6938 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006939 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 mParent.recomputeViewAttributes(this);
6941 }
6942 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006943
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006944 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006945 requestLayout();
6946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 }
6948
6949 /**
6950 * Change the view's z order in the tree, so it's on top of other sibling
6951 * views
6952 */
6953 public void bringToFront() {
6954 if (mParent != null) {
6955 mParent.bringChildToFront(this);
6956 }
6957 }
6958
6959 /**
6960 * This is called in response to an internal scroll in this view (i.e., the
6961 * view scrolled its own contents). This is typically as a result of
6962 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6963 * called.
6964 *
6965 * @param l Current horizontal scroll origin.
6966 * @param t Current vertical scroll origin.
6967 * @param oldl Previous horizontal scroll origin.
6968 * @param oldt Previous vertical scroll origin.
6969 */
6970 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006971 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6972 postSendViewScrolledAccessibilityEventCallback();
6973 }
6974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 mBackgroundSizeChanged = true;
6976
6977 final AttachInfo ai = mAttachInfo;
6978 if (ai != null) {
6979 ai.mViewScrollChanged = true;
6980 }
6981 }
6982
6983 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006984 * Interface definition for a callback to be invoked when the layout bounds of a view
6985 * changes due to layout processing.
6986 */
6987 public interface OnLayoutChangeListener {
6988 /**
6989 * Called when the focus state of a view has changed.
6990 *
6991 * @param v The view whose state has changed.
6992 * @param left The new value of the view's left property.
6993 * @param top The new value of the view's top property.
6994 * @param right The new value of the view's right property.
6995 * @param bottom The new value of the view's bottom property.
6996 * @param oldLeft The previous value of the view's left property.
6997 * @param oldTop The previous value of the view's top property.
6998 * @param oldRight The previous value of the view's right property.
6999 * @param oldBottom The previous value of the view's bottom property.
7000 */
7001 void onLayoutChange(View v, int left, int top, int right, int bottom,
7002 int oldLeft, int oldTop, int oldRight, int oldBottom);
7003 }
7004
7005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 * This is called during layout when the size of this view has changed. If
7007 * you were just added to the view hierarchy, you're called with the old
7008 * values of 0.
7009 *
7010 * @param w Current width of this view.
7011 * @param h Current height of this view.
7012 * @param oldw Old width of this view.
7013 * @param oldh Old height of this view.
7014 */
7015 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7016 }
7017
7018 /**
7019 * Called by draw to draw the child views. This may be overridden
7020 * by derived classes to gain control just before its children are drawn
7021 * (but after its own view has been drawn).
7022 * @param canvas the canvas on which to draw the view
7023 */
7024 protected void dispatchDraw(Canvas canvas) {
7025 }
7026
7027 /**
7028 * Gets the parent of this view. Note that the parent is a
7029 * ViewParent and not necessarily a View.
7030 *
7031 * @return Parent of this view.
7032 */
7033 public final ViewParent getParent() {
7034 return mParent;
7035 }
7036
7037 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007038 * Set the horizontal scrolled position of your view. This will cause a call to
7039 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7040 * invalidated.
7041 * @param value the x position to scroll to
7042 */
7043 public void setScrollX(int value) {
7044 scrollTo(value, mScrollY);
7045 }
7046
7047 /**
7048 * Set the vertical scrolled position of your view. This will cause a call to
7049 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7050 * invalidated.
7051 * @param value the y position to scroll to
7052 */
7053 public void setScrollY(int value) {
7054 scrollTo(mScrollX, value);
7055 }
7056
7057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 * Return the scrolled left position of this view. This is the left edge of
7059 * the displayed part of your view. You do not need to draw any pixels
7060 * farther left, since those are outside of the frame of your view on
7061 * screen.
7062 *
7063 * @return The left edge of the displayed part of your view, in pixels.
7064 */
7065 public final int getScrollX() {
7066 return mScrollX;
7067 }
7068
7069 /**
7070 * Return the scrolled top position of this view. This is the top edge of
7071 * the displayed part of your view. You do not need to draw any pixels above
7072 * it, since those are outside of the frame of your view on screen.
7073 *
7074 * @return The top edge of the displayed part of your view, in pixels.
7075 */
7076 public final int getScrollY() {
7077 return mScrollY;
7078 }
7079
7080 /**
7081 * Return the width of the your view.
7082 *
7083 * @return The width of your view, in pixels.
7084 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007085 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 public final int getWidth() {
7087 return mRight - mLeft;
7088 }
7089
7090 /**
7091 * Return the height of your view.
7092 *
7093 * @return The height of your view, in pixels.
7094 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007095 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 public final int getHeight() {
7097 return mBottom - mTop;
7098 }
7099
7100 /**
7101 * Return the visible drawing bounds of your view. Fills in the output
7102 * rectangle with the values from getScrollX(), getScrollY(),
7103 * getWidth(), and getHeight().
7104 *
7105 * @param outRect The (scrolled) drawing bounds of the view.
7106 */
7107 public void getDrawingRect(Rect outRect) {
7108 outRect.left = mScrollX;
7109 outRect.top = mScrollY;
7110 outRect.right = mScrollX + (mRight - mLeft);
7111 outRect.bottom = mScrollY + (mBottom - mTop);
7112 }
7113
7114 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007115 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7116 * raw width component (that is the result is masked by
7117 * {@link #MEASURED_SIZE_MASK}).
7118 *
7119 * @return The raw measured width of this view.
7120 */
7121 public final int getMeasuredWidth() {
7122 return mMeasuredWidth & MEASURED_SIZE_MASK;
7123 }
7124
7125 /**
7126 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007127 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007128 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 * This should be used during measurement and layout calculations only. Use
7130 * {@link #getWidth()} to see how wide a view is after layout.
7131 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007132 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007134 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 return mMeasuredWidth;
7136 }
7137
7138 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007139 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7140 * raw width component (that is the result is masked by
7141 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007143 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 */
7145 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007146 return mMeasuredHeight & MEASURED_SIZE_MASK;
7147 }
7148
7149 /**
7150 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007151 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007152 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7153 * This should be used during measurement and layout calculations only. Use
7154 * {@link #getHeight()} to see how wide a view is after layout.
7155 *
7156 * @return The measured width of this view as a bit mask.
7157 */
7158 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 return mMeasuredHeight;
7160 }
7161
7162 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007163 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7164 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7165 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7166 * and the height component is at the shifted bits
7167 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7168 */
7169 public final int getMeasuredState() {
7170 return (mMeasuredWidth&MEASURED_STATE_MASK)
7171 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7172 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7173 }
7174
7175 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007176 * The transform matrix of this view, which is calculated based on the current
7177 * roation, scale, and pivot properties.
7178 *
7179 * @see #getRotation()
7180 * @see #getScaleX()
7181 * @see #getScaleY()
7182 * @see #getPivotX()
7183 * @see #getPivotY()
7184 * @return The current transform matrix for the view
7185 */
7186 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007187 if (mTransformationInfo != null) {
7188 updateMatrix();
7189 return mTransformationInfo.mMatrix;
7190 }
7191 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007192 }
7193
7194 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007195 * Utility function to determine if the value is far enough away from zero to be
7196 * considered non-zero.
7197 * @param value A floating point value to check for zero-ness
7198 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7199 */
7200 private static boolean nonzero(float value) {
7201 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7202 }
7203
7204 /**
Jeff Brown86671742010-09-30 20:00:15 -07007205 * Returns true if the transform matrix is the identity matrix.
7206 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007207 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007208 * @return True if the transform matrix is the identity matrix, false otherwise.
7209 */
Jeff Brown86671742010-09-30 20:00:15 -07007210 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007211 if (mTransformationInfo != null) {
7212 updateMatrix();
7213 return mTransformationInfo.mMatrixIsIdentity;
7214 }
7215 return true;
7216 }
7217
7218 void ensureTransformationInfo() {
7219 if (mTransformationInfo == null) {
7220 mTransformationInfo = new TransformationInfo();
7221 }
Jeff Brown86671742010-09-30 20:00:15 -07007222 }
7223
7224 /**
7225 * Recomputes the transform matrix if necessary.
7226 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007227 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007228 final TransformationInfo info = mTransformationInfo;
7229 if (info == null) {
7230 return;
7231 }
7232 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007233 // transform-related properties have changed since the last time someone
7234 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007235
7236 // Figure out if we need to update the pivot point
7237 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007238 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7239 info.mPrevWidth = mRight - mLeft;
7240 info.mPrevHeight = mBottom - mTop;
7241 info.mPivotX = info.mPrevWidth / 2f;
7242 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007243 }
7244 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007245 info.mMatrix.reset();
7246 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7247 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7248 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7249 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007250 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007251 if (info.mCamera == null) {
7252 info.mCamera = new Camera();
7253 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007254 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007255 info.mCamera.save();
7256 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7257 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7258 info.mCamera.getMatrix(info.matrix3D);
7259 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7260 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7261 info.mPivotY + info.mTranslationY);
7262 info.mMatrix.postConcat(info.matrix3D);
7263 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007264 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007265 info.mMatrixDirty = false;
7266 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7267 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007268 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007269 }
7270
7271 /**
7272 * Utility method to retrieve the inverse of the current mMatrix property.
7273 * We cache the matrix to avoid recalculating it when transform properties
7274 * have not changed.
7275 *
7276 * @return The inverse of the current matrix of this view.
7277 */
Jeff Brown86671742010-09-30 20:00:15 -07007278 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007279 final TransformationInfo info = mTransformationInfo;
7280 if (info != null) {
7281 updateMatrix();
7282 if (info.mInverseMatrixDirty) {
7283 if (info.mInverseMatrix == null) {
7284 info.mInverseMatrix = new Matrix();
7285 }
7286 info.mMatrix.invert(info.mInverseMatrix);
7287 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007288 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007289 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007290 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007291 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007292 }
7293
7294 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007295 * Gets the distance along the Z axis from the camera to this view.
7296 *
7297 * @see #setCameraDistance(float)
7298 *
7299 * @return The distance along the Z axis.
7300 */
7301 public float getCameraDistance() {
7302 ensureTransformationInfo();
7303 final float dpi = mResources.getDisplayMetrics().densityDpi;
7304 final TransformationInfo info = mTransformationInfo;
7305 if (info.mCamera == null) {
7306 info.mCamera = new Camera();
7307 info.matrix3D = new Matrix();
7308 }
7309 return -(info.mCamera.getLocationZ() * dpi);
7310 }
7311
7312 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007313 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7314 * views are drawn) from the camera to this view. The camera's distance
7315 * affects 3D transformations, for instance rotations around the X and Y
7316 * axis. If the rotationX or rotationY properties are changed and this view is
7317 * large (more than half the size of the screen), it is recommended to always
7318 * use a camera distance that's greater than the height (X axis rotation) or
7319 * the width (Y axis rotation) of this view.</p>
7320 *
7321 * <p>The distance of the camera from the view plane can have an affect on the
7322 * perspective distortion of the view when it is rotated around the x or y axis.
7323 * For example, a large distance will result in a large viewing angle, and there
7324 * will not be much perspective distortion of the view as it rotates. A short
7325 * distance may cause much more perspective distortion upon rotation, and can
7326 * also result in some drawing artifacts if the rotated view ends up partially
7327 * behind the camera (which is why the recommendation is to use a distance at
7328 * least as far as the size of the view, if the view is to be rotated.)</p>
7329 *
7330 * <p>The distance is expressed in "depth pixels." The default distance depends
7331 * on the screen density. For instance, on a medium density display, the
7332 * default distance is 1280. On a high density display, the default distance
7333 * is 1920.</p>
7334 *
7335 * <p>If you want to specify a distance that leads to visually consistent
7336 * results across various densities, use the following formula:</p>
7337 * <pre>
7338 * float scale = context.getResources().getDisplayMetrics().density;
7339 * view.setCameraDistance(distance * scale);
7340 * </pre>
7341 *
7342 * <p>The density scale factor of a high density display is 1.5,
7343 * and 1920 = 1280 * 1.5.</p>
7344 *
7345 * @param distance The distance in "depth pixels", if negative the opposite
7346 * value is used
7347 *
7348 * @see #setRotationX(float)
7349 * @see #setRotationY(float)
7350 */
7351 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007352 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007353
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007354 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007355 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007356 final TransformationInfo info = mTransformationInfo;
7357 if (info.mCamera == null) {
7358 info.mCamera = new Camera();
7359 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007360 }
7361
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007362 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7363 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007364
Chet Haase9d1992d2012-03-13 11:03:25 -07007365 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007366 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7367 mDisplayList.setCameraDistance(distance);
7368 }
Romain Guya5364ee2011-02-24 14:46:04 -08007369 }
7370
7371 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007372 * The degrees that the view is rotated around the pivot point.
7373 *
Romain Guya5364ee2011-02-24 14:46:04 -08007374 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007375 * @see #getPivotX()
7376 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007377 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007378 * @return The degrees of rotation.
7379 */
Chet Haasea5531132012-02-02 13:41:44 -08007380 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007381 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007382 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007383 }
7384
7385 /**
Chet Haase897247b2010-09-09 14:54:47 -07007386 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7387 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007388 *
7389 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007390 *
7391 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007392 * @see #getPivotX()
7393 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007394 * @see #setRotationX(float)
7395 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007396 *
7397 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007398 */
7399 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007400 ensureTransformationInfo();
7401 final TransformationInfo info = mTransformationInfo;
7402 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007404 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 info.mRotation = rotation;
7406 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007407 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007408 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7409 mDisplayList.setRotation(rotation);
7410 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007411 }
7412 }
7413
7414 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007415 * The degrees that the view is rotated around the vertical axis through the pivot point.
7416 *
7417 * @see #getPivotX()
7418 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007419 * @see #setRotationY(float)
7420 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007421 * @return The degrees of Y rotation.
7422 */
Chet Haasea5531132012-02-02 13:41:44 -08007423 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007424 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007425 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007426 }
7427
7428 /**
Chet Haase897247b2010-09-09 14:54:47 -07007429 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7430 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7431 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007432 *
7433 * When rotating large views, it is recommended to adjust the camera distance
7434 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007435 *
7436 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007437 *
7438 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007439 * @see #getPivotX()
7440 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007441 * @see #setRotation(float)
7442 * @see #setRotationX(float)
7443 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007444 *
7445 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007446 */
7447 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007448 ensureTransformationInfo();
7449 final TransformationInfo info = mTransformationInfo;
7450 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007451 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007452 info.mRotationY = rotationY;
7453 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007454 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007455 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7456 mDisplayList.setRotationY(rotationY);
7457 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007458 }
7459 }
7460
7461 /**
7462 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7463 *
7464 * @see #getPivotX()
7465 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007466 * @see #setRotationX(float)
7467 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007468 * @return The degrees of X rotation.
7469 */
Chet Haasea5531132012-02-02 13:41:44 -08007470 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007471 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007472 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007473 }
7474
7475 /**
Chet Haase897247b2010-09-09 14:54:47 -07007476 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7477 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7478 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007479 *
7480 * When rotating large views, it is recommended to adjust the camera distance
7481 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007482 *
7483 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007484 *
7485 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007486 * @see #getPivotX()
7487 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007488 * @see #setRotation(float)
7489 * @see #setRotationY(float)
7490 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007491 *
7492 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007493 */
7494 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007495 ensureTransformationInfo();
7496 final TransformationInfo info = mTransformationInfo;
7497 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007498 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007499 info.mRotationX = rotationX;
7500 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007501 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007502 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7503 mDisplayList.setRotationX(rotationX);
7504 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007505 }
7506 }
7507
7508 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007509 * The amount that the view is scaled in x around the pivot point, as a proportion of
7510 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7511 *
Joe Onorato93162322010-09-16 15:42:01 -04007512 * <p>By default, this is 1.0f.
7513 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007514 * @see #getPivotX()
7515 * @see #getPivotY()
7516 * @return The scaling factor.
7517 */
Chet Haasea5531132012-02-02 13:41:44 -08007518 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007519 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007520 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007521 }
7522
7523 /**
7524 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7525 * the view's unscaled width. A value of 1 means that no scaling is applied.
7526 *
7527 * @param scaleX The scaling factor.
7528 * @see #getPivotX()
7529 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007530 *
7531 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 */
7533 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007534 ensureTransformationInfo();
7535 final TransformationInfo info = mTransformationInfo;
7536 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007537 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007538 info.mScaleX = scaleX;
7539 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007540 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007541 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7542 mDisplayList.setScaleX(scaleX);
7543 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007544 }
7545 }
7546
7547 /**
7548 * The amount that the view is scaled in y around the pivot point, as a proportion of
7549 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7550 *
Joe Onorato93162322010-09-16 15:42:01 -04007551 * <p>By default, this is 1.0f.
7552 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007553 * @see #getPivotX()
7554 * @see #getPivotY()
7555 * @return The scaling factor.
7556 */
Chet Haasea5531132012-02-02 13:41:44 -08007557 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007558 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007559 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007560 }
7561
7562 /**
7563 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7564 * the view's unscaled width. A value of 1 means that no scaling is applied.
7565 *
7566 * @param scaleY The scaling factor.
7567 * @see #getPivotX()
7568 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007569 *
7570 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007571 */
7572 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007573 ensureTransformationInfo();
7574 final TransformationInfo info = mTransformationInfo;
7575 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007576 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007577 info.mScaleY = scaleY;
7578 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007579 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007580 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7581 mDisplayList.setScaleY(scaleY);
7582 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007583 }
7584 }
7585
7586 /**
7587 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7588 * and {@link #setScaleX(float) scaled}.
7589 *
7590 * @see #getRotation()
7591 * @see #getScaleX()
7592 * @see #getScaleY()
7593 * @see #getPivotY()
7594 * @return The x location of the pivot point.
7595 */
Chet Haasea5531132012-02-02 13:41:44 -08007596 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007597 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007598 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007599 }
7600
7601 /**
7602 * Sets the x location of the point around which the view is
7603 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007604 * By default, the pivot point is centered on the object.
7605 * Setting this property disables this behavior and causes the view to use only the
7606 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007607 *
7608 * @param pivotX The x location of the pivot point.
7609 * @see #getRotation()
7610 * @see #getScaleX()
7611 * @see #getScaleY()
7612 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007613 *
7614 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007615 */
7616 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007617 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007618 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007619 final TransformationInfo info = mTransformationInfo;
7620 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007621 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007622 info.mPivotX = pivotX;
7623 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007624 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007625 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7626 mDisplayList.setPivotX(pivotX);
7627 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007628 }
7629 }
7630
7631 /**
7632 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7633 * and {@link #setScaleY(float) scaled}.
7634 *
7635 * @see #getRotation()
7636 * @see #getScaleX()
7637 * @see #getScaleY()
7638 * @see #getPivotY()
7639 * @return The y location of the pivot point.
7640 */
Chet Haasea5531132012-02-02 13:41:44 -08007641 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007642 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007643 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007644 }
7645
7646 /**
7647 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007648 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7649 * Setting this property disables this behavior and causes the view to use only the
7650 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007651 *
7652 * @param pivotY The y location of the pivot point.
7653 * @see #getRotation()
7654 * @see #getScaleX()
7655 * @see #getScaleY()
7656 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007657 *
7658 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007659 */
7660 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007661 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007662 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007663 final TransformationInfo info = mTransformationInfo;
7664 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007665 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007666 info.mPivotY = pivotY;
7667 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007668 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007669 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7670 mDisplayList.setPivotY(pivotY);
7671 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007672 }
7673 }
7674
7675 /**
7676 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7677 * completely transparent and 1 means the view is completely opaque.
7678 *
Joe Onorato93162322010-09-16 15:42:01 -04007679 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007680 * @return The opacity of the view.
7681 */
Chet Haasea5531132012-02-02 13:41:44 -08007682 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007683 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007684 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007685 }
7686
7687 /**
Romain Guy171c5922011-01-06 10:04:23 -08007688 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7689 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007690 *
Romain Guy171c5922011-01-06 10:04:23 -08007691 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7692 * responsible for applying the opacity itself. Otherwise, calling this method is
7693 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007694 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007695 *
Chet Haasea5531132012-02-02 13:41:44 -08007696 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7697 * performance implications. It is generally best to use the alpha property sparingly and
7698 * transiently, as in the case of fading animations.</p>
7699 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007700 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007701 *
Joe Malin32736f02011-01-19 16:14:20 -08007702 * @see #setLayerType(int, android.graphics.Paint)
7703 *
Chet Haase73066682010-11-29 15:55:32 -08007704 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007705 */
7706 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007707 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007708 if (mTransformationInfo.mAlpha != alpha) {
7709 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007710 if (onSetAlpha((int) (alpha * 255))) {
7711 mPrivateFlags |= ALPHA_SET;
7712 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07007713 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007714 invalidate(true);
7715 } else {
7716 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07007717 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007718 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7719 mDisplayList.setAlpha(alpha);
7720 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007721 }
Chet Haaseed032702010-10-01 14:05:54 -07007722 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007723 }
7724
7725 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007726 * Faster version of setAlpha() which performs the same steps except there are
7727 * no calls to invalidate(). The caller of this function should perform proper invalidation
7728 * on the parent and this object. The return value indicates whether the subclass handles
7729 * alpha (the return value for onSetAlpha()).
7730 *
7731 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007732 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7733 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007734 */
7735 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007736 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007737 if (mTransformationInfo.mAlpha != alpha) {
7738 mTransformationInfo.mAlpha = alpha;
7739 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7740 if (subclassHandlesAlpha) {
7741 mPrivateFlags |= ALPHA_SET;
7742 return true;
7743 } else {
7744 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08007745 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7746 mDisplayList.setAlpha(alpha);
7747 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007748 }
Chet Haasea00f3862011-02-22 06:34:40 -08007749 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007750 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007751 }
7752
7753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 * Top position of this view relative to its parent.
7755 *
7756 * @return The top of this view, in pixels.
7757 */
7758 @ViewDebug.CapturedViewProperty
7759 public final int getTop() {
7760 return mTop;
7761 }
7762
7763 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007764 * Sets the top position of this view relative to its parent. This method is meant to be called
7765 * by the layout system and should not generally be called otherwise, because the property
7766 * may be changed at any time by the layout.
7767 *
7768 * @param top The top of this view, in pixels.
7769 */
7770 public final void setTop(int top) {
7771 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007772 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007773 final boolean matrixIsIdentity = mTransformationInfo == null
7774 || mTransformationInfo.mMatrixIsIdentity;
7775 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007776 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007777 int minTop;
7778 int yLoc;
7779 if (top < mTop) {
7780 minTop = top;
7781 yLoc = top - mTop;
7782 } else {
7783 minTop = mTop;
7784 yLoc = 0;
7785 }
Chet Haasee9140a72011-02-16 16:23:29 -08007786 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007787 }
7788 } else {
7789 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007790 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007791 }
7792
Chet Haaseed032702010-10-01 14:05:54 -07007793 int width = mRight - mLeft;
7794 int oldHeight = mBottom - mTop;
7795
Chet Haase21cd1382010-09-01 17:42:29 -07007796 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08007797 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7798 mDisplayList.setTop(mTop);
7799 }
Chet Haase21cd1382010-09-01 17:42:29 -07007800
Chet Haaseed032702010-10-01 14:05:54 -07007801 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7802
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007803 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007804 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7805 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007806 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007807 }
Chet Haase21cd1382010-09-01 17:42:29 -07007808 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007809 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007810 }
Chet Haase55dbb652010-12-21 20:15:08 -08007811 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007812 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007813 }
7814 }
7815
7816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 * Bottom position of this view relative to its parent.
7818 *
7819 * @return The bottom of this view, in pixels.
7820 */
7821 @ViewDebug.CapturedViewProperty
7822 public final int getBottom() {
7823 return mBottom;
7824 }
7825
7826 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007827 * True if this view has changed since the last time being drawn.
7828 *
7829 * @return The dirty state of this view.
7830 */
7831 public boolean isDirty() {
7832 return (mPrivateFlags & DIRTY_MASK) != 0;
7833 }
7834
7835 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007836 * Sets the bottom position of this view relative to its parent. This method is meant to be
7837 * called by the layout system and should not generally be called otherwise, because the
7838 * property may be changed at any time by the layout.
7839 *
7840 * @param bottom The bottom of this view, in pixels.
7841 */
7842 public final void setBottom(int bottom) {
7843 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007844 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007845 final boolean matrixIsIdentity = mTransformationInfo == null
7846 || mTransformationInfo.mMatrixIsIdentity;
7847 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007848 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007849 int maxBottom;
7850 if (bottom < mBottom) {
7851 maxBottom = mBottom;
7852 } else {
7853 maxBottom = bottom;
7854 }
Chet Haasee9140a72011-02-16 16:23:29 -08007855 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007856 }
7857 } else {
7858 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007859 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007860 }
7861
Chet Haaseed032702010-10-01 14:05:54 -07007862 int width = mRight - mLeft;
7863 int oldHeight = mBottom - mTop;
7864
Chet Haase21cd1382010-09-01 17:42:29 -07007865 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08007866 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7867 mDisplayList.setBottom(mBottom);
7868 }
Chet Haase21cd1382010-09-01 17:42:29 -07007869
Chet Haaseed032702010-10-01 14:05:54 -07007870 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7871
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007872 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007873 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7874 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007875 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007876 }
Chet Haase21cd1382010-09-01 17:42:29 -07007877 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007878 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007879 }
Chet Haase55dbb652010-12-21 20:15:08 -08007880 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007881 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007882 }
7883 }
7884
7885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 * Left position of this view relative to its parent.
7887 *
7888 * @return The left edge of this view, in pixels.
7889 */
7890 @ViewDebug.CapturedViewProperty
7891 public final int getLeft() {
7892 return mLeft;
7893 }
7894
7895 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007896 * Sets the left position of this view relative to its parent. This method is meant to be called
7897 * by the layout system and should not generally be called otherwise, because the property
7898 * may be changed at any time by the layout.
7899 *
7900 * @param left The bottom of this view, in pixels.
7901 */
7902 public final void setLeft(int left) {
7903 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007904 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007905 final boolean matrixIsIdentity = mTransformationInfo == null
7906 || mTransformationInfo.mMatrixIsIdentity;
7907 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007908 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007909 int minLeft;
7910 int xLoc;
7911 if (left < mLeft) {
7912 minLeft = left;
7913 xLoc = left - mLeft;
7914 } else {
7915 minLeft = mLeft;
7916 xLoc = 0;
7917 }
Chet Haasee9140a72011-02-16 16:23:29 -08007918 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007919 }
7920 } else {
7921 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007922 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007923 }
7924
Chet Haaseed032702010-10-01 14:05:54 -07007925 int oldWidth = mRight - mLeft;
7926 int height = mBottom - mTop;
7927
Chet Haase21cd1382010-09-01 17:42:29 -07007928 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08007929 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7930 mDisplayList.setLeft(left);
7931 }
Chet Haase21cd1382010-09-01 17:42:29 -07007932
Chet Haaseed032702010-10-01 14:05:54 -07007933 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7934
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007935 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007936 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7937 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007938 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007939 }
Chet Haase21cd1382010-09-01 17:42:29 -07007940 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007941 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007942 }
Chet Haase55dbb652010-12-21 20:15:08 -08007943 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007944 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08007945 if (USE_DISPLAY_LIST_PROPERTIES) {
7946
7947 }
Chet Haase21cd1382010-09-01 17:42:29 -07007948 }
7949 }
7950
7951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 * Right position of this view relative to its parent.
7953 *
7954 * @return The right edge of this view, in pixels.
7955 */
7956 @ViewDebug.CapturedViewProperty
7957 public final int getRight() {
7958 return mRight;
7959 }
7960
7961 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007962 * Sets the right position of this view relative to its parent. This method is meant to be called
7963 * by the layout system and should not generally be called otherwise, because the property
7964 * may be changed at any time by the layout.
7965 *
7966 * @param right The bottom of this view, in pixels.
7967 */
7968 public final void setRight(int right) {
7969 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007970 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007971 final boolean matrixIsIdentity = mTransformationInfo == null
7972 || mTransformationInfo.mMatrixIsIdentity;
7973 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007974 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007975 int maxRight;
7976 if (right < mRight) {
7977 maxRight = mRight;
7978 } else {
7979 maxRight = right;
7980 }
Chet Haasee9140a72011-02-16 16:23:29 -08007981 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007982 }
7983 } else {
7984 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007985 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007986 }
7987
Chet Haaseed032702010-10-01 14:05:54 -07007988 int oldWidth = mRight - mLeft;
7989 int height = mBottom - mTop;
7990
Chet Haase21cd1382010-09-01 17:42:29 -07007991 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08007992 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7993 mDisplayList.setRight(mRight);
7994 }
Chet Haase21cd1382010-09-01 17:42:29 -07007995
Chet Haaseed032702010-10-01 14:05:54 -07007996 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7997
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007998 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007999 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8000 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008001 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008002 }
Chet Haase21cd1382010-09-01 17:42:29 -07008003 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008004 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008005 }
Chet Haase55dbb652010-12-21 20:15:08 -08008006 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008007 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008008 }
8009 }
8010
8011 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008012 * The visual x position of this view, in pixels. This is equivalent to the
8013 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008014 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008015 *
Chet Haasedf030d22010-07-30 17:22:38 -07008016 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 */
Chet Haasea5531132012-02-02 13:41:44 -08008018 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008019 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008020 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008021 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008022
Chet Haasedf030d22010-07-30 17:22:38 -07008023 /**
8024 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8025 * {@link #setTranslationX(float) translationX} property to be the difference between
8026 * the x value passed in and the current {@link #getLeft() left} property.
8027 *
8028 * @param x The visual x position of this view, in pixels.
8029 */
8030 public void setX(float x) {
8031 setTranslationX(x - mLeft);
8032 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008033
Chet Haasedf030d22010-07-30 17:22:38 -07008034 /**
8035 * The visual y position of this view, in pixels. This is equivalent to the
8036 * {@link #setTranslationY(float) translationY} property plus the current
8037 * {@link #getTop() top} property.
8038 *
8039 * @return The visual y position of this view, in pixels.
8040 */
Chet Haasea5531132012-02-02 13:41:44 -08008041 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008042 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008043 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008044 }
8045
8046 /**
8047 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8048 * {@link #setTranslationY(float) translationY} property to be the difference between
8049 * the y value passed in and the current {@link #getTop() top} property.
8050 *
8051 * @param y The visual y position of this view, in pixels.
8052 */
8053 public void setY(float y) {
8054 setTranslationY(y - mTop);
8055 }
8056
8057
8058 /**
8059 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8060 * This position is post-layout, in addition to wherever the object's
8061 * layout placed it.
8062 *
8063 * @return The horizontal position of this view relative to its left position, in pixels.
8064 */
Chet Haasea5531132012-02-02 13:41:44 -08008065 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008066 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008067 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008068 }
8069
8070 /**
8071 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8072 * This effectively positions the object post-layout, in addition to wherever the object's
8073 * layout placed it.
8074 *
8075 * @param translationX The horizontal position of this view relative to its left position,
8076 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008077 *
8078 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008079 */
8080 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008081 ensureTransformationInfo();
8082 final TransformationInfo info = mTransformationInfo;
8083 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008084 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008085 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008086 info.mTranslationX = translationX;
8087 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008088 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008089 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8090 mDisplayList.setTranslationX(translationX);
8091 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008092 }
8093 }
8094
8095 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008096 * The horizontal location of this view relative to its {@link #getTop() top} position.
8097 * This position is post-layout, in addition to wherever the object's
8098 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008099 *
Chet Haasedf030d22010-07-30 17:22:38 -07008100 * @return The vertical position of this view relative to its top position,
8101 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008102 */
Chet Haasea5531132012-02-02 13:41:44 -08008103 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008104 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008105 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008106 }
8107
8108 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008109 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8110 * This effectively positions the object post-layout, in addition to wherever the object's
8111 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008112 *
Chet Haasedf030d22010-07-30 17:22:38 -07008113 * @param translationY The vertical position of this view relative to its top position,
8114 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008115 *
8116 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008117 */
Chet Haasedf030d22010-07-30 17:22:38 -07008118 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008119 ensureTransformationInfo();
8120 final TransformationInfo info = mTransformationInfo;
8121 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008122 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008123 info.mTranslationY = translationY;
8124 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008125 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008126 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8127 mDisplayList.setTranslationY(translationY);
8128 }
Chet Haasedf030d22010-07-30 17:22:38 -07008129 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008130 }
8131
8132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 * Hit rectangle in parent's coordinates
8134 *
8135 * @param outRect The hit rectangle of the view.
8136 */
8137 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008138 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008139 final TransformationInfo info = mTransformationInfo;
8140 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008141 outRect.set(mLeft, mTop, mRight, mBottom);
8142 } else {
8143 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008144 tmpRect.set(-info.mPivotX, -info.mPivotY,
8145 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8146 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008147 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8148 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008149 }
8150 }
8151
8152 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008153 * Determines whether the given point, in local coordinates is inside the view.
8154 */
8155 /*package*/ final boolean pointInView(float localX, float localY) {
8156 return localX >= 0 && localX < (mRight - mLeft)
8157 && localY >= 0 && localY < (mBottom - mTop);
8158 }
8159
8160 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008161 * Utility method to determine whether the given point, in local coordinates,
8162 * is inside the view, where the area of the view is expanded by the slop factor.
8163 * This method is called while processing touch-move events to determine if the event
8164 * is still within the view.
8165 */
8166 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008167 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008168 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 }
8170
8171 /**
8172 * When a view has focus and the user navigates away from it, the next view is searched for
8173 * starting from the rectangle filled in by this method.
8174 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008175 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8176 * of the view. However, if your view maintains some idea of internal selection,
8177 * such as a cursor, or a selected row or column, you should override this method and
8178 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 *
8180 * @param r The rectangle to fill in, in this view's coordinates.
8181 */
8182 public void getFocusedRect(Rect r) {
8183 getDrawingRect(r);
8184 }
8185
8186 /**
8187 * If some part of this view is not clipped by any of its parents, then
8188 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008189 * coordinates (without taking possible View rotations into account), offset
8190 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8191 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 *
8193 * @param r If true is returned, r holds the global coordinates of the
8194 * visible portion of this view.
8195 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8196 * between this view and its root. globalOffet may be null.
8197 * @return true if r is non-empty (i.e. part of the view is visible at the
8198 * root level.
8199 */
8200 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8201 int width = mRight - mLeft;
8202 int height = mBottom - mTop;
8203 if (width > 0 && height > 0) {
8204 r.set(0, 0, width, height);
8205 if (globalOffset != null) {
8206 globalOffset.set(-mScrollX, -mScrollY);
8207 }
8208 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8209 }
8210 return false;
8211 }
8212
8213 public final boolean getGlobalVisibleRect(Rect r) {
8214 return getGlobalVisibleRect(r, null);
8215 }
8216
8217 public final boolean getLocalVisibleRect(Rect r) {
8218 Point offset = new Point();
8219 if (getGlobalVisibleRect(r, offset)) {
8220 r.offset(-offset.x, -offset.y); // make r local
8221 return true;
8222 }
8223 return false;
8224 }
8225
8226 /**
8227 * Offset this view's vertical location by the specified number of pixels.
8228 *
8229 * @param offset the number of pixels to offset the view by
8230 */
8231 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008232 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008233 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008234 final boolean matrixIsIdentity = mTransformationInfo == null
8235 || mTransformationInfo.mMatrixIsIdentity;
8236 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008237 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8238 invalidateViewProperty(false, false);
8239 } else {
8240 final ViewParent p = mParent;
8241 if (p != null && mAttachInfo != null) {
8242 final Rect r = mAttachInfo.mTmpInvalRect;
8243 int minTop;
8244 int maxBottom;
8245 int yLoc;
8246 if (offset < 0) {
8247 minTop = mTop + offset;
8248 maxBottom = mBottom;
8249 yLoc = offset;
8250 } else {
8251 minTop = mTop;
8252 maxBottom = mBottom + offset;
8253 yLoc = 0;
8254 }
8255 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8256 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008257 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008258 }
8259 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008260 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008261 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008262
Chet Haasec3aa3612010-06-17 08:50:37 -07008263 mTop += offset;
8264 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008265 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8266 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008267 invalidateViewProperty(false, false);
8268 } else {
8269 if (!matrixIsIdentity) {
8270 invalidateViewProperty(false, true);
8271 }
8272 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008273 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 }
8276
8277 /**
8278 * Offset this view's horizontal location by the specified amount of pixels.
8279 *
8280 * @param offset the numer of pixels to offset the view by
8281 */
8282 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008283 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008284 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008285 final boolean matrixIsIdentity = mTransformationInfo == null
8286 || mTransformationInfo.mMatrixIsIdentity;
8287 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008288 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8289 invalidateViewProperty(false, false);
8290 } else {
8291 final ViewParent p = mParent;
8292 if (p != null && mAttachInfo != null) {
8293 final Rect r = mAttachInfo.mTmpInvalRect;
8294 int minLeft;
8295 int maxRight;
8296 if (offset < 0) {
8297 minLeft = mLeft + offset;
8298 maxRight = mRight;
8299 } else {
8300 minLeft = mLeft;
8301 maxRight = mRight + offset;
8302 }
8303 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8304 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008305 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008306 }
8307 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008308 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008309 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008310
Chet Haasec3aa3612010-06-17 08:50:37 -07008311 mLeft += offset;
8312 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008313 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8314 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008315 invalidateViewProperty(false, false);
8316 } else {
8317 if (!matrixIsIdentity) {
8318 invalidateViewProperty(false, true);
8319 }
8320 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008321 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 }
8324
8325 /**
8326 * Get the LayoutParams associated with this view. All views should have
8327 * layout parameters. These supply parameters to the <i>parent</i> of this
8328 * view specifying how it should be arranged. There are many subclasses of
8329 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8330 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008331 *
8332 * This method may return null if this View is not attached to a parent
8333 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8334 * was not invoked successfully. When a View is attached to a parent
8335 * ViewGroup, this method must not return null.
8336 *
8337 * @return The LayoutParams associated with this view, or null if no
8338 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008340 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 public ViewGroup.LayoutParams getLayoutParams() {
8342 return mLayoutParams;
8343 }
8344
8345 /**
8346 * Set the layout parameters associated with this view. These supply
8347 * parameters to the <i>parent</i> of this view specifying how it should be
8348 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8349 * correspond to the different subclasses of ViewGroup that are responsible
8350 * for arranging their children.
8351 *
Romain Guy01c174b2011-02-22 11:51:06 -08008352 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 */
8354 public void setLayoutParams(ViewGroup.LayoutParams params) {
8355 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008356 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 }
8358 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008359 if (mParent instanceof ViewGroup) {
8360 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 requestLayout();
8363 }
8364
8365 /**
8366 * Set the scrolled position of your view. This will cause a call to
8367 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8368 * invalidated.
8369 * @param x the x position to scroll to
8370 * @param y the y position to scroll to
8371 */
8372 public void scrollTo(int x, int y) {
8373 if (mScrollX != x || mScrollY != y) {
8374 int oldX = mScrollX;
8375 int oldY = mScrollY;
8376 mScrollX = x;
8377 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008378 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008380 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008381 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008383 }
8384 }
8385
8386 /**
8387 * Move the scrolled position of your view. This will cause a call to
8388 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8389 * invalidated.
8390 * @param x the amount of pixels to scroll by horizontally
8391 * @param y the amount of pixels to scroll by vertically
8392 */
8393 public void scrollBy(int x, int y) {
8394 scrollTo(mScrollX + x, mScrollY + y);
8395 }
8396
8397 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008398 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8399 * animation to fade the scrollbars out after a default delay. If a subclass
8400 * provides animated scrolling, the start delay should equal the duration
8401 * of the scrolling animation.</p>
8402 *
8403 * <p>The animation starts only if at least one of the scrollbars is
8404 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8405 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8406 * this method returns true, and false otherwise. If the animation is
8407 * started, this method calls {@link #invalidate()}; in that case the
8408 * caller should not call {@link #invalidate()}.</p>
8409 *
8410 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008411 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008412 *
8413 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8414 * and {@link #scrollTo(int, int)}.</p>
8415 *
8416 * @return true if the animation is played, false otherwise
8417 *
8418 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008419 * @see #scrollBy(int, int)
8420 * @see #scrollTo(int, int)
8421 * @see #isHorizontalScrollBarEnabled()
8422 * @see #isVerticalScrollBarEnabled()
8423 * @see #setHorizontalScrollBarEnabled(boolean)
8424 * @see #setVerticalScrollBarEnabled(boolean)
8425 */
8426 protected boolean awakenScrollBars() {
8427 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008428 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008429 }
8430
8431 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008432 * Trigger the scrollbars to draw.
8433 * This method differs from awakenScrollBars() only in its default duration.
8434 * initialAwakenScrollBars() will show the scroll bars for longer than
8435 * usual to give the user more of a chance to notice them.
8436 *
8437 * @return true if the animation is played, false otherwise.
8438 */
8439 private boolean initialAwakenScrollBars() {
8440 return mScrollCache != null &&
8441 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8442 }
8443
8444 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008445 * <p>
8446 * Trigger the scrollbars to draw. When invoked this method starts an
8447 * animation to fade the scrollbars out after a fixed delay. If a subclass
8448 * provides animated scrolling, the start delay should equal the duration of
8449 * the scrolling animation.
8450 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008451 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008452 * <p>
8453 * The animation starts only if at least one of the scrollbars is enabled,
8454 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8455 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8456 * this method returns true, and false otherwise. If the animation is
8457 * started, this method calls {@link #invalidate()}; in that case the caller
8458 * should not call {@link #invalidate()}.
8459 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008460 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008461 * <p>
8462 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008463 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008464 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008465 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008466 * @param startDelay the delay, in milliseconds, after which the animation
8467 * should start; when the delay is 0, the animation starts
8468 * immediately
8469 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008470 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008471 * @see #scrollBy(int, int)
8472 * @see #scrollTo(int, int)
8473 * @see #isHorizontalScrollBarEnabled()
8474 * @see #isVerticalScrollBarEnabled()
8475 * @see #setHorizontalScrollBarEnabled(boolean)
8476 * @see #setVerticalScrollBarEnabled(boolean)
8477 */
8478 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008479 return awakenScrollBars(startDelay, true);
8480 }
Joe Malin32736f02011-01-19 16:14:20 -08008481
Mike Cleron290947b2009-09-29 18:34:32 -07008482 /**
8483 * <p>
8484 * Trigger the scrollbars to draw. When invoked this method starts an
8485 * animation to fade the scrollbars out after a fixed delay. If a subclass
8486 * provides animated scrolling, the start delay should equal the duration of
8487 * the scrolling animation.
8488 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008489 *
Mike Cleron290947b2009-09-29 18:34:32 -07008490 * <p>
8491 * The animation starts only if at least one of the scrollbars is enabled,
8492 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8493 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8494 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008495 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008496 * is set to true; in that case the caller
8497 * should not call {@link #invalidate()}.
8498 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008499 *
Mike Cleron290947b2009-09-29 18:34:32 -07008500 * <p>
8501 * This method should be invoked everytime a subclass directly updates the
8502 * scroll parameters.
8503 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008504 *
Mike Cleron290947b2009-09-29 18:34:32 -07008505 * @param startDelay the delay, in milliseconds, after which the animation
8506 * should start; when the delay is 0, the animation starts
8507 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008508 *
Mike Cleron290947b2009-09-29 18:34:32 -07008509 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008510 *
Mike Cleron290947b2009-09-29 18:34:32 -07008511 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008512 *
Mike Cleron290947b2009-09-29 18:34:32 -07008513 * @see #scrollBy(int, int)
8514 * @see #scrollTo(int, int)
8515 * @see #isHorizontalScrollBarEnabled()
8516 * @see #isVerticalScrollBarEnabled()
8517 * @see #setHorizontalScrollBarEnabled(boolean)
8518 * @see #setVerticalScrollBarEnabled(boolean)
8519 */
8520 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008521 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008522
Mike Cleronf116bf82009-09-27 19:14:12 -07008523 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8524 return false;
8525 }
8526
8527 if (scrollCache.scrollBar == null) {
8528 scrollCache.scrollBar = new ScrollBarDrawable();
8529 }
8530
8531 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8532
Mike Cleron290947b2009-09-29 18:34:32 -07008533 if (invalidate) {
8534 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008535 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008536 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008537
8538 if (scrollCache.state == ScrollabilityCache.OFF) {
8539 // FIXME: this is copied from WindowManagerService.
8540 // We should get this value from the system when it
8541 // is possible to do so.
8542 final int KEY_REPEAT_FIRST_DELAY = 750;
8543 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8544 }
8545
8546 // Tell mScrollCache when we should start fading. This may
8547 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008548 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008549 scrollCache.fadeStartTime = fadeStartTime;
8550 scrollCache.state = ScrollabilityCache.ON;
8551
8552 // Schedule our fader to run, unscheduling any old ones first
8553 if (mAttachInfo != null) {
8554 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8555 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8556 }
8557
8558 return true;
8559 }
8560
8561 return false;
8562 }
8563
8564 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008565 * Do not invalidate views which are not visible and which are not running an animation. They
8566 * will not get drawn and they should not set dirty flags as if they will be drawn
8567 */
8568 private boolean skipInvalidate() {
8569 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8570 (!(mParent instanceof ViewGroup) ||
8571 !((ViewGroup) mParent).isViewTransitioning(this));
8572 }
8573 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008574 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008575 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8576 * in the future. This must be called from a UI thread. To call from a non-UI
8577 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 *
8579 * WARNING: This method is destructive to dirty.
8580 * @param dirty the rectangle representing the bounds of the dirty region
8581 */
8582 public void invalidate(Rect dirty) {
8583 if (ViewDebug.TRACE_HIERARCHY) {
8584 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8585 }
8586
Chet Haaseaceafe62011-08-26 15:44:33 -07008587 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008588 return;
8589 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008590 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008591 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8592 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008593 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008594 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008595 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 final ViewParent p = mParent;
8597 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008598 //noinspection PointlessBooleanExpression,ConstantConditions
8599 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8600 if (p != null && ai != null && ai.mHardwareAccelerated) {
8601 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008602 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008603 p.invalidateChild(this, null);
8604 return;
8605 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 if (p != null && ai != null) {
8608 final int scrollX = mScrollX;
8609 final int scrollY = mScrollY;
8610 final Rect r = ai.mTmpInvalRect;
8611 r.set(dirty.left - scrollX, dirty.top - scrollY,
8612 dirty.right - scrollX, dirty.bottom - scrollY);
8613 mParent.invalidateChild(this, r);
8614 }
8615 }
8616 }
8617
8618 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008619 * 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 -08008620 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008621 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8622 * will be called at some point in the future. This must be called from
8623 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 * @param l the left position of the dirty region
8625 * @param t the top position of the dirty region
8626 * @param r the right position of the dirty region
8627 * @param b the bottom position of the dirty region
8628 */
8629 public void invalidate(int l, int t, int r, int b) {
8630 if (ViewDebug.TRACE_HIERARCHY) {
8631 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8632 }
8633
Chet Haaseaceafe62011-08-26 15:44:33 -07008634 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008635 return;
8636 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008637 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008638 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8639 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008641 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008642 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 final ViewParent p = mParent;
8644 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008645 //noinspection PointlessBooleanExpression,ConstantConditions
8646 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8647 if (p != null && ai != null && ai.mHardwareAccelerated) {
8648 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008649 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008650 p.invalidateChild(this, null);
8651 return;
8652 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008654 if (p != null && ai != null && l < r && t < b) {
8655 final int scrollX = mScrollX;
8656 final int scrollY = mScrollY;
8657 final Rect tmpr = ai.mTmpInvalRect;
8658 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8659 p.invalidateChild(this, tmpr);
8660 }
8661 }
8662 }
8663
8664 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008665 * Invalidate the whole view. If the view is visible,
8666 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8667 * the future. This must be called from a UI thread. To call from a non-UI thread,
8668 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 */
8670 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008671 invalidate(true);
8672 }
Joe Malin32736f02011-01-19 16:14:20 -08008673
Chet Haaseed032702010-10-01 14:05:54 -07008674 /**
8675 * This is where the invalidate() work actually happens. A full invalidate()
8676 * causes the drawing cache to be invalidated, but this function can be called with
8677 * invalidateCache set to false to skip that invalidation step for cases that do not
8678 * need it (for example, a component that remains at the same dimensions with the same
8679 * content).
8680 *
8681 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8682 * well. This is usually true for a full invalidate, but may be set to false if the
8683 * View's contents or dimensions have not changed.
8684 */
Romain Guy849d0a32011-02-01 17:20:48 -08008685 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008686 if (ViewDebug.TRACE_HIERARCHY) {
8687 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8688 }
8689
Chet Haaseaceafe62011-08-26 15:44:33 -07008690 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008691 return;
8692 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008693 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008694 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008695 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8696 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008697 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008698 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008699 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008700 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008701 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008704 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008705 //noinspection PointlessBooleanExpression,ConstantConditions
8706 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8707 if (p != null && ai != null && ai.mHardwareAccelerated) {
8708 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008709 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008710 p.invalidateChild(this, null);
8711 return;
8712 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008713 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 if (p != null && ai != null) {
8716 final Rect r = ai.mTmpInvalRect;
8717 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8718 // Don't call invalidate -- we don't want to internally scroll
8719 // our own bounds
8720 p.invalidateChild(this, r);
8721 }
8722 }
8723 }
8724
8725 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07008726 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
8727 * set any flags or handle all of the cases handled by the default invalidation methods.
8728 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
8729 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
8730 * walk up the hierarchy, transforming the dirty rect as necessary.
8731 *
8732 * The method also handles normal invalidation logic if display list properties are not
8733 * being used in this view. The invalidateParent and forceRedraw flags are used by that
8734 * backup approach, to handle these cases used in the various property-setting methods.
8735 *
8736 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
8737 * are not being used in this view
8738 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
8739 * list properties are not being used in this view
8740 */
8741 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
8742 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
8743 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
8744 if (invalidateParent) {
8745 invalidateParentCaches();
8746 }
8747 if (forceRedraw) {
8748 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
8749 }
8750 invalidate(false);
8751 } else {
8752 final AttachInfo ai = mAttachInfo;
8753 final ViewParent p = mParent;
8754 if (p != null && ai != null) {
8755 final Rect r = ai.mTmpInvalRect;
8756 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8757 if (mParent instanceof ViewGroup) {
8758 ((ViewGroup) mParent).invalidateChildFast(this, r);
8759 } else {
8760 mParent.invalidateChild(this, r);
8761 }
8762 }
8763 }
8764 }
8765
8766 /**
8767 * Utility method to transform a given Rect by the current matrix of this view.
8768 */
8769 void transformRect(final Rect rect) {
8770 if (!getMatrix().isIdentity()) {
8771 RectF boundingRect = mAttachInfo.mTmpTransformRect;
8772 boundingRect.set(rect);
8773 getMatrix().mapRect(boundingRect);
8774 rect.set((int) (boundingRect.left - 0.5f),
8775 (int) (boundingRect.top - 0.5f),
8776 (int) (boundingRect.right + 0.5f),
8777 (int) (boundingRect.bottom + 0.5f));
8778 }
8779 }
8780
8781 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008782 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008783 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8784 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008785 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8786 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008787 *
8788 * @hide
8789 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008790 protected void invalidateParentCaches() {
8791 if (mParent instanceof View) {
8792 ((View) mParent).mPrivateFlags |= INVALIDATED;
8793 }
8794 }
Joe Malin32736f02011-01-19 16:14:20 -08008795
Romain Guy0fd89bf2011-01-26 15:41:30 -08008796 /**
8797 * Used to indicate that the parent of this view should be invalidated. This functionality
8798 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8799 * which is necessary when various parent-managed properties of the view change, such as
8800 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8801 * an invalidation event to the parent.
8802 *
8803 * @hide
8804 */
8805 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008806 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008807 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008808 }
8809 }
8810
8811 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008812 * Indicates whether this View is opaque. An opaque View guarantees that it will
8813 * draw all the pixels overlapping its bounds using a fully opaque color.
8814 *
8815 * Subclasses of View should override this method whenever possible to indicate
8816 * whether an instance is opaque. Opaque Views are treated in a special way by
8817 * the View hierarchy, possibly allowing it to perform optimizations during
8818 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008819 *
Romain Guy24443ea2009-05-11 11:56:30 -07008820 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008821 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008822 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008823 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008824 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008825 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8826 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008827 }
8828
Adam Powell20232d02010-12-08 21:08:53 -08008829 /**
8830 * @hide
8831 */
8832 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008833 // Opaque if:
8834 // - Has a background
8835 // - Background is opaque
8836 // - Doesn't have scrollbars or scrollbars are inside overlay
8837
8838 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8839 mPrivateFlags |= OPAQUE_BACKGROUND;
8840 } else {
8841 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8842 }
8843
8844 final int flags = mViewFlags;
8845 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8846 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8847 mPrivateFlags |= OPAQUE_SCROLLBARS;
8848 } else {
8849 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8850 }
8851 }
8852
8853 /**
8854 * @hide
8855 */
8856 protected boolean hasOpaqueScrollbars() {
8857 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008858 }
8859
8860 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008861 * @return A handler associated with the thread running the View. This
8862 * handler can be used to pump events in the UI events queue.
8863 */
8864 public Handler getHandler() {
8865 if (mAttachInfo != null) {
8866 return mAttachInfo.mHandler;
8867 }
8868 return null;
8869 }
8870
8871 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008872 * Gets the view root associated with the View.
8873 * @return The view root, or null if none.
8874 * @hide
8875 */
8876 public ViewRootImpl getViewRootImpl() {
8877 if (mAttachInfo != null) {
8878 return mAttachInfo.mViewRootImpl;
8879 }
8880 return null;
8881 }
8882
8883 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008884 * <p>Causes the Runnable to be added to the message queue.
8885 * The runnable will be run on the user interface thread.</p>
8886 *
8887 * <p>This method can be invoked from outside of the UI thread
8888 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 *
8890 * @param action The Runnable that will be executed.
8891 *
8892 * @return Returns true if the Runnable was successfully placed in to the
8893 * message queue. Returns false on failure, usually because the
8894 * looper processing the message queue is exiting.
8895 */
8896 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008897 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008898 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008899 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008900 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008901 // Assume that post will succeed later
8902 ViewRootImpl.getRunQueue().post(action);
8903 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 }
8905
8906 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008907 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008909 * The runnable will be run on the user interface thread.</p>
8910 *
8911 * <p>This method can be invoked from outside of the UI thread
8912 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 *
8914 * @param action The Runnable that will be executed.
8915 * @param delayMillis The delay (in milliseconds) until the Runnable
8916 * will be executed.
8917 *
8918 * @return true if the Runnable was successfully placed in to the
8919 * message queue. Returns false on failure, usually because the
8920 * looper processing the message queue is exiting. Note that a
8921 * result of true does not mean the Runnable will be processed --
8922 * if the looper is quit before the delivery time of the message
8923 * occurs then the message will be dropped.
8924 */
8925 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008926 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008927 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008928 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008929 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008930 // Assume that post will succeed later
8931 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8932 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 }
8934
8935 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008936 * <p>Causes the Runnable to execute on the next animation time step.
8937 * The runnable will be run on the user interface thread.</p>
8938 *
8939 * <p>This method can be invoked from outside of the UI thread
8940 * only when this View is attached to a window.</p>
8941 *
8942 * @param action The Runnable that will be executed.
8943 *
8944 * @hide
8945 */
8946 public void postOnAnimation(Runnable action) {
8947 final AttachInfo attachInfo = mAttachInfo;
8948 if (attachInfo != null) {
8949 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallback(action, null);
8950 } else {
8951 // Assume that post will succeed later
8952 ViewRootImpl.getRunQueue().post(action);
8953 }
8954 }
8955
8956 /**
8957 * <p>Causes the Runnable to execute on the next animation time step,
8958 * after the specified amount of time elapses.
8959 * The runnable will be run on the user interface thread.</p>
8960 *
8961 * <p>This method can be invoked from outside of the UI thread
8962 * only when this View is attached to a window.</p>
8963 *
8964 * @param action The Runnable that will be executed.
8965 * @param delayMillis The delay (in milliseconds) until the Runnable
8966 * will be executed.
8967 *
8968 * @hide
8969 */
8970 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
8971 final AttachInfo attachInfo = mAttachInfo;
8972 if (attachInfo != null) {
8973 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
8974 action, null, delayMillis);
8975 } else {
8976 // Assume that post will succeed later
8977 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8978 }
8979 }
8980
8981 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008982 * <p>Removes the specified Runnable from the message queue.</p>
8983 *
8984 * <p>This method can be invoked from outside of the UI thread
8985 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 *
8987 * @param action The Runnable to remove from the message handling queue
8988 *
8989 * @return true if this view could ask the Handler to remove the Runnable,
8990 * false otherwise. When the returned value is true, the Runnable
8991 * may or may not have been actually removed from the message queue
8992 * (for instance, if the Runnable was not in the queue already.)
8993 */
8994 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08008995 if (action != null) {
8996 final AttachInfo attachInfo = mAttachInfo;
8997 if (attachInfo != null) {
8998 attachInfo.mHandler.removeCallbacks(action);
8999 attachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(action, null);
9000 } else {
9001 // Assume that post will succeed later
9002 ViewRootImpl.getRunQueue().removeCallbacks(action);
9003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009005 return true;
9006 }
9007
9008 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009009 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9010 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011 *
Romain Guye63a4f32011-08-11 11:33:31 -07009012 * <p>This method can be invoked from outside of the UI thread
9013 * only when this View is attached to a window.</p>
9014 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 * @see #invalidate()
9016 */
9017 public void postInvalidate() {
9018 postInvalidateDelayed(0);
9019 }
9020
9021 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009022 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9023 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
9024 *
9025 * <p>This method can be invoked from outside of the UI thread
9026 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 *
9028 * @param left The left coordinate of the rectangle to invalidate.
9029 * @param top The top coordinate of the rectangle to invalidate.
9030 * @param right The right coordinate of the rectangle to invalidate.
9031 * @param bottom The bottom coordinate of the rectangle to invalidate.
9032 *
9033 * @see #invalidate(int, int, int, int)
9034 * @see #invalidate(Rect)
9035 */
9036 public void postInvalidate(int left, int top, int right, int bottom) {
9037 postInvalidateDelayed(0, left, top, right, bottom);
9038 }
9039
9040 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009041 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9042 * loop. Waits for the specified amount of time.</p>
9043 *
9044 * <p>This method can be invoked from outside of the UI thread
9045 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 *
9047 * @param delayMilliseconds the duration in milliseconds to delay the
9048 * invalidation by
9049 */
9050 public void postInvalidateDelayed(long delayMilliseconds) {
9051 // We try only with the AttachInfo because there's no point in invalidating
9052 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009053 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009054 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009055 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 }
9057 }
9058
9059 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009060 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9061 * through the event loop. Waits for the specified amount of time.</p>
9062 *
9063 * <p>This method can be invoked from outside of the UI thread
9064 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009065 *
9066 * @param delayMilliseconds the duration in milliseconds to delay the
9067 * invalidation by
9068 * @param left The left coordinate of the rectangle to invalidate.
9069 * @param top The top coordinate of the rectangle to invalidate.
9070 * @param right The right coordinate of the rectangle to invalidate.
9071 * @param bottom The bottom coordinate of the rectangle to invalidate.
9072 */
9073 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9074 int right, int bottom) {
9075
9076 // We try only with the AttachInfo because there's no point in invalidating
9077 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009078 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009079 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009080 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9081 info.target = this;
9082 info.left = left;
9083 info.top = top;
9084 info.right = right;
9085 info.bottom = bottom;
9086
Jeff Browna175a5b2012-02-15 19:18:31 -08009087 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 }
9089 }
9090
9091 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009092 * <p>Cause an invalidate to happen on the next animation time step, typically the
9093 * next display frame.</p>
9094 *
9095 * <p>This method can be invoked from outside of the UI thread
9096 * only when this View is attached to a window.</p>
9097 *
9098 * @hide
9099 */
9100 public void postInvalidateOnAnimation() {
9101 // We try only with the AttachInfo because there's no point in invalidating
9102 // if we are not attached to our window
9103 final AttachInfo attachInfo = mAttachInfo;
9104 if (attachInfo != null) {
9105 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9106 }
9107 }
9108
9109 /**
9110 * <p>Cause an invalidate of the specified area to happen on the next animation
9111 * time step, typically the next display frame.</p>
9112 *
9113 * <p>This method can be invoked from outside of the UI thread
9114 * only when this View is attached to a window.</p>
9115 *
9116 * @param left The left coordinate of the rectangle to invalidate.
9117 * @param top The top coordinate of the rectangle to invalidate.
9118 * @param right The right coordinate of the rectangle to invalidate.
9119 * @param bottom The bottom coordinate of the rectangle to invalidate.
9120 *
9121 * @hide
9122 */
9123 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9124 // We try only with the AttachInfo because there's no point in invalidating
9125 // if we are not attached to our window
9126 final AttachInfo attachInfo = mAttachInfo;
9127 if (attachInfo != null) {
9128 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9129 info.target = this;
9130 info.left = left;
9131 info.top = top;
9132 info.right = right;
9133 info.bottom = bottom;
9134
9135 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9136 }
9137 }
9138
9139 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009140 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9141 * This event is sent at most once every
9142 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9143 */
9144 private void postSendViewScrolledAccessibilityEventCallback() {
9145 if (mSendViewScrolledAccessibilityEvent == null) {
9146 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9147 }
9148 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9149 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9150 postDelayed(mSendViewScrolledAccessibilityEvent,
9151 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9152 }
9153 }
9154
9155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 * Called by a parent to request that a child update its values for mScrollX
9157 * and mScrollY if necessary. This will typically be done if the child is
9158 * animating a scroll using a {@link android.widget.Scroller Scroller}
9159 * object.
9160 */
9161 public void computeScroll() {
9162 }
9163
9164 /**
9165 * <p>Indicate whether the horizontal edges are faded when the view is
9166 * scrolled horizontally.</p>
9167 *
9168 * @return true if the horizontal edges should are faded on scroll, false
9169 * otherwise
9170 *
9171 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009172 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 */
9174 public boolean isHorizontalFadingEdgeEnabled() {
9175 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9176 }
9177
9178 /**
9179 * <p>Define whether the horizontal edges should be faded when this view
9180 * is scrolled horizontally.</p>
9181 *
9182 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9183 * be faded when the view is scrolled
9184 * horizontally
9185 *
9186 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009187 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009188 */
9189 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9190 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9191 if (horizontalFadingEdgeEnabled) {
9192 initScrollCache();
9193 }
9194
9195 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9196 }
9197 }
9198
9199 /**
9200 * <p>Indicate whether the vertical edges are faded when the view is
9201 * scrolled horizontally.</p>
9202 *
9203 * @return true if the vertical edges should are faded on scroll, false
9204 * otherwise
9205 *
9206 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009207 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 */
9209 public boolean isVerticalFadingEdgeEnabled() {
9210 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9211 }
9212
9213 /**
9214 * <p>Define whether the vertical edges should be faded when this view
9215 * is scrolled vertically.</p>
9216 *
9217 * @param verticalFadingEdgeEnabled true if the vertical edges should
9218 * be faded when the view is scrolled
9219 * vertically
9220 *
9221 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009222 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009223 */
9224 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9225 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9226 if (verticalFadingEdgeEnabled) {
9227 initScrollCache();
9228 }
9229
9230 mViewFlags ^= FADING_EDGE_VERTICAL;
9231 }
9232 }
9233
9234 /**
9235 * Returns the strength, or intensity, of the top faded edge. The strength is
9236 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9237 * returns 0.0 or 1.0 but no value in between.
9238 *
9239 * Subclasses should override this method to provide a smoother fade transition
9240 * when scrolling occurs.
9241 *
9242 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9243 */
9244 protected float getTopFadingEdgeStrength() {
9245 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9246 }
9247
9248 /**
9249 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9250 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9251 * returns 0.0 or 1.0 but no value in between.
9252 *
9253 * Subclasses should override this method to provide a smoother fade transition
9254 * when scrolling occurs.
9255 *
9256 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9257 */
9258 protected float getBottomFadingEdgeStrength() {
9259 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9260 computeVerticalScrollRange() ? 1.0f : 0.0f;
9261 }
9262
9263 /**
9264 * Returns the strength, or intensity, of the left faded edge. The strength is
9265 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9266 * returns 0.0 or 1.0 but no value in between.
9267 *
9268 * Subclasses should override this method to provide a smoother fade transition
9269 * when scrolling occurs.
9270 *
9271 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9272 */
9273 protected float getLeftFadingEdgeStrength() {
9274 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9275 }
9276
9277 /**
9278 * Returns the strength, or intensity, of the right faded edge. The strength is
9279 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9280 * returns 0.0 or 1.0 but no value in between.
9281 *
9282 * Subclasses should override this method to provide a smoother fade transition
9283 * when scrolling occurs.
9284 *
9285 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9286 */
9287 protected float getRightFadingEdgeStrength() {
9288 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9289 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9290 }
9291
9292 /**
9293 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9294 * scrollbar is not drawn by default.</p>
9295 *
9296 * @return true if the horizontal scrollbar should be painted, false
9297 * otherwise
9298 *
9299 * @see #setHorizontalScrollBarEnabled(boolean)
9300 */
9301 public boolean isHorizontalScrollBarEnabled() {
9302 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9303 }
9304
9305 /**
9306 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9307 * scrollbar is not drawn by default.</p>
9308 *
9309 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9310 * be painted
9311 *
9312 * @see #isHorizontalScrollBarEnabled()
9313 */
9314 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9315 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9316 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009317 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009318 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 }
9320 }
9321
9322 /**
9323 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9324 * scrollbar is not drawn by default.</p>
9325 *
9326 * @return true if the vertical scrollbar should be painted, false
9327 * otherwise
9328 *
9329 * @see #setVerticalScrollBarEnabled(boolean)
9330 */
9331 public boolean isVerticalScrollBarEnabled() {
9332 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9333 }
9334
9335 /**
9336 * <p>Define whether the vertical scrollbar should be drawn or not. The
9337 * scrollbar is not drawn by default.</p>
9338 *
9339 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9340 * be painted
9341 *
9342 * @see #isVerticalScrollBarEnabled()
9343 */
9344 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9345 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9346 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009347 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009348 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 }
9350 }
9351
Adam Powell20232d02010-12-08 21:08:53 -08009352 /**
9353 * @hide
9354 */
9355 protected void recomputePadding() {
9356 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 }
Joe Malin32736f02011-01-19 16:14:20 -08009358
Mike Cleronfe81d382009-09-28 14:22:16 -07009359 /**
9360 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009361 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009362 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009363 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009364 */
9365 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9366 initScrollCache();
9367 final ScrollabilityCache scrollabilityCache = mScrollCache;
9368 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009369 if (fadeScrollbars) {
9370 scrollabilityCache.state = ScrollabilityCache.OFF;
9371 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009372 scrollabilityCache.state = ScrollabilityCache.ON;
9373 }
9374 }
Joe Malin32736f02011-01-19 16:14:20 -08009375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 /**
Joe Malin32736f02011-01-19 16:14:20 -08009377 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009378 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009379 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009380 * @return true if scrollbar fading is enabled
9381 */
9382 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009383 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009384 }
Joe Malin32736f02011-01-19 16:14:20 -08009385
Mike Cleron52f0a642009-09-28 18:21:37 -07009386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9388 * inset. When inset, they add to the padding of the view. And the scrollbars
9389 * can be drawn inside the padding area or on the edge of the view. For example,
9390 * if a view has a background drawable and you want to draw the scrollbars
9391 * inside the padding specified by the drawable, you can use
9392 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9393 * appear at the edge of the view, ignoring the padding, then you can use
9394 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9395 * @param style the style of the scrollbars. Should be one of
9396 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9397 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9398 * @see #SCROLLBARS_INSIDE_OVERLAY
9399 * @see #SCROLLBARS_INSIDE_INSET
9400 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9401 * @see #SCROLLBARS_OUTSIDE_INSET
9402 */
9403 public void setScrollBarStyle(int style) {
9404 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9405 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009406 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009407 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009408 }
9409 }
9410
9411 /**
9412 * <p>Returns the current scrollbar style.</p>
9413 * @return the current scrollbar style
9414 * @see #SCROLLBARS_INSIDE_OVERLAY
9415 * @see #SCROLLBARS_INSIDE_INSET
9416 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9417 * @see #SCROLLBARS_OUTSIDE_INSET
9418 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009419 @ViewDebug.ExportedProperty(mapping = {
9420 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9421 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9422 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9423 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9424 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425 public int getScrollBarStyle() {
9426 return mViewFlags & SCROLLBARS_STYLE_MASK;
9427 }
9428
9429 /**
9430 * <p>Compute the horizontal range that the horizontal scrollbar
9431 * represents.</p>
9432 *
9433 * <p>The range is expressed in arbitrary units that must be the same as the
9434 * units used by {@link #computeHorizontalScrollExtent()} and
9435 * {@link #computeHorizontalScrollOffset()}.</p>
9436 *
9437 * <p>The default range is the drawing width of this view.</p>
9438 *
9439 * @return the total horizontal range represented by the horizontal
9440 * scrollbar
9441 *
9442 * @see #computeHorizontalScrollExtent()
9443 * @see #computeHorizontalScrollOffset()
9444 * @see android.widget.ScrollBarDrawable
9445 */
9446 protected int computeHorizontalScrollRange() {
9447 return getWidth();
9448 }
9449
9450 /**
9451 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9452 * within the horizontal range. This value is used to compute the position
9453 * of the thumb within the scrollbar's track.</p>
9454 *
9455 * <p>The range is expressed in arbitrary units that must be the same as the
9456 * units used by {@link #computeHorizontalScrollRange()} and
9457 * {@link #computeHorizontalScrollExtent()}.</p>
9458 *
9459 * <p>The default offset is the scroll offset of this view.</p>
9460 *
9461 * @return the horizontal offset of the scrollbar's thumb
9462 *
9463 * @see #computeHorizontalScrollRange()
9464 * @see #computeHorizontalScrollExtent()
9465 * @see android.widget.ScrollBarDrawable
9466 */
9467 protected int computeHorizontalScrollOffset() {
9468 return mScrollX;
9469 }
9470
9471 /**
9472 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9473 * within the horizontal range. This value is used to compute the length
9474 * of the thumb within the scrollbar's track.</p>
9475 *
9476 * <p>The range is expressed in arbitrary units that must be the same as the
9477 * units used by {@link #computeHorizontalScrollRange()} and
9478 * {@link #computeHorizontalScrollOffset()}.</p>
9479 *
9480 * <p>The default extent is the drawing width of this view.</p>
9481 *
9482 * @return the horizontal extent of the scrollbar's thumb
9483 *
9484 * @see #computeHorizontalScrollRange()
9485 * @see #computeHorizontalScrollOffset()
9486 * @see android.widget.ScrollBarDrawable
9487 */
9488 protected int computeHorizontalScrollExtent() {
9489 return getWidth();
9490 }
9491
9492 /**
9493 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9494 *
9495 * <p>The range is expressed in arbitrary units that must be the same as the
9496 * units used by {@link #computeVerticalScrollExtent()} and
9497 * {@link #computeVerticalScrollOffset()}.</p>
9498 *
9499 * @return the total vertical range represented by the vertical scrollbar
9500 *
9501 * <p>The default range is the drawing height of this view.</p>
9502 *
9503 * @see #computeVerticalScrollExtent()
9504 * @see #computeVerticalScrollOffset()
9505 * @see android.widget.ScrollBarDrawable
9506 */
9507 protected int computeVerticalScrollRange() {
9508 return getHeight();
9509 }
9510
9511 /**
9512 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9513 * within the horizontal range. This value is used to compute the position
9514 * of the thumb within the scrollbar's track.</p>
9515 *
9516 * <p>The range is expressed in arbitrary units that must be the same as the
9517 * units used by {@link #computeVerticalScrollRange()} and
9518 * {@link #computeVerticalScrollExtent()}.</p>
9519 *
9520 * <p>The default offset is the scroll offset of this view.</p>
9521 *
9522 * @return the vertical offset of the scrollbar's thumb
9523 *
9524 * @see #computeVerticalScrollRange()
9525 * @see #computeVerticalScrollExtent()
9526 * @see android.widget.ScrollBarDrawable
9527 */
9528 protected int computeVerticalScrollOffset() {
9529 return mScrollY;
9530 }
9531
9532 /**
9533 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9534 * within the vertical range. This value is used to compute the length
9535 * of the thumb within the scrollbar's track.</p>
9536 *
9537 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009538 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 * {@link #computeVerticalScrollOffset()}.</p>
9540 *
9541 * <p>The default extent is the drawing height of this view.</p>
9542 *
9543 * @return the vertical extent of the scrollbar's thumb
9544 *
9545 * @see #computeVerticalScrollRange()
9546 * @see #computeVerticalScrollOffset()
9547 * @see android.widget.ScrollBarDrawable
9548 */
9549 protected int computeVerticalScrollExtent() {
9550 return getHeight();
9551 }
9552
9553 /**
Adam Powell69159442011-06-13 17:53:06 -07009554 * Check if this view can be scrolled horizontally in a certain direction.
9555 *
9556 * @param direction Negative to check scrolling left, positive to check scrolling right.
9557 * @return true if this view can be scrolled in the specified direction, false otherwise.
9558 */
9559 public boolean canScrollHorizontally(int direction) {
9560 final int offset = computeHorizontalScrollOffset();
9561 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9562 if (range == 0) return false;
9563 if (direction < 0) {
9564 return offset > 0;
9565 } else {
9566 return offset < range - 1;
9567 }
9568 }
9569
9570 /**
9571 * Check if this view can be scrolled vertically in a certain direction.
9572 *
9573 * @param direction Negative to check scrolling up, positive to check scrolling down.
9574 * @return true if this view can be scrolled in the specified direction, false otherwise.
9575 */
9576 public boolean canScrollVertically(int direction) {
9577 final int offset = computeVerticalScrollOffset();
9578 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9579 if (range == 0) return false;
9580 if (direction < 0) {
9581 return offset > 0;
9582 } else {
9583 return offset < range - 1;
9584 }
9585 }
9586
9587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9589 * scrollbars are painted only if they have been awakened first.</p>
9590 *
9591 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009592 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009593 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009595 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 // scrollbars are drawn only when the animation is running
9597 final ScrollabilityCache cache = mScrollCache;
9598 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009599
Mike Cleronf116bf82009-09-27 19:14:12 -07009600 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009601
Mike Cleronf116bf82009-09-27 19:14:12 -07009602 if (state == ScrollabilityCache.OFF) {
9603 return;
9604 }
Joe Malin32736f02011-01-19 16:14:20 -08009605
Mike Cleronf116bf82009-09-27 19:14:12 -07009606 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009607
Mike Cleronf116bf82009-09-27 19:14:12 -07009608 if (state == ScrollabilityCache.FADING) {
9609 // We're fading -- get our fade interpolation
9610 if (cache.interpolatorValues == null) {
9611 cache.interpolatorValues = new float[1];
9612 }
Joe Malin32736f02011-01-19 16:14:20 -08009613
Mike Cleronf116bf82009-09-27 19:14:12 -07009614 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009615
Mike Cleronf116bf82009-09-27 19:14:12 -07009616 // Stops the animation if we're done
9617 if (cache.scrollBarInterpolator.timeToValues(values) ==
9618 Interpolator.Result.FREEZE_END) {
9619 cache.state = ScrollabilityCache.OFF;
9620 } else {
9621 cache.scrollBar.setAlpha(Math.round(values[0]));
9622 }
Joe Malin32736f02011-01-19 16:14:20 -08009623
9624 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009625 // drawing. We only want this when we're fading so that
9626 // we prevent excessive redraws
9627 invalidate = true;
9628 } else {
9629 // We're just on -- but we may have been fading before so
9630 // reset alpha
9631 cache.scrollBar.setAlpha(255);
9632 }
9633
Joe Malin32736f02011-01-19 16:14:20 -08009634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009635 final int viewFlags = mViewFlags;
9636
9637 final boolean drawHorizontalScrollBar =
9638 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9639 final boolean drawVerticalScrollBar =
9640 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9641 && !isVerticalScrollBarHidden();
9642
9643 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9644 final int width = mRight - mLeft;
9645 final int height = mBottom - mTop;
9646
9647 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648
Mike Reede8853fc2009-09-04 14:01:48 -04009649 final int scrollX = mScrollX;
9650 final int scrollY = mScrollY;
9651 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9652
Mike Cleronf116bf82009-09-27 19:14:12 -07009653 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009655 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009656 int size = scrollBar.getSize(false);
9657 if (size <= 0) {
9658 size = cache.scrollBarSize;
9659 }
9660
Mike Cleronf116bf82009-09-27 19:14:12 -07009661 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009662 computeHorizontalScrollOffset(),
9663 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009664 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009665 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009666 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009667 left = scrollX + (mPaddingLeft & inside);
9668 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9669 bottom = top + size;
9670 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9671 if (invalidate) {
9672 invalidate(left, top, right, bottom);
9673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 }
9675
9676 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009677 int size = scrollBar.getSize(true);
9678 if (size <= 0) {
9679 size = cache.scrollBarSize;
9680 }
9681
Mike Reede8853fc2009-09-04 14:01:48 -04009682 scrollBar.setParameters(computeVerticalScrollRange(),
9683 computeVerticalScrollOffset(),
9684 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009685 switch (mVerticalScrollbarPosition) {
9686 default:
9687 case SCROLLBAR_POSITION_DEFAULT:
9688 case SCROLLBAR_POSITION_RIGHT:
9689 left = scrollX + width - size - (mUserPaddingRight & inside);
9690 break;
9691 case SCROLLBAR_POSITION_LEFT:
9692 left = scrollX + (mUserPaddingLeft & inside);
9693 break;
9694 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009695 top = scrollY + (mPaddingTop & inside);
9696 right = left + size;
9697 bottom = scrollY + height - (mUserPaddingBottom & inside);
9698 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9699 if (invalidate) {
9700 invalidate(left, top, right, bottom);
9701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 }
9703 }
9704 }
9705 }
Romain Guy8506ab42009-06-11 17:35:47 -07009706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009708 * 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 -08009709 * FastScroller is visible.
9710 * @return whether to temporarily hide the vertical scrollbar
9711 * @hide
9712 */
9713 protected boolean isVerticalScrollBarHidden() {
9714 return false;
9715 }
9716
9717 /**
9718 * <p>Draw the horizontal scrollbar if
9719 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9720 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 * @param canvas the canvas on which to draw the scrollbar
9722 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 *
9724 * @see #isHorizontalScrollBarEnabled()
9725 * @see #computeHorizontalScrollRange()
9726 * @see #computeHorizontalScrollExtent()
9727 * @see #computeHorizontalScrollOffset()
9728 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009729 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009730 */
Romain Guy8fb95422010-08-17 18:38:51 -07009731 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9732 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009733 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009734 scrollBar.draw(canvas);
9735 }
Mike Reede8853fc2009-09-04 14:01:48 -04009736
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9739 * returns true.</p>
9740 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009741 * @param canvas the canvas on which to draw the scrollbar
9742 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009743 *
9744 * @see #isVerticalScrollBarEnabled()
9745 * @see #computeVerticalScrollRange()
9746 * @see #computeVerticalScrollExtent()
9747 * @see #computeVerticalScrollOffset()
9748 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009749 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 */
Romain Guy8fb95422010-08-17 18:38:51 -07009751 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9752 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009753 scrollBar.setBounds(l, t, r, b);
9754 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 }
9756
9757 /**
9758 * Implement this to do your drawing.
9759 *
9760 * @param canvas the canvas on which the background will be drawn
9761 */
9762 protected void onDraw(Canvas canvas) {
9763 }
9764
9765 /*
9766 * Caller is responsible for calling requestLayout if necessary.
9767 * (This allows addViewInLayout to not request a new layout.)
9768 */
9769 void assignParent(ViewParent parent) {
9770 if (mParent == null) {
9771 mParent = parent;
9772 } else if (parent == null) {
9773 mParent = null;
9774 } else {
9775 throw new RuntimeException("view " + this + " being added, but"
9776 + " it already has a parent");
9777 }
9778 }
9779
9780 /**
9781 * This is called when the view is attached to a window. At this point it
9782 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009783 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9784 * however it may be called any time before the first onDraw -- including
9785 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 *
9787 * @see #onDetachedFromWindow()
9788 */
9789 protected void onAttachedToWindow() {
9790 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9791 mParent.requestTransparentRegion(this);
9792 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009793 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9794 initialAwakenScrollBars();
9795 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9796 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009797 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009798 // Order is important here: LayoutDirection MUST be resolved before Padding
9799 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009800 resolveLayoutDirectionIfNeeded();
9801 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009802 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009803 if (isFocused()) {
9804 InputMethodManager imm = InputMethodManager.peekInstance();
9805 imm.focusIn(this);
9806 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009807 }
Cibu Johny86666632010-02-22 13:01:02 -08009808
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009809 /**
Romain Guybb9908b2012-03-08 11:14:07 -08009810 * @see #onScreenStateChanged(int)
9811 */
9812 void dispatchScreenStateChanged(int screenState) {
9813 onScreenStateChanged(screenState);
9814 }
9815
9816 /**
9817 * This method is called whenever the state of the screen this view is
9818 * attached to changes. A state change will usually occurs when the screen
9819 * turns on or off (whether it happens automatically or the user does it
9820 * manually.)
9821 *
9822 * @param screenState The new state of the screen. Can be either
9823 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
9824 */
9825 public void onScreenStateChanged(int screenState) {
9826 }
9827
9828 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009829 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9830 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009831 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009832 private void resolveLayoutDirectionIfNeeded() {
9833 // Do not resolve if it is not needed
9834 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9835
9836 // Clear any previous layout direction resolution
9837 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9838
9839 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009840 switch (getLayoutDirection()) {
9841 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009842 // We cannot do the resolution if there is no parent
9843 if (mParent == null) return;
9844
Cibu Johny86666632010-02-22 13:01:02 -08009845 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009846 if (mParent instanceof ViewGroup) {
9847 ViewGroup viewGroup = ((ViewGroup) mParent);
9848
9849 // Check if the parent view group can resolve
9850 if (! viewGroup.canResolveLayoutDirection()) {
9851 return;
9852 }
9853 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9854 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9855 }
Cibu Johny86666632010-02-22 13:01:02 -08009856 }
9857 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009858 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009859 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009860 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009861 case LAYOUT_DIRECTION_LOCALE:
9862 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009863 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009864 }
9865 break;
9866 default:
9867 // Nothing to do, LTR by default
9868 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009869
9870 // Set to resolved
9871 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009872 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009873 // Resolve padding
9874 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009875 }
9876
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009877 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009878 * Called when layout direction has been resolved.
9879 *
9880 * The default implementation does nothing.
9881 */
9882 public void onResolvedLayoutDirectionChanged() {
9883 }
9884
9885 /**
9886 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009887 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009888 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009889 // If the user specified the absolute padding (either with android:padding or
9890 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9891 // use the default padding or the padding from the background drawable
9892 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009893 int resolvedLayoutDirection = getResolvedLayoutDirection();
9894 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009895 case LAYOUT_DIRECTION_RTL:
9896 // Start user padding override Right user padding. Otherwise, if Right user
9897 // padding is not defined, use the default Right padding. If Right user padding
9898 // is defined, just use it.
9899 if (mUserPaddingStart >= 0) {
9900 mUserPaddingRight = mUserPaddingStart;
9901 } else if (mUserPaddingRight < 0) {
9902 mUserPaddingRight = mPaddingRight;
9903 }
9904 if (mUserPaddingEnd >= 0) {
9905 mUserPaddingLeft = mUserPaddingEnd;
9906 } else if (mUserPaddingLeft < 0) {
9907 mUserPaddingLeft = mPaddingLeft;
9908 }
9909 break;
9910 case LAYOUT_DIRECTION_LTR:
9911 default:
9912 // Start user padding override Left user padding. Otherwise, if Left user
9913 // padding is not defined, use the default left padding. If Left user padding
9914 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009915 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009916 mUserPaddingLeft = mUserPaddingStart;
9917 } else if (mUserPaddingLeft < 0) {
9918 mUserPaddingLeft = mPaddingLeft;
9919 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009920 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009921 mUserPaddingRight = mUserPaddingEnd;
9922 } else if (mUserPaddingRight < 0) {
9923 mUserPaddingRight = mPaddingRight;
9924 }
9925 }
9926
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009927 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9928
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009929 if(isPaddingRelative()) {
9930 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
9931 } else {
9932 recomputePadding();
9933 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009934 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009935 }
9936
9937 /**
9938 * Resolve padding depending on the layout direction. Subclasses that care about
9939 * padding resolution should override this method. The default implementation does
9940 * nothing.
9941 *
9942 * @param layoutDirection the direction of the layout
9943 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -08009944 * @see {@link #LAYOUT_DIRECTION_LTR}
9945 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009946 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009947 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009948 }
9949
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009950 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009951 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009952 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009953 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009954 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009955 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009956 switch (getLayoutDirection()) {
9957 case LAYOUT_DIRECTION_INHERIT:
9958 return (mParent != null);
9959 default:
9960 return true;
9961 }
9962 }
9963
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009964 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009965 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9966 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009967 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009968 public void resetResolvedLayoutDirection() {
9969 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009970 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009971 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009972 // Reset also the text direction
9973 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009974 }
9975
9976 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009977 * Called during reset of resolved layout direction.
9978 *
9979 * Subclasses need to override this method to clear cached information that depends on the
9980 * resolved layout direction, or to inform child views that inherit their layout direction.
9981 *
9982 * The default implementation does nothing.
9983 */
9984 public void onResolvedLayoutDirectionReset() {
9985 }
9986
9987 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009988 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009989 *
9990 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009991 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009992 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009993 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009994 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 }
9996
9997 /**
9998 * This is called when the view is detached from a window. At this point it
9999 * no longer has a surface for drawing.
10000 *
10001 * @see #onAttachedToWindow()
10002 */
10003 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010004 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010005
Romain Guya440b002010-02-24 15:57:54 -080010006 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010007 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010008 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010009 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010012
Romain Guy6d7475d2011-07-27 16:28:21 -070010013 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010014
Chet Haasedaf98e92011-01-10 14:10:36 -080010015 if (mDisplayList != null) {
10016 mDisplayList.invalidate();
10017 }
10018
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010019 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010020 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010021 }
10022
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010023 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010024
10025 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 }
10027
10028 /**
10029 * @return The number of times this view has been attached to a window
10030 */
10031 protected int getWindowAttachCount() {
10032 return mWindowAttachCount;
10033 }
10034
10035 /**
10036 * Retrieve a unique token identifying the window this view is attached to.
10037 * @return Return the window's token for use in
10038 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10039 */
10040 public IBinder getWindowToken() {
10041 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10042 }
10043
10044 /**
10045 * Retrieve a unique token identifying the top-level "real" window of
10046 * the window that this view is attached to. That is, this is like
10047 * {@link #getWindowToken}, except if the window this view in is a panel
10048 * window (attached to another containing window), then the token of
10049 * the containing window is returned instead.
10050 *
10051 * @return Returns the associated window token, either
10052 * {@link #getWindowToken()} or the containing window's token.
10053 */
10054 public IBinder getApplicationWindowToken() {
10055 AttachInfo ai = mAttachInfo;
10056 if (ai != null) {
10057 IBinder appWindowToken = ai.mPanelParentWindowToken;
10058 if (appWindowToken == null) {
10059 appWindowToken = ai.mWindowToken;
10060 }
10061 return appWindowToken;
10062 }
10063 return null;
10064 }
10065
10066 /**
10067 * Retrieve private session object this view hierarchy is using to
10068 * communicate with the window manager.
10069 * @return the session object to communicate with the window manager
10070 */
10071 /*package*/ IWindowSession getWindowSession() {
10072 return mAttachInfo != null ? mAttachInfo.mSession : null;
10073 }
10074
10075 /**
10076 * @param info the {@link android.view.View.AttachInfo} to associated with
10077 * this view
10078 */
10079 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10080 //System.out.println("Attached! " + this);
10081 mAttachInfo = info;
10082 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010083 // We will need to evaluate the drawable state at least once.
10084 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 if (mFloatingTreeObserver != null) {
10086 info.mTreeObserver.merge(mFloatingTreeObserver);
10087 mFloatingTreeObserver = null;
10088 }
10089 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10090 mAttachInfo.mScrollContainers.add(this);
10091 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10092 }
10093 performCollectViewAttributes(visibility);
10094 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010095
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010096 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010097 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010098 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010099 if (listeners != null && listeners.size() > 0) {
10100 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10101 // perform the dispatching. The iterator is a safe guard against listeners that
10102 // could mutate the list by calling the various add/remove methods. This prevents
10103 // the array from being modified while we iterate it.
10104 for (OnAttachStateChangeListener listener : listeners) {
10105 listener.onViewAttachedToWindow(this);
10106 }
10107 }
10108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010109 int vis = info.mWindowVisibility;
10110 if (vis != GONE) {
10111 onWindowVisibilityChanged(vis);
10112 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010113 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10114 // If nobody has evaluated the drawable state yet, then do it now.
10115 refreshDrawableState();
10116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010117 }
10118
10119 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010120 AttachInfo info = mAttachInfo;
10121 if (info != null) {
10122 int vis = info.mWindowVisibility;
10123 if (vis != GONE) {
10124 onWindowVisibilityChanged(GONE);
10125 }
10126 }
10127
10128 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010129
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010130 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010131 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010132 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010133 if (listeners != null && listeners.size() > 0) {
10134 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10135 // perform the dispatching. The iterator is a safe guard against listeners that
10136 // could mutate the list by calling the various add/remove methods. This prevents
10137 // the array from being modified while we iterate it.
10138 for (OnAttachStateChangeListener listener : listeners) {
10139 listener.onViewDetachedFromWindow(this);
10140 }
10141 }
10142
Romain Guy01d5edc2011-01-28 11:28:53 -080010143 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 mAttachInfo.mScrollContainers.remove(this);
10145 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10146 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 mAttachInfo = null;
10149 }
10150
10151 /**
10152 * Store this view hierarchy's frozen state into the given container.
10153 *
10154 * @param container The SparseArray in which to save the view's state.
10155 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010156 * @see #restoreHierarchyState(android.util.SparseArray)
10157 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10158 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010159 */
10160 public void saveHierarchyState(SparseArray<Parcelable> container) {
10161 dispatchSaveInstanceState(container);
10162 }
10163
10164 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010165 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10166 * this view and its children. May be overridden to modify how freezing happens to a
10167 * 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 -080010168 *
10169 * @param container The SparseArray in which to save the view's state.
10170 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010171 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10172 * @see #saveHierarchyState(android.util.SparseArray)
10173 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 */
10175 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10176 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10177 mPrivateFlags &= ~SAVE_STATE_CALLED;
10178 Parcelable state = onSaveInstanceState();
10179 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10180 throw new IllegalStateException(
10181 "Derived class did not call super.onSaveInstanceState()");
10182 }
10183 if (state != null) {
10184 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10185 // + ": " + state);
10186 container.put(mID, state);
10187 }
10188 }
10189 }
10190
10191 /**
10192 * Hook allowing a view to generate a representation of its internal state
10193 * that can later be used to create a new instance with that same state.
10194 * This state should only contain information that is not persistent or can
10195 * not be reconstructed later. For example, you will never store your
10196 * current position on screen because that will be computed again when a
10197 * new instance of the view is placed in its view hierarchy.
10198 * <p>
10199 * Some examples of things you may store here: the current cursor position
10200 * in a text view (but usually not the text itself since that is stored in a
10201 * content provider or other persistent storage), the currently selected
10202 * item in a list view.
10203 *
10204 * @return Returns a Parcelable object containing the view's current dynamic
10205 * state, or null if there is nothing interesting to save. The
10206 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010207 * @see #onRestoreInstanceState(android.os.Parcelable)
10208 * @see #saveHierarchyState(android.util.SparseArray)
10209 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210 * @see #setSaveEnabled(boolean)
10211 */
10212 protected Parcelable onSaveInstanceState() {
10213 mPrivateFlags |= SAVE_STATE_CALLED;
10214 return BaseSavedState.EMPTY_STATE;
10215 }
10216
10217 /**
10218 * Restore this view hierarchy's frozen state from the given container.
10219 *
10220 * @param container The SparseArray which holds previously frozen states.
10221 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010222 * @see #saveHierarchyState(android.util.SparseArray)
10223 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10224 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 */
10226 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10227 dispatchRestoreInstanceState(container);
10228 }
10229
10230 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010231 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10232 * state for this view and its children. May be overridden to modify how restoring
10233 * happens to a view's children; for example, some views may want to not store state
10234 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 *
10236 * @param container The SparseArray which holds previously saved state.
10237 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010238 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10239 * @see #restoreHierarchyState(android.util.SparseArray)
10240 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 */
10242 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10243 if (mID != NO_ID) {
10244 Parcelable state = container.get(mID);
10245 if (state != null) {
10246 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10247 // + ": " + state);
10248 mPrivateFlags &= ~SAVE_STATE_CALLED;
10249 onRestoreInstanceState(state);
10250 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10251 throw new IllegalStateException(
10252 "Derived class did not call super.onRestoreInstanceState()");
10253 }
10254 }
10255 }
10256 }
10257
10258 /**
10259 * Hook allowing a view to re-apply a representation of its internal state that had previously
10260 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10261 * null state.
10262 *
10263 * @param state The frozen state that had previously been returned by
10264 * {@link #onSaveInstanceState}.
10265 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010266 * @see #onSaveInstanceState()
10267 * @see #restoreHierarchyState(android.util.SparseArray)
10268 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 */
10270 protected void onRestoreInstanceState(Parcelable state) {
10271 mPrivateFlags |= SAVE_STATE_CALLED;
10272 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010273 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10274 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010275 + "when two views of different type have the same id in the same hierarchy. "
10276 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010277 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 }
10279 }
10280
10281 /**
10282 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10283 *
10284 * @return the drawing start time in milliseconds
10285 */
10286 public long getDrawingTime() {
10287 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10288 }
10289
10290 /**
10291 * <p>Enables or disables the duplication of the parent's state into this view. When
10292 * duplication is enabled, this view gets its drawable state from its parent rather
10293 * than from its own internal properties.</p>
10294 *
10295 * <p>Note: in the current implementation, setting this property to true after the
10296 * view was added to a ViewGroup might have no effect at all. This property should
10297 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10298 *
10299 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10300 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010301 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010302 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10303 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 *
10305 * @param enabled True to enable duplication of the parent's drawable state, false
10306 * to disable it.
10307 *
10308 * @see #getDrawableState()
10309 * @see #isDuplicateParentStateEnabled()
10310 */
10311 public void setDuplicateParentStateEnabled(boolean enabled) {
10312 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10313 }
10314
10315 /**
10316 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10317 *
10318 * @return True if this view's drawable state is duplicated from the parent,
10319 * false otherwise
10320 *
10321 * @see #getDrawableState()
10322 * @see #setDuplicateParentStateEnabled(boolean)
10323 */
10324 public boolean isDuplicateParentStateEnabled() {
10325 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10326 }
10327
10328 /**
Romain Guy171c5922011-01-06 10:04:23 -080010329 * <p>Specifies the type of layer backing this view. The layer can be
10330 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10331 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010332 *
Romain Guy171c5922011-01-06 10:04:23 -080010333 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10334 * instance that controls how the layer is composed on screen. The following
10335 * properties of the paint are taken into account when composing the layer:</p>
10336 * <ul>
10337 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10338 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10339 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10340 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010341 *
Romain Guy171c5922011-01-06 10:04:23 -080010342 * <p>If this view has an alpha value set to < 1.0 by calling
10343 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10344 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10345 * equivalent to setting a hardware layer on this view and providing a paint with
10346 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010347 *
Romain Guy171c5922011-01-06 10:04:23 -080010348 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10349 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10350 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010351 *
Romain Guy171c5922011-01-06 10:04:23 -080010352 * @param layerType The ype of layer to use with this view, must be one of
10353 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10354 * {@link #LAYER_TYPE_HARDWARE}
10355 * @param paint The paint used to compose the layer. This argument is optional
10356 * and can be null. It is ignored when the layer type is
10357 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010358 *
10359 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010360 * @see #LAYER_TYPE_NONE
10361 * @see #LAYER_TYPE_SOFTWARE
10362 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010363 * @see #setAlpha(float)
10364 *
Romain Guy171c5922011-01-06 10:04:23 -080010365 * @attr ref android.R.styleable#View_layerType
10366 */
10367 public void setLayerType(int layerType, Paint paint) {
10368 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010369 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010370 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10371 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010372
Romain Guyd6cd5722011-01-17 14:42:41 -080010373 if (layerType == mLayerType) {
10374 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10375 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010376 invalidateParentCaches();
10377 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010378 }
10379 return;
10380 }
Romain Guy171c5922011-01-06 10:04:23 -080010381
10382 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010383 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010384 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010385 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010386 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010387 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010388 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010389 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010390 default:
10391 break;
Romain Guy171c5922011-01-06 10:04:23 -080010392 }
10393
10394 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010395 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10396 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10397 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010398
Romain Guy0fd89bf2011-01-26 15:41:30 -080010399 invalidateParentCaches();
10400 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010401 }
10402
10403 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010404 * Indicates whether this view has a static layer. A view with layer type
10405 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10406 * dynamic.
10407 */
10408 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010409 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010410 }
10411
10412 /**
Romain Guy171c5922011-01-06 10:04:23 -080010413 * Indicates what type of layer is currently associated with this view. By default
10414 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10415 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10416 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010417 *
Romain Guy171c5922011-01-06 10:04:23 -080010418 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10419 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010420 *
10421 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010422 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010423 * @see #LAYER_TYPE_NONE
10424 * @see #LAYER_TYPE_SOFTWARE
10425 * @see #LAYER_TYPE_HARDWARE
10426 */
10427 public int getLayerType() {
10428 return mLayerType;
10429 }
Joe Malin32736f02011-01-19 16:14:20 -080010430
Romain Guy6c319ca2011-01-11 14:29:25 -080010431 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010432 * Forces this view's layer to be created and this view to be rendered
10433 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10434 * invoking this method will have no effect.
10435 *
10436 * This method can for instance be used to render a view into its layer before
10437 * starting an animation. If this view is complex, rendering into the layer
10438 * before starting the animation will avoid skipping frames.
10439 *
10440 * @throws IllegalStateException If this view is not attached to a window
10441 *
10442 * @see #setLayerType(int, android.graphics.Paint)
10443 */
10444 public void buildLayer() {
10445 if (mLayerType == LAYER_TYPE_NONE) return;
10446
10447 if (mAttachInfo == null) {
10448 throw new IllegalStateException("This view must be attached to a window first");
10449 }
10450
10451 switch (mLayerType) {
10452 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010453 if (mAttachInfo.mHardwareRenderer != null &&
10454 mAttachInfo.mHardwareRenderer.isEnabled() &&
10455 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010456 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010457 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010458 break;
10459 case LAYER_TYPE_SOFTWARE:
10460 buildDrawingCache(true);
10461 break;
10462 }
10463 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010464
10465 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10466 void flushLayer() {
10467 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10468 mHardwareLayer.flush();
10469 }
10470 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010471
10472 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010473 * <p>Returns a hardware layer that can be used to draw this view again
10474 * without executing its draw method.</p>
10475 *
10476 * @return A HardwareLayer ready to render, or null if an error occurred.
10477 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010478 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010479 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10480 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010481 return null;
10482 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010483
10484 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010485
10486 final int width = mRight - mLeft;
10487 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010488
Romain Guy6c319ca2011-01-11 14:29:25 -080010489 if (width == 0 || height == 0) {
10490 return null;
10491 }
10492
10493 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10494 if (mHardwareLayer == null) {
10495 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10496 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010497 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010498 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10499 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010500 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010501 }
10502
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010503 // The layer is not valid if the underlying GPU resources cannot be allocated
10504 if (!mHardwareLayer.isValid()) {
10505 return null;
10506 }
10507
Chet Haasea1cff502012-02-21 13:43:44 -080010508 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010509 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010510 }
10511
10512 return mHardwareLayer;
10513 }
Romain Guy171c5922011-01-06 10:04:23 -080010514
Romain Guy589b0bb2011-10-10 13:57:47 -070010515 /**
10516 * Destroys this View's hardware layer if possible.
10517 *
10518 * @return True if the layer was destroyed, false otherwise.
10519 *
10520 * @see #setLayerType(int, android.graphics.Paint)
10521 * @see #LAYER_TYPE_HARDWARE
10522 */
Romain Guy65b345f2011-07-27 18:51:50 -070010523 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010524 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010525 AttachInfo info = mAttachInfo;
10526 if (info != null && info.mHardwareRenderer != null &&
10527 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10528 mHardwareLayer.destroy();
10529 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010530
Romain Guy9c4b79a2011-11-10 19:23:58 -080010531 invalidate(true);
10532 invalidateParentCaches();
10533 }
Romain Guy65b345f2011-07-27 18:51:50 -070010534 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010535 }
Romain Guy65b345f2011-07-27 18:51:50 -070010536 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010537 }
10538
Romain Guy171c5922011-01-06 10:04:23 -080010539 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010540 * Destroys all hardware rendering resources. This method is invoked
10541 * when the system needs to reclaim resources. Upon execution of this
10542 * method, you should free any OpenGL resources created by the view.
10543 *
10544 * Note: you <strong>must</strong> call
10545 * <code>super.destroyHardwareResources()</code> when overriding
10546 * this method.
10547 *
10548 * @hide
10549 */
10550 protected void destroyHardwareResources() {
10551 destroyLayer();
10552 }
10553
10554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010555 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10556 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10557 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10558 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10559 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10560 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010561 *
Romain Guy171c5922011-01-06 10:04:23 -080010562 * <p>Enabling the drawing cache is similar to
10563 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010564 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10565 * drawing cache has no effect on rendering because the system uses a different mechanism
10566 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10567 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10568 * for information on how to enable software and hardware layers.</p>
10569 *
10570 * <p>This API can be used to manually generate
10571 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10572 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 *
10574 * @param enabled true to enable the drawing cache, false otherwise
10575 *
10576 * @see #isDrawingCacheEnabled()
10577 * @see #getDrawingCache()
10578 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010579 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 */
10581 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010582 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10584 }
10585
10586 /**
10587 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10588 *
10589 * @return true if the drawing cache is enabled
10590 *
10591 * @see #setDrawingCacheEnabled(boolean)
10592 * @see #getDrawingCache()
10593 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010594 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 public boolean isDrawingCacheEnabled() {
10596 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10597 }
10598
10599 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010600 * Debugging utility which recursively outputs the dirty state of a view and its
10601 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010602 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010603 * @hide
10604 */
Romain Guy676b1732011-02-14 14:45:33 -080010605 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010606 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10607 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10608 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10609 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10610 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10611 if (clear) {
10612 mPrivateFlags &= clearMask;
10613 }
10614 if (this instanceof ViewGroup) {
10615 ViewGroup parent = (ViewGroup) this;
10616 final int count = parent.getChildCount();
10617 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010618 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010619 child.outputDirtyFlags(indent + " ", clear, clearMask);
10620 }
10621 }
10622 }
10623
10624 /**
10625 * This method is used by ViewGroup to cause its children to restore or recreate their
10626 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10627 * to recreate its own display list, which would happen if it went through the normal
10628 * draw/dispatchDraw mechanisms.
10629 *
10630 * @hide
10631 */
10632 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010633
10634 /**
10635 * A view that is not attached or hardware accelerated cannot create a display list.
10636 * This method checks these conditions and returns the appropriate result.
10637 *
10638 * @return true if view has the ability to create a display list, false otherwise.
10639 *
10640 * @hide
10641 */
10642 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010643 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010644 }
Joe Malin32736f02011-01-19 16:14:20 -080010645
Chet Haasedaf98e92011-01-10 14:10:36 -080010646 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010647 * @return The HardwareRenderer associated with that view or null if hardware rendering
10648 * is not supported or this this has not been attached to a window.
10649 *
10650 * @hide
10651 */
10652 public HardwareRenderer getHardwareRenderer() {
10653 if (mAttachInfo != null) {
10654 return mAttachInfo.mHardwareRenderer;
10655 }
10656 return null;
10657 }
10658
10659 /**
Chet Haasea1cff502012-02-21 13:43:44 -080010660 * Returns a DisplayList. If the incoming displayList is null, one will be created.
10661 * Otherwise, the same display list will be returned (after having been rendered into
10662 * along the way, depending on the invalidation state of the view).
10663 *
10664 * @param displayList The previous version of this displayList, could be null.
10665 * @param isLayer Whether the requester of the display list is a layer. If so,
10666 * the view will avoid creating a layer inside the resulting display list.
10667 * @return A new or reused DisplayList object.
10668 */
10669 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
10670 if (!canHaveDisplayList()) {
10671 return null;
10672 }
10673
10674 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10675 displayList == null || !displayList.isValid() ||
10676 (!isLayer && mRecreateDisplayList))) {
10677 // Don't need to recreate the display list, just need to tell our
10678 // children to restore/recreate theirs
10679 if (displayList != null && displayList.isValid() &&
10680 !isLayer && !mRecreateDisplayList) {
10681 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10682 mPrivateFlags &= ~DIRTY_MASK;
10683 dispatchGetDisplayList();
10684
10685 return displayList;
10686 }
10687
10688 if (!isLayer) {
10689 // If we got here, we're recreating it. Mark it as such to ensure that
10690 // we copy in child display lists into ours in drawChild()
10691 mRecreateDisplayList = true;
10692 }
10693 if (displayList == null) {
10694 final String name = getClass().getSimpleName();
10695 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
10696 // If we're creating a new display list, make sure our parent gets invalidated
10697 // since they will need to recreate their display list to account for this
10698 // new child display list.
10699 invalidateParentCaches();
10700 }
10701
10702 boolean caching = false;
10703 final HardwareCanvas canvas = displayList.start();
10704 int restoreCount = 0;
10705 int width = mRight - mLeft;
10706 int height = mBottom - mTop;
10707
10708 try {
10709 canvas.setViewport(width, height);
10710 // The dirty rect should always be null for a display list
10711 canvas.onPreDraw(null);
10712 int layerType = (
10713 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
10714 getLayerType() : LAYER_TYPE_NONE;
10715 if (!isLayer && layerType == LAYER_TYPE_HARDWARE && USE_DISPLAY_LIST_PROPERTIES) {
10716 final HardwareLayer layer = getHardwareLayer();
10717 if (layer != null && layer.isValid()) {
10718 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
10719 } else {
10720 canvas.saveLayer(0, 0,
10721 mRight - mLeft, mBottom - mTop, mLayerPaint,
10722 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
10723 }
10724 caching = true;
10725 } else {
10726
10727 computeScroll();
10728
10729 if (!USE_DISPLAY_LIST_PROPERTIES) {
10730 restoreCount = canvas.save();
10731 }
10732 canvas.translate(-mScrollX, -mScrollY);
10733 if (!isLayer) {
10734 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10735 mPrivateFlags &= ~DIRTY_MASK;
10736 }
10737
10738 // Fast path for layouts with no backgrounds
10739 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10740 dispatchDraw(canvas);
10741 } else {
10742 draw(canvas);
10743 }
10744 }
10745 } finally {
10746 if (USE_DISPLAY_LIST_PROPERTIES) {
10747 canvas.restoreToCount(restoreCount);
10748 }
10749 canvas.onPostDraw();
10750
10751 displayList.end();
10752 if (USE_DISPLAY_LIST_PROPERTIES) {
10753 displayList.setCaching(caching);
10754 }
10755 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
10756 displayList.setLeftTopRightBottom(0, 0, width, height);
10757 } else {
10758 setDisplayListProperties(displayList);
10759 }
10760 }
10761 } else if (!isLayer) {
10762 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10763 mPrivateFlags &= ~DIRTY_MASK;
10764 }
10765
10766 return displayList;
10767 }
10768
10769 /**
10770 * Get the DisplayList for the HardwareLayer
10771 *
10772 * @param layer The HardwareLayer whose DisplayList we want
10773 * @return A DisplayList fopr the specified HardwareLayer
10774 */
10775 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
10776 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
10777 layer.setDisplayList(displayList);
10778 return displayList;
10779 }
10780
10781
10782 /**
Romain Guyb051e892010-09-28 19:09:36 -070010783 * <p>Returns a display list that can be used to draw this view again
10784 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010785 *
Romain Guyb051e892010-09-28 19:09:36 -070010786 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010787 *
10788 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010789 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010790 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080010791 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070010792 return mDisplayList;
10793 }
10794
10795 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010796 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010797 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010798 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010799 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010800 * @see #getDrawingCache(boolean)
10801 */
10802 public Bitmap getDrawingCache() {
10803 return getDrawingCache(false);
10804 }
10805
10806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010807 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10808 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10809 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10810 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10811 * request the drawing cache by calling this method and draw it on screen if the
10812 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010813 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010814 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10815 * this method will create a bitmap of the same size as this view. Because this bitmap
10816 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10817 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10818 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10819 * size than the view. This implies that your application must be able to handle this
10820 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010821 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010822 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10823 * the current density of the screen when the application is in compatibility
10824 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010826 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828 * @see #setDrawingCacheEnabled(boolean)
10829 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010830 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 * @see #destroyDrawingCache()
10832 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010833 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10835 return null;
10836 }
10837 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010838 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 }
Romain Guy02890fd2010-08-06 17:58:44 -070010840 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010841 }
10842
10843 /**
10844 * <p>Frees the resources used by the drawing cache. If you call
10845 * {@link #buildDrawingCache()} manually without calling
10846 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10847 * should cleanup the cache with this method afterwards.</p>
10848 *
10849 * @see #setDrawingCacheEnabled(boolean)
10850 * @see #buildDrawingCache()
10851 * @see #getDrawingCache()
10852 */
10853 public void destroyDrawingCache() {
10854 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010855 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 mDrawingCache = null;
10857 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010858 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010859 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010860 mUnscaledDrawingCache = null;
10861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 }
10863
10864 /**
10865 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010866 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 * view will always be drawn on top of a solid color.
10868 *
10869 * @param color The background color to use for the drawing cache's bitmap
10870 *
10871 * @see #setDrawingCacheEnabled(boolean)
10872 * @see #buildDrawingCache()
10873 * @see #getDrawingCache()
10874 */
10875 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010876 if (color != mDrawingCacheBackgroundColor) {
10877 mDrawingCacheBackgroundColor = color;
10878 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 }
10881
10882 /**
10883 * @see #setDrawingCacheBackgroundColor(int)
10884 *
10885 * @return The background color to used for the drawing cache's bitmap
10886 */
10887 public int getDrawingCacheBackgroundColor() {
10888 return mDrawingCacheBackgroundColor;
10889 }
10890
10891 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010892 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010893 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010894 * @see #buildDrawingCache(boolean)
10895 */
10896 public void buildDrawingCache() {
10897 buildDrawingCache(false);
10898 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010899
Romain Guyfbd8f692009-06-26 14:51:58 -070010900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10902 *
10903 * <p>If you call {@link #buildDrawingCache()} manually without calling
10904 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10905 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010906 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010907 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10908 * this method will create a bitmap of the same size as this view. Because this bitmap
10909 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10910 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10911 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10912 * size than the view. This implies that your application must be able to handle this
10913 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010914 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010915 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10916 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010917 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010918 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 *
10920 * @see #getDrawingCache()
10921 * @see #destroyDrawingCache()
10922 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010923 public void buildDrawingCache(boolean autoScale) {
10924 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010925 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010926 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927
10928 if (ViewDebug.TRACE_HIERARCHY) {
10929 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010931
Romain Guy8506ab42009-06-11 17:35:47 -070010932 int width = mRight - mLeft;
10933 int height = mBottom - mTop;
10934
10935 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010936 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010937
Romain Guye1123222009-06-29 14:24:56 -070010938 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010939 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10940 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010942
10943 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010944 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010945 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946
10947 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010948 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010949 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010950 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10951 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010952 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953 return;
10954 }
10955
10956 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010957 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958
10959 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 Bitmap.Config quality;
10961 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010962 // Never pick ARGB_4444 because it looks awful
10963 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010964 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10965 case DRAWING_CACHE_QUALITY_AUTO:
10966 quality = Bitmap.Config.ARGB_8888;
10967 break;
10968 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010969 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010970 break;
10971 case DRAWING_CACHE_QUALITY_HIGH:
10972 quality = Bitmap.Config.ARGB_8888;
10973 break;
10974 default:
10975 quality = Bitmap.Config.ARGB_8888;
10976 break;
10977 }
10978 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010979 // Optimization for translucent windows
10980 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010981 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 }
10983
10984 // Try to cleanup memory
10985 if (bitmap != null) bitmap.recycle();
10986
10987 try {
10988 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010989 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010990 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010991 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010992 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010993 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010994 }
Adam Powell26153a32010-11-08 15:22:27 -080010995 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996 } catch (OutOfMemoryError e) {
10997 // If there is not enough memory to create the bitmap cache, just
10998 // ignore the issue as bitmap caches are not required to draw the
10999 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011000 if (autoScale) {
11001 mDrawingCache = null;
11002 } else {
11003 mUnscaledDrawingCache = null;
11004 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011005 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 return;
11007 }
11008
11009 clear = drawingCacheBackgroundColor != 0;
11010 }
11011
11012 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 if (attachInfo != null) {
11014 canvas = attachInfo.mCanvas;
11015 if (canvas == null) {
11016 canvas = new Canvas();
11017 }
11018 canvas.setBitmap(bitmap);
11019 // Temporarily clobber the cached Canvas in case one of our children
11020 // is also using a drawing cache. Without this, the children would
11021 // steal the canvas by attaching their own bitmap to it and bad, bad
11022 // thing would happen (invisible views, corrupted drawings, etc.)
11023 attachInfo.mCanvas = null;
11024 } else {
11025 // This case should hopefully never or seldom happen
11026 canvas = new Canvas(bitmap);
11027 }
11028
11029 if (clear) {
11030 bitmap.eraseColor(drawingCacheBackgroundColor);
11031 }
11032
11033 computeScroll();
11034 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011035
Romain Guye1123222009-06-29 14:24:56 -070011036 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011037 final float scale = attachInfo.mApplicationScale;
11038 canvas.scale(scale, scale);
11039 }
Joe Malin32736f02011-01-19 16:14:20 -080011040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011041 canvas.translate(-mScrollX, -mScrollY);
11042
Romain Guy5bcdff42009-05-14 21:27:18 -070011043 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011044 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11045 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011046 mPrivateFlags |= DRAWING_CACHE_VALID;
11047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048
11049 // Fast path for layouts with no backgrounds
11050 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11051 if (ViewDebug.TRACE_HIERARCHY) {
11052 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11053 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011054 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 dispatchDraw(canvas);
11056 } else {
11057 draw(canvas);
11058 }
11059
11060 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011061 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062
11063 if (attachInfo != null) {
11064 // Restore the cached Canvas for our siblings
11065 attachInfo.mCanvas = canvas;
11066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011067 }
11068 }
11069
11070 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011071 * Create a snapshot of the view into a bitmap. We should probably make
11072 * some form of this public, but should think about the API.
11073 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011074 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011075 int width = mRight - mLeft;
11076 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011077
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011078 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011079 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011080 width = (int) ((width * scale) + 0.5f);
11081 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011082
Romain Guy8c11e312009-09-14 15:15:30 -070011083 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011084 if (bitmap == null) {
11085 throw new OutOfMemoryError();
11086 }
11087
Romain Guyc529d8d2011-09-06 15:01:39 -070011088 Resources resources = getResources();
11089 if (resources != null) {
11090 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11091 }
Joe Malin32736f02011-01-19 16:14:20 -080011092
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011093 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011094 if (attachInfo != null) {
11095 canvas = attachInfo.mCanvas;
11096 if (canvas == null) {
11097 canvas = new Canvas();
11098 }
11099 canvas.setBitmap(bitmap);
11100 // Temporarily clobber the cached Canvas in case one of our children
11101 // is also using a drawing cache. Without this, the children would
11102 // steal the canvas by attaching their own bitmap to it and bad, bad
11103 // things would happen (invisible views, corrupted drawings, etc.)
11104 attachInfo.mCanvas = null;
11105 } else {
11106 // This case should hopefully never or seldom happen
11107 canvas = new Canvas(bitmap);
11108 }
11109
Romain Guy5bcdff42009-05-14 21:27:18 -070011110 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011111 bitmap.eraseColor(backgroundColor);
11112 }
11113
11114 computeScroll();
11115 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011116 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011117 canvas.translate(-mScrollX, -mScrollY);
11118
Romain Guy5bcdff42009-05-14 21:27:18 -070011119 // Temporarily remove the dirty mask
11120 int flags = mPrivateFlags;
11121 mPrivateFlags &= ~DIRTY_MASK;
11122
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011123 // Fast path for layouts with no backgrounds
11124 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11125 dispatchDraw(canvas);
11126 } else {
11127 draw(canvas);
11128 }
11129
Romain Guy5bcdff42009-05-14 21:27:18 -070011130 mPrivateFlags = flags;
11131
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011132 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011133 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011134
11135 if (attachInfo != null) {
11136 // Restore the cached Canvas for our siblings
11137 attachInfo.mCanvas = canvas;
11138 }
Romain Guy8506ab42009-06-11 17:35:47 -070011139
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011140 return bitmap;
11141 }
11142
11143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 * Indicates whether this View is currently in edit mode. A View is usually
11145 * in edit mode when displayed within a developer tool. For instance, if
11146 * this View is being drawn by a visual user interface builder, this method
11147 * should return true.
11148 *
11149 * Subclasses should check the return value of this method to provide
11150 * different behaviors if their normal behavior might interfere with the
11151 * host environment. For instance: the class spawns a thread in its
11152 * constructor, the drawing code relies on device-specific features, etc.
11153 *
11154 * This method is usually checked in the drawing code of custom widgets.
11155 *
11156 * @return True if this View is in edit mode, false otherwise.
11157 */
11158 public boolean isInEditMode() {
11159 return false;
11160 }
11161
11162 /**
11163 * If the View draws content inside its padding and enables fading edges,
11164 * it needs to support padding offsets. Padding offsets are added to the
11165 * fading edges to extend the length of the fade so that it covers pixels
11166 * drawn inside the padding.
11167 *
11168 * Subclasses of this class should override this method if they need
11169 * to draw content inside the padding.
11170 *
11171 * @return True if padding offset must be applied, false otherwise.
11172 *
11173 * @see #getLeftPaddingOffset()
11174 * @see #getRightPaddingOffset()
11175 * @see #getTopPaddingOffset()
11176 * @see #getBottomPaddingOffset()
11177 *
11178 * @since CURRENT
11179 */
11180 protected boolean isPaddingOffsetRequired() {
11181 return false;
11182 }
11183
11184 /**
11185 * Amount by which to extend the left fading region. Called only when
11186 * {@link #isPaddingOffsetRequired()} returns true.
11187 *
11188 * @return The left padding offset in pixels.
11189 *
11190 * @see #isPaddingOffsetRequired()
11191 *
11192 * @since CURRENT
11193 */
11194 protected int getLeftPaddingOffset() {
11195 return 0;
11196 }
11197
11198 /**
11199 * Amount by which to extend the right fading region. Called only when
11200 * {@link #isPaddingOffsetRequired()} returns true.
11201 *
11202 * @return The right padding offset in pixels.
11203 *
11204 * @see #isPaddingOffsetRequired()
11205 *
11206 * @since CURRENT
11207 */
11208 protected int getRightPaddingOffset() {
11209 return 0;
11210 }
11211
11212 /**
11213 * Amount by which to extend the top fading region. Called only when
11214 * {@link #isPaddingOffsetRequired()} returns true.
11215 *
11216 * @return The top padding offset in pixels.
11217 *
11218 * @see #isPaddingOffsetRequired()
11219 *
11220 * @since CURRENT
11221 */
11222 protected int getTopPaddingOffset() {
11223 return 0;
11224 }
11225
11226 /**
11227 * Amount by which to extend the bottom fading region. Called only when
11228 * {@link #isPaddingOffsetRequired()} returns true.
11229 *
11230 * @return The bottom padding offset in pixels.
11231 *
11232 * @see #isPaddingOffsetRequired()
11233 *
11234 * @since CURRENT
11235 */
11236 protected int getBottomPaddingOffset() {
11237 return 0;
11238 }
11239
11240 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011241 * @hide
11242 * @param offsetRequired
11243 */
11244 protected int getFadeTop(boolean offsetRequired) {
11245 int top = mPaddingTop;
11246 if (offsetRequired) top += getTopPaddingOffset();
11247 return top;
11248 }
11249
11250 /**
11251 * @hide
11252 * @param offsetRequired
11253 */
11254 protected int getFadeHeight(boolean offsetRequired) {
11255 int padding = mPaddingTop;
11256 if (offsetRequired) padding += getTopPaddingOffset();
11257 return mBottom - mTop - mPaddingBottom - padding;
11258 }
11259
11260 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011261 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011262 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011263 *
Romain Guy2bffd262010-09-12 17:40:02 -070011264 * <p>Even if this method returns true, it does not mean that every call
11265 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11266 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011267 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011268 * window is hardware accelerated,
11269 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11270 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011271 *
Romain Guy2bffd262010-09-12 17:40:02 -070011272 * @return True if the view is attached to a window and the window is
11273 * hardware accelerated; false in any other case.
11274 */
11275 public boolean isHardwareAccelerated() {
11276 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11277 }
Joe Malin32736f02011-01-19 16:14:20 -080011278
Romain Guy2bffd262010-09-12 17:40:02 -070011279 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011280 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11281 * case of an active Animation being run on the view.
11282 */
11283 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11284 Animation a, boolean scalingRequired) {
11285 Transformation invalidationTransform;
11286 final int flags = parent.mGroupFlags;
11287 final boolean initialized = a.isInitialized();
11288 if (!initialized) {
11289 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11290 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11291 onAnimationStart();
11292 }
11293
11294 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11295 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11296 if (parent.mInvalidationTransformation == null) {
11297 parent.mInvalidationTransformation = new Transformation();
11298 }
11299 invalidationTransform = parent.mInvalidationTransformation;
11300 a.getTransformation(drawingTime, invalidationTransform, 1f);
11301 } else {
11302 invalidationTransform = parent.mChildTransformation;
11303 }
11304 if (more) {
11305 if (!a.willChangeBounds()) {
11306 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11307 parent.FLAG_OPTIMIZE_INVALIDATE) {
11308 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11309 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11310 // The child need to draw an animation, potentially offscreen, so
11311 // make sure we do not cancel invalidate requests
11312 parent.mPrivateFlags |= DRAW_ANIMATION;
11313 parent.invalidate(mLeft, mTop, mRight, mBottom);
11314 }
11315 } else {
11316 if (parent.mInvalidateRegion == null) {
11317 parent.mInvalidateRegion = new RectF();
11318 }
11319 final RectF region = parent.mInvalidateRegion;
11320 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11321 invalidationTransform);
11322
11323 // The child need to draw an animation, potentially offscreen, so
11324 // make sure we do not cancel invalidate requests
11325 parent.mPrivateFlags |= DRAW_ANIMATION;
11326
11327 final int left = mLeft + (int) region.left;
11328 final int top = mTop + (int) region.top;
11329 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11330 top + (int) (region.height() + .5f));
11331 }
11332 }
11333 return more;
11334 }
11335
Chet Haasea1cff502012-02-21 13:43:44 -080011336 void setDisplayListProperties() {
11337 setDisplayListProperties(mDisplayList);
11338 }
11339
11340 /**
11341 * This method is called by getDisplayList() when a display list is created or re-rendered.
11342 * It sets or resets the current value of all properties on that display list (resetting is
11343 * necessary when a display list is being re-created, because we need to make sure that
11344 * previously-set transform values
11345 */
11346 void setDisplayListProperties(DisplayList displayList) {
11347 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11348 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
11349 if (mParent instanceof ViewGroup) {
11350 displayList.setClipChildren(
11351 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11352 }
11353 if (mAttachInfo != null && mAttachInfo.mScalingRequired &&
11354 mAttachInfo.mApplicationScale != 1.0f) {
11355 displayList.setApplicationScale(1f / mAttachInfo.mApplicationScale);
11356 }
11357 if (mTransformationInfo != null) {
11358 displayList.setTransformationInfo(mTransformationInfo.mAlpha,
11359 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11360 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11361 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11362 mTransformationInfo.mScaleY);
11363 displayList.setCameraDistance(getCameraDistance());
11364 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11365 displayList.setPivotX(getPivotX());
11366 displayList.setPivotY(getPivotY());
11367 }
11368 }
11369 }
11370 }
11371
Chet Haasebcca79a2012-02-14 08:45:14 -080011372 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011373 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11374 * This draw() method is an implementation detail and is not intended to be overridden or
11375 * to be called from anywhere else other than ViewGroup.drawChild().
11376 */
11377 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011378 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11379 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011380 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011381 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011382 final int flags = parent.mGroupFlags;
11383
Chet Haasea1cff502012-02-21 13:43:44 -080011384 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011385 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011386 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011387 }
11388
11389 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011390 boolean concatMatrix = false;
11391
11392 boolean scalingRequired = false;
11393 boolean caching;
11394 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11395
11396 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011397 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11398 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011399 caching = true;
11400 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11401 } else {
11402 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11403 }
11404
Chet Haasebcca79a2012-02-14 08:45:14 -080011405 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011406 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011407 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011408 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011409 transformToApply = parent.mChildTransformation;
Chet Haasea1cff502012-02-21 13:43:44 -080011410 } else if ((flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011411 final boolean hasTransform =
11412 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011413 if (hasTransform) {
11414 final int transformType = parent.mChildTransformation.getTransformationType();
11415 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11416 parent.mChildTransformation : null;
11417 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11418 }
11419 }
11420
11421 concatMatrix |= !childHasIdentityMatrix;
11422
11423 // Sets the flag as early as possible to allow draw() implementations
11424 // to call invalidate() successfully when doing animations
11425 mPrivateFlags |= DRAWN;
11426
Chet Haasebcca79a2012-02-14 08:45:14 -080011427 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011428 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11429 return more;
11430 }
11431
11432 if (hardwareAccelerated) {
11433 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11434 // retain the flag's value temporarily in the mRecreateDisplayList flag
11435 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11436 mPrivateFlags &= ~INVALIDATED;
11437 }
11438
11439 computeScroll();
11440
11441 final int sx = mScrollX;
11442 final int sy = mScrollY;
11443
11444 DisplayList displayList = null;
11445 Bitmap cache = null;
11446 boolean hasDisplayList = false;
11447 if (caching) {
11448 if (!hardwareAccelerated) {
11449 if (layerType != LAYER_TYPE_NONE) {
11450 layerType = LAYER_TYPE_SOFTWARE;
11451 buildDrawingCache(true);
11452 }
11453 cache = getDrawingCache(true);
11454 } else {
11455 switch (layerType) {
11456 case LAYER_TYPE_SOFTWARE:
11457 buildDrawingCache(true);
11458 cache = getDrawingCache(true);
11459 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011460 case LAYER_TYPE_HARDWARE:
11461 if (useDisplayListProperties) {
11462 hasDisplayList = canHaveDisplayList();
11463 }
11464 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011465 case LAYER_TYPE_NONE:
11466 // Delay getting the display list until animation-driven alpha values are
11467 // set up and possibly passed on to the view
11468 hasDisplayList = canHaveDisplayList();
11469 break;
11470 }
11471 }
11472 }
Chet Haasea1cff502012-02-21 13:43:44 -080011473 useDisplayListProperties &= hasDisplayList;
Chet Haase64a48c12012-02-13 16:33:29 -080011474
11475 final boolean hasNoCache = cache == null || hasDisplayList;
11476 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11477 layerType != LAYER_TYPE_HARDWARE;
11478
Chet Haasea1cff502012-02-21 13:43:44 -080011479 int restoreTo = -1;
11480 if (!useDisplayListProperties) {
11481 restoreTo = canvas.save();
11482 }
Chet Haase64a48c12012-02-13 16:33:29 -080011483 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011484 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011485 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011486 if (!useDisplayListProperties) {
11487 canvas.translate(mLeft, mTop);
11488 }
Chet Haase64a48c12012-02-13 16:33:29 -080011489 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011490 if (useDisplayListProperties) {
11491 restoreTo = canvas.save();
11492 }
Chet Haase64a48c12012-02-13 16:33:29 -080011493 // mAttachInfo cannot be null, otherwise scalingRequired == false
11494 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11495 canvas.scale(scale, scale);
11496 }
11497 }
11498
Chet Haasea1cff502012-02-21 13:43:44 -080011499 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase64a48c12012-02-13 16:33:29 -080011500 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11501 if (transformToApply != null || !childHasIdentityMatrix) {
11502 int transX = 0;
11503 int transY = 0;
11504
11505 if (offsetForScroll) {
11506 transX = -sx;
11507 transY = -sy;
11508 }
11509
11510 if (transformToApply != null) {
11511 if (concatMatrix) {
11512 // Undo the scroll translation, apply the transformation matrix,
11513 // then redo the scroll translate to get the correct result.
Chet Haasebcbec0c2012-03-15 09:22:17 -070011514 if (!useDisplayListProperties) {
Chet Haasea1cff502012-02-21 13:43:44 -080011515 canvas.translate(-transX, -transY);
11516 canvas.concat(transformToApply.getMatrix());
11517 canvas.translate(transX, transY);
11518 }
11519 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011520 }
11521
11522 float transformAlpha = transformToApply.getAlpha();
11523 if (transformAlpha < 1.0f) {
11524 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080011525 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011526 }
11527 }
11528
Chet Haasea1cff502012-02-21 13:43:44 -080011529 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011530 canvas.translate(-transX, -transY);
11531 canvas.concat(getMatrix());
11532 canvas.translate(transX, transY);
11533 }
11534 }
11535
11536 if (alpha < 1.0f) {
Chet Haasea1cff502012-02-21 13:43:44 -080011537 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011538 if (hasNoCache) {
11539 final int multipliedAlpha = (int) (255 * alpha);
11540 if (!onSetAlpha(multipliedAlpha)) {
11541 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080011542 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080011543 layerType != LAYER_TYPE_NONE) {
11544 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11545 }
11546 if (layerType == LAYER_TYPE_NONE) {
Chet Haasea1cff502012-02-21 13:43:44 -080011547 if (!useDisplayListProperties) {
11548 final int scrollX = hasDisplayList ? 0 : sx;
11549 final int scrollY = hasDisplayList ? 0 : sy;
11550 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11551 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
11552 }
Chet Haase64a48c12012-02-13 16:33:29 -080011553 }
11554 } else {
11555 // Alpha is handled by the child directly, clobber the layer's alpha
11556 mPrivateFlags |= ALPHA_SET;
11557 }
11558 }
11559 }
11560 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11561 onSetAlpha(255);
11562 mPrivateFlags &= ~ALPHA_SET;
11563 }
11564
Chet Haasea1cff502012-02-21 13:43:44 -080011565 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
11566 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011567 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011568 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011569 } else {
11570 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011571 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011572 } else {
11573 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11574 }
11575 }
11576 }
11577
11578 if (hasDisplayList) {
11579 displayList = getDisplayList();
11580 if (!displayList.isValid()) {
11581 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11582 // to getDisplayList(), the display list will be marked invalid and we should not
11583 // try to use it again.
11584 displayList = null;
11585 hasDisplayList = false;
11586 }
11587 }
11588
11589 if (hasNoCache) {
11590 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080011591 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011592 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011593 if (layer != null && layer.isValid()) {
11594 mLayerPaint.setAlpha((int) (alpha * 255));
11595 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11596 layerRendered = true;
11597 } else {
11598 final int scrollX = hasDisplayList ? 0 : sx;
11599 final int scrollY = hasDisplayList ? 0 : sy;
11600 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011601 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011602 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11603 }
11604 }
11605
11606 if (!layerRendered) {
11607 if (!hasDisplayList) {
11608 // Fast path for layouts with no backgrounds
11609 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11610 if (ViewDebug.TRACE_HIERARCHY) {
11611 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11612 }
11613 mPrivateFlags &= ~DIRTY_MASK;
11614 dispatchDraw(canvas);
11615 } else {
11616 draw(canvas);
11617 }
11618 } else {
11619 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011620 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011621 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011622 }
11623 }
11624 } else if (cache != null) {
11625 mPrivateFlags &= ~DIRTY_MASK;
11626 Paint cachePaint;
11627
11628 if (layerType == LAYER_TYPE_NONE) {
11629 cachePaint = parent.mCachePaint;
11630 if (cachePaint == null) {
11631 cachePaint = new Paint();
11632 cachePaint.setDither(false);
11633 parent.mCachePaint = cachePaint;
11634 }
11635 if (alpha < 1.0f) {
11636 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080011637 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
11638 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011639 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080011640 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080011641 }
11642 } else {
11643 cachePaint = mLayerPaint;
11644 cachePaint.setAlpha((int) (alpha * 255));
11645 }
11646 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11647 }
11648
Chet Haasea1cff502012-02-21 13:43:44 -080011649 if (restoreTo >= 0) {
11650 canvas.restoreToCount(restoreTo);
11651 }
Chet Haase64a48c12012-02-13 16:33:29 -080011652
11653 if (a != null && !more) {
11654 if (!hardwareAccelerated && !a.getFillAfter()) {
11655 onSetAlpha(255);
11656 }
11657 parent.finishAnimatingView(this, a);
11658 }
11659
11660 if (more && hardwareAccelerated) {
11661 // invalidation is the trigger to recreate display lists, so if we're using
11662 // display lists to render, force an invalidate to allow the animation to
11663 // continue drawing another frame
11664 parent.invalidate(true);
11665 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11666 // alpha animations should cause the child to recreate its display list
11667 invalidate(true);
11668 }
11669 }
11670
11671 mRecreateDisplayList = false;
11672
11673 return more;
11674 }
11675
11676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011677 * Manually render this view (and all of its children) to the given Canvas.
11678 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011679 * called. When implementing a view, implement
11680 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11681 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011682 *
11683 * @param canvas The Canvas to which the View is rendered.
11684 */
11685 public void draw(Canvas canvas) {
11686 if (ViewDebug.TRACE_HIERARCHY) {
11687 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11688 }
11689
Romain Guy5bcdff42009-05-14 21:27:18 -070011690 final int privateFlags = mPrivateFlags;
11691 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11692 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11693 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 /*
11696 * Draw traversal performs several drawing steps which must be executed
11697 * in the appropriate order:
11698 *
11699 * 1. Draw the background
11700 * 2. If necessary, save the canvas' layers to prepare for fading
11701 * 3. Draw view's content
11702 * 4. Draw children
11703 * 5. If necessary, draw the fading edges and restore layers
11704 * 6. Draw decorations (scrollbars for instance)
11705 */
11706
11707 // Step 1, draw the background, if needed
11708 int saveCount;
11709
Romain Guy24443ea2009-05-11 11:56:30 -070011710 if (!dirtyOpaque) {
11711 final Drawable background = mBGDrawable;
11712 if (background != null) {
11713 final int scrollX = mScrollX;
11714 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011715
Romain Guy24443ea2009-05-11 11:56:30 -070011716 if (mBackgroundSizeChanged) {
11717 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11718 mBackgroundSizeChanged = false;
11719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011720
Romain Guy24443ea2009-05-11 11:56:30 -070011721 if ((scrollX | scrollY) == 0) {
11722 background.draw(canvas);
11723 } else {
11724 canvas.translate(scrollX, scrollY);
11725 background.draw(canvas);
11726 canvas.translate(-scrollX, -scrollY);
11727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728 }
11729 }
11730
11731 // skip step 2 & 5 if possible (common case)
11732 final int viewFlags = mViewFlags;
11733 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11734 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11735 if (!verticalEdges && !horizontalEdges) {
11736 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011737 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011738
11739 // Step 4, draw the children
11740 dispatchDraw(canvas);
11741
11742 // Step 6, draw decorations (scrollbars)
11743 onDrawScrollBars(canvas);
11744
11745 // we're done...
11746 return;
11747 }
11748
11749 /*
11750 * Here we do the full fledged routine...
11751 * (this is an uncommon case where speed matters less,
11752 * this is why we repeat some of the tests that have been
11753 * done above)
11754 */
11755
11756 boolean drawTop = false;
11757 boolean drawBottom = false;
11758 boolean drawLeft = false;
11759 boolean drawRight = false;
11760
11761 float topFadeStrength = 0.0f;
11762 float bottomFadeStrength = 0.0f;
11763 float leftFadeStrength = 0.0f;
11764 float rightFadeStrength = 0.0f;
11765
11766 // Step 2, save the canvas' layers
11767 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011768
11769 final boolean offsetRequired = isPaddingOffsetRequired();
11770 if (offsetRequired) {
11771 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 }
11773
11774 int left = mScrollX + paddingLeft;
11775 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011776 int top = mScrollY + getFadeTop(offsetRequired);
11777 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778
11779 if (offsetRequired) {
11780 right += getRightPaddingOffset();
11781 bottom += getBottomPaddingOffset();
11782 }
11783
11784 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011785 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11786 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011787
11788 // clip the fade length if top and bottom fades overlap
11789 // overlapping fades produce odd-looking artifacts
11790 if (verticalEdges && (top + length > bottom - length)) {
11791 length = (bottom - top) / 2;
11792 }
11793
11794 // also clip horizontal fades if necessary
11795 if (horizontalEdges && (left + length > right - length)) {
11796 length = (right - left) / 2;
11797 }
11798
11799 if (verticalEdges) {
11800 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011801 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011802 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011803 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011804 }
11805
11806 if (horizontalEdges) {
11807 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011808 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011809 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011810 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011811 }
11812
11813 saveCount = canvas.getSaveCount();
11814
11815 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011816 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011817 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11818
11819 if (drawTop) {
11820 canvas.saveLayer(left, top, right, top + length, null, flags);
11821 }
11822
11823 if (drawBottom) {
11824 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11825 }
11826
11827 if (drawLeft) {
11828 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11829 }
11830
11831 if (drawRight) {
11832 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11833 }
11834 } else {
11835 scrollabilityCache.setFadeColor(solidColor);
11836 }
11837
11838 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011839 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011840
11841 // Step 4, draw the children
11842 dispatchDraw(canvas);
11843
11844 // Step 5, draw the fade effect and restore layers
11845 final Paint p = scrollabilityCache.paint;
11846 final Matrix matrix = scrollabilityCache.matrix;
11847 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848
11849 if (drawTop) {
11850 matrix.setScale(1, fadeHeight * topFadeStrength);
11851 matrix.postTranslate(left, top);
11852 fade.setLocalMatrix(matrix);
11853 canvas.drawRect(left, top, right, top + length, p);
11854 }
11855
11856 if (drawBottom) {
11857 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11858 matrix.postRotate(180);
11859 matrix.postTranslate(left, bottom);
11860 fade.setLocalMatrix(matrix);
11861 canvas.drawRect(left, bottom - length, right, bottom, p);
11862 }
11863
11864 if (drawLeft) {
11865 matrix.setScale(1, fadeHeight * leftFadeStrength);
11866 matrix.postRotate(-90);
11867 matrix.postTranslate(left, top);
11868 fade.setLocalMatrix(matrix);
11869 canvas.drawRect(left, top, left + length, bottom, p);
11870 }
11871
11872 if (drawRight) {
11873 matrix.setScale(1, fadeHeight * rightFadeStrength);
11874 matrix.postRotate(90);
11875 matrix.postTranslate(right, top);
11876 fade.setLocalMatrix(matrix);
11877 canvas.drawRect(right - length, top, right, bottom, p);
11878 }
11879
11880 canvas.restoreToCount(saveCount);
11881
11882 // Step 6, draw decorations (scrollbars)
11883 onDrawScrollBars(canvas);
11884 }
11885
11886 /**
11887 * Override this if your view is known to always be drawn on top of a solid color background,
11888 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11889 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11890 * should be set to 0xFF.
11891 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011892 * @see #setVerticalFadingEdgeEnabled(boolean)
11893 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011894 *
11895 * @return The known solid color background for this view, or 0 if the color may vary
11896 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011897 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898 public int getSolidColor() {
11899 return 0;
11900 }
11901
11902 /**
11903 * Build a human readable string representation of the specified view flags.
11904 *
11905 * @param flags the view flags to convert to a string
11906 * @return a String representing the supplied flags
11907 */
11908 private static String printFlags(int flags) {
11909 String output = "";
11910 int numFlags = 0;
11911 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11912 output += "TAKES_FOCUS";
11913 numFlags++;
11914 }
11915
11916 switch (flags & VISIBILITY_MASK) {
11917 case INVISIBLE:
11918 if (numFlags > 0) {
11919 output += " ";
11920 }
11921 output += "INVISIBLE";
11922 // USELESS HERE numFlags++;
11923 break;
11924 case GONE:
11925 if (numFlags > 0) {
11926 output += " ";
11927 }
11928 output += "GONE";
11929 // USELESS HERE numFlags++;
11930 break;
11931 default:
11932 break;
11933 }
11934 return output;
11935 }
11936
11937 /**
11938 * Build a human readable string representation of the specified private
11939 * view flags.
11940 *
11941 * @param privateFlags the private view flags to convert to a string
11942 * @return a String representing the supplied flags
11943 */
11944 private static String printPrivateFlags(int privateFlags) {
11945 String output = "";
11946 int numFlags = 0;
11947
11948 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11949 output += "WANTS_FOCUS";
11950 numFlags++;
11951 }
11952
11953 if ((privateFlags & FOCUSED) == FOCUSED) {
11954 if (numFlags > 0) {
11955 output += " ";
11956 }
11957 output += "FOCUSED";
11958 numFlags++;
11959 }
11960
11961 if ((privateFlags & SELECTED) == SELECTED) {
11962 if (numFlags > 0) {
11963 output += " ";
11964 }
11965 output += "SELECTED";
11966 numFlags++;
11967 }
11968
11969 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11970 if (numFlags > 0) {
11971 output += " ";
11972 }
11973 output += "IS_ROOT_NAMESPACE";
11974 numFlags++;
11975 }
11976
11977 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11978 if (numFlags > 0) {
11979 output += " ";
11980 }
11981 output += "HAS_BOUNDS";
11982 numFlags++;
11983 }
11984
11985 if ((privateFlags & DRAWN) == DRAWN) {
11986 if (numFlags > 0) {
11987 output += " ";
11988 }
11989 output += "DRAWN";
11990 // USELESS HERE numFlags++;
11991 }
11992 return output;
11993 }
11994
11995 /**
11996 * <p>Indicates whether or not this view's layout will be requested during
11997 * the next hierarchy layout pass.</p>
11998 *
11999 * @return true if the layout will be forced during next layout pass
12000 */
12001 public boolean isLayoutRequested() {
12002 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12003 }
12004
12005 /**
12006 * Assign a size and position to a view and all of its
12007 * descendants
12008 *
12009 * <p>This is the second phase of the layout mechanism.
12010 * (The first is measuring). In this phase, each parent calls
12011 * layout on all of its children to position them.
12012 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012013 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 *
Chet Haase9c087442011-01-12 16:20:16 -080012015 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012016 * Derived classes with children should override
12017 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012018 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012019 *
12020 * @param l Left position, relative to parent
12021 * @param t Top position, relative to parent
12022 * @param r Right position, relative to parent
12023 * @param b Bottom position, relative to parent
12024 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012025 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012026 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012027 int oldL = mLeft;
12028 int oldT = mTop;
12029 int oldB = mBottom;
12030 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 boolean changed = setFrame(l, t, r, b);
12032 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12033 if (ViewDebug.TRACE_HIERARCHY) {
12034 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12035 }
12036
12037 onLayout(changed, l, t, r, b);
12038 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012039
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012040 ListenerInfo li = mListenerInfo;
12041 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012042 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012043 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012044 int numListeners = listenersCopy.size();
12045 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012046 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012047 }
12048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 }
12050 mPrivateFlags &= ~FORCE_LAYOUT;
12051 }
12052
12053 /**
12054 * Called from layout when this view should
12055 * assign a size and position to each of its children.
12056 *
12057 * Derived classes with children should override
12058 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012059 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012060 * @param changed This is a new size or position for this view
12061 * @param left Left position, relative to parent
12062 * @param top Top position, relative to parent
12063 * @param right Right position, relative to parent
12064 * @param bottom Bottom position, relative to parent
12065 */
12066 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12067 }
12068
12069 /**
12070 * Assign a size and position to this view.
12071 *
12072 * This is called from layout.
12073 *
12074 * @param left Left position, relative to parent
12075 * @param top Top position, relative to parent
12076 * @param right Right position, relative to parent
12077 * @param bottom Bottom position, relative to parent
12078 * @return true if the new size and position are different than the
12079 * previous ones
12080 * {@hide}
12081 */
12082 protected boolean setFrame(int left, int top, int right, int bottom) {
12083 boolean changed = false;
12084
12085 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012086 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087 + right + "," + bottom + ")");
12088 }
12089
12090 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12091 changed = true;
12092
12093 // Remember our drawn bit
12094 int drawn = mPrivateFlags & DRAWN;
12095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012096 int oldWidth = mRight - mLeft;
12097 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012098 int newWidth = right - left;
12099 int newHeight = bottom - top;
12100 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12101
12102 // Invalidate our old position
12103 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012104
12105 mLeft = left;
12106 mTop = top;
12107 mRight = right;
12108 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012109 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12110 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012112
12113 mPrivateFlags |= HAS_BOUNDS;
12114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115
Chet Haase75755e22011-07-18 17:48:25 -070012116 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012117 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12118 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012119 if (mTransformationInfo != null) {
12120 mTransformationInfo.mMatrixDirty = true;
12121 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12124 }
12125
12126 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12127 // If we are visible, force the DRAWN bit to on so that
12128 // this invalidate will go through (at least to our parent).
12129 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012130 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012131 // the DRAWN bit.
12132 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012133 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012134 // parent display list may need to be recreated based on a change in the bounds
12135 // of any child
12136 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137 }
12138
12139 // Reset drawn bit to original value (invalidate turns it off)
12140 mPrivateFlags |= drawn;
12141
12142 mBackgroundSizeChanged = true;
12143 }
12144 return changed;
12145 }
12146
12147 /**
12148 * Finalize inflating a view from XML. This is called as the last phase
12149 * of inflation, after all child views have been added.
12150 *
12151 * <p>Even if the subclass overrides onFinishInflate, they should always be
12152 * sure to call the super method, so that we get called.
12153 */
12154 protected void onFinishInflate() {
12155 }
12156
12157 /**
12158 * Returns the resources associated with this view.
12159 *
12160 * @return Resources object.
12161 */
12162 public Resources getResources() {
12163 return mResources;
12164 }
12165
12166 /**
12167 * Invalidates the specified Drawable.
12168 *
12169 * @param drawable the drawable to invalidate
12170 */
12171 public void invalidateDrawable(Drawable drawable) {
12172 if (verifyDrawable(drawable)) {
12173 final Rect dirty = drawable.getBounds();
12174 final int scrollX = mScrollX;
12175 final int scrollY = mScrollY;
12176
12177 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12178 dirty.right + scrollX, dirty.bottom + scrollY);
12179 }
12180 }
12181
12182 /**
12183 * Schedules an action on a drawable to occur at a specified time.
12184 *
12185 * @param who the recipient of the action
12186 * @param what the action to run on the drawable
12187 * @param when the time at which the action must occur. Uses the
12188 * {@link SystemClock#uptimeMillis} timebase.
12189 */
12190 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012191 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012192 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012193 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012194 mAttachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
12195 what, who, Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012196 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012197 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 }
12200 }
12201
12202 /**
12203 * Cancels a scheduled action on a drawable.
12204 *
12205 * @param who the recipient of the action
12206 * @param what the action to cancel
12207 */
12208 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012209 if (verifyDrawable(who) && what != null) {
12210 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012211 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012212 } else {
12213 ViewRootImpl.getRunQueue().removeCallbacks(what);
12214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215 }
12216 }
12217
12218 /**
12219 * Unschedule any events associated with the given Drawable. This can be
12220 * used when selecting a new Drawable into a view, so that the previous
12221 * one is completely unscheduled.
12222 *
12223 * @param who The Drawable to unschedule.
12224 *
12225 * @see #drawableStateChanged
12226 */
12227 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012228 if (mAttachInfo != null && who != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012229 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012230 }
12231 }
12232
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012233 /**
12234 * Return the layout direction of a given Drawable.
12235 *
12236 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012237 */
12238 public int getResolvedLayoutDirection(Drawable who) {
12239 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012240 }
12241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012242 /**
12243 * If your view subclass is displaying its own Drawable objects, it should
12244 * override this function and return true for any Drawable it is
12245 * displaying. This allows animations for those drawables to be
12246 * scheduled.
12247 *
12248 * <p>Be sure to call through to the super class when overriding this
12249 * function.
12250 *
12251 * @param who The Drawable to verify. Return true if it is one you are
12252 * displaying, else return the result of calling through to the
12253 * super class.
12254 *
12255 * @return boolean If true than the Drawable is being displayed in the
12256 * view; else false and it is not allowed to animate.
12257 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012258 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12259 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012260 */
12261 protected boolean verifyDrawable(Drawable who) {
12262 return who == mBGDrawable;
12263 }
12264
12265 /**
12266 * This function is called whenever the state of the view changes in such
12267 * a way that it impacts the state of drawables being shown.
12268 *
12269 * <p>Be sure to call through to the superclass when overriding this
12270 * function.
12271 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012272 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 */
12274 protected void drawableStateChanged() {
12275 Drawable d = mBGDrawable;
12276 if (d != null && d.isStateful()) {
12277 d.setState(getDrawableState());
12278 }
12279 }
12280
12281 /**
12282 * Call this to force a view to update its drawable state. This will cause
12283 * drawableStateChanged to be called on this view. Views that are interested
12284 * in the new state should call getDrawableState.
12285 *
12286 * @see #drawableStateChanged
12287 * @see #getDrawableState
12288 */
12289 public void refreshDrawableState() {
12290 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12291 drawableStateChanged();
12292
12293 ViewParent parent = mParent;
12294 if (parent != null) {
12295 parent.childDrawableStateChanged(this);
12296 }
12297 }
12298
12299 /**
12300 * Return an array of resource IDs of the drawable states representing the
12301 * current state of the view.
12302 *
12303 * @return The current drawable state
12304 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012305 * @see Drawable#setState(int[])
12306 * @see #drawableStateChanged()
12307 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012308 */
12309 public final int[] getDrawableState() {
12310 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12311 return mDrawableState;
12312 } else {
12313 mDrawableState = onCreateDrawableState(0);
12314 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12315 return mDrawableState;
12316 }
12317 }
12318
12319 /**
12320 * Generate the new {@link android.graphics.drawable.Drawable} state for
12321 * this view. This is called by the view
12322 * system when the cached Drawable state is determined to be invalid. To
12323 * retrieve the current state, you should use {@link #getDrawableState}.
12324 *
12325 * @param extraSpace if non-zero, this is the number of extra entries you
12326 * would like in the returned array in which you can place your own
12327 * states.
12328 *
12329 * @return Returns an array holding the current {@link Drawable} state of
12330 * the view.
12331 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012332 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012333 */
12334 protected int[] onCreateDrawableState(int extraSpace) {
12335 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12336 mParent instanceof View) {
12337 return ((View) mParent).onCreateDrawableState(extraSpace);
12338 }
12339
12340 int[] drawableState;
12341
12342 int privateFlags = mPrivateFlags;
12343
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012344 int viewStateIndex = 0;
12345 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12346 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12347 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012348 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012349 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12350 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012351 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12352 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012353 // This is set if HW acceleration is requested, even if the current
12354 // process doesn't allow it. This is just to allow app preview
12355 // windows to better match their app.
12356 viewStateIndex |= VIEW_STATE_ACCELERATED;
12357 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012358 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359
Christopher Tate3d4bf172011-03-28 16:16:46 -070012360 final int privateFlags2 = mPrivateFlags2;
12361 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12362 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012364 drawableState = VIEW_STATE_SETS[viewStateIndex];
12365
12366 //noinspection ConstantIfStatement
12367 if (false) {
12368 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12369 Log.i("View", toString()
12370 + " pressed=" + ((privateFlags & PRESSED) != 0)
12371 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12372 + " fo=" + hasFocus()
12373 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012374 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012375 + ": " + Arrays.toString(drawableState));
12376 }
12377
12378 if (extraSpace == 0) {
12379 return drawableState;
12380 }
12381
12382 final int[] fullState;
12383 if (drawableState != null) {
12384 fullState = new int[drawableState.length + extraSpace];
12385 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12386 } else {
12387 fullState = new int[extraSpace];
12388 }
12389
12390 return fullState;
12391 }
12392
12393 /**
12394 * Merge your own state values in <var>additionalState</var> into the base
12395 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012396 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012397 *
12398 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012399 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012400 * own additional state values.
12401 *
12402 * @param additionalState The additional state values you would like
12403 * added to <var>baseState</var>; this array is not modified.
12404 *
12405 * @return As a convenience, the <var>baseState</var> array you originally
12406 * passed into the function is returned.
12407 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012408 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012409 */
12410 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12411 final int N = baseState.length;
12412 int i = N - 1;
12413 while (i >= 0 && baseState[i] == 0) {
12414 i--;
12415 }
12416 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12417 return baseState;
12418 }
12419
12420 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012421 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12422 * on all Drawable objects associated with this view.
12423 */
12424 public void jumpDrawablesToCurrentState() {
12425 if (mBGDrawable != null) {
12426 mBGDrawable.jumpToCurrentState();
12427 }
12428 }
12429
12430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012431 * Sets the background color for this view.
12432 * @param color the color of the background
12433 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012434 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012435 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012436 if (mBGDrawable instanceof ColorDrawable) {
12437 ((ColorDrawable) mBGDrawable).setColor(color);
12438 } else {
12439 setBackgroundDrawable(new ColorDrawable(color));
12440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012441 }
12442
12443 /**
12444 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012445 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012446 * @param resid The identifier of the resource.
12447 * @attr ref android.R.styleable#View_background
12448 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012449 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012450 public void setBackgroundResource(int resid) {
12451 if (resid != 0 && resid == mBackgroundResource) {
12452 return;
12453 }
12454
12455 Drawable d= null;
12456 if (resid != 0) {
12457 d = mResources.getDrawable(resid);
12458 }
12459 setBackgroundDrawable(d);
12460
12461 mBackgroundResource = resid;
12462 }
12463
12464 /**
12465 * Set the background to a given Drawable, or remove the background. If the
12466 * background has padding, this View's padding is set to the background's
12467 * padding. However, when a background is removed, this View's padding isn't
12468 * touched. If setting the padding is desired, please use
12469 * {@link #setPadding(int, int, int, int)}.
12470 *
12471 * @param d The Drawable to use as the background, or null to remove the
12472 * background
12473 */
12474 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012475 if (d == mBGDrawable) {
12476 return;
12477 }
12478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012479 boolean requestLayout = false;
12480
12481 mBackgroundResource = 0;
12482
12483 /*
12484 * Regardless of whether we're setting a new background or not, we want
12485 * to clear the previous drawable.
12486 */
12487 if (mBGDrawable != null) {
12488 mBGDrawable.setCallback(null);
12489 unscheduleDrawable(mBGDrawable);
12490 }
12491
12492 if (d != null) {
12493 Rect padding = sThreadLocal.get();
12494 if (padding == null) {
12495 padding = new Rect();
12496 sThreadLocal.set(padding);
12497 }
12498 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012499 switch (d.getResolvedLayoutDirectionSelf()) {
12500 case LAYOUT_DIRECTION_RTL:
12501 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12502 break;
12503 case LAYOUT_DIRECTION_LTR:
12504 default:
12505 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 }
12508
12509 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12510 // if it has a different minimum size, we should layout again
12511 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12512 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12513 requestLayout = true;
12514 }
12515
12516 d.setCallback(this);
12517 if (d.isStateful()) {
12518 d.setState(getDrawableState());
12519 }
12520 d.setVisible(getVisibility() == VISIBLE, false);
12521 mBGDrawable = d;
12522
12523 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12524 mPrivateFlags &= ~SKIP_DRAW;
12525 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12526 requestLayout = true;
12527 }
12528 } else {
12529 /* Remove the background */
12530 mBGDrawable = null;
12531
12532 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12533 /*
12534 * This view ONLY drew the background before and we're removing
12535 * the background, so now it won't draw anything
12536 * (hence we SKIP_DRAW)
12537 */
12538 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12539 mPrivateFlags |= SKIP_DRAW;
12540 }
12541
12542 /*
12543 * When the background is set, we try to apply its padding to this
12544 * View. When the background is removed, we don't touch this View's
12545 * padding. This is noted in the Javadocs. Hence, we don't need to
12546 * requestLayout(), the invalidate() below is sufficient.
12547 */
12548
12549 // The old background's minimum size could have affected this
12550 // View's layout, so let's requestLayout
12551 requestLayout = true;
12552 }
12553
Romain Guy8f1344f52009-05-15 16:03:59 -070012554 computeOpaqueFlags();
12555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012556 if (requestLayout) {
12557 requestLayout();
12558 }
12559
12560 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012561 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012562 }
12563
12564 /**
12565 * Gets the background drawable
12566 * @return The drawable used as the background for this view, if any.
12567 */
12568 public Drawable getBackground() {
12569 return mBGDrawable;
12570 }
12571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012572 /**
12573 * Sets the padding. The view may add on the space required to display
12574 * the scrollbars, depending on the style and visibility of the scrollbars.
12575 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12576 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12577 * from the values set in this call.
12578 *
12579 * @attr ref android.R.styleable#View_padding
12580 * @attr ref android.R.styleable#View_paddingBottom
12581 * @attr ref android.R.styleable#View_paddingLeft
12582 * @attr ref android.R.styleable#View_paddingRight
12583 * @attr ref android.R.styleable#View_paddingTop
12584 * @param left the left padding in pixels
12585 * @param top the top padding in pixels
12586 * @param right the right padding in pixels
12587 * @param bottom the bottom padding in pixels
12588 */
12589 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012590 mUserPaddingStart = -1;
12591 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012592 mUserPaddingRelative = false;
12593
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012594 internalSetPadding(left, top, right, bottom);
12595 }
12596
12597 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012598 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012599 mUserPaddingRight = right;
12600 mUserPaddingBottom = bottom;
12601
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012602 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012603 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012604
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012605 // Common case is there are no scroll bars.
12606 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012607 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012608 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012609 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012610 switch (mVerticalScrollbarPosition) {
12611 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012612 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12613 left += offset;
12614 } else {
12615 right += offset;
12616 }
12617 break;
Adam Powell20232d02010-12-08 21:08:53 -080012618 case SCROLLBAR_POSITION_RIGHT:
12619 right += offset;
12620 break;
12621 case SCROLLBAR_POSITION_LEFT:
12622 left += offset;
12623 break;
12624 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012625 }
Adam Powell20232d02010-12-08 21:08:53 -080012626 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012627 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12628 ? 0 : getHorizontalScrollbarHeight();
12629 }
12630 }
Romain Guy8506ab42009-06-11 17:35:47 -070012631
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012632 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012633 changed = true;
12634 mPaddingLeft = left;
12635 }
12636 if (mPaddingTop != top) {
12637 changed = true;
12638 mPaddingTop = top;
12639 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012640 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012641 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012642 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012644 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012645 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012646 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012647 }
12648
12649 if (changed) {
12650 requestLayout();
12651 }
12652 }
12653
12654 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012655 * Sets the relative padding. The view may add on the space required to display
12656 * the scrollbars, depending on the style and visibility of the scrollbars.
12657 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12658 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12659 * from the values set in this call.
12660 *
12661 * @attr ref android.R.styleable#View_padding
12662 * @attr ref android.R.styleable#View_paddingBottom
12663 * @attr ref android.R.styleable#View_paddingStart
12664 * @attr ref android.R.styleable#View_paddingEnd
12665 * @attr ref android.R.styleable#View_paddingTop
12666 * @param start the start padding in pixels
12667 * @param top the top padding in pixels
12668 * @param end the end padding in pixels
12669 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012670 */
12671 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012672 mUserPaddingStart = start;
12673 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012674 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012675
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012676 switch(getResolvedLayoutDirection()) {
12677 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012678 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012679 break;
12680 case LAYOUT_DIRECTION_LTR:
12681 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012682 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012683 }
12684 }
12685
12686 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687 * Returns the top padding of this view.
12688 *
12689 * @return the top padding in pixels
12690 */
12691 public int getPaddingTop() {
12692 return mPaddingTop;
12693 }
12694
12695 /**
12696 * Returns the bottom padding of this view. If there are inset and enabled
12697 * scrollbars, this value may include the space required to display the
12698 * scrollbars as well.
12699 *
12700 * @return the bottom padding in pixels
12701 */
12702 public int getPaddingBottom() {
12703 return mPaddingBottom;
12704 }
12705
12706 /**
12707 * Returns the left padding of this view. If there are inset and enabled
12708 * scrollbars, this value may include the space required to display the
12709 * scrollbars as well.
12710 *
12711 * @return the left padding in pixels
12712 */
12713 public int getPaddingLeft() {
12714 return mPaddingLeft;
12715 }
12716
12717 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012718 * Returns the start padding of this view depending on its resolved layout direction.
12719 * If there are inset and enabled scrollbars, this value may include the space
12720 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012721 *
12722 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012723 */
12724 public int getPaddingStart() {
12725 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12726 mPaddingRight : mPaddingLeft;
12727 }
12728
12729 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012730 * Returns the right padding of this view. If there are inset and enabled
12731 * scrollbars, this value may include the space required to display the
12732 * scrollbars as well.
12733 *
12734 * @return the right padding in pixels
12735 */
12736 public int getPaddingRight() {
12737 return mPaddingRight;
12738 }
12739
12740 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012741 * Returns the end padding of this view depending on its resolved layout direction.
12742 * If there are inset and enabled scrollbars, this value may include the space
12743 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012744 *
12745 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012746 */
12747 public int getPaddingEnd() {
12748 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12749 mPaddingLeft : mPaddingRight;
12750 }
12751
12752 /**
12753 * Return if the padding as been set thru relative values
12754 * {@link #setPaddingRelative(int, int, int, int)} or thru
12755 * @attr ref android.R.styleable#View_paddingStart or
12756 * @attr ref android.R.styleable#View_paddingEnd
12757 *
12758 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012759 */
12760 public boolean isPaddingRelative() {
12761 return mUserPaddingRelative;
12762 }
12763
12764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012765 * Changes the selection state of this view. A view can be selected or not.
12766 * Note that selection is not the same as focus. Views are typically
12767 * selected in the context of an AdapterView like ListView or GridView;
12768 * the selected view is the view that is highlighted.
12769 *
12770 * @param selected true if the view must be selected, false otherwise
12771 */
12772 public void setSelected(boolean selected) {
12773 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12774 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012775 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012776 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 refreshDrawableState();
12778 dispatchSetSelected(selected);
12779 }
12780 }
12781
12782 /**
12783 * Dispatch setSelected to all of this View's children.
12784 *
12785 * @see #setSelected(boolean)
12786 *
12787 * @param selected The new selected state
12788 */
12789 protected void dispatchSetSelected(boolean selected) {
12790 }
12791
12792 /**
12793 * Indicates the selection state of this view.
12794 *
12795 * @return true if the view is selected, false otherwise
12796 */
12797 @ViewDebug.ExportedProperty
12798 public boolean isSelected() {
12799 return (mPrivateFlags & SELECTED) != 0;
12800 }
12801
12802 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012803 * Changes the activated state of this view. A view can be activated or not.
12804 * Note that activation is not the same as selection. Selection is
12805 * a transient property, representing the view (hierarchy) the user is
12806 * currently interacting with. Activation is a longer-term state that the
12807 * user can move views in and out of. For example, in a list view with
12808 * single or multiple selection enabled, the views in the current selection
12809 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12810 * here.) The activated state is propagated down to children of the view it
12811 * is set on.
12812 *
12813 * @param activated true if the view must be activated, false otherwise
12814 */
12815 public void setActivated(boolean activated) {
12816 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12817 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012818 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012819 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012820 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012821 }
12822 }
12823
12824 /**
12825 * Dispatch setActivated to all of this View's children.
12826 *
12827 * @see #setActivated(boolean)
12828 *
12829 * @param activated The new activated state
12830 */
12831 protected void dispatchSetActivated(boolean activated) {
12832 }
12833
12834 /**
12835 * Indicates the activation state of this view.
12836 *
12837 * @return true if the view is activated, false otherwise
12838 */
12839 @ViewDebug.ExportedProperty
12840 public boolean isActivated() {
12841 return (mPrivateFlags & ACTIVATED) != 0;
12842 }
12843
12844 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012845 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12846 * observer can be used to get notifications when global events, like
12847 * layout, happen.
12848 *
12849 * The returned ViewTreeObserver observer is not guaranteed to remain
12850 * valid for the lifetime of this View. If the caller of this method keeps
12851 * a long-lived reference to ViewTreeObserver, it should always check for
12852 * the return value of {@link ViewTreeObserver#isAlive()}.
12853 *
12854 * @return The ViewTreeObserver for this view's hierarchy.
12855 */
12856 public ViewTreeObserver getViewTreeObserver() {
12857 if (mAttachInfo != null) {
12858 return mAttachInfo.mTreeObserver;
12859 }
12860 if (mFloatingTreeObserver == null) {
12861 mFloatingTreeObserver = new ViewTreeObserver();
12862 }
12863 return mFloatingTreeObserver;
12864 }
12865
12866 /**
12867 * <p>Finds the topmost view in the current view hierarchy.</p>
12868 *
12869 * @return the topmost view containing this view
12870 */
12871 public View getRootView() {
12872 if (mAttachInfo != null) {
12873 final View v = mAttachInfo.mRootView;
12874 if (v != null) {
12875 return v;
12876 }
12877 }
Romain Guy8506ab42009-06-11 17:35:47 -070012878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012879 View parent = this;
12880
12881 while (parent.mParent != null && parent.mParent instanceof View) {
12882 parent = (View) parent.mParent;
12883 }
12884
12885 return parent;
12886 }
12887
12888 /**
12889 * <p>Computes the coordinates of this view on the screen. The argument
12890 * must be an array of two integers. After the method returns, the array
12891 * contains the x and y location in that order.</p>
12892 *
12893 * @param location an array of two integers in which to hold the coordinates
12894 */
12895 public void getLocationOnScreen(int[] location) {
12896 getLocationInWindow(location);
12897
12898 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012899 if (info != null) {
12900 location[0] += info.mWindowLeft;
12901 location[1] += info.mWindowTop;
12902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903 }
12904
12905 /**
12906 * <p>Computes the coordinates of this view in its window. The argument
12907 * must be an array of two integers. After the method returns, the array
12908 * contains the x and y location in that order.</p>
12909 *
12910 * @param location an array of two integers in which to hold the coordinates
12911 */
12912 public void getLocationInWindow(int[] location) {
12913 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012914 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 }
12916
Gilles Debunne6583ce52011-12-06 18:09:02 -080012917 if (mAttachInfo == null) {
12918 // When the view is not attached to a window, this method does not make sense
12919 location[0] = location[1] = 0;
12920 return;
12921 }
12922
Gilles Debunnecea45132011-11-24 02:19:27 +010012923 float[] position = mAttachInfo.mTmpTransformLocation;
12924 position[0] = position[1] = 0.0f;
12925
12926 if (!hasIdentityMatrix()) {
12927 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012929
Gilles Debunnecea45132011-11-24 02:19:27 +010012930 position[0] += mLeft;
12931 position[1] += mTop;
12932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012933 ViewParent viewParent = mParent;
12934 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012935 final View view = (View) viewParent;
12936
12937 position[0] -= view.mScrollX;
12938 position[1] -= view.mScrollY;
12939
12940 if (!view.hasIdentityMatrix()) {
12941 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012942 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012943
12944 position[0] += view.mLeft;
12945 position[1] += view.mTop;
12946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012947 viewParent = view.mParent;
12948 }
Romain Guy8506ab42009-06-11 17:35:47 -070012949
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012950 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012951 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012952 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12953 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012954 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012955
12956 location[0] = (int) (position[0] + 0.5f);
12957 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012958 }
12959
12960 /**
12961 * {@hide}
12962 * @param id the id of the view to be found
12963 * @return the view of the specified id, null if cannot be found
12964 */
12965 protected View findViewTraversal(int id) {
12966 if (id == mID) {
12967 return this;
12968 }
12969 return null;
12970 }
12971
12972 /**
12973 * {@hide}
12974 * @param tag the tag of the view to be found
12975 * @return the view of specified tag, null if cannot be found
12976 */
12977 protected View findViewWithTagTraversal(Object tag) {
12978 if (tag != null && tag.equals(mTag)) {
12979 return this;
12980 }
12981 return null;
12982 }
12983
12984 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012985 * {@hide}
12986 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012987 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012988 * @return The first view that matches the predicate or null.
12989 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012990 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012991 if (predicate.apply(this)) {
12992 return this;
12993 }
12994 return null;
12995 }
12996
12997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012998 * Look for a child view with the given id. If this view has the given
12999 * id, return this view.
13000 *
13001 * @param id The id to search for.
13002 * @return The view that has the given id in the hierarchy or null
13003 */
13004 public final View findViewById(int id) {
13005 if (id < 0) {
13006 return null;
13007 }
13008 return findViewTraversal(id);
13009 }
13010
13011 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013012 * Finds a view by its unuque and stable accessibility id.
13013 *
13014 * @param accessibilityId The searched accessibility id.
13015 * @return The found view.
13016 */
13017 final View findViewByAccessibilityId(int accessibilityId) {
13018 if (accessibilityId < 0) {
13019 return null;
13020 }
13021 return findViewByAccessibilityIdTraversal(accessibilityId);
13022 }
13023
13024 /**
13025 * Performs the traversal to find a view by its unuque and stable accessibility id.
13026 *
13027 * <strong>Note:</strong>This method does not stop at the root namespace
13028 * boundary since the user can touch the screen at an arbitrary location
13029 * potentially crossing the root namespace bounday which will send an
13030 * accessibility event to accessibility services and they should be able
13031 * to obtain the event source. Also accessibility ids are guaranteed to be
13032 * unique in the window.
13033 *
13034 * @param accessibilityId The accessibility id.
13035 * @return The found view.
13036 */
13037 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13038 if (getAccessibilityViewId() == accessibilityId) {
13039 return this;
13040 }
13041 return null;
13042 }
13043
13044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013045 * Look for a child view with the given tag. If this view has the given
13046 * tag, return this view.
13047 *
13048 * @param tag The tag to search for, using "tag.equals(getTag())".
13049 * @return The View that has the given tag in the hierarchy or null
13050 */
13051 public final View findViewWithTag(Object tag) {
13052 if (tag == null) {
13053 return null;
13054 }
13055 return findViewWithTagTraversal(tag);
13056 }
13057
13058 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013059 * {@hide}
13060 * Look for a child view that matches the specified predicate.
13061 * If this view matches the predicate, return this view.
13062 *
13063 * @param predicate The predicate to evaluate.
13064 * @return The first view that matches the predicate or null.
13065 */
13066 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013067 return findViewByPredicateTraversal(predicate, null);
13068 }
13069
13070 /**
13071 * {@hide}
13072 * Look for a child view that matches the specified predicate,
13073 * starting with the specified view and its descendents and then
13074 * recusively searching the ancestors and siblings of that view
13075 * until this view is reached.
13076 *
13077 * This method is useful in cases where the predicate does not match
13078 * a single unique view (perhaps multiple views use the same id)
13079 * and we are trying to find the view that is "closest" in scope to the
13080 * starting view.
13081 *
13082 * @param start The view to start from.
13083 * @param predicate The predicate to evaluate.
13084 * @return The first view that matches the predicate or null.
13085 */
13086 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13087 View childToSkip = null;
13088 for (;;) {
13089 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13090 if (view != null || start == this) {
13091 return view;
13092 }
13093
13094 ViewParent parent = start.getParent();
13095 if (parent == null || !(parent instanceof View)) {
13096 return null;
13097 }
13098
13099 childToSkip = start;
13100 start = (View) parent;
13101 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013102 }
13103
13104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013105 * Sets the identifier for this view. The identifier does not have to be
13106 * unique in this view's hierarchy. The identifier should be a positive
13107 * number.
13108 *
13109 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070013110 * @see #getId()
13111 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013112 *
13113 * @param id a number used to identify the view
13114 *
13115 * @attr ref android.R.styleable#View_id
13116 */
13117 public void setId(int id) {
13118 mID = id;
13119 }
13120
13121 /**
13122 * {@hide}
13123 *
13124 * @param isRoot true if the view belongs to the root namespace, false
13125 * otherwise
13126 */
13127 public void setIsRootNamespace(boolean isRoot) {
13128 if (isRoot) {
13129 mPrivateFlags |= IS_ROOT_NAMESPACE;
13130 } else {
13131 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13132 }
13133 }
13134
13135 /**
13136 * {@hide}
13137 *
13138 * @return true if the view belongs to the root namespace, false otherwise
13139 */
13140 public boolean isRootNamespace() {
13141 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13142 }
13143
13144 /**
13145 * Returns this view's identifier.
13146 *
13147 * @return a positive integer used to identify the view or {@link #NO_ID}
13148 * if the view has no ID
13149 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013150 * @see #setId(int)
13151 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013152 * @attr ref android.R.styleable#View_id
13153 */
13154 @ViewDebug.CapturedViewProperty
13155 public int getId() {
13156 return mID;
13157 }
13158
13159 /**
13160 * Returns this view's tag.
13161 *
13162 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013163 *
13164 * @see #setTag(Object)
13165 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013166 */
13167 @ViewDebug.ExportedProperty
13168 public Object getTag() {
13169 return mTag;
13170 }
13171
13172 /**
13173 * Sets the tag associated with this view. A tag can be used to mark
13174 * a view in its hierarchy and does not have to be unique within the
13175 * hierarchy. Tags can also be used to store data within a view without
13176 * resorting to another data structure.
13177 *
13178 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013179 *
13180 * @see #getTag()
13181 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013182 */
13183 public void setTag(final Object tag) {
13184 mTag = tag;
13185 }
13186
13187 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013188 * Returns the tag associated with this view and the specified key.
13189 *
13190 * @param key The key identifying the tag
13191 *
13192 * @return the Object stored in this view as a tag
13193 *
13194 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013195 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013196 */
13197 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013198 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013199 return null;
13200 }
13201
13202 /**
13203 * Sets a tag associated with this view and a key. A tag can be used
13204 * to mark a view in its hierarchy and does not have to be unique within
13205 * the hierarchy. Tags can also be used to store data within a view
13206 * without resorting to another data structure.
13207 *
13208 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013209 * application to ensure it is unique (see the <a
13210 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13211 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013212 * the Android framework or not associated with any package will cause
13213 * an {@link IllegalArgumentException} to be thrown.
13214 *
13215 * @param key The key identifying the tag
13216 * @param tag An Object to tag the view with
13217 *
13218 * @throws IllegalArgumentException If they specified key is not valid
13219 *
13220 * @see #setTag(Object)
13221 * @see #getTag(int)
13222 */
13223 public void setTag(int key, final Object tag) {
13224 // If the package id is 0x00 or 0x01, it's either an undefined package
13225 // or a framework id
13226 if ((key >>> 24) < 2) {
13227 throw new IllegalArgumentException("The key must be an application-specific "
13228 + "resource id.");
13229 }
13230
Adam Powell2b2f6d62011-09-23 11:15:39 -070013231 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013232 }
13233
13234 /**
13235 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13236 * framework id.
13237 *
13238 * @hide
13239 */
13240 public void setTagInternal(int key, Object tag) {
13241 if ((key >>> 24) != 0x1) {
13242 throw new IllegalArgumentException("The key must be a framework-specific "
13243 + "resource id.");
13244 }
13245
Adam Powell2b2f6d62011-09-23 11:15:39 -070013246 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013247 }
13248
Adam Powell2b2f6d62011-09-23 11:15:39 -070013249 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013250 if (mKeyedTags == null) {
13251 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013252 }
13253
Adam Powell7db82ac2011-09-22 19:44:04 -070013254 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013255 }
13256
13257 /**
Romain Guy13922e02009-05-12 17:56:14 -070013258 * @param consistency The type of consistency. See ViewDebug for more information.
13259 *
13260 * @hide
13261 */
13262 protected boolean dispatchConsistencyCheck(int consistency) {
13263 return onConsistencyCheck(consistency);
13264 }
13265
13266 /**
13267 * Method that subclasses should implement to check their consistency. The type of
13268 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013269 *
Romain Guy13922e02009-05-12 17:56:14 -070013270 * @param consistency The type of consistency. See ViewDebug for more information.
13271 *
13272 * @throws IllegalStateException if the view is in an inconsistent state.
13273 *
13274 * @hide
13275 */
13276 protected boolean onConsistencyCheck(int consistency) {
13277 boolean result = true;
13278
13279 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13280 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13281
13282 if (checkLayout) {
13283 if (getParent() == null) {
13284 result = false;
13285 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13286 "View " + this + " does not have a parent.");
13287 }
13288
13289 if (mAttachInfo == null) {
13290 result = false;
13291 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13292 "View " + this + " is not attached to a window.");
13293 }
13294 }
13295
13296 if (checkDrawing) {
13297 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13298 // from their draw() method
13299
13300 if ((mPrivateFlags & DRAWN) != DRAWN &&
13301 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13302 result = false;
13303 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13304 "View " + this + " was invalidated but its drawing cache is valid.");
13305 }
13306 }
13307
13308 return result;
13309 }
13310
13311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312 * Prints information about this view in the log output, with the tag
13313 * {@link #VIEW_LOG_TAG}.
13314 *
13315 * @hide
13316 */
13317 public void debug() {
13318 debug(0);
13319 }
13320
13321 /**
13322 * Prints information about this view in the log output, with the tag
13323 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13324 * indentation defined by the <code>depth</code>.
13325 *
13326 * @param depth the indentation level
13327 *
13328 * @hide
13329 */
13330 protected void debug(int depth) {
13331 String output = debugIndent(depth - 1);
13332
13333 output += "+ " + this;
13334 int id = getId();
13335 if (id != -1) {
13336 output += " (id=" + id + ")";
13337 }
13338 Object tag = getTag();
13339 if (tag != null) {
13340 output += " (tag=" + tag + ")";
13341 }
13342 Log.d(VIEW_LOG_TAG, output);
13343
13344 if ((mPrivateFlags & FOCUSED) != 0) {
13345 output = debugIndent(depth) + " FOCUSED";
13346 Log.d(VIEW_LOG_TAG, output);
13347 }
13348
13349 output = debugIndent(depth);
13350 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13351 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13352 + "} ";
13353 Log.d(VIEW_LOG_TAG, output);
13354
13355 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13356 || mPaddingBottom != 0) {
13357 output = debugIndent(depth);
13358 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13359 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13360 Log.d(VIEW_LOG_TAG, output);
13361 }
13362
13363 output = debugIndent(depth);
13364 output += "mMeasureWidth=" + mMeasuredWidth +
13365 " mMeasureHeight=" + mMeasuredHeight;
13366 Log.d(VIEW_LOG_TAG, output);
13367
13368 output = debugIndent(depth);
13369 if (mLayoutParams == null) {
13370 output += "BAD! no layout params";
13371 } else {
13372 output = mLayoutParams.debug(output);
13373 }
13374 Log.d(VIEW_LOG_TAG, output);
13375
13376 output = debugIndent(depth);
13377 output += "flags={";
13378 output += View.printFlags(mViewFlags);
13379 output += "}";
13380 Log.d(VIEW_LOG_TAG, output);
13381
13382 output = debugIndent(depth);
13383 output += "privateFlags={";
13384 output += View.printPrivateFlags(mPrivateFlags);
13385 output += "}";
13386 Log.d(VIEW_LOG_TAG, output);
13387 }
13388
13389 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013390 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013391 *
13392 * @param depth the indentation level
13393 * @return a String containing (depth * 2 + 3) * 2 white spaces
13394 *
13395 * @hide
13396 */
13397 protected static String debugIndent(int depth) {
13398 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13399 for (int i = 0; i < (depth * 2) + 3; i++) {
13400 spaces.append(' ').append(' ');
13401 }
13402 return spaces.toString();
13403 }
13404
13405 /**
13406 * <p>Return the offset of the widget's text baseline from the widget's top
13407 * boundary. If this widget does not support baseline alignment, this
13408 * method returns -1. </p>
13409 *
13410 * @return the offset of the baseline within the widget's bounds or -1
13411 * if baseline alignment is not supported
13412 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013413 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013414 public int getBaseline() {
13415 return -1;
13416 }
13417
13418 /**
13419 * Call this when something has changed which has invalidated the
13420 * layout of this view. This will schedule a layout pass of the view
13421 * tree.
13422 */
13423 public void requestLayout() {
13424 if (ViewDebug.TRACE_HIERARCHY) {
13425 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13426 }
13427
13428 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013429 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013430
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013431 if (mParent != null) {
13432 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013433 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013434 }
13435 if (!mParent.isLayoutRequested()) {
13436 mParent.requestLayout();
13437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013438 }
13439 }
13440
13441 /**
13442 * Forces this view to be laid out during the next layout pass.
13443 * This method does not call requestLayout() or forceLayout()
13444 * on the parent.
13445 */
13446 public void forceLayout() {
13447 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013448 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013449 }
13450
13451 /**
13452 * <p>
13453 * This is called to find out how big a view should be. The parent
13454 * supplies constraint information in the width and height parameters.
13455 * </p>
13456 *
13457 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013458 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013459 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013460 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013461 * </p>
13462 *
13463 *
13464 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13465 * parent
13466 * @param heightMeasureSpec Vertical space requirements as imposed by the
13467 * parent
13468 *
13469 * @see #onMeasure(int, int)
13470 */
13471 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13472 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13473 widthMeasureSpec != mOldWidthMeasureSpec ||
13474 heightMeasureSpec != mOldHeightMeasureSpec) {
13475
13476 // first clears the measured dimension flag
13477 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13478
13479 if (ViewDebug.TRACE_HIERARCHY) {
13480 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13481 }
13482
13483 // measure ourselves, this should set the measured dimension flag back
13484 onMeasure(widthMeasureSpec, heightMeasureSpec);
13485
13486 // flag not set, setMeasuredDimension() was not invoked, we raise
13487 // an exception to warn the developer
13488 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13489 throw new IllegalStateException("onMeasure() did not set the"
13490 + " measured dimension by calling"
13491 + " setMeasuredDimension()");
13492 }
13493
13494 mPrivateFlags |= LAYOUT_REQUIRED;
13495 }
13496
13497 mOldWidthMeasureSpec = widthMeasureSpec;
13498 mOldHeightMeasureSpec = heightMeasureSpec;
13499 }
13500
13501 /**
13502 * <p>
13503 * Measure the view and its content to determine the measured width and the
13504 * measured height. This method is invoked by {@link #measure(int, int)} and
13505 * should be overriden by subclasses to provide accurate and efficient
13506 * measurement of their contents.
13507 * </p>
13508 *
13509 * <p>
13510 * <strong>CONTRACT:</strong> When overriding this method, you
13511 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13512 * measured width and height of this view. Failure to do so will trigger an
13513 * <code>IllegalStateException</code>, thrown by
13514 * {@link #measure(int, int)}. Calling the superclass'
13515 * {@link #onMeasure(int, int)} is a valid use.
13516 * </p>
13517 *
13518 * <p>
13519 * The base class implementation of measure defaults to the background size,
13520 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13521 * override {@link #onMeasure(int, int)} to provide better measurements of
13522 * their content.
13523 * </p>
13524 *
13525 * <p>
13526 * If this method is overridden, it is the subclass's responsibility to make
13527 * sure the measured height and width are at least the view's minimum height
13528 * and width ({@link #getSuggestedMinimumHeight()} and
13529 * {@link #getSuggestedMinimumWidth()}).
13530 * </p>
13531 *
13532 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13533 * The requirements are encoded with
13534 * {@link android.view.View.MeasureSpec}.
13535 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13536 * The requirements are encoded with
13537 * {@link android.view.View.MeasureSpec}.
13538 *
13539 * @see #getMeasuredWidth()
13540 * @see #getMeasuredHeight()
13541 * @see #setMeasuredDimension(int, int)
13542 * @see #getSuggestedMinimumHeight()
13543 * @see #getSuggestedMinimumWidth()
13544 * @see android.view.View.MeasureSpec#getMode(int)
13545 * @see android.view.View.MeasureSpec#getSize(int)
13546 */
13547 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13548 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13549 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13550 }
13551
13552 /**
13553 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13554 * measured width and measured height. Failing to do so will trigger an
13555 * exception at measurement time.</p>
13556 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013557 * @param measuredWidth The measured width of this view. May be a complex
13558 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13559 * {@link #MEASURED_STATE_TOO_SMALL}.
13560 * @param measuredHeight The measured height of this view. May be a complex
13561 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13562 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013563 */
13564 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13565 mMeasuredWidth = measuredWidth;
13566 mMeasuredHeight = measuredHeight;
13567
13568 mPrivateFlags |= MEASURED_DIMENSION_SET;
13569 }
13570
13571 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013572 * Merge two states as returned by {@link #getMeasuredState()}.
13573 * @param curState The current state as returned from a view or the result
13574 * of combining multiple views.
13575 * @param newState The new view state to combine.
13576 * @return Returns a new integer reflecting the combination of the two
13577 * states.
13578 */
13579 public static int combineMeasuredStates(int curState, int newState) {
13580 return curState | newState;
13581 }
13582
13583 /**
13584 * Version of {@link #resolveSizeAndState(int, int, int)}
13585 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13586 */
13587 public static int resolveSize(int size, int measureSpec) {
13588 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13589 }
13590
13591 /**
13592 * Utility to reconcile a desired size and state, with constraints imposed
13593 * by a MeasureSpec. Will take the desired size, unless a different size
13594 * is imposed by the constraints. The returned value is a compound integer,
13595 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13596 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13597 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013598 *
13599 * @param size How big the view wants to be
13600 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013601 * @return Size information bit mask as defined by
13602 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013604 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013605 int result = size;
13606 int specMode = MeasureSpec.getMode(measureSpec);
13607 int specSize = MeasureSpec.getSize(measureSpec);
13608 switch (specMode) {
13609 case MeasureSpec.UNSPECIFIED:
13610 result = size;
13611 break;
13612 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013613 if (specSize < size) {
13614 result = specSize | MEASURED_STATE_TOO_SMALL;
13615 } else {
13616 result = size;
13617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013618 break;
13619 case MeasureSpec.EXACTLY:
13620 result = specSize;
13621 break;
13622 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013623 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013624 }
13625
13626 /**
13627 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013628 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629 * by the MeasureSpec.
13630 *
13631 * @param size Default size for this view
13632 * @param measureSpec Constraints imposed by the parent
13633 * @return The size this view should be.
13634 */
13635 public static int getDefaultSize(int size, int measureSpec) {
13636 int result = size;
13637 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013638 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013639
13640 switch (specMode) {
13641 case MeasureSpec.UNSPECIFIED:
13642 result = size;
13643 break;
13644 case MeasureSpec.AT_MOST:
13645 case MeasureSpec.EXACTLY:
13646 result = specSize;
13647 break;
13648 }
13649 return result;
13650 }
13651
13652 /**
13653 * Returns the suggested minimum height that the view should use. This
13654 * returns the maximum of the view's minimum height
13655 * and the background's minimum height
13656 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13657 * <p>
13658 * When being used in {@link #onMeasure(int, int)}, the caller should still
13659 * ensure the returned height is within the requirements of the parent.
13660 *
13661 * @return The suggested minimum height of the view.
13662 */
13663 protected int getSuggestedMinimumHeight() {
13664 int suggestedMinHeight = mMinHeight;
13665
13666 if (mBGDrawable != null) {
13667 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13668 if (suggestedMinHeight < bgMinHeight) {
13669 suggestedMinHeight = bgMinHeight;
13670 }
13671 }
13672
13673 return suggestedMinHeight;
13674 }
13675
13676 /**
13677 * Returns the suggested minimum width that the view should use. This
13678 * returns the maximum of the view's minimum width)
13679 * and the background's minimum width
13680 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13681 * <p>
13682 * When being used in {@link #onMeasure(int, int)}, the caller should still
13683 * ensure the returned width is within the requirements of the parent.
13684 *
13685 * @return The suggested minimum width of the view.
13686 */
13687 protected int getSuggestedMinimumWidth() {
13688 int suggestedMinWidth = mMinWidth;
13689
13690 if (mBGDrawable != null) {
13691 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13692 if (suggestedMinWidth < bgMinWidth) {
13693 suggestedMinWidth = bgMinWidth;
13694 }
13695 }
13696
13697 return suggestedMinWidth;
13698 }
13699
13700 /**
13701 * Sets the minimum height of the view. It is not guaranteed the view will
13702 * be able to achieve this minimum height (for example, if its parent layout
13703 * constrains it with less available height).
13704 *
13705 * @param minHeight The minimum height the view will try to be.
13706 */
13707 public void setMinimumHeight(int minHeight) {
13708 mMinHeight = minHeight;
13709 }
13710
13711 /**
13712 * Sets the minimum width of the view. It is not guaranteed the view will
13713 * be able to achieve this minimum width (for example, if its parent layout
13714 * constrains it with less available width).
13715 *
13716 * @param minWidth The minimum width the view will try to be.
13717 */
13718 public void setMinimumWidth(int minWidth) {
13719 mMinWidth = minWidth;
13720 }
13721
13722 /**
13723 * Get the animation currently associated with this view.
13724 *
13725 * @return The animation that is currently playing or
13726 * scheduled to play for this view.
13727 */
13728 public Animation getAnimation() {
13729 return mCurrentAnimation;
13730 }
13731
13732 /**
13733 * Start the specified animation now.
13734 *
13735 * @param animation the animation to start now
13736 */
13737 public void startAnimation(Animation animation) {
13738 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13739 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013740 invalidateParentCaches();
13741 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013742 }
13743
13744 /**
13745 * Cancels any animations for this view.
13746 */
13747 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013748 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013749 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013752 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 }
13754
13755 /**
13756 * Sets the next animation to play for this view.
13757 * If you want the animation to play immediately, use
13758 * startAnimation. This method provides allows fine-grained
13759 * control over the start time and invalidation, but you
13760 * must make sure that 1) the animation has a start time set, and
13761 * 2) the view will be invalidated when the animation is supposed to
13762 * start.
13763 *
13764 * @param animation The next animation, or null.
13765 */
13766 public void setAnimation(Animation animation) {
13767 mCurrentAnimation = animation;
13768 if (animation != null) {
13769 animation.reset();
13770 }
13771 }
13772
13773 /**
13774 * Invoked by a parent ViewGroup to notify the start of the animation
13775 * currently associated with this view. If you override this method,
13776 * always call super.onAnimationStart();
13777 *
13778 * @see #setAnimation(android.view.animation.Animation)
13779 * @see #getAnimation()
13780 */
13781 protected void onAnimationStart() {
13782 mPrivateFlags |= ANIMATION_STARTED;
13783 }
13784
13785 /**
13786 * Invoked by a parent ViewGroup to notify the end of the animation
13787 * currently associated with this view. If you override this method,
13788 * always call super.onAnimationEnd();
13789 *
13790 * @see #setAnimation(android.view.animation.Animation)
13791 * @see #getAnimation()
13792 */
13793 protected void onAnimationEnd() {
13794 mPrivateFlags &= ~ANIMATION_STARTED;
13795 }
13796
13797 /**
13798 * Invoked if there is a Transform that involves alpha. Subclass that can
13799 * draw themselves with the specified alpha should return true, and then
13800 * respect that alpha when their onDraw() is called. If this returns false
13801 * then the view may be redirected to draw into an offscreen buffer to
13802 * fulfill the request, which will look fine, but may be slower than if the
13803 * subclass handles it internally. The default implementation returns false.
13804 *
13805 * @param alpha The alpha (0..255) to apply to the view's drawing
13806 * @return true if the view can draw with the specified alpha.
13807 */
13808 protected boolean onSetAlpha(int alpha) {
13809 return false;
13810 }
13811
13812 /**
13813 * This is used by the RootView to perform an optimization when
13814 * the view hierarchy contains one or several SurfaceView.
13815 * SurfaceView is always considered transparent, but its children are not,
13816 * therefore all View objects remove themselves from the global transparent
13817 * region (passed as a parameter to this function).
13818 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013819 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820 *
13821 * @return Returns true if the effective visibility of the view at this
13822 * point is opaque, regardless of the transparent region; returns false
13823 * if it is possible for underlying windows to be seen behind the view.
13824 *
13825 * {@hide}
13826 */
13827 public boolean gatherTransparentRegion(Region region) {
13828 final AttachInfo attachInfo = mAttachInfo;
13829 if (region != null && attachInfo != null) {
13830 final int pflags = mPrivateFlags;
13831 if ((pflags & SKIP_DRAW) == 0) {
13832 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13833 // remove it from the transparent region.
13834 final int[] location = attachInfo.mTransparentLocation;
13835 getLocationInWindow(location);
13836 region.op(location[0], location[1], location[0] + mRight - mLeft,
13837 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13838 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13839 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13840 // exists, so we remove the background drawable's non-transparent
13841 // parts from this transparent region.
13842 applyDrawableToTransparentRegion(mBGDrawable, region);
13843 }
13844 }
13845 return true;
13846 }
13847
13848 /**
13849 * Play a sound effect for this view.
13850 *
13851 * <p>The framework will play sound effects for some built in actions, such as
13852 * clicking, but you may wish to play these effects in your widget,
13853 * for instance, for internal navigation.
13854 *
13855 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13856 * {@link #isSoundEffectsEnabled()} is true.
13857 *
13858 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13859 */
13860 public void playSoundEffect(int soundConstant) {
13861 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13862 return;
13863 }
13864 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13865 }
13866
13867 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013868 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013869 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013870 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013871 *
13872 * <p>The framework will provide haptic feedback for some built in actions,
13873 * such as long presses, but you may wish to provide feedback for your
13874 * own widget.
13875 *
13876 * <p>The feedback will only be performed if
13877 * {@link #isHapticFeedbackEnabled()} is true.
13878 *
13879 * @param feedbackConstant One of the constants defined in
13880 * {@link HapticFeedbackConstants}
13881 */
13882 public boolean performHapticFeedback(int feedbackConstant) {
13883 return performHapticFeedback(feedbackConstant, 0);
13884 }
13885
13886 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013887 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013888 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013889 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 *
13891 * @param feedbackConstant One of the constants defined in
13892 * {@link HapticFeedbackConstants}
13893 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13894 */
13895 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13896 if (mAttachInfo == null) {
13897 return false;
13898 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013899 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013900 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013901 && !isHapticFeedbackEnabled()) {
13902 return false;
13903 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013904 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13905 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013906 }
13907
13908 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013909 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013910 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13911 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013912 */
13913 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013914 if (visibility != mSystemUiVisibility) {
13915 mSystemUiVisibility = visibility;
13916 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13917 mParent.recomputeViewAttributes(this);
13918 }
Joe Onorato664644d2011-01-23 17:53:23 -080013919 }
13920 }
13921
13922 /**
13923 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013924 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13925 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013926 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013927 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013928 return mSystemUiVisibility;
13929 }
13930
Scott Mainec6331b2011-05-24 16:55:56 -070013931 /**
13932 * Set a listener to receive callbacks when the visibility of the system bar changes.
13933 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13934 */
Joe Onorato664644d2011-01-23 17:53:23 -080013935 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013936 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013937 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13938 mParent.recomputeViewAttributes(this);
13939 }
13940 }
13941
13942 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013943 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13944 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013945 */
13946 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013947 ListenerInfo li = mListenerInfo;
13948 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13949 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013950 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013951 }
13952 }
13953
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013954 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13955 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13956 if (val != mSystemUiVisibility) {
13957 setSystemUiVisibility(val);
13958 }
13959 }
13960
Joe Onorato664644d2011-01-23 17:53:23 -080013961 /**
Joe Malin32736f02011-01-19 16:14:20 -080013962 * Creates an image that the system displays during the drag and drop
13963 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13964 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13965 * appearance as the given View. The default also positions the center of the drag shadow
13966 * directly under the touch point. If no View is provided (the constructor with no parameters
13967 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13968 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13969 * default is an invisible drag shadow.
13970 * <p>
13971 * You are not required to use the View you provide to the constructor as the basis of the
13972 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13973 * anything you want as the drag shadow.
13974 * </p>
13975 * <p>
13976 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13977 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13978 * size and position of the drag shadow. It uses this data to construct a
13979 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13980 * so that your application can draw the shadow image in the Canvas.
13981 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013982 *
13983 * <div class="special reference">
13984 * <h3>Developer Guides</h3>
13985 * <p>For a guide to implementing drag and drop features, read the
13986 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13987 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013988 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013989 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013990 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013991
13992 /**
Joe Malin32736f02011-01-19 16:14:20 -080013993 * Constructs a shadow image builder based on a View. By default, the resulting drag
13994 * shadow will have the same appearance and dimensions as the View, with the touch point
13995 * over the center of the View.
13996 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013997 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013998 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013999 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014000 }
14001
Christopher Tate17ed60c2011-01-18 12:50:26 -080014002 /**
14003 * Construct a shadow builder object with no associated View. This
14004 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14005 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14006 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014007 * reference to any View object. If they are not overridden, then the result is an
14008 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014009 */
14010 public DragShadowBuilder() {
14011 mView = new WeakReference<View>(null);
14012 }
14013
14014 /**
14015 * Returns the View object that had been passed to the
14016 * {@link #View.DragShadowBuilder(View)}
14017 * constructor. If that View parameter was {@code null} or if the
14018 * {@link #View.DragShadowBuilder()}
14019 * constructor was used to instantiate the builder object, this method will return
14020 * null.
14021 *
14022 * @return The View object associate with this builder object.
14023 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014024 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014025 final public View getView() {
14026 return mView.get();
14027 }
14028
Christopher Tate2c095f32010-10-04 14:13:40 -070014029 /**
Joe Malin32736f02011-01-19 16:14:20 -080014030 * Provides the metrics for the shadow image. These include the dimensions of
14031 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014032 * be centered under the touch location while dragging.
14033 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014034 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014035 * same as the dimensions of the View itself and centers the shadow under
14036 * the touch point.
14037 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014038 *
Joe Malin32736f02011-01-19 16:14:20 -080014039 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14040 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14041 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14042 * image.
14043 *
14044 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14045 * shadow image that should be underneath the touch point during the drag and drop
14046 * operation. Your application must set {@link android.graphics.Point#x} to the
14047 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014048 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014049 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014050 final View view = mView.get();
14051 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014052 shadowSize.set(view.getWidth(), view.getHeight());
14053 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014054 } else {
14055 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14056 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014057 }
14058
14059 /**
Joe Malin32736f02011-01-19 16:14:20 -080014060 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14061 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014062 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014063 *
Joe Malin32736f02011-01-19 16:14:20 -080014064 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014065 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014066 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014067 final View view = mView.get();
14068 if (view != null) {
14069 view.draw(canvas);
14070 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014071 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014072 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014073 }
14074 }
14075
14076 /**
Joe Malin32736f02011-01-19 16:14:20 -080014077 * Starts a drag and drop operation. When your application calls this method, it passes a
14078 * {@link android.view.View.DragShadowBuilder} object to the system. The
14079 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14080 * to get metrics for the drag shadow, and then calls the object's
14081 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14082 * <p>
14083 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14084 * drag events to all the View objects in your application that are currently visible. It does
14085 * this either by calling the View object's drag listener (an implementation of
14086 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14087 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14088 * Both are passed a {@link android.view.DragEvent} object that has a
14089 * {@link android.view.DragEvent#getAction()} value of
14090 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14091 * </p>
14092 * <p>
14093 * Your application can invoke startDrag() on any attached View object. The View object does not
14094 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14095 * be related to the View the user selected for dragging.
14096 * </p>
14097 * @param data A {@link android.content.ClipData} object pointing to the data to be
14098 * transferred by the drag and drop operation.
14099 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14100 * drag shadow.
14101 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14102 * drop operation. This Object is put into every DragEvent object sent by the system during the
14103 * current drag.
14104 * <p>
14105 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14106 * to the target Views. For example, it can contain flags that differentiate between a
14107 * a copy operation and a move operation.
14108 * </p>
14109 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14110 * so the parameter should be set to 0.
14111 * @return {@code true} if the method completes successfully, or
14112 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14113 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014114 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014115 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014116 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014117 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014118 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014119 }
14120 boolean okay = false;
14121
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014122 Point shadowSize = new Point();
14123 Point shadowTouchPoint = new Point();
14124 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014125
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014126 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14127 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14128 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014129 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014130
Chris Tatea32dcf72010-10-14 12:13:50 -070014131 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014132 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14133 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014134 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014135 Surface surface = new Surface();
14136 try {
14137 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014138 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014139 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014140 + " surface=" + surface);
14141 if (token != null) {
14142 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014143 try {
Chris Tate6b391282010-10-14 15:48:59 -070014144 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014145 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014146 } finally {
14147 surface.unlockCanvasAndPost(canvas);
14148 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014149
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014150 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014151
14152 // Cache the local state object for delivery with DragEvents
14153 root.setLocalDragState(myLocalState);
14154
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014155 // repurpose 'shadowSize' for the last touch point
14156 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014157
Christopher Tatea53146c2010-09-07 11:57:52 -070014158 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014159 shadowSize.x, shadowSize.y,
14160 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014161 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014162
14163 // Off and running! Release our local surface instance; the drag
14164 // shadow surface is now managed by the system process.
14165 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014166 }
14167 } catch (Exception e) {
14168 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14169 surface.destroy();
14170 }
14171
14172 return okay;
14173 }
14174
Christopher Tatea53146c2010-09-07 11:57:52 -070014175 /**
Joe Malin32736f02011-01-19 16:14:20 -080014176 * Handles drag events sent by the system following a call to
14177 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14178 *<p>
14179 * When the system calls this method, it passes a
14180 * {@link android.view.DragEvent} object. A call to
14181 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14182 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14183 * operation.
14184 * @param event The {@link android.view.DragEvent} sent by the system.
14185 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14186 * in DragEvent, indicating the type of drag event represented by this object.
14187 * @return {@code true} if the method was successful, otherwise {@code false}.
14188 * <p>
14189 * The method should return {@code true} in response to an action type of
14190 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14191 * operation.
14192 * </p>
14193 * <p>
14194 * The method should also return {@code true} in response to an action type of
14195 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14196 * {@code false} if it didn't.
14197 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014198 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014199 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014200 return false;
14201 }
14202
14203 /**
Joe Malin32736f02011-01-19 16:14:20 -080014204 * Detects if this View is enabled and has a drag event listener.
14205 * If both are true, then it calls the drag event listener with the
14206 * {@link android.view.DragEvent} it received. If the drag event listener returns
14207 * {@code true}, then dispatchDragEvent() returns {@code true}.
14208 * <p>
14209 * For all other cases, the method calls the
14210 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14211 * method and returns its result.
14212 * </p>
14213 * <p>
14214 * This ensures that a drag event is always consumed, even if the View does not have a drag
14215 * event listener. However, if the View has a listener and the listener returns true, then
14216 * onDragEvent() is not called.
14217 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014218 */
14219 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014220 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014221 ListenerInfo li = mListenerInfo;
14222 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14223 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014224 return true;
14225 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014226 return onDragEvent(event);
14227 }
14228
Christopher Tate3d4bf172011-03-28 16:16:46 -070014229 boolean canAcceptDrag() {
14230 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14231 }
14232
Christopher Tatea53146c2010-09-07 11:57:52 -070014233 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014234 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14235 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014236 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014237 */
14238 public void onCloseSystemDialogs(String reason) {
14239 }
Joe Malin32736f02011-01-19 16:14:20 -080014240
Dianne Hackbornffa42482009-09-23 22:20:11 -070014241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014242 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014243 * update a Region being computed for
14244 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014245 * that any non-transparent parts of the Drawable are removed from the
14246 * given transparent region.
14247 *
14248 * @param dr The Drawable whose transparency is to be applied to the region.
14249 * @param region A Region holding the current transparency information,
14250 * where any parts of the region that are set are considered to be
14251 * transparent. On return, this region will be modified to have the
14252 * transparency information reduced by the corresponding parts of the
14253 * Drawable that are not transparent.
14254 * {@hide}
14255 */
14256 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14257 if (DBG) {
14258 Log.i("View", "Getting transparent region for: " + this);
14259 }
14260 final Region r = dr.getTransparentRegion();
14261 final Rect db = dr.getBounds();
14262 final AttachInfo attachInfo = mAttachInfo;
14263 if (r != null && attachInfo != null) {
14264 final int w = getRight()-getLeft();
14265 final int h = getBottom()-getTop();
14266 if (db.left > 0) {
14267 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14268 r.op(0, 0, db.left, h, Region.Op.UNION);
14269 }
14270 if (db.right < w) {
14271 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14272 r.op(db.right, 0, w, h, Region.Op.UNION);
14273 }
14274 if (db.top > 0) {
14275 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14276 r.op(0, 0, w, db.top, Region.Op.UNION);
14277 }
14278 if (db.bottom < h) {
14279 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14280 r.op(0, db.bottom, w, h, Region.Op.UNION);
14281 }
14282 final int[] location = attachInfo.mTransparentLocation;
14283 getLocationInWindow(location);
14284 r.translate(location[0], location[1]);
14285 region.op(r, Region.Op.INTERSECT);
14286 } else {
14287 region.op(db, Region.Op.DIFFERENCE);
14288 }
14289 }
14290
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014291 private void checkForLongClick(int delayOffset) {
14292 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14293 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014294
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014295 if (mPendingCheckForLongPress == null) {
14296 mPendingCheckForLongPress = new CheckForLongPress();
14297 }
14298 mPendingCheckForLongPress.rememberWindowAttachCount();
14299 postDelayed(mPendingCheckForLongPress,
14300 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 }
14303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014304 /**
14305 * Inflate a view from an XML resource. This convenience method wraps the {@link
14306 * LayoutInflater} class, which provides a full range of options for view inflation.
14307 *
14308 * @param context The Context object for your activity or application.
14309 * @param resource The resource ID to inflate
14310 * @param root A view group that will be the parent. Used to properly inflate the
14311 * layout_* parameters.
14312 * @see LayoutInflater
14313 */
14314 public static View inflate(Context context, int resource, ViewGroup root) {
14315 LayoutInflater factory = LayoutInflater.from(context);
14316 return factory.inflate(resource, root);
14317 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014319 /**
Adam Powell637d3372010-08-25 14:37:03 -070014320 * Scroll the view with standard behavior for scrolling beyond the normal
14321 * content boundaries. Views that call this method should override
14322 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14323 * results of an over-scroll operation.
14324 *
14325 * Views can use this method to handle any touch or fling-based scrolling.
14326 *
14327 * @param deltaX Change in X in pixels
14328 * @param deltaY Change in Y in pixels
14329 * @param scrollX Current X scroll value in pixels before applying deltaX
14330 * @param scrollY Current Y scroll value in pixels before applying deltaY
14331 * @param scrollRangeX Maximum content scroll range along the X axis
14332 * @param scrollRangeY Maximum content scroll range along the Y axis
14333 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14334 * along the X axis.
14335 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14336 * along the Y axis.
14337 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14338 * @return true if scrolling was clamped to an over-scroll boundary along either
14339 * axis, false otherwise.
14340 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014341 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014342 protected boolean overScrollBy(int deltaX, int deltaY,
14343 int scrollX, int scrollY,
14344 int scrollRangeX, int scrollRangeY,
14345 int maxOverScrollX, int maxOverScrollY,
14346 boolean isTouchEvent) {
14347 final int overScrollMode = mOverScrollMode;
14348 final boolean canScrollHorizontal =
14349 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14350 final boolean canScrollVertical =
14351 computeVerticalScrollRange() > computeVerticalScrollExtent();
14352 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14353 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14354 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14355 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14356
14357 int newScrollX = scrollX + deltaX;
14358 if (!overScrollHorizontal) {
14359 maxOverScrollX = 0;
14360 }
14361
14362 int newScrollY = scrollY + deltaY;
14363 if (!overScrollVertical) {
14364 maxOverScrollY = 0;
14365 }
14366
14367 // Clamp values if at the limits and record
14368 final int left = -maxOverScrollX;
14369 final int right = maxOverScrollX + scrollRangeX;
14370 final int top = -maxOverScrollY;
14371 final int bottom = maxOverScrollY + scrollRangeY;
14372
14373 boolean clampedX = false;
14374 if (newScrollX > right) {
14375 newScrollX = right;
14376 clampedX = true;
14377 } else if (newScrollX < left) {
14378 newScrollX = left;
14379 clampedX = true;
14380 }
14381
14382 boolean clampedY = false;
14383 if (newScrollY > bottom) {
14384 newScrollY = bottom;
14385 clampedY = true;
14386 } else if (newScrollY < top) {
14387 newScrollY = top;
14388 clampedY = true;
14389 }
14390
14391 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14392
14393 return clampedX || clampedY;
14394 }
14395
14396 /**
14397 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14398 * respond to the results of an over-scroll operation.
14399 *
14400 * @param scrollX New X scroll value in pixels
14401 * @param scrollY New Y scroll value in pixels
14402 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14403 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14404 */
14405 protected void onOverScrolled(int scrollX, int scrollY,
14406 boolean clampedX, boolean clampedY) {
14407 // Intentionally empty.
14408 }
14409
14410 /**
14411 * Returns the over-scroll mode for this view. The result will be
14412 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14413 * (allow over-scrolling only if the view content is larger than the container),
14414 * or {@link #OVER_SCROLL_NEVER}.
14415 *
14416 * @return This view's over-scroll mode.
14417 */
14418 public int getOverScrollMode() {
14419 return mOverScrollMode;
14420 }
14421
14422 /**
14423 * Set the over-scroll mode for this view. Valid over-scroll modes are
14424 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14425 * (allow over-scrolling only if the view content is larger than the container),
14426 * or {@link #OVER_SCROLL_NEVER}.
14427 *
14428 * Setting the over-scroll mode of a view will have an effect only if the
14429 * view is capable of scrolling.
14430 *
14431 * @param overScrollMode The new over-scroll mode for this view.
14432 */
14433 public void setOverScrollMode(int overScrollMode) {
14434 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14435 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14436 overScrollMode != OVER_SCROLL_NEVER) {
14437 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14438 }
14439 mOverScrollMode = overScrollMode;
14440 }
14441
14442 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014443 * Gets a scale factor that determines the distance the view should scroll
14444 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14445 * @return The vertical scroll scale factor.
14446 * @hide
14447 */
14448 protected float getVerticalScrollFactor() {
14449 if (mVerticalScrollFactor == 0) {
14450 TypedValue outValue = new TypedValue();
14451 if (!mContext.getTheme().resolveAttribute(
14452 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14453 throw new IllegalStateException(
14454 "Expected theme to define listPreferredItemHeight.");
14455 }
14456 mVerticalScrollFactor = outValue.getDimension(
14457 mContext.getResources().getDisplayMetrics());
14458 }
14459 return mVerticalScrollFactor;
14460 }
14461
14462 /**
14463 * Gets a scale factor that determines the distance the view should scroll
14464 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14465 * @return The horizontal scroll scale factor.
14466 * @hide
14467 */
14468 protected float getHorizontalScrollFactor() {
14469 // TODO: Should use something else.
14470 return getVerticalScrollFactor();
14471 }
14472
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014473 /**
14474 * Return the value specifying the text direction or policy that was set with
14475 * {@link #setTextDirection(int)}.
14476 *
14477 * @return the defined text direction. It can be one of:
14478 *
14479 * {@link #TEXT_DIRECTION_INHERIT},
14480 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14481 * {@link #TEXT_DIRECTION_ANY_RTL},
14482 * {@link #TEXT_DIRECTION_LTR},
14483 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014484 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014485 */
14486 public int getTextDirection() {
14487 return mTextDirection;
14488 }
14489
14490 /**
14491 * Set the text direction.
14492 *
14493 * @param textDirection the direction to set. Should be one of:
14494 *
14495 * {@link #TEXT_DIRECTION_INHERIT},
14496 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14497 * {@link #TEXT_DIRECTION_ANY_RTL},
14498 * {@link #TEXT_DIRECTION_LTR},
14499 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014500 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014501 */
14502 public void setTextDirection(int textDirection) {
14503 if (textDirection != mTextDirection) {
14504 mTextDirection = textDirection;
14505 resetResolvedTextDirection();
14506 requestLayout();
14507 }
14508 }
14509
14510 /**
14511 * Return the resolved text direction.
14512 *
14513 * @return the resolved text direction. Return one of:
14514 *
Doug Feltcb3791202011-07-07 11:57:48 -070014515 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14516 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014517 * {@link #TEXT_DIRECTION_LTR},
14518 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014519 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014520 */
14521 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014522 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014523 resolveTextDirection();
14524 }
14525 return mResolvedTextDirection;
14526 }
14527
14528 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014529 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14530 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014531 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014532 public void resolveTextDirection() {
14533 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14534 // Resolution has already been done.
14535 return;
14536 }
Doug Feltcb3791202011-07-07 11:57:48 -070014537 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14538 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014539 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014540 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014541 } else {
14542 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014543 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014544 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014545 }
14546
14547 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014548 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014549 * resolution should override this method.
14550 *
14551 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014552 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014553 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014554 }
14555
14556 /**
14557 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014558 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014559 * reset is done.
14560 */
14561 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014562 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014563 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014564 }
14565
14566 /**
14567 * Called when text direction is reset. Subclasses that care about text direction reset should
14568 * override this method and do a reset of the text direction of their children. The default
14569 * implementation does nothing.
14570 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014571 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014572 }
14573
Chet Haaseb39f0512011-05-24 14:36:40 -070014574 //
14575 // Properties
14576 //
14577 /**
14578 * A Property wrapper around the <code>alpha</code> functionality handled by the
14579 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14580 */
Chet Haased47f1532011-12-16 11:18:52 -080014581 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014582 @Override
14583 public void setValue(View object, float value) {
14584 object.setAlpha(value);
14585 }
14586
14587 @Override
14588 public Float get(View object) {
14589 return object.getAlpha();
14590 }
14591 };
14592
14593 /**
14594 * A Property wrapper around the <code>translationX</code> functionality handled by the
14595 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14596 */
Chet Haased47f1532011-12-16 11:18:52 -080014597 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014598 @Override
14599 public void setValue(View object, float value) {
14600 object.setTranslationX(value);
14601 }
14602
14603 @Override
14604 public Float get(View object) {
14605 return object.getTranslationX();
14606 }
14607 };
14608
14609 /**
14610 * A Property wrapper around the <code>translationY</code> functionality handled by the
14611 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14612 */
Chet Haased47f1532011-12-16 11:18:52 -080014613 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014614 @Override
14615 public void setValue(View object, float value) {
14616 object.setTranslationY(value);
14617 }
14618
14619 @Override
14620 public Float get(View object) {
14621 return object.getTranslationY();
14622 }
14623 };
14624
14625 /**
14626 * A Property wrapper around the <code>x</code> functionality handled by the
14627 * {@link View#setX(float)} and {@link View#getX()} methods.
14628 */
Chet Haased47f1532011-12-16 11:18:52 -080014629 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014630 @Override
14631 public void setValue(View object, float value) {
14632 object.setX(value);
14633 }
14634
14635 @Override
14636 public Float get(View object) {
14637 return object.getX();
14638 }
14639 };
14640
14641 /**
14642 * A Property wrapper around the <code>y</code> functionality handled by the
14643 * {@link View#setY(float)} and {@link View#getY()} methods.
14644 */
Chet Haased47f1532011-12-16 11:18:52 -080014645 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014646 @Override
14647 public void setValue(View object, float value) {
14648 object.setY(value);
14649 }
14650
14651 @Override
14652 public Float get(View object) {
14653 return object.getY();
14654 }
14655 };
14656
14657 /**
14658 * A Property wrapper around the <code>rotation</code> functionality handled by the
14659 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14660 */
Chet Haased47f1532011-12-16 11:18:52 -080014661 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014662 @Override
14663 public void setValue(View object, float value) {
14664 object.setRotation(value);
14665 }
14666
14667 @Override
14668 public Float get(View object) {
14669 return object.getRotation();
14670 }
14671 };
14672
14673 /**
14674 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14675 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14676 */
Chet Haased47f1532011-12-16 11:18:52 -080014677 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014678 @Override
14679 public void setValue(View object, float value) {
14680 object.setRotationX(value);
14681 }
14682
14683 @Override
14684 public Float get(View object) {
14685 return object.getRotationX();
14686 }
14687 };
14688
14689 /**
14690 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14691 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14692 */
Chet Haased47f1532011-12-16 11:18:52 -080014693 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014694 @Override
14695 public void setValue(View object, float value) {
14696 object.setRotationY(value);
14697 }
14698
14699 @Override
14700 public Float get(View object) {
14701 return object.getRotationY();
14702 }
14703 };
14704
14705 /**
14706 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14707 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14708 */
Chet Haased47f1532011-12-16 11:18:52 -080014709 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014710 @Override
14711 public void setValue(View object, float value) {
14712 object.setScaleX(value);
14713 }
14714
14715 @Override
14716 public Float get(View object) {
14717 return object.getScaleX();
14718 }
14719 };
14720
14721 /**
14722 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14723 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14724 */
Chet Haased47f1532011-12-16 11:18:52 -080014725 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014726 @Override
14727 public void setValue(View object, float value) {
14728 object.setScaleY(value);
14729 }
14730
14731 @Override
14732 public Float get(View object) {
14733 return object.getScaleY();
14734 }
14735 };
14736
Jeff Brown33bbfd22011-02-24 20:55:35 -080014737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14739 * Each MeasureSpec represents a requirement for either the width or the height.
14740 * A MeasureSpec is comprised of a size and a mode. There are three possible
14741 * modes:
14742 * <dl>
14743 * <dt>UNSPECIFIED</dt>
14744 * <dd>
14745 * The parent has not imposed any constraint on the child. It can be whatever size
14746 * it wants.
14747 * </dd>
14748 *
14749 * <dt>EXACTLY</dt>
14750 * <dd>
14751 * The parent has determined an exact size for the child. The child is going to be
14752 * given those bounds regardless of how big it wants to be.
14753 * </dd>
14754 *
14755 * <dt>AT_MOST</dt>
14756 * <dd>
14757 * The child can be as large as it wants up to the specified size.
14758 * </dd>
14759 * </dl>
14760 *
14761 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14762 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14763 */
14764 public static class MeasureSpec {
14765 private static final int MODE_SHIFT = 30;
14766 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14767
14768 /**
14769 * Measure specification mode: The parent has not imposed any constraint
14770 * on the child. It can be whatever size it wants.
14771 */
14772 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14773
14774 /**
14775 * Measure specification mode: The parent has determined an exact size
14776 * for the child. The child is going to be given those bounds regardless
14777 * of how big it wants to be.
14778 */
14779 public static final int EXACTLY = 1 << MODE_SHIFT;
14780
14781 /**
14782 * Measure specification mode: The child can be as large as it wants up
14783 * to the specified size.
14784 */
14785 public static final int AT_MOST = 2 << MODE_SHIFT;
14786
14787 /**
14788 * Creates a measure specification based on the supplied size and mode.
14789 *
14790 * The mode must always be one of the following:
14791 * <ul>
14792 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14793 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14794 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14795 * </ul>
14796 *
14797 * @param size the size of the measure specification
14798 * @param mode the mode of the measure specification
14799 * @return the measure specification based on size and mode
14800 */
14801 public static int makeMeasureSpec(int size, int mode) {
14802 return size + mode;
14803 }
14804
14805 /**
14806 * Extracts the mode from the supplied measure specification.
14807 *
14808 * @param measureSpec the measure specification to extract the mode from
14809 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14810 * {@link android.view.View.MeasureSpec#AT_MOST} or
14811 * {@link android.view.View.MeasureSpec#EXACTLY}
14812 */
14813 public static int getMode(int measureSpec) {
14814 return (measureSpec & MODE_MASK);
14815 }
14816
14817 /**
14818 * Extracts the size from the supplied measure specification.
14819 *
14820 * @param measureSpec the measure specification to extract the size from
14821 * @return the size in pixels defined in the supplied measure specification
14822 */
14823 public static int getSize(int measureSpec) {
14824 return (measureSpec & ~MODE_MASK);
14825 }
14826
14827 /**
14828 * Returns a String representation of the specified measure
14829 * specification.
14830 *
14831 * @param measureSpec the measure specification to convert to a String
14832 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14833 */
14834 public static String toString(int measureSpec) {
14835 int mode = getMode(measureSpec);
14836 int size = getSize(measureSpec);
14837
14838 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14839
14840 if (mode == UNSPECIFIED)
14841 sb.append("UNSPECIFIED ");
14842 else if (mode == EXACTLY)
14843 sb.append("EXACTLY ");
14844 else if (mode == AT_MOST)
14845 sb.append("AT_MOST ");
14846 else
14847 sb.append(mode).append(" ");
14848
14849 sb.append(size);
14850 return sb.toString();
14851 }
14852 }
14853
14854 class CheckForLongPress implements Runnable {
14855
14856 private int mOriginalWindowAttachCount;
14857
14858 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014859 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 && mOriginalWindowAttachCount == mWindowAttachCount) {
14861 if (performLongClick()) {
14862 mHasPerformedLongPress = true;
14863 }
14864 }
14865 }
14866
14867 public void rememberWindowAttachCount() {
14868 mOriginalWindowAttachCount = mWindowAttachCount;
14869 }
14870 }
Joe Malin32736f02011-01-19 16:14:20 -080014871
Adam Powelle14579b2009-12-16 18:39:52 -080014872 private final class CheckForTap implements Runnable {
14873 public void run() {
14874 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014875 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014876 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014877 }
14878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014879
Adam Powella35d7682010-03-12 14:48:13 -080014880 private final class PerformClick implements Runnable {
14881 public void run() {
14882 performClick();
14883 }
14884 }
14885
Dianne Hackborn63042d62011-01-26 18:56:29 -080014886 /** @hide */
14887 public void hackTurnOffWindowResizeAnim(boolean off) {
14888 mAttachInfo.mTurnOffWindowResizeAnim = off;
14889 }
Joe Malin32736f02011-01-19 16:14:20 -080014890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014891 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014892 * This method returns a ViewPropertyAnimator object, which can be used to animate
14893 * specific properties on this View.
14894 *
14895 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14896 */
14897 public ViewPropertyAnimator animate() {
14898 if (mAnimator == null) {
14899 mAnimator = new ViewPropertyAnimator(this);
14900 }
14901 return mAnimator;
14902 }
14903
14904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014905 * Interface definition for a callback to be invoked when a key event is
14906 * dispatched to this view. The callback will be invoked before the key
14907 * event is given to the view.
14908 */
14909 public interface OnKeyListener {
14910 /**
14911 * Called when a key is dispatched to a view. This allows listeners to
14912 * get a chance to respond before the target view.
14913 *
14914 * @param v The view the key has been dispatched to.
14915 * @param keyCode The code for the physical key that was pressed
14916 * @param event The KeyEvent object containing full information about
14917 * the event.
14918 * @return True if the listener has consumed the event, false otherwise.
14919 */
14920 boolean onKey(View v, int keyCode, KeyEvent event);
14921 }
14922
14923 /**
14924 * Interface definition for a callback to be invoked when a touch event is
14925 * dispatched to this view. The callback will be invoked before the touch
14926 * event is given to the view.
14927 */
14928 public interface OnTouchListener {
14929 /**
14930 * Called when a touch event is dispatched to a view. This allows listeners to
14931 * get a chance to respond before the target view.
14932 *
14933 * @param v The view the touch event has been dispatched to.
14934 * @param event The MotionEvent object containing full information about
14935 * the event.
14936 * @return True if the listener has consumed the event, false otherwise.
14937 */
14938 boolean onTouch(View v, MotionEvent event);
14939 }
14940
14941 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014942 * Interface definition for a callback to be invoked when a hover event is
14943 * dispatched to this view. The callback will be invoked before the hover
14944 * event is given to the view.
14945 */
14946 public interface OnHoverListener {
14947 /**
14948 * Called when a hover event is dispatched to a view. This allows listeners to
14949 * get a chance to respond before the target view.
14950 *
14951 * @param v The view the hover event has been dispatched to.
14952 * @param event The MotionEvent object containing full information about
14953 * the event.
14954 * @return True if the listener has consumed the event, false otherwise.
14955 */
14956 boolean onHover(View v, MotionEvent event);
14957 }
14958
14959 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014960 * Interface definition for a callback to be invoked when a generic motion event is
14961 * dispatched to this view. The callback will be invoked before the generic motion
14962 * event is given to the view.
14963 */
14964 public interface OnGenericMotionListener {
14965 /**
14966 * Called when a generic motion event is dispatched to a view. This allows listeners to
14967 * get a chance to respond before the target view.
14968 *
14969 * @param v The view the generic motion event has been dispatched to.
14970 * @param event The MotionEvent object containing full information about
14971 * the event.
14972 * @return True if the listener has consumed the event, false otherwise.
14973 */
14974 boolean onGenericMotion(View v, MotionEvent event);
14975 }
14976
14977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014978 * Interface definition for a callback to be invoked when a view has been clicked and held.
14979 */
14980 public interface OnLongClickListener {
14981 /**
14982 * Called when a view has been clicked and held.
14983 *
14984 * @param v The view that was clicked and held.
14985 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014986 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014987 */
14988 boolean onLongClick(View v);
14989 }
14990
14991 /**
Chris Tate32affef2010-10-18 15:29:21 -070014992 * Interface definition for a callback to be invoked when a drag is being dispatched
14993 * to this view. The callback will be invoked before the hosting view's own
14994 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14995 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014996 *
14997 * <div class="special reference">
14998 * <h3>Developer Guides</h3>
14999 * <p>For a guide to implementing drag and drop features, read the
15000 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15001 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015002 */
15003 public interface OnDragListener {
15004 /**
15005 * Called when a drag event is dispatched to a view. This allows listeners
15006 * to get a chance to override base View behavior.
15007 *
Joe Malin32736f02011-01-19 16:14:20 -080015008 * @param v The View that received the drag event.
15009 * @param event The {@link android.view.DragEvent} object for the drag event.
15010 * @return {@code true} if the drag event was handled successfully, or {@code false}
15011 * if the drag event was not handled. Note that {@code false} will trigger the View
15012 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015013 */
15014 boolean onDrag(View v, DragEvent event);
15015 }
15016
15017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015018 * Interface definition for a callback to be invoked when the focus state of
15019 * a view changed.
15020 */
15021 public interface OnFocusChangeListener {
15022 /**
15023 * Called when the focus state of a view has changed.
15024 *
15025 * @param v The view whose state has changed.
15026 * @param hasFocus The new focus state of v.
15027 */
15028 void onFocusChange(View v, boolean hasFocus);
15029 }
15030
15031 /**
15032 * Interface definition for a callback to be invoked when a view is clicked.
15033 */
15034 public interface OnClickListener {
15035 /**
15036 * Called when a view has been clicked.
15037 *
15038 * @param v The view that was clicked.
15039 */
15040 void onClick(View v);
15041 }
15042
15043 /**
15044 * Interface definition for a callback to be invoked when the context menu
15045 * for this view is being built.
15046 */
15047 public interface OnCreateContextMenuListener {
15048 /**
15049 * Called when the context menu for this view is being built. It is not
15050 * safe to hold onto the menu after this method returns.
15051 *
15052 * @param menu The context menu that is being built
15053 * @param v The view for which the context menu is being built
15054 * @param menuInfo Extra information about the item for which the
15055 * context menu should be shown. This information will vary
15056 * depending on the class of v.
15057 */
15058 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15059 }
15060
Joe Onorato664644d2011-01-23 17:53:23 -080015061 /**
15062 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015063 * visibility. This reports <strong>global</strong> changes to the system UI
15064 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015065 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070015066 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015067 */
15068 public interface OnSystemUiVisibilityChangeListener {
15069 /**
15070 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015071 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015072 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015073 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015074 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15075 * <strong>global</strong> state of the UI visibility flags, not what your
15076 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015077 */
15078 public void onSystemUiVisibilityChange(int visibility);
15079 }
15080
Adam Powell4afd62b2011-02-18 15:02:18 -080015081 /**
15082 * Interface definition for a callback to be invoked when this view is attached
15083 * or detached from its window.
15084 */
15085 public interface OnAttachStateChangeListener {
15086 /**
15087 * Called when the view is attached to a window.
15088 * @param v The view that was attached
15089 */
15090 public void onViewAttachedToWindow(View v);
15091 /**
15092 * Called when the view is detached from a window.
15093 * @param v The view that was detached
15094 */
15095 public void onViewDetachedFromWindow(View v);
15096 }
15097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015098 private final class UnsetPressedState implements Runnable {
15099 public void run() {
15100 setPressed(false);
15101 }
15102 }
15103
15104 /**
15105 * Base class for derived classes that want to save and restore their own
15106 * state in {@link android.view.View#onSaveInstanceState()}.
15107 */
15108 public static class BaseSavedState extends AbsSavedState {
15109 /**
15110 * Constructor used when reading from a parcel. Reads the state of the superclass.
15111 *
15112 * @param source
15113 */
15114 public BaseSavedState(Parcel source) {
15115 super(source);
15116 }
15117
15118 /**
15119 * Constructor called by derived classes when creating their SavedState objects
15120 *
15121 * @param superState The state of the superclass of this view
15122 */
15123 public BaseSavedState(Parcelable superState) {
15124 super(superState);
15125 }
15126
15127 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15128 new Parcelable.Creator<BaseSavedState>() {
15129 public BaseSavedState createFromParcel(Parcel in) {
15130 return new BaseSavedState(in);
15131 }
15132
15133 public BaseSavedState[] newArray(int size) {
15134 return new BaseSavedState[size];
15135 }
15136 };
15137 }
15138
15139 /**
15140 * A set of information given to a view when it is attached to its parent
15141 * window.
15142 */
15143 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144 interface Callbacks {
15145 void playSoundEffect(int effectId);
15146 boolean performHapticFeedback(int effectId, boolean always);
15147 }
15148
15149 /**
15150 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
15151 * to a Handler. This class contains the target (View) to invalidate and
15152 * the coordinates of the dirty rectangle.
15153 *
15154 * For performance purposes, this class also implements a pool of up to
15155 * POOL_LIMIT objects that get reused. This reduces memory allocations
15156 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015157 */
Romain Guyd928d682009-03-31 17:52:16 -070015158 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015159 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070015160 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
15161 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070015162 public InvalidateInfo newInstance() {
15163 return new InvalidateInfo();
15164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015165
Romain Guyd928d682009-03-31 17:52:16 -070015166 public void onAcquired(InvalidateInfo element) {
15167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015168
Romain Guyd928d682009-03-31 17:52:16 -070015169 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070015170 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070015171 }
15172 }, POOL_LIMIT)
15173 );
15174
15175 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015176 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015177
15178 View target;
15179
15180 int left;
15181 int top;
15182 int right;
15183 int bottom;
15184
Romain Guyd928d682009-03-31 17:52:16 -070015185 public void setNextPoolable(InvalidateInfo element) {
15186 mNext = element;
15187 }
15188
15189 public InvalidateInfo getNextPoolable() {
15190 return mNext;
15191 }
15192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015193 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070015194 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015195 }
15196
15197 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070015198 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015199 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015200
15201 public boolean isPooled() {
15202 return mIsPooled;
15203 }
15204
15205 public void setPooled(boolean isPooled) {
15206 mIsPooled = isPooled;
15207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015208 }
15209
15210 final IWindowSession mSession;
15211
15212 final IWindow mWindow;
15213
15214 final IBinder mWindowToken;
15215
15216 final Callbacks mRootCallbacks;
15217
Romain Guy59a12ca2011-06-09 17:48:21 -070015218 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080015219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015220 /**
15221 * The top view of the hierarchy.
15222 */
15223 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070015224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015225 IBinder mPanelParentWindowToken;
15226 Surface mSurface;
15227
Romain Guyb051e892010-09-28 19:09:36 -070015228 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080015229 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070015230 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080015231
Romain Guy7e4e5612012-03-05 14:37:29 -080015232 boolean mScreenOn;
15233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015234 /**
Romain Guy8506ab42009-06-11 17:35:47 -070015235 * Scale factor used by the compatibility mode
15236 */
15237 float mApplicationScale;
15238
15239 /**
15240 * Indicates whether the application is in compatibility mode
15241 */
15242 boolean mScalingRequired;
15243
15244 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015245 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080015246 */
15247 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080015248
Dianne Hackborn63042d62011-01-26 18:56:29 -080015249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015250 * Left position of this view's window
15251 */
15252 int mWindowLeft;
15253
15254 /**
15255 * Top position of this view's window
15256 */
15257 int mWindowTop;
15258
15259 /**
Adam Powell26153a32010-11-08 15:22:27 -080015260 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070015261 */
Adam Powell26153a32010-11-08 15:22:27 -080015262 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015263
15264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015265 * For windows that are full-screen but using insets to layout inside
15266 * of the screen decorations, these are the current insets for the
15267 * content of the window.
15268 */
15269 final Rect mContentInsets = new Rect();
15270
15271 /**
15272 * For windows that are full-screen but using insets to layout inside
15273 * of the screen decorations, these are the current insets for the
15274 * actual visible parts of the window.
15275 */
15276 final Rect mVisibleInsets = new Rect();
15277
15278 /**
15279 * The internal insets given by this window. This value is
15280 * supplied by the client (through
15281 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15282 * be given to the window manager when changed to be used in laying
15283 * out windows behind it.
15284 */
15285 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15286 = new ViewTreeObserver.InternalInsetsInfo();
15287
15288 /**
15289 * All views in the window's hierarchy that serve as scroll containers,
15290 * used to determine if the window can be resized or must be panned
15291 * to adjust for a soft input area.
15292 */
15293 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15294
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015295 final KeyEvent.DispatcherState mKeyDispatchState
15296 = new KeyEvent.DispatcherState();
15297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015298 /**
15299 * Indicates whether the view's window currently has the focus.
15300 */
15301 boolean mHasWindowFocus;
15302
15303 /**
15304 * The current visibility of the window.
15305 */
15306 int mWindowVisibility;
15307
15308 /**
15309 * Indicates the time at which drawing started to occur.
15310 */
15311 long mDrawingTime;
15312
15313 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015314 * Indicates whether or not ignoring the DIRTY_MASK flags.
15315 */
15316 boolean mIgnoreDirtyState;
15317
15318 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015319 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15320 * to avoid clearing that flag prematurely.
15321 */
15322 boolean mSetIgnoreDirtyState = false;
15323
15324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015325 * Indicates whether the view's window is currently in touch mode.
15326 */
15327 boolean mInTouchMode;
15328
15329 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015330 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015331 * the next time it performs a traversal
15332 */
15333 boolean mRecomputeGlobalAttributes;
15334
15335 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015336 * Always report new attributes at next traversal.
15337 */
15338 boolean mForceReportNewAttributes;
15339
15340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015341 * Set during a traveral if any views want to keep the screen on.
15342 */
15343 boolean mKeepScreenOn;
15344
15345 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015346 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15347 */
15348 int mSystemUiVisibility;
15349
15350 /**
15351 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15352 * attached.
15353 */
15354 boolean mHasSystemUiListeners;
15355
15356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015357 * Set if the visibility of any views has changed.
15358 */
15359 boolean mViewVisibilityChanged;
15360
15361 /**
15362 * Set to true if a view has been scrolled.
15363 */
15364 boolean mViewScrollChanged;
15365
15366 /**
15367 * Global to the view hierarchy used as a temporary for dealing with
15368 * x/y points in the transparent region computations.
15369 */
15370 final int[] mTransparentLocation = new int[2];
15371
15372 /**
15373 * Global to the view hierarchy used as a temporary for dealing with
15374 * x/y points in the ViewGroup.invalidateChild implementation.
15375 */
15376 final int[] mInvalidateChildLocation = new int[2];
15377
Chet Haasec3aa3612010-06-17 08:50:37 -070015378
15379 /**
15380 * Global to the view hierarchy used as a temporary for dealing with
15381 * x/y location when view is transformed.
15382 */
15383 final float[] mTmpTransformLocation = new float[2];
15384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015385 /**
15386 * The view tree observer used to dispatch global events like
15387 * layout, pre-draw, touch mode change, etc.
15388 */
15389 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15390
15391 /**
15392 * A Canvas used by the view hierarchy to perform bitmap caching.
15393 */
15394 Canvas mCanvas;
15395
15396 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015397 * The view root impl.
15398 */
15399 final ViewRootImpl mViewRootImpl;
15400
15401 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015402 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015403 * handler can be used to pump events in the UI events queue.
15404 */
15405 final Handler mHandler;
15406
15407 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015408 * Temporary for use in computing invalidate rectangles while
15409 * calling up the hierarchy.
15410 */
15411 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015412
15413 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015414 * Temporary for use in computing hit areas with transformed views
15415 */
15416 final RectF mTmpTransformRect = new RectF();
15417
15418 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015419 * Temporary list for use in collecting focusable descendents of a view.
15420 */
15421 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015423 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015424 * The id of the window for accessibility purposes.
15425 */
15426 int mAccessibilityWindowId = View.NO_ID;
15427
15428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015429 * Creates a new set of attachment information with the specified
15430 * events handler and thread.
15431 *
15432 * @param handler the events handler the view must use
15433 */
15434 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015435 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015436 mSession = session;
15437 mWindow = window;
15438 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015439 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015440 mHandler = handler;
15441 mRootCallbacks = effectPlayer;
15442 }
15443 }
15444
15445 /**
15446 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15447 * is supported. This avoids keeping too many unused fields in most
15448 * instances of View.</p>
15449 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015450 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015451
Mike Cleronf116bf82009-09-27 19:14:12 -070015452 /**
15453 * Scrollbars are not visible
15454 */
15455 public static final int OFF = 0;
15456
15457 /**
15458 * Scrollbars are visible
15459 */
15460 public static final int ON = 1;
15461
15462 /**
15463 * Scrollbars are fading away
15464 */
15465 public static final int FADING = 2;
15466
15467 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015469 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015470 public int scrollBarDefaultDelayBeforeFade;
15471 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015472
15473 public int scrollBarSize;
15474 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015475 public float[] interpolatorValues;
15476 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015477
15478 public final Paint paint;
15479 public final Matrix matrix;
15480 public Shader shader;
15481
Mike Cleronf116bf82009-09-27 19:14:12 -070015482 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15483
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015484 private static final float[] OPAQUE = { 255 };
15485 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015486
Mike Cleronf116bf82009-09-27 19:14:12 -070015487 /**
15488 * When fading should start. This time moves into the future every time
15489 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15490 */
15491 public long fadeStartTime;
15492
15493
15494 /**
15495 * The current state of the scrollbars: ON, OFF, or FADING
15496 */
15497 public int state = OFF;
15498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015499 private int mLastColor;
15500
Mike Cleronf116bf82009-09-27 19:14:12 -070015501 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015502 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15503 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015504 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15505 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015506
15507 paint = new Paint();
15508 matrix = new Matrix();
15509 // use use a height of 1, and then wack the matrix each time we
15510 // actually use it.
15511 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015513 paint.setShader(shader);
15514 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015515 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015516 }
Romain Guy8506ab42009-06-11 17:35:47 -070015517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015518 public void setFadeColor(int color) {
15519 if (color != 0 && color != mLastColor) {
15520 mLastColor = color;
15521 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015522
Romain Guye55e1a72009-08-27 10:42:26 -070015523 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15524 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526 paint.setShader(shader);
15527 // Restore the default transfer mode (src_over)
15528 paint.setXfermode(null);
15529 }
15530 }
Joe Malin32736f02011-01-19 16:14:20 -080015531
Mike Cleronf116bf82009-09-27 19:14:12 -070015532 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015533 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015534 if (now >= fadeStartTime) {
15535
15536 // the animation fades the scrollbars out by changing
15537 // the opacity (alpha) from fully opaque to fully
15538 // transparent
15539 int nextFrame = (int) now;
15540 int framesCount = 0;
15541
15542 Interpolator interpolator = scrollBarInterpolator;
15543
15544 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015545 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015546
15547 // End transparent
15548 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015549 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015550
15551 state = FADING;
15552
15553 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015554 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015555 }
15556 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015557 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015558
Svetoslav Ganova0156172011-06-26 17:55:44 -070015559 /**
15560 * Resuable callback for sending
15561 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15562 */
15563 private class SendViewScrolledAccessibilityEvent implements Runnable {
15564 public volatile boolean mIsPending;
15565
15566 public void run() {
15567 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15568 mIsPending = false;
15569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015570 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015571
15572 /**
15573 * <p>
15574 * This class represents a delegate that can be registered in a {@link View}
15575 * to enhance accessibility support via composition rather via inheritance.
15576 * It is specifically targeted to widget developers that extend basic View
15577 * classes i.e. classes in package android.view, that would like their
15578 * applications to be backwards compatible.
15579 * </p>
15580 * <p>
15581 * A scenario in which a developer would like to use an accessibility delegate
15582 * is overriding a method introduced in a later API version then the minimal API
15583 * version supported by the application. For example, the method
15584 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15585 * in API version 4 when the accessibility APIs were first introduced. If a
15586 * developer would like his application to run on API version 4 devices (assuming
15587 * all other APIs used by the application are version 4 or lower) and take advantage
15588 * of this method, instead of overriding the method which would break the application's
15589 * backwards compatibility, he can override the corresponding method in this
15590 * delegate and register the delegate in the target View if the API version of
15591 * the system is high enough i.e. the API version is same or higher to the API
15592 * version that introduced
15593 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15594 * </p>
15595 * <p>
15596 * Here is an example implementation:
15597 * </p>
15598 * <code><pre><p>
15599 * if (Build.VERSION.SDK_INT >= 14) {
15600 * // If the API version is equal of higher than the version in
15601 * // which onInitializeAccessibilityNodeInfo was introduced we
15602 * // register a delegate with a customized implementation.
15603 * View view = findViewById(R.id.view_id);
15604 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15605 * public void onInitializeAccessibilityNodeInfo(View host,
15606 * AccessibilityNodeInfo info) {
15607 * // Let the default implementation populate the info.
15608 * super.onInitializeAccessibilityNodeInfo(host, info);
15609 * // Set some other information.
15610 * info.setEnabled(host.isEnabled());
15611 * }
15612 * });
15613 * }
15614 * </code></pre></p>
15615 * <p>
15616 * This delegate contains methods that correspond to the accessibility methods
15617 * in View. If a delegate has been specified the implementation in View hands
15618 * off handling to the corresponding method in this delegate. The default
15619 * implementation the delegate methods behaves exactly as the corresponding
15620 * method in View for the case of no accessibility delegate been set. Hence,
15621 * to customize the behavior of a View method, clients can override only the
15622 * corresponding delegate method without altering the behavior of the rest
15623 * accessibility related methods of the host view.
15624 * </p>
15625 */
15626 public static class AccessibilityDelegate {
15627
15628 /**
15629 * Sends an accessibility event of the given type. If accessibility is not
15630 * enabled this method has no effect.
15631 * <p>
15632 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15633 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15634 * been set.
15635 * </p>
15636 *
15637 * @param host The View hosting the delegate.
15638 * @param eventType The type of the event to send.
15639 *
15640 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15641 */
15642 public void sendAccessibilityEvent(View host, int eventType) {
15643 host.sendAccessibilityEventInternal(eventType);
15644 }
15645
15646 /**
15647 * Sends an accessibility event. This method behaves exactly as
15648 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15649 * empty {@link AccessibilityEvent} and does not perform a check whether
15650 * accessibility is enabled.
15651 * <p>
15652 * The default implementation behaves as
15653 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15654 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15655 * the case of no accessibility delegate been set.
15656 * </p>
15657 *
15658 * @param host The View hosting the delegate.
15659 * @param event The event to send.
15660 *
15661 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15662 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15663 */
15664 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15665 host.sendAccessibilityEventUncheckedInternal(event);
15666 }
15667
15668 /**
15669 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15670 * to its children for adding their text content to the event.
15671 * <p>
15672 * The default implementation behaves as
15673 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15674 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15675 * the case of no accessibility delegate been set.
15676 * </p>
15677 *
15678 * @param host The View hosting the delegate.
15679 * @param event The event.
15680 * @return True if the event population was completed.
15681 *
15682 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15683 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15684 */
15685 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15686 return host.dispatchPopulateAccessibilityEventInternal(event);
15687 }
15688
15689 /**
15690 * Gives a chance to the host View to populate the accessibility event with its
15691 * text content.
15692 * <p>
15693 * The default implementation behaves as
15694 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15695 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15696 * the case of no accessibility delegate been set.
15697 * </p>
15698 *
15699 * @param host The View hosting the delegate.
15700 * @param event The accessibility event which to populate.
15701 *
15702 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15703 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15704 */
15705 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15706 host.onPopulateAccessibilityEventInternal(event);
15707 }
15708
15709 /**
15710 * Initializes an {@link AccessibilityEvent} with information about the
15711 * the host View which is the event source.
15712 * <p>
15713 * The default implementation behaves as
15714 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15715 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15716 * the case of no accessibility delegate been set.
15717 * </p>
15718 *
15719 * @param host The View hosting the delegate.
15720 * @param event The event to initialize.
15721 *
15722 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15723 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15724 */
15725 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15726 host.onInitializeAccessibilityEventInternal(event);
15727 }
15728
15729 /**
15730 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15731 * <p>
15732 * The default implementation behaves as
15733 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15734 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15735 * the case of no accessibility delegate been set.
15736 * </p>
15737 *
15738 * @param host The View hosting the delegate.
15739 * @param info The instance to initialize.
15740 *
15741 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15742 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15743 */
15744 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15745 host.onInitializeAccessibilityNodeInfoInternal(info);
15746 }
15747
15748 /**
15749 * Called when a child of the host View has requested sending an
15750 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15751 * to augment the event.
15752 * <p>
15753 * The default implementation behaves as
15754 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15755 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15756 * the case of no accessibility delegate been set.
15757 * </p>
15758 *
15759 * @param host The View hosting the delegate.
15760 * @param child The child which requests sending the event.
15761 * @param event The event to be sent.
15762 * @return True if the event should be sent
15763 *
15764 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15765 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15766 */
15767 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15768 AccessibilityEvent event) {
15769 return host.onRequestSendAccessibilityEventInternal(child, event);
15770 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015771
15772 /**
15773 * Gets the provider for managing a virtual view hierarchy rooted at this View
15774 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15775 * that explore the window content.
15776 * <p>
15777 * The default implementation behaves as
15778 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15779 * the case of no accessibility delegate been set.
15780 * </p>
15781 *
15782 * @return The provider.
15783 *
15784 * @see AccessibilityNodeProvider
15785 */
15786 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15787 return null;
15788 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015790}