blob: 81f3f6a516f64325d28d4ec714556f3368ca48e0 [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 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700701 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int ENABLED = 0x00000000;
706
707 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700708 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 * 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
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700768 /**
769 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
770 * that they are optional and should be skipped if the window has
771 * requested system UI flags that ignore those insets for layout.
772 */
773 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774
775 /**
776 * <p>This view doesn't show fading edges.</p>
777 * {@hide}
778 */
779 static final int FADING_EDGE_NONE = 0x00000000;
780
781 /**
782 * <p>This view shows horizontal fading edges.</p>
783 * {@hide}
784 */
785 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
786
787 /**
788 * <p>This view shows vertical fading edges.</p>
789 * {@hide}
790 */
791 static final int FADING_EDGE_VERTICAL = 0x00002000;
792
793 /**
794 * <p>Mask for use with setFlags indicating bits used for indicating which
795 * fading edges are enabled.</p>
796 * {@hide}
797 */
798 static final int FADING_EDGE_MASK = 0x00003000;
799
800 /**
801 * <p>Indicates this view can be clicked. When clickable, a View reacts
802 * to clicks by notifying the OnClickListener.<p>
803 * {@hide}
804 */
805 static final int CLICKABLE = 0x00004000;
806
807 /**
808 * <p>Indicates this view is caching its drawing into a bitmap.</p>
809 * {@hide}
810 */
811 static final int DRAWING_CACHE_ENABLED = 0x00008000;
812
813 /**
814 * <p>Indicates that no icicle should be saved for this view.<p>
815 * {@hide}
816 */
817 static final int SAVE_DISABLED = 0x000010000;
818
819 /**
820 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
821 * property.</p>
822 * {@hide}
823 */
824 static final int SAVE_DISABLED_MASK = 0x000010000;
825
826 /**
827 * <p>Indicates that no drawing cache should ever be created for this view.<p>
828 * {@hide}
829 */
830 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
831
832 /**
833 * <p>Indicates this view can take / keep focus when int touch mode.</p>
834 * {@hide}
835 */
836 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
837
838 /**
839 * <p>Enables low quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
842
843 /**
844 * <p>Enables high quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
847
848 /**
849 * <p>Enables automatic quality mode for the drawing cache.</p>
850 */
851 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
852
853 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
854 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
855 };
856
857 /**
858 * <p>Mask for use with setFlags indicating bits used for the cache
859 * quality property.</p>
860 * {@hide}
861 */
862 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
863
864 /**
865 * <p>
866 * Indicates this view can be long clicked. When long clickable, a View
867 * reacts to long clicks by notifying the OnLongClickListener or showing a
868 * context menu.
869 * </p>
870 * {@hide}
871 */
872 static final int LONG_CLICKABLE = 0x00200000;
873
874 /**
875 * <p>Indicates that this view gets its drawable states from its direct parent
876 * and ignores its original internal states.</p>
877 *
878 * @hide
879 */
880 static final int DUPLICATE_PARENT_STATE = 0x00400000;
881
882 /**
883 * The scrollbar style to display the scrollbars inside the content area,
884 * without increasing the padding. The scrollbars will be overlaid with
885 * translucency on the view's content.
886 */
887 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
888
889 /**
890 * The scrollbar style to display the scrollbars inside the padded area,
891 * increasing the padding of the view. The scrollbars will not overlap the
892 * content area of the view.
893 */
894 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
895
896 /**
897 * The scrollbar style to display the scrollbars at the edge of the view,
898 * without increasing the padding. The scrollbars will be overlaid with
899 * translucency.
900 */
901 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
902
903 /**
904 * The scrollbar style to display the scrollbars at the edge of the view,
905 * increasing the padding of the view. The scrollbars will only overlap the
906 * background, if any.
907 */
908 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
909
910 /**
911 * Mask to check if the scrollbar style is overlay or inset.
912 * {@hide}
913 */
914 static final int SCROLLBARS_INSET_MASK = 0x01000000;
915
916 /**
917 * Mask to check if the scrollbar style is inside or outside.
918 * {@hide}
919 */
920 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
921
922 /**
923 * Mask for scrollbar style.
924 * {@hide}
925 */
926 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
927
928 /**
929 * View flag indicating that the screen should remain on while the
930 * window containing this view is visible to the user. This effectively
931 * takes care of automatically setting the WindowManager's
932 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
933 */
934 public static final int KEEP_SCREEN_ON = 0x04000000;
935
936 /**
937 * View flag indicating whether this view should have sound effects enabled
938 * for events such as clicking and touching.
939 */
940 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
941
942 /**
943 * View flag indicating whether this view should have haptic feedback
944 * enabled for events such as long presses.
945 */
946 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
947
948 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700949 * <p>Indicates that the view hierarchy should stop saving state when
950 * it reaches this view. If state saving is initiated immediately at
951 * the view, it will be allowed.
952 * {@hide}
953 */
954 static final int PARENT_SAVE_DISABLED = 0x20000000;
955
956 /**
957 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
958 * {@hide}
959 */
960 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
961
962 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700963 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
964 * should add all focusable Views regardless if they are focusable in touch mode.
965 */
966 public static final int FOCUSABLES_ALL = 0x00000000;
967
968 /**
969 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
970 * should add only Views focusable in touch mode.
971 */
972 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
973
974 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700975 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 * item.
977 */
978 public static final int FOCUS_BACKWARD = 0x00000001;
979
980 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700981 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 * item.
983 */
984 public static final int FOCUS_FORWARD = 0x00000002;
985
986 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700987 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 */
989 public static final int FOCUS_LEFT = 0x00000011;
990
991 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700992 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 */
994 public static final int FOCUS_UP = 0x00000021;
995
996 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700997 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 */
999 public static final int FOCUS_RIGHT = 0x00000042;
1000
1001 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001002 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 */
1004 public static final int FOCUS_DOWN = 0x00000082;
1005
1006 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001007 * Bits of {@link #getMeasuredWidthAndState()} and
1008 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1009 */
1010 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1011
1012 /**
1013 * Bits of {@link #getMeasuredWidthAndState()} and
1014 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1015 */
1016 public static final int MEASURED_STATE_MASK = 0xff000000;
1017
1018 /**
1019 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1020 * for functions that combine both width and height into a single int,
1021 * such as {@link #getMeasuredState()} and the childState argument of
1022 * {@link #resolveSizeAndState(int, int, int)}.
1023 */
1024 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1025
1026 /**
1027 * Bit of {@link #getMeasuredWidthAndState()} and
1028 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1029 * is smaller that the space the view would like to have.
1030 */
1031 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1032
1033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 * Base View state sets
1035 */
1036 // Singles
1037 /**
1038 * Indicates the view has no states set. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is enabled. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is focused. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is selected. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001072 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 /**
1074 * Indicates the view is pressed. States are used with
1075 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1076 * view depending on its state.
1077 *
1078 * @see android.graphics.drawable.Drawable
1079 * @see #getDrawableState()
1080 * @hide
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 /**
1084 * Indicates the view's window has focus. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 // Doubles
1093 /**
1094 * Indicates the view is enabled and has the focus.
1095 *
1096 * @see #ENABLED_STATE_SET
1097 * @see #FOCUSED_STATE_SET
1098 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001099 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /**
1101 * Indicates the view is enabled and selected.
1102 *
1103 * @see #ENABLED_STATE_SET
1104 * @see #SELECTED_STATE_SET
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is enabled and that its window has focus.
1109 *
1110 * @see #ENABLED_STATE_SET
1111 * @see #WINDOW_FOCUSED_STATE_SET
1112 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001113 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
1115 * Indicates the view is focused and selected.
1116 *
1117 * @see #FOCUSED_STATE_SET
1118 * @see #SELECTED_STATE_SET
1119 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001120 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /**
1122 * Indicates the view has the focus and that its window has the focus.
1123 *
1124 * @see #FOCUSED_STATE_SET
1125 * @see #WINDOW_FOCUSED_STATE_SET
1126 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001127 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 /**
1129 * Indicates the view is selected and that its window has the focus.
1130 *
1131 * @see #SELECTED_STATE_SET
1132 * @see #WINDOW_FOCUSED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 // Triples
1136 /**
1137 * Indicates the view is enabled, focused and selected.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #FOCUSED_STATE_SET
1141 * @see #SELECTED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled, focused and its window has the focus.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #FOCUSED_STATE_SET
1149 * @see #WINDOW_FOCUSED_STATE_SET
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 /**
1153 * Indicates the view is enabled, selected and its window has the focus.
1154 *
1155 * @see #ENABLED_STATE_SET
1156 * @see #SELECTED_STATE_SET
1157 * @see #WINDOW_FOCUSED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view is focused, selected and its window has the focus.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 * @see #WINDOW_FOCUSED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is enabled, focused, selected and its window
1170 * has the focus.
1171 *
1172 * @see #ENABLED_STATE_SET
1173 * @see #FOCUSED_STATE_SET
1174 * @see #SELECTED_STATE_SET
1175 * @see #WINDOW_FOCUSED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 /**
1179 * Indicates the view is pressed and its window has the focus.
1180 *
1181 * @see #PRESSED_STATE_SET
1182 * @see #WINDOW_FOCUSED_STATE_SET
1183 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001184 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 /**
1186 * Indicates the view is pressed and selected.
1187 *
1188 * @see #PRESSED_STATE_SET
1189 * @see #SELECTED_STATE_SET
1190 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001191 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 /**
1193 * Indicates the view is pressed, selected and its window has the focus.
1194 *
1195 * @see #PRESSED_STATE_SET
1196 * @see #SELECTED_STATE_SET
1197 * @see #WINDOW_FOCUSED_STATE_SET
1198 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001199 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 /**
1201 * Indicates the view is pressed and focused.
1202 *
1203 * @see #PRESSED_STATE_SET
1204 * @see #FOCUSED_STATE_SET
1205 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001206 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
1208 * Indicates the view is pressed, focused and its window has the focus.
1209 *
1210 * @see #PRESSED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 * @see #WINDOW_FOCUSED_STATE_SET
1213 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001214 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /**
1216 * Indicates the view is pressed, focused and selected.
1217 *
1218 * @see #PRESSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed, focused, selected and its window has the focus.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #FOCUSED_STATE_SET
1228 * @see #SELECTED_STATE_SET
1229 * @see #WINDOW_FOCUSED_STATE_SET
1230 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001231 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 /**
1233 * Indicates the view is pressed and enabled.
1234 *
1235 * @see #PRESSED_STATE_SET
1236 * @see #ENABLED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed, enabled and its window has the focus.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #ENABLED_STATE_SET
1244 * @see #WINDOW_FOCUSED_STATE_SET
1245 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001246 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
1248 * Indicates the view is pressed, enabled and selected.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed, enabled, selected and its window has the
1257 * focus.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #SELECTED_STATE_SET
1262 * @see #WINDOW_FOCUSED_STATE_SET
1263 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001264 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 /**
1266 * Indicates the view is pressed, enabled and focused.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001272 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 /**
1274 * Indicates the view is pressed, enabled, focused and its window has the
1275 * focus.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #FOCUSED_STATE_SET
1280 * @see #WINDOW_FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused and selected.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 * @see #FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, enabled, focused, selected and its window
1294 * has the focus.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 * @see #FOCUSED_STATE_SET
1300 * @see #WINDOW_FOCUSED_STATE_SET
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303
1304 /**
1305 * The order here is very important to {@link #getDrawableState()}
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 private static final int[][] VIEW_STATE_SETS;
1308
Romain Guyb051e892010-09-28 19:09:36 -07001309 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1310 static final int VIEW_STATE_SELECTED = 1 << 1;
1311 static final int VIEW_STATE_FOCUSED = 1 << 2;
1312 static final int VIEW_STATE_ENABLED = 1 << 3;
1313 static final int VIEW_STATE_PRESSED = 1 << 4;
1314 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001315 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001316 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001317 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1318 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001319
1320 static final int[] VIEW_STATE_IDS = new int[] {
1321 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1322 R.attr.state_selected, VIEW_STATE_SELECTED,
1323 R.attr.state_focused, VIEW_STATE_FOCUSED,
1324 R.attr.state_enabled, VIEW_STATE_ENABLED,
1325 R.attr.state_pressed, VIEW_STATE_PRESSED,
1326 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001327 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001328 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001329 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1330 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 };
1332
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001334 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1335 throw new IllegalStateException(
1336 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1337 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001339 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001341 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001343 orderedIds[i * 2] = viewState;
1344 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 }
1346 }
1347 }
Romain Guyb051e892010-09-28 19:09:36 -07001348 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1349 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1350 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 int numBits = Integer.bitCount(i);
1352 int[] set = new int[numBits];
1353 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001354 for (int j = 0; j < orderedIds.length; j += 2) {
1355 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356 set[pos++] = orderedIds[j];
1357 }
1358 }
1359 VIEW_STATE_SETS[i] = set;
1360 }
1361
1362 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1363 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1364 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1365 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1367 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1368 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1370 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1372 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1373 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1374 | VIEW_STATE_FOCUSED];
1375 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1376 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1378 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1379 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1380 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1381 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1382 | VIEW_STATE_ENABLED];
1383 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1385 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1387 | VIEW_STATE_ENABLED];
1388 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1390 | VIEW_STATE_ENABLED];
1391 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1393 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1394
1395 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1396 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1398 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1400 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1402 | VIEW_STATE_PRESSED];
1403 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1405 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1407 | VIEW_STATE_PRESSED];
1408 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1410 | VIEW_STATE_PRESSED];
1411 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1413 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1414 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1418 | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1421 | VIEW_STATE_PRESSED];
1422 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1424 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1425 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1427 | VIEW_STATE_PRESSED];
1428 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1431 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1434 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1436 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1437 | VIEW_STATE_PRESSED];
1438 }
1439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001441 * Accessibility event types that are dispatched for text population.
1442 */
1443 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1444 AccessibilityEvent.TYPE_VIEW_CLICKED
1445 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1446 | AccessibilityEvent.TYPE_VIEW_SELECTED
1447 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1448 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1449 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001450 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001451 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1452 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001453
1454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 * Temporary Rect currently for use in setBackground(). This will probably
1456 * be extended in the future to hold our own class with more than just
1457 * a Rect. :)
1458 */
1459 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001460
1461 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001462 * Temporary flag, used to enable processing of View properties in the native DisplayList
1463 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1464 * apps.
1465 * @hide
1466 */
Chet Haase76240da2012-03-29 09:42:34 -07001467 public static final boolean USE_DISPLAY_LIST_PROPERTIES = false;
Chet Haasea1cff502012-02-21 13:43:44 -08001468
1469 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001470 * Map used to store views' tags.
1471 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001472 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001475 * The next available accessiiblity id.
1476 */
1477 private static int sNextAccessibilityViewId;
1478
1479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * The animation currently associated with this view.
1481 * @hide
1482 */
1483 protected Animation mCurrentAnimation = null;
1484
1485 /**
1486 * Width as measured during measure pass.
1487 * {@hide}
1488 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001489 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001490 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491
1492 /**
1493 * Height as measured during measure pass.
1494 * {@hide}
1495 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001496 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001497 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498
1499 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001500 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1501 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1502 * its display list. This flag, used only when hw accelerated, allows us to clear the
1503 * flag while retaining this information until it's needed (at getDisplayList() time and
1504 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1505 *
1506 * {@hide}
1507 */
1508 boolean mRecreateDisplayList = false;
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * The view's identifier.
1512 * {@hide}
1513 *
1514 * @see #setId(int)
1515 * @see #getId()
1516 */
1517 @ViewDebug.ExportedProperty(resolveId = true)
1518 int mID = NO_ID;
1519
1520 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001521 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001522 */
1523 int mAccessibilityViewId = NO_ID;
1524
1525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 * The view's tag.
1527 * {@hide}
1528 *
1529 * @see #setTag(Object)
1530 * @see #getTag()
1531 */
1532 protected Object mTag;
1533
1534 // for mPrivateFlags:
1535 /** {@hide} */
1536 static final int WANTS_FOCUS = 0x00000001;
1537 /** {@hide} */
1538 static final int FOCUSED = 0x00000002;
1539 /** {@hide} */
1540 static final int SELECTED = 0x00000004;
1541 /** {@hide} */
1542 static final int IS_ROOT_NAMESPACE = 0x00000008;
1543 /** {@hide} */
1544 static final int HAS_BOUNDS = 0x00000010;
1545 /** {@hide} */
1546 static final int DRAWN = 0x00000020;
1547 /**
1548 * When this flag is set, this view is running an animation on behalf of its
1549 * children and should therefore not cancel invalidate requests, even if they
1550 * lie outside of this view's bounds.
1551 *
1552 * {@hide}
1553 */
1554 static final int DRAW_ANIMATION = 0x00000040;
1555 /** {@hide} */
1556 static final int SKIP_DRAW = 0x00000080;
1557 /** {@hide} */
1558 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1559 /** {@hide} */
1560 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1561 /** {@hide} */
1562 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1563 /** {@hide} */
1564 static final int MEASURED_DIMENSION_SET = 0x00000800;
1565 /** {@hide} */
1566 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001567 /** {@hide} */
1568 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 private static final int PRESSED = 0x00004000;
1571
1572 /** {@hide} */
1573 static final int DRAWING_CACHE_VALID = 0x00008000;
1574 /**
1575 * Flag used to indicate that this view should be drawn once more (and only once
1576 * more) after its animation has completed.
1577 * {@hide}
1578 */
1579 static final int ANIMATION_STARTED = 0x00010000;
1580
1581 private static final int SAVE_STATE_CALLED = 0x00020000;
1582
1583 /**
1584 * Indicates that the View returned true when onSetAlpha() was called and that
1585 * the alpha must be restored.
1586 * {@hide}
1587 */
1588 static final int ALPHA_SET = 0x00040000;
1589
1590 /**
1591 * Set by {@link #setScrollContainer(boolean)}.
1592 */
1593 static final int SCROLL_CONTAINER = 0x00080000;
1594
1595 /**
1596 * Set by {@link #setScrollContainer(boolean)}.
1597 */
1598 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1599
1600 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001601 * View flag indicating whether this view was invalidated (fully or partially.)
1602 *
1603 * @hide
1604 */
1605 static final int DIRTY = 0x00200000;
1606
1607 /**
1608 * View flag indicating whether this view was invalidated by an opaque
1609 * invalidate request.
1610 *
1611 * @hide
1612 */
1613 static final int DIRTY_OPAQUE = 0x00400000;
1614
1615 /**
1616 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1617 *
1618 * @hide
1619 */
1620 static final int DIRTY_MASK = 0x00600000;
1621
1622 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001623 * Indicates whether the background is opaque.
1624 *
1625 * @hide
1626 */
1627 static final int OPAQUE_BACKGROUND = 0x00800000;
1628
1629 /**
1630 * Indicates whether the scrollbars are opaque.
1631 *
1632 * @hide
1633 */
1634 static final int OPAQUE_SCROLLBARS = 0x01000000;
1635
1636 /**
1637 * Indicates whether the view is opaque.
1638 *
1639 * @hide
1640 */
1641 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001642
Adam Powelle14579b2009-12-16 18:39:52 -08001643 /**
1644 * Indicates a prepressed state;
1645 * the short time between ACTION_DOWN and recognizing
1646 * a 'real' press. Prepressed is used to recognize quick taps
1647 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001648 *
Adam Powelle14579b2009-12-16 18:39:52 -08001649 * @hide
1650 */
1651 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001652
Adam Powellc9fbaab2010-02-16 17:16:19 -08001653 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001654 * Indicates whether the view is temporarily detached.
1655 *
1656 * @hide
1657 */
1658 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001659
Adam Powell8568c3a2010-04-19 14:26:11 -07001660 /**
1661 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001662 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001663 * @hide
1664 */
1665 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001666
1667 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001668 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1669 * @hide
1670 */
1671 private static final int HOVERED = 0x10000000;
1672
1673 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001674 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1675 * for transform operations
1676 *
1677 * @hide
1678 */
Adam Powellf37df072010-09-17 16:22:49 -07001679 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001680
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001681 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001682 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001683
Chet Haasefd2b0022010-08-06 13:08:56 -07001684 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001685 * Indicates that this view was specifically invalidated, not just dirtied because some
1686 * child view was invalidated. The flag is used to determine when we need to recreate
1687 * a view's display list (as opposed to just returning a reference to its existing
1688 * display list).
1689 *
1690 * @hide
1691 */
1692 static final int INVALIDATED = 0x80000000;
1693
Christopher Tate3d4bf172011-03-28 16:16:46 -07001694 /* Masks for mPrivateFlags2 */
1695
1696 /**
1697 * Indicates that this view has reported that it can accept the current drag's content.
1698 * Cleared when the drag operation concludes.
1699 * @hide
1700 */
1701 static final int DRAG_CAN_ACCEPT = 0x00000001;
1702
1703 /**
1704 * Indicates that this view is currently directly under the drag location in a
1705 * drag-and-drop operation involving content that it can accept. Cleared when
1706 * the drag exits the view, or when the drag operation concludes.
1707 * @hide
1708 */
1709 static final int DRAG_HOVERED = 0x00000002;
1710
Cibu Johny86666632010-02-22 13:01:02 -08001711 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001712 * Horizontal layout direction of this view is from Left to Right.
1713 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001714 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001715 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001716
1717 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001718 * Horizontal layout direction of this view is from Right to Left.
1719 * Use with {@link #setLayoutDirection}.
1720 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001721 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001722
1723 /**
1724 * Horizontal layout direction of this view is inherited from its parent.
1725 * Use with {@link #setLayoutDirection}.
1726 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001727 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001728
1729 /**
1730 * Horizontal layout direction of this view is from deduced from the default language
1731 * script for the locale. Use with {@link #setLayoutDirection}.
1732 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001733 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001734
1735 /**
1736 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001737 * @hide
1738 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001739 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1740
1741 /**
1742 * Mask for use with private flags indicating bits used for horizontal layout direction.
1743 * @hide
1744 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001745 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001746
1747 /**
1748 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1749 * right-to-left direction.
1750 * @hide
1751 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001752 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001753
1754 /**
1755 * Indicates whether the view horizontal layout direction has been resolved.
1756 * @hide
1757 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001758 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001759
1760 /**
1761 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1762 * @hide
1763 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001764 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001765
1766 /*
1767 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1768 * flag value.
1769 * @hide
1770 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001771 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1772 LAYOUT_DIRECTION_LTR,
1773 LAYOUT_DIRECTION_RTL,
1774 LAYOUT_DIRECTION_INHERIT,
1775 LAYOUT_DIRECTION_LOCALE
1776 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001777
1778 /**
1779 * Default horizontal layout direction.
1780 * @hide
1781 */
1782 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001783
Cibu Johny86666632010-02-22 13:01:02 -08001784
Adam Powell539ee872012-02-03 19:00:49 -08001785 /**
1786 * Indicates that the view is tracking some sort of transient state
1787 * that the app should not need to be aware of, but that the framework
1788 * should take special care to preserve.
1789 *
1790 * @hide
1791 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001792 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001793
1794
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001795 /**
1796 * Text direction is inherited thru {@link ViewGroup}
1797 */
1798 public static final int TEXT_DIRECTION_INHERIT = 0;
1799
1800 /**
1801 * Text direction is using "first strong algorithm". The first strong directional character
1802 * determines the paragraph direction. If there is no strong directional character, the
1803 * paragraph direction is the view's resolved layout direction.
1804 */
1805 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1806
1807 /**
1808 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1809 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1810 * If there are neither, the paragraph direction is the view's resolved layout direction.
1811 */
1812 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1813
1814 /**
1815 * Text direction is forced to LTR.
1816 */
1817 public static final int TEXT_DIRECTION_LTR = 3;
1818
1819 /**
1820 * Text direction is forced to RTL.
1821 */
1822 public static final int TEXT_DIRECTION_RTL = 4;
1823
1824 /**
1825 * Text direction is coming from the system Locale.
1826 */
1827 public static final int TEXT_DIRECTION_LOCALE = 5;
1828
1829 /**
1830 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1831 * @hide
1832 */
1833 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1834
1835 /**
1836 * Default text direction is inherited
1837 */
1838 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1839
1840 /**
1841 * Mask for use with private flags indicating bits used for text direction.
1842 * @hide
1843 */
1844 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1845
1846 /**
1847 * Array of text direction flags for mapping attribute "textDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
1851 private static final int[] TEXT_DIRECTION_FLAGS = {
1852 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1853 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1854 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1855 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1856 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1857 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1858 };
1859
1860 /**
1861 * Indicates whether the view text direction has been resolved.
1862 * @hide
1863 */
1864 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1865
1866 /**
1867 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1868 * @hide
1869 */
1870 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1871
1872 /**
1873 * Mask for use with private flags indicating bits used for resolved text direction.
1874 * @hide
1875 */
1876 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1877
1878 /**
1879 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1880 * @hide
1881 */
1882 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1883 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1884
1885
Christopher Tate3d4bf172011-03-28 16:16:46 -07001886 /* End of masks for mPrivateFlags2 */
1887
1888 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1889
Chet Haasedaf98e92011-01-10 14:10:36 -08001890 /**
Adam Powell637d3372010-08-25 14:37:03 -07001891 * Always allow a user to over-scroll this view, provided it is a
1892 * view that can scroll.
1893 *
1894 * @see #getOverScrollMode()
1895 * @see #setOverScrollMode(int)
1896 */
1897 public static final int OVER_SCROLL_ALWAYS = 0;
1898
1899 /**
1900 * Allow a user to over-scroll this view only if the content is large
1901 * enough to meaningfully scroll, provided it is a view that can scroll.
1902 *
1903 * @see #getOverScrollMode()
1904 * @see #setOverScrollMode(int)
1905 */
1906 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1907
1908 /**
1909 * Never allow a user to over-scroll this view.
1910 *
1911 * @see #getOverScrollMode()
1912 * @see #setOverScrollMode(int)
1913 */
1914 public static final int OVER_SCROLL_NEVER = 2;
1915
1916 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001917 * Special constant for {@link #setSystemUiVisibility(int)}: View has
1918 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001919 *
Joe Malin32736f02011-01-19 16:14:20 -08001920 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001921 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001922 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001923
1924 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001925 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
1926 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001927 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001928 * <p>This is for use in games, book readers, video players, or any other
1929 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001930 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001931 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001932 *
Joe Malin32736f02011-01-19 16:14:20 -08001933 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001934 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001935 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1936
1937 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001938 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
1939 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001940 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001941 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04001942 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1943 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1944 * those to disappear. This is useful (in conjunction with the
1945 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1946 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1947 * window flags) for displaying content using every last pixel on the display.
1948 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001949 * <p>There is a limitation: because navigation controls are so important, the least user
1950 * interaction will cause them to reappear immediately. When this happens, both
1951 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
1952 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001953 *
1954 * @see #setSystemUiVisibility(int)
1955 */
1956 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1957
1958 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001959 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
1960 * into the normal fullscreen mode so that its content can take over the screen
1961 * while still allowing the user to interact with the application.
1962 *
1963 * <p>This has the same visual effect as
1964 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
1965 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
1966 * meaning that non-critical screen decorations (such as the status bar) will be
1967 * hidden while the user is in the View's window, focusing the experience on
1968 * that content. Unlike the window flag, if you are using ActionBar in
1969 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
1970 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
1971 * hide the action bar.
1972 *
1973 * <p>This approach to going fullscreen is best used over the window flag when
1974 * it is a transient state -- that is, the application does this at certain
1975 * points in its user interaction where it wants to allow the user to focus
1976 * on content, but not as a continuous state. For situations where the application
1977 * would like to simply stay full screen the entire time (such as a game that
1978 * wants to take over the screen), the
1979 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
1980 * is usually a better approach. The state set here will be removed by the system
1981 * in various situations (such as the user moving to another application) like
1982 * the other system UI states.
1983 *
1984 * <p>When using this flag, the application should provide some easy facility
1985 * for the user to go out of it. A common example would be in an e-book
1986 * reader, where tapping on the screen brings back whatever screen and UI
1987 * decorations that had been hidden while the user was immersed in reading
1988 * the book.
1989 *
1990 * @see #setSystemUiVisibility(int)
1991 */
1992 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
1993
1994 /**
1995 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
1996 * flags, we would like a stable view of the content insets given to
1997 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
1998 * will always represent the worst case that the application can expect
1999 * as a continue state. In practice this means with any of system bar,
2000 * nav bar, and status bar shown, but not the space that would be needed
2001 * for an input method.
2002 *
2003 * <p>If you are using ActionBar in
2004 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2005 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2006 * insets it adds to those given to the application.
2007 */
2008 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2009
2010 /**
2011 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2012 * to be layed out as if it has requested
2013 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2014 * allows it to avoid artifacts when switching in and out of that mode, at
2015 * the expense that some of its user interface may be covered by screen
2016 * decorations when they are shown. You can perform layout of your inner
2017 * UI elements to account for the navagation system UI through the
2018 * {@link #fitSystemWindows(Rect)} method.
2019 */
2020 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2021
2022 /**
2023 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2024 * to be layed out as if it has requested
2025 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2026 * allows it to avoid artifacts when switching in and out of that mode, at
2027 * the expense that some of its user interface may be covered by screen
2028 * decorations when they are shown. You can perform layout of your inner
2029 * UI elements to account for non-fullscreen system UI through the
2030 * {@link #fitSystemWindows(Rect)} method.
2031 */
2032 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2033
2034 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002035 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2036 */
2037 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2038
2039 /**
2040 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2041 */
2042 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002043
2044 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002045 * @hide
2046 *
2047 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2048 * out of the public fields to keep the undefined bits out of the developer's way.
2049 *
2050 * Flag to make the status bar not expandable. Unless you also
2051 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2052 */
2053 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2054
2055 /**
2056 * @hide
2057 *
2058 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2059 * out of the public fields to keep the undefined bits out of the developer's way.
2060 *
2061 * Flag to hide notification icons and scrolling ticker text.
2062 */
2063 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2064
2065 /**
2066 * @hide
2067 *
2068 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2069 * out of the public fields to keep the undefined bits out of the developer's way.
2070 *
2071 * Flag to disable incoming notification alerts. This will not block
2072 * icons, but it will block sound, vibrating and other visual or aural notifications.
2073 */
2074 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2075
2076 /**
2077 * @hide
2078 *
2079 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2080 * out of the public fields to keep the undefined bits out of the developer's way.
2081 *
2082 * Flag to hide only the scrolling ticker. Note that
2083 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2084 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2085 */
2086 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2087
2088 /**
2089 * @hide
2090 *
2091 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2092 * out of the public fields to keep the undefined bits out of the developer's way.
2093 *
2094 * Flag to hide the center system info area.
2095 */
2096 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2097
2098 /**
2099 * @hide
2100 *
2101 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2102 * out of the public fields to keep the undefined bits out of the developer's way.
2103 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002104 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002105 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2106 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002107 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002108
2109 /**
2110 * @hide
2111 *
2112 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2113 * out of the public fields to keep the undefined bits out of the developer's way.
2114 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002115 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002116 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2117 */
2118 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2119
2120 /**
2121 * @hide
2122 *
2123 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2124 * out of the public fields to keep the undefined bits out of the developer's way.
2125 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002126 * Flag to hide only the clock. You might use this if your activity has
2127 * its own clock making the status bar's clock redundant.
2128 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002129 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2130
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002131 /**
2132 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002133 *
2134 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2135 * out of the public fields to keep the undefined bits out of the developer's way.
2136 *
2137 * Flag to hide only the recent apps button. Don't use this
2138 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2139 */
2140 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2141
2142 /**
2143 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002144 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002145 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002146
2147 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002148 * These are the system UI flags that can be cleared by events outside
2149 * of an application. Currently this is just the ability to tap on the
2150 * screen while hiding the navigation bar to have it return.
2151 * @hide
2152 */
2153 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002154 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2155 | SYSTEM_UI_FLAG_FULLSCREEN;
2156
2157 /**
2158 * Flags that can impact the layout in relation to system UI.
2159 */
2160 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2161 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2162 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002163
2164 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002165 * Find views that render the specified text.
2166 *
2167 * @see #findViewsWithText(ArrayList, CharSequence, int)
2168 */
2169 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2170
2171 /**
2172 * Find find views that contain the specified content description.
2173 *
2174 * @see #findViewsWithText(ArrayList, CharSequence, int)
2175 */
2176 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2177
2178 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002179 * Find views that contain {@link AccessibilityNodeProvider}. Such
2180 * a View is a root of virtual view hierarchy and may contain the searched
2181 * text. If this flag is set Views with providers are automatically
2182 * added and it is a responsibility of the client to call the APIs of
2183 * the provider to determine whether the virtual tree rooted at this View
2184 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2185 * represeting the virtual views with this text.
2186 *
2187 * @see #findViewsWithText(ArrayList, CharSequence, int)
2188 *
2189 * @hide
2190 */
2191 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2192
2193 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002194 * Indicates that the screen has changed state and is now off.
2195 *
2196 * @see #onScreenStateChanged(int)
2197 */
2198 public static final int SCREEN_STATE_OFF = 0x0;
2199
2200 /**
2201 * Indicates that the screen has changed state and is now on.
2202 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002203 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002204 */
2205 public static final int SCREEN_STATE_ON = 0x1;
2206
2207 /**
Adam Powell637d3372010-08-25 14:37:03 -07002208 * Controls the over-scroll mode for this view.
2209 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2210 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2211 * and {@link #OVER_SCROLL_NEVER}.
2212 */
2213 private int mOverScrollMode;
2214
2215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 * The parent this view is attached to.
2217 * {@hide}
2218 *
2219 * @see #getParent()
2220 */
2221 protected ViewParent mParent;
2222
2223 /**
2224 * {@hide}
2225 */
2226 AttachInfo mAttachInfo;
2227
2228 /**
2229 * {@hide}
2230 */
Romain Guy809a7f62009-05-14 15:44:42 -07002231 @ViewDebug.ExportedProperty(flagMapping = {
2232 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2233 name = "FORCE_LAYOUT"),
2234 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2235 name = "LAYOUT_REQUIRED"),
2236 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002237 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002238 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2239 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2240 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2241 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2242 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002244 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245
2246 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002247 * This view's request for the visibility of the status bar.
2248 * @hide
2249 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002250 @ViewDebug.ExportedProperty(flagMapping = {
2251 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2252 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2253 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2254 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2255 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2256 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2257 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2258 equals = SYSTEM_UI_FLAG_VISIBLE,
2259 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2260 })
Joe Onorato664644d2011-01-23 17:53:23 -08002261 int mSystemUiVisibility;
2262
2263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 * Count of how many windows this view has been attached to.
2265 */
2266 int mWindowAttachCount;
2267
2268 /**
2269 * The layout parameters associated with this view and used by the parent
2270 * {@link android.view.ViewGroup} to determine how this view should be
2271 * laid out.
2272 * {@hide}
2273 */
2274 protected ViewGroup.LayoutParams mLayoutParams;
2275
2276 /**
2277 * The view flags hold various views states.
2278 * {@hide}
2279 */
2280 @ViewDebug.ExportedProperty
2281 int mViewFlags;
2282
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002283 static class TransformationInfo {
2284 /**
2285 * The transform matrix for the View. This transform is calculated internally
2286 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2287 * is used by default. Do *not* use this variable directly; instead call
2288 * getMatrix(), which will automatically recalculate the matrix if necessary
2289 * to get the correct matrix based on the latest rotation and scale properties.
2290 */
2291 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002292
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002293 /**
2294 * The transform matrix for the View. This transform is calculated internally
2295 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2296 * is used by default. Do *not* use this variable directly; instead call
2297 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2298 * to get the correct matrix based on the latest rotation and scale properties.
2299 */
2300 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002301
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002302 /**
2303 * An internal variable that tracks whether we need to recalculate the
2304 * transform matrix, based on whether the rotation or scaleX/Y properties
2305 * have changed since the matrix was last calculated.
2306 */
2307 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002308
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002309 /**
2310 * An internal variable that tracks whether we need to recalculate the
2311 * transform matrix, based on whether the rotation or scaleX/Y properties
2312 * have changed since the matrix was last calculated.
2313 */
2314 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002315
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002316 /**
2317 * A variable that tracks whether we need to recalculate the
2318 * transform matrix, based on whether the rotation or scaleX/Y properties
2319 * have changed since the matrix was last calculated. This variable
2320 * is only valid after a call to updateMatrix() or to a function that
2321 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2322 */
2323 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002324
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002325 /**
2326 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2327 */
2328 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002329
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002330 /**
2331 * This matrix is used when computing the matrix for 3D rotations.
2332 */
2333 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002334
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002335 /**
2336 * These prev values are used to recalculate a centered pivot point when necessary. The
2337 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2338 * set), so thes values are only used then as well.
2339 */
2340 private int mPrevWidth = -1;
2341 private int mPrevHeight = -1;
2342
2343 /**
2344 * The degrees rotation around the vertical axis through the pivot point.
2345 */
2346 @ViewDebug.ExportedProperty
2347 float mRotationY = 0f;
2348
2349 /**
2350 * The degrees rotation around the horizontal axis through the pivot point.
2351 */
2352 @ViewDebug.ExportedProperty
2353 float mRotationX = 0f;
2354
2355 /**
2356 * The degrees rotation around the pivot point.
2357 */
2358 @ViewDebug.ExportedProperty
2359 float mRotation = 0f;
2360
2361 /**
2362 * The amount of translation of the object away from its left property (post-layout).
2363 */
2364 @ViewDebug.ExportedProperty
2365 float mTranslationX = 0f;
2366
2367 /**
2368 * The amount of translation of the object away from its top property (post-layout).
2369 */
2370 @ViewDebug.ExportedProperty
2371 float mTranslationY = 0f;
2372
2373 /**
2374 * The amount of scale in the x direction around the pivot point. A
2375 * value of 1 means no scaling is applied.
2376 */
2377 @ViewDebug.ExportedProperty
2378 float mScaleX = 1f;
2379
2380 /**
2381 * The amount of scale in the y direction around the pivot point. A
2382 * value of 1 means no scaling is applied.
2383 */
2384 @ViewDebug.ExportedProperty
2385 float mScaleY = 1f;
2386
2387 /**
Chet Haasea33de552012-02-03 16:28:24 -08002388 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002389 */
2390 @ViewDebug.ExportedProperty
2391 float mPivotX = 0f;
2392
2393 /**
Chet Haasea33de552012-02-03 16:28:24 -08002394 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002395 */
2396 @ViewDebug.ExportedProperty
2397 float mPivotY = 0f;
2398
2399 /**
2400 * The opacity of the View. This is a value from 0 to 1, where 0 means
2401 * completely transparent and 1 means completely opaque.
2402 */
2403 @ViewDebug.ExportedProperty
2404 float mAlpha = 1f;
2405 }
2406
2407 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002408
Joe Malin32736f02011-01-19 16:14:20 -08002409 private boolean mLastIsOpaque;
2410
Chet Haasefd2b0022010-08-06 13:08:56 -07002411 /**
2412 * Convenience value to check for float values that are close enough to zero to be considered
2413 * zero.
2414 */
Romain Guy2542d192010-08-18 11:47:12 -07002415 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002416
2417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 * The distance in pixels from the left edge of this view's parent
2419 * to the left edge of this view.
2420 * {@hide}
2421 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002422 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 protected int mLeft;
2424 /**
2425 * The distance in pixels from the left edge of this view's parent
2426 * to the right edge of this view.
2427 * {@hide}
2428 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002429 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 protected int mRight;
2431 /**
2432 * The distance in pixels from the top edge of this view's parent
2433 * to the top edge of this view.
2434 * {@hide}
2435 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002436 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 protected int mTop;
2438 /**
2439 * The distance in pixels from the top edge of this view's parent
2440 * to the bottom edge of this view.
2441 * {@hide}
2442 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002443 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 protected int mBottom;
2445
2446 /**
2447 * The offset, in pixels, by which the content of this view is scrolled
2448 * horizontally.
2449 * {@hide}
2450 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002451 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 protected int mScrollX;
2453 /**
2454 * The offset, in pixels, by which the content of this view is scrolled
2455 * vertically.
2456 * {@hide}
2457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002458 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 protected int mScrollY;
2460
2461 /**
2462 * The left padding in pixels, that is the distance in pixels between the
2463 * left edge of this view and the left edge of its content.
2464 * {@hide}
2465 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002466 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 protected int mPaddingLeft;
2468 /**
2469 * The right padding in pixels, that is the distance in pixels between the
2470 * right edge of this view and the right edge of its content.
2471 * {@hide}
2472 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002473 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 protected int mPaddingRight;
2475 /**
2476 * The top padding in pixels, that is the distance in pixels between the
2477 * top edge of this view and the top edge of its content.
2478 * {@hide}
2479 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002480 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 protected int mPaddingTop;
2482 /**
2483 * The bottom padding in pixels, that is the distance in pixels between the
2484 * bottom edge of this view and the bottom edge of its content.
2485 * {@hide}
2486 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002487 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 protected int mPaddingBottom;
2489
2490 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002491 * Briefly describes the view and is primarily used for accessibility support.
2492 */
2493 private CharSequence mContentDescription;
2494
2495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002497 *
2498 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002500 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002501 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
2503 /**
2504 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002505 *
2506 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002508 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002509 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002511 /**
Adam Powell20232d02010-12-08 21:08:53 -08002512 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002513 *
2514 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002515 */
2516 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002517 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002518
2519 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002520 * Cache if the user padding is relative.
2521 *
2522 */
2523 @ViewDebug.ExportedProperty(category = "padding")
2524 boolean mUserPaddingRelative;
2525
2526 /**
2527 * Cache the paddingStart set by the user to append to the scrollbar's size.
2528 *
2529 */
2530 @ViewDebug.ExportedProperty(category = "padding")
2531 int mUserPaddingStart;
2532
2533 /**
2534 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2535 *
2536 */
2537 @ViewDebug.ExportedProperty(category = "padding")
2538 int mUserPaddingEnd;
2539
2540 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002541 * @hide
2542 */
2543 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2544 /**
2545 * @hide
2546 */
2547 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 private Drawable mBGDrawable;
2550
2551 private int mBackgroundResource;
2552 private boolean mBackgroundSizeChanged;
2553
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002554 static class ListenerInfo {
2555 /**
2556 * Listener used to dispatch focus change events.
2557 * This field should be made private, so it is hidden from the SDK.
2558 * {@hide}
2559 */
2560 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002562 /**
2563 * Listeners for layout change events.
2564 */
2565 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002566
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002567 /**
2568 * Listeners for attach events.
2569 */
2570 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002571
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002572 /**
2573 * Listener used to dispatch click events.
2574 * This field should be made private, so it is hidden from the SDK.
2575 * {@hide}
2576 */
2577 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002579 /**
2580 * Listener used to dispatch long click events.
2581 * This field should be made private, so it is hidden from the SDK.
2582 * {@hide}
2583 */
2584 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002586 /**
2587 * Listener used to build the context menu.
2588 * This field should be made private, so it is hidden from the SDK.
2589 * {@hide}
2590 */
2591 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002593 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002595 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002597 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002598
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002599 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002600
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002601 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002602
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002603 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2604 }
2605
2606 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 /**
2609 * The application environment this view lives in.
2610 * This field should be made private, so it is hidden from the SDK.
2611 * {@hide}
2612 */
2613 protected Context mContext;
2614
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002615 private final Resources mResources;
2616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 private ScrollabilityCache mScrollCache;
2618
2619 private int[] mDrawableState = null;
2620
Romain Guy0211a0a2011-02-14 16:34:59 -08002621 /**
2622 * Set to true when drawing cache is enabled and cannot be created.
2623 *
2624 * @hide
2625 */
2626 public boolean mCachingFailed;
2627
Romain Guy02890fd2010-08-06 17:58:44 -07002628 private Bitmap mDrawingCache;
2629 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002630 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002631 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632
2633 /**
2634 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2635 * the user may specify which view to go to next.
2636 */
2637 private int mNextFocusLeftId = View.NO_ID;
2638
2639 /**
2640 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2641 * the user may specify which view to go to next.
2642 */
2643 private int mNextFocusRightId = View.NO_ID;
2644
2645 /**
2646 * When this view has focus and the next focus is {@link #FOCUS_UP},
2647 * the user may specify which view to go to next.
2648 */
2649 private int mNextFocusUpId = View.NO_ID;
2650
2651 /**
2652 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2653 * the user may specify which view to go to next.
2654 */
2655 private int mNextFocusDownId = View.NO_ID;
2656
Jeff Brown4e6319b2010-12-13 10:36:51 -08002657 /**
2658 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2659 * the user may specify which view to go to next.
2660 */
2661 int mNextFocusForwardId = View.NO_ID;
2662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002664 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002665 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002666 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 private UnsetPressedState mUnsetPressedState;
2669
2670 /**
2671 * Whether the long press's action has been invoked. The tap's action is invoked on the
2672 * up event while a long press is invoked as soon as the long press duration is reached, so
2673 * a long press could be performed before the tap is checked, in which case the tap's action
2674 * should not be invoked.
2675 */
2676 private boolean mHasPerformedLongPress;
2677
2678 /**
2679 * The minimum height of the view. We'll try our best to have the height
2680 * of this view to at least this amount.
2681 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002682 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 private int mMinHeight;
2684
2685 /**
2686 * The minimum width of the view. We'll try our best to have the width
2687 * of this view to at least this amount.
2688 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002689 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 private int mMinWidth;
2691
2692 /**
2693 * The delegate to handle touch events that are physically in this view
2694 * but should be handled by another view.
2695 */
2696 private TouchDelegate mTouchDelegate = null;
2697
2698 /**
2699 * Solid color to use as a background when creating the drawing cache. Enables
2700 * the cache to use 16 bit bitmaps instead of 32 bit.
2701 */
2702 private int mDrawingCacheBackgroundColor = 0;
2703
2704 /**
2705 * Special tree observer used when mAttachInfo is null.
2706 */
2707 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002708
Adam Powelle14579b2009-12-16 18:39:52 -08002709 /**
2710 * Cache the touch slop from the context that created the view.
2711 */
2712 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002715 * Object that handles automatic animation of view properties.
2716 */
2717 private ViewPropertyAnimator mAnimator = null;
2718
2719 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002720 * Flag indicating that a drag can cross window boundaries. When
2721 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2722 * with this flag set, all visible applications will be able to participate
2723 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002724 *
2725 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002726 */
2727 public static final int DRAG_FLAG_GLOBAL = 1;
2728
2729 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002730 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2731 */
2732 private float mVerticalScrollFactor;
2733
2734 /**
Adam Powell20232d02010-12-08 21:08:53 -08002735 * Position of the vertical scroll bar.
2736 */
2737 private int mVerticalScrollbarPosition;
2738
2739 /**
2740 * Position the scroll bar at the default position as determined by the system.
2741 */
2742 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2743
2744 /**
2745 * Position the scroll bar along the left edge.
2746 */
2747 public static final int SCROLLBAR_POSITION_LEFT = 1;
2748
2749 /**
2750 * Position the scroll bar along the right edge.
2751 */
2752 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2753
2754 /**
Romain Guy171c5922011-01-06 10:04:23 -08002755 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002756 *
2757 * @see #getLayerType()
2758 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002759 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002760 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002761 */
2762 public static final int LAYER_TYPE_NONE = 0;
2763
2764 /**
2765 * <p>Indicates that the view has a software layer. A software layer is backed
2766 * by a bitmap and causes the view to be rendered using Android's software
2767 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002768 *
Romain Guy171c5922011-01-06 10:04:23 -08002769 * <p>Software layers have various usages:</p>
2770 * <p>When the application is not using hardware acceleration, a software layer
2771 * is useful to apply a specific color filter and/or blending mode and/or
2772 * translucency to a view and all its children.</p>
2773 * <p>When the application is using hardware acceleration, a software layer
2774 * is useful to render drawing primitives not supported by the hardware
2775 * accelerated pipeline. It can also be used to cache a complex view tree
2776 * into a texture and reduce the complexity of drawing operations. For instance,
2777 * when animating a complex view tree with a translation, a software layer can
2778 * be used to render the view tree only once.</p>
2779 * <p>Software layers should be avoided when the affected view tree updates
2780 * often. Every update will require to re-render the software layer, which can
2781 * potentially be slow (particularly when hardware acceleration is turned on
2782 * since the layer will have to be uploaded into a hardware texture after every
2783 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002784 *
2785 * @see #getLayerType()
2786 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002787 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002788 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002789 */
2790 public static final int LAYER_TYPE_SOFTWARE = 1;
2791
2792 /**
2793 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2794 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2795 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2796 * rendering pipeline, but only if hardware acceleration is turned on for the
2797 * view hierarchy. When hardware acceleration is turned off, hardware layers
2798 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002799 *
Romain Guy171c5922011-01-06 10:04:23 -08002800 * <p>A hardware layer is useful to apply a specific color filter and/or
2801 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002802 * <p>A hardware layer can be used to cache a complex view tree into a
2803 * texture and reduce the complexity of drawing operations. For instance,
2804 * when animating a complex view tree with a translation, a hardware layer can
2805 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002806 * <p>A hardware layer can also be used to increase the rendering quality when
2807 * rotation transformations are applied on a view. It can also be used to
2808 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002809 *
2810 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002811 * @see #setLayerType(int, android.graphics.Paint)
2812 * @see #LAYER_TYPE_NONE
2813 * @see #LAYER_TYPE_SOFTWARE
2814 */
2815 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002816
Romain Guy3aaff3a2011-01-12 14:18:47 -08002817 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2818 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2819 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2820 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2821 })
Romain Guy171c5922011-01-06 10:04:23 -08002822 int mLayerType = LAYER_TYPE_NONE;
2823 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002824 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002825
2826 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002827 * Set to true when the view is sending hover accessibility events because it
2828 * is the innermost hovered view.
2829 */
2830 private boolean mSendingHoverAccessibilityEvents;
2831
2832 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002833 * Delegate for injecting accessiblity functionality.
2834 */
2835 AccessibilityDelegate mAccessibilityDelegate;
2836
2837 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002838 * Consistency verifier for debugging purposes.
2839 * @hide
2840 */
2841 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2842 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2843 new InputEventConsistencyVerifier(this, 0) : null;
2844
2845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 * Simple constructor to use when creating a view from code.
2847 *
2848 * @param context The Context the view is running in, through which it can
2849 * access the current theme, resources, etc.
2850 */
2851 public View(Context context) {
2852 mContext = context;
2853 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002854 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002855 // Set layout and text direction defaults
2856 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
2857 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08002858 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002859 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002860 mUserPaddingStart = -1;
2861 mUserPaddingEnd = -1;
2862 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 }
2864
2865 /**
2866 * Constructor that is called when inflating a view from XML. This is called
2867 * when a view is being constructed from an XML file, supplying attributes
2868 * that were specified in the XML file. This version uses a default style of
2869 * 0, so the only attribute values applied are those in the Context's Theme
2870 * and the given AttributeSet.
2871 *
2872 * <p>
2873 * The method onFinishInflate() will be called after all children have been
2874 * added.
2875 *
2876 * @param context The Context the view is running in, through which it can
2877 * access the current theme, resources, etc.
2878 * @param attrs The attributes of the XML tag that is inflating the view.
2879 * @see #View(Context, AttributeSet, int)
2880 */
2881 public View(Context context, AttributeSet attrs) {
2882 this(context, attrs, 0);
2883 }
2884
2885 /**
2886 * Perform inflation from XML and apply a class-specific base style. This
2887 * constructor of View allows subclasses to use their own base style when
2888 * they are inflating. For example, a Button class's constructor would call
2889 * this version of the super class constructor and supply
2890 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2891 * the theme's button style to modify all of the base view attributes (in
2892 * particular its background) as well as the Button class's attributes.
2893 *
2894 * @param context The Context the view is running in, through which it can
2895 * access the current theme, resources, etc.
2896 * @param attrs The attributes of the XML tag that is inflating the view.
2897 * @param defStyle The default style to apply to this view. If 0, no style
2898 * will be applied (beyond what is included in the theme). This may
2899 * either be an attribute resource, whose value will be retrieved
2900 * from the current theme, or an explicit style resource.
2901 * @see #View(Context, AttributeSet)
2902 */
2903 public View(Context context, AttributeSet attrs, int defStyle) {
2904 this(context);
2905
2906 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2907 defStyle, 0);
2908
2909 Drawable background = null;
2910
2911 int leftPadding = -1;
2912 int topPadding = -1;
2913 int rightPadding = -1;
2914 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002915 int startPadding = -1;
2916 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917
2918 int padding = -1;
2919
2920 int viewFlagValues = 0;
2921 int viewFlagMasks = 0;
2922
2923 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 int x = 0;
2926 int y = 0;
2927
Chet Haase73066682010-11-29 15:55:32 -08002928 float tx = 0;
2929 float ty = 0;
2930 float rotation = 0;
2931 float rotationX = 0;
2932 float rotationY = 0;
2933 float sx = 1f;
2934 float sy = 1f;
2935 boolean transformSet = false;
2936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2938
Adam Powell637d3372010-08-25 14:37:03 -07002939 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 final int N = a.getIndexCount();
2941 for (int i = 0; i < N; i++) {
2942 int attr = a.getIndex(i);
2943 switch (attr) {
2944 case com.android.internal.R.styleable.View_background:
2945 background = a.getDrawable(attr);
2946 break;
2947 case com.android.internal.R.styleable.View_padding:
2948 padding = a.getDimensionPixelSize(attr, -1);
2949 break;
2950 case com.android.internal.R.styleable.View_paddingLeft:
2951 leftPadding = a.getDimensionPixelSize(attr, -1);
2952 break;
2953 case com.android.internal.R.styleable.View_paddingTop:
2954 topPadding = a.getDimensionPixelSize(attr, -1);
2955 break;
2956 case com.android.internal.R.styleable.View_paddingRight:
2957 rightPadding = a.getDimensionPixelSize(attr, -1);
2958 break;
2959 case com.android.internal.R.styleable.View_paddingBottom:
2960 bottomPadding = a.getDimensionPixelSize(attr, -1);
2961 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002962 case com.android.internal.R.styleable.View_paddingStart:
2963 startPadding = a.getDimensionPixelSize(attr, -1);
2964 break;
2965 case com.android.internal.R.styleable.View_paddingEnd:
2966 endPadding = a.getDimensionPixelSize(attr, -1);
2967 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 case com.android.internal.R.styleable.View_scrollX:
2969 x = a.getDimensionPixelOffset(attr, 0);
2970 break;
2971 case com.android.internal.R.styleable.View_scrollY:
2972 y = a.getDimensionPixelOffset(attr, 0);
2973 break;
Chet Haase73066682010-11-29 15:55:32 -08002974 case com.android.internal.R.styleable.View_alpha:
2975 setAlpha(a.getFloat(attr, 1f));
2976 break;
2977 case com.android.internal.R.styleable.View_transformPivotX:
2978 setPivotX(a.getDimensionPixelOffset(attr, 0));
2979 break;
2980 case com.android.internal.R.styleable.View_transformPivotY:
2981 setPivotY(a.getDimensionPixelOffset(attr, 0));
2982 break;
2983 case com.android.internal.R.styleable.View_translationX:
2984 tx = a.getDimensionPixelOffset(attr, 0);
2985 transformSet = true;
2986 break;
2987 case com.android.internal.R.styleable.View_translationY:
2988 ty = a.getDimensionPixelOffset(attr, 0);
2989 transformSet = true;
2990 break;
2991 case com.android.internal.R.styleable.View_rotation:
2992 rotation = a.getFloat(attr, 0);
2993 transformSet = true;
2994 break;
2995 case com.android.internal.R.styleable.View_rotationX:
2996 rotationX = a.getFloat(attr, 0);
2997 transformSet = true;
2998 break;
2999 case com.android.internal.R.styleable.View_rotationY:
3000 rotationY = a.getFloat(attr, 0);
3001 transformSet = true;
3002 break;
3003 case com.android.internal.R.styleable.View_scaleX:
3004 sx = a.getFloat(attr, 1f);
3005 transformSet = true;
3006 break;
3007 case com.android.internal.R.styleable.View_scaleY:
3008 sy = a.getFloat(attr, 1f);
3009 transformSet = true;
3010 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 case com.android.internal.R.styleable.View_id:
3012 mID = a.getResourceId(attr, NO_ID);
3013 break;
3014 case com.android.internal.R.styleable.View_tag:
3015 mTag = a.getText(attr);
3016 break;
3017 case com.android.internal.R.styleable.View_fitsSystemWindows:
3018 if (a.getBoolean(attr, false)) {
3019 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3020 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3021 }
3022 break;
3023 case com.android.internal.R.styleable.View_focusable:
3024 if (a.getBoolean(attr, false)) {
3025 viewFlagValues |= FOCUSABLE;
3026 viewFlagMasks |= FOCUSABLE_MASK;
3027 }
3028 break;
3029 case com.android.internal.R.styleable.View_focusableInTouchMode:
3030 if (a.getBoolean(attr, false)) {
3031 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3032 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3033 }
3034 break;
3035 case com.android.internal.R.styleable.View_clickable:
3036 if (a.getBoolean(attr, false)) {
3037 viewFlagValues |= CLICKABLE;
3038 viewFlagMasks |= CLICKABLE;
3039 }
3040 break;
3041 case com.android.internal.R.styleable.View_longClickable:
3042 if (a.getBoolean(attr, false)) {
3043 viewFlagValues |= LONG_CLICKABLE;
3044 viewFlagMasks |= LONG_CLICKABLE;
3045 }
3046 break;
3047 case com.android.internal.R.styleable.View_saveEnabled:
3048 if (!a.getBoolean(attr, true)) {
3049 viewFlagValues |= SAVE_DISABLED;
3050 viewFlagMasks |= SAVE_DISABLED_MASK;
3051 }
3052 break;
3053 case com.android.internal.R.styleable.View_duplicateParentState:
3054 if (a.getBoolean(attr, false)) {
3055 viewFlagValues |= DUPLICATE_PARENT_STATE;
3056 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3057 }
3058 break;
3059 case com.android.internal.R.styleable.View_visibility:
3060 final int visibility = a.getInt(attr, 0);
3061 if (visibility != 0) {
3062 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3063 viewFlagMasks |= VISIBILITY_MASK;
3064 }
3065 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003066 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003067 // Clear any layout direction flags (included resolved bits) already set
3068 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3069 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003070 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003071 final int value = (layoutDirection != -1) ?
3072 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3073 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003074 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 case com.android.internal.R.styleable.View_drawingCacheQuality:
3076 final int cacheQuality = a.getInt(attr, 0);
3077 if (cacheQuality != 0) {
3078 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3079 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3080 }
3081 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003082 case com.android.internal.R.styleable.View_contentDescription:
3083 mContentDescription = a.getString(attr);
3084 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3086 if (!a.getBoolean(attr, true)) {
3087 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3088 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3089 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003090 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3092 if (!a.getBoolean(attr, true)) {
3093 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3094 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3095 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003096 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 case R.styleable.View_scrollbars:
3098 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3099 if (scrollbars != SCROLLBARS_NONE) {
3100 viewFlagValues |= scrollbars;
3101 viewFlagMasks |= SCROLLBARS_MASK;
3102 initializeScrollbars(a);
3103 }
3104 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003105 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003107 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3108 // Ignore the attribute starting with ICS
3109 break;
3110 }
3111 // With builds < ICS, fall through and apply fading edges
3112 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3114 if (fadingEdge != FADING_EDGE_NONE) {
3115 viewFlagValues |= fadingEdge;
3116 viewFlagMasks |= FADING_EDGE_MASK;
3117 initializeFadingEdge(a);
3118 }
3119 break;
3120 case R.styleable.View_scrollbarStyle:
3121 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3122 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3123 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3124 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3125 }
3126 break;
3127 case R.styleable.View_isScrollContainer:
3128 setScrollContainer = true;
3129 if (a.getBoolean(attr, false)) {
3130 setScrollContainer(true);
3131 }
3132 break;
3133 case com.android.internal.R.styleable.View_keepScreenOn:
3134 if (a.getBoolean(attr, false)) {
3135 viewFlagValues |= KEEP_SCREEN_ON;
3136 viewFlagMasks |= KEEP_SCREEN_ON;
3137 }
3138 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003139 case R.styleable.View_filterTouchesWhenObscured:
3140 if (a.getBoolean(attr, false)) {
3141 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3142 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3143 }
3144 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 case R.styleable.View_nextFocusLeft:
3146 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3147 break;
3148 case R.styleable.View_nextFocusRight:
3149 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3150 break;
3151 case R.styleable.View_nextFocusUp:
3152 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3153 break;
3154 case R.styleable.View_nextFocusDown:
3155 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3156 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003157 case R.styleable.View_nextFocusForward:
3158 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3159 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 case R.styleable.View_minWidth:
3161 mMinWidth = a.getDimensionPixelSize(attr, 0);
3162 break;
3163 case R.styleable.View_minHeight:
3164 mMinHeight = a.getDimensionPixelSize(attr, 0);
3165 break;
Romain Guy9a817362009-05-01 10:57:14 -07003166 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003167 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003168 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003169 + "be used within a restricted context");
3170 }
3171
Romain Guy9a817362009-05-01 10:57:14 -07003172 final String handlerName = a.getString(attr);
3173 if (handlerName != null) {
3174 setOnClickListener(new OnClickListener() {
3175 private Method mHandler;
3176
3177 public void onClick(View v) {
3178 if (mHandler == null) {
3179 try {
3180 mHandler = getContext().getClass().getMethod(handlerName,
3181 View.class);
3182 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003183 int id = getId();
3184 String idText = id == NO_ID ? "" : " with id '"
3185 + getContext().getResources().getResourceEntryName(
3186 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003187 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003188 handlerName + "(View) in the activity "
3189 + getContext().getClass() + " for onClick handler"
3190 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003191 }
3192 }
3193
3194 try {
3195 mHandler.invoke(getContext(), View.this);
3196 } catch (IllegalAccessException e) {
3197 throw new IllegalStateException("Could not execute non "
3198 + "public method of the activity", e);
3199 } catch (InvocationTargetException e) {
3200 throw new IllegalStateException("Could not execute "
3201 + "method of the activity", e);
3202 }
3203 }
3204 });
3205 }
3206 break;
Adam Powell637d3372010-08-25 14:37:03 -07003207 case R.styleable.View_overScrollMode:
3208 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3209 break;
Adam Powell20232d02010-12-08 21:08:53 -08003210 case R.styleable.View_verticalScrollbarPosition:
3211 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3212 break;
Romain Guy171c5922011-01-06 10:04:23 -08003213 case R.styleable.View_layerType:
3214 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3215 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003216 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003217 // Clear any text direction flag already set
3218 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3219 // Set the text direction flags depending on the value of the attribute
3220 final int textDirection = a.getInt(attr, -1);
3221 if (textDirection != -1) {
3222 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3223 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003224 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
3226 }
3227
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003228 a.recycle();
3229
Adam Powell637d3372010-08-25 14:37:03 -07003230 setOverScrollMode(overScrollMode);
3231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 if (background != null) {
3233 setBackgroundDrawable(background);
3234 }
3235
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003236 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3237 // layout direction). Those cached values will be used later during padding resolution.
3238 mUserPaddingStart = startPadding;
3239 mUserPaddingEnd = endPadding;
3240
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003241 updateUserPaddingRelative();
3242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 if (padding >= 0) {
3244 leftPadding = padding;
3245 topPadding = padding;
3246 rightPadding = padding;
3247 bottomPadding = padding;
3248 }
3249
3250 // If the user specified the padding (either with android:padding or
3251 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3252 // use the default padding or the padding from the background drawable
3253 // (stored at this point in mPadding*)
3254 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3255 topPadding >= 0 ? topPadding : mPaddingTop,
3256 rightPadding >= 0 ? rightPadding : mPaddingRight,
3257 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3258
3259 if (viewFlagMasks != 0) {
3260 setFlags(viewFlagValues, viewFlagMasks);
3261 }
3262
3263 // Needs to be called after mViewFlags is set
3264 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3265 recomputePadding();
3266 }
3267
3268 if (x != 0 || y != 0) {
3269 scrollTo(x, y);
3270 }
3271
Chet Haase73066682010-11-29 15:55:32 -08003272 if (transformSet) {
3273 setTranslationX(tx);
3274 setTranslationY(ty);
3275 setRotation(rotation);
3276 setRotationX(rotationX);
3277 setRotationY(rotationY);
3278 setScaleX(sx);
3279 setScaleY(sy);
3280 }
3281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3283 setScrollContainer(true);
3284 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003285
3286 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
3288
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003289 private void updateUserPaddingRelative() {
3290 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3291 }
3292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 /**
3294 * Non-public constructor for use in testing
3295 */
3296 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003297 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 }
3299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 /**
3301 * <p>
3302 * Initializes the fading edges from a given set of styled attributes. This
3303 * method should be called by subclasses that need fading edges and when an
3304 * instance of these subclasses is created programmatically rather than
3305 * being inflated from XML. This method is automatically called when the XML
3306 * is inflated.
3307 * </p>
3308 *
3309 * @param a the styled attributes set to initialize the fading edges from
3310 */
3311 protected void initializeFadingEdge(TypedArray a) {
3312 initScrollCache();
3313
3314 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3315 R.styleable.View_fadingEdgeLength,
3316 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3317 }
3318
3319 /**
3320 * Returns the size of the vertical faded edges used to indicate that more
3321 * content in this view is visible.
3322 *
3323 * @return The size in pixels of the vertical faded edge or 0 if vertical
3324 * faded edges are not enabled for this view.
3325 * @attr ref android.R.styleable#View_fadingEdgeLength
3326 */
3327 public int getVerticalFadingEdgeLength() {
3328 if (isVerticalFadingEdgeEnabled()) {
3329 ScrollabilityCache cache = mScrollCache;
3330 if (cache != null) {
3331 return cache.fadingEdgeLength;
3332 }
3333 }
3334 return 0;
3335 }
3336
3337 /**
3338 * Set the size of the faded edge used to indicate that more content in this
3339 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003340 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3341 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3342 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 *
3344 * @param length The size in pixels of the faded edge used to indicate that more
3345 * content in this view is visible.
3346 */
3347 public void setFadingEdgeLength(int length) {
3348 initScrollCache();
3349 mScrollCache.fadingEdgeLength = length;
3350 }
3351
3352 /**
3353 * Returns the size of the horizontal faded edges used to indicate that more
3354 * content in this view is visible.
3355 *
3356 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3357 * faded edges are not enabled for this view.
3358 * @attr ref android.R.styleable#View_fadingEdgeLength
3359 */
3360 public int getHorizontalFadingEdgeLength() {
3361 if (isHorizontalFadingEdgeEnabled()) {
3362 ScrollabilityCache cache = mScrollCache;
3363 if (cache != null) {
3364 return cache.fadingEdgeLength;
3365 }
3366 }
3367 return 0;
3368 }
3369
3370 /**
3371 * Returns the width of the vertical scrollbar.
3372 *
3373 * @return The width in pixels of the vertical scrollbar or 0 if there
3374 * is no vertical scrollbar.
3375 */
3376 public int getVerticalScrollbarWidth() {
3377 ScrollabilityCache cache = mScrollCache;
3378 if (cache != null) {
3379 ScrollBarDrawable scrollBar = cache.scrollBar;
3380 if (scrollBar != null) {
3381 int size = scrollBar.getSize(true);
3382 if (size <= 0) {
3383 size = cache.scrollBarSize;
3384 }
3385 return size;
3386 }
3387 return 0;
3388 }
3389 return 0;
3390 }
3391
3392 /**
3393 * Returns the height of the horizontal scrollbar.
3394 *
3395 * @return The height in pixels of the horizontal scrollbar or 0 if
3396 * there is no horizontal scrollbar.
3397 */
3398 protected int getHorizontalScrollbarHeight() {
3399 ScrollabilityCache cache = mScrollCache;
3400 if (cache != null) {
3401 ScrollBarDrawable scrollBar = cache.scrollBar;
3402 if (scrollBar != null) {
3403 int size = scrollBar.getSize(false);
3404 if (size <= 0) {
3405 size = cache.scrollBarSize;
3406 }
3407 return size;
3408 }
3409 return 0;
3410 }
3411 return 0;
3412 }
3413
3414 /**
3415 * <p>
3416 * Initializes the scrollbars from a given set of styled attributes. This
3417 * method should be called by subclasses that need scrollbars and when an
3418 * instance of these subclasses is created programmatically rather than
3419 * being inflated from XML. This method is automatically called when the XML
3420 * is inflated.
3421 * </p>
3422 *
3423 * @param a the styled attributes set to initialize the scrollbars from
3424 */
3425 protected void initializeScrollbars(TypedArray a) {
3426 initScrollCache();
3427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003429
Mike Cleronf116bf82009-09-27 19:14:12 -07003430 if (scrollabilityCache.scrollBar == null) {
3431 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3432 }
Joe Malin32736f02011-01-19 16:14:20 -08003433
Romain Guy8bda2482010-03-02 11:42:11 -08003434 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435
Mike Cleronf116bf82009-09-27 19:14:12 -07003436 if (!fadeScrollbars) {
3437 scrollabilityCache.state = ScrollabilityCache.ON;
3438 }
3439 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003440
3441
Mike Cleronf116bf82009-09-27 19:14:12 -07003442 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3443 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3444 .getScrollBarFadeDuration());
3445 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3446 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3447 ViewConfiguration.getScrollDefaultDelay());
3448
Joe Malin32736f02011-01-19 16:14:20 -08003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3451 com.android.internal.R.styleable.View_scrollbarSize,
3452 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3453
3454 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3455 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3456
3457 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3458 if (thumb != null) {
3459 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3460 }
3461
3462 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3463 false);
3464 if (alwaysDraw) {
3465 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3466 }
3467
3468 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3469 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3470
3471 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3472 if (thumb != null) {
3473 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3474 }
3475
3476 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3477 false);
3478 if (alwaysDraw) {
3479 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3480 }
3481
3482 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003483 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485
3486 /**
3487 * <p>
3488 * Initalizes the scrollability cache if necessary.
3489 * </p>
3490 */
3491 private void initScrollCache() {
3492 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003493 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495 }
3496
3497 /**
Adam Powell20232d02010-12-08 21:08:53 -08003498 * Set the position of the vertical scroll bar. Should be one of
3499 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3500 * {@link #SCROLLBAR_POSITION_RIGHT}.
3501 *
3502 * @param position Where the vertical scroll bar should be positioned.
3503 */
3504 public void setVerticalScrollbarPosition(int position) {
3505 if (mVerticalScrollbarPosition != position) {
3506 mVerticalScrollbarPosition = position;
3507 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003508 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003509 }
3510 }
3511
3512 /**
3513 * @return The position where the vertical scroll bar will show, if applicable.
3514 * @see #setVerticalScrollbarPosition(int)
3515 */
3516 public int getVerticalScrollbarPosition() {
3517 return mVerticalScrollbarPosition;
3518 }
3519
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003520 ListenerInfo getListenerInfo() {
3521 if (mListenerInfo != null) {
3522 return mListenerInfo;
3523 }
3524 mListenerInfo = new ListenerInfo();
3525 return mListenerInfo;
3526 }
3527
Adam Powell20232d02010-12-08 21:08:53 -08003528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 * Register a callback to be invoked when focus of this view changed.
3530 *
3531 * @param l The callback that will run.
3532 */
3533 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003534 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536
3537 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003538 * Add a listener that will be called when the bounds of the view change due to
3539 * layout processing.
3540 *
3541 * @param listener The listener that will be called when layout bounds change.
3542 */
3543 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003544 ListenerInfo li = getListenerInfo();
3545 if (li.mOnLayoutChangeListeners == null) {
3546 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003547 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003548 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3549 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003550 }
Chet Haase21cd1382010-09-01 17:42:29 -07003551 }
3552
3553 /**
3554 * Remove a listener for layout changes.
3555 *
3556 * @param listener The listener for layout bounds change.
3557 */
3558 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003559 ListenerInfo li = mListenerInfo;
3560 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003561 return;
3562 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003563 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003564 }
3565
3566 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003567 * Add a listener for attach state changes.
3568 *
3569 * This listener will be called whenever this view is attached or detached
3570 * from a window. Remove the listener using
3571 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3572 *
3573 * @param listener Listener to attach
3574 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3575 */
3576 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003577 ListenerInfo li = getListenerInfo();
3578 if (li.mOnAttachStateChangeListeners == null) {
3579 li.mOnAttachStateChangeListeners
3580 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003581 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003582 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003583 }
3584
3585 /**
3586 * Remove a listener for attach state changes. The listener will receive no further
3587 * notification of window attach/detach events.
3588 *
3589 * @param listener Listener to remove
3590 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3591 */
3592 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003593 ListenerInfo li = mListenerInfo;
3594 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003595 return;
3596 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003597 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003598 }
3599
3600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 * Returns the focus-change callback registered for this view.
3602 *
3603 * @return The callback, or null if one is not registered.
3604 */
3605 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003606 ListenerInfo li = mListenerInfo;
3607 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 }
3609
3610 /**
3611 * Register a callback to be invoked when this view is clicked. If this view is not
3612 * clickable, it becomes clickable.
3613 *
3614 * @param l The callback that will run
3615 *
3616 * @see #setClickable(boolean)
3617 */
3618 public void setOnClickListener(OnClickListener l) {
3619 if (!isClickable()) {
3620 setClickable(true);
3621 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003622 getListenerInfo().mOnClickListener = l;
3623 }
3624
3625 /**
3626 * Return whether this view has an attached OnClickListener. Returns
3627 * true if there is a listener, false if there is none.
3628 */
3629 public boolean hasOnClickListeners() {
3630 ListenerInfo li = mListenerInfo;
3631 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633
3634 /**
3635 * Register a callback to be invoked when this view is clicked and held. If this view is not
3636 * long clickable, it becomes long clickable.
3637 *
3638 * @param l The callback that will run
3639 *
3640 * @see #setLongClickable(boolean)
3641 */
3642 public void setOnLongClickListener(OnLongClickListener l) {
3643 if (!isLongClickable()) {
3644 setLongClickable(true);
3645 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003646 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 }
3648
3649 /**
3650 * Register a callback to be invoked when the context menu for this view is
3651 * being built. If this view is not long clickable, it becomes long clickable.
3652 *
3653 * @param l The callback that will run
3654 *
3655 */
3656 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3657 if (!isLongClickable()) {
3658 setLongClickable(true);
3659 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003660 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 }
3662
3663 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003664 * Call this view's OnClickListener, if it is defined. Performs all normal
3665 * actions associated with clicking: reporting accessibility event, playing
3666 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 *
3668 * @return True there was an assigned OnClickListener that was called, false
3669 * otherwise is returned.
3670 */
3671 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003672 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3673
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 ListenerInfo li = mListenerInfo;
3675 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003677 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 return true;
3679 }
3680
3681 return false;
3682 }
3683
3684 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003685 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3686 * this only calls the listener, and does not do any associated clicking
3687 * actions like reporting an accessibility event.
3688 *
3689 * @return True there was an assigned OnClickListener that was called, false
3690 * otherwise is returned.
3691 */
3692 public boolean callOnClick() {
3693 ListenerInfo li = mListenerInfo;
3694 if (li != null && li.mOnClickListener != null) {
3695 li.mOnClickListener.onClick(this);
3696 return true;
3697 }
3698 return false;
3699 }
3700
3701 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003702 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3703 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003705 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 */
3707 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003708 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003711 ListenerInfo li = mListenerInfo;
3712 if (li != null && li.mOnLongClickListener != null) {
3713 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 }
3715 if (!handled) {
3716 handled = showContextMenu();
3717 }
3718 if (handled) {
3719 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3720 }
3721 return handled;
3722 }
3723
3724 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003725 * Performs button-related actions during a touch down event.
3726 *
3727 * @param event The event.
3728 * @return True if the down was consumed.
3729 *
3730 * @hide
3731 */
3732 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3733 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3734 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3735 return true;
3736 }
3737 }
3738 return false;
3739 }
3740
3741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 * Bring up the context menu for this view.
3743 *
3744 * @return Whether a context menu was displayed.
3745 */
3746 public boolean showContextMenu() {
3747 return getParent().showContextMenuForChild(this);
3748 }
3749
3750 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003751 * Bring up the context menu for this view, referring to the item under the specified point.
3752 *
3753 * @param x The referenced x coordinate.
3754 * @param y The referenced y coordinate.
3755 * @param metaState The keyboard modifiers that were pressed.
3756 * @return Whether a context menu was displayed.
3757 *
3758 * @hide
3759 */
3760 public boolean showContextMenu(float x, float y, int metaState) {
3761 return showContextMenu();
3762 }
3763
3764 /**
Adam Powell6e346362010-07-23 10:18:23 -07003765 * Start an action mode.
3766 *
3767 * @param callback Callback that will control the lifecycle of the action mode
3768 * @return The new action mode if it is started, null otherwise
3769 *
3770 * @see ActionMode
3771 */
3772 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003773 ViewParent parent = getParent();
3774 if (parent == null) return null;
3775 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003776 }
3777
3778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 * Register a callback to be invoked when a key is pressed in this view.
3780 * @param l the key listener to attach to this view
3781 */
3782 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003783 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 }
3785
3786 /**
3787 * Register a callback to be invoked when a touch event is sent to this view.
3788 * @param l the touch listener to attach to this view
3789 */
3790 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003791 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 }
3793
3794 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003795 * Register a callback to be invoked when a generic motion event is sent to this view.
3796 * @param l the generic motion listener to attach to this view
3797 */
3798 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003799 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003800 }
3801
3802 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003803 * Register a callback to be invoked when a hover event is sent to this view.
3804 * @param l the hover listener to attach to this view
3805 */
3806 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003807 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003808 }
3809
3810 /**
Joe Malin32736f02011-01-19 16:14:20 -08003811 * Register a drag event listener callback object for this View. The parameter is
3812 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3813 * View, the system calls the
3814 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3815 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003816 */
3817 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003819 }
3820
3821 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003822 * Give this view focus. This will cause
3823 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 *
3825 * Note: this does not check whether this {@link View} should get focus, it just
3826 * gives it focus no matter what. It should only be called internally by framework
3827 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3828 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003829 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3830 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 * focus moved when requestFocus() is called. It may not always
3832 * apply, in which case use the default View.FOCUS_DOWN.
3833 * @param previouslyFocusedRect The rectangle of the view that had focus
3834 * prior in this View's coordinate system.
3835 */
3836 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3837 if (DBG) {
3838 System.out.println(this + " requestFocus()");
3839 }
3840
3841 if ((mPrivateFlags & FOCUSED) == 0) {
3842 mPrivateFlags |= FOCUSED;
3843
3844 if (mParent != null) {
3845 mParent.requestChildFocus(this, this);
3846 }
3847
3848 onFocusChanged(true, direction, previouslyFocusedRect);
3849 refreshDrawableState();
3850 }
3851 }
3852
3853 /**
3854 * Request that a rectangle of this view be visible on the screen,
3855 * scrolling if necessary just enough.
3856 *
3857 * <p>A View should call this if it maintains some notion of which part
3858 * of its content is interesting. For example, a text editing view
3859 * should call this when its cursor moves.
3860 *
3861 * @param rectangle The rectangle.
3862 * @return Whether any parent scrolled.
3863 */
3864 public boolean requestRectangleOnScreen(Rect rectangle) {
3865 return requestRectangleOnScreen(rectangle, false);
3866 }
3867
3868 /**
3869 * Request that a rectangle of this view be visible on the screen,
3870 * scrolling if necessary just enough.
3871 *
3872 * <p>A View should call this if it maintains some notion of which part
3873 * of its content is interesting. For example, a text editing view
3874 * should call this when its cursor moves.
3875 *
3876 * <p>When <code>immediate</code> is set to true, scrolling will not be
3877 * animated.
3878 *
3879 * @param rectangle The rectangle.
3880 * @param immediate True to forbid animated scrolling, false otherwise
3881 * @return Whether any parent scrolled.
3882 */
3883 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3884 View child = this;
3885 ViewParent parent = mParent;
3886 boolean scrolled = false;
3887 while (parent != null) {
3888 scrolled |= parent.requestChildRectangleOnScreen(child,
3889 rectangle, immediate);
3890
3891 // offset rect so next call has the rectangle in the
3892 // coordinate system of its direct child.
3893 rectangle.offset(child.getLeft(), child.getTop());
3894 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3895
3896 if (!(parent instanceof View)) {
3897 break;
3898 }
Romain Guy8506ab42009-06-11 17:35:47 -07003899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 child = (View) parent;
3901 parent = child.getParent();
3902 }
3903 return scrolled;
3904 }
3905
3906 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003907 * Called when this view wants to give up focus. If focus is cleared
3908 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3909 * <p>
3910 * <strong>Note:</strong> When a View clears focus the framework is trying
3911 * to give focus to the first focusable View from the top. Hence, if this
3912 * View is the first from the top that can take focus, then its focus will
3913 * not be cleared nor will the focus change callback be invoked.
3914 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 */
3916 public void clearFocus() {
3917 if (DBG) {
3918 System.out.println(this + " clearFocus()");
3919 }
3920
3921 if ((mPrivateFlags & FOCUSED) != 0) {
3922 mPrivateFlags &= ~FOCUSED;
3923
3924 if (mParent != null) {
3925 mParent.clearChildFocus(this);
3926 }
3927
3928 onFocusChanged(false, 0, null);
3929 refreshDrawableState();
3930 }
3931 }
3932
3933 /**
3934 * Called to clear the focus of a view that is about to be removed.
3935 * Doesn't call clearChildFocus, which prevents this view from taking
3936 * focus again before it has been removed from the parent
3937 */
3938 void clearFocusForRemoval() {
3939 if ((mPrivateFlags & FOCUSED) != 0) {
3940 mPrivateFlags &= ~FOCUSED;
3941
3942 onFocusChanged(false, 0, null);
3943 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003944
3945 // The view cleared focus and invoked the callbacks, so now is the
3946 // time to give focus to the the first focusable from the top to
3947 // ensure that the gain focus is announced after clear focus.
3948 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 }
3950 }
3951
3952 /**
3953 * Called internally by the view system when a new view is getting focus.
3954 * This is what clears the old focus.
3955 */
3956 void unFocus() {
3957 if (DBG) {
3958 System.out.println(this + " unFocus()");
3959 }
3960
3961 if ((mPrivateFlags & FOCUSED) != 0) {
3962 mPrivateFlags &= ~FOCUSED;
3963
3964 onFocusChanged(false, 0, null);
3965 refreshDrawableState();
3966 }
3967 }
3968
3969 /**
3970 * Returns true if this view has focus iteself, or is the ancestor of the
3971 * view that has focus.
3972 *
3973 * @return True if this view has or contains focus, false otherwise.
3974 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003975 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 public boolean hasFocus() {
3977 return (mPrivateFlags & FOCUSED) != 0;
3978 }
3979
3980 /**
3981 * Returns true if this view is focusable or if it contains a reachable View
3982 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3983 * is a View whose parents do not block descendants focus.
3984 *
3985 * Only {@link #VISIBLE} views are considered focusable.
3986 *
3987 * @return True if the view is focusable or if the view contains a focusable
3988 * View, false otherwise.
3989 *
3990 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3991 */
3992 public boolean hasFocusable() {
3993 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3994 }
3995
3996 /**
3997 * Called by the view system when the focus state of this view changes.
3998 * When the focus change event is caused by directional navigation, direction
3999 * and previouslyFocusedRect provide insight into where the focus is coming from.
4000 * When overriding, be sure to call up through to the super class so that
4001 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 * @param gainFocus True if the View has focus; false otherwise.
4004 * @param direction The direction focus has moved when requestFocus()
4005 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004006 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4007 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4008 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4010 * system, of the previously focused view. If applicable, this will be
4011 * passed in as finer grained information about where the focus is coming
4012 * from (in addition to direction). Will be <code>null</code> otherwise.
4013 */
4014 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004015 if (gainFocus) {
4016 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4017 }
4018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 InputMethodManager imm = InputMethodManager.peekInstance();
4020 if (!gainFocus) {
4021 if (isPressed()) {
4022 setPressed(false);
4023 }
4024 if (imm != null && mAttachInfo != null
4025 && mAttachInfo.mHasWindowFocus) {
4026 imm.focusOut(this);
4027 }
Romain Guya2431d02009-04-30 16:30:00 -07004028 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 } else if (imm != null && mAttachInfo != null
4030 && mAttachInfo.mHasWindowFocus) {
4031 imm.focusIn(this);
4032 }
Romain Guy8506ab42009-06-11 17:35:47 -07004033
Romain Guy0fd89bf2011-01-26 15:41:30 -08004034 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004035 ListenerInfo li = mListenerInfo;
4036 if (li != null && li.mOnFocusChangeListener != null) {
4037 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 }
Joe Malin32736f02011-01-19 16:14:20 -08004039
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004040 if (mAttachInfo != null) {
4041 mAttachInfo.mKeyDispatchState.reset(this);
4042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 }
4044
4045 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004046 * Sends an accessibility event of the given type. If accessiiblity is
4047 * not enabled this method has no effect. The default implementation calls
4048 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4049 * to populate information about the event source (this View), then calls
4050 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4051 * populate the text content of the event source including its descendants,
4052 * and last calls
4053 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4054 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004055 * <p>
4056 * If an {@link AccessibilityDelegate} has been specified via calling
4057 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4058 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4059 * responsible for handling this call.
4060 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004061 *
Scott Mainb303d832011-10-12 16:45:18 -07004062 * @param eventType The type of the event to send, as defined by several types from
4063 * {@link android.view.accessibility.AccessibilityEvent}, such as
4064 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4065 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004066 *
4067 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4068 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4069 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004070 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004071 */
4072 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004073 if (mAccessibilityDelegate != null) {
4074 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4075 } else {
4076 sendAccessibilityEventInternal(eventType);
4077 }
4078 }
4079
4080 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004081 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4082 * {@link AccessibilityEvent} to make an announcement which is related to some
4083 * sort of a context change for which none of the events representing UI transitions
4084 * is a good fit. For example, announcing a new page in a book. If accessibility
4085 * is not enabled this method does nothing.
4086 *
4087 * @param text The announcement text.
4088 */
4089 public void announceForAccessibility(CharSequence text) {
4090 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4091 AccessibilityEvent event = AccessibilityEvent.obtain(
4092 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4093 event.getText().add(text);
4094 sendAccessibilityEventUnchecked(event);
4095 }
4096 }
4097
4098 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004099 * @see #sendAccessibilityEvent(int)
4100 *
4101 * Note: Called from the default {@link AccessibilityDelegate}.
4102 */
4103 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004104 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4105 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4106 }
4107 }
4108
4109 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004110 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4111 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004112 * perform a check whether accessibility is enabled.
4113 * <p>
4114 * If an {@link AccessibilityDelegate} has been specified via calling
4115 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4116 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4117 * is responsible for handling this call.
4118 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004119 *
4120 * @param event The event to send.
4121 *
4122 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004123 */
4124 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004125 if (mAccessibilityDelegate != null) {
4126 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4127 } else {
4128 sendAccessibilityEventUncheckedInternal(event);
4129 }
4130 }
4131
4132 /**
4133 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4134 *
4135 * Note: Called from the default {@link AccessibilityDelegate}.
4136 */
4137 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004138 if (!isShown()) {
4139 return;
4140 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004141 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004142 // Only a subset of accessibility events populates text content.
4143 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4144 dispatchPopulateAccessibilityEvent(event);
4145 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004146 // In the beginning we called #isShown(), so we know that getParent() is not null.
4147 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004148 }
4149
4150 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004151 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4152 * to its children for adding their text content to the event. Note that the
4153 * event text is populated in a separate dispatch path since we add to the
4154 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004155 * A typical implementation will call
4156 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4157 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4158 * on each child. Override this method if custom population of the event text
4159 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004160 * <p>
4161 * If an {@link AccessibilityDelegate} has been specified via calling
4162 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4163 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4164 * is responsible for handling this call.
4165 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004166 * <p>
4167 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4168 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4169 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004170 *
4171 * @param event The event.
4172 *
4173 * @return True if the event population was completed.
4174 */
4175 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004176 if (mAccessibilityDelegate != null) {
4177 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4178 } else {
4179 return dispatchPopulateAccessibilityEventInternal(event);
4180 }
4181 }
4182
4183 /**
4184 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4185 *
4186 * Note: Called from the default {@link AccessibilityDelegate}.
4187 */
4188 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004189 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004190 return false;
4191 }
4192
4193 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004194 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004195 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004196 * text content. While this method is free to modify event
4197 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004198 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4199 * <p>
4200 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004201 * to the text added by the super implementation:
4202 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004203 * super.onPopulateAccessibilityEvent(event);
4204 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4205 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4206 * mCurrentDate.getTimeInMillis(), flags);
4207 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004208 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004209 * <p>
4210 * If an {@link AccessibilityDelegate} has been specified via calling
4211 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4212 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4213 * is responsible for handling this call.
4214 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004215 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4216 * information to the event, in case the default implementation has basic information to add.
4217 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004218 *
4219 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004220 *
4221 * @see #sendAccessibilityEvent(int)
4222 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004223 */
4224 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004225 if (mAccessibilityDelegate != null) {
4226 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4227 } else {
4228 onPopulateAccessibilityEventInternal(event);
4229 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004230 }
4231
4232 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004233 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4234 *
4235 * Note: Called from the default {@link AccessibilityDelegate}.
4236 */
4237 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4238
4239 }
4240
4241 /**
4242 * Initializes an {@link AccessibilityEvent} with information about
4243 * this View which is the event source. In other words, the source of
4244 * an accessibility event is the view whose state change triggered firing
4245 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004246 * <p>
4247 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004248 * to properties set by the super implementation:
4249 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4250 * super.onInitializeAccessibilityEvent(event);
4251 * event.setPassword(true);
4252 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004253 * <p>
4254 * If an {@link AccessibilityDelegate} has been specified via calling
4255 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4256 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4257 * is responsible for handling this call.
4258 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004259 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4260 * information to the event, in case the default implementation has basic information to add.
4261 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004262 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004263 *
4264 * @see #sendAccessibilityEvent(int)
4265 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4266 */
4267 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004268 if (mAccessibilityDelegate != null) {
4269 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4270 } else {
4271 onInitializeAccessibilityEventInternal(event);
4272 }
4273 }
4274
4275 /**
4276 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4277 *
4278 * Note: Called from the default {@link AccessibilityDelegate}.
4279 */
4280 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004281 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004282 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004283 event.setPackageName(getContext().getPackageName());
4284 event.setEnabled(isEnabled());
4285 event.setContentDescription(mContentDescription);
4286
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004287 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4288 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4289 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4290 FOCUSABLES_ALL);
4291 event.setItemCount(focusablesTempList.size());
4292 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4293 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004294 }
4295 }
4296
4297 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004298 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4299 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4300 * This method is responsible for obtaining an accessibility node info from a
4301 * pool of reusable instances and calling
4302 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4303 * initialize the former.
4304 * <p>
4305 * Note: The client is responsible for recycling the obtained instance by calling
4306 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4307 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004308 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004309 * @return A populated {@link AccessibilityNodeInfo}.
4310 *
4311 * @see AccessibilityNodeInfo
4312 */
4313 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004314 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4315 if (provider != null) {
4316 return provider.createAccessibilityNodeInfo(View.NO_ID);
4317 } else {
4318 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4319 onInitializeAccessibilityNodeInfo(info);
4320 return info;
4321 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004322 }
4323
4324 /**
4325 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4326 * The base implementation sets:
4327 * <ul>
4328 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004329 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4330 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004331 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4332 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4333 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4334 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4335 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4336 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4337 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4338 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4339 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4340 * </ul>
4341 * <p>
4342 * Subclasses should override this method, call the super implementation,
4343 * and set additional attributes.
4344 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004345 * <p>
4346 * If an {@link AccessibilityDelegate} has been specified via calling
4347 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4348 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4349 * is responsible for handling this call.
4350 * </p>
4351 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004352 * @param info The instance to initialize.
4353 */
4354 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004355 if (mAccessibilityDelegate != null) {
4356 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4357 } else {
4358 onInitializeAccessibilityNodeInfoInternal(info);
4359 }
4360 }
4361
4362 /**
4363 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4364 *
4365 * Note: Called from the default {@link AccessibilityDelegate}.
4366 */
4367 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004368 Rect bounds = mAttachInfo.mTmpInvalRect;
4369 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004370 info.setBoundsInParent(bounds);
4371
4372 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4373 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004374 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004375 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4376 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004377
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004378 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4379 ViewParent parent = getParent();
4380 if (parent instanceof View) {
4381 View parentView = (View) parent;
4382 info.setParent(parentView);
4383 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004384 }
4385
4386 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004387 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004388 info.setContentDescription(getContentDescription());
4389
4390 info.setEnabled(isEnabled());
4391 info.setClickable(isClickable());
4392 info.setFocusable(isFocusable());
4393 info.setFocused(isFocused());
4394 info.setSelected(isSelected());
4395 info.setLongClickable(isLongClickable());
4396
4397 // TODO: These make sense only if we are in an AdapterView but all
4398 // views can be selected. Maybe from accessiiblity perspective
4399 // we should report as selectable view in an AdapterView.
4400 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4401 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4402
4403 if (isFocusable()) {
4404 if (isFocused()) {
4405 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4406 } else {
4407 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4408 }
4409 }
4410 }
4411
4412 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004413 * Sets a delegate for implementing accessibility support via compositon as
4414 * opposed to inheritance. The delegate's primary use is for implementing
4415 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4416 *
4417 * @param delegate The delegate instance.
4418 *
4419 * @see AccessibilityDelegate
4420 */
4421 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4422 mAccessibilityDelegate = delegate;
4423 }
4424
4425 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004426 * Gets the provider for managing a virtual view hierarchy rooted at this View
4427 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4428 * that explore the window content.
4429 * <p>
4430 * If this method returns an instance, this instance is responsible for managing
4431 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4432 * View including the one representing the View itself. Similarly the returned
4433 * instance is responsible for performing accessibility actions on any virtual
4434 * view or the root view itself.
4435 * </p>
4436 * <p>
4437 * If an {@link AccessibilityDelegate} has been specified via calling
4438 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4439 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4440 * is responsible for handling this call.
4441 * </p>
4442 *
4443 * @return The provider.
4444 *
4445 * @see AccessibilityNodeProvider
4446 */
4447 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4448 if (mAccessibilityDelegate != null) {
4449 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4450 } else {
4451 return null;
4452 }
4453 }
4454
4455 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004456 * Gets the unique identifier of this view on the screen for accessibility purposes.
4457 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4458 *
4459 * @return The view accessibility id.
4460 *
4461 * @hide
4462 */
4463 public int getAccessibilityViewId() {
4464 if (mAccessibilityViewId == NO_ID) {
4465 mAccessibilityViewId = sNextAccessibilityViewId++;
4466 }
4467 return mAccessibilityViewId;
4468 }
4469
4470 /**
4471 * Gets the unique identifier of the window in which this View reseides.
4472 *
4473 * @return The window accessibility id.
4474 *
4475 * @hide
4476 */
4477 public int getAccessibilityWindowId() {
4478 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4479 }
4480
4481 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004482 * Gets the {@link View} description. It briefly describes the view and is
4483 * primarily used for accessibility support. Set this property to enable
4484 * better accessibility support for your application. This is especially
4485 * true for views that do not have textual representation (For example,
4486 * ImageButton).
4487 *
4488 * @return The content descriptiopn.
4489 *
4490 * @attr ref android.R.styleable#View_contentDescription
4491 */
4492 public CharSequence getContentDescription() {
4493 return mContentDescription;
4494 }
4495
4496 /**
4497 * Sets the {@link View} description. It briefly describes the view and is
4498 * primarily used for accessibility support. Set this property to enable
4499 * better accessibility support for your application. This is especially
4500 * true for views that do not have textual representation (For example,
4501 * ImageButton).
4502 *
4503 * @param contentDescription The content description.
4504 *
4505 * @attr ref android.R.styleable#View_contentDescription
4506 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004507 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004508 public void setContentDescription(CharSequence contentDescription) {
4509 mContentDescription = contentDescription;
4510 }
4511
4512 /**
Romain Guya2431d02009-04-30 16:30:00 -07004513 * Invoked whenever this view loses focus, either by losing window focus or by losing
4514 * focus within its window. This method can be used to clear any state tied to the
4515 * focus. For instance, if a button is held pressed with the trackball and the window
4516 * loses focus, this method can be used to cancel the press.
4517 *
4518 * Subclasses of View overriding this method should always call super.onFocusLost().
4519 *
4520 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004521 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004522 *
4523 * @hide pending API council approval
4524 */
4525 protected void onFocusLost() {
4526 resetPressedState();
4527 }
4528
4529 private void resetPressedState() {
4530 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4531 return;
4532 }
4533
4534 if (isPressed()) {
4535 setPressed(false);
4536
4537 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004538 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004539 }
4540 }
4541 }
4542
4543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 * Returns true if this view has focus
4545 *
4546 * @return True if this view has focus, false otherwise.
4547 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004548 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 public boolean isFocused() {
4550 return (mPrivateFlags & FOCUSED) != 0;
4551 }
4552
4553 /**
4554 * Find the view in the hierarchy rooted at this view that currently has
4555 * focus.
4556 *
4557 * @return The view that currently has focus, or null if no focused view can
4558 * be found.
4559 */
4560 public View findFocus() {
4561 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4562 }
4563
4564 /**
4565 * Change whether this view is one of the set of scrollable containers in
4566 * its window. This will be used to determine whether the window can
4567 * resize or must pan when a soft input area is open -- scrollable
4568 * containers allow the window to use resize mode since the container
4569 * will appropriately shrink.
4570 */
4571 public void setScrollContainer(boolean isScrollContainer) {
4572 if (isScrollContainer) {
4573 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4574 mAttachInfo.mScrollContainers.add(this);
4575 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4576 }
4577 mPrivateFlags |= SCROLL_CONTAINER;
4578 } else {
4579 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4580 mAttachInfo.mScrollContainers.remove(this);
4581 }
4582 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4583 }
4584 }
4585
4586 /**
4587 * Returns the quality of the drawing cache.
4588 *
4589 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4590 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4591 *
4592 * @see #setDrawingCacheQuality(int)
4593 * @see #setDrawingCacheEnabled(boolean)
4594 * @see #isDrawingCacheEnabled()
4595 *
4596 * @attr ref android.R.styleable#View_drawingCacheQuality
4597 */
4598 public int getDrawingCacheQuality() {
4599 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4600 }
4601
4602 /**
4603 * Set the drawing cache quality of this view. This value is used only when the
4604 * drawing cache is enabled
4605 *
4606 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4607 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4608 *
4609 * @see #getDrawingCacheQuality()
4610 * @see #setDrawingCacheEnabled(boolean)
4611 * @see #isDrawingCacheEnabled()
4612 *
4613 * @attr ref android.R.styleable#View_drawingCacheQuality
4614 */
4615 public void setDrawingCacheQuality(int quality) {
4616 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4617 }
4618
4619 /**
4620 * Returns whether the screen should remain on, corresponding to the current
4621 * value of {@link #KEEP_SCREEN_ON}.
4622 *
4623 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4624 *
4625 * @see #setKeepScreenOn(boolean)
4626 *
4627 * @attr ref android.R.styleable#View_keepScreenOn
4628 */
4629 public boolean getKeepScreenOn() {
4630 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4631 }
4632
4633 /**
4634 * Controls whether the screen should remain on, modifying the
4635 * value of {@link #KEEP_SCREEN_ON}.
4636 *
4637 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4638 *
4639 * @see #getKeepScreenOn()
4640 *
4641 * @attr ref android.R.styleable#View_keepScreenOn
4642 */
4643 public void setKeepScreenOn(boolean keepScreenOn) {
4644 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4645 }
4646
4647 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004648 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4649 * @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 -08004650 *
4651 * @attr ref android.R.styleable#View_nextFocusLeft
4652 */
4653 public int getNextFocusLeftId() {
4654 return mNextFocusLeftId;
4655 }
4656
4657 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004658 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4659 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4660 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 *
4662 * @attr ref android.R.styleable#View_nextFocusLeft
4663 */
4664 public void setNextFocusLeftId(int nextFocusLeftId) {
4665 mNextFocusLeftId = nextFocusLeftId;
4666 }
4667
4668 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004669 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4670 * @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 -08004671 *
4672 * @attr ref android.R.styleable#View_nextFocusRight
4673 */
4674 public int getNextFocusRightId() {
4675 return mNextFocusRightId;
4676 }
4677
4678 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004679 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4680 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4681 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 *
4683 * @attr ref android.R.styleable#View_nextFocusRight
4684 */
4685 public void setNextFocusRightId(int nextFocusRightId) {
4686 mNextFocusRightId = nextFocusRightId;
4687 }
4688
4689 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004690 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4691 * @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 -08004692 *
4693 * @attr ref android.R.styleable#View_nextFocusUp
4694 */
4695 public int getNextFocusUpId() {
4696 return mNextFocusUpId;
4697 }
4698
4699 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004700 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4701 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4702 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 *
4704 * @attr ref android.R.styleable#View_nextFocusUp
4705 */
4706 public void setNextFocusUpId(int nextFocusUpId) {
4707 mNextFocusUpId = nextFocusUpId;
4708 }
4709
4710 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004711 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4712 * @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 -08004713 *
4714 * @attr ref android.R.styleable#View_nextFocusDown
4715 */
4716 public int getNextFocusDownId() {
4717 return mNextFocusDownId;
4718 }
4719
4720 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004721 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4722 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4723 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 *
4725 * @attr ref android.R.styleable#View_nextFocusDown
4726 */
4727 public void setNextFocusDownId(int nextFocusDownId) {
4728 mNextFocusDownId = nextFocusDownId;
4729 }
4730
4731 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004732 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4733 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4734 *
4735 * @attr ref android.R.styleable#View_nextFocusForward
4736 */
4737 public int getNextFocusForwardId() {
4738 return mNextFocusForwardId;
4739 }
4740
4741 /**
4742 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4743 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4744 * decide automatically.
4745 *
4746 * @attr ref android.R.styleable#View_nextFocusForward
4747 */
4748 public void setNextFocusForwardId(int nextFocusForwardId) {
4749 mNextFocusForwardId = nextFocusForwardId;
4750 }
4751
4752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753 * Returns the visibility of this view and all of its ancestors
4754 *
4755 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4756 */
4757 public boolean isShown() {
4758 View current = this;
4759 //noinspection ConstantConditions
4760 do {
4761 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4762 return false;
4763 }
4764 ViewParent parent = current.mParent;
4765 if (parent == null) {
4766 return false; // We are not attached to the view root
4767 }
4768 if (!(parent instanceof View)) {
4769 return true;
4770 }
4771 current = (View) parent;
4772 } while (current != null);
4773
4774 return false;
4775 }
4776
4777 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004778 * Called by the view hierarchy when the content insets for a window have
4779 * changed, to allow it to adjust its content to fit within those windows.
4780 * The content insets tell you the space that the status bar, input method,
4781 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004783 * <p>You do not normally need to deal with this function, since the default
4784 * window decoration given to applications takes care of applying it to the
4785 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
4786 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
4787 * and your content can be placed under those system elements. You can then
4788 * use this method within your view hierarchy if you have parts of your UI
4789 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004791 * <p>The default implementation of this method simply applies the content
4792 * inset's to the view's padding. This can be enabled through
4793 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
4794 * the method and handle the insets however you would like. Note that the
4795 * insets provided by the framework are always relative to the far edges
4796 * of the window, not accounting for the location of the called view within
4797 * that window. (In fact when this method is called you do not yet know
4798 * where the layout will place the view, as it is done before layout happens.)
4799 *
4800 * <p>Note: unlike many View methods, there is no dispatch phase to this
4801 * call. If you are overriding it in a ViewGroup and want to allow the
4802 * call to continue to your children, you must be sure to call the super
4803 * implementation.
4804 *
4805 * @param insets Current content insets of the window. Prior to
4806 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
4807 * the insets or else you and Android will be unhappy.
4808 *
4809 * @return Return true if this view applied the insets and it should not
4810 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 */
4812 protected boolean fitSystemWindows(Rect insets) {
4813 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004814 mUserPaddingStart = -1;
4815 mUserPaddingEnd = -1;
4816 mUserPaddingRelative = false;
4817 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
4818 || mAttachInfo == null
4819 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
4820 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
4821 return true;
4822 } else {
4823 internalSetPadding(0, 0, 0, 0);
4824 return false;
4825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 }
4827 return false;
4828 }
4829
4830 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004831 * Set whether or not this view should account for system screen decorations
4832 * such as the status bar and inset its content. This allows this view to be
4833 * positioned in absolute screen coordinates and remain visible to the user.
4834 *
4835 * <p>This should only be used by top-level window decor views.
4836 *
4837 * @param fitSystemWindows true to inset content for system screen decorations, false for
4838 * default behavior.
4839 *
4840 * @attr ref android.R.styleable#View_fitsSystemWindows
4841 */
4842 public void setFitsSystemWindows(boolean fitSystemWindows) {
4843 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4844 }
4845
4846 /**
4847 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4848 * will account for system screen decorations such as the status bar and inset its
4849 * content. This allows the view to be positioned in absolute screen coordinates
4850 * and remain visible to the user.
4851 *
4852 * @return true if this view will adjust its content bounds for system screen decorations.
4853 *
4854 * @attr ref android.R.styleable#View_fitsSystemWindows
4855 */
4856 public boolean fitsSystemWindows() {
4857 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4858 }
4859
4860 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004861 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
4862 */
4863 public void requestFitSystemWindows() {
4864 if (mParent != null) {
4865 mParent.requestFitSystemWindows();
4866 }
4867 }
4868
4869 /**
4870 * For use by PhoneWindow to make its own system window fitting optional.
4871 * @hide
4872 */
4873 public void makeOptionalFitsSystemWindows() {
4874 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
4875 }
4876
4877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 * Returns the visibility status for this view.
4879 *
4880 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4881 * @attr ref android.R.styleable#View_visibility
4882 */
4883 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004884 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4885 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4886 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 })
4888 public int getVisibility() {
4889 return mViewFlags & VISIBILITY_MASK;
4890 }
4891
4892 /**
4893 * Set the enabled state of this view.
4894 *
4895 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4896 * @attr ref android.R.styleable#View_visibility
4897 */
4898 @RemotableViewMethod
4899 public void setVisibility(int visibility) {
4900 setFlags(visibility, VISIBILITY_MASK);
4901 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4902 }
4903
4904 /**
4905 * Returns the enabled status for this view. The interpretation of the
4906 * enabled state varies by subclass.
4907 *
4908 * @return True if this view is enabled, false otherwise.
4909 */
4910 @ViewDebug.ExportedProperty
4911 public boolean isEnabled() {
4912 return (mViewFlags & ENABLED_MASK) == ENABLED;
4913 }
4914
4915 /**
4916 * Set the enabled state of this view. The interpretation of the enabled
4917 * state varies by subclass.
4918 *
4919 * @param enabled True if this view is enabled, false otherwise.
4920 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004921 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004923 if (enabled == isEnabled()) return;
4924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4926
4927 /*
4928 * The View most likely has to change its appearance, so refresh
4929 * the drawable state.
4930 */
4931 refreshDrawableState();
4932
4933 // Invalidate too, since the default behavior for views is to be
4934 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004935 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 }
4937
4938 /**
4939 * Set whether this view can receive the focus.
4940 *
4941 * Setting this to false will also ensure that this view is not focusable
4942 * in touch mode.
4943 *
4944 * @param focusable If true, this view can receive the focus.
4945 *
4946 * @see #setFocusableInTouchMode(boolean)
4947 * @attr ref android.R.styleable#View_focusable
4948 */
4949 public void setFocusable(boolean focusable) {
4950 if (!focusable) {
4951 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4952 }
4953 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4954 }
4955
4956 /**
4957 * Set whether this view can receive focus while in touch mode.
4958 *
4959 * Setting this to true will also ensure that this view is focusable.
4960 *
4961 * @param focusableInTouchMode If true, this view can receive the focus while
4962 * in touch mode.
4963 *
4964 * @see #setFocusable(boolean)
4965 * @attr ref android.R.styleable#View_focusableInTouchMode
4966 */
4967 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4968 // Focusable in touch mode should always be set before the focusable flag
4969 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4970 // which, in touch mode, will not successfully request focus on this view
4971 // because the focusable in touch mode flag is not set
4972 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4973 if (focusableInTouchMode) {
4974 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4975 }
4976 }
4977
4978 /**
4979 * Set whether this view should have sound effects enabled for events such as
4980 * clicking and touching.
4981 *
4982 * <p>You may wish to disable sound effects for a view if you already play sounds,
4983 * for instance, a dial key that plays dtmf tones.
4984 *
4985 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4986 * @see #isSoundEffectsEnabled()
4987 * @see #playSoundEffect(int)
4988 * @attr ref android.R.styleable#View_soundEffectsEnabled
4989 */
4990 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4991 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4992 }
4993
4994 /**
4995 * @return whether this view should have sound effects enabled for events such as
4996 * clicking and touching.
4997 *
4998 * @see #setSoundEffectsEnabled(boolean)
4999 * @see #playSoundEffect(int)
5000 * @attr ref android.R.styleable#View_soundEffectsEnabled
5001 */
5002 @ViewDebug.ExportedProperty
5003 public boolean isSoundEffectsEnabled() {
5004 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5005 }
5006
5007 /**
5008 * Set whether this view should have haptic feedback for events such as
5009 * long presses.
5010 *
5011 * <p>You may wish to disable haptic feedback if your view already controls
5012 * its own haptic feedback.
5013 *
5014 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5015 * @see #isHapticFeedbackEnabled()
5016 * @see #performHapticFeedback(int)
5017 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5018 */
5019 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5020 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5021 }
5022
5023 /**
5024 * @return whether this view should have haptic feedback enabled for events
5025 * long presses.
5026 *
5027 * @see #setHapticFeedbackEnabled(boolean)
5028 * @see #performHapticFeedback(int)
5029 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5030 */
5031 @ViewDebug.ExportedProperty
5032 public boolean isHapticFeedbackEnabled() {
5033 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5034 }
5035
5036 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005037 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005038 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005039 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5040 * {@link #LAYOUT_DIRECTION_RTL},
5041 * {@link #LAYOUT_DIRECTION_INHERIT} or
5042 * {@link #LAYOUT_DIRECTION_LOCALE}.
5043 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005044 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005045 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005046 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5047 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5048 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5049 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005050 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005051 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005052 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005053 }
5054
5055 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005056 * Set the layout direction for this view. This will propagate a reset of layout direction
5057 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005058 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005059 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5060 * {@link #LAYOUT_DIRECTION_RTL},
5061 * {@link #LAYOUT_DIRECTION_INHERIT} or
5062 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005063 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005064 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005065 */
5066 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005067 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005068 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005069 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005070 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005071 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005072 // Set the new layout direction (filtered) and ask for a layout pass
5073 mPrivateFlags2 |=
5074 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5075 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005076 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005077 }
5078
5079 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005080 * Returns the resolved layout direction for this view.
5081 *
5082 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005083 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005084 */
5085 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005086 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5087 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005088 })
5089 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005090 // The layout diretion will be resolved only if needed
5091 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5092 resolveLayoutDirection();
5093 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005094 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005095 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5096 }
5097
5098 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005099 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5100 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005101 *
5102 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005103 */
5104 @ViewDebug.ExportedProperty(category = "layout")
5105 public boolean isLayoutRtl() {
5106 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5107 }
5108
5109 /**
Adam Powell539ee872012-02-03 19:00:49 -08005110 * Indicates whether the view is currently tracking transient state that the
5111 * app should not need to concern itself with saving and restoring, but that
5112 * the framework should take special note to preserve when possible.
5113 *
5114 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005115 */
5116 @ViewDebug.ExportedProperty(category = "layout")
5117 public boolean hasTransientState() {
5118 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5119 }
5120
5121 /**
5122 * Set whether this view is currently tracking transient state that the
5123 * framework should attempt to preserve when possible.
5124 *
5125 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005126 */
5127 public void setHasTransientState(boolean hasTransientState) {
5128 if (hasTransientState() == hasTransientState) return;
5129
5130 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5131 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5132 if (mParent != null) {
5133 try {
5134 mParent.childHasTransientStateChanged(this, hasTransientState);
5135 } catch (AbstractMethodError e) {
5136 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5137 " does not fully implement ViewParent", e);
5138 }
5139 }
5140 }
5141
5142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 * If this view doesn't do any drawing on its own, set this flag to
5144 * allow further optimizations. By default, this flag is not set on
5145 * View, but could be set on some View subclasses such as ViewGroup.
5146 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005147 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5148 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 *
5150 * @param willNotDraw whether or not this View draw on its own
5151 */
5152 public void setWillNotDraw(boolean willNotDraw) {
5153 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5154 }
5155
5156 /**
5157 * Returns whether or not this View draws on its own.
5158 *
5159 * @return true if this view has nothing to draw, false otherwise
5160 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005161 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 public boolean willNotDraw() {
5163 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5164 }
5165
5166 /**
5167 * When a View's drawing cache is enabled, drawing is redirected to an
5168 * offscreen bitmap. Some views, like an ImageView, must be able to
5169 * bypass this mechanism if they already draw a single bitmap, to avoid
5170 * unnecessary usage of the memory.
5171 *
5172 * @param willNotCacheDrawing true if this view does not cache its
5173 * drawing, false otherwise
5174 */
5175 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5176 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5177 }
5178
5179 /**
5180 * Returns whether or not this View can cache its drawing or not.
5181 *
5182 * @return true if this view does not cache its drawing, false otherwise
5183 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005184 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 public boolean willNotCacheDrawing() {
5186 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5187 }
5188
5189 /**
5190 * Indicates whether this view reacts to click events or not.
5191 *
5192 * @return true if the view is clickable, false otherwise
5193 *
5194 * @see #setClickable(boolean)
5195 * @attr ref android.R.styleable#View_clickable
5196 */
5197 @ViewDebug.ExportedProperty
5198 public boolean isClickable() {
5199 return (mViewFlags & CLICKABLE) == CLICKABLE;
5200 }
5201
5202 /**
5203 * Enables or disables click events for this view. When a view
5204 * is clickable it will change its state to "pressed" on every click.
5205 * Subclasses should set the view clickable to visually react to
5206 * user's clicks.
5207 *
5208 * @param clickable true to make the view clickable, false otherwise
5209 *
5210 * @see #isClickable()
5211 * @attr ref android.R.styleable#View_clickable
5212 */
5213 public void setClickable(boolean clickable) {
5214 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5215 }
5216
5217 /**
5218 * Indicates whether this view reacts to long click events or not.
5219 *
5220 * @return true if the view is long clickable, false otherwise
5221 *
5222 * @see #setLongClickable(boolean)
5223 * @attr ref android.R.styleable#View_longClickable
5224 */
5225 public boolean isLongClickable() {
5226 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5227 }
5228
5229 /**
5230 * Enables or disables long click events for this view. When a view is long
5231 * clickable it reacts to the user holding down the button for a longer
5232 * duration than a tap. This event can either launch the listener or a
5233 * context menu.
5234 *
5235 * @param longClickable true to make the view long clickable, false otherwise
5236 * @see #isLongClickable()
5237 * @attr ref android.R.styleable#View_longClickable
5238 */
5239 public void setLongClickable(boolean longClickable) {
5240 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5241 }
5242
5243 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005244 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 *
5246 * @see #isClickable()
5247 * @see #setClickable(boolean)
5248 *
5249 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5250 * the View's internal state from a previously set "pressed" state.
5251 */
5252 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005253 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 if (pressed) {
5256 mPrivateFlags |= PRESSED;
5257 } else {
5258 mPrivateFlags &= ~PRESSED;
5259 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005260
5261 if (needsRefresh) {
5262 refreshDrawableState();
5263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 dispatchSetPressed(pressed);
5265 }
5266
5267 /**
5268 * Dispatch setPressed to all of this View's children.
5269 *
5270 * @see #setPressed(boolean)
5271 *
5272 * @param pressed The new pressed state
5273 */
5274 protected void dispatchSetPressed(boolean pressed) {
5275 }
5276
5277 /**
5278 * Indicates whether the view is currently in pressed state. Unless
5279 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5280 * the pressed state.
5281 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005282 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 * @see #isClickable()
5284 * @see #setClickable(boolean)
5285 *
5286 * @return true if the view is currently pressed, false otherwise
5287 */
5288 public boolean isPressed() {
5289 return (mPrivateFlags & PRESSED) == PRESSED;
5290 }
5291
5292 /**
5293 * Indicates whether this view will save its state (that is,
5294 * whether its {@link #onSaveInstanceState} method will be called).
5295 *
5296 * @return Returns true if the view state saving is enabled, else false.
5297 *
5298 * @see #setSaveEnabled(boolean)
5299 * @attr ref android.R.styleable#View_saveEnabled
5300 */
5301 public boolean isSaveEnabled() {
5302 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5303 }
5304
5305 /**
5306 * Controls whether the saving of this view's state is
5307 * enabled (that is, whether its {@link #onSaveInstanceState} method
5308 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005309 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 * for its state to be saved. This flag can only disable the
5311 * saving of this view; any child views may still have their state saved.
5312 *
5313 * @param enabled Set to false to <em>disable</em> state saving, or true
5314 * (the default) to allow it.
5315 *
5316 * @see #isSaveEnabled()
5317 * @see #setId(int)
5318 * @see #onSaveInstanceState()
5319 * @attr ref android.R.styleable#View_saveEnabled
5320 */
5321 public void setSaveEnabled(boolean enabled) {
5322 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5323 }
5324
Jeff Brown85a31762010-09-01 17:01:00 -07005325 /**
5326 * Gets whether the framework should discard touches when the view's
5327 * window is obscured by another visible window.
5328 * Refer to the {@link View} security documentation for more details.
5329 *
5330 * @return True if touch filtering is enabled.
5331 *
5332 * @see #setFilterTouchesWhenObscured(boolean)
5333 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5334 */
5335 @ViewDebug.ExportedProperty
5336 public boolean getFilterTouchesWhenObscured() {
5337 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5338 }
5339
5340 /**
5341 * Sets whether the framework should discard touches when the view's
5342 * window is obscured by another visible window.
5343 * Refer to the {@link View} security documentation for more details.
5344 *
5345 * @param enabled True if touch filtering should be enabled.
5346 *
5347 * @see #getFilterTouchesWhenObscured
5348 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5349 */
5350 public void setFilterTouchesWhenObscured(boolean enabled) {
5351 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5352 FILTER_TOUCHES_WHEN_OBSCURED);
5353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354
5355 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005356 * Indicates whether the entire hierarchy under this view will save its
5357 * state when a state saving traversal occurs from its parent. The default
5358 * is true; if false, these views will not be saved unless
5359 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5360 *
5361 * @return Returns true if the view state saving from parent is enabled, else false.
5362 *
5363 * @see #setSaveFromParentEnabled(boolean)
5364 */
5365 public boolean isSaveFromParentEnabled() {
5366 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5367 }
5368
5369 /**
5370 * Controls whether the entire hierarchy under this view will save its
5371 * state when a state saving traversal occurs from its parent. The default
5372 * is true; if false, these views will not be saved unless
5373 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5374 *
5375 * @param enabled Set to false to <em>disable</em> state saving, or true
5376 * (the default) to allow it.
5377 *
5378 * @see #isSaveFromParentEnabled()
5379 * @see #setId(int)
5380 * @see #onSaveInstanceState()
5381 */
5382 public void setSaveFromParentEnabled(boolean enabled) {
5383 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5384 }
5385
5386
5387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 * Returns whether this View is able to take focus.
5389 *
5390 * @return True if this view can take focus, or false otherwise.
5391 * @attr ref android.R.styleable#View_focusable
5392 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005393 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 public final boolean isFocusable() {
5395 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5396 }
5397
5398 /**
5399 * When a view is focusable, it may not want to take focus when in touch mode.
5400 * For example, a button would like focus when the user is navigating via a D-pad
5401 * so that the user can click on it, but once the user starts touching the screen,
5402 * the button shouldn't take focus
5403 * @return Whether the view is focusable in touch mode.
5404 * @attr ref android.R.styleable#View_focusableInTouchMode
5405 */
5406 @ViewDebug.ExportedProperty
5407 public final boolean isFocusableInTouchMode() {
5408 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5409 }
5410
5411 /**
5412 * Find the nearest view in the specified direction that can take focus.
5413 * This does not actually give focus to that view.
5414 *
5415 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5416 *
5417 * @return The nearest focusable in the specified direction, or null if none
5418 * can be found.
5419 */
5420 public View focusSearch(int direction) {
5421 if (mParent != null) {
5422 return mParent.focusSearch(this, direction);
5423 } else {
5424 return null;
5425 }
5426 }
5427
5428 /**
5429 * This method is the last chance for the focused view and its ancestors to
5430 * respond to an arrow key. This is called when the focused view did not
5431 * consume the key internally, nor could the view system find a new view in
5432 * the requested direction to give focus to.
5433 *
5434 * @param focused The currently focused view.
5435 * @param direction The direction focus wants to move. One of FOCUS_UP,
5436 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5437 * @return True if the this view consumed this unhandled move.
5438 */
5439 public boolean dispatchUnhandledMove(View focused, int direction) {
5440 return false;
5441 }
5442
5443 /**
5444 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005445 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005447 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5448 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 * @return The user specified next view, or null if there is none.
5450 */
5451 View findUserSetNextFocus(View root, int direction) {
5452 switch (direction) {
5453 case FOCUS_LEFT:
5454 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005455 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 case FOCUS_RIGHT:
5457 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005458 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 case FOCUS_UP:
5460 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005461 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 case FOCUS_DOWN:
5463 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005464 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005465 case FOCUS_FORWARD:
5466 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005467 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005468 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005469 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005470 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005471 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005472 @Override
5473 public boolean apply(View t) {
5474 return t.mNextFocusForwardId == id;
5475 }
5476 });
5477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 }
5479 return null;
5480 }
5481
Jeff Brown4dfbec22011-08-15 14:55:37 -07005482 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5483 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5484 @Override
5485 public boolean apply(View t) {
5486 return t.mID == childViewId;
5487 }
5488 });
5489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 if (result == null) {
5491 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5492 + "by user for id " + childViewId);
5493 }
5494 return result;
5495 }
5496
5497 /**
5498 * Find and return all focusable views that are descendants of this view,
5499 * possibly including this view if it is focusable itself.
5500 *
5501 * @param direction The direction of the focus
5502 * @return A list of focusable views
5503 */
5504 public ArrayList<View> getFocusables(int direction) {
5505 ArrayList<View> result = new ArrayList<View>(24);
5506 addFocusables(result, direction);
5507 return result;
5508 }
5509
5510 /**
5511 * Add any focusable views that are descendants of this view (possibly
5512 * including this view if it is focusable itself) to views. If we are in touch mode,
5513 * only add views that are also focusable in touch mode.
5514 *
5515 * @param views Focusable views found so far
5516 * @param direction The direction of the focus
5517 */
5518 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005519 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521
svetoslavganov75986cf2009-05-14 22:28:01 -07005522 /**
5523 * Adds any focusable views that are descendants of this view (possibly
5524 * including this view if it is focusable itself) to views. This method
5525 * adds all focusable views regardless if we are in touch mode or
5526 * only views focusable in touch mode if we are in touch mode depending on
5527 * the focusable mode paramater.
5528 *
5529 * @param views Focusable views found so far or null if all we are interested is
5530 * the number of focusables.
5531 * @param direction The direction of the focus.
5532 * @param focusableMode The type of focusables to be added.
5533 *
5534 * @see #FOCUSABLES_ALL
5535 * @see #FOCUSABLES_TOUCH_MODE
5536 */
5537 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5538 if (!isFocusable()) {
5539 return;
5540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541
svetoslavganov75986cf2009-05-14 22:28:01 -07005542 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5543 isInTouchMode() && !isFocusableInTouchMode()) {
5544 return;
5545 }
5546
5547 if (views != null) {
5548 views.add(this);
5549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 }
5551
5552 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005553 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005554 * The search is performed by either the text that the View renders or the content
5555 * description that describes the view for accessibility purposes and the view does
5556 * not render or both. Clients can specify how the search is to be performed via
5557 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5558 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005559 *
5560 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005561 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005562 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005563 * @see #FIND_VIEWS_WITH_TEXT
5564 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5565 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005566 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005567 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005568 if (getAccessibilityNodeProvider() != null) {
5569 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5570 outViews.add(this);
5571 }
5572 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5573 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005574 String searchedLowerCase = searched.toString().toLowerCase();
5575 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5576 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5577 outViews.add(this);
5578 }
5579 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005580 }
5581
5582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 * Find and return all touchable views that are descendants of this view,
5584 * possibly including this view if it is touchable itself.
5585 *
5586 * @return A list of touchable views
5587 */
5588 public ArrayList<View> getTouchables() {
5589 ArrayList<View> result = new ArrayList<View>();
5590 addTouchables(result);
5591 return result;
5592 }
5593
5594 /**
5595 * Add any touchable views that are descendants of this view (possibly
5596 * including this view if it is touchable itself) to views.
5597 *
5598 * @param views Touchable views found so far
5599 */
5600 public void addTouchables(ArrayList<View> views) {
5601 final int viewFlags = mViewFlags;
5602
5603 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5604 && (viewFlags & ENABLED_MASK) == ENABLED) {
5605 views.add(this);
5606 }
5607 }
5608
5609 /**
5610 * Call this to try to give focus to a specific view or to one of its
5611 * descendants.
5612 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005613 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5614 * false), or if it is focusable and it is not focusable in touch mode
5615 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005617 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 * have focus, and you want your parent to look for the next one.
5619 *
5620 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5621 * {@link #FOCUS_DOWN} and <code>null</code>.
5622 *
5623 * @return Whether this view or one of its descendants actually took focus.
5624 */
5625 public final boolean requestFocus() {
5626 return requestFocus(View.FOCUS_DOWN);
5627 }
5628
5629
5630 /**
5631 * Call this to try to give focus to a specific view or to one of its
5632 * descendants and give it a hint about what direction focus is heading.
5633 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005634 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5635 * false), or if it is focusable and it is not focusable in touch mode
5636 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005638 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 * have focus, and you want your parent to look for the next one.
5640 *
5641 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5642 * <code>null</code> set for the previously focused rectangle.
5643 *
5644 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5645 * @return Whether this view or one of its descendants actually took focus.
5646 */
5647 public final boolean requestFocus(int direction) {
5648 return requestFocus(direction, null);
5649 }
5650
5651 /**
5652 * Call this to try to give focus to a specific view or to one of its descendants
5653 * and give it hints about the direction and a specific rectangle that the focus
5654 * is coming from. The rectangle can help give larger views a finer grained hint
5655 * about where focus is coming from, and therefore, where to show selection, or
5656 * forward focus change internally.
5657 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005658 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5659 * false), or if it is focusable and it is not focusable in touch mode
5660 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 *
5662 * A View will not take focus if it is not visible.
5663 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005664 * A View will not take focus if one of its parents has
5665 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5666 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005668 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 * have focus, and you want your parent to look for the next one.
5670 *
5671 * You may wish to override this method if your custom {@link View} has an internal
5672 * {@link View} that it wishes to forward the request to.
5673 *
5674 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5675 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5676 * to give a finer grained hint about where focus is coming from. May be null
5677 * if there is no hint.
5678 * @return Whether this view or one of its descendants actually took focus.
5679 */
5680 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5681 // need to be focusable
5682 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5683 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5684 return false;
5685 }
5686
5687 // need to be focusable in touch mode if in touch mode
5688 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005689 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5690 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 }
5692
5693 // need to not have any parents blocking us
5694 if (hasAncestorThatBlocksDescendantFocus()) {
5695 return false;
5696 }
5697
5698 handleFocusGainInternal(direction, previouslyFocusedRect);
5699 return true;
5700 }
5701
5702 /**
5703 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5704 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5705 * touch mode to request focus when they are touched.
5706 *
5707 * @return Whether this view or one of its descendants actually took focus.
5708 *
5709 * @see #isInTouchMode()
5710 *
5711 */
5712 public final boolean requestFocusFromTouch() {
5713 // Leave touch mode if we need to
5714 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005715 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005716 if (viewRoot != null) {
5717 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
5719 }
5720 return requestFocus(View.FOCUS_DOWN);
5721 }
5722
5723 /**
5724 * @return Whether any ancestor of this view blocks descendant focus.
5725 */
5726 private boolean hasAncestorThatBlocksDescendantFocus() {
5727 ViewParent ancestor = mParent;
5728 while (ancestor instanceof ViewGroup) {
5729 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5730 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5731 return true;
5732 } else {
5733 ancestor = vgAncestor.getParent();
5734 }
5735 }
5736 return false;
5737 }
5738
5739 /**
Romain Guya440b002010-02-24 15:57:54 -08005740 * @hide
5741 */
5742 public void dispatchStartTemporaryDetach() {
5743 onStartTemporaryDetach();
5744 }
5745
5746 /**
5747 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5749 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005750 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 */
5752 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005753 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005754 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005755 }
5756
5757 /**
5758 * @hide
5759 */
5760 public void dispatchFinishTemporaryDetach() {
5761 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 }
Romain Guy8506ab42009-06-11 17:35:47 -07005763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 /**
5765 * Called after {@link #onStartTemporaryDetach} when the container is done
5766 * changing the view.
5767 */
5768 public void onFinishTemporaryDetach() {
5769 }
Romain Guy8506ab42009-06-11 17:35:47 -07005770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005772 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5773 * for this view's window. Returns null if the view is not currently attached
5774 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005775 * just use the standard high-level event callbacks like
5776 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005777 */
5778 public KeyEvent.DispatcherState getKeyDispatcherState() {
5779 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5780 }
Joe Malin32736f02011-01-19 16:14:20 -08005781
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 * Dispatch a key event before it is processed by any input method
5784 * associated with the view hierarchy. This can be used to intercept
5785 * key events in special situations before the IME consumes them; a
5786 * typical example would be handling the BACK key to update the application's
5787 * UI instead of allowing the IME to see it and close itself.
5788 *
5789 * @param event The key event to be dispatched.
5790 * @return True if the event was handled, false otherwise.
5791 */
5792 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5793 return onKeyPreIme(event.getKeyCode(), event);
5794 }
5795
5796 /**
5797 * Dispatch a key event to the next view on the focus path. This path runs
5798 * from the top of the view tree down to the currently focused view. If this
5799 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5800 * the next node down the focus path. This method also fires any key
5801 * listeners.
5802 *
5803 * @param event The key event to be dispatched.
5804 * @return True if the event was handled, false otherwise.
5805 */
5806 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005807 if (mInputEventConsistencyVerifier != null) {
5808 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810
Jeff Brown21bc5c92011-02-28 18:27:14 -08005811 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005812 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005813 ListenerInfo li = mListenerInfo;
5814 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5815 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 return true;
5817 }
5818
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005819 if (event.dispatch(this, mAttachInfo != null
5820 ? mAttachInfo.mKeyDispatchState : null, this)) {
5821 return true;
5822 }
5823
5824 if (mInputEventConsistencyVerifier != null) {
5825 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5826 }
5827 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 }
5829
5830 /**
5831 * Dispatches a key shortcut event.
5832 *
5833 * @param event The key event to be dispatched.
5834 * @return True if the event was handled by the view, false otherwise.
5835 */
5836 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5837 return onKeyShortcut(event.getKeyCode(), event);
5838 }
5839
5840 /**
5841 * Pass the touch screen motion event down to the target view, or this
5842 * view if it is the target.
5843 *
5844 * @param event The motion event to be dispatched.
5845 * @return True if the event was handled by the view, false otherwise.
5846 */
5847 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005848 if (mInputEventConsistencyVerifier != null) {
5849 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5850 }
5851
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005852 if (onFilterTouchEventForSecurity(event)) {
5853 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005854 ListenerInfo li = mListenerInfo;
5855 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5856 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005857 return true;
5858 }
5859
5860 if (onTouchEvent(event)) {
5861 return true;
5862 }
Jeff Brown85a31762010-09-01 17:01:00 -07005863 }
5864
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005865 if (mInputEventConsistencyVerifier != null) {
5866 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005868 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 }
5870
5871 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005872 * Filter the touch event to apply security policies.
5873 *
5874 * @param event The motion event to be filtered.
5875 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005876 *
Jeff Brown85a31762010-09-01 17:01:00 -07005877 * @see #getFilterTouchesWhenObscured
5878 */
5879 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005880 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005881 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5882 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5883 // Window is obscured, drop this touch.
5884 return false;
5885 }
5886 return true;
5887 }
5888
5889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 * Pass a trackball motion event down to the focused view.
5891 *
5892 * @param event The motion event to be dispatched.
5893 * @return True if the event was handled by the view, false otherwise.
5894 */
5895 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005896 if (mInputEventConsistencyVerifier != null) {
5897 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5898 }
5899
Romain Guy02ccac62011-06-24 13:20:23 -07005900 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 }
5902
5903 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005904 * Dispatch a generic motion event.
5905 * <p>
5906 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5907 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005908 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005909 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005910 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005911 *
5912 * @param event The motion event to be dispatched.
5913 * @return True if the event was handled by the view, false otherwise.
5914 */
5915 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005916 if (mInputEventConsistencyVerifier != null) {
5917 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5918 }
5919
Jeff Browna032cc02011-03-07 16:56:21 -08005920 final int source = event.getSource();
5921 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5922 final int action = event.getAction();
5923 if (action == MotionEvent.ACTION_HOVER_ENTER
5924 || action == MotionEvent.ACTION_HOVER_MOVE
5925 || action == MotionEvent.ACTION_HOVER_EXIT) {
5926 if (dispatchHoverEvent(event)) {
5927 return true;
5928 }
5929 } else if (dispatchGenericPointerEvent(event)) {
5930 return true;
5931 }
5932 } else if (dispatchGenericFocusedEvent(event)) {
5933 return true;
5934 }
5935
Jeff Brown10b62902011-06-20 16:40:37 -07005936 if (dispatchGenericMotionEventInternal(event)) {
5937 return true;
5938 }
5939
5940 if (mInputEventConsistencyVerifier != null) {
5941 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5942 }
5943 return false;
5944 }
5945
5946 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005947 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005948 ListenerInfo li = mListenerInfo;
5949 if (li != null && li.mOnGenericMotionListener != null
5950 && (mViewFlags & ENABLED_MASK) == ENABLED
5951 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005952 return true;
5953 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005954
5955 if (onGenericMotionEvent(event)) {
5956 return true;
5957 }
5958
5959 if (mInputEventConsistencyVerifier != null) {
5960 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5961 }
5962 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005963 }
5964
5965 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005966 * Dispatch a hover event.
5967 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005968 * Do not call this method directly.
5969 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005970 * </p>
5971 *
5972 * @param event The motion event to be dispatched.
5973 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005974 */
5975 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005976 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005977 ListenerInfo li = mListenerInfo;
5978 if (li != null && li.mOnHoverListener != null
5979 && (mViewFlags & ENABLED_MASK) == ENABLED
5980 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005981 return true;
5982 }
5983
Jeff Browna032cc02011-03-07 16:56:21 -08005984 return onHoverEvent(event);
5985 }
5986
5987 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005988 * Returns true if the view has a child to which it has recently sent
5989 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5990 * it does not have a hovered child, then it must be the innermost hovered view.
5991 * @hide
5992 */
5993 protected boolean hasHoveredChild() {
5994 return false;
5995 }
5996
5997 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005998 * Dispatch a generic motion event to the view under the first pointer.
5999 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006000 * Do not call this method directly.
6001 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006002 * </p>
6003 *
6004 * @param event The motion event to be dispatched.
6005 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006006 */
6007 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6008 return false;
6009 }
6010
6011 /**
6012 * Dispatch a generic motion event to the currently focused view.
6013 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006014 * Do not call this method directly.
6015 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006016 * </p>
6017 *
6018 * @param event The motion event to be dispatched.
6019 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006020 */
6021 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6022 return false;
6023 }
6024
6025 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006026 * Dispatch a pointer event.
6027 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006028 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6029 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6030 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006031 * and should not be expected to handle other pointing device features.
6032 * </p>
6033 *
6034 * @param event The motion event to be dispatched.
6035 * @return True if the event was handled by the view, false otherwise.
6036 * @hide
6037 */
6038 public final boolean dispatchPointerEvent(MotionEvent event) {
6039 if (event.isTouchEvent()) {
6040 return dispatchTouchEvent(event);
6041 } else {
6042 return dispatchGenericMotionEvent(event);
6043 }
6044 }
6045
6046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 * Called when the window containing this view gains or loses window focus.
6048 * ViewGroups should override to route to their children.
6049 *
6050 * @param hasFocus True if the window containing this view now has focus,
6051 * false otherwise.
6052 */
6053 public void dispatchWindowFocusChanged(boolean hasFocus) {
6054 onWindowFocusChanged(hasFocus);
6055 }
6056
6057 /**
6058 * Called when the window containing this view gains or loses focus. Note
6059 * that this is separate from view focus: to receive key events, both
6060 * your view and its window must have focus. If a window is displayed
6061 * on top of yours that takes input focus, then your own window will lose
6062 * focus but the view focus will remain unchanged.
6063 *
6064 * @param hasWindowFocus True if the window containing this view now has
6065 * focus, false otherwise.
6066 */
6067 public void onWindowFocusChanged(boolean hasWindowFocus) {
6068 InputMethodManager imm = InputMethodManager.peekInstance();
6069 if (!hasWindowFocus) {
6070 if (isPressed()) {
6071 setPressed(false);
6072 }
6073 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6074 imm.focusOut(this);
6075 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006076 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006077 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006078 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6080 imm.focusIn(this);
6081 }
6082 refreshDrawableState();
6083 }
6084
6085 /**
6086 * Returns true if this view is in a window that currently has window focus.
6087 * Note that this is not the same as the view itself having focus.
6088 *
6089 * @return True if this view is in a window that currently has window focus.
6090 */
6091 public boolean hasWindowFocus() {
6092 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6093 }
6094
6095 /**
Adam Powell326d8082009-12-09 15:10:07 -08006096 * Dispatch a view visibility change down the view hierarchy.
6097 * ViewGroups should override to route to their children.
6098 * @param changedView The view whose visibility changed. Could be 'this' or
6099 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006100 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6101 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006102 */
6103 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6104 onVisibilityChanged(changedView, visibility);
6105 }
6106
6107 /**
6108 * Called when the visibility of the view or an ancestor of the view is changed.
6109 * @param changedView The view whose visibility changed. Could be 'this' or
6110 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006111 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6112 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006113 */
6114 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006115 if (visibility == VISIBLE) {
6116 if (mAttachInfo != null) {
6117 initialAwakenScrollBars();
6118 } else {
6119 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6120 }
6121 }
Adam Powell326d8082009-12-09 15:10:07 -08006122 }
6123
6124 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006125 * Dispatch a hint about whether this view is displayed. For instance, when
6126 * a View moves out of the screen, it might receives a display hint indicating
6127 * the view is not displayed. Applications should not <em>rely</em> on this hint
6128 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006129 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006130 * @param hint A hint about whether or not this view is displayed:
6131 * {@link #VISIBLE} or {@link #INVISIBLE}.
6132 */
6133 public void dispatchDisplayHint(int hint) {
6134 onDisplayHint(hint);
6135 }
6136
6137 /**
6138 * Gives this view a hint about whether is displayed or not. For instance, when
6139 * a View moves out of the screen, it might receives a display hint indicating
6140 * the view is not displayed. Applications should not <em>rely</em> on this hint
6141 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006142 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006143 * @param hint A hint about whether or not this view is displayed:
6144 * {@link #VISIBLE} or {@link #INVISIBLE}.
6145 */
6146 protected void onDisplayHint(int hint) {
6147 }
6148
6149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 * Dispatch a window visibility change down the view hierarchy.
6151 * ViewGroups should override to route to their children.
6152 *
6153 * @param visibility The new visibility of the window.
6154 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006155 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 */
6157 public void dispatchWindowVisibilityChanged(int visibility) {
6158 onWindowVisibilityChanged(visibility);
6159 }
6160
6161 /**
6162 * Called when the window containing has change its visibility
6163 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6164 * that this tells you whether or not your window is being made visible
6165 * to the window manager; this does <em>not</em> tell you whether or not
6166 * your window is obscured by other windows on the screen, even if it
6167 * is itself visible.
6168 *
6169 * @param visibility The new visibility of the window.
6170 */
6171 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006172 if (visibility == VISIBLE) {
6173 initialAwakenScrollBars();
6174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 }
6176
6177 /**
6178 * Returns the current visibility of the window this view is attached to
6179 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6180 *
6181 * @return Returns the current visibility of the view's window.
6182 */
6183 public int getWindowVisibility() {
6184 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6185 }
6186
6187 /**
6188 * Retrieve the overall visible display size in which the window this view is
6189 * attached to has been positioned in. This takes into account screen
6190 * decorations above the window, for both cases where the window itself
6191 * is being position inside of them or the window is being placed under
6192 * then and covered insets are used for the window to position its content
6193 * inside. In effect, this tells you the available area where content can
6194 * be placed and remain visible to users.
6195 *
6196 * <p>This function requires an IPC back to the window manager to retrieve
6197 * the requested information, so should not be used in performance critical
6198 * code like drawing.
6199 *
6200 * @param outRect Filled in with the visible display frame. If the view
6201 * is not attached to a window, this is simply the raw display size.
6202 */
6203 public void getWindowVisibleDisplayFrame(Rect outRect) {
6204 if (mAttachInfo != null) {
6205 try {
6206 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6207 } catch (RemoteException e) {
6208 return;
6209 }
6210 // XXX This is really broken, and probably all needs to be done
6211 // in the window manager, and we need to know more about whether
6212 // we want the area behind or in front of the IME.
6213 final Rect insets = mAttachInfo.mVisibleInsets;
6214 outRect.left += insets.left;
6215 outRect.top += insets.top;
6216 outRect.right -= insets.right;
6217 outRect.bottom -= insets.bottom;
6218 return;
6219 }
6220 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006221 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 }
6223
6224 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006225 * Dispatch a notification about a resource configuration change down
6226 * the view hierarchy.
6227 * ViewGroups should override to route to their children.
6228 *
6229 * @param newConfig The new resource configuration.
6230 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006231 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006232 */
6233 public void dispatchConfigurationChanged(Configuration newConfig) {
6234 onConfigurationChanged(newConfig);
6235 }
6236
6237 /**
6238 * Called when the current configuration of the resources being used
6239 * by the application have changed. You can use this to decide when
6240 * to reload resources that can changed based on orientation and other
6241 * configuration characterstics. You only need to use this if you are
6242 * not relying on the normal {@link android.app.Activity} mechanism of
6243 * recreating the activity instance upon a configuration change.
6244 *
6245 * @param newConfig The new resource configuration.
6246 */
6247 protected void onConfigurationChanged(Configuration newConfig) {
6248 }
6249
6250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 * Private function to aggregate all per-view attributes in to the view
6252 * root.
6253 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006254 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6255 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 }
6257
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006258 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6259 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006260 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006261 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006262 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006263 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006264 ListenerInfo li = mListenerInfo;
6265 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006266 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 }
6269 }
6270
6271 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006272 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006274 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6275 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 ai.mRecomputeGlobalAttributes = true;
6277 }
6278 }
6279 }
6280
6281 /**
6282 * Returns whether the device is currently in touch mode. Touch mode is entered
6283 * once the user begins interacting with the device by touch, and affects various
6284 * things like whether focus is always visible to the user.
6285 *
6286 * @return Whether the device is in touch mode.
6287 */
6288 @ViewDebug.ExportedProperty
6289 public boolean isInTouchMode() {
6290 if (mAttachInfo != null) {
6291 return mAttachInfo.mInTouchMode;
6292 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006293 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006294 }
6295 }
6296
6297 /**
6298 * Returns the context the view is running in, through which it can
6299 * access the current theme, resources, etc.
6300 *
6301 * @return The view's Context.
6302 */
6303 @ViewDebug.CapturedViewProperty
6304 public final Context getContext() {
6305 return mContext;
6306 }
6307
6308 /**
6309 * Handle a key event before it is processed by any input method
6310 * associated with the view hierarchy. This can be used to intercept
6311 * key events in special situations before the IME consumes them; a
6312 * typical example would be handling the BACK key to update the application's
6313 * UI instead of allowing the IME to see it and close itself.
6314 *
6315 * @param keyCode The value in event.getKeyCode().
6316 * @param event Description of the key event.
6317 * @return If you handled the event, return true. If you want to allow the
6318 * event to be handled by the next receiver, return false.
6319 */
6320 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6321 return false;
6322 }
6323
6324 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006325 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6326 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6328 * is released, if the view is enabled and clickable.
6329 *
6330 * @param keyCode A key code that represents the button pressed, from
6331 * {@link android.view.KeyEvent}.
6332 * @param event The KeyEvent object that defines the button action.
6333 */
6334 public boolean onKeyDown(int keyCode, KeyEvent event) {
6335 boolean result = false;
6336
6337 switch (keyCode) {
6338 case KeyEvent.KEYCODE_DPAD_CENTER:
6339 case KeyEvent.KEYCODE_ENTER: {
6340 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6341 return true;
6342 }
6343 // Long clickable items don't necessarily have to be clickable
6344 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6345 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6346 (event.getRepeatCount() == 0)) {
6347 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006348 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 return true;
6350 }
6351 break;
6352 }
6353 }
6354 return result;
6355 }
6356
6357 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006358 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6359 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6360 * the event).
6361 */
6362 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6363 return false;
6364 }
6365
6366 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006367 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6368 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6370 * {@link KeyEvent#KEYCODE_ENTER} is released.
6371 *
6372 * @param keyCode A key code that represents the button pressed, from
6373 * {@link android.view.KeyEvent}.
6374 * @param event The KeyEvent object that defines the button action.
6375 */
6376 public boolean onKeyUp(int keyCode, KeyEvent event) {
6377 boolean result = false;
6378
6379 switch (keyCode) {
6380 case KeyEvent.KEYCODE_DPAD_CENTER:
6381 case KeyEvent.KEYCODE_ENTER: {
6382 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6383 return true;
6384 }
6385 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6386 setPressed(false);
6387
6388 if (!mHasPerformedLongPress) {
6389 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006390 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391
6392 result = performClick();
6393 }
6394 }
6395 break;
6396 }
6397 }
6398 return result;
6399 }
6400
6401 /**
6402 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6403 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6404 * the event).
6405 *
6406 * @param keyCode A key code that represents the button pressed, from
6407 * {@link android.view.KeyEvent}.
6408 * @param repeatCount The number of times the action was made.
6409 * @param event The KeyEvent object that defines the button action.
6410 */
6411 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6412 return false;
6413 }
6414
6415 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006416 * Called on the focused view when a key shortcut event is not handled.
6417 * Override this method to implement local key shortcuts for the View.
6418 * Key shortcuts can also be implemented by setting the
6419 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 *
6421 * @param keyCode The value in event.getKeyCode().
6422 * @param event Description of the key event.
6423 * @return If you handled the event, return true. If you want to allow the
6424 * event to be handled by the next receiver, return false.
6425 */
6426 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6427 return false;
6428 }
6429
6430 /**
6431 * Check whether the called view is a text editor, in which case it
6432 * would make sense to automatically display a soft input window for
6433 * it. Subclasses should override this if they implement
6434 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006435 * a call on that method would return a non-null InputConnection, and
6436 * they are really a first-class editor that the user would normally
6437 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006438 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006439 * <p>The default implementation always returns false. This does
6440 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6441 * will not be called or the user can not otherwise perform edits on your
6442 * view; it is just a hint to the system that this is not the primary
6443 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 * @return Returns true if this view is a text editor, else false.
6446 */
6447 public boolean onCheckIsTextEditor() {
6448 return false;
6449 }
Romain Guy8506ab42009-06-11 17:35:47 -07006450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 /**
6452 * Create a new InputConnection for an InputMethod to interact
6453 * with the view. The default implementation returns null, since it doesn't
6454 * support input methods. You can override this to implement such support.
6455 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 * <p>When implementing this, you probably also want to implement
6458 * {@link #onCheckIsTextEditor()} to indicate you will return a
6459 * non-null InputConnection.
6460 *
6461 * @param outAttrs Fill in with attribute information about the connection.
6462 */
6463 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6464 return null;
6465 }
6466
6467 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006468 * Called by the {@link android.view.inputmethod.InputMethodManager}
6469 * when a view who is not the current
6470 * input connection target is trying to make a call on the manager. The
6471 * default implementation returns false; you can override this to return
6472 * true for certain views if you are performing InputConnection proxying
6473 * to them.
6474 * @param view The View that is making the InputMethodManager call.
6475 * @return Return true to allow the call, false to reject.
6476 */
6477 public boolean checkInputConnectionProxy(View view) {
6478 return false;
6479 }
Romain Guy8506ab42009-06-11 17:35:47 -07006480
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 * Show the context menu for this view. It is not safe to hold on to the
6483 * menu after returning from this method.
6484 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006485 * You should normally not overload this method. Overload
6486 * {@link #onCreateContextMenu(ContextMenu)} or define an
6487 * {@link OnCreateContextMenuListener} to add items to the context menu.
6488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 * @param menu The context menu to populate
6490 */
6491 public void createContextMenu(ContextMenu menu) {
6492 ContextMenuInfo menuInfo = getContextMenuInfo();
6493
6494 // Sets the current menu info so all items added to menu will have
6495 // my extra info set.
6496 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6497
6498 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006499 ListenerInfo li = mListenerInfo;
6500 if (li != null && li.mOnCreateContextMenuListener != null) {
6501 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 }
6503
6504 // Clear the extra information so subsequent items that aren't mine don't
6505 // have my extra info.
6506 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6507
6508 if (mParent != null) {
6509 mParent.createContextMenu(menu);
6510 }
6511 }
6512
6513 /**
6514 * Views should implement this if they have extra information to associate
6515 * with the context menu. The return result is supplied as a parameter to
6516 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6517 * callback.
6518 *
6519 * @return Extra information about the item for which the context menu
6520 * should be shown. This information will vary across different
6521 * subclasses of View.
6522 */
6523 protected ContextMenuInfo getContextMenuInfo() {
6524 return null;
6525 }
6526
6527 /**
6528 * Views should implement this if the view itself is going to add items to
6529 * the context menu.
6530 *
6531 * @param menu the context menu to populate
6532 */
6533 protected void onCreateContextMenu(ContextMenu menu) {
6534 }
6535
6536 /**
6537 * Implement this method to handle trackball motion events. The
6538 * <em>relative</em> movement of the trackball since the last event
6539 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6540 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6541 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6542 * they will often be fractional values, representing the more fine-grained
6543 * movement information available from a trackball).
6544 *
6545 * @param event The motion event.
6546 * @return True if the event was handled, false otherwise.
6547 */
6548 public boolean onTrackballEvent(MotionEvent event) {
6549 return false;
6550 }
6551
6552 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006553 * Implement this method to handle generic motion events.
6554 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006555 * Generic motion events describe joystick movements, mouse hovers, track pad
6556 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006557 * {@link MotionEvent#getSource() source} of the motion event specifies
6558 * the class of input that was received. Implementations of this method
6559 * must examine the bits in the source before processing the event.
6560 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006561 * </p><p>
6562 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6563 * are delivered to the view under the pointer. All other generic motion events are
6564 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006565 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006566 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006567 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006568 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6569 * // process the joystick movement...
6570 * return true;
6571 * }
6572 * }
6573 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6574 * switch (event.getAction()) {
6575 * case MotionEvent.ACTION_HOVER_MOVE:
6576 * // process the mouse hover movement...
6577 * return true;
6578 * case MotionEvent.ACTION_SCROLL:
6579 * // process the scroll wheel movement...
6580 * return true;
6581 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006582 * }
6583 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006584 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006585 *
6586 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006587 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006588 */
6589 public boolean onGenericMotionEvent(MotionEvent event) {
6590 return false;
6591 }
6592
6593 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006594 * Implement this method to handle hover events.
6595 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006596 * This method is called whenever a pointer is hovering into, over, or out of the
6597 * bounds of a view and the view is not currently being touched.
6598 * Hover events are represented as pointer events with action
6599 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6600 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6601 * </p>
6602 * <ul>
6603 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6604 * when the pointer enters the bounds of the view.</li>
6605 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6606 * when the pointer has already entered the bounds of the view and has moved.</li>
6607 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6608 * when the pointer has exited the bounds of the view or when the pointer is
6609 * about to go down due to a button click, tap, or similar user action that
6610 * causes the view to be touched.</li>
6611 * </ul>
6612 * <p>
6613 * The view should implement this method to return true to indicate that it is
6614 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006615 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006616 * The default implementation calls {@link #setHovered} to update the hovered state
6617 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006618 * is enabled and is clickable. The default implementation also sends hover
6619 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006620 * </p>
6621 *
6622 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006623 * @return True if the view handled the hover event.
6624 *
6625 * @see #isHovered
6626 * @see #setHovered
6627 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006628 */
6629 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006630 // The root view may receive hover (or touch) events that are outside the bounds of
6631 // the window. This code ensures that we only send accessibility events for
6632 // hovers that are actually within the bounds of the root view.
6633 final int action = event.getAction();
6634 if (!mSendingHoverAccessibilityEvents) {
6635 if ((action == MotionEvent.ACTION_HOVER_ENTER
6636 || action == MotionEvent.ACTION_HOVER_MOVE)
6637 && !hasHoveredChild()
6638 && pointInView(event.getX(), event.getY())) {
6639 mSendingHoverAccessibilityEvents = true;
6640 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6641 }
6642 } else {
6643 if (action == MotionEvent.ACTION_HOVER_EXIT
6644 || (action == MotionEvent.ACTION_HOVER_MOVE
6645 && !pointInView(event.getX(), event.getY()))) {
6646 mSendingHoverAccessibilityEvents = false;
6647 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6648 }
Jeff Browna1b24182011-07-28 13:38:24 -07006649 }
6650
Jeff Brown87b7f802011-06-21 18:35:45 -07006651 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006652 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006653 case MotionEvent.ACTION_HOVER_ENTER:
6654 setHovered(true);
6655 break;
6656 case MotionEvent.ACTION_HOVER_EXIT:
6657 setHovered(false);
6658 break;
6659 }
Jeff Browna1b24182011-07-28 13:38:24 -07006660
6661 // Dispatch the event to onGenericMotionEvent before returning true.
6662 // This is to provide compatibility with existing applications that
6663 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6664 // break because of the new default handling for hoverable views
6665 // in onHoverEvent.
6666 // Note that onGenericMotionEvent will be called by default when
6667 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6668 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006669 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006670 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006671 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006672 }
6673
6674 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006675 * Returns true if the view should handle {@link #onHoverEvent}
6676 * by calling {@link #setHovered} to change its hovered state.
6677 *
6678 * @return True if the view is hoverable.
6679 */
6680 private boolean isHoverable() {
6681 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006682 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006683 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6684 return false;
6685 }
6686
6687 return (viewFlags & CLICKABLE) == CLICKABLE
6688 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6689 }
6690
6691 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006692 * Returns true if the view is currently hovered.
6693 *
6694 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006695 *
6696 * @see #setHovered
6697 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006698 */
Jeff Brown10b62902011-06-20 16:40:37 -07006699 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006700 public boolean isHovered() {
6701 return (mPrivateFlags & HOVERED) != 0;
6702 }
6703
6704 /**
6705 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006706 * <p>
6707 * Calling this method also changes the drawable state of the view. This
6708 * enables the view to react to hover by using different drawable resources
6709 * to change its appearance.
6710 * </p><p>
6711 * The {@link #onHoverChanged} method is called when the hovered state changes.
6712 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006713 *
6714 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006715 *
6716 * @see #isHovered
6717 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006718 */
6719 public void setHovered(boolean hovered) {
6720 if (hovered) {
6721 if ((mPrivateFlags & HOVERED) == 0) {
6722 mPrivateFlags |= HOVERED;
6723 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006724 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006725 }
6726 } else {
6727 if ((mPrivateFlags & HOVERED) != 0) {
6728 mPrivateFlags &= ~HOVERED;
6729 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006730 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006731 }
6732 }
6733 }
6734
6735 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006736 * Implement this method to handle hover state changes.
6737 * <p>
6738 * This method is called whenever the hover state changes as a result of a
6739 * call to {@link #setHovered}.
6740 * </p>
6741 *
6742 * @param hovered The current hover state, as returned by {@link #isHovered}.
6743 *
6744 * @see #isHovered
6745 * @see #setHovered
6746 */
6747 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006748 }
6749
6750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006751 * Implement this method to handle touch screen motion events.
6752 *
6753 * @param event The motion event.
6754 * @return True if the event was handled, false otherwise.
6755 */
6756 public boolean onTouchEvent(MotionEvent event) {
6757 final int viewFlags = mViewFlags;
6758
6759 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006760 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006761 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006763 // A disabled view that is clickable still consumes the touch
6764 // events, it just doesn't respond to them.
6765 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6766 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6767 }
6768
6769 if (mTouchDelegate != null) {
6770 if (mTouchDelegate.onTouchEvent(event)) {
6771 return true;
6772 }
6773 }
6774
6775 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6776 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6777 switch (event.getAction()) {
6778 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006779 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6780 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 // take focus if we don't have it already and we should in
6782 // touch mode.
6783 boolean focusTaken = false;
6784 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6785 focusTaken = requestFocus();
6786 }
6787
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006788 if (prepressed) {
6789 // The button is being released before we actually
6790 // showed it as pressed. Make it show the pressed
6791 // state now (before scheduling the click) to ensure
6792 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006793 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006794 }
Joe Malin32736f02011-01-19 16:14:20 -08006795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 if (!mHasPerformedLongPress) {
6797 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006798 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799
6800 // Only perform take click actions if we were in the pressed state
6801 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006802 // Use a Runnable and post this rather than calling
6803 // performClick directly. This lets other visual state
6804 // of the view update before click actions start.
6805 if (mPerformClick == null) {
6806 mPerformClick = new PerformClick();
6807 }
6808 if (!post(mPerformClick)) {
6809 performClick();
6810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 }
6812 }
6813
6814 if (mUnsetPressedState == null) {
6815 mUnsetPressedState = new UnsetPressedState();
6816 }
6817
Adam Powelle14579b2009-12-16 18:39:52 -08006818 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006819 postDelayed(mUnsetPressedState,
6820 ViewConfiguration.getPressedStateDuration());
6821 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 // If the post failed, unpress right now
6823 mUnsetPressedState.run();
6824 }
Adam Powelle14579b2009-12-16 18:39:52 -08006825 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 }
6827 break;
6828
6829 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006830 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006831
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006832 if (performButtonActionOnTouchDown(event)) {
6833 break;
6834 }
6835
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006836 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006837 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006838
6839 // For views inside a scrolling container, delay the pressed feedback for
6840 // a short period in case this is a scroll.
6841 if (isInScrollingContainer) {
6842 mPrivateFlags |= PREPRESSED;
6843 if (mPendingCheckForTap == null) {
6844 mPendingCheckForTap = new CheckForTap();
6845 }
6846 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6847 } else {
6848 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006849 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006850 checkForLongClick(0);
6851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 break;
6853
6854 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006855 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006856 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 break;
6858
6859 case MotionEvent.ACTION_MOVE:
6860 final int x = (int) event.getX();
6861 final int y = (int) event.getY();
6862
6863 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006864 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006866 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006868 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006869 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870
Adam Powell4d6f0662012-02-21 15:11:11 -08006871 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 }
6874 break;
6875 }
6876 return true;
6877 }
6878
6879 return false;
6880 }
6881
6882 /**
Adam Powell10298662011-08-14 18:26:30 -07006883 * @hide
6884 */
6885 public boolean isInScrollingContainer() {
6886 ViewParent p = getParent();
6887 while (p != null && p instanceof ViewGroup) {
6888 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6889 return true;
6890 }
6891 p = p.getParent();
6892 }
6893 return false;
6894 }
6895
6896 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006897 * Remove the longpress detection timer.
6898 */
6899 private void removeLongPressCallback() {
6900 if (mPendingCheckForLongPress != null) {
6901 removeCallbacks(mPendingCheckForLongPress);
6902 }
6903 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006904
6905 /**
6906 * Remove the pending click action
6907 */
6908 private void removePerformClickCallback() {
6909 if (mPerformClick != null) {
6910 removeCallbacks(mPerformClick);
6911 }
6912 }
6913
Adam Powelle14579b2009-12-16 18:39:52 -08006914 /**
Romain Guya440b002010-02-24 15:57:54 -08006915 * Remove the prepress detection timer.
6916 */
6917 private void removeUnsetPressCallback() {
6918 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6919 setPressed(false);
6920 removeCallbacks(mUnsetPressedState);
6921 }
6922 }
6923
6924 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006925 * Remove the tap detection timer.
6926 */
6927 private void removeTapCallback() {
6928 if (mPendingCheckForTap != null) {
6929 mPrivateFlags &= ~PREPRESSED;
6930 removeCallbacks(mPendingCheckForTap);
6931 }
6932 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006933
6934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 * Cancels a pending long press. Your subclass can use this if you
6936 * want the context menu to come up if the user presses and holds
6937 * at the same place, but you don't want it to come up if they press
6938 * and then move around enough to cause scrolling.
6939 */
6940 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006941 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006942
6943 /*
6944 * The prepressed state handled by the tap callback is a display
6945 * construct, but the tap callback will post a long press callback
6946 * less its own timeout. Remove it here.
6947 */
6948 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 }
6950
6951 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006952 * Remove the pending callback for sending a
6953 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6954 */
6955 private void removeSendViewScrolledAccessibilityEventCallback() {
6956 if (mSendViewScrolledAccessibilityEvent != null) {
6957 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6958 }
6959 }
6960
6961 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 * Sets the TouchDelegate for this View.
6963 */
6964 public void setTouchDelegate(TouchDelegate delegate) {
6965 mTouchDelegate = delegate;
6966 }
6967
6968 /**
6969 * Gets the TouchDelegate for this View.
6970 */
6971 public TouchDelegate getTouchDelegate() {
6972 return mTouchDelegate;
6973 }
6974
6975 /**
6976 * Set flags controlling behavior of this view.
6977 *
6978 * @param flags Constant indicating the value which should be set
6979 * @param mask Constant indicating the bit range that should be changed
6980 */
6981 void setFlags(int flags, int mask) {
6982 int old = mViewFlags;
6983 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6984
6985 int changed = mViewFlags ^ old;
6986 if (changed == 0) {
6987 return;
6988 }
6989 int privateFlags = mPrivateFlags;
6990
6991 /* Check if the FOCUSABLE bit has changed */
6992 if (((changed & FOCUSABLE_MASK) != 0) &&
6993 ((privateFlags & HAS_BOUNDS) !=0)) {
6994 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6995 && ((privateFlags & FOCUSED) != 0)) {
6996 /* Give up focus if we are no longer focusable */
6997 clearFocus();
6998 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6999 && ((privateFlags & FOCUSED) == 0)) {
7000 /*
7001 * Tell the view system that we are now available to take focus
7002 * if no one else already has it.
7003 */
7004 if (mParent != null) mParent.focusableViewAvailable(this);
7005 }
7006 }
7007
7008 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7009 if ((changed & VISIBILITY_MASK) != 0) {
7010 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007011 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007012 * it was not visible. Marking it drawn ensures that the invalidation will
7013 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007015 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007016 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017
7018 needGlobalAttributesUpdate(true);
7019
7020 // a view becoming visible is worth notifying the parent
7021 // about in case nothing has focus. even if this specific view
7022 // isn't focusable, it may contain something that is, so let
7023 // the root view try to give this focus if nothing else does.
7024 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7025 mParent.focusableViewAvailable(this);
7026 }
7027 }
7028 }
7029
7030 /* Check if the GONE bit has changed */
7031 if ((changed & GONE) != 0) {
7032 needGlobalAttributesUpdate(false);
7033 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034
Romain Guyecd80ee2009-12-03 17:13:02 -08007035 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7036 if (hasFocus()) clearFocus();
7037 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007038 if (mParent instanceof View) {
7039 // GONE views noop invalidation, so invalidate the parent
7040 ((View) mParent).invalidate(true);
7041 }
7042 // Mark the view drawn to ensure that it gets invalidated properly the next
7043 // time it is visible and gets invalidated
7044 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 }
7046 if (mAttachInfo != null) {
7047 mAttachInfo.mViewVisibilityChanged = true;
7048 }
7049 }
7050
7051 /* Check if the VISIBLE bit has changed */
7052 if ((changed & INVISIBLE) != 0) {
7053 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007054 /*
7055 * If this view is becoming invisible, set the DRAWN flag so that
7056 * the next invalidate() will not be skipped.
7057 */
7058 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059
7060 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
7061 // root view becoming invisible shouldn't clear focus
7062 if (getRootView() != this) {
7063 clearFocus();
7064 }
7065 }
7066 if (mAttachInfo != null) {
7067 mAttachInfo.mViewVisibilityChanged = true;
7068 }
7069 }
7070
Adam Powell326d8082009-12-09 15:10:07 -08007071 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007072 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007073 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7074 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007075 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007076 } else if (mParent != null) {
7077 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007078 }
Adam Powell326d8082009-12-09 15:10:07 -08007079 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7080 }
7081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7083 destroyDrawingCache();
7084 }
7085
7086 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7087 destroyDrawingCache();
7088 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007089 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 }
7091
7092 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7093 destroyDrawingCache();
7094 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7095 }
7096
7097 if ((changed & DRAW_MASK) != 0) {
7098 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
7099 if (mBGDrawable != null) {
7100 mPrivateFlags &= ~SKIP_DRAW;
7101 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7102 } else {
7103 mPrivateFlags |= SKIP_DRAW;
7104 }
7105 } else {
7106 mPrivateFlags &= ~SKIP_DRAW;
7107 }
7108 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007109 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 }
7111
7112 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007113 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 mParent.recomputeViewAttributes(this);
7115 }
7116 }
7117 }
7118
7119 /**
7120 * Change the view's z order in the tree, so it's on top of other sibling
7121 * views
7122 */
7123 public void bringToFront() {
7124 if (mParent != null) {
7125 mParent.bringChildToFront(this);
7126 }
7127 }
7128
7129 /**
7130 * This is called in response to an internal scroll in this view (i.e., the
7131 * view scrolled its own contents). This is typically as a result of
7132 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7133 * called.
7134 *
7135 * @param l Current horizontal scroll origin.
7136 * @param t Current vertical scroll origin.
7137 * @param oldl Previous horizontal scroll origin.
7138 * @param oldt Previous vertical scroll origin.
7139 */
7140 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007141 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7142 postSendViewScrolledAccessibilityEventCallback();
7143 }
7144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 mBackgroundSizeChanged = true;
7146
7147 final AttachInfo ai = mAttachInfo;
7148 if (ai != null) {
7149 ai.mViewScrollChanged = true;
7150 }
7151 }
7152
7153 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007154 * Interface definition for a callback to be invoked when the layout bounds of a view
7155 * changes due to layout processing.
7156 */
7157 public interface OnLayoutChangeListener {
7158 /**
7159 * Called when the focus state of a view has changed.
7160 *
7161 * @param v The view whose state has changed.
7162 * @param left The new value of the view's left property.
7163 * @param top The new value of the view's top property.
7164 * @param right The new value of the view's right property.
7165 * @param bottom The new value of the view's bottom property.
7166 * @param oldLeft The previous value of the view's left property.
7167 * @param oldTop The previous value of the view's top property.
7168 * @param oldRight The previous value of the view's right property.
7169 * @param oldBottom The previous value of the view's bottom property.
7170 */
7171 void onLayoutChange(View v, int left, int top, int right, int bottom,
7172 int oldLeft, int oldTop, int oldRight, int oldBottom);
7173 }
7174
7175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 * This is called during layout when the size of this view has changed. If
7177 * you were just added to the view hierarchy, you're called with the old
7178 * values of 0.
7179 *
7180 * @param w Current width of this view.
7181 * @param h Current height of this view.
7182 * @param oldw Old width of this view.
7183 * @param oldh Old height of this view.
7184 */
7185 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7186 }
7187
7188 /**
7189 * Called by draw to draw the child views. This may be overridden
7190 * by derived classes to gain control just before its children are drawn
7191 * (but after its own view has been drawn).
7192 * @param canvas the canvas on which to draw the view
7193 */
7194 protected void dispatchDraw(Canvas canvas) {
7195 }
7196
7197 /**
7198 * Gets the parent of this view. Note that the parent is a
7199 * ViewParent and not necessarily a View.
7200 *
7201 * @return Parent of this view.
7202 */
7203 public final ViewParent getParent() {
7204 return mParent;
7205 }
7206
7207 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007208 * Set the horizontal scrolled position of your view. This will cause a call to
7209 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7210 * invalidated.
7211 * @param value the x position to scroll to
7212 */
7213 public void setScrollX(int value) {
7214 scrollTo(value, mScrollY);
7215 }
7216
7217 /**
7218 * Set the vertical scrolled position of your view. This will cause a call to
7219 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7220 * invalidated.
7221 * @param value the y position to scroll to
7222 */
7223 public void setScrollY(int value) {
7224 scrollTo(mScrollX, value);
7225 }
7226
7227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 * Return the scrolled left position of this view. This is the left edge of
7229 * the displayed part of your view. You do not need to draw any pixels
7230 * farther left, since those are outside of the frame of your view on
7231 * screen.
7232 *
7233 * @return The left edge of the displayed part of your view, in pixels.
7234 */
7235 public final int getScrollX() {
7236 return mScrollX;
7237 }
7238
7239 /**
7240 * Return the scrolled top position of this view. This is the top edge of
7241 * the displayed part of your view. You do not need to draw any pixels above
7242 * it, since those are outside of the frame of your view on screen.
7243 *
7244 * @return The top edge of the displayed part of your view, in pixels.
7245 */
7246 public final int getScrollY() {
7247 return mScrollY;
7248 }
7249
7250 /**
7251 * Return the width of the your view.
7252 *
7253 * @return The width of your view, in pixels.
7254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007255 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 public final int getWidth() {
7257 return mRight - mLeft;
7258 }
7259
7260 /**
7261 * Return the height of your view.
7262 *
7263 * @return The height of your view, in pixels.
7264 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007265 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007266 public final int getHeight() {
7267 return mBottom - mTop;
7268 }
7269
7270 /**
7271 * Return the visible drawing bounds of your view. Fills in the output
7272 * rectangle with the values from getScrollX(), getScrollY(),
7273 * getWidth(), and getHeight().
7274 *
7275 * @param outRect The (scrolled) drawing bounds of the view.
7276 */
7277 public void getDrawingRect(Rect outRect) {
7278 outRect.left = mScrollX;
7279 outRect.top = mScrollY;
7280 outRect.right = mScrollX + (mRight - mLeft);
7281 outRect.bottom = mScrollY + (mBottom - mTop);
7282 }
7283
7284 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007285 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7286 * raw width component (that is the result is masked by
7287 * {@link #MEASURED_SIZE_MASK}).
7288 *
7289 * @return The raw measured width of this view.
7290 */
7291 public final int getMeasuredWidth() {
7292 return mMeasuredWidth & MEASURED_SIZE_MASK;
7293 }
7294
7295 /**
7296 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007297 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007298 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 * This should be used during measurement and layout calculations only. Use
7300 * {@link #getWidth()} to see how wide a view is after layout.
7301 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007302 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007304 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 return mMeasuredWidth;
7306 }
7307
7308 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007309 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7310 * raw width component (that is the result is masked by
7311 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007313 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 */
7315 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007316 return mMeasuredHeight & MEASURED_SIZE_MASK;
7317 }
7318
7319 /**
7320 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007321 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007322 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7323 * This should be used during measurement and layout calculations only. Use
7324 * {@link #getHeight()} to see how wide a view is after layout.
7325 *
7326 * @return The measured width of this view as a bit mask.
7327 */
7328 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 return mMeasuredHeight;
7330 }
7331
7332 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007333 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7334 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7335 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7336 * and the height component is at the shifted bits
7337 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7338 */
7339 public final int getMeasuredState() {
7340 return (mMeasuredWidth&MEASURED_STATE_MASK)
7341 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7342 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7343 }
7344
7345 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007346 * The transform matrix of this view, which is calculated based on the current
7347 * roation, scale, and pivot properties.
7348 *
7349 * @see #getRotation()
7350 * @see #getScaleX()
7351 * @see #getScaleY()
7352 * @see #getPivotX()
7353 * @see #getPivotY()
7354 * @return The current transform matrix for the view
7355 */
7356 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007357 if (mTransformationInfo != null) {
7358 updateMatrix();
7359 return mTransformationInfo.mMatrix;
7360 }
7361 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007362 }
7363
7364 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007365 * Utility function to determine if the value is far enough away from zero to be
7366 * considered non-zero.
7367 * @param value A floating point value to check for zero-ness
7368 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7369 */
7370 private static boolean nonzero(float value) {
7371 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7372 }
7373
7374 /**
Jeff Brown86671742010-09-30 20:00:15 -07007375 * Returns true if the transform matrix is the identity matrix.
7376 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007377 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007378 * @return True if the transform matrix is the identity matrix, false otherwise.
7379 */
Jeff Brown86671742010-09-30 20:00:15 -07007380 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007381 if (mTransformationInfo != null) {
7382 updateMatrix();
7383 return mTransformationInfo.mMatrixIsIdentity;
7384 }
7385 return true;
7386 }
7387
7388 void ensureTransformationInfo() {
7389 if (mTransformationInfo == null) {
7390 mTransformationInfo = new TransformationInfo();
7391 }
Jeff Brown86671742010-09-30 20:00:15 -07007392 }
7393
7394 /**
7395 * Recomputes the transform matrix if necessary.
7396 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007397 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007398 final TransformationInfo info = mTransformationInfo;
7399 if (info == null) {
7400 return;
7401 }
7402 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 // transform-related properties have changed since the last time someone
7404 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007405
7406 // Figure out if we need to update the pivot point
7407 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007408 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7409 info.mPrevWidth = mRight - mLeft;
7410 info.mPrevHeight = mBottom - mTop;
7411 info.mPivotX = info.mPrevWidth / 2f;
7412 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007413 }
7414 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007415 info.mMatrix.reset();
7416 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7417 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7418 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7419 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007420 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007421 if (info.mCamera == null) {
7422 info.mCamera = new Camera();
7423 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007424 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007425 info.mCamera.save();
7426 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7427 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7428 info.mCamera.getMatrix(info.matrix3D);
7429 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7430 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7431 info.mPivotY + info.mTranslationY);
7432 info.mMatrix.postConcat(info.matrix3D);
7433 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007434 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007435 info.mMatrixDirty = false;
7436 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7437 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007438 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007439 }
7440
7441 /**
7442 * Utility method to retrieve the inverse of the current mMatrix property.
7443 * We cache the matrix to avoid recalculating it when transform properties
7444 * have not changed.
7445 *
7446 * @return The inverse of the current matrix of this view.
7447 */
Jeff Brown86671742010-09-30 20:00:15 -07007448 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007449 final TransformationInfo info = mTransformationInfo;
7450 if (info != null) {
7451 updateMatrix();
7452 if (info.mInverseMatrixDirty) {
7453 if (info.mInverseMatrix == null) {
7454 info.mInverseMatrix = new Matrix();
7455 }
7456 info.mMatrix.invert(info.mInverseMatrix);
7457 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007458 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007459 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007460 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007461 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007462 }
7463
7464 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007465 * Gets the distance along the Z axis from the camera to this view.
7466 *
7467 * @see #setCameraDistance(float)
7468 *
7469 * @return The distance along the Z axis.
7470 */
7471 public float getCameraDistance() {
7472 ensureTransformationInfo();
7473 final float dpi = mResources.getDisplayMetrics().densityDpi;
7474 final TransformationInfo info = mTransformationInfo;
7475 if (info.mCamera == null) {
7476 info.mCamera = new Camera();
7477 info.matrix3D = new Matrix();
7478 }
7479 return -(info.mCamera.getLocationZ() * dpi);
7480 }
7481
7482 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007483 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7484 * views are drawn) from the camera to this view. The camera's distance
7485 * affects 3D transformations, for instance rotations around the X and Y
7486 * axis. If the rotationX or rotationY properties are changed and this view is
7487 * large (more than half the size of the screen), it is recommended to always
7488 * use a camera distance that's greater than the height (X axis rotation) or
7489 * the width (Y axis rotation) of this view.</p>
7490 *
7491 * <p>The distance of the camera from the view plane can have an affect on the
7492 * perspective distortion of the view when it is rotated around the x or y axis.
7493 * For example, a large distance will result in a large viewing angle, and there
7494 * will not be much perspective distortion of the view as it rotates. A short
7495 * distance may cause much more perspective distortion upon rotation, and can
7496 * also result in some drawing artifacts if the rotated view ends up partially
7497 * behind the camera (which is why the recommendation is to use a distance at
7498 * least as far as the size of the view, if the view is to be rotated.)</p>
7499 *
7500 * <p>The distance is expressed in "depth pixels." The default distance depends
7501 * on the screen density. For instance, on a medium density display, the
7502 * default distance is 1280. On a high density display, the default distance
7503 * is 1920.</p>
7504 *
7505 * <p>If you want to specify a distance that leads to visually consistent
7506 * results across various densities, use the following formula:</p>
7507 * <pre>
7508 * float scale = context.getResources().getDisplayMetrics().density;
7509 * view.setCameraDistance(distance * scale);
7510 * </pre>
7511 *
7512 * <p>The density scale factor of a high density display is 1.5,
7513 * and 1920 = 1280 * 1.5.</p>
7514 *
7515 * @param distance The distance in "depth pixels", if negative the opposite
7516 * value is used
7517 *
7518 * @see #setRotationX(float)
7519 * @see #setRotationY(float)
7520 */
7521 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007522 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007523
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007524 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007525 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007526 final TransformationInfo info = mTransformationInfo;
7527 if (info.mCamera == null) {
7528 info.mCamera = new Camera();
7529 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007530 }
7531
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007532 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7533 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007534
Chet Haase9d1992d2012-03-13 11:03:25 -07007535 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007536 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07007537 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08007538 }
Romain Guya5364ee2011-02-24 14:46:04 -08007539 }
7540
7541 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007542 * The degrees that the view is rotated around the pivot point.
7543 *
Romain Guya5364ee2011-02-24 14:46:04 -08007544 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007545 * @see #getPivotX()
7546 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007547 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007548 * @return The degrees of rotation.
7549 */
Chet Haasea5531132012-02-02 13:41:44 -08007550 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007551 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007552 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007553 }
7554
7555 /**
Chet Haase897247b2010-09-09 14:54:47 -07007556 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7557 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007558 *
7559 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007560 *
7561 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007562 * @see #getPivotX()
7563 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007564 * @see #setRotationX(float)
7565 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007566 *
7567 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007568 */
7569 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007570 ensureTransformationInfo();
7571 final TransformationInfo info = mTransformationInfo;
7572 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007573 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007574 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007575 info.mRotation = rotation;
7576 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007577 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007578 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7579 mDisplayList.setRotation(rotation);
7580 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007581 }
7582 }
7583
7584 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007585 * The degrees that the view is rotated around the vertical axis through the pivot point.
7586 *
7587 * @see #getPivotX()
7588 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007589 * @see #setRotationY(float)
7590 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007591 * @return The degrees of Y rotation.
7592 */
Chet Haasea5531132012-02-02 13:41:44 -08007593 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007594 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007595 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007596 }
7597
7598 /**
Chet Haase897247b2010-09-09 14:54:47 -07007599 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7600 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7601 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007602 *
7603 * When rotating large views, it is recommended to adjust the camera distance
7604 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007605 *
7606 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007607 *
7608 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007609 * @see #getPivotX()
7610 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007611 * @see #setRotation(float)
7612 * @see #setRotationX(float)
7613 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007614 *
7615 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007616 */
7617 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007618 ensureTransformationInfo();
7619 final TransformationInfo info = mTransformationInfo;
7620 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007621 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007622 info.mRotationY = rotationY;
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.setRotationY(rotationY);
7627 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007628 }
7629 }
7630
7631 /**
7632 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7633 *
7634 * @see #getPivotX()
7635 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007636 * @see #setRotationX(float)
7637 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007638 * @return The degrees of X rotation.
7639 */
Chet Haasea5531132012-02-02 13:41:44 -08007640 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007641 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007642 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007643 }
7644
7645 /**
Chet Haase897247b2010-09-09 14:54:47 -07007646 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7647 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7648 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007649 *
7650 * When rotating large views, it is recommended to adjust the camera distance
7651 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007652 *
7653 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007654 *
7655 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007656 * @see #getPivotX()
7657 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007658 * @see #setRotation(float)
7659 * @see #setRotationY(float)
7660 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007661 *
7662 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007663 */
7664 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007665 ensureTransformationInfo();
7666 final TransformationInfo info = mTransformationInfo;
7667 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007668 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007669 info.mRotationX = rotationX;
7670 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007671 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007672 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7673 mDisplayList.setRotationX(rotationX);
7674 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007675 }
7676 }
7677
7678 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007679 * The amount that the view is scaled in x around the pivot point, as a proportion of
7680 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7681 *
Joe Onorato93162322010-09-16 15:42:01 -04007682 * <p>By default, this is 1.0f.
7683 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007684 * @see #getPivotX()
7685 * @see #getPivotY()
7686 * @return The scaling factor.
7687 */
Chet Haasea5531132012-02-02 13:41:44 -08007688 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007689 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007690 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007691 }
7692
7693 /**
7694 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7695 * the view's unscaled width. A value of 1 means that no scaling is applied.
7696 *
7697 * @param scaleX The scaling factor.
7698 * @see #getPivotX()
7699 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007700 *
7701 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007702 */
7703 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007704 ensureTransformationInfo();
7705 final TransformationInfo info = mTransformationInfo;
7706 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007707 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007708 info.mScaleX = scaleX;
7709 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007710 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007711 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7712 mDisplayList.setScaleX(scaleX);
7713 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007714 }
7715 }
7716
7717 /**
7718 * The amount that the view is scaled in y around the pivot point, as a proportion of
7719 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7720 *
Joe Onorato93162322010-09-16 15:42:01 -04007721 * <p>By default, this is 1.0f.
7722 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007723 * @see #getPivotX()
7724 * @see #getPivotY()
7725 * @return The scaling factor.
7726 */
Chet Haasea5531132012-02-02 13:41:44 -08007727 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007728 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007729 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007730 }
7731
7732 /**
7733 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7734 * the view's unscaled width. A value of 1 means that no scaling is applied.
7735 *
7736 * @param scaleY The scaling factor.
7737 * @see #getPivotX()
7738 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007739 *
7740 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007741 */
7742 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007743 ensureTransformationInfo();
7744 final TransformationInfo info = mTransformationInfo;
7745 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007746 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007747 info.mScaleY = scaleY;
7748 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007749 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007750 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7751 mDisplayList.setScaleY(scaleY);
7752 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007753 }
7754 }
7755
7756 /**
7757 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7758 * and {@link #setScaleX(float) scaled}.
7759 *
7760 * @see #getRotation()
7761 * @see #getScaleX()
7762 * @see #getScaleY()
7763 * @see #getPivotY()
7764 * @return The x location of the pivot point.
7765 */
Chet Haasea5531132012-02-02 13:41:44 -08007766 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007767 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007768 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007769 }
7770
7771 /**
7772 * Sets the x location of the point around which the view is
7773 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007774 * By default, the pivot point is centered on the object.
7775 * Setting this property disables this behavior and causes the view to use only the
7776 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007777 *
7778 * @param pivotX The x location of the pivot point.
7779 * @see #getRotation()
7780 * @see #getScaleX()
7781 * @see #getScaleY()
7782 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007783 *
7784 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007785 */
7786 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007787 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007788 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007789 final TransformationInfo info = mTransformationInfo;
7790 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007791 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007792 info.mPivotX = pivotX;
7793 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007794 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007795 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7796 mDisplayList.setPivotX(pivotX);
7797 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007798 }
7799 }
7800
7801 /**
7802 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7803 * and {@link #setScaleY(float) scaled}.
7804 *
7805 * @see #getRotation()
7806 * @see #getScaleX()
7807 * @see #getScaleY()
7808 * @see #getPivotY()
7809 * @return The y location of the pivot point.
7810 */
Chet Haasea5531132012-02-02 13:41:44 -08007811 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007812 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007813 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007814 }
7815
7816 /**
7817 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007818 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7819 * Setting this property disables this behavior and causes the view to use only the
7820 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007821 *
7822 * @param pivotY The y location of the pivot point.
7823 * @see #getRotation()
7824 * @see #getScaleX()
7825 * @see #getScaleY()
7826 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007827 *
7828 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007829 */
7830 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007831 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007832 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007833 final TransformationInfo info = mTransformationInfo;
7834 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007835 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007836 info.mPivotY = pivotY;
7837 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007838 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007839 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7840 mDisplayList.setPivotY(pivotY);
7841 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007842 }
7843 }
7844
7845 /**
7846 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7847 * completely transparent and 1 means the view is completely opaque.
7848 *
Joe Onorato93162322010-09-16 15:42:01 -04007849 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007850 * @return The opacity of the view.
7851 */
Chet Haasea5531132012-02-02 13:41:44 -08007852 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007853 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007854 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007855 }
7856
7857 /**
Romain Guy171c5922011-01-06 10:04:23 -08007858 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7859 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007860 *
Romain Guy171c5922011-01-06 10:04:23 -08007861 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7862 * responsible for applying the opacity itself. Otherwise, calling this method is
7863 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007864 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007865 *
Chet Haasea5531132012-02-02 13:41:44 -08007866 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7867 * performance implications. It is generally best to use the alpha property sparingly and
7868 * transiently, as in the case of fading animations.</p>
7869 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007870 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007871 *
Joe Malin32736f02011-01-19 16:14:20 -08007872 * @see #setLayerType(int, android.graphics.Paint)
7873 *
Chet Haase73066682010-11-29 15:55:32 -08007874 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007875 */
7876 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007877 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007878 if (mTransformationInfo.mAlpha != alpha) {
7879 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007880 if (onSetAlpha((int) (alpha * 255))) {
7881 mPrivateFlags |= ALPHA_SET;
7882 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07007883 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007884 invalidate(true);
7885 } else {
7886 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07007887 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007888 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7889 mDisplayList.setAlpha(alpha);
7890 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007891 }
Chet Haaseed032702010-10-01 14:05:54 -07007892 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007893 }
7894
7895 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007896 * Faster version of setAlpha() which performs the same steps except there are
7897 * no calls to invalidate(). The caller of this function should perform proper invalidation
7898 * on the parent and this object. The return value indicates whether the subclass handles
7899 * alpha (the return value for onSetAlpha()).
7900 *
7901 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007902 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7903 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007904 */
7905 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007906 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007907 if (mTransformationInfo.mAlpha != alpha) {
7908 mTransformationInfo.mAlpha = alpha;
7909 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7910 if (subclassHandlesAlpha) {
7911 mPrivateFlags |= ALPHA_SET;
7912 return true;
7913 } else {
7914 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08007915 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7916 mDisplayList.setAlpha(alpha);
7917 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007918 }
Chet Haasea00f3862011-02-22 06:34:40 -08007919 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007920 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007921 }
7922
7923 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007924 * Top position of this view relative to its parent.
7925 *
7926 * @return The top of this view, in pixels.
7927 */
7928 @ViewDebug.CapturedViewProperty
7929 public final int getTop() {
7930 return mTop;
7931 }
7932
7933 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007934 * Sets the top position of this view relative to its parent. This method is meant to be called
7935 * by the layout system and should not generally be called otherwise, because the property
7936 * may be changed at any time by the layout.
7937 *
7938 * @param top The top of this view, in pixels.
7939 */
7940 public final void setTop(int top) {
7941 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007942 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007943 final boolean matrixIsIdentity = mTransformationInfo == null
7944 || mTransformationInfo.mMatrixIsIdentity;
7945 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007946 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007947 int minTop;
7948 int yLoc;
7949 if (top < mTop) {
7950 minTop = top;
7951 yLoc = top - mTop;
7952 } else {
7953 minTop = mTop;
7954 yLoc = 0;
7955 }
Chet Haasee9140a72011-02-16 16:23:29 -08007956 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007957 }
7958 } else {
7959 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007960 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007961 }
7962
Chet Haaseed032702010-10-01 14:05:54 -07007963 int width = mRight - mLeft;
7964 int oldHeight = mBottom - mTop;
7965
Chet Haase21cd1382010-09-01 17:42:29 -07007966 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08007967 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7968 mDisplayList.setTop(mTop);
7969 }
Chet Haase21cd1382010-09-01 17:42:29 -07007970
Chet Haaseed032702010-10-01 14:05:54 -07007971 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7972
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007973 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007974 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7975 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007976 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007977 }
Chet Haase21cd1382010-09-01 17:42:29 -07007978 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007979 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007980 }
Chet Haase55dbb652010-12-21 20:15:08 -08007981 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007982 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007983 }
7984 }
7985
7986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007987 * Bottom position of this view relative to its parent.
7988 *
7989 * @return The bottom of this view, in pixels.
7990 */
7991 @ViewDebug.CapturedViewProperty
7992 public final int getBottom() {
7993 return mBottom;
7994 }
7995
7996 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007997 * True if this view has changed since the last time being drawn.
7998 *
7999 * @return The dirty state of this view.
8000 */
8001 public boolean isDirty() {
8002 return (mPrivateFlags & DIRTY_MASK) != 0;
8003 }
8004
8005 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008006 * Sets the bottom position of this view relative to its parent. This method is meant to be
8007 * called by the layout system and should not generally be called otherwise, because the
8008 * property may be changed at any time by the layout.
8009 *
8010 * @param bottom The bottom of this view, in pixels.
8011 */
8012 public final void setBottom(int bottom) {
8013 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008014 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008015 final boolean matrixIsIdentity = mTransformationInfo == null
8016 || mTransformationInfo.mMatrixIsIdentity;
8017 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008018 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008019 int maxBottom;
8020 if (bottom < mBottom) {
8021 maxBottom = mBottom;
8022 } else {
8023 maxBottom = bottom;
8024 }
Chet Haasee9140a72011-02-16 16:23:29 -08008025 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008026 }
8027 } else {
8028 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008029 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008030 }
8031
Chet Haaseed032702010-10-01 14:05:54 -07008032 int width = mRight - mLeft;
8033 int oldHeight = mBottom - mTop;
8034
Chet Haase21cd1382010-09-01 17:42:29 -07008035 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008036 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8037 mDisplayList.setBottom(mBottom);
8038 }
Chet Haase21cd1382010-09-01 17:42:29 -07008039
Chet Haaseed032702010-10-01 14:05:54 -07008040 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8041
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008042 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008043 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8044 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008045 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008046 }
Chet Haase21cd1382010-09-01 17:42:29 -07008047 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008048 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008049 }
Chet Haase55dbb652010-12-21 20:15:08 -08008050 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008051 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008052 }
8053 }
8054
8055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 * Left position of this view relative to its parent.
8057 *
8058 * @return The left edge of this view, in pixels.
8059 */
8060 @ViewDebug.CapturedViewProperty
8061 public final int getLeft() {
8062 return mLeft;
8063 }
8064
8065 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008066 * Sets the left position of this view relative to its parent. This method is meant to be called
8067 * by the layout system and should not generally be called otherwise, because the property
8068 * may be changed at any time by the layout.
8069 *
8070 * @param left The bottom of this view, in pixels.
8071 */
8072 public final void setLeft(int left) {
8073 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008074 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008075 final boolean matrixIsIdentity = mTransformationInfo == null
8076 || mTransformationInfo.mMatrixIsIdentity;
8077 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008078 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008079 int minLeft;
8080 int xLoc;
8081 if (left < mLeft) {
8082 minLeft = left;
8083 xLoc = left - mLeft;
8084 } else {
8085 minLeft = mLeft;
8086 xLoc = 0;
8087 }
Chet Haasee9140a72011-02-16 16:23:29 -08008088 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008089 }
8090 } else {
8091 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008092 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008093 }
8094
Chet Haaseed032702010-10-01 14:05:54 -07008095 int oldWidth = mRight - mLeft;
8096 int height = mBottom - mTop;
8097
Chet Haase21cd1382010-09-01 17:42:29 -07008098 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008099 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8100 mDisplayList.setLeft(left);
8101 }
Chet Haase21cd1382010-09-01 17:42:29 -07008102
Chet Haaseed032702010-10-01 14:05:54 -07008103 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008105 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008106 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8107 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008108 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008109 }
Chet Haase21cd1382010-09-01 17:42:29 -07008110 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008111 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008112 }
Chet Haase55dbb652010-12-21 20:15:08 -08008113 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008114 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008115 if (USE_DISPLAY_LIST_PROPERTIES) {
8116
8117 }
Chet Haase21cd1382010-09-01 17:42:29 -07008118 }
8119 }
8120
8121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 * Right position of this view relative to its parent.
8123 *
8124 * @return The right edge of this view, in pixels.
8125 */
8126 @ViewDebug.CapturedViewProperty
8127 public final int getRight() {
8128 return mRight;
8129 }
8130
8131 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008132 * Sets the right position of this view relative to its parent. This method is meant to be called
8133 * by the layout system and should not generally be called otherwise, because the property
8134 * may be changed at any time by the layout.
8135 *
8136 * @param right The bottom of this view, in pixels.
8137 */
8138 public final void setRight(int right) {
8139 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008140 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008141 final boolean matrixIsIdentity = mTransformationInfo == null
8142 || mTransformationInfo.mMatrixIsIdentity;
8143 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008144 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008145 int maxRight;
8146 if (right < mRight) {
8147 maxRight = mRight;
8148 } else {
8149 maxRight = right;
8150 }
Chet Haasee9140a72011-02-16 16:23:29 -08008151 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008152 }
8153 } else {
8154 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008155 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008156 }
8157
Chet Haaseed032702010-10-01 14:05:54 -07008158 int oldWidth = mRight - mLeft;
8159 int height = mBottom - mTop;
8160
Chet Haase21cd1382010-09-01 17:42:29 -07008161 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008162 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8163 mDisplayList.setRight(mRight);
8164 }
Chet Haase21cd1382010-09-01 17:42:29 -07008165
Chet Haaseed032702010-10-01 14:05:54 -07008166 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8167
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008168 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008169 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8170 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008171 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008172 }
Chet Haase21cd1382010-09-01 17:42:29 -07008173 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008174 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008175 }
Chet Haase55dbb652010-12-21 20:15:08 -08008176 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008177 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008178 }
8179 }
8180
8181 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008182 * The visual x position of this view, in pixels. This is equivalent to the
8183 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008184 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008185 *
Chet Haasedf030d22010-07-30 17:22:38 -07008186 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 */
Chet Haasea5531132012-02-02 13:41:44 -08008188 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008189 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008190 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008191 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008192
Chet Haasedf030d22010-07-30 17:22:38 -07008193 /**
8194 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8195 * {@link #setTranslationX(float) translationX} property to be the difference between
8196 * the x value passed in and the current {@link #getLeft() left} property.
8197 *
8198 * @param x The visual x position of this view, in pixels.
8199 */
8200 public void setX(float x) {
8201 setTranslationX(x - mLeft);
8202 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008203
Chet Haasedf030d22010-07-30 17:22:38 -07008204 /**
8205 * The visual y position of this view, in pixels. This is equivalent to the
8206 * {@link #setTranslationY(float) translationY} property plus the current
8207 * {@link #getTop() top} property.
8208 *
8209 * @return The visual y position of this view, in pixels.
8210 */
Chet Haasea5531132012-02-02 13:41:44 -08008211 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008212 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008213 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008214 }
8215
8216 /**
8217 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8218 * {@link #setTranslationY(float) translationY} property to be the difference between
8219 * the y value passed in and the current {@link #getTop() top} property.
8220 *
8221 * @param y The visual y position of this view, in pixels.
8222 */
8223 public void setY(float y) {
8224 setTranslationY(y - mTop);
8225 }
8226
8227
8228 /**
8229 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8230 * This position is post-layout, in addition to wherever the object's
8231 * layout placed it.
8232 *
8233 * @return The horizontal position of this view relative to its left position, in pixels.
8234 */
Chet Haasea5531132012-02-02 13:41:44 -08008235 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008236 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008237 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008238 }
8239
8240 /**
8241 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8242 * This effectively positions the object post-layout, in addition to wherever the object's
8243 * layout placed it.
8244 *
8245 * @param translationX The horizontal position of this view relative to its left position,
8246 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008247 *
8248 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008249 */
8250 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008251 ensureTransformationInfo();
8252 final TransformationInfo info = mTransformationInfo;
8253 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008254 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008255 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008256 info.mTranslationX = translationX;
8257 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008258 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008259 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8260 mDisplayList.setTranslationX(translationX);
8261 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008262 }
8263 }
8264
8265 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008266 * The horizontal location of this view relative to its {@link #getTop() top} position.
8267 * This position is post-layout, in addition to wherever the object's
8268 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008269 *
Chet Haasedf030d22010-07-30 17:22:38 -07008270 * @return The vertical position of this view relative to its top position,
8271 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008272 */
Chet Haasea5531132012-02-02 13:41:44 -08008273 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008274 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008275 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008276 }
8277
8278 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008279 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8280 * This effectively positions the object post-layout, in addition to wherever the object's
8281 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008282 *
Chet Haasedf030d22010-07-30 17:22:38 -07008283 * @param translationY The vertical position of this view relative to its top position,
8284 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008285 *
8286 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008287 */
Chet Haasedf030d22010-07-30 17:22:38 -07008288 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008289 ensureTransformationInfo();
8290 final TransformationInfo info = mTransformationInfo;
8291 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008292 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008293 info.mTranslationY = translationY;
8294 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008295 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008296 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8297 mDisplayList.setTranslationY(translationY);
8298 }
Chet Haasedf030d22010-07-30 17:22:38 -07008299 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008300 }
8301
8302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008303 * Hit rectangle in parent's coordinates
8304 *
8305 * @param outRect The hit rectangle of the view.
8306 */
8307 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008308 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008309 final TransformationInfo info = mTransformationInfo;
8310 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008311 outRect.set(mLeft, mTop, mRight, mBottom);
8312 } else {
8313 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008314 tmpRect.set(-info.mPivotX, -info.mPivotY,
8315 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8316 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008317 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8318 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008319 }
8320 }
8321
8322 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008323 * Determines whether the given point, in local coordinates is inside the view.
8324 */
8325 /*package*/ final boolean pointInView(float localX, float localY) {
8326 return localX >= 0 && localX < (mRight - mLeft)
8327 && localY >= 0 && localY < (mBottom - mTop);
8328 }
8329
8330 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008331 * Utility method to determine whether the given point, in local coordinates,
8332 * is inside the view, where the area of the view is expanded by the slop factor.
8333 * This method is called while processing touch-move events to determine if the event
8334 * is still within the view.
8335 */
8336 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008337 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008338 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 }
8340
8341 /**
8342 * When a view has focus and the user navigates away from it, the next view is searched for
8343 * starting from the rectangle filled in by this method.
8344 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008345 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8346 * of the view. However, if your view maintains some idea of internal selection,
8347 * such as a cursor, or a selected row or column, you should override this method and
8348 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 *
8350 * @param r The rectangle to fill in, in this view's coordinates.
8351 */
8352 public void getFocusedRect(Rect r) {
8353 getDrawingRect(r);
8354 }
8355
8356 /**
8357 * If some part of this view is not clipped by any of its parents, then
8358 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008359 * coordinates (without taking possible View rotations into account), offset
8360 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8361 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 *
8363 * @param r If true is returned, r holds the global coordinates of the
8364 * visible portion of this view.
8365 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8366 * between this view and its root. globalOffet may be null.
8367 * @return true if r is non-empty (i.e. part of the view is visible at the
8368 * root level.
8369 */
8370 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8371 int width = mRight - mLeft;
8372 int height = mBottom - mTop;
8373 if (width > 0 && height > 0) {
8374 r.set(0, 0, width, height);
8375 if (globalOffset != null) {
8376 globalOffset.set(-mScrollX, -mScrollY);
8377 }
8378 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8379 }
8380 return false;
8381 }
8382
8383 public final boolean getGlobalVisibleRect(Rect r) {
8384 return getGlobalVisibleRect(r, null);
8385 }
8386
8387 public final boolean getLocalVisibleRect(Rect r) {
8388 Point offset = new Point();
8389 if (getGlobalVisibleRect(r, offset)) {
8390 r.offset(-offset.x, -offset.y); // make r local
8391 return true;
8392 }
8393 return false;
8394 }
8395
8396 /**
8397 * Offset this view's vertical location by the specified number of pixels.
8398 *
8399 * @param offset the number of pixels to offset the view by
8400 */
8401 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008402 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008403 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008404 final boolean matrixIsIdentity = mTransformationInfo == null
8405 || mTransformationInfo.mMatrixIsIdentity;
8406 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008407 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8408 invalidateViewProperty(false, false);
8409 } else {
8410 final ViewParent p = mParent;
8411 if (p != null && mAttachInfo != null) {
8412 final Rect r = mAttachInfo.mTmpInvalRect;
8413 int minTop;
8414 int maxBottom;
8415 int yLoc;
8416 if (offset < 0) {
8417 minTop = mTop + offset;
8418 maxBottom = mBottom;
8419 yLoc = offset;
8420 } else {
8421 minTop = mTop;
8422 maxBottom = mBottom + offset;
8423 yLoc = 0;
8424 }
8425 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8426 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008427 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008428 }
8429 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008430 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008431 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008432
Chet Haasec3aa3612010-06-17 08:50:37 -07008433 mTop += offset;
8434 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008435 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8436 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008437 invalidateViewProperty(false, false);
8438 } else {
8439 if (!matrixIsIdentity) {
8440 invalidateViewProperty(false, true);
8441 }
8442 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008443 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 }
8446
8447 /**
8448 * Offset this view's horizontal location by the specified amount of pixels.
8449 *
8450 * @param offset the numer of pixels to offset the view by
8451 */
8452 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008453 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008454 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008455 final boolean matrixIsIdentity = mTransformationInfo == null
8456 || mTransformationInfo.mMatrixIsIdentity;
8457 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008458 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8459 invalidateViewProperty(false, false);
8460 } else {
8461 final ViewParent p = mParent;
8462 if (p != null && mAttachInfo != null) {
8463 final Rect r = mAttachInfo.mTmpInvalRect;
8464 int minLeft;
8465 int maxRight;
8466 if (offset < 0) {
8467 minLeft = mLeft + offset;
8468 maxRight = mRight;
8469 } else {
8470 minLeft = mLeft;
8471 maxRight = mRight + offset;
8472 }
8473 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8474 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008475 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008476 }
8477 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008478 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008479 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008480
Chet Haasec3aa3612010-06-17 08:50:37 -07008481 mLeft += offset;
8482 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008483 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8484 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008485 invalidateViewProperty(false, false);
8486 } else {
8487 if (!matrixIsIdentity) {
8488 invalidateViewProperty(false, true);
8489 }
8490 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008491 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 }
8494
8495 /**
8496 * Get the LayoutParams associated with this view. All views should have
8497 * layout parameters. These supply parameters to the <i>parent</i> of this
8498 * view specifying how it should be arranged. There are many subclasses of
8499 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8500 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008501 *
8502 * This method may return null if this View is not attached to a parent
8503 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8504 * was not invoked successfully. When a View is attached to a parent
8505 * ViewGroup, this method must not return null.
8506 *
8507 * @return The LayoutParams associated with this view, or null if no
8508 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008510 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 public ViewGroup.LayoutParams getLayoutParams() {
8512 return mLayoutParams;
8513 }
8514
8515 /**
8516 * Set the layout parameters associated with this view. These supply
8517 * parameters to the <i>parent</i> of this view specifying how it should be
8518 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8519 * correspond to the different subclasses of ViewGroup that are responsible
8520 * for arranging their children.
8521 *
Romain Guy01c174b2011-02-22 11:51:06 -08008522 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 */
8524 public void setLayoutParams(ViewGroup.LayoutParams params) {
8525 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008526 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008527 }
8528 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008529 if (mParent instanceof ViewGroup) {
8530 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008532 requestLayout();
8533 }
8534
8535 /**
8536 * Set the scrolled position of your view. This will cause a call to
8537 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8538 * invalidated.
8539 * @param x the x position to scroll to
8540 * @param y the y position to scroll to
8541 */
8542 public void scrollTo(int x, int y) {
8543 if (mScrollX != x || mScrollY != y) {
8544 int oldX = mScrollX;
8545 int oldY = mScrollY;
8546 mScrollX = x;
8547 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008548 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008550 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008551 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 }
8554 }
8555
8556 /**
8557 * Move the scrolled position of your view. This will cause a call to
8558 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8559 * invalidated.
8560 * @param x the amount of pixels to scroll by horizontally
8561 * @param y the amount of pixels to scroll by vertically
8562 */
8563 public void scrollBy(int x, int y) {
8564 scrollTo(mScrollX + x, mScrollY + y);
8565 }
8566
8567 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008568 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8569 * animation to fade the scrollbars out after a default delay. If a subclass
8570 * provides animated scrolling, the start delay should equal the duration
8571 * of the scrolling animation.</p>
8572 *
8573 * <p>The animation starts only if at least one of the scrollbars is
8574 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8575 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8576 * this method returns true, and false otherwise. If the animation is
8577 * started, this method calls {@link #invalidate()}; in that case the
8578 * caller should not call {@link #invalidate()}.</p>
8579 *
8580 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008581 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008582 *
8583 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8584 * and {@link #scrollTo(int, int)}.</p>
8585 *
8586 * @return true if the animation is played, false otherwise
8587 *
8588 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008589 * @see #scrollBy(int, int)
8590 * @see #scrollTo(int, int)
8591 * @see #isHorizontalScrollBarEnabled()
8592 * @see #isVerticalScrollBarEnabled()
8593 * @see #setHorizontalScrollBarEnabled(boolean)
8594 * @see #setVerticalScrollBarEnabled(boolean)
8595 */
8596 protected boolean awakenScrollBars() {
8597 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008598 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008599 }
8600
8601 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008602 * Trigger the scrollbars to draw.
8603 * This method differs from awakenScrollBars() only in its default duration.
8604 * initialAwakenScrollBars() will show the scroll bars for longer than
8605 * usual to give the user more of a chance to notice them.
8606 *
8607 * @return true if the animation is played, false otherwise.
8608 */
8609 private boolean initialAwakenScrollBars() {
8610 return mScrollCache != null &&
8611 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8612 }
8613
8614 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008615 * <p>
8616 * Trigger the scrollbars to draw. When invoked this method starts an
8617 * animation to fade the scrollbars out after a fixed delay. If a subclass
8618 * provides animated scrolling, the start delay should equal the duration of
8619 * the scrolling animation.
8620 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008621 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008622 * <p>
8623 * The animation starts only if at least one of the scrollbars is enabled,
8624 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8625 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8626 * this method returns true, and false otherwise. If the animation is
8627 * started, this method calls {@link #invalidate()}; in that case the caller
8628 * should not call {@link #invalidate()}.
8629 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008630 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008631 * <p>
8632 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008633 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008634 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008635 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008636 * @param startDelay the delay, in milliseconds, after which the animation
8637 * should start; when the delay is 0, the animation starts
8638 * immediately
8639 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008640 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008641 * @see #scrollBy(int, int)
8642 * @see #scrollTo(int, int)
8643 * @see #isHorizontalScrollBarEnabled()
8644 * @see #isVerticalScrollBarEnabled()
8645 * @see #setHorizontalScrollBarEnabled(boolean)
8646 * @see #setVerticalScrollBarEnabled(boolean)
8647 */
8648 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008649 return awakenScrollBars(startDelay, true);
8650 }
Joe Malin32736f02011-01-19 16:14:20 -08008651
Mike Cleron290947b2009-09-29 18:34:32 -07008652 /**
8653 * <p>
8654 * Trigger the scrollbars to draw. When invoked this method starts an
8655 * animation to fade the scrollbars out after a fixed delay. If a subclass
8656 * provides animated scrolling, the start delay should equal the duration of
8657 * the scrolling animation.
8658 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008659 *
Mike Cleron290947b2009-09-29 18:34:32 -07008660 * <p>
8661 * The animation starts only if at least one of the scrollbars is enabled,
8662 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8663 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8664 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008665 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008666 * is set to true; in that case the caller
8667 * should not call {@link #invalidate()}.
8668 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008669 *
Mike Cleron290947b2009-09-29 18:34:32 -07008670 * <p>
8671 * This method should be invoked everytime a subclass directly updates the
8672 * scroll parameters.
8673 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008674 *
Mike Cleron290947b2009-09-29 18:34:32 -07008675 * @param startDelay the delay, in milliseconds, after which the animation
8676 * should start; when the delay is 0, the animation starts
8677 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008678 *
Mike Cleron290947b2009-09-29 18:34:32 -07008679 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008680 *
Mike Cleron290947b2009-09-29 18:34:32 -07008681 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008682 *
Mike Cleron290947b2009-09-29 18:34:32 -07008683 * @see #scrollBy(int, int)
8684 * @see #scrollTo(int, int)
8685 * @see #isHorizontalScrollBarEnabled()
8686 * @see #isVerticalScrollBarEnabled()
8687 * @see #setHorizontalScrollBarEnabled(boolean)
8688 * @see #setVerticalScrollBarEnabled(boolean)
8689 */
8690 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008691 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008692
Mike Cleronf116bf82009-09-27 19:14:12 -07008693 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8694 return false;
8695 }
8696
8697 if (scrollCache.scrollBar == null) {
8698 scrollCache.scrollBar = new ScrollBarDrawable();
8699 }
8700
8701 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8702
Mike Cleron290947b2009-09-29 18:34:32 -07008703 if (invalidate) {
8704 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008705 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008706 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008707
8708 if (scrollCache.state == ScrollabilityCache.OFF) {
8709 // FIXME: this is copied from WindowManagerService.
8710 // We should get this value from the system when it
8711 // is possible to do so.
8712 final int KEY_REPEAT_FIRST_DELAY = 750;
8713 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8714 }
8715
8716 // Tell mScrollCache when we should start fading. This may
8717 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008718 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008719 scrollCache.fadeStartTime = fadeStartTime;
8720 scrollCache.state = ScrollabilityCache.ON;
8721
8722 // Schedule our fader to run, unscheduling any old ones first
8723 if (mAttachInfo != null) {
8724 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8725 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8726 }
8727
8728 return true;
8729 }
8730
8731 return false;
8732 }
8733
8734 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008735 * Do not invalidate views which are not visible and which are not running an animation. They
8736 * will not get drawn and they should not set dirty flags as if they will be drawn
8737 */
8738 private boolean skipInvalidate() {
8739 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8740 (!(mParent instanceof ViewGroup) ||
8741 !((ViewGroup) mParent).isViewTransitioning(this));
8742 }
8743 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008744 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008745 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8746 * in the future. This must be called from a UI thread. To call from a non-UI
8747 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 *
8749 * WARNING: This method is destructive to dirty.
8750 * @param dirty the rectangle representing the bounds of the dirty region
8751 */
8752 public void invalidate(Rect dirty) {
8753 if (ViewDebug.TRACE_HIERARCHY) {
8754 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8755 }
8756
Chet Haaseaceafe62011-08-26 15:44:33 -07008757 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008758 return;
8759 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008760 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008761 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8762 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008764 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008765 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 final ViewParent p = mParent;
8767 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008768 //noinspection PointlessBooleanExpression,ConstantConditions
8769 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8770 if (p != null && ai != null && ai.mHardwareAccelerated) {
8771 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008772 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008773 p.invalidateChild(this, null);
8774 return;
8775 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 if (p != null && ai != null) {
8778 final int scrollX = mScrollX;
8779 final int scrollY = mScrollY;
8780 final Rect r = ai.mTmpInvalRect;
8781 r.set(dirty.left - scrollX, dirty.top - scrollY,
8782 dirty.right - scrollX, dirty.bottom - scrollY);
8783 mParent.invalidateChild(this, r);
8784 }
8785 }
8786 }
8787
8788 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008789 * 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 -08008790 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008791 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8792 * will be called at some point in the future. This must be called from
8793 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008794 * @param l the left position of the dirty region
8795 * @param t the top position of the dirty region
8796 * @param r the right position of the dirty region
8797 * @param b the bottom position of the dirty region
8798 */
8799 public void invalidate(int l, int t, int r, int b) {
8800 if (ViewDebug.TRACE_HIERARCHY) {
8801 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8802 }
8803
Chet Haaseaceafe62011-08-26 15:44:33 -07008804 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008805 return;
8806 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008807 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008808 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8809 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008811 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008812 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 final ViewParent p = mParent;
8814 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008815 //noinspection PointlessBooleanExpression,ConstantConditions
8816 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8817 if (p != null && ai != null && ai.mHardwareAccelerated) {
8818 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008819 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008820 p.invalidateChild(this, null);
8821 return;
8822 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008824 if (p != null && ai != null && l < r && t < b) {
8825 final int scrollX = mScrollX;
8826 final int scrollY = mScrollY;
8827 final Rect tmpr = ai.mTmpInvalRect;
8828 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8829 p.invalidateChild(this, tmpr);
8830 }
8831 }
8832 }
8833
8834 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008835 * Invalidate the whole view. If the view is visible,
8836 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8837 * the future. This must be called from a UI thread. To call from a non-UI thread,
8838 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008839 */
8840 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008841 invalidate(true);
8842 }
Joe Malin32736f02011-01-19 16:14:20 -08008843
Chet Haaseed032702010-10-01 14:05:54 -07008844 /**
8845 * This is where the invalidate() work actually happens. A full invalidate()
8846 * causes the drawing cache to be invalidated, but this function can be called with
8847 * invalidateCache set to false to skip that invalidation step for cases that do not
8848 * need it (for example, a component that remains at the same dimensions with the same
8849 * content).
8850 *
8851 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8852 * well. This is usually true for a full invalidate, but may be set to false if the
8853 * View's contents or dimensions have not changed.
8854 */
Romain Guy849d0a32011-02-01 17:20:48 -08008855 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 if (ViewDebug.TRACE_HIERARCHY) {
8857 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8858 }
8859
Chet Haaseaceafe62011-08-26 15:44:33 -07008860 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008861 return;
8862 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008863 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008864 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008865 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8866 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008867 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008868 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008869 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008870 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008871 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008874 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008875 //noinspection PointlessBooleanExpression,ConstantConditions
8876 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8877 if (p != null && ai != null && ai.mHardwareAccelerated) {
8878 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008879 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008880 p.invalidateChild(this, null);
8881 return;
8882 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008883 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 if (p != null && ai != null) {
8886 final Rect r = ai.mTmpInvalRect;
8887 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8888 // Don't call invalidate -- we don't want to internally scroll
8889 // our own bounds
8890 p.invalidateChild(this, r);
8891 }
8892 }
8893 }
8894
8895 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07008896 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
8897 * set any flags or handle all of the cases handled by the default invalidation methods.
8898 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
8899 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
8900 * walk up the hierarchy, transforming the dirty rect as necessary.
8901 *
8902 * The method also handles normal invalidation logic if display list properties are not
8903 * being used in this view. The invalidateParent and forceRedraw flags are used by that
8904 * backup approach, to handle these cases used in the various property-setting methods.
8905 *
8906 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
8907 * are not being used in this view
8908 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
8909 * list properties are not being used in this view
8910 */
8911 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
8912 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
8913 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
8914 if (invalidateParent) {
8915 invalidateParentCaches();
8916 }
8917 if (forceRedraw) {
8918 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
8919 }
8920 invalidate(false);
8921 } else {
8922 final AttachInfo ai = mAttachInfo;
8923 final ViewParent p = mParent;
8924 if (p != null && ai != null) {
8925 final Rect r = ai.mTmpInvalRect;
8926 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8927 if (mParent instanceof ViewGroup) {
8928 ((ViewGroup) mParent).invalidateChildFast(this, r);
8929 } else {
8930 mParent.invalidateChild(this, r);
8931 }
8932 }
8933 }
8934 }
8935
8936 /**
8937 * Utility method to transform a given Rect by the current matrix of this view.
8938 */
8939 void transformRect(final Rect rect) {
8940 if (!getMatrix().isIdentity()) {
8941 RectF boundingRect = mAttachInfo.mTmpTransformRect;
8942 boundingRect.set(rect);
8943 getMatrix().mapRect(boundingRect);
8944 rect.set((int) (boundingRect.left - 0.5f),
8945 (int) (boundingRect.top - 0.5f),
8946 (int) (boundingRect.right + 0.5f),
8947 (int) (boundingRect.bottom + 0.5f));
8948 }
8949 }
8950
8951 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008952 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008953 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8954 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008955 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8956 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008957 *
8958 * @hide
8959 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008960 protected void invalidateParentCaches() {
8961 if (mParent instanceof View) {
8962 ((View) mParent).mPrivateFlags |= INVALIDATED;
8963 }
8964 }
Joe Malin32736f02011-01-19 16:14:20 -08008965
Romain Guy0fd89bf2011-01-26 15:41:30 -08008966 /**
8967 * Used to indicate that the parent of this view should be invalidated. This functionality
8968 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8969 * which is necessary when various parent-managed properties of the view change, such as
8970 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8971 * an invalidation event to the parent.
8972 *
8973 * @hide
8974 */
8975 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008976 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008977 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008978 }
8979 }
8980
8981 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008982 * Indicates whether this View is opaque. An opaque View guarantees that it will
8983 * draw all the pixels overlapping its bounds using a fully opaque color.
8984 *
8985 * Subclasses of View should override this method whenever possible to indicate
8986 * whether an instance is opaque. Opaque Views are treated in a special way by
8987 * the View hierarchy, possibly allowing it to perform optimizations during
8988 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008989 *
Romain Guy24443ea2009-05-11 11:56:30 -07008990 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008991 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008992 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008993 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008994 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008995 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8996 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008997 }
8998
Adam Powell20232d02010-12-08 21:08:53 -08008999 /**
9000 * @hide
9001 */
9002 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009003 // Opaque if:
9004 // - Has a background
9005 // - Background is opaque
9006 // - Doesn't have scrollbars or scrollbars are inside overlay
9007
9008 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
9009 mPrivateFlags |= OPAQUE_BACKGROUND;
9010 } else {
9011 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9012 }
9013
9014 final int flags = mViewFlags;
9015 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9016 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9017 mPrivateFlags |= OPAQUE_SCROLLBARS;
9018 } else {
9019 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9020 }
9021 }
9022
9023 /**
9024 * @hide
9025 */
9026 protected boolean hasOpaqueScrollbars() {
9027 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009028 }
9029
9030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 * @return A handler associated with the thread running the View. This
9032 * handler can be used to pump events in the UI events queue.
9033 */
9034 public Handler getHandler() {
9035 if (mAttachInfo != null) {
9036 return mAttachInfo.mHandler;
9037 }
9038 return null;
9039 }
9040
9041 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009042 * Gets the view root associated with the View.
9043 * @return The view root, or null if none.
9044 * @hide
9045 */
9046 public ViewRootImpl getViewRootImpl() {
9047 if (mAttachInfo != null) {
9048 return mAttachInfo.mViewRootImpl;
9049 }
9050 return null;
9051 }
9052
9053 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009054 * <p>Causes the Runnable to be added to the message queue.
9055 * The runnable will be run on the user interface thread.</p>
9056 *
9057 * <p>This method can be invoked from outside of the UI thread
9058 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009059 *
9060 * @param action The Runnable that will be executed.
9061 *
9062 * @return Returns true if the Runnable was successfully placed in to the
9063 * message queue. Returns false on failure, usually because the
9064 * looper processing the message queue is exiting.
9065 */
9066 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009067 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009068 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009069 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009070 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009071 // Assume that post will succeed later
9072 ViewRootImpl.getRunQueue().post(action);
9073 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009074 }
9075
9076 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009077 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009079 * The runnable will be run on the user interface thread.</p>
9080 *
9081 * <p>This method can be invoked from outside of the UI thread
9082 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 *
9084 * @param action The Runnable that will be executed.
9085 * @param delayMillis The delay (in milliseconds) until the Runnable
9086 * will be executed.
9087 *
9088 * @return true if the Runnable was successfully placed in to the
9089 * message queue. Returns false on failure, usually because the
9090 * looper processing the message queue is exiting. Note that a
9091 * result of true does not mean the Runnable will be processed --
9092 * if the looper is quit before the delivery time of the message
9093 * occurs then the message will be dropped.
9094 */
9095 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009096 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009097 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009098 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009100 // Assume that post will succeed later
9101 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9102 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 }
9104
9105 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009106 * <p>Causes the Runnable to execute on the next animation time step.
9107 * The runnable will be run on the user interface thread.</p>
9108 *
9109 * <p>This method can be invoked from outside of the UI thread
9110 * only when this View is attached to a window.</p>
9111 *
9112 * @param action The Runnable that will be executed.
9113 *
9114 * @hide
9115 */
9116 public void postOnAnimation(Runnable action) {
9117 final AttachInfo attachInfo = mAttachInfo;
9118 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009119 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9120 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009121 } else {
9122 // Assume that post will succeed later
9123 ViewRootImpl.getRunQueue().post(action);
9124 }
9125 }
9126
9127 /**
9128 * <p>Causes the Runnable to execute on the next animation time step,
9129 * after the specified amount of time elapses.
9130 * The runnable will be run on the user interface thread.</p>
9131 *
9132 * <p>This method can be invoked from outside of the UI thread
9133 * only when this View is attached to a window.</p>
9134 *
9135 * @param action The Runnable that will be executed.
9136 * @param delayMillis The delay (in milliseconds) until the Runnable
9137 * will be executed.
9138 *
9139 * @hide
9140 */
9141 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9142 final AttachInfo attachInfo = mAttachInfo;
9143 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009144 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9145 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009146 } else {
9147 // Assume that post will succeed later
9148 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9149 }
9150 }
9151
9152 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009153 * <p>Removes the specified Runnable from the message queue.</p>
9154 *
9155 * <p>This method can be invoked from outside of the UI thread
9156 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 *
9158 * @param action The Runnable to remove from the message handling queue
9159 *
9160 * @return true if this view could ask the Handler to remove the Runnable,
9161 * false otherwise. When the returned value is true, the Runnable
9162 * may or may not have been actually removed from the message queue
9163 * (for instance, if the Runnable was not in the queue already.)
9164 */
9165 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009166 if (action != null) {
9167 final AttachInfo attachInfo = mAttachInfo;
9168 if (attachInfo != null) {
9169 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009170 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9171 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009172 } else {
9173 // Assume that post will succeed later
9174 ViewRootImpl.getRunQueue().removeCallbacks(action);
9175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009177 return true;
9178 }
9179
9180 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009181 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9182 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009183 *
Romain Guye63a4f32011-08-11 11:33:31 -07009184 * <p>This method can be invoked from outside of the UI thread
9185 * only when this View is attached to a window.</p>
9186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 * @see #invalidate()
9188 */
9189 public void postInvalidate() {
9190 postInvalidateDelayed(0);
9191 }
9192
9193 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009194 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9195 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
9196 *
9197 * <p>This method can be invoked from outside of the UI thread
9198 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199 *
9200 * @param left The left coordinate of the rectangle to invalidate.
9201 * @param top The top coordinate of the rectangle to invalidate.
9202 * @param right The right coordinate of the rectangle to invalidate.
9203 * @param bottom The bottom coordinate of the rectangle to invalidate.
9204 *
9205 * @see #invalidate(int, int, int, int)
9206 * @see #invalidate(Rect)
9207 */
9208 public void postInvalidate(int left, int top, int right, int bottom) {
9209 postInvalidateDelayed(0, left, top, right, bottom);
9210 }
9211
9212 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009213 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9214 * loop. Waits for the specified amount of time.</p>
9215 *
9216 * <p>This method can be invoked from outside of the UI thread
9217 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 *
9219 * @param delayMilliseconds the duration in milliseconds to delay the
9220 * invalidation by
9221 */
9222 public void postInvalidateDelayed(long delayMilliseconds) {
9223 // We try only with the AttachInfo because there's no point in invalidating
9224 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009225 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009226 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009227 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 }
9229 }
9230
9231 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009232 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9233 * through the event loop. Waits for the specified amount of time.</p>
9234 *
9235 * <p>This method can be invoked from outside of the UI thread
9236 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 *
9238 * @param delayMilliseconds the duration in milliseconds to delay the
9239 * invalidation by
9240 * @param left The left coordinate of the rectangle to invalidate.
9241 * @param top The top coordinate of the rectangle to invalidate.
9242 * @param right The right coordinate of the rectangle to invalidate.
9243 * @param bottom The bottom coordinate of the rectangle to invalidate.
9244 */
9245 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9246 int right, int bottom) {
9247
9248 // We try only with the AttachInfo because there's no point in invalidating
9249 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009250 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009251 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009252 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9253 info.target = this;
9254 info.left = left;
9255 info.top = top;
9256 info.right = right;
9257 info.bottom = bottom;
9258
Jeff Browna175a5b2012-02-15 19:18:31 -08009259 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 }
9261 }
9262
9263 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009264 * <p>Cause an invalidate to happen on the next animation time step, typically the
9265 * next display frame.</p>
9266 *
9267 * <p>This method can be invoked from outside of the UI thread
9268 * only when this View is attached to a window.</p>
9269 *
9270 * @hide
9271 */
9272 public void postInvalidateOnAnimation() {
9273 // We try only with the AttachInfo because there's no point in invalidating
9274 // if we are not attached to our window
9275 final AttachInfo attachInfo = mAttachInfo;
9276 if (attachInfo != null) {
9277 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9278 }
9279 }
9280
9281 /**
9282 * <p>Cause an invalidate of the specified area to happen on the next animation
9283 * time step, typically the next display frame.</p>
9284 *
9285 * <p>This method can be invoked from outside of the UI thread
9286 * only when this View is attached to a window.</p>
9287 *
9288 * @param left The left coordinate of the rectangle to invalidate.
9289 * @param top The top coordinate of the rectangle to invalidate.
9290 * @param right The right coordinate of the rectangle to invalidate.
9291 * @param bottom The bottom coordinate of the rectangle to invalidate.
9292 *
9293 * @hide
9294 */
9295 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9296 // We try only with the AttachInfo because there's no point in invalidating
9297 // if we are not attached to our window
9298 final AttachInfo attachInfo = mAttachInfo;
9299 if (attachInfo != null) {
9300 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9301 info.target = this;
9302 info.left = left;
9303 info.top = top;
9304 info.right = right;
9305 info.bottom = bottom;
9306
9307 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9308 }
9309 }
9310
9311 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009312 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9313 * This event is sent at most once every
9314 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9315 */
9316 private void postSendViewScrolledAccessibilityEventCallback() {
9317 if (mSendViewScrolledAccessibilityEvent == null) {
9318 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9319 }
9320 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9321 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9322 postDelayed(mSendViewScrolledAccessibilityEvent,
9323 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9324 }
9325 }
9326
9327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 * Called by a parent to request that a child update its values for mScrollX
9329 * and mScrollY if necessary. This will typically be done if the child is
9330 * animating a scroll using a {@link android.widget.Scroller Scroller}
9331 * object.
9332 */
9333 public void computeScroll() {
9334 }
9335
9336 /**
9337 * <p>Indicate whether the horizontal edges are faded when the view is
9338 * scrolled horizontally.</p>
9339 *
9340 * @return true if the horizontal edges should are faded on scroll, false
9341 * otherwise
9342 *
9343 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009344 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 */
9346 public boolean isHorizontalFadingEdgeEnabled() {
9347 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9348 }
9349
9350 /**
9351 * <p>Define whether the horizontal edges should be faded when this view
9352 * is scrolled horizontally.</p>
9353 *
9354 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9355 * be faded when the view is scrolled
9356 * horizontally
9357 *
9358 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009359 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 */
9361 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9362 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9363 if (horizontalFadingEdgeEnabled) {
9364 initScrollCache();
9365 }
9366
9367 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9368 }
9369 }
9370
9371 /**
9372 * <p>Indicate whether the vertical edges are faded when the view is
9373 * scrolled horizontally.</p>
9374 *
9375 * @return true if the vertical edges should are faded on scroll, false
9376 * otherwise
9377 *
9378 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009379 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 */
9381 public boolean isVerticalFadingEdgeEnabled() {
9382 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9383 }
9384
9385 /**
9386 * <p>Define whether the vertical edges should be faded when this view
9387 * is scrolled vertically.</p>
9388 *
9389 * @param verticalFadingEdgeEnabled true if the vertical edges should
9390 * be faded when the view is scrolled
9391 * vertically
9392 *
9393 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009394 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 */
9396 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9397 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9398 if (verticalFadingEdgeEnabled) {
9399 initScrollCache();
9400 }
9401
9402 mViewFlags ^= FADING_EDGE_VERTICAL;
9403 }
9404 }
9405
9406 /**
9407 * Returns the strength, or intensity, of the top faded edge. The strength is
9408 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9409 * returns 0.0 or 1.0 but no value in between.
9410 *
9411 * Subclasses should override this method to provide a smoother fade transition
9412 * when scrolling occurs.
9413 *
9414 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9415 */
9416 protected float getTopFadingEdgeStrength() {
9417 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9418 }
9419
9420 /**
9421 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9422 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9423 * returns 0.0 or 1.0 but no value in between.
9424 *
9425 * Subclasses should override this method to provide a smoother fade transition
9426 * when scrolling occurs.
9427 *
9428 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9429 */
9430 protected float getBottomFadingEdgeStrength() {
9431 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9432 computeVerticalScrollRange() ? 1.0f : 0.0f;
9433 }
9434
9435 /**
9436 * Returns the strength, or intensity, of the left faded edge. The strength is
9437 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9438 * returns 0.0 or 1.0 but no value in between.
9439 *
9440 * Subclasses should override this method to provide a smoother fade transition
9441 * when scrolling occurs.
9442 *
9443 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9444 */
9445 protected float getLeftFadingEdgeStrength() {
9446 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9447 }
9448
9449 /**
9450 * Returns the strength, or intensity, of the right faded edge. The strength is
9451 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9452 * returns 0.0 or 1.0 but no value in between.
9453 *
9454 * Subclasses should override this method to provide a smoother fade transition
9455 * when scrolling occurs.
9456 *
9457 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9458 */
9459 protected float getRightFadingEdgeStrength() {
9460 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9461 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9462 }
9463
9464 /**
9465 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9466 * scrollbar is not drawn by default.</p>
9467 *
9468 * @return true if the horizontal scrollbar should be painted, false
9469 * otherwise
9470 *
9471 * @see #setHorizontalScrollBarEnabled(boolean)
9472 */
9473 public boolean isHorizontalScrollBarEnabled() {
9474 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9475 }
9476
9477 /**
9478 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9479 * scrollbar is not drawn by default.</p>
9480 *
9481 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9482 * be painted
9483 *
9484 * @see #isHorizontalScrollBarEnabled()
9485 */
9486 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9487 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9488 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009489 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009490 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 }
9492 }
9493
9494 /**
9495 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9496 * scrollbar is not drawn by default.</p>
9497 *
9498 * @return true if the vertical scrollbar should be painted, false
9499 * otherwise
9500 *
9501 * @see #setVerticalScrollBarEnabled(boolean)
9502 */
9503 public boolean isVerticalScrollBarEnabled() {
9504 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9505 }
9506
9507 /**
9508 * <p>Define whether the vertical scrollbar should be drawn or not. The
9509 * scrollbar is not drawn by default.</p>
9510 *
9511 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9512 * be painted
9513 *
9514 * @see #isVerticalScrollBarEnabled()
9515 */
9516 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9517 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9518 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009519 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009520 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521 }
9522 }
9523
Adam Powell20232d02010-12-08 21:08:53 -08009524 /**
9525 * @hide
9526 */
9527 protected void recomputePadding() {
9528 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 }
Joe Malin32736f02011-01-19 16:14:20 -08009530
Mike Cleronfe81d382009-09-28 14:22:16 -07009531 /**
9532 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009533 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009534 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009535 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009536 */
9537 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9538 initScrollCache();
9539 final ScrollabilityCache scrollabilityCache = mScrollCache;
9540 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009541 if (fadeScrollbars) {
9542 scrollabilityCache.state = ScrollabilityCache.OFF;
9543 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009544 scrollabilityCache.state = ScrollabilityCache.ON;
9545 }
9546 }
Joe Malin32736f02011-01-19 16:14:20 -08009547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009548 /**
Joe Malin32736f02011-01-19 16:14:20 -08009549 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009550 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009551 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009552 * @return true if scrollbar fading is enabled
9553 */
9554 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009555 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009556 }
Joe Malin32736f02011-01-19 16:14:20 -08009557
Mike Cleron52f0a642009-09-28 18:21:37 -07009558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9560 * inset. When inset, they add to the padding of the view. And the scrollbars
9561 * can be drawn inside the padding area or on the edge of the view. For example,
9562 * if a view has a background drawable and you want to draw the scrollbars
9563 * inside the padding specified by the drawable, you can use
9564 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9565 * appear at the edge of the view, ignoring the padding, then you can use
9566 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9567 * @param style the style of the scrollbars. Should be one of
9568 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9569 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9570 * @see #SCROLLBARS_INSIDE_OVERLAY
9571 * @see #SCROLLBARS_INSIDE_INSET
9572 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9573 * @see #SCROLLBARS_OUTSIDE_INSET
9574 */
9575 public void setScrollBarStyle(int style) {
9576 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9577 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009578 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009579 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 }
9581 }
9582
9583 /**
9584 * <p>Returns the current scrollbar style.</p>
9585 * @return the current scrollbar style
9586 * @see #SCROLLBARS_INSIDE_OVERLAY
9587 * @see #SCROLLBARS_INSIDE_INSET
9588 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9589 * @see #SCROLLBARS_OUTSIDE_INSET
9590 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009591 @ViewDebug.ExportedProperty(mapping = {
9592 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9593 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9594 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9595 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9596 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 public int getScrollBarStyle() {
9598 return mViewFlags & SCROLLBARS_STYLE_MASK;
9599 }
9600
9601 /**
9602 * <p>Compute the horizontal range that the horizontal scrollbar
9603 * represents.</p>
9604 *
9605 * <p>The range is expressed in arbitrary units that must be the same as the
9606 * units used by {@link #computeHorizontalScrollExtent()} and
9607 * {@link #computeHorizontalScrollOffset()}.</p>
9608 *
9609 * <p>The default range is the drawing width of this view.</p>
9610 *
9611 * @return the total horizontal range represented by the horizontal
9612 * scrollbar
9613 *
9614 * @see #computeHorizontalScrollExtent()
9615 * @see #computeHorizontalScrollOffset()
9616 * @see android.widget.ScrollBarDrawable
9617 */
9618 protected int computeHorizontalScrollRange() {
9619 return getWidth();
9620 }
9621
9622 /**
9623 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9624 * within the horizontal range. This value is used to compute the position
9625 * of the thumb within the scrollbar's track.</p>
9626 *
9627 * <p>The range is expressed in arbitrary units that must be the same as the
9628 * units used by {@link #computeHorizontalScrollRange()} and
9629 * {@link #computeHorizontalScrollExtent()}.</p>
9630 *
9631 * <p>The default offset is the scroll offset of this view.</p>
9632 *
9633 * @return the horizontal offset of the scrollbar's thumb
9634 *
9635 * @see #computeHorizontalScrollRange()
9636 * @see #computeHorizontalScrollExtent()
9637 * @see android.widget.ScrollBarDrawable
9638 */
9639 protected int computeHorizontalScrollOffset() {
9640 return mScrollX;
9641 }
9642
9643 /**
9644 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9645 * within the horizontal range. This value is used to compute the length
9646 * of the thumb within the scrollbar's track.</p>
9647 *
9648 * <p>The range is expressed in arbitrary units that must be the same as the
9649 * units used by {@link #computeHorizontalScrollRange()} and
9650 * {@link #computeHorizontalScrollOffset()}.</p>
9651 *
9652 * <p>The default extent is the drawing width of this view.</p>
9653 *
9654 * @return the horizontal extent of the scrollbar's thumb
9655 *
9656 * @see #computeHorizontalScrollRange()
9657 * @see #computeHorizontalScrollOffset()
9658 * @see android.widget.ScrollBarDrawable
9659 */
9660 protected int computeHorizontalScrollExtent() {
9661 return getWidth();
9662 }
9663
9664 /**
9665 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9666 *
9667 * <p>The range is expressed in arbitrary units that must be the same as the
9668 * units used by {@link #computeVerticalScrollExtent()} and
9669 * {@link #computeVerticalScrollOffset()}.</p>
9670 *
9671 * @return the total vertical range represented by the vertical scrollbar
9672 *
9673 * <p>The default range is the drawing height of this view.</p>
9674 *
9675 * @see #computeVerticalScrollExtent()
9676 * @see #computeVerticalScrollOffset()
9677 * @see android.widget.ScrollBarDrawable
9678 */
9679 protected int computeVerticalScrollRange() {
9680 return getHeight();
9681 }
9682
9683 /**
9684 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9685 * within the horizontal range. This value is used to compute the position
9686 * of the thumb within the scrollbar's track.</p>
9687 *
9688 * <p>The range is expressed in arbitrary units that must be the same as the
9689 * units used by {@link #computeVerticalScrollRange()} and
9690 * {@link #computeVerticalScrollExtent()}.</p>
9691 *
9692 * <p>The default offset is the scroll offset of this view.</p>
9693 *
9694 * @return the vertical offset of the scrollbar's thumb
9695 *
9696 * @see #computeVerticalScrollRange()
9697 * @see #computeVerticalScrollExtent()
9698 * @see android.widget.ScrollBarDrawable
9699 */
9700 protected int computeVerticalScrollOffset() {
9701 return mScrollY;
9702 }
9703
9704 /**
9705 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9706 * within the vertical range. This value is used to compute the length
9707 * of the thumb within the scrollbar's track.</p>
9708 *
9709 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009710 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 * {@link #computeVerticalScrollOffset()}.</p>
9712 *
9713 * <p>The default extent is the drawing height of this view.</p>
9714 *
9715 * @return the vertical extent of the scrollbar's thumb
9716 *
9717 * @see #computeVerticalScrollRange()
9718 * @see #computeVerticalScrollOffset()
9719 * @see android.widget.ScrollBarDrawable
9720 */
9721 protected int computeVerticalScrollExtent() {
9722 return getHeight();
9723 }
9724
9725 /**
Adam Powell69159442011-06-13 17:53:06 -07009726 * Check if this view can be scrolled horizontally in a certain direction.
9727 *
9728 * @param direction Negative to check scrolling left, positive to check scrolling right.
9729 * @return true if this view can be scrolled in the specified direction, false otherwise.
9730 */
9731 public boolean canScrollHorizontally(int direction) {
9732 final int offset = computeHorizontalScrollOffset();
9733 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9734 if (range == 0) return false;
9735 if (direction < 0) {
9736 return offset > 0;
9737 } else {
9738 return offset < range - 1;
9739 }
9740 }
9741
9742 /**
9743 * Check if this view can be scrolled vertically in a certain direction.
9744 *
9745 * @param direction Negative to check scrolling up, positive to check scrolling down.
9746 * @return true if this view can be scrolled in the specified direction, false otherwise.
9747 */
9748 public boolean canScrollVertically(int direction) {
9749 final int offset = computeVerticalScrollOffset();
9750 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9751 if (range == 0) return false;
9752 if (direction < 0) {
9753 return offset > 0;
9754 } else {
9755 return offset < range - 1;
9756 }
9757 }
9758
9759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9761 * scrollbars are painted only if they have been awakened first.</p>
9762 *
9763 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009764 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009765 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009767 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009768 // scrollbars are drawn only when the animation is running
9769 final ScrollabilityCache cache = mScrollCache;
9770 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009771
Mike Cleronf116bf82009-09-27 19:14:12 -07009772 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009773
Mike Cleronf116bf82009-09-27 19:14:12 -07009774 if (state == ScrollabilityCache.OFF) {
9775 return;
9776 }
Joe Malin32736f02011-01-19 16:14:20 -08009777
Mike Cleronf116bf82009-09-27 19:14:12 -07009778 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009779
Mike Cleronf116bf82009-09-27 19:14:12 -07009780 if (state == ScrollabilityCache.FADING) {
9781 // We're fading -- get our fade interpolation
9782 if (cache.interpolatorValues == null) {
9783 cache.interpolatorValues = new float[1];
9784 }
Joe Malin32736f02011-01-19 16:14:20 -08009785
Mike Cleronf116bf82009-09-27 19:14:12 -07009786 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009787
Mike Cleronf116bf82009-09-27 19:14:12 -07009788 // Stops the animation if we're done
9789 if (cache.scrollBarInterpolator.timeToValues(values) ==
9790 Interpolator.Result.FREEZE_END) {
9791 cache.state = ScrollabilityCache.OFF;
9792 } else {
9793 cache.scrollBar.setAlpha(Math.round(values[0]));
9794 }
Joe Malin32736f02011-01-19 16:14:20 -08009795
9796 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009797 // drawing. We only want this when we're fading so that
9798 // we prevent excessive redraws
9799 invalidate = true;
9800 } else {
9801 // We're just on -- but we may have been fading before so
9802 // reset alpha
9803 cache.scrollBar.setAlpha(255);
9804 }
9805
Joe Malin32736f02011-01-19 16:14:20 -08009806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 final int viewFlags = mViewFlags;
9808
9809 final boolean drawHorizontalScrollBar =
9810 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9811 final boolean drawVerticalScrollBar =
9812 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9813 && !isVerticalScrollBarHidden();
9814
9815 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9816 final int width = mRight - mLeft;
9817 final int height = mBottom - mTop;
9818
9819 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820
Mike Reede8853fc2009-09-04 14:01:48 -04009821 final int scrollX = mScrollX;
9822 final int scrollY = mScrollY;
9823 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9824
Mike Cleronf116bf82009-09-27 19:14:12 -07009825 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009828 int size = scrollBar.getSize(false);
9829 if (size <= 0) {
9830 size = cache.scrollBarSize;
9831 }
9832
Mike Cleronf116bf82009-09-27 19:14:12 -07009833 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009834 computeHorizontalScrollOffset(),
9835 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009836 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009837 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009838 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009839 left = scrollX + (mPaddingLeft & inside);
9840 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9841 bottom = top + size;
9842 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9843 if (invalidate) {
9844 invalidate(left, top, right, bottom);
9845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 }
9847
9848 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009849 int size = scrollBar.getSize(true);
9850 if (size <= 0) {
9851 size = cache.scrollBarSize;
9852 }
9853
Mike Reede8853fc2009-09-04 14:01:48 -04009854 scrollBar.setParameters(computeVerticalScrollRange(),
9855 computeVerticalScrollOffset(),
9856 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009857 switch (mVerticalScrollbarPosition) {
9858 default:
9859 case SCROLLBAR_POSITION_DEFAULT:
9860 case SCROLLBAR_POSITION_RIGHT:
9861 left = scrollX + width - size - (mUserPaddingRight & inside);
9862 break;
9863 case SCROLLBAR_POSITION_LEFT:
9864 left = scrollX + (mUserPaddingLeft & inside);
9865 break;
9866 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009867 top = scrollY + (mPaddingTop & inside);
9868 right = left + size;
9869 bottom = scrollY + height - (mUserPaddingBottom & inside);
9870 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9871 if (invalidate) {
9872 invalidate(left, top, right, bottom);
9873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 }
9875 }
9876 }
9877 }
Romain Guy8506ab42009-06-11 17:35:47 -07009878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009880 * 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 -08009881 * FastScroller is visible.
9882 * @return whether to temporarily hide the vertical scrollbar
9883 * @hide
9884 */
9885 protected boolean isVerticalScrollBarHidden() {
9886 return false;
9887 }
9888
9889 /**
9890 * <p>Draw the horizontal scrollbar if
9891 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009893 * @param canvas the canvas on which to draw the scrollbar
9894 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009895 *
9896 * @see #isHorizontalScrollBarEnabled()
9897 * @see #computeHorizontalScrollRange()
9898 * @see #computeHorizontalScrollExtent()
9899 * @see #computeHorizontalScrollOffset()
9900 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009901 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009902 */
Romain Guy8fb95422010-08-17 18:38:51 -07009903 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9904 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009905 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009906 scrollBar.draw(canvas);
9907 }
Mike Reede8853fc2009-09-04 14:01:48 -04009908
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009909 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9911 * returns true.</p>
9912 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 * @param canvas the canvas on which to draw the scrollbar
9914 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 *
9916 * @see #isVerticalScrollBarEnabled()
9917 * @see #computeVerticalScrollRange()
9918 * @see #computeVerticalScrollExtent()
9919 * @see #computeVerticalScrollOffset()
9920 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009921 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 */
Romain Guy8fb95422010-08-17 18:38:51 -07009923 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9924 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009925 scrollBar.setBounds(l, t, r, b);
9926 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 }
9928
9929 /**
9930 * Implement this to do your drawing.
9931 *
9932 * @param canvas the canvas on which the background will be drawn
9933 */
9934 protected void onDraw(Canvas canvas) {
9935 }
9936
9937 /*
9938 * Caller is responsible for calling requestLayout if necessary.
9939 * (This allows addViewInLayout to not request a new layout.)
9940 */
9941 void assignParent(ViewParent parent) {
9942 if (mParent == null) {
9943 mParent = parent;
9944 } else if (parent == null) {
9945 mParent = null;
9946 } else {
9947 throw new RuntimeException("view " + this + " being added, but"
9948 + " it already has a parent");
9949 }
9950 }
9951
9952 /**
9953 * This is called when the view is attached to a window. At this point it
9954 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009955 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9956 * however it may be called any time before the first onDraw -- including
9957 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 *
9959 * @see #onDetachedFromWindow()
9960 */
9961 protected void onAttachedToWindow() {
9962 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9963 mParent.requestTransparentRegion(this);
9964 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009965 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9966 initialAwakenScrollBars();
9967 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9968 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009969 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009970 // Order is important here: LayoutDirection MUST be resolved before Padding
9971 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07009972 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009973 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009974 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009975 if (isFocused()) {
9976 InputMethodManager imm = InputMethodManager.peekInstance();
9977 imm.focusIn(this);
9978 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009979 }
Cibu Johny86666632010-02-22 13:01:02 -08009980
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009981 /**
Romain Guybb9908b2012-03-08 11:14:07 -08009982 * @see #onScreenStateChanged(int)
9983 */
9984 void dispatchScreenStateChanged(int screenState) {
9985 onScreenStateChanged(screenState);
9986 }
9987
9988 /**
9989 * This method is called whenever the state of the screen this view is
9990 * attached to changes. A state change will usually occurs when the screen
9991 * turns on or off (whether it happens automatically or the user does it
9992 * manually.)
9993 *
9994 * @param screenState The new state of the screen. Can be either
9995 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
9996 */
9997 public void onScreenStateChanged(int screenState) {
9998 }
9999
10000 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010001 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10002 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010003 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010004 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010005 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010006 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010007 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010008
10009 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -070010010 switch (getLayoutDirection()) {
10011 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -080010012 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010013 if (canResolveLayoutDirection()) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010014 ViewGroup viewGroup = ((ViewGroup) mParent);
10015
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010016 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10017 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10018 }
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010019 } else {
10020 // Nothing to do, LTR by default
Cibu Johny86666632010-02-22 13:01:02 -080010021 }
10022 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -070010023 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010024 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -080010025 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010026 case LAYOUT_DIRECTION_LOCALE:
10027 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010028 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010029 }
10030 break;
10031 default:
10032 // Nothing to do, LTR by default
10033 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010034
10035 // Set to resolved
10036 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010037 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010038 // Resolve padding
10039 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010040 }
10041
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010042 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010043 * Called when layout direction has been resolved.
10044 *
10045 * The default implementation does nothing.
10046 */
10047 public void onResolvedLayoutDirectionChanged() {
10048 }
10049
10050 /**
10051 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010052 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010053 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010054 // If the user specified the absolute padding (either with android:padding or
10055 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10056 // use the default padding or the padding from the background drawable
10057 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010058 int resolvedLayoutDirection = getResolvedLayoutDirection();
10059 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010060 case LAYOUT_DIRECTION_RTL:
10061 // Start user padding override Right user padding. Otherwise, if Right user
10062 // padding is not defined, use the default Right padding. If Right user padding
10063 // is defined, just use it.
10064 if (mUserPaddingStart >= 0) {
10065 mUserPaddingRight = mUserPaddingStart;
10066 } else if (mUserPaddingRight < 0) {
10067 mUserPaddingRight = mPaddingRight;
10068 }
10069 if (mUserPaddingEnd >= 0) {
10070 mUserPaddingLeft = mUserPaddingEnd;
10071 } else if (mUserPaddingLeft < 0) {
10072 mUserPaddingLeft = mPaddingLeft;
10073 }
10074 break;
10075 case LAYOUT_DIRECTION_LTR:
10076 default:
10077 // Start user padding override Left user padding. Otherwise, if Left user
10078 // padding is not defined, use the default left padding. If Left user padding
10079 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010080 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010081 mUserPaddingLeft = mUserPaddingStart;
10082 } else if (mUserPaddingLeft < 0) {
10083 mUserPaddingLeft = mPaddingLeft;
10084 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010085 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010086 mUserPaddingRight = mUserPaddingEnd;
10087 } else if (mUserPaddingRight < 0) {
10088 mUserPaddingRight = mPaddingRight;
10089 }
10090 }
10091
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010092 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10093
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010094 if(isPaddingRelative()) {
10095 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10096 } else {
10097 recomputePadding();
10098 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010099 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010100 }
10101
10102 /**
10103 * Resolve padding depending on the layout direction. Subclasses that care about
10104 * padding resolution should override this method. The default implementation does
10105 * nothing.
10106 *
10107 * @param layoutDirection the direction of the layout
10108 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010109 * @see {@link #LAYOUT_DIRECTION_LTR}
10110 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010111 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010112 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010113 }
10114
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010115 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010116 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010117 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010118 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010119 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010120 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010121 switch (getLayoutDirection()) {
10122 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010123 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010124 default:
10125 return true;
10126 }
10127 }
10128
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010129 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010130 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10131 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010132 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010133 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010134 // Reset the current resolved bits
10135 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010136 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010137 // Reset also the text direction
10138 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010139 }
10140
10141 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010142 * Called during reset of resolved layout direction.
10143 *
10144 * Subclasses need to override this method to clear cached information that depends on the
10145 * resolved layout direction, or to inform child views that inherit their layout direction.
10146 *
10147 * The default implementation does nothing.
10148 */
10149 public void onResolvedLayoutDirectionReset() {
10150 }
10151
10152 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010153 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010154 *
10155 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010156 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010157 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010158 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010159 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010160 }
10161
10162 /**
10163 * This is called when the view is detached from a window. At this point it
10164 * no longer has a surface for drawing.
10165 *
10166 * @see #onAttachedToWindow()
10167 */
10168 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010169 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010170
Romain Guya440b002010-02-24 15:57:54 -080010171 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010172 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010173 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010174 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010177
Romain Guya998dff2012-03-23 18:58:36 -070010178 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010179
10180 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010181 if (mDisplayList != null) {
10182 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10183 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010184 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010185 } else {
10186 if (mDisplayList != null) {
10187 // Should never happen
10188 mDisplayList.invalidate();
10189 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010190 }
10191
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010192 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010193
10194 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 }
10196
10197 /**
10198 * @return The number of times this view has been attached to a window
10199 */
10200 protected int getWindowAttachCount() {
10201 return mWindowAttachCount;
10202 }
10203
10204 /**
10205 * Retrieve a unique token identifying the window this view is attached to.
10206 * @return Return the window's token for use in
10207 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10208 */
10209 public IBinder getWindowToken() {
10210 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10211 }
10212
10213 /**
10214 * Retrieve a unique token identifying the top-level "real" window of
10215 * the window that this view is attached to. That is, this is like
10216 * {@link #getWindowToken}, except if the window this view in is a panel
10217 * window (attached to another containing window), then the token of
10218 * the containing window is returned instead.
10219 *
10220 * @return Returns the associated window token, either
10221 * {@link #getWindowToken()} or the containing window's token.
10222 */
10223 public IBinder getApplicationWindowToken() {
10224 AttachInfo ai = mAttachInfo;
10225 if (ai != null) {
10226 IBinder appWindowToken = ai.mPanelParentWindowToken;
10227 if (appWindowToken == null) {
10228 appWindowToken = ai.mWindowToken;
10229 }
10230 return appWindowToken;
10231 }
10232 return null;
10233 }
10234
10235 /**
10236 * Retrieve private session object this view hierarchy is using to
10237 * communicate with the window manager.
10238 * @return the session object to communicate with the window manager
10239 */
10240 /*package*/ IWindowSession getWindowSession() {
10241 return mAttachInfo != null ? mAttachInfo.mSession : null;
10242 }
10243
10244 /**
10245 * @param info the {@link android.view.View.AttachInfo} to associated with
10246 * this view
10247 */
10248 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10249 //System.out.println("Attached! " + this);
10250 mAttachInfo = info;
10251 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010252 // We will need to evaluate the drawable state at least once.
10253 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010254 if (mFloatingTreeObserver != null) {
10255 info.mTreeObserver.merge(mFloatingTreeObserver);
10256 mFloatingTreeObserver = null;
10257 }
10258 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10259 mAttachInfo.mScrollContainers.add(this);
10260 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10261 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070010262 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010264
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010265 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010266 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010267 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010268 if (listeners != null && listeners.size() > 0) {
10269 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10270 // perform the dispatching. The iterator is a safe guard against listeners that
10271 // could mutate the list by calling the various add/remove methods. This prevents
10272 // the array from being modified while we iterate it.
10273 for (OnAttachStateChangeListener listener : listeners) {
10274 listener.onViewAttachedToWindow(this);
10275 }
10276 }
10277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 int vis = info.mWindowVisibility;
10279 if (vis != GONE) {
10280 onWindowVisibilityChanged(vis);
10281 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010282 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10283 // If nobody has evaluated the drawable state yet, then do it now.
10284 refreshDrawableState();
10285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 }
10287
10288 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010289 AttachInfo info = mAttachInfo;
10290 if (info != null) {
10291 int vis = info.mWindowVisibility;
10292 if (vis != GONE) {
10293 onWindowVisibilityChanged(GONE);
10294 }
10295 }
10296
10297 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010298
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010299 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010300 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010301 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010302 if (listeners != null && listeners.size() > 0) {
10303 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10304 // perform the dispatching. The iterator is a safe guard against listeners that
10305 // could mutate the list by calling the various add/remove methods. This prevents
10306 // the array from being modified while we iterate it.
10307 for (OnAttachStateChangeListener listener : listeners) {
10308 listener.onViewDetachedFromWindow(this);
10309 }
10310 }
10311
Romain Guy01d5edc2011-01-28 11:28:53 -080010312 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 mAttachInfo.mScrollContainers.remove(this);
10314 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10315 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 mAttachInfo = null;
10318 }
10319
10320 /**
10321 * Store this view hierarchy's frozen state into the given container.
10322 *
10323 * @param container The SparseArray in which to save the view's state.
10324 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010325 * @see #restoreHierarchyState(android.util.SparseArray)
10326 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10327 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 */
10329 public void saveHierarchyState(SparseArray<Parcelable> container) {
10330 dispatchSaveInstanceState(container);
10331 }
10332
10333 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010334 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10335 * this view and its children. May be overridden to modify how freezing happens to a
10336 * 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 -080010337 *
10338 * @param container The SparseArray in which to save the view's state.
10339 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010340 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10341 * @see #saveHierarchyState(android.util.SparseArray)
10342 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 */
10344 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10345 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10346 mPrivateFlags &= ~SAVE_STATE_CALLED;
10347 Parcelable state = onSaveInstanceState();
10348 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10349 throw new IllegalStateException(
10350 "Derived class did not call super.onSaveInstanceState()");
10351 }
10352 if (state != null) {
10353 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10354 // + ": " + state);
10355 container.put(mID, state);
10356 }
10357 }
10358 }
10359
10360 /**
10361 * Hook allowing a view to generate a representation of its internal state
10362 * that can later be used to create a new instance with that same state.
10363 * This state should only contain information that is not persistent or can
10364 * not be reconstructed later. For example, you will never store your
10365 * current position on screen because that will be computed again when a
10366 * new instance of the view is placed in its view hierarchy.
10367 * <p>
10368 * Some examples of things you may store here: the current cursor position
10369 * in a text view (but usually not the text itself since that is stored in a
10370 * content provider or other persistent storage), the currently selected
10371 * item in a list view.
10372 *
10373 * @return Returns a Parcelable object containing the view's current dynamic
10374 * state, or null if there is nothing interesting to save. The
10375 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010376 * @see #onRestoreInstanceState(android.os.Parcelable)
10377 * @see #saveHierarchyState(android.util.SparseArray)
10378 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 * @see #setSaveEnabled(boolean)
10380 */
10381 protected Parcelable onSaveInstanceState() {
10382 mPrivateFlags |= SAVE_STATE_CALLED;
10383 return BaseSavedState.EMPTY_STATE;
10384 }
10385
10386 /**
10387 * Restore this view hierarchy's frozen state from the given container.
10388 *
10389 * @param container The SparseArray which holds previously frozen states.
10390 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010391 * @see #saveHierarchyState(android.util.SparseArray)
10392 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10393 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 */
10395 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10396 dispatchRestoreInstanceState(container);
10397 }
10398
10399 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010400 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10401 * state for this view and its children. May be overridden to modify how restoring
10402 * happens to a view's children; for example, some views may want to not store state
10403 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 *
10405 * @param container The SparseArray which holds previously saved state.
10406 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010407 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10408 * @see #restoreHierarchyState(android.util.SparseArray)
10409 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 */
10411 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10412 if (mID != NO_ID) {
10413 Parcelable state = container.get(mID);
10414 if (state != null) {
10415 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10416 // + ": " + state);
10417 mPrivateFlags &= ~SAVE_STATE_CALLED;
10418 onRestoreInstanceState(state);
10419 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10420 throw new IllegalStateException(
10421 "Derived class did not call super.onRestoreInstanceState()");
10422 }
10423 }
10424 }
10425 }
10426
10427 /**
10428 * Hook allowing a view to re-apply a representation of its internal state that had previously
10429 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10430 * null state.
10431 *
10432 * @param state The frozen state that had previously been returned by
10433 * {@link #onSaveInstanceState}.
10434 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010435 * @see #onSaveInstanceState()
10436 * @see #restoreHierarchyState(android.util.SparseArray)
10437 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438 */
10439 protected void onRestoreInstanceState(Parcelable state) {
10440 mPrivateFlags |= SAVE_STATE_CALLED;
10441 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010442 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10443 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010444 + "when two views of different type have the same id in the same hierarchy. "
10445 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010446 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 }
10448 }
10449
10450 /**
10451 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10452 *
10453 * @return the drawing start time in milliseconds
10454 */
10455 public long getDrawingTime() {
10456 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10457 }
10458
10459 /**
10460 * <p>Enables or disables the duplication of the parent's state into this view. When
10461 * duplication is enabled, this view gets its drawable state from its parent rather
10462 * than from its own internal properties.</p>
10463 *
10464 * <p>Note: in the current implementation, setting this property to true after the
10465 * view was added to a ViewGroup might have no effect at all. This property should
10466 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10467 *
10468 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10469 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010470 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010471 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10472 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 *
10474 * @param enabled True to enable duplication of the parent's drawable state, false
10475 * to disable it.
10476 *
10477 * @see #getDrawableState()
10478 * @see #isDuplicateParentStateEnabled()
10479 */
10480 public void setDuplicateParentStateEnabled(boolean enabled) {
10481 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10482 }
10483
10484 /**
10485 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10486 *
10487 * @return True if this view's drawable state is duplicated from the parent,
10488 * false otherwise
10489 *
10490 * @see #getDrawableState()
10491 * @see #setDuplicateParentStateEnabled(boolean)
10492 */
10493 public boolean isDuplicateParentStateEnabled() {
10494 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10495 }
10496
10497 /**
Romain Guy171c5922011-01-06 10:04:23 -080010498 * <p>Specifies the type of layer backing this view. The layer can be
10499 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10500 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010501 *
Romain Guy171c5922011-01-06 10:04:23 -080010502 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10503 * instance that controls how the layer is composed on screen. The following
10504 * properties of the paint are taken into account when composing the layer:</p>
10505 * <ul>
10506 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10507 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10508 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10509 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010510 *
Romain Guy171c5922011-01-06 10:04:23 -080010511 * <p>If this view has an alpha value set to < 1.0 by calling
10512 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10513 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10514 * equivalent to setting a hardware layer on this view and providing a paint with
10515 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010516 *
Romain Guy171c5922011-01-06 10:04:23 -080010517 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10518 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10519 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010520 *
Romain Guy171c5922011-01-06 10:04:23 -080010521 * @param layerType The ype of layer to use with this view, must be one of
10522 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10523 * {@link #LAYER_TYPE_HARDWARE}
10524 * @param paint The paint used to compose the layer. This argument is optional
10525 * and can be null. It is ignored when the layer type is
10526 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010527 *
10528 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010529 * @see #LAYER_TYPE_NONE
10530 * @see #LAYER_TYPE_SOFTWARE
10531 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010532 * @see #setAlpha(float)
10533 *
Romain Guy171c5922011-01-06 10:04:23 -080010534 * @attr ref android.R.styleable#View_layerType
10535 */
10536 public void setLayerType(int layerType, Paint paint) {
10537 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010538 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010539 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10540 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010541
Romain Guyd6cd5722011-01-17 14:42:41 -080010542 if (layerType == mLayerType) {
10543 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10544 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010545 invalidateParentCaches();
10546 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010547 }
10548 return;
10549 }
Romain Guy171c5922011-01-06 10:04:23 -080010550
10551 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010552 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010553 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070010554 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010555 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010556 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010557 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010558 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010559 default:
10560 break;
Romain Guy171c5922011-01-06 10:04:23 -080010561 }
10562
10563 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010564 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10565 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10566 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010567
Romain Guy0fd89bf2011-01-26 15:41:30 -080010568 invalidateParentCaches();
10569 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010570 }
10571
10572 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010573 * Indicates whether this view has a static layer. A view with layer type
10574 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10575 * dynamic.
10576 */
10577 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010578 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010579 }
10580
10581 /**
Romain Guy171c5922011-01-06 10:04:23 -080010582 * Indicates what type of layer is currently associated with this view. By default
10583 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10584 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10585 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010586 *
Romain Guy171c5922011-01-06 10:04:23 -080010587 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10588 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010589 *
10590 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010591 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010592 * @see #LAYER_TYPE_NONE
10593 * @see #LAYER_TYPE_SOFTWARE
10594 * @see #LAYER_TYPE_HARDWARE
10595 */
10596 public int getLayerType() {
10597 return mLayerType;
10598 }
Joe Malin32736f02011-01-19 16:14:20 -080010599
Romain Guy6c319ca2011-01-11 14:29:25 -080010600 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010601 * Forces this view's layer to be created and this view to be rendered
10602 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10603 * invoking this method will have no effect.
10604 *
10605 * This method can for instance be used to render a view into its layer before
10606 * starting an animation. If this view is complex, rendering into the layer
10607 * before starting the animation will avoid skipping frames.
10608 *
10609 * @throws IllegalStateException If this view is not attached to a window
10610 *
10611 * @see #setLayerType(int, android.graphics.Paint)
10612 */
10613 public void buildLayer() {
10614 if (mLayerType == LAYER_TYPE_NONE) return;
10615
10616 if (mAttachInfo == null) {
10617 throw new IllegalStateException("This view must be attached to a window first");
10618 }
10619
10620 switch (mLayerType) {
10621 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010622 if (mAttachInfo.mHardwareRenderer != null &&
10623 mAttachInfo.mHardwareRenderer.isEnabled() &&
10624 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010625 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010626 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010627 break;
10628 case LAYER_TYPE_SOFTWARE:
10629 buildDrawingCache(true);
10630 break;
10631 }
10632 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010633
10634 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10635 void flushLayer() {
10636 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10637 mHardwareLayer.flush();
10638 }
10639 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010640
10641 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010642 * <p>Returns a hardware layer that can be used to draw this view again
10643 * without executing its draw method.</p>
10644 *
10645 * @return A HardwareLayer ready to render, or null if an error occurred.
10646 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010647 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010648 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10649 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010650 return null;
10651 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010652
10653 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010654
10655 final int width = mRight - mLeft;
10656 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010657
Romain Guy6c319ca2011-01-11 14:29:25 -080010658 if (width == 0 || height == 0) {
10659 return null;
10660 }
10661
10662 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10663 if (mHardwareLayer == null) {
10664 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10665 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010666 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010667 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10668 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010669 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010670 }
10671
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010672 // The layer is not valid if the underlying GPU resources cannot be allocated
10673 if (!mHardwareLayer.isValid()) {
10674 return null;
10675 }
10676
Chet Haasea1cff502012-02-21 13:43:44 -080010677 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010678 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010679 }
10680
10681 return mHardwareLayer;
10682 }
Romain Guy171c5922011-01-06 10:04:23 -080010683
Romain Guy589b0bb2011-10-10 13:57:47 -070010684 /**
10685 * Destroys this View's hardware layer if possible.
10686 *
10687 * @return True if the layer was destroyed, false otherwise.
10688 *
10689 * @see #setLayerType(int, android.graphics.Paint)
10690 * @see #LAYER_TYPE_HARDWARE
10691 */
Romain Guya998dff2012-03-23 18:58:36 -070010692 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070010693 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010694 AttachInfo info = mAttachInfo;
10695 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070010696 info.mHardwareRenderer.isEnabled() &&
10697 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010698 mHardwareLayer.destroy();
10699 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010700
Romain Guy9c4b79a2011-11-10 19:23:58 -080010701 invalidate(true);
10702 invalidateParentCaches();
10703 }
Romain Guy65b345f2011-07-27 18:51:50 -070010704 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010705 }
Romain Guy65b345f2011-07-27 18:51:50 -070010706 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010707 }
10708
Romain Guy171c5922011-01-06 10:04:23 -080010709 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010710 * Destroys all hardware rendering resources. This method is invoked
10711 * when the system needs to reclaim resources. Upon execution of this
10712 * method, you should free any OpenGL resources created by the view.
10713 *
10714 * Note: you <strong>must</strong> call
10715 * <code>super.destroyHardwareResources()</code> when overriding
10716 * this method.
10717 *
10718 * @hide
10719 */
10720 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070010721 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010722 }
10723
10724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10726 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10727 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10728 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10729 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10730 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010731 *
Romain Guy171c5922011-01-06 10:04:23 -080010732 * <p>Enabling the drawing cache is similar to
10733 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010734 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10735 * drawing cache has no effect on rendering because the system uses a different mechanism
10736 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10737 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10738 * for information on how to enable software and hardware layers.</p>
10739 *
10740 * <p>This API can be used to manually generate
10741 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10742 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743 *
10744 * @param enabled true to enable the drawing cache, false otherwise
10745 *
10746 * @see #isDrawingCacheEnabled()
10747 * @see #getDrawingCache()
10748 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010749 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 */
10751 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010752 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10754 }
10755
10756 /**
10757 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10758 *
10759 * @return true if the drawing cache is enabled
10760 *
10761 * @see #setDrawingCacheEnabled(boolean)
10762 * @see #getDrawingCache()
10763 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010764 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010765 public boolean isDrawingCacheEnabled() {
10766 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10767 }
10768
10769 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010770 * Debugging utility which recursively outputs the dirty state of a view and its
10771 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010772 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010773 * @hide
10774 */
Romain Guy676b1732011-02-14 14:45:33 -080010775 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010776 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10777 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10778 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10779 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10780 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10781 if (clear) {
10782 mPrivateFlags &= clearMask;
10783 }
10784 if (this instanceof ViewGroup) {
10785 ViewGroup parent = (ViewGroup) this;
10786 final int count = parent.getChildCount();
10787 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010788 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010789 child.outputDirtyFlags(indent + " ", clear, clearMask);
10790 }
10791 }
10792 }
10793
10794 /**
10795 * This method is used by ViewGroup to cause its children to restore or recreate their
10796 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10797 * to recreate its own display list, which would happen if it went through the normal
10798 * draw/dispatchDraw mechanisms.
10799 *
10800 * @hide
10801 */
10802 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010803
10804 /**
10805 * A view that is not attached or hardware accelerated cannot create a display list.
10806 * This method checks these conditions and returns the appropriate result.
10807 *
10808 * @return true if view has the ability to create a display list, false otherwise.
10809 *
10810 * @hide
10811 */
10812 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010813 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010814 }
Joe Malin32736f02011-01-19 16:14:20 -080010815
Chet Haasedaf98e92011-01-10 14:10:36 -080010816 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010817 * @return The HardwareRenderer associated with that view or null if hardware rendering
10818 * is not supported or this this has not been attached to a window.
10819 *
10820 * @hide
10821 */
10822 public HardwareRenderer getHardwareRenderer() {
10823 if (mAttachInfo != null) {
10824 return mAttachInfo.mHardwareRenderer;
10825 }
10826 return null;
10827 }
10828
10829 /**
Chet Haasea1cff502012-02-21 13:43:44 -080010830 * Returns a DisplayList. If the incoming displayList is null, one will be created.
10831 * Otherwise, the same display list will be returned (after having been rendered into
10832 * along the way, depending on the invalidation state of the view).
10833 *
10834 * @param displayList The previous version of this displayList, could be null.
10835 * @param isLayer Whether the requester of the display list is a layer. If so,
10836 * the view will avoid creating a layer inside the resulting display list.
10837 * @return A new or reused DisplayList object.
10838 */
10839 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
10840 if (!canHaveDisplayList()) {
10841 return null;
10842 }
10843
10844 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10845 displayList == null || !displayList.isValid() ||
10846 (!isLayer && mRecreateDisplayList))) {
10847 // Don't need to recreate the display list, just need to tell our
10848 // children to restore/recreate theirs
10849 if (displayList != null && displayList.isValid() &&
10850 !isLayer && !mRecreateDisplayList) {
10851 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10852 mPrivateFlags &= ~DIRTY_MASK;
10853 dispatchGetDisplayList();
10854
10855 return displayList;
10856 }
10857
10858 if (!isLayer) {
10859 // If we got here, we're recreating it. Mark it as such to ensure that
10860 // we copy in child display lists into ours in drawChild()
10861 mRecreateDisplayList = true;
10862 }
10863 if (displayList == null) {
10864 final String name = getClass().getSimpleName();
10865 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
10866 // If we're creating a new display list, make sure our parent gets invalidated
10867 // since they will need to recreate their display list to account for this
10868 // new child display list.
10869 invalidateParentCaches();
10870 }
10871
10872 boolean caching = false;
10873 final HardwareCanvas canvas = displayList.start();
10874 int restoreCount = 0;
10875 int width = mRight - mLeft;
10876 int height = mBottom - mTop;
10877
10878 try {
10879 canvas.setViewport(width, height);
10880 // The dirty rect should always be null for a display list
10881 canvas.onPreDraw(null);
10882 int layerType = (
10883 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
10884 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070010885 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
10886 if (layerType == LAYER_TYPE_HARDWARE) {
10887 final HardwareLayer layer = getHardwareLayer();
10888 if (layer != null && layer.isValid()) {
10889 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
10890 } else {
10891 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
10892 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
10893 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
10894 }
10895 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080010896 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070010897 buildDrawingCache(true);
10898 Bitmap cache = getDrawingCache(true);
10899 if (cache != null) {
10900 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
10901 caching = true;
10902 }
Chet Haasea1cff502012-02-21 13:43:44 -080010903 }
Chet Haasea1cff502012-02-21 13:43:44 -080010904 } else {
10905
10906 computeScroll();
10907
10908 if (!USE_DISPLAY_LIST_PROPERTIES) {
10909 restoreCount = canvas.save();
10910 }
10911 canvas.translate(-mScrollX, -mScrollY);
10912 if (!isLayer) {
10913 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10914 mPrivateFlags &= ~DIRTY_MASK;
10915 }
10916
10917 // Fast path for layouts with no backgrounds
10918 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10919 dispatchDraw(canvas);
10920 } else {
10921 draw(canvas);
10922 }
10923 }
10924 } finally {
10925 if (USE_DISPLAY_LIST_PROPERTIES) {
10926 canvas.restoreToCount(restoreCount);
10927 }
10928 canvas.onPostDraw();
10929
10930 displayList.end();
10931 if (USE_DISPLAY_LIST_PROPERTIES) {
10932 displayList.setCaching(caching);
10933 }
10934 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
10935 displayList.setLeftTopRightBottom(0, 0, width, height);
10936 } else {
10937 setDisplayListProperties(displayList);
10938 }
10939 }
10940 } else if (!isLayer) {
10941 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10942 mPrivateFlags &= ~DIRTY_MASK;
10943 }
10944
10945 return displayList;
10946 }
10947
10948 /**
10949 * Get the DisplayList for the HardwareLayer
10950 *
10951 * @param layer The HardwareLayer whose DisplayList we want
10952 * @return A DisplayList fopr the specified HardwareLayer
10953 */
10954 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
10955 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
10956 layer.setDisplayList(displayList);
10957 return displayList;
10958 }
10959
10960
10961 /**
Romain Guyb051e892010-09-28 19:09:36 -070010962 * <p>Returns a display list that can be used to draw this view again
10963 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010964 *
Romain Guyb051e892010-09-28 19:09:36 -070010965 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010966 *
10967 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010968 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010969 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080010970 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070010971 return mDisplayList;
10972 }
10973
10974 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010975 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010976 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010977 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010978 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010979 * @see #getDrawingCache(boolean)
10980 */
10981 public Bitmap getDrawingCache() {
10982 return getDrawingCache(false);
10983 }
10984
10985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010986 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10987 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10988 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10989 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10990 * request the drawing cache by calling this method and draw it on screen if the
10991 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010992 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010993 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10994 * this method will create a bitmap of the same size as this view. Because this bitmap
10995 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10996 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10997 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10998 * size than the view. This implies that your application must be able to handle this
10999 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011000 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011001 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11002 * the current density of the screen when the application is in compatibility
11003 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011004 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011005 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 * @see #setDrawingCacheEnabled(boolean)
11008 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011009 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 * @see #destroyDrawingCache()
11011 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011012 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11014 return null;
11015 }
11016 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011017 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011018 }
Romain Guy02890fd2010-08-06 17:58:44 -070011019 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 }
11021
11022 /**
11023 * <p>Frees the resources used by the drawing cache. If you call
11024 * {@link #buildDrawingCache()} manually without calling
11025 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11026 * should cleanup the cache with this method afterwards.</p>
11027 *
11028 * @see #setDrawingCacheEnabled(boolean)
11029 * @see #buildDrawingCache()
11030 * @see #getDrawingCache()
11031 */
11032 public void destroyDrawingCache() {
11033 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011034 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 mDrawingCache = null;
11036 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011037 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011038 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011039 mUnscaledDrawingCache = null;
11040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011041 }
11042
11043 /**
11044 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011045 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011046 * view will always be drawn on top of a solid color.
11047 *
11048 * @param color The background color to use for the drawing cache's bitmap
11049 *
11050 * @see #setDrawingCacheEnabled(boolean)
11051 * @see #buildDrawingCache()
11052 * @see #getDrawingCache()
11053 */
11054 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011055 if (color != mDrawingCacheBackgroundColor) {
11056 mDrawingCacheBackgroundColor = color;
11057 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011059 }
11060
11061 /**
11062 * @see #setDrawingCacheBackgroundColor(int)
11063 *
11064 * @return The background color to used for the drawing cache's bitmap
11065 */
11066 public int getDrawingCacheBackgroundColor() {
11067 return mDrawingCacheBackgroundColor;
11068 }
11069
11070 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011071 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011072 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011073 * @see #buildDrawingCache(boolean)
11074 */
11075 public void buildDrawingCache() {
11076 buildDrawingCache(false);
11077 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011078
Romain Guyfbd8f692009-06-26 14:51:58 -070011079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011080 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11081 *
11082 * <p>If you call {@link #buildDrawingCache()} manually without calling
11083 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11084 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011085 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011086 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11087 * this method will create a bitmap of the same size as this view. Because this bitmap
11088 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11089 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11090 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11091 * size than the view. This implies that your application must be able to handle this
11092 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011093 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011094 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11095 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011096 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011097 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011098 *
11099 * @see #getDrawingCache()
11100 * @see #destroyDrawingCache()
11101 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011102 public void buildDrawingCache(boolean autoScale) {
11103 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011104 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011105 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011106
11107 if (ViewDebug.TRACE_HIERARCHY) {
11108 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110
Romain Guy8506ab42009-06-11 17:35:47 -070011111 int width = mRight - mLeft;
11112 int height = mBottom - mTop;
11113
11114 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011115 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011116
Romain Guye1123222009-06-29 14:24:56 -070011117 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011118 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11119 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121
11122 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011123 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011124 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011125
11126 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011127 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011128 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11130 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011131 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 return;
11133 }
11134
11135 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011136 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137
11138 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 Bitmap.Config quality;
11140 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011141 // Never pick ARGB_4444 because it looks awful
11142 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11144 case DRAWING_CACHE_QUALITY_AUTO:
11145 quality = Bitmap.Config.ARGB_8888;
11146 break;
11147 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011148 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 break;
11150 case DRAWING_CACHE_QUALITY_HIGH:
11151 quality = Bitmap.Config.ARGB_8888;
11152 break;
11153 default:
11154 quality = Bitmap.Config.ARGB_8888;
11155 break;
11156 }
11157 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011158 // Optimization for translucent windows
11159 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011160 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 }
11162
11163 // Try to cleanup memory
11164 if (bitmap != null) bitmap.recycle();
11165
11166 try {
11167 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011168 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011169 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011170 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011171 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011172 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011173 }
Adam Powell26153a32010-11-08 15:22:27 -080011174 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 } catch (OutOfMemoryError e) {
11176 // If there is not enough memory to create the bitmap cache, just
11177 // ignore the issue as bitmap caches are not required to draw the
11178 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011179 if (autoScale) {
11180 mDrawingCache = null;
11181 } else {
11182 mUnscaledDrawingCache = null;
11183 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011184 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011185 return;
11186 }
11187
11188 clear = drawingCacheBackgroundColor != 0;
11189 }
11190
11191 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011192 if (attachInfo != null) {
11193 canvas = attachInfo.mCanvas;
11194 if (canvas == null) {
11195 canvas = new Canvas();
11196 }
11197 canvas.setBitmap(bitmap);
11198 // Temporarily clobber the cached Canvas in case one of our children
11199 // is also using a drawing cache. Without this, the children would
11200 // steal the canvas by attaching their own bitmap to it and bad, bad
11201 // thing would happen (invisible views, corrupted drawings, etc.)
11202 attachInfo.mCanvas = null;
11203 } else {
11204 // This case should hopefully never or seldom happen
11205 canvas = new Canvas(bitmap);
11206 }
11207
11208 if (clear) {
11209 bitmap.eraseColor(drawingCacheBackgroundColor);
11210 }
11211
11212 computeScroll();
11213 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011214
Romain Guye1123222009-06-29 14:24:56 -070011215 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011216 final float scale = attachInfo.mApplicationScale;
11217 canvas.scale(scale, scale);
11218 }
Joe Malin32736f02011-01-19 16:14:20 -080011219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011220 canvas.translate(-mScrollX, -mScrollY);
11221
Romain Guy5bcdff42009-05-14 21:27:18 -070011222 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011223 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11224 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011225 mPrivateFlags |= DRAWING_CACHE_VALID;
11226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011227
11228 // Fast path for layouts with no backgrounds
11229 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11230 if (ViewDebug.TRACE_HIERARCHY) {
11231 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11232 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011233 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 dispatchDraw(canvas);
11235 } else {
11236 draw(canvas);
11237 }
11238
11239 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011240 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241
11242 if (attachInfo != null) {
11243 // Restore the cached Canvas for our siblings
11244 attachInfo.mCanvas = canvas;
11245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 }
11247 }
11248
11249 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011250 * Create a snapshot of the view into a bitmap. We should probably make
11251 * some form of this public, but should think about the API.
11252 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011253 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011254 int width = mRight - mLeft;
11255 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011256
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011257 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011258 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011259 width = (int) ((width * scale) + 0.5f);
11260 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011261
Romain Guy8c11e312009-09-14 15:15:30 -070011262 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011263 if (bitmap == null) {
11264 throw new OutOfMemoryError();
11265 }
11266
Romain Guyc529d8d2011-09-06 15:01:39 -070011267 Resources resources = getResources();
11268 if (resources != null) {
11269 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11270 }
Joe Malin32736f02011-01-19 16:14:20 -080011271
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011272 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011273 if (attachInfo != null) {
11274 canvas = attachInfo.mCanvas;
11275 if (canvas == null) {
11276 canvas = new Canvas();
11277 }
11278 canvas.setBitmap(bitmap);
11279 // Temporarily clobber the cached Canvas in case one of our children
11280 // is also using a drawing cache. Without this, the children would
11281 // steal the canvas by attaching their own bitmap to it and bad, bad
11282 // things would happen (invisible views, corrupted drawings, etc.)
11283 attachInfo.mCanvas = null;
11284 } else {
11285 // This case should hopefully never or seldom happen
11286 canvas = new Canvas(bitmap);
11287 }
11288
Romain Guy5bcdff42009-05-14 21:27:18 -070011289 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011290 bitmap.eraseColor(backgroundColor);
11291 }
11292
11293 computeScroll();
11294 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011295 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011296 canvas.translate(-mScrollX, -mScrollY);
11297
Romain Guy5bcdff42009-05-14 21:27:18 -070011298 // Temporarily remove the dirty mask
11299 int flags = mPrivateFlags;
11300 mPrivateFlags &= ~DIRTY_MASK;
11301
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011302 // Fast path for layouts with no backgrounds
11303 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11304 dispatchDraw(canvas);
11305 } else {
11306 draw(canvas);
11307 }
11308
Romain Guy5bcdff42009-05-14 21:27:18 -070011309 mPrivateFlags = flags;
11310
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011311 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011312 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011313
11314 if (attachInfo != null) {
11315 // Restore the cached Canvas for our siblings
11316 attachInfo.mCanvas = canvas;
11317 }
Romain Guy8506ab42009-06-11 17:35:47 -070011318
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011319 return bitmap;
11320 }
11321
11322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 * Indicates whether this View is currently in edit mode. A View is usually
11324 * in edit mode when displayed within a developer tool. For instance, if
11325 * this View is being drawn by a visual user interface builder, this method
11326 * should return true.
11327 *
11328 * Subclasses should check the return value of this method to provide
11329 * different behaviors if their normal behavior might interfere with the
11330 * host environment. For instance: the class spawns a thread in its
11331 * constructor, the drawing code relies on device-specific features, etc.
11332 *
11333 * This method is usually checked in the drawing code of custom widgets.
11334 *
11335 * @return True if this View is in edit mode, false otherwise.
11336 */
11337 public boolean isInEditMode() {
11338 return false;
11339 }
11340
11341 /**
11342 * If the View draws content inside its padding and enables fading edges,
11343 * it needs to support padding offsets. Padding offsets are added to the
11344 * fading edges to extend the length of the fade so that it covers pixels
11345 * drawn inside the padding.
11346 *
11347 * Subclasses of this class should override this method if they need
11348 * to draw content inside the padding.
11349 *
11350 * @return True if padding offset must be applied, false otherwise.
11351 *
11352 * @see #getLeftPaddingOffset()
11353 * @see #getRightPaddingOffset()
11354 * @see #getTopPaddingOffset()
11355 * @see #getBottomPaddingOffset()
11356 *
11357 * @since CURRENT
11358 */
11359 protected boolean isPaddingOffsetRequired() {
11360 return false;
11361 }
11362
11363 /**
11364 * Amount by which to extend the left fading region. Called only when
11365 * {@link #isPaddingOffsetRequired()} returns true.
11366 *
11367 * @return The left padding offset in pixels.
11368 *
11369 * @see #isPaddingOffsetRequired()
11370 *
11371 * @since CURRENT
11372 */
11373 protected int getLeftPaddingOffset() {
11374 return 0;
11375 }
11376
11377 /**
11378 * Amount by which to extend the right fading region. Called only when
11379 * {@link #isPaddingOffsetRequired()} returns true.
11380 *
11381 * @return The right padding offset in pixels.
11382 *
11383 * @see #isPaddingOffsetRequired()
11384 *
11385 * @since CURRENT
11386 */
11387 protected int getRightPaddingOffset() {
11388 return 0;
11389 }
11390
11391 /**
11392 * Amount by which to extend the top fading region. Called only when
11393 * {@link #isPaddingOffsetRequired()} returns true.
11394 *
11395 * @return The top padding offset in pixels.
11396 *
11397 * @see #isPaddingOffsetRequired()
11398 *
11399 * @since CURRENT
11400 */
11401 protected int getTopPaddingOffset() {
11402 return 0;
11403 }
11404
11405 /**
11406 * Amount by which to extend the bottom fading region. Called only when
11407 * {@link #isPaddingOffsetRequired()} returns true.
11408 *
11409 * @return The bottom padding offset in pixels.
11410 *
11411 * @see #isPaddingOffsetRequired()
11412 *
11413 * @since CURRENT
11414 */
11415 protected int getBottomPaddingOffset() {
11416 return 0;
11417 }
11418
11419 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011420 * @hide
11421 * @param offsetRequired
11422 */
11423 protected int getFadeTop(boolean offsetRequired) {
11424 int top = mPaddingTop;
11425 if (offsetRequired) top += getTopPaddingOffset();
11426 return top;
11427 }
11428
11429 /**
11430 * @hide
11431 * @param offsetRequired
11432 */
11433 protected int getFadeHeight(boolean offsetRequired) {
11434 int padding = mPaddingTop;
11435 if (offsetRequired) padding += getTopPaddingOffset();
11436 return mBottom - mTop - mPaddingBottom - padding;
11437 }
11438
11439 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011440 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011441 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011442 *
Romain Guy2bffd262010-09-12 17:40:02 -070011443 * <p>Even if this method returns true, it does not mean that every call
11444 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11445 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011446 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011447 * window is hardware accelerated,
11448 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11449 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011450 *
Romain Guy2bffd262010-09-12 17:40:02 -070011451 * @return True if the view is attached to a window and the window is
11452 * hardware accelerated; false in any other case.
11453 */
11454 public boolean isHardwareAccelerated() {
11455 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11456 }
Joe Malin32736f02011-01-19 16:14:20 -080011457
Romain Guy2bffd262010-09-12 17:40:02 -070011458 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011459 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11460 * case of an active Animation being run on the view.
11461 */
11462 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11463 Animation a, boolean scalingRequired) {
11464 Transformation invalidationTransform;
11465 final int flags = parent.mGroupFlags;
11466 final boolean initialized = a.isInitialized();
11467 if (!initialized) {
11468 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11469 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11470 onAnimationStart();
11471 }
11472
11473 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11474 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11475 if (parent.mInvalidationTransformation == null) {
11476 parent.mInvalidationTransformation = new Transformation();
11477 }
11478 invalidationTransform = parent.mInvalidationTransformation;
11479 a.getTransformation(drawingTime, invalidationTransform, 1f);
11480 } else {
11481 invalidationTransform = parent.mChildTransformation;
11482 }
11483 if (more) {
11484 if (!a.willChangeBounds()) {
11485 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11486 parent.FLAG_OPTIMIZE_INVALIDATE) {
11487 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11488 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11489 // The child need to draw an animation, potentially offscreen, so
11490 // make sure we do not cancel invalidate requests
11491 parent.mPrivateFlags |= DRAW_ANIMATION;
11492 parent.invalidate(mLeft, mTop, mRight, mBottom);
11493 }
11494 } else {
11495 if (parent.mInvalidateRegion == null) {
11496 parent.mInvalidateRegion = new RectF();
11497 }
11498 final RectF region = parent.mInvalidateRegion;
11499 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11500 invalidationTransform);
11501
11502 // The child need to draw an animation, potentially offscreen, so
11503 // make sure we do not cancel invalidate requests
11504 parent.mPrivateFlags |= DRAW_ANIMATION;
11505
11506 final int left = mLeft + (int) region.left;
11507 final int top = mTop + (int) region.top;
11508 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11509 top + (int) (region.height() + .5f));
11510 }
11511 }
11512 return more;
11513 }
11514
Chet Haasea1cff502012-02-21 13:43:44 -080011515 void setDisplayListProperties() {
11516 setDisplayListProperties(mDisplayList);
11517 }
11518
11519 /**
11520 * This method is called by getDisplayList() when a display list is created or re-rendered.
11521 * It sets or resets the current value of all properties on that display list (resetting is
11522 * necessary when a display list is being re-created, because we need to make sure that
11523 * previously-set transform values
11524 */
11525 void setDisplayListProperties(DisplayList displayList) {
11526 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11527 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
11528 if (mParent instanceof ViewGroup) {
11529 displayList.setClipChildren(
11530 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11531 }
11532 if (mAttachInfo != null && mAttachInfo.mScalingRequired &&
11533 mAttachInfo.mApplicationScale != 1.0f) {
11534 displayList.setApplicationScale(1f / mAttachInfo.mApplicationScale);
11535 }
11536 if (mTransformationInfo != null) {
11537 displayList.setTransformationInfo(mTransformationInfo.mAlpha,
11538 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11539 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11540 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11541 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070011542 if (mTransformationInfo.mCamera == null) {
11543 mTransformationInfo.mCamera = new Camera();
11544 mTransformationInfo.matrix3D = new Matrix();
11545 }
11546 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080011547 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11548 displayList.setPivotX(getPivotX());
11549 displayList.setPivotY(getPivotY());
11550 }
11551 }
11552 }
11553 }
11554
Chet Haasebcca79a2012-02-14 08:45:14 -080011555 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011556 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11557 * This draw() method is an implementation detail and is not intended to be overridden or
11558 * to be called from anywhere else other than ViewGroup.drawChild().
11559 */
11560 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011561 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11562 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011563 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011564 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011565 final int flags = parent.mGroupFlags;
11566
Chet Haasea1cff502012-02-21 13:43:44 -080011567 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011568 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011569 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011570 }
11571
11572 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011573 boolean concatMatrix = false;
11574
11575 boolean scalingRequired = false;
11576 boolean caching;
11577 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11578
11579 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011580 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11581 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011582 caching = true;
11583 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11584 } else {
11585 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11586 }
11587
Chet Haasebcca79a2012-02-14 08:45:14 -080011588 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011589 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011590 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011591 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011592 transformToApply = parent.mChildTransformation;
Chet Haasea1cff502012-02-21 13:43:44 -080011593 } else if ((flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011594 final boolean hasTransform =
11595 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011596 if (hasTransform) {
11597 final int transformType = parent.mChildTransformation.getTransformationType();
11598 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11599 parent.mChildTransformation : null;
11600 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11601 }
11602 }
11603
11604 concatMatrix |= !childHasIdentityMatrix;
11605
11606 // Sets the flag as early as possible to allow draw() implementations
11607 // to call invalidate() successfully when doing animations
11608 mPrivateFlags |= DRAWN;
11609
Chet Haasebcca79a2012-02-14 08:45:14 -080011610 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011611 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11612 return more;
11613 }
11614
11615 if (hardwareAccelerated) {
11616 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11617 // retain the flag's value temporarily in the mRecreateDisplayList flag
11618 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11619 mPrivateFlags &= ~INVALIDATED;
11620 }
11621
11622 computeScroll();
11623
11624 final int sx = mScrollX;
11625 final int sy = mScrollY;
11626
11627 DisplayList displayList = null;
11628 Bitmap cache = null;
11629 boolean hasDisplayList = false;
11630 if (caching) {
11631 if (!hardwareAccelerated) {
11632 if (layerType != LAYER_TYPE_NONE) {
11633 layerType = LAYER_TYPE_SOFTWARE;
11634 buildDrawingCache(true);
11635 }
11636 cache = getDrawingCache(true);
11637 } else {
11638 switch (layerType) {
11639 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070011640 if (useDisplayListProperties) {
11641 hasDisplayList = canHaveDisplayList();
11642 } else {
11643 buildDrawingCache(true);
11644 cache = getDrawingCache(true);
11645 }
Chet Haase64a48c12012-02-13 16:33:29 -080011646 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011647 case LAYER_TYPE_HARDWARE:
11648 if (useDisplayListProperties) {
11649 hasDisplayList = canHaveDisplayList();
11650 }
11651 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011652 case LAYER_TYPE_NONE:
11653 // Delay getting the display list until animation-driven alpha values are
11654 // set up and possibly passed on to the view
11655 hasDisplayList = canHaveDisplayList();
11656 break;
11657 }
11658 }
11659 }
Chet Haasea1cff502012-02-21 13:43:44 -080011660 useDisplayListProperties &= hasDisplayList;
Chet Haase64a48c12012-02-13 16:33:29 -080011661
11662 final boolean hasNoCache = cache == null || hasDisplayList;
11663 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11664 layerType != LAYER_TYPE_HARDWARE;
11665
Chet Haasea1cff502012-02-21 13:43:44 -080011666 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011667 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011668 restoreTo = canvas.save();
11669 }
Chet Haase64a48c12012-02-13 16:33:29 -080011670 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011671 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011672 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011673 if (!useDisplayListProperties) {
11674 canvas.translate(mLeft, mTop);
11675 }
Chet Haase64a48c12012-02-13 16:33:29 -080011676 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011677 if (useDisplayListProperties) {
11678 restoreTo = canvas.save();
11679 }
Chet Haase64a48c12012-02-13 16:33:29 -080011680 // mAttachInfo cannot be null, otherwise scalingRequired == false
11681 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11682 canvas.scale(scale, scale);
11683 }
11684 }
11685
Chet Haasea1cff502012-02-21 13:43:44 -080011686 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase64a48c12012-02-13 16:33:29 -080011687 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11688 if (transformToApply != null || !childHasIdentityMatrix) {
11689 int transX = 0;
11690 int transY = 0;
11691
11692 if (offsetForScroll) {
11693 transX = -sx;
11694 transY = -sy;
11695 }
11696
11697 if (transformToApply != null) {
11698 if (concatMatrix) {
11699 // Undo the scroll translation, apply the transformation matrix,
11700 // then redo the scroll translate to get the correct result.
Chet Haase89b7f2e2012-03-21 11:15:37 -070011701 canvas.translate(-transX, -transY);
11702 canvas.concat(transformToApply.getMatrix());
11703 canvas.translate(transX, transY);
Chet Haasea1cff502012-02-21 13:43:44 -080011704 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011705 }
11706
11707 float transformAlpha = transformToApply.getAlpha();
11708 if (transformAlpha < 1.0f) {
11709 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080011710 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011711 }
11712 }
11713
Chet Haasea1cff502012-02-21 13:43:44 -080011714 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011715 canvas.translate(-transX, -transY);
11716 canvas.concat(getMatrix());
11717 canvas.translate(transX, transY);
11718 }
11719 }
11720
11721 if (alpha < 1.0f) {
Chet Haasea1cff502012-02-21 13:43:44 -080011722 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011723 if (hasNoCache) {
11724 final int multipliedAlpha = (int) (255 * alpha);
11725 if (!onSetAlpha(multipliedAlpha)) {
11726 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080011727 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080011728 layerType != LAYER_TYPE_NONE) {
11729 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11730 }
11731 if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070011732 final int scrollX = hasDisplayList ? 0 : sx;
11733 final int scrollY = hasDisplayList ? 0 : sy;
11734 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11735 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011736 }
11737 } else {
11738 // Alpha is handled by the child directly, clobber the layer's alpha
11739 mPrivateFlags |= ALPHA_SET;
11740 }
11741 }
11742 }
11743 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11744 onSetAlpha(255);
11745 mPrivateFlags &= ~ALPHA_SET;
11746 }
11747
Chet Haasea1cff502012-02-21 13:43:44 -080011748 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
11749 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011750 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011751 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011752 } else {
11753 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011754 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011755 } else {
11756 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11757 }
11758 }
11759 }
11760
11761 if (hasDisplayList) {
11762 displayList = getDisplayList();
11763 if (!displayList.isValid()) {
11764 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11765 // to getDisplayList(), the display list will be marked invalid and we should not
11766 // try to use it again.
11767 displayList = null;
11768 hasDisplayList = false;
11769 }
11770 }
11771
11772 if (hasNoCache) {
11773 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080011774 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011775 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011776 if (layer != null && layer.isValid()) {
11777 mLayerPaint.setAlpha((int) (alpha * 255));
11778 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11779 layerRendered = true;
11780 } else {
11781 final int scrollX = hasDisplayList ? 0 : sx;
11782 final int scrollY = hasDisplayList ? 0 : sy;
11783 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011784 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011785 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11786 }
11787 }
11788
11789 if (!layerRendered) {
11790 if (!hasDisplayList) {
11791 // Fast path for layouts with no backgrounds
11792 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11793 if (ViewDebug.TRACE_HIERARCHY) {
11794 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11795 }
11796 mPrivateFlags &= ~DIRTY_MASK;
11797 dispatchDraw(canvas);
11798 } else {
11799 draw(canvas);
11800 }
11801 } else {
11802 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011803 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011804 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011805 }
11806 }
11807 } else if (cache != null) {
11808 mPrivateFlags &= ~DIRTY_MASK;
11809 Paint cachePaint;
11810
11811 if (layerType == LAYER_TYPE_NONE) {
11812 cachePaint = parent.mCachePaint;
11813 if (cachePaint == null) {
11814 cachePaint = new Paint();
11815 cachePaint.setDither(false);
11816 parent.mCachePaint = cachePaint;
11817 }
11818 if (alpha < 1.0f) {
11819 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080011820 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
11821 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011822 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080011823 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080011824 }
11825 } else {
11826 cachePaint = mLayerPaint;
11827 cachePaint.setAlpha((int) (alpha * 255));
11828 }
11829 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11830 }
11831
Chet Haasea1cff502012-02-21 13:43:44 -080011832 if (restoreTo >= 0) {
11833 canvas.restoreToCount(restoreTo);
11834 }
Chet Haase64a48c12012-02-13 16:33:29 -080011835
11836 if (a != null && !more) {
11837 if (!hardwareAccelerated && !a.getFillAfter()) {
11838 onSetAlpha(255);
11839 }
11840 parent.finishAnimatingView(this, a);
11841 }
11842
11843 if (more && hardwareAccelerated) {
11844 // invalidation is the trigger to recreate display lists, so if we're using
11845 // display lists to render, force an invalidate to allow the animation to
11846 // continue drawing another frame
11847 parent.invalidate(true);
11848 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11849 // alpha animations should cause the child to recreate its display list
11850 invalidate(true);
11851 }
11852 }
11853
11854 mRecreateDisplayList = false;
11855
11856 return more;
11857 }
11858
11859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 * Manually render this view (and all of its children) to the given Canvas.
11861 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011862 * called. When implementing a view, implement
11863 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11864 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011865 *
11866 * @param canvas The Canvas to which the View is rendered.
11867 */
11868 public void draw(Canvas canvas) {
11869 if (ViewDebug.TRACE_HIERARCHY) {
11870 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11871 }
11872
Romain Guy5bcdff42009-05-14 21:27:18 -070011873 final int privateFlags = mPrivateFlags;
11874 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11875 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11876 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011878 /*
11879 * Draw traversal performs several drawing steps which must be executed
11880 * in the appropriate order:
11881 *
11882 * 1. Draw the background
11883 * 2. If necessary, save the canvas' layers to prepare for fading
11884 * 3. Draw view's content
11885 * 4. Draw children
11886 * 5. If necessary, draw the fading edges and restore layers
11887 * 6. Draw decorations (scrollbars for instance)
11888 */
11889
11890 // Step 1, draw the background, if needed
11891 int saveCount;
11892
Romain Guy24443ea2009-05-11 11:56:30 -070011893 if (!dirtyOpaque) {
11894 final Drawable background = mBGDrawable;
11895 if (background != null) {
11896 final int scrollX = mScrollX;
11897 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898
Romain Guy24443ea2009-05-11 11:56:30 -070011899 if (mBackgroundSizeChanged) {
11900 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11901 mBackgroundSizeChanged = false;
11902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011903
Romain Guy24443ea2009-05-11 11:56:30 -070011904 if ((scrollX | scrollY) == 0) {
11905 background.draw(canvas);
11906 } else {
11907 canvas.translate(scrollX, scrollY);
11908 background.draw(canvas);
11909 canvas.translate(-scrollX, -scrollY);
11910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911 }
11912 }
11913
11914 // skip step 2 & 5 if possible (common case)
11915 final int viewFlags = mViewFlags;
11916 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11917 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11918 if (!verticalEdges && !horizontalEdges) {
11919 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011920 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011921
11922 // Step 4, draw the children
11923 dispatchDraw(canvas);
11924
11925 // Step 6, draw decorations (scrollbars)
11926 onDrawScrollBars(canvas);
11927
11928 // we're done...
11929 return;
11930 }
11931
11932 /*
11933 * Here we do the full fledged routine...
11934 * (this is an uncommon case where speed matters less,
11935 * this is why we repeat some of the tests that have been
11936 * done above)
11937 */
11938
11939 boolean drawTop = false;
11940 boolean drawBottom = false;
11941 boolean drawLeft = false;
11942 boolean drawRight = false;
11943
11944 float topFadeStrength = 0.0f;
11945 float bottomFadeStrength = 0.0f;
11946 float leftFadeStrength = 0.0f;
11947 float rightFadeStrength = 0.0f;
11948
11949 // Step 2, save the canvas' layers
11950 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011951
11952 final boolean offsetRequired = isPaddingOffsetRequired();
11953 if (offsetRequired) {
11954 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955 }
11956
11957 int left = mScrollX + paddingLeft;
11958 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011959 int top = mScrollY + getFadeTop(offsetRequired);
11960 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011961
11962 if (offsetRequired) {
11963 right += getRightPaddingOffset();
11964 bottom += getBottomPaddingOffset();
11965 }
11966
11967 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011968 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11969 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970
11971 // clip the fade length if top and bottom fades overlap
11972 // overlapping fades produce odd-looking artifacts
11973 if (verticalEdges && (top + length > bottom - length)) {
11974 length = (bottom - top) / 2;
11975 }
11976
11977 // also clip horizontal fades if necessary
11978 if (horizontalEdges && (left + length > right - length)) {
11979 length = (right - left) / 2;
11980 }
11981
11982 if (verticalEdges) {
11983 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011984 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011985 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011986 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 }
11988
11989 if (horizontalEdges) {
11990 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011991 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011993 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011994 }
11995
11996 saveCount = canvas.getSaveCount();
11997
11998 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011999 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12001
12002 if (drawTop) {
12003 canvas.saveLayer(left, top, right, top + length, null, flags);
12004 }
12005
12006 if (drawBottom) {
12007 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12008 }
12009
12010 if (drawLeft) {
12011 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12012 }
12013
12014 if (drawRight) {
12015 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12016 }
12017 } else {
12018 scrollabilityCache.setFadeColor(solidColor);
12019 }
12020
12021 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012022 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012023
12024 // Step 4, draw the children
12025 dispatchDraw(canvas);
12026
12027 // Step 5, draw the fade effect and restore layers
12028 final Paint p = scrollabilityCache.paint;
12029 final Matrix matrix = scrollabilityCache.matrix;
12030 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031
12032 if (drawTop) {
12033 matrix.setScale(1, fadeHeight * topFadeStrength);
12034 matrix.postTranslate(left, top);
12035 fade.setLocalMatrix(matrix);
12036 canvas.drawRect(left, top, right, top + length, p);
12037 }
12038
12039 if (drawBottom) {
12040 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12041 matrix.postRotate(180);
12042 matrix.postTranslate(left, bottom);
12043 fade.setLocalMatrix(matrix);
12044 canvas.drawRect(left, bottom - length, right, bottom, p);
12045 }
12046
12047 if (drawLeft) {
12048 matrix.setScale(1, fadeHeight * leftFadeStrength);
12049 matrix.postRotate(-90);
12050 matrix.postTranslate(left, top);
12051 fade.setLocalMatrix(matrix);
12052 canvas.drawRect(left, top, left + length, bottom, p);
12053 }
12054
12055 if (drawRight) {
12056 matrix.setScale(1, fadeHeight * rightFadeStrength);
12057 matrix.postRotate(90);
12058 matrix.postTranslate(right, top);
12059 fade.setLocalMatrix(matrix);
12060 canvas.drawRect(right - length, top, right, bottom, p);
12061 }
12062
12063 canvas.restoreToCount(saveCount);
12064
12065 // Step 6, draw decorations (scrollbars)
12066 onDrawScrollBars(canvas);
12067 }
12068
12069 /**
12070 * Override this if your view is known to always be drawn on top of a solid color background,
12071 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12072 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12073 * should be set to 0xFF.
12074 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012075 * @see #setVerticalFadingEdgeEnabled(boolean)
12076 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012077 *
12078 * @return The known solid color background for this view, or 0 if the color may vary
12079 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012080 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012081 public int getSolidColor() {
12082 return 0;
12083 }
12084
12085 /**
12086 * Build a human readable string representation of the specified view flags.
12087 *
12088 * @param flags the view flags to convert to a string
12089 * @return a String representing the supplied flags
12090 */
12091 private static String printFlags(int flags) {
12092 String output = "";
12093 int numFlags = 0;
12094 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12095 output += "TAKES_FOCUS";
12096 numFlags++;
12097 }
12098
12099 switch (flags & VISIBILITY_MASK) {
12100 case INVISIBLE:
12101 if (numFlags > 0) {
12102 output += " ";
12103 }
12104 output += "INVISIBLE";
12105 // USELESS HERE numFlags++;
12106 break;
12107 case GONE:
12108 if (numFlags > 0) {
12109 output += " ";
12110 }
12111 output += "GONE";
12112 // USELESS HERE numFlags++;
12113 break;
12114 default:
12115 break;
12116 }
12117 return output;
12118 }
12119
12120 /**
12121 * Build a human readable string representation of the specified private
12122 * view flags.
12123 *
12124 * @param privateFlags the private view flags to convert to a string
12125 * @return a String representing the supplied flags
12126 */
12127 private static String printPrivateFlags(int privateFlags) {
12128 String output = "";
12129 int numFlags = 0;
12130
12131 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
12132 output += "WANTS_FOCUS";
12133 numFlags++;
12134 }
12135
12136 if ((privateFlags & FOCUSED) == FOCUSED) {
12137 if (numFlags > 0) {
12138 output += " ";
12139 }
12140 output += "FOCUSED";
12141 numFlags++;
12142 }
12143
12144 if ((privateFlags & SELECTED) == SELECTED) {
12145 if (numFlags > 0) {
12146 output += " ";
12147 }
12148 output += "SELECTED";
12149 numFlags++;
12150 }
12151
12152 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12153 if (numFlags > 0) {
12154 output += " ";
12155 }
12156 output += "IS_ROOT_NAMESPACE";
12157 numFlags++;
12158 }
12159
12160 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12161 if (numFlags > 0) {
12162 output += " ";
12163 }
12164 output += "HAS_BOUNDS";
12165 numFlags++;
12166 }
12167
12168 if ((privateFlags & DRAWN) == DRAWN) {
12169 if (numFlags > 0) {
12170 output += " ";
12171 }
12172 output += "DRAWN";
12173 // USELESS HERE numFlags++;
12174 }
12175 return output;
12176 }
12177
12178 /**
12179 * <p>Indicates whether or not this view's layout will be requested during
12180 * the next hierarchy layout pass.</p>
12181 *
12182 * @return true if the layout will be forced during next layout pass
12183 */
12184 public boolean isLayoutRequested() {
12185 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12186 }
12187
12188 /**
12189 * Assign a size and position to a view and all of its
12190 * descendants
12191 *
12192 * <p>This is the second phase of the layout mechanism.
12193 * (The first is measuring). In this phase, each parent calls
12194 * layout on all of its children to position them.
12195 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012196 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012197 *
Chet Haase9c087442011-01-12 16:20:16 -080012198 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 * Derived classes with children should override
12200 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012201 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 *
12203 * @param l Left position, relative to parent
12204 * @param t Top position, relative to parent
12205 * @param r Right position, relative to parent
12206 * @param b Bottom position, relative to parent
12207 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012208 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012209 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012210 int oldL = mLeft;
12211 int oldT = mTop;
12212 int oldB = mBottom;
12213 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012214 boolean changed = setFrame(l, t, r, b);
12215 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12216 if (ViewDebug.TRACE_HIERARCHY) {
12217 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12218 }
12219
12220 onLayout(changed, l, t, r, b);
12221 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012222
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012223 ListenerInfo li = mListenerInfo;
12224 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012225 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012226 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012227 int numListeners = listenersCopy.size();
12228 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012229 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012230 }
12231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012232 }
12233 mPrivateFlags &= ~FORCE_LAYOUT;
12234 }
12235
12236 /**
12237 * Called from layout when this view should
12238 * assign a size and position to each of its children.
12239 *
12240 * Derived classes with children should override
12241 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012242 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012243 * @param changed This is a new size or position for this view
12244 * @param left Left position, relative to parent
12245 * @param top Top position, relative to parent
12246 * @param right Right position, relative to parent
12247 * @param bottom Bottom position, relative to parent
12248 */
12249 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12250 }
12251
12252 /**
12253 * Assign a size and position to this view.
12254 *
12255 * This is called from layout.
12256 *
12257 * @param left Left position, relative to parent
12258 * @param top Top position, relative to parent
12259 * @param right Right position, relative to parent
12260 * @param bottom Bottom position, relative to parent
12261 * @return true if the new size and position are different than the
12262 * previous ones
12263 * {@hide}
12264 */
12265 protected boolean setFrame(int left, int top, int right, int bottom) {
12266 boolean changed = false;
12267
12268 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012269 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012270 + right + "," + bottom + ")");
12271 }
12272
12273 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12274 changed = true;
12275
12276 // Remember our drawn bit
12277 int drawn = mPrivateFlags & DRAWN;
12278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012279 int oldWidth = mRight - mLeft;
12280 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012281 int newWidth = right - left;
12282 int newHeight = bottom - top;
12283 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12284
12285 // Invalidate our old position
12286 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287
12288 mLeft = left;
12289 mTop = top;
12290 mRight = right;
12291 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012292 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12293 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012295
12296 mPrivateFlags |= HAS_BOUNDS;
12297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012298
Chet Haase75755e22011-07-18 17:48:25 -070012299 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012300 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12301 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012302 if (mTransformationInfo != null) {
12303 mTransformationInfo.mMatrixDirty = true;
12304 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012306 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12307 }
12308
12309 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12310 // If we are visible, force the DRAWN bit to on so that
12311 // this invalidate will go through (at least to our parent).
12312 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012313 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 // the DRAWN bit.
12315 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012316 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012317 // parent display list may need to be recreated based on a change in the bounds
12318 // of any child
12319 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012320 }
12321
12322 // Reset drawn bit to original value (invalidate turns it off)
12323 mPrivateFlags |= drawn;
12324
12325 mBackgroundSizeChanged = true;
12326 }
12327 return changed;
12328 }
12329
12330 /**
12331 * Finalize inflating a view from XML. This is called as the last phase
12332 * of inflation, after all child views have been added.
12333 *
12334 * <p>Even if the subclass overrides onFinishInflate, they should always be
12335 * sure to call the super method, so that we get called.
12336 */
12337 protected void onFinishInflate() {
12338 }
12339
12340 /**
12341 * Returns the resources associated with this view.
12342 *
12343 * @return Resources object.
12344 */
12345 public Resources getResources() {
12346 return mResources;
12347 }
12348
12349 /**
12350 * Invalidates the specified Drawable.
12351 *
12352 * @param drawable the drawable to invalidate
12353 */
12354 public void invalidateDrawable(Drawable drawable) {
12355 if (verifyDrawable(drawable)) {
12356 final Rect dirty = drawable.getBounds();
12357 final int scrollX = mScrollX;
12358 final int scrollY = mScrollY;
12359
12360 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12361 dirty.right + scrollX, dirty.bottom + scrollY);
12362 }
12363 }
12364
12365 /**
12366 * Schedules an action on a drawable to occur at a specified time.
12367 *
12368 * @param who the recipient of the action
12369 * @param what the action to run on the drawable
12370 * @param when the time at which the action must occur. Uses the
12371 * {@link SystemClock#uptimeMillis} timebase.
12372 */
12373 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012374 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012375 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012376 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012377 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12378 Choreographer.CALLBACK_ANIMATION, what, who,
12379 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012380 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012381 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012383 }
12384 }
12385
12386 /**
12387 * Cancels a scheduled action on a drawable.
12388 *
12389 * @param who the recipient of the action
12390 * @param what the action to cancel
12391 */
12392 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012393 if (verifyDrawable(who) && what != null) {
12394 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012395 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12396 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012397 } else {
12398 ViewRootImpl.getRunQueue().removeCallbacks(what);
12399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012400 }
12401 }
12402
12403 /**
12404 * Unschedule any events associated with the given Drawable. This can be
12405 * used when selecting a new Drawable into a view, so that the previous
12406 * one is completely unscheduled.
12407 *
12408 * @param who The Drawable to unschedule.
12409 *
12410 * @see #drawableStateChanged
12411 */
12412 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012413 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012414 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12415 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012416 }
12417 }
12418
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012419 /**
12420 * Return the layout direction of a given Drawable.
12421 *
12422 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012423 */
12424 public int getResolvedLayoutDirection(Drawable who) {
12425 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012426 }
12427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012428 /**
12429 * If your view subclass is displaying its own Drawable objects, it should
12430 * override this function and return true for any Drawable it is
12431 * displaying. This allows animations for those drawables to be
12432 * scheduled.
12433 *
12434 * <p>Be sure to call through to the super class when overriding this
12435 * function.
12436 *
12437 * @param who The Drawable to verify. Return true if it is one you are
12438 * displaying, else return the result of calling through to the
12439 * super class.
12440 *
12441 * @return boolean If true than the Drawable is being displayed in the
12442 * view; else false and it is not allowed to animate.
12443 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012444 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12445 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012446 */
12447 protected boolean verifyDrawable(Drawable who) {
12448 return who == mBGDrawable;
12449 }
12450
12451 /**
12452 * This function is called whenever the state of the view changes in such
12453 * a way that it impacts the state of drawables being shown.
12454 *
12455 * <p>Be sure to call through to the superclass when overriding this
12456 * function.
12457 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012458 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459 */
12460 protected void drawableStateChanged() {
12461 Drawable d = mBGDrawable;
12462 if (d != null && d.isStateful()) {
12463 d.setState(getDrawableState());
12464 }
12465 }
12466
12467 /**
12468 * Call this to force a view to update its drawable state. This will cause
12469 * drawableStateChanged to be called on this view. Views that are interested
12470 * in the new state should call getDrawableState.
12471 *
12472 * @see #drawableStateChanged
12473 * @see #getDrawableState
12474 */
12475 public void refreshDrawableState() {
12476 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12477 drawableStateChanged();
12478
12479 ViewParent parent = mParent;
12480 if (parent != null) {
12481 parent.childDrawableStateChanged(this);
12482 }
12483 }
12484
12485 /**
12486 * Return an array of resource IDs of the drawable states representing the
12487 * current state of the view.
12488 *
12489 * @return The current drawable state
12490 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012491 * @see Drawable#setState(int[])
12492 * @see #drawableStateChanged()
12493 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494 */
12495 public final int[] getDrawableState() {
12496 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12497 return mDrawableState;
12498 } else {
12499 mDrawableState = onCreateDrawableState(0);
12500 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12501 return mDrawableState;
12502 }
12503 }
12504
12505 /**
12506 * Generate the new {@link android.graphics.drawable.Drawable} state for
12507 * this view. This is called by the view
12508 * system when the cached Drawable state is determined to be invalid. To
12509 * retrieve the current state, you should use {@link #getDrawableState}.
12510 *
12511 * @param extraSpace if non-zero, this is the number of extra entries you
12512 * would like in the returned array in which you can place your own
12513 * states.
12514 *
12515 * @return Returns an array holding the current {@link Drawable} state of
12516 * the view.
12517 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012518 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519 */
12520 protected int[] onCreateDrawableState(int extraSpace) {
12521 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12522 mParent instanceof View) {
12523 return ((View) mParent).onCreateDrawableState(extraSpace);
12524 }
12525
12526 int[] drawableState;
12527
12528 int privateFlags = mPrivateFlags;
12529
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012530 int viewStateIndex = 0;
12531 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12532 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12533 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012534 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012535 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12536 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012537 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12538 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012539 // This is set if HW acceleration is requested, even if the current
12540 // process doesn't allow it. This is just to allow app preview
12541 // windows to better match their app.
12542 viewStateIndex |= VIEW_STATE_ACCELERATED;
12543 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012544 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012545
Christopher Tate3d4bf172011-03-28 16:16:46 -070012546 final int privateFlags2 = mPrivateFlags2;
12547 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12548 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012550 drawableState = VIEW_STATE_SETS[viewStateIndex];
12551
12552 //noinspection ConstantIfStatement
12553 if (false) {
12554 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12555 Log.i("View", toString()
12556 + " pressed=" + ((privateFlags & PRESSED) != 0)
12557 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12558 + " fo=" + hasFocus()
12559 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012560 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012561 + ": " + Arrays.toString(drawableState));
12562 }
12563
12564 if (extraSpace == 0) {
12565 return drawableState;
12566 }
12567
12568 final int[] fullState;
12569 if (drawableState != null) {
12570 fullState = new int[drawableState.length + extraSpace];
12571 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12572 } else {
12573 fullState = new int[extraSpace];
12574 }
12575
12576 return fullState;
12577 }
12578
12579 /**
12580 * Merge your own state values in <var>additionalState</var> into the base
12581 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012582 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583 *
12584 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012585 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012586 * own additional state values.
12587 *
12588 * @param additionalState The additional state values you would like
12589 * added to <var>baseState</var>; this array is not modified.
12590 *
12591 * @return As a convenience, the <var>baseState</var> array you originally
12592 * passed into the function is returned.
12593 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012594 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012595 */
12596 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12597 final int N = baseState.length;
12598 int i = N - 1;
12599 while (i >= 0 && baseState[i] == 0) {
12600 i--;
12601 }
12602 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12603 return baseState;
12604 }
12605
12606 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012607 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12608 * on all Drawable objects associated with this view.
12609 */
12610 public void jumpDrawablesToCurrentState() {
12611 if (mBGDrawable != null) {
12612 mBGDrawable.jumpToCurrentState();
12613 }
12614 }
12615
12616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012617 * Sets the background color for this view.
12618 * @param color the color of the background
12619 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012620 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012621 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012622 if (mBGDrawable instanceof ColorDrawable) {
12623 ((ColorDrawable) mBGDrawable).setColor(color);
12624 } else {
12625 setBackgroundDrawable(new ColorDrawable(color));
12626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012627 }
12628
12629 /**
12630 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012631 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012632 * @param resid The identifier of the resource.
12633 * @attr ref android.R.styleable#View_background
12634 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012635 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012636 public void setBackgroundResource(int resid) {
12637 if (resid != 0 && resid == mBackgroundResource) {
12638 return;
12639 }
12640
12641 Drawable d= null;
12642 if (resid != 0) {
12643 d = mResources.getDrawable(resid);
12644 }
12645 setBackgroundDrawable(d);
12646
12647 mBackgroundResource = resid;
12648 }
12649
12650 /**
12651 * Set the background to a given Drawable, or remove the background. If the
12652 * background has padding, this View's padding is set to the background's
12653 * padding. However, when a background is removed, this View's padding isn't
12654 * touched. If setting the padding is desired, please use
12655 * {@link #setPadding(int, int, int, int)}.
12656 *
12657 * @param d The Drawable to use as the background, or null to remove the
12658 * background
12659 */
12660 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012661 if (d == mBGDrawable) {
12662 return;
12663 }
12664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012665 boolean requestLayout = false;
12666
12667 mBackgroundResource = 0;
12668
12669 /*
12670 * Regardless of whether we're setting a new background or not, we want
12671 * to clear the previous drawable.
12672 */
12673 if (mBGDrawable != null) {
12674 mBGDrawable.setCallback(null);
12675 unscheduleDrawable(mBGDrawable);
12676 }
12677
12678 if (d != null) {
12679 Rect padding = sThreadLocal.get();
12680 if (padding == null) {
12681 padding = new Rect();
12682 sThreadLocal.set(padding);
12683 }
12684 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012685 switch (d.getResolvedLayoutDirectionSelf()) {
12686 case LAYOUT_DIRECTION_RTL:
12687 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12688 break;
12689 case LAYOUT_DIRECTION_LTR:
12690 default:
12691 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012693 }
12694
12695 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12696 // if it has a different minimum size, we should layout again
12697 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12698 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12699 requestLayout = true;
12700 }
12701
12702 d.setCallback(this);
12703 if (d.isStateful()) {
12704 d.setState(getDrawableState());
12705 }
12706 d.setVisible(getVisibility() == VISIBLE, false);
12707 mBGDrawable = d;
12708
12709 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12710 mPrivateFlags &= ~SKIP_DRAW;
12711 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12712 requestLayout = true;
12713 }
12714 } else {
12715 /* Remove the background */
12716 mBGDrawable = null;
12717
12718 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12719 /*
12720 * This view ONLY drew the background before and we're removing
12721 * the background, so now it won't draw anything
12722 * (hence we SKIP_DRAW)
12723 */
12724 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12725 mPrivateFlags |= SKIP_DRAW;
12726 }
12727
12728 /*
12729 * When the background is set, we try to apply its padding to this
12730 * View. When the background is removed, we don't touch this View's
12731 * padding. This is noted in the Javadocs. Hence, we don't need to
12732 * requestLayout(), the invalidate() below is sufficient.
12733 */
12734
12735 // The old background's minimum size could have affected this
12736 // View's layout, so let's requestLayout
12737 requestLayout = true;
12738 }
12739
Romain Guy8f1344f52009-05-15 16:03:59 -070012740 computeOpaqueFlags();
12741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012742 if (requestLayout) {
12743 requestLayout();
12744 }
12745
12746 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012747 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 }
12749
12750 /**
12751 * Gets the background drawable
12752 * @return The drawable used as the background for this view, if any.
12753 */
12754 public Drawable getBackground() {
12755 return mBGDrawable;
12756 }
12757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012758 /**
12759 * Sets the padding. The view may add on the space required to display
12760 * the scrollbars, depending on the style and visibility of the scrollbars.
12761 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12762 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12763 * from the values set in this call.
12764 *
12765 * @attr ref android.R.styleable#View_padding
12766 * @attr ref android.R.styleable#View_paddingBottom
12767 * @attr ref android.R.styleable#View_paddingLeft
12768 * @attr ref android.R.styleable#View_paddingRight
12769 * @attr ref android.R.styleable#View_paddingTop
12770 * @param left the left padding in pixels
12771 * @param top the top padding in pixels
12772 * @param right the right padding in pixels
12773 * @param bottom the bottom padding in pixels
12774 */
12775 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012776 mUserPaddingStart = -1;
12777 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012778 mUserPaddingRelative = false;
12779
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012780 internalSetPadding(left, top, right, bottom);
12781 }
12782
12783 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012784 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012785 mUserPaddingRight = right;
12786 mUserPaddingBottom = bottom;
12787
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012788 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012789 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012790
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012791 // Common case is there are no scroll bars.
12792 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012793 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012794 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012795 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012796 switch (mVerticalScrollbarPosition) {
12797 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012798 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12799 left += offset;
12800 } else {
12801 right += offset;
12802 }
12803 break;
Adam Powell20232d02010-12-08 21:08:53 -080012804 case SCROLLBAR_POSITION_RIGHT:
12805 right += offset;
12806 break;
12807 case SCROLLBAR_POSITION_LEFT:
12808 left += offset;
12809 break;
12810 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012811 }
Adam Powell20232d02010-12-08 21:08:53 -080012812 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012813 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12814 ? 0 : getHorizontalScrollbarHeight();
12815 }
12816 }
Romain Guy8506ab42009-06-11 17:35:47 -070012817
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012818 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012819 changed = true;
12820 mPaddingLeft = left;
12821 }
12822 if (mPaddingTop != top) {
12823 changed = true;
12824 mPaddingTop = top;
12825 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012826 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012827 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012828 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012829 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012830 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012831 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012832 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833 }
12834
12835 if (changed) {
12836 requestLayout();
12837 }
12838 }
12839
12840 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012841 * Sets the relative padding. The view may add on the space required to display
12842 * the scrollbars, depending on the style and visibility of the scrollbars.
12843 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12844 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12845 * from the values set in this call.
12846 *
12847 * @attr ref android.R.styleable#View_padding
12848 * @attr ref android.R.styleable#View_paddingBottom
12849 * @attr ref android.R.styleable#View_paddingStart
12850 * @attr ref android.R.styleable#View_paddingEnd
12851 * @attr ref android.R.styleable#View_paddingTop
12852 * @param start the start padding in pixels
12853 * @param top the top padding in pixels
12854 * @param end the end padding in pixels
12855 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012856 */
12857 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012858 mUserPaddingStart = start;
12859 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012860 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012861
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012862 switch(getResolvedLayoutDirection()) {
12863 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012864 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012865 break;
12866 case LAYOUT_DIRECTION_LTR:
12867 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012868 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012869 }
12870 }
12871
12872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 * Returns the top padding of this view.
12874 *
12875 * @return the top padding in pixels
12876 */
12877 public int getPaddingTop() {
12878 return mPaddingTop;
12879 }
12880
12881 /**
12882 * Returns the bottom padding of this view. If there are inset and enabled
12883 * scrollbars, this value may include the space required to display the
12884 * scrollbars as well.
12885 *
12886 * @return the bottom padding in pixels
12887 */
12888 public int getPaddingBottom() {
12889 return mPaddingBottom;
12890 }
12891
12892 /**
12893 * Returns the left padding of this view. If there are inset and enabled
12894 * scrollbars, this value may include the space required to display the
12895 * scrollbars as well.
12896 *
12897 * @return the left padding in pixels
12898 */
12899 public int getPaddingLeft() {
12900 return mPaddingLeft;
12901 }
12902
12903 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012904 * Returns the start padding of this view depending on its resolved layout direction.
12905 * If there are inset and enabled scrollbars, this value may include the space
12906 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012907 *
12908 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012909 */
12910 public int getPaddingStart() {
12911 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12912 mPaddingRight : mPaddingLeft;
12913 }
12914
12915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012916 * Returns the right padding of this view. If there are inset and enabled
12917 * scrollbars, this value may include the space required to display the
12918 * scrollbars as well.
12919 *
12920 * @return the right padding in pixels
12921 */
12922 public int getPaddingRight() {
12923 return mPaddingRight;
12924 }
12925
12926 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070012927 * Returns the end padding of this view depending on its resolved layout direction.
12928 * If there are inset and enabled scrollbars, this value may include the space
12929 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012930 *
12931 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012932 */
12933 public int getPaddingEnd() {
12934 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12935 mPaddingLeft : mPaddingRight;
12936 }
12937
12938 /**
12939 * Return if the padding as been set thru relative values
12940 * {@link #setPaddingRelative(int, int, int, int)} or thru
12941 * @attr ref android.R.styleable#View_paddingStart or
12942 * @attr ref android.R.styleable#View_paddingEnd
12943 *
12944 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012945 */
12946 public boolean isPaddingRelative() {
12947 return mUserPaddingRelative;
12948 }
12949
12950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012951 * Changes the selection state of this view. A view can be selected or not.
12952 * Note that selection is not the same as focus. Views are typically
12953 * selected in the context of an AdapterView like ListView or GridView;
12954 * the selected view is the view that is highlighted.
12955 *
12956 * @param selected true if the view must be selected, false otherwise
12957 */
12958 public void setSelected(boolean selected) {
12959 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12960 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012961 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012962 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012963 refreshDrawableState();
12964 dispatchSetSelected(selected);
12965 }
12966 }
12967
12968 /**
12969 * Dispatch setSelected to all of this View's children.
12970 *
12971 * @see #setSelected(boolean)
12972 *
12973 * @param selected The new selected state
12974 */
12975 protected void dispatchSetSelected(boolean selected) {
12976 }
12977
12978 /**
12979 * Indicates the selection state of this view.
12980 *
12981 * @return true if the view is selected, false otherwise
12982 */
12983 @ViewDebug.ExportedProperty
12984 public boolean isSelected() {
12985 return (mPrivateFlags & SELECTED) != 0;
12986 }
12987
12988 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012989 * Changes the activated state of this view. A view can be activated or not.
12990 * Note that activation is not the same as selection. Selection is
12991 * a transient property, representing the view (hierarchy) the user is
12992 * currently interacting with. Activation is a longer-term state that the
12993 * user can move views in and out of. For example, in a list view with
12994 * single or multiple selection enabled, the views in the current selection
12995 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12996 * here.) The activated state is propagated down to children of the view it
12997 * is set on.
12998 *
12999 * @param activated true if the view must be activated, false otherwise
13000 */
13001 public void setActivated(boolean activated) {
13002 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13003 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013004 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013005 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013006 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013007 }
13008 }
13009
13010 /**
13011 * Dispatch setActivated to all of this View's children.
13012 *
13013 * @see #setActivated(boolean)
13014 *
13015 * @param activated The new activated state
13016 */
13017 protected void dispatchSetActivated(boolean activated) {
13018 }
13019
13020 /**
13021 * Indicates the activation state of this view.
13022 *
13023 * @return true if the view is activated, false otherwise
13024 */
13025 @ViewDebug.ExportedProperty
13026 public boolean isActivated() {
13027 return (mPrivateFlags & ACTIVATED) != 0;
13028 }
13029
13030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013031 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13032 * observer can be used to get notifications when global events, like
13033 * layout, happen.
13034 *
13035 * The returned ViewTreeObserver observer is not guaranteed to remain
13036 * valid for the lifetime of this View. If the caller of this method keeps
13037 * a long-lived reference to ViewTreeObserver, it should always check for
13038 * the return value of {@link ViewTreeObserver#isAlive()}.
13039 *
13040 * @return The ViewTreeObserver for this view's hierarchy.
13041 */
13042 public ViewTreeObserver getViewTreeObserver() {
13043 if (mAttachInfo != null) {
13044 return mAttachInfo.mTreeObserver;
13045 }
13046 if (mFloatingTreeObserver == null) {
13047 mFloatingTreeObserver = new ViewTreeObserver();
13048 }
13049 return mFloatingTreeObserver;
13050 }
13051
13052 /**
13053 * <p>Finds the topmost view in the current view hierarchy.</p>
13054 *
13055 * @return the topmost view containing this view
13056 */
13057 public View getRootView() {
13058 if (mAttachInfo != null) {
13059 final View v = mAttachInfo.mRootView;
13060 if (v != null) {
13061 return v;
13062 }
13063 }
Romain Guy8506ab42009-06-11 17:35:47 -070013064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013065 View parent = this;
13066
13067 while (parent.mParent != null && parent.mParent instanceof View) {
13068 parent = (View) parent.mParent;
13069 }
13070
13071 return parent;
13072 }
13073
13074 /**
13075 * <p>Computes the coordinates of this view on the screen. The argument
13076 * must be an array of two integers. After the method returns, the array
13077 * contains the x and y location in that order.</p>
13078 *
13079 * @param location an array of two integers in which to hold the coordinates
13080 */
13081 public void getLocationOnScreen(int[] location) {
13082 getLocationInWindow(location);
13083
13084 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013085 if (info != null) {
13086 location[0] += info.mWindowLeft;
13087 location[1] += info.mWindowTop;
13088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013089 }
13090
13091 /**
13092 * <p>Computes the coordinates of this view in its window. The argument
13093 * must be an array of two integers. After the method returns, the array
13094 * contains the x and y location in that order.</p>
13095 *
13096 * @param location an array of two integers in which to hold the coordinates
13097 */
13098 public void getLocationInWindow(int[] location) {
13099 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013100 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013101 }
13102
Gilles Debunne6583ce52011-12-06 18:09:02 -080013103 if (mAttachInfo == null) {
13104 // When the view is not attached to a window, this method does not make sense
13105 location[0] = location[1] = 0;
13106 return;
13107 }
13108
Gilles Debunnecea45132011-11-24 02:19:27 +010013109 float[] position = mAttachInfo.mTmpTransformLocation;
13110 position[0] = position[1] = 0.0f;
13111
13112 if (!hasIdentityMatrix()) {
13113 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013115
Gilles Debunnecea45132011-11-24 02:19:27 +010013116 position[0] += mLeft;
13117 position[1] += mTop;
13118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013119 ViewParent viewParent = mParent;
13120 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013121 final View view = (View) viewParent;
13122
13123 position[0] -= view.mScrollX;
13124 position[1] -= view.mScrollY;
13125
13126 if (!view.hasIdentityMatrix()) {
13127 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013128 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013129
13130 position[0] += view.mLeft;
13131 position[1] += view.mTop;
13132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013133 viewParent = view.mParent;
13134 }
Romain Guy8506ab42009-06-11 17:35:47 -070013135
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013136 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013137 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010013138 final ViewRootImpl vr = (ViewRootImpl) viewParent;
13139 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013140 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013141
13142 location[0] = (int) (position[0] + 0.5f);
13143 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013144 }
13145
13146 /**
13147 * {@hide}
13148 * @param id the id of the view to be found
13149 * @return the view of the specified id, null if cannot be found
13150 */
13151 protected View findViewTraversal(int id) {
13152 if (id == mID) {
13153 return this;
13154 }
13155 return null;
13156 }
13157
13158 /**
13159 * {@hide}
13160 * @param tag the tag of the view to be found
13161 * @return the view of specified tag, null if cannot be found
13162 */
13163 protected View findViewWithTagTraversal(Object tag) {
13164 if (tag != null && tag.equals(mTag)) {
13165 return this;
13166 }
13167 return null;
13168 }
13169
13170 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013171 * {@hide}
13172 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013173 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013174 * @return The first view that matches the predicate or null.
13175 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013176 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013177 if (predicate.apply(this)) {
13178 return this;
13179 }
13180 return null;
13181 }
13182
13183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013184 * Look for a child view with the given id. If this view has the given
13185 * id, return this view.
13186 *
13187 * @param id The id to search for.
13188 * @return The view that has the given id in the hierarchy or null
13189 */
13190 public final View findViewById(int id) {
13191 if (id < 0) {
13192 return null;
13193 }
13194 return findViewTraversal(id);
13195 }
13196
13197 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013198 * Finds a view by its unuque and stable accessibility id.
13199 *
13200 * @param accessibilityId The searched accessibility id.
13201 * @return The found view.
13202 */
13203 final View findViewByAccessibilityId(int accessibilityId) {
13204 if (accessibilityId < 0) {
13205 return null;
13206 }
13207 return findViewByAccessibilityIdTraversal(accessibilityId);
13208 }
13209
13210 /**
13211 * Performs the traversal to find a view by its unuque and stable accessibility id.
13212 *
13213 * <strong>Note:</strong>This method does not stop at the root namespace
13214 * boundary since the user can touch the screen at an arbitrary location
13215 * potentially crossing the root namespace bounday which will send an
13216 * accessibility event to accessibility services and they should be able
13217 * to obtain the event source. Also accessibility ids are guaranteed to be
13218 * unique in the window.
13219 *
13220 * @param accessibilityId The accessibility id.
13221 * @return The found view.
13222 */
13223 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13224 if (getAccessibilityViewId() == accessibilityId) {
13225 return this;
13226 }
13227 return null;
13228 }
13229
13230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013231 * Look for a child view with the given tag. If this view has the given
13232 * tag, return this view.
13233 *
13234 * @param tag The tag to search for, using "tag.equals(getTag())".
13235 * @return The View that has the given tag in the hierarchy or null
13236 */
13237 public final View findViewWithTag(Object tag) {
13238 if (tag == null) {
13239 return null;
13240 }
13241 return findViewWithTagTraversal(tag);
13242 }
13243
13244 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013245 * {@hide}
13246 * Look for a child view that matches the specified predicate.
13247 * If this view matches the predicate, return this view.
13248 *
13249 * @param predicate The predicate to evaluate.
13250 * @return The first view that matches the predicate or null.
13251 */
13252 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013253 return findViewByPredicateTraversal(predicate, null);
13254 }
13255
13256 /**
13257 * {@hide}
13258 * Look for a child view that matches the specified predicate,
13259 * starting with the specified view and its descendents and then
13260 * recusively searching the ancestors and siblings of that view
13261 * until this view is reached.
13262 *
13263 * This method is useful in cases where the predicate does not match
13264 * a single unique view (perhaps multiple views use the same id)
13265 * and we are trying to find the view that is "closest" in scope to the
13266 * starting view.
13267 *
13268 * @param start The view to start from.
13269 * @param predicate The predicate to evaluate.
13270 * @return The first view that matches the predicate or null.
13271 */
13272 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13273 View childToSkip = null;
13274 for (;;) {
13275 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13276 if (view != null || start == this) {
13277 return view;
13278 }
13279
13280 ViewParent parent = start.getParent();
13281 if (parent == null || !(parent instanceof View)) {
13282 return null;
13283 }
13284
13285 childToSkip = start;
13286 start = (View) parent;
13287 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013288 }
13289
13290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013291 * Sets the identifier for this view. The identifier does not have to be
13292 * unique in this view's hierarchy. The identifier should be a positive
13293 * number.
13294 *
13295 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070013296 * @see #getId()
13297 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013298 *
13299 * @param id a number used to identify the view
13300 *
13301 * @attr ref android.R.styleable#View_id
13302 */
13303 public void setId(int id) {
13304 mID = id;
13305 }
13306
13307 /**
13308 * {@hide}
13309 *
13310 * @param isRoot true if the view belongs to the root namespace, false
13311 * otherwise
13312 */
13313 public void setIsRootNamespace(boolean isRoot) {
13314 if (isRoot) {
13315 mPrivateFlags |= IS_ROOT_NAMESPACE;
13316 } else {
13317 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13318 }
13319 }
13320
13321 /**
13322 * {@hide}
13323 *
13324 * @return true if the view belongs to the root namespace, false otherwise
13325 */
13326 public boolean isRootNamespace() {
13327 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13328 }
13329
13330 /**
13331 * Returns this view's identifier.
13332 *
13333 * @return a positive integer used to identify the view or {@link #NO_ID}
13334 * if the view has no ID
13335 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013336 * @see #setId(int)
13337 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338 * @attr ref android.R.styleable#View_id
13339 */
13340 @ViewDebug.CapturedViewProperty
13341 public int getId() {
13342 return mID;
13343 }
13344
13345 /**
13346 * Returns this view's tag.
13347 *
13348 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013349 *
13350 * @see #setTag(Object)
13351 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013352 */
13353 @ViewDebug.ExportedProperty
13354 public Object getTag() {
13355 return mTag;
13356 }
13357
13358 /**
13359 * Sets the tag associated with this view. A tag can be used to mark
13360 * a view in its hierarchy and does not have to be unique within the
13361 * hierarchy. Tags can also be used to store data within a view without
13362 * resorting to another data structure.
13363 *
13364 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013365 *
13366 * @see #getTag()
13367 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013368 */
13369 public void setTag(final Object tag) {
13370 mTag = tag;
13371 }
13372
13373 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013374 * Returns the tag associated with this view and the specified key.
13375 *
13376 * @param key The key identifying the tag
13377 *
13378 * @return the Object stored in this view as a tag
13379 *
13380 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013381 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013382 */
13383 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013384 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013385 return null;
13386 }
13387
13388 /**
13389 * Sets a tag associated with this view and a key. A tag can be used
13390 * to mark a view in its hierarchy and does not have to be unique within
13391 * the hierarchy. Tags can also be used to store data within a view
13392 * without resorting to another data structure.
13393 *
13394 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013395 * application to ensure it is unique (see the <a
13396 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13397 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013398 * the Android framework or not associated with any package will cause
13399 * an {@link IllegalArgumentException} to be thrown.
13400 *
13401 * @param key The key identifying the tag
13402 * @param tag An Object to tag the view with
13403 *
13404 * @throws IllegalArgumentException If they specified key is not valid
13405 *
13406 * @see #setTag(Object)
13407 * @see #getTag(int)
13408 */
13409 public void setTag(int key, final Object tag) {
13410 // If the package id is 0x00 or 0x01, it's either an undefined package
13411 // or a framework id
13412 if ((key >>> 24) < 2) {
13413 throw new IllegalArgumentException("The key must be an application-specific "
13414 + "resource id.");
13415 }
13416
Adam Powell2b2f6d62011-09-23 11:15:39 -070013417 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013418 }
13419
13420 /**
13421 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13422 * framework id.
13423 *
13424 * @hide
13425 */
13426 public void setTagInternal(int key, Object tag) {
13427 if ((key >>> 24) != 0x1) {
13428 throw new IllegalArgumentException("The key must be a framework-specific "
13429 + "resource id.");
13430 }
13431
Adam Powell2b2f6d62011-09-23 11:15:39 -070013432 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013433 }
13434
Adam Powell2b2f6d62011-09-23 11:15:39 -070013435 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013436 if (mKeyedTags == null) {
13437 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013438 }
13439
Adam Powell7db82ac2011-09-22 19:44:04 -070013440 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013441 }
13442
13443 /**
Romain Guy13922e02009-05-12 17:56:14 -070013444 * @param consistency The type of consistency. See ViewDebug for more information.
13445 *
13446 * @hide
13447 */
13448 protected boolean dispatchConsistencyCheck(int consistency) {
13449 return onConsistencyCheck(consistency);
13450 }
13451
13452 /**
13453 * Method that subclasses should implement to check their consistency. The type of
13454 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013455 *
Romain Guy13922e02009-05-12 17:56:14 -070013456 * @param consistency The type of consistency. See ViewDebug for more information.
13457 *
13458 * @throws IllegalStateException if the view is in an inconsistent state.
13459 *
13460 * @hide
13461 */
13462 protected boolean onConsistencyCheck(int consistency) {
13463 boolean result = true;
13464
13465 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13466 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13467
13468 if (checkLayout) {
13469 if (getParent() == null) {
13470 result = false;
13471 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13472 "View " + this + " does not have a parent.");
13473 }
13474
13475 if (mAttachInfo == null) {
13476 result = false;
13477 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13478 "View " + this + " is not attached to a window.");
13479 }
13480 }
13481
13482 if (checkDrawing) {
13483 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13484 // from their draw() method
13485
13486 if ((mPrivateFlags & DRAWN) != DRAWN &&
13487 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13488 result = false;
13489 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13490 "View " + this + " was invalidated but its drawing cache is valid.");
13491 }
13492 }
13493
13494 return result;
13495 }
13496
13497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013498 * Prints information about this view in the log output, with the tag
13499 * {@link #VIEW_LOG_TAG}.
13500 *
13501 * @hide
13502 */
13503 public void debug() {
13504 debug(0);
13505 }
13506
13507 /**
13508 * Prints information about this view in the log output, with the tag
13509 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13510 * indentation defined by the <code>depth</code>.
13511 *
13512 * @param depth the indentation level
13513 *
13514 * @hide
13515 */
13516 protected void debug(int depth) {
13517 String output = debugIndent(depth - 1);
13518
13519 output += "+ " + this;
13520 int id = getId();
13521 if (id != -1) {
13522 output += " (id=" + id + ")";
13523 }
13524 Object tag = getTag();
13525 if (tag != null) {
13526 output += " (tag=" + tag + ")";
13527 }
13528 Log.d(VIEW_LOG_TAG, output);
13529
13530 if ((mPrivateFlags & FOCUSED) != 0) {
13531 output = debugIndent(depth) + " FOCUSED";
13532 Log.d(VIEW_LOG_TAG, output);
13533 }
13534
13535 output = debugIndent(depth);
13536 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13537 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13538 + "} ";
13539 Log.d(VIEW_LOG_TAG, output);
13540
13541 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13542 || mPaddingBottom != 0) {
13543 output = debugIndent(depth);
13544 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13545 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13546 Log.d(VIEW_LOG_TAG, output);
13547 }
13548
13549 output = debugIndent(depth);
13550 output += "mMeasureWidth=" + mMeasuredWidth +
13551 " mMeasureHeight=" + mMeasuredHeight;
13552 Log.d(VIEW_LOG_TAG, output);
13553
13554 output = debugIndent(depth);
13555 if (mLayoutParams == null) {
13556 output += "BAD! no layout params";
13557 } else {
13558 output = mLayoutParams.debug(output);
13559 }
13560 Log.d(VIEW_LOG_TAG, output);
13561
13562 output = debugIndent(depth);
13563 output += "flags={";
13564 output += View.printFlags(mViewFlags);
13565 output += "}";
13566 Log.d(VIEW_LOG_TAG, output);
13567
13568 output = debugIndent(depth);
13569 output += "privateFlags={";
13570 output += View.printPrivateFlags(mPrivateFlags);
13571 output += "}";
13572 Log.d(VIEW_LOG_TAG, output);
13573 }
13574
13575 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013576 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013577 *
13578 * @param depth the indentation level
13579 * @return a String containing (depth * 2 + 3) * 2 white spaces
13580 *
13581 * @hide
13582 */
13583 protected static String debugIndent(int depth) {
13584 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13585 for (int i = 0; i < (depth * 2) + 3; i++) {
13586 spaces.append(' ').append(' ');
13587 }
13588 return spaces.toString();
13589 }
13590
13591 /**
13592 * <p>Return the offset of the widget's text baseline from the widget's top
13593 * boundary. If this widget does not support baseline alignment, this
13594 * method returns -1. </p>
13595 *
13596 * @return the offset of the baseline within the widget's bounds or -1
13597 * if baseline alignment is not supported
13598 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013599 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013600 public int getBaseline() {
13601 return -1;
13602 }
13603
13604 /**
13605 * Call this when something has changed which has invalidated the
13606 * layout of this view. This will schedule a layout pass of the view
13607 * tree.
13608 */
13609 public void requestLayout() {
13610 if (ViewDebug.TRACE_HIERARCHY) {
13611 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13612 }
13613
13614 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013615 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013616
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013617 if (mParent != null) {
13618 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013619 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013620 }
13621 if (!mParent.isLayoutRequested()) {
13622 mParent.requestLayout();
13623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013624 }
13625 }
13626
13627 /**
13628 * Forces this view to be laid out during the next layout pass.
13629 * This method does not call requestLayout() or forceLayout()
13630 * on the parent.
13631 */
13632 public void forceLayout() {
13633 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013634 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013635 }
13636
13637 /**
13638 * <p>
13639 * This is called to find out how big a view should be. The parent
13640 * supplies constraint information in the width and height parameters.
13641 * </p>
13642 *
13643 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013644 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013646 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013647 * </p>
13648 *
13649 *
13650 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13651 * parent
13652 * @param heightMeasureSpec Vertical space requirements as imposed by the
13653 * parent
13654 *
13655 * @see #onMeasure(int, int)
13656 */
13657 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13658 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13659 widthMeasureSpec != mOldWidthMeasureSpec ||
13660 heightMeasureSpec != mOldHeightMeasureSpec) {
13661
13662 // first clears the measured dimension flag
13663 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13664
13665 if (ViewDebug.TRACE_HIERARCHY) {
13666 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13667 }
13668
13669 // measure ourselves, this should set the measured dimension flag back
13670 onMeasure(widthMeasureSpec, heightMeasureSpec);
13671
13672 // flag not set, setMeasuredDimension() was not invoked, we raise
13673 // an exception to warn the developer
13674 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13675 throw new IllegalStateException("onMeasure() did not set the"
13676 + " measured dimension by calling"
13677 + " setMeasuredDimension()");
13678 }
13679
13680 mPrivateFlags |= LAYOUT_REQUIRED;
13681 }
13682
13683 mOldWidthMeasureSpec = widthMeasureSpec;
13684 mOldHeightMeasureSpec = heightMeasureSpec;
13685 }
13686
13687 /**
13688 * <p>
13689 * Measure the view and its content to determine the measured width and the
13690 * measured height. This method is invoked by {@link #measure(int, int)} and
13691 * should be overriden by subclasses to provide accurate and efficient
13692 * measurement of their contents.
13693 * </p>
13694 *
13695 * <p>
13696 * <strong>CONTRACT:</strong> When overriding this method, you
13697 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13698 * measured width and height of this view. Failure to do so will trigger an
13699 * <code>IllegalStateException</code>, thrown by
13700 * {@link #measure(int, int)}. Calling the superclass'
13701 * {@link #onMeasure(int, int)} is a valid use.
13702 * </p>
13703 *
13704 * <p>
13705 * The base class implementation of measure defaults to the background size,
13706 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13707 * override {@link #onMeasure(int, int)} to provide better measurements of
13708 * their content.
13709 * </p>
13710 *
13711 * <p>
13712 * If this method is overridden, it is the subclass's responsibility to make
13713 * sure the measured height and width are at least the view's minimum height
13714 * and width ({@link #getSuggestedMinimumHeight()} and
13715 * {@link #getSuggestedMinimumWidth()}).
13716 * </p>
13717 *
13718 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13719 * The requirements are encoded with
13720 * {@link android.view.View.MeasureSpec}.
13721 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13722 * The requirements are encoded with
13723 * {@link android.view.View.MeasureSpec}.
13724 *
13725 * @see #getMeasuredWidth()
13726 * @see #getMeasuredHeight()
13727 * @see #setMeasuredDimension(int, int)
13728 * @see #getSuggestedMinimumHeight()
13729 * @see #getSuggestedMinimumWidth()
13730 * @see android.view.View.MeasureSpec#getMode(int)
13731 * @see android.view.View.MeasureSpec#getSize(int)
13732 */
13733 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13734 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13735 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13736 }
13737
13738 /**
13739 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13740 * measured width and measured height. Failing to do so will trigger an
13741 * exception at measurement time.</p>
13742 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013743 * @param measuredWidth The measured width of this view. May be a complex
13744 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13745 * {@link #MEASURED_STATE_TOO_SMALL}.
13746 * @param measuredHeight The measured height of this view. May be a complex
13747 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13748 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013749 */
13750 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13751 mMeasuredWidth = measuredWidth;
13752 mMeasuredHeight = measuredHeight;
13753
13754 mPrivateFlags |= MEASURED_DIMENSION_SET;
13755 }
13756
13757 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013758 * Merge two states as returned by {@link #getMeasuredState()}.
13759 * @param curState The current state as returned from a view or the result
13760 * of combining multiple views.
13761 * @param newState The new view state to combine.
13762 * @return Returns a new integer reflecting the combination of the two
13763 * states.
13764 */
13765 public static int combineMeasuredStates(int curState, int newState) {
13766 return curState | newState;
13767 }
13768
13769 /**
13770 * Version of {@link #resolveSizeAndState(int, int, int)}
13771 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13772 */
13773 public static int resolveSize(int size, int measureSpec) {
13774 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13775 }
13776
13777 /**
13778 * Utility to reconcile a desired size and state, with constraints imposed
13779 * by a MeasureSpec. Will take the desired size, unless a different size
13780 * is imposed by the constraints. The returned value is a compound integer,
13781 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13782 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13783 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784 *
13785 * @param size How big the view wants to be
13786 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013787 * @return Size information bit mask as defined by
13788 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013789 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013790 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791 int result = size;
13792 int specMode = MeasureSpec.getMode(measureSpec);
13793 int specSize = MeasureSpec.getSize(measureSpec);
13794 switch (specMode) {
13795 case MeasureSpec.UNSPECIFIED:
13796 result = size;
13797 break;
13798 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013799 if (specSize < size) {
13800 result = specSize | MEASURED_STATE_TOO_SMALL;
13801 } else {
13802 result = size;
13803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013804 break;
13805 case MeasureSpec.EXACTLY:
13806 result = specSize;
13807 break;
13808 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013809 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013810 }
13811
13812 /**
13813 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013814 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013815 * by the MeasureSpec.
13816 *
13817 * @param size Default size for this view
13818 * @param measureSpec Constraints imposed by the parent
13819 * @return The size this view should be.
13820 */
13821 public static int getDefaultSize(int size, int measureSpec) {
13822 int result = size;
13823 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013824 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013825
13826 switch (specMode) {
13827 case MeasureSpec.UNSPECIFIED:
13828 result = size;
13829 break;
13830 case MeasureSpec.AT_MOST:
13831 case MeasureSpec.EXACTLY:
13832 result = specSize;
13833 break;
13834 }
13835 return result;
13836 }
13837
13838 /**
13839 * Returns the suggested minimum height that the view should use. This
13840 * returns the maximum of the view's minimum height
13841 * and the background's minimum height
13842 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13843 * <p>
13844 * When being used in {@link #onMeasure(int, int)}, the caller should still
13845 * ensure the returned height is within the requirements of the parent.
13846 *
13847 * @return The suggested minimum height of the view.
13848 */
13849 protected int getSuggestedMinimumHeight() {
13850 int suggestedMinHeight = mMinHeight;
13851
13852 if (mBGDrawable != null) {
13853 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13854 if (suggestedMinHeight < bgMinHeight) {
13855 suggestedMinHeight = bgMinHeight;
13856 }
13857 }
13858
13859 return suggestedMinHeight;
13860 }
13861
13862 /**
13863 * Returns the suggested minimum width that the view should use. This
13864 * returns the maximum of the view's minimum width)
13865 * and the background's minimum width
13866 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13867 * <p>
13868 * When being used in {@link #onMeasure(int, int)}, the caller should still
13869 * ensure the returned width is within the requirements of the parent.
13870 *
13871 * @return The suggested minimum width of the view.
13872 */
13873 protected int getSuggestedMinimumWidth() {
13874 int suggestedMinWidth = mMinWidth;
13875
13876 if (mBGDrawable != null) {
13877 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13878 if (suggestedMinWidth < bgMinWidth) {
13879 suggestedMinWidth = bgMinWidth;
13880 }
13881 }
13882
13883 return suggestedMinWidth;
13884 }
13885
13886 /**
13887 * Sets the minimum height of the view. It is not guaranteed the view will
13888 * be able to achieve this minimum height (for example, if its parent layout
13889 * constrains it with less available height).
13890 *
13891 * @param minHeight The minimum height the view will try to be.
13892 */
13893 public void setMinimumHeight(int minHeight) {
13894 mMinHeight = minHeight;
13895 }
13896
13897 /**
13898 * Sets the minimum width of the view. It is not guaranteed the view will
13899 * be able to achieve this minimum width (for example, if its parent layout
13900 * constrains it with less available width).
13901 *
13902 * @param minWidth The minimum width the view will try to be.
13903 */
13904 public void setMinimumWidth(int minWidth) {
13905 mMinWidth = minWidth;
13906 }
13907
13908 /**
13909 * Get the animation currently associated with this view.
13910 *
13911 * @return The animation that is currently playing or
13912 * scheduled to play for this view.
13913 */
13914 public Animation getAnimation() {
13915 return mCurrentAnimation;
13916 }
13917
13918 /**
13919 * Start the specified animation now.
13920 *
13921 * @param animation the animation to start now
13922 */
13923 public void startAnimation(Animation animation) {
13924 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13925 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013926 invalidateParentCaches();
13927 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013928 }
13929
13930 /**
13931 * Cancels any animations for this view.
13932 */
13933 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013934 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013935 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013938 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013939 }
13940
13941 /**
13942 * Sets the next animation to play for this view.
13943 * If you want the animation to play immediately, use
13944 * startAnimation. This method provides allows fine-grained
13945 * control over the start time and invalidation, but you
13946 * must make sure that 1) the animation has a start time set, and
13947 * 2) the view will be invalidated when the animation is supposed to
13948 * start.
13949 *
13950 * @param animation The next animation, or null.
13951 */
13952 public void setAnimation(Animation animation) {
13953 mCurrentAnimation = animation;
13954 if (animation != null) {
13955 animation.reset();
13956 }
13957 }
13958
13959 /**
13960 * Invoked by a parent ViewGroup to notify the start of the animation
13961 * currently associated with this view. If you override this method,
13962 * always call super.onAnimationStart();
13963 *
13964 * @see #setAnimation(android.view.animation.Animation)
13965 * @see #getAnimation()
13966 */
13967 protected void onAnimationStart() {
13968 mPrivateFlags |= ANIMATION_STARTED;
13969 }
13970
13971 /**
13972 * Invoked by a parent ViewGroup to notify the end of the animation
13973 * currently associated with this view. If you override this method,
13974 * always call super.onAnimationEnd();
13975 *
13976 * @see #setAnimation(android.view.animation.Animation)
13977 * @see #getAnimation()
13978 */
13979 protected void onAnimationEnd() {
13980 mPrivateFlags &= ~ANIMATION_STARTED;
13981 }
13982
13983 /**
13984 * Invoked if there is a Transform that involves alpha. Subclass that can
13985 * draw themselves with the specified alpha should return true, and then
13986 * respect that alpha when their onDraw() is called. If this returns false
13987 * then the view may be redirected to draw into an offscreen buffer to
13988 * fulfill the request, which will look fine, but may be slower than if the
13989 * subclass handles it internally. The default implementation returns false.
13990 *
13991 * @param alpha The alpha (0..255) to apply to the view's drawing
13992 * @return true if the view can draw with the specified alpha.
13993 */
13994 protected boolean onSetAlpha(int alpha) {
13995 return false;
13996 }
13997
13998 /**
13999 * This is used by the RootView to perform an optimization when
14000 * the view hierarchy contains one or several SurfaceView.
14001 * SurfaceView is always considered transparent, but its children are not,
14002 * therefore all View objects remove themselves from the global transparent
14003 * region (passed as a parameter to this function).
14004 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014005 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 *
14007 * @return Returns true if the effective visibility of the view at this
14008 * point is opaque, regardless of the transparent region; returns false
14009 * if it is possible for underlying windows to be seen behind the view.
14010 *
14011 * {@hide}
14012 */
14013 public boolean gatherTransparentRegion(Region region) {
14014 final AttachInfo attachInfo = mAttachInfo;
14015 if (region != null && attachInfo != null) {
14016 final int pflags = mPrivateFlags;
14017 if ((pflags & SKIP_DRAW) == 0) {
14018 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14019 // remove it from the transparent region.
14020 final int[] location = attachInfo.mTransparentLocation;
14021 getLocationInWindow(location);
14022 region.op(location[0], location[1], location[0] + mRight - mLeft,
14023 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
14024 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
14025 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14026 // exists, so we remove the background drawable's non-transparent
14027 // parts from this transparent region.
14028 applyDrawableToTransparentRegion(mBGDrawable, region);
14029 }
14030 }
14031 return true;
14032 }
14033
14034 /**
14035 * Play a sound effect for this view.
14036 *
14037 * <p>The framework will play sound effects for some built in actions, such as
14038 * clicking, but you may wish to play these effects in your widget,
14039 * for instance, for internal navigation.
14040 *
14041 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14042 * {@link #isSoundEffectsEnabled()} is true.
14043 *
14044 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14045 */
14046 public void playSoundEffect(int soundConstant) {
14047 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14048 return;
14049 }
14050 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14051 }
14052
14053 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014054 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014055 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014056 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014057 *
14058 * <p>The framework will provide haptic feedback for some built in actions,
14059 * such as long presses, but you may wish to provide feedback for your
14060 * own widget.
14061 *
14062 * <p>The feedback will only be performed if
14063 * {@link #isHapticFeedbackEnabled()} is true.
14064 *
14065 * @param feedbackConstant One of the constants defined in
14066 * {@link HapticFeedbackConstants}
14067 */
14068 public boolean performHapticFeedback(int feedbackConstant) {
14069 return performHapticFeedback(feedbackConstant, 0);
14070 }
14071
14072 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014073 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014074 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014075 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014076 *
14077 * @param feedbackConstant One of the constants defined in
14078 * {@link HapticFeedbackConstants}
14079 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14080 */
14081 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14082 if (mAttachInfo == null) {
14083 return false;
14084 }
Romain Guyf607bdc2010-09-10 19:20:06 -070014085 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070014086 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 && !isHapticFeedbackEnabled()) {
14088 return false;
14089 }
Romain Guy812ccbe2010-06-01 14:07:24 -070014090 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
14091 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092 }
14093
14094 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014095 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040014096 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14097 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014098 */
14099 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040014100 if (visibility != mSystemUiVisibility) {
14101 mSystemUiVisibility = visibility;
14102 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14103 mParent.recomputeViewAttributes(this);
14104 }
Joe Onorato664644d2011-01-23 17:53:23 -080014105 }
14106 }
14107
14108 /**
14109 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040014110 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14111 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014112 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080014113 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080014114 return mSystemUiVisibility;
14115 }
14116
Scott Mainec6331b2011-05-24 16:55:56 -070014117 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070014118 * Returns the current system UI visibility that is currently set for
14119 * the entire window. This is the combination of the
14120 * {@link #setSystemUiVisibility(int)} values supplied by all of the
14121 * views in the window.
14122 */
14123 public int getWindowSystemUiVisibility() {
14124 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
14125 }
14126
14127 /**
14128 * Override to find out when the window's requested system UI visibility
14129 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
14130 * This is different from the callbacks recieved through
14131 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
14132 * in that this is only telling you about the local request of the window,
14133 * not the actual values applied by the system.
14134 */
14135 public void onWindowSystemUiVisibilityChanged(int visible) {
14136 }
14137
14138 /**
14139 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
14140 * the view hierarchy.
14141 */
14142 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
14143 onWindowSystemUiVisibilityChanged(visible);
14144 }
14145
14146 /**
Scott Mainec6331b2011-05-24 16:55:56 -070014147 * Set a listener to receive callbacks when the visibility of the system bar changes.
14148 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
14149 */
Joe Onorato664644d2011-01-23 17:53:23 -080014150 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014151 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080014152 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14153 mParent.recomputeViewAttributes(this);
14154 }
14155 }
14156
14157 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014158 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
14159 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080014160 */
14161 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014162 ListenerInfo li = mListenerInfo;
14163 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
14164 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080014165 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080014166 }
14167 }
14168
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014169 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
14170 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
14171 if (val != mSystemUiVisibility) {
14172 setSystemUiVisibility(val);
14173 }
14174 }
14175
Joe Onorato664644d2011-01-23 17:53:23 -080014176 /**
Joe Malin32736f02011-01-19 16:14:20 -080014177 * Creates an image that the system displays during the drag and drop
14178 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14179 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14180 * appearance as the given View. The default also positions the center of the drag shadow
14181 * directly under the touch point. If no View is provided (the constructor with no parameters
14182 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14183 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14184 * default is an invisible drag shadow.
14185 * <p>
14186 * You are not required to use the View you provide to the constructor as the basis of the
14187 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14188 * anything you want as the drag shadow.
14189 * </p>
14190 * <p>
14191 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14192 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14193 * size and position of the drag shadow. It uses this data to construct a
14194 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14195 * so that your application can draw the shadow image in the Canvas.
14196 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014197 *
14198 * <div class="special reference">
14199 * <h3>Developer Guides</h3>
14200 * <p>For a guide to implementing drag and drop features, read the
14201 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14202 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014203 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014204 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014205 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014206
14207 /**
Joe Malin32736f02011-01-19 16:14:20 -080014208 * Constructs a shadow image builder based on a View. By default, the resulting drag
14209 * shadow will have the same appearance and dimensions as the View, with the touch point
14210 * over the center of the View.
14211 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014212 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014213 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014214 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014215 }
14216
Christopher Tate17ed60c2011-01-18 12:50:26 -080014217 /**
14218 * Construct a shadow builder object with no associated View. This
14219 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14220 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14221 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014222 * reference to any View object. If they are not overridden, then the result is an
14223 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014224 */
14225 public DragShadowBuilder() {
14226 mView = new WeakReference<View>(null);
14227 }
14228
14229 /**
14230 * Returns the View object that had been passed to the
14231 * {@link #View.DragShadowBuilder(View)}
14232 * constructor. If that View parameter was {@code null} or if the
14233 * {@link #View.DragShadowBuilder()}
14234 * constructor was used to instantiate the builder object, this method will return
14235 * null.
14236 *
14237 * @return The View object associate with this builder object.
14238 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014239 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014240 final public View getView() {
14241 return mView.get();
14242 }
14243
Christopher Tate2c095f32010-10-04 14:13:40 -070014244 /**
Joe Malin32736f02011-01-19 16:14:20 -080014245 * Provides the metrics for the shadow image. These include the dimensions of
14246 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014247 * be centered under the touch location while dragging.
14248 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014249 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014250 * same as the dimensions of the View itself and centers the shadow under
14251 * the touch point.
14252 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014253 *
Joe Malin32736f02011-01-19 16:14:20 -080014254 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14255 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14256 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14257 * image.
14258 *
14259 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14260 * shadow image that should be underneath the touch point during the drag and drop
14261 * operation. Your application must set {@link android.graphics.Point#x} to the
14262 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014263 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014264 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014265 final View view = mView.get();
14266 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014267 shadowSize.set(view.getWidth(), view.getHeight());
14268 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014269 } else {
14270 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14271 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014272 }
14273
14274 /**
Joe Malin32736f02011-01-19 16:14:20 -080014275 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14276 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014277 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014278 *
Joe Malin32736f02011-01-19 16:14:20 -080014279 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014280 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014281 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014282 final View view = mView.get();
14283 if (view != null) {
14284 view.draw(canvas);
14285 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014286 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014287 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014288 }
14289 }
14290
14291 /**
Joe Malin32736f02011-01-19 16:14:20 -080014292 * Starts a drag and drop operation. When your application calls this method, it passes a
14293 * {@link android.view.View.DragShadowBuilder} object to the system. The
14294 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14295 * to get metrics for the drag shadow, and then calls the object's
14296 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14297 * <p>
14298 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14299 * drag events to all the View objects in your application that are currently visible. It does
14300 * this either by calling the View object's drag listener (an implementation of
14301 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14302 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14303 * Both are passed a {@link android.view.DragEvent} object that has a
14304 * {@link android.view.DragEvent#getAction()} value of
14305 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14306 * </p>
14307 * <p>
14308 * Your application can invoke startDrag() on any attached View object. The View object does not
14309 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14310 * be related to the View the user selected for dragging.
14311 * </p>
14312 * @param data A {@link android.content.ClipData} object pointing to the data to be
14313 * transferred by the drag and drop operation.
14314 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14315 * drag shadow.
14316 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14317 * drop operation. This Object is put into every DragEvent object sent by the system during the
14318 * current drag.
14319 * <p>
14320 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14321 * to the target Views. For example, it can contain flags that differentiate between a
14322 * a copy operation and a move operation.
14323 * </p>
14324 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14325 * so the parameter should be set to 0.
14326 * @return {@code true} if the method completes successfully, or
14327 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14328 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014329 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014330 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014331 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014332 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014333 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014334 }
14335 boolean okay = false;
14336
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014337 Point shadowSize = new Point();
14338 Point shadowTouchPoint = new Point();
14339 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014340
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014341 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14342 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14343 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014344 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014345
Chris Tatea32dcf72010-10-14 12:13:50 -070014346 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014347 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14348 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014349 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014350 Surface surface = new Surface();
14351 try {
14352 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014353 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014354 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014355 + " surface=" + surface);
14356 if (token != null) {
14357 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014358 try {
Chris Tate6b391282010-10-14 15:48:59 -070014359 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014360 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014361 } finally {
14362 surface.unlockCanvasAndPost(canvas);
14363 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014364
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014365 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014366
14367 // Cache the local state object for delivery with DragEvents
14368 root.setLocalDragState(myLocalState);
14369
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014370 // repurpose 'shadowSize' for the last touch point
14371 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014372
Christopher Tatea53146c2010-09-07 11:57:52 -070014373 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014374 shadowSize.x, shadowSize.y,
14375 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014376 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014377
14378 // Off and running! Release our local surface instance; the drag
14379 // shadow surface is now managed by the system process.
14380 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014381 }
14382 } catch (Exception e) {
14383 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14384 surface.destroy();
14385 }
14386
14387 return okay;
14388 }
14389
Christopher Tatea53146c2010-09-07 11:57:52 -070014390 /**
Joe Malin32736f02011-01-19 16:14:20 -080014391 * Handles drag events sent by the system following a call to
14392 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14393 *<p>
14394 * When the system calls this method, it passes a
14395 * {@link android.view.DragEvent} object. A call to
14396 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14397 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14398 * operation.
14399 * @param event The {@link android.view.DragEvent} sent by the system.
14400 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14401 * in DragEvent, indicating the type of drag event represented by this object.
14402 * @return {@code true} if the method was successful, otherwise {@code false}.
14403 * <p>
14404 * The method should return {@code true} in response to an action type of
14405 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14406 * operation.
14407 * </p>
14408 * <p>
14409 * The method should also return {@code true} in response to an action type of
14410 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14411 * {@code false} if it didn't.
14412 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014413 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014414 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014415 return false;
14416 }
14417
14418 /**
Joe Malin32736f02011-01-19 16:14:20 -080014419 * Detects if this View is enabled and has a drag event listener.
14420 * If both are true, then it calls the drag event listener with the
14421 * {@link android.view.DragEvent} it received. If the drag event listener returns
14422 * {@code true}, then dispatchDragEvent() returns {@code true}.
14423 * <p>
14424 * For all other cases, the method calls the
14425 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14426 * method and returns its result.
14427 * </p>
14428 * <p>
14429 * This ensures that a drag event is always consumed, even if the View does not have a drag
14430 * event listener. However, if the View has a listener and the listener returns true, then
14431 * onDragEvent() is not called.
14432 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014433 */
14434 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014435 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014436 ListenerInfo li = mListenerInfo;
14437 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14438 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014439 return true;
14440 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014441 return onDragEvent(event);
14442 }
14443
Christopher Tate3d4bf172011-03-28 16:16:46 -070014444 boolean canAcceptDrag() {
14445 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14446 }
14447
Christopher Tatea53146c2010-09-07 11:57:52 -070014448 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014449 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14450 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014451 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014452 */
14453 public void onCloseSystemDialogs(String reason) {
14454 }
Joe Malin32736f02011-01-19 16:14:20 -080014455
Dianne Hackbornffa42482009-09-23 22:20:11 -070014456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014457 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014458 * update a Region being computed for
14459 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014460 * that any non-transparent parts of the Drawable are removed from the
14461 * given transparent region.
14462 *
14463 * @param dr The Drawable whose transparency is to be applied to the region.
14464 * @param region A Region holding the current transparency information,
14465 * where any parts of the region that are set are considered to be
14466 * transparent. On return, this region will be modified to have the
14467 * transparency information reduced by the corresponding parts of the
14468 * Drawable that are not transparent.
14469 * {@hide}
14470 */
14471 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14472 if (DBG) {
14473 Log.i("View", "Getting transparent region for: " + this);
14474 }
14475 final Region r = dr.getTransparentRegion();
14476 final Rect db = dr.getBounds();
14477 final AttachInfo attachInfo = mAttachInfo;
14478 if (r != null && attachInfo != null) {
14479 final int w = getRight()-getLeft();
14480 final int h = getBottom()-getTop();
14481 if (db.left > 0) {
14482 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14483 r.op(0, 0, db.left, h, Region.Op.UNION);
14484 }
14485 if (db.right < w) {
14486 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14487 r.op(db.right, 0, w, h, Region.Op.UNION);
14488 }
14489 if (db.top > 0) {
14490 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14491 r.op(0, 0, w, db.top, Region.Op.UNION);
14492 }
14493 if (db.bottom < h) {
14494 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14495 r.op(0, db.bottom, w, h, Region.Op.UNION);
14496 }
14497 final int[] location = attachInfo.mTransparentLocation;
14498 getLocationInWindow(location);
14499 r.translate(location[0], location[1]);
14500 region.op(r, Region.Op.INTERSECT);
14501 } else {
14502 region.op(db, Region.Op.DIFFERENCE);
14503 }
14504 }
14505
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014506 private void checkForLongClick(int delayOffset) {
14507 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14508 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014509
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014510 if (mPendingCheckForLongPress == null) {
14511 mPendingCheckForLongPress = new CheckForLongPress();
14512 }
14513 mPendingCheckForLongPress.rememberWindowAttachCount();
14514 postDelayed(mPendingCheckForLongPress,
14515 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014517 }
14518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014519 /**
14520 * Inflate a view from an XML resource. This convenience method wraps the {@link
14521 * LayoutInflater} class, which provides a full range of options for view inflation.
14522 *
14523 * @param context The Context object for your activity or application.
14524 * @param resource The resource ID to inflate
14525 * @param root A view group that will be the parent. Used to properly inflate the
14526 * layout_* parameters.
14527 * @see LayoutInflater
14528 */
14529 public static View inflate(Context context, int resource, ViewGroup root) {
14530 LayoutInflater factory = LayoutInflater.from(context);
14531 return factory.inflate(resource, root);
14532 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014534 /**
Adam Powell637d3372010-08-25 14:37:03 -070014535 * Scroll the view with standard behavior for scrolling beyond the normal
14536 * content boundaries. Views that call this method should override
14537 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14538 * results of an over-scroll operation.
14539 *
14540 * Views can use this method to handle any touch or fling-based scrolling.
14541 *
14542 * @param deltaX Change in X in pixels
14543 * @param deltaY Change in Y in pixels
14544 * @param scrollX Current X scroll value in pixels before applying deltaX
14545 * @param scrollY Current Y scroll value in pixels before applying deltaY
14546 * @param scrollRangeX Maximum content scroll range along the X axis
14547 * @param scrollRangeY Maximum content scroll range along the Y axis
14548 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14549 * along the X axis.
14550 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14551 * along the Y axis.
14552 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14553 * @return true if scrolling was clamped to an over-scroll boundary along either
14554 * axis, false otherwise.
14555 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014556 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014557 protected boolean overScrollBy(int deltaX, int deltaY,
14558 int scrollX, int scrollY,
14559 int scrollRangeX, int scrollRangeY,
14560 int maxOverScrollX, int maxOverScrollY,
14561 boolean isTouchEvent) {
14562 final int overScrollMode = mOverScrollMode;
14563 final boolean canScrollHorizontal =
14564 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14565 final boolean canScrollVertical =
14566 computeVerticalScrollRange() > computeVerticalScrollExtent();
14567 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14568 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14569 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14570 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14571
14572 int newScrollX = scrollX + deltaX;
14573 if (!overScrollHorizontal) {
14574 maxOverScrollX = 0;
14575 }
14576
14577 int newScrollY = scrollY + deltaY;
14578 if (!overScrollVertical) {
14579 maxOverScrollY = 0;
14580 }
14581
14582 // Clamp values if at the limits and record
14583 final int left = -maxOverScrollX;
14584 final int right = maxOverScrollX + scrollRangeX;
14585 final int top = -maxOverScrollY;
14586 final int bottom = maxOverScrollY + scrollRangeY;
14587
14588 boolean clampedX = false;
14589 if (newScrollX > right) {
14590 newScrollX = right;
14591 clampedX = true;
14592 } else if (newScrollX < left) {
14593 newScrollX = left;
14594 clampedX = true;
14595 }
14596
14597 boolean clampedY = false;
14598 if (newScrollY > bottom) {
14599 newScrollY = bottom;
14600 clampedY = true;
14601 } else if (newScrollY < top) {
14602 newScrollY = top;
14603 clampedY = true;
14604 }
14605
14606 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14607
14608 return clampedX || clampedY;
14609 }
14610
14611 /**
14612 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14613 * respond to the results of an over-scroll operation.
14614 *
14615 * @param scrollX New X scroll value in pixels
14616 * @param scrollY New Y scroll value in pixels
14617 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14618 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14619 */
14620 protected void onOverScrolled(int scrollX, int scrollY,
14621 boolean clampedX, boolean clampedY) {
14622 // Intentionally empty.
14623 }
14624
14625 /**
14626 * Returns the over-scroll mode for this view. The result will be
14627 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14628 * (allow over-scrolling only if the view content is larger than the container),
14629 * or {@link #OVER_SCROLL_NEVER}.
14630 *
14631 * @return This view's over-scroll mode.
14632 */
14633 public int getOverScrollMode() {
14634 return mOverScrollMode;
14635 }
14636
14637 /**
14638 * Set the over-scroll mode for this view. Valid over-scroll modes are
14639 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14640 * (allow over-scrolling only if the view content is larger than the container),
14641 * or {@link #OVER_SCROLL_NEVER}.
14642 *
14643 * Setting the over-scroll mode of a view will have an effect only if the
14644 * view is capable of scrolling.
14645 *
14646 * @param overScrollMode The new over-scroll mode for this view.
14647 */
14648 public void setOverScrollMode(int overScrollMode) {
14649 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14650 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14651 overScrollMode != OVER_SCROLL_NEVER) {
14652 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14653 }
14654 mOverScrollMode = overScrollMode;
14655 }
14656
14657 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014658 * Gets a scale factor that determines the distance the view should scroll
14659 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14660 * @return The vertical scroll scale factor.
14661 * @hide
14662 */
14663 protected float getVerticalScrollFactor() {
14664 if (mVerticalScrollFactor == 0) {
14665 TypedValue outValue = new TypedValue();
14666 if (!mContext.getTheme().resolveAttribute(
14667 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14668 throw new IllegalStateException(
14669 "Expected theme to define listPreferredItemHeight.");
14670 }
14671 mVerticalScrollFactor = outValue.getDimension(
14672 mContext.getResources().getDisplayMetrics());
14673 }
14674 return mVerticalScrollFactor;
14675 }
14676
14677 /**
14678 * Gets a scale factor that determines the distance the view should scroll
14679 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14680 * @return The horizontal scroll scale factor.
14681 * @hide
14682 */
14683 protected float getHorizontalScrollFactor() {
14684 // TODO: Should use something else.
14685 return getVerticalScrollFactor();
14686 }
14687
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014688 /**
14689 * Return the value specifying the text direction or policy that was set with
14690 * {@link #setTextDirection(int)}.
14691 *
14692 * @return the defined text direction. It can be one of:
14693 *
14694 * {@link #TEXT_DIRECTION_INHERIT},
14695 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14696 * {@link #TEXT_DIRECTION_ANY_RTL},
14697 * {@link #TEXT_DIRECTION_LTR},
14698 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014699 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014700 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014701 @ViewDebug.ExportedProperty(category = "text", mapping = {
14702 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
14703 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
14704 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
14705 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
14706 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
14707 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
14708 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014709 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014710 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014711 }
14712
14713 /**
14714 * Set the text direction.
14715 *
14716 * @param textDirection the direction to set. Should be one of:
14717 *
14718 * {@link #TEXT_DIRECTION_INHERIT},
14719 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14720 * {@link #TEXT_DIRECTION_ANY_RTL},
14721 * {@link #TEXT_DIRECTION_LTR},
14722 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014723 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014724 */
14725 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014726 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070014727 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014728 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070014729 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014730 // Set the new text direction
14731 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014732 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070014733 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014734 }
14735 }
14736
14737 /**
14738 * Return the resolved text direction.
14739 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014740 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
14741 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
14742 * up the parent chain of the view. if there is no parent, then it will return the default
14743 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
14744 *
14745 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014746 *
Doug Feltcb3791202011-07-07 11:57:48 -070014747 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14748 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014749 * {@link #TEXT_DIRECTION_LTR},
14750 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014751 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014752 */
14753 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070014754 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014755 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014756 resolveTextDirection();
14757 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014758 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014759 }
14760
14761 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014762 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14763 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014764 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014765 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014766 // Reset any previous text direction resolution
14767 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
14768
14769 // Set resolved text direction flag depending on text direction flag
14770 final int textDirection = getTextDirection();
14771 switch(textDirection) {
14772 case TEXT_DIRECTION_INHERIT:
14773 if (canResolveTextDirection()) {
14774 ViewGroup viewGroup = ((ViewGroup) mParent);
14775
14776 // Set current resolved direction to the same value as the parent's one
14777 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
14778 switch (parentResolvedDirection) {
14779 case TEXT_DIRECTION_FIRST_STRONG:
14780 case TEXT_DIRECTION_ANY_RTL:
14781 case TEXT_DIRECTION_LTR:
14782 case TEXT_DIRECTION_RTL:
14783 case TEXT_DIRECTION_LOCALE:
14784 mPrivateFlags2 |=
14785 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
14786 break;
14787 default:
14788 // Default resolved direction is "first strong" heuristic
14789 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
14790 }
14791 } else {
14792 // We cannot do the resolution if there is no parent, so use the default one
14793 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
14794 }
14795 break;
14796 case TEXT_DIRECTION_FIRST_STRONG:
14797 case TEXT_DIRECTION_ANY_RTL:
14798 case TEXT_DIRECTION_LTR:
14799 case TEXT_DIRECTION_RTL:
14800 case TEXT_DIRECTION_LOCALE:
14801 // Resolved direction is the same as text direction
14802 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
14803 break;
14804 default:
14805 // Default resolved direction is "first strong" heuristic
14806 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014807 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014808
14809 // Set to resolved
14810 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014811 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014812 }
14813
14814 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014815 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014816 * resolution should override this method.
14817 *
14818 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014819 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014820 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014821 }
14822
14823 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014824 * Check if text direction resolution can be done.
14825 *
14826 * @return true if text direction resolution can be done otherwise return false.
14827 */
14828 public boolean canResolveTextDirection() {
14829 switch (getTextDirection()) {
14830 case TEXT_DIRECTION_INHERIT:
14831 return (mParent != null) && (mParent instanceof ViewGroup);
14832 default:
14833 return true;
14834 }
14835 }
14836
14837 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014838 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014839 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014840 * reset is done.
14841 */
14842 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014843 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014844 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014845 }
14846
14847 /**
14848 * Called when text direction is reset. Subclasses that care about text direction reset should
14849 * override this method and do a reset of the text direction of their children. The default
14850 * implementation does nothing.
14851 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014852 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014853 }
14854
Chet Haaseb39f0512011-05-24 14:36:40 -070014855 //
14856 // Properties
14857 //
14858 /**
14859 * A Property wrapper around the <code>alpha</code> functionality handled by the
14860 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14861 */
Chet Haased47f1532011-12-16 11:18:52 -080014862 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014863 @Override
14864 public void setValue(View object, float value) {
14865 object.setAlpha(value);
14866 }
14867
14868 @Override
14869 public Float get(View object) {
14870 return object.getAlpha();
14871 }
14872 };
14873
14874 /**
14875 * A Property wrapper around the <code>translationX</code> functionality handled by the
14876 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14877 */
Chet Haased47f1532011-12-16 11:18:52 -080014878 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014879 @Override
14880 public void setValue(View object, float value) {
14881 object.setTranslationX(value);
14882 }
14883
14884 @Override
14885 public Float get(View object) {
14886 return object.getTranslationX();
14887 }
14888 };
14889
14890 /**
14891 * A Property wrapper around the <code>translationY</code> functionality handled by the
14892 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14893 */
Chet Haased47f1532011-12-16 11:18:52 -080014894 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014895 @Override
14896 public void setValue(View object, float value) {
14897 object.setTranslationY(value);
14898 }
14899
14900 @Override
14901 public Float get(View object) {
14902 return object.getTranslationY();
14903 }
14904 };
14905
14906 /**
14907 * A Property wrapper around the <code>x</code> functionality handled by the
14908 * {@link View#setX(float)} and {@link View#getX()} methods.
14909 */
Chet Haased47f1532011-12-16 11:18:52 -080014910 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014911 @Override
14912 public void setValue(View object, float value) {
14913 object.setX(value);
14914 }
14915
14916 @Override
14917 public Float get(View object) {
14918 return object.getX();
14919 }
14920 };
14921
14922 /**
14923 * A Property wrapper around the <code>y</code> functionality handled by the
14924 * {@link View#setY(float)} and {@link View#getY()} methods.
14925 */
Chet Haased47f1532011-12-16 11:18:52 -080014926 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014927 @Override
14928 public void setValue(View object, float value) {
14929 object.setY(value);
14930 }
14931
14932 @Override
14933 public Float get(View object) {
14934 return object.getY();
14935 }
14936 };
14937
14938 /**
14939 * A Property wrapper around the <code>rotation</code> functionality handled by the
14940 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14941 */
Chet Haased47f1532011-12-16 11:18:52 -080014942 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014943 @Override
14944 public void setValue(View object, float value) {
14945 object.setRotation(value);
14946 }
14947
14948 @Override
14949 public Float get(View object) {
14950 return object.getRotation();
14951 }
14952 };
14953
14954 /**
14955 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14956 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14957 */
Chet Haased47f1532011-12-16 11:18:52 -080014958 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014959 @Override
14960 public void setValue(View object, float value) {
14961 object.setRotationX(value);
14962 }
14963
14964 @Override
14965 public Float get(View object) {
14966 return object.getRotationX();
14967 }
14968 };
14969
14970 /**
14971 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14972 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14973 */
Chet Haased47f1532011-12-16 11:18:52 -080014974 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014975 @Override
14976 public void setValue(View object, float value) {
14977 object.setRotationY(value);
14978 }
14979
14980 @Override
14981 public Float get(View object) {
14982 return object.getRotationY();
14983 }
14984 };
14985
14986 /**
14987 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14988 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14989 */
Chet Haased47f1532011-12-16 11:18:52 -080014990 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014991 @Override
14992 public void setValue(View object, float value) {
14993 object.setScaleX(value);
14994 }
14995
14996 @Override
14997 public Float get(View object) {
14998 return object.getScaleX();
14999 }
15000 };
15001
15002 /**
15003 * A Property wrapper around the <code>scaleY</code> functionality handled by the
15004 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
15005 */
Chet Haased47f1532011-12-16 11:18:52 -080015006 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015007 @Override
15008 public void setValue(View object, float value) {
15009 object.setScaleY(value);
15010 }
15011
15012 @Override
15013 public Float get(View object) {
15014 return object.getScaleY();
15015 }
15016 };
15017
Jeff Brown33bbfd22011-02-24 20:55:35 -080015018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015019 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
15020 * Each MeasureSpec represents a requirement for either the width or the height.
15021 * A MeasureSpec is comprised of a size and a mode. There are three possible
15022 * modes:
15023 * <dl>
15024 * <dt>UNSPECIFIED</dt>
15025 * <dd>
15026 * The parent has not imposed any constraint on the child. It can be whatever size
15027 * it wants.
15028 * </dd>
15029 *
15030 * <dt>EXACTLY</dt>
15031 * <dd>
15032 * The parent has determined an exact size for the child. The child is going to be
15033 * given those bounds regardless of how big it wants to be.
15034 * </dd>
15035 *
15036 * <dt>AT_MOST</dt>
15037 * <dd>
15038 * The child can be as large as it wants up to the specified size.
15039 * </dd>
15040 * </dl>
15041 *
15042 * MeasureSpecs are implemented as ints to reduce object allocation. This class
15043 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
15044 */
15045 public static class MeasureSpec {
15046 private static final int MODE_SHIFT = 30;
15047 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
15048
15049 /**
15050 * Measure specification mode: The parent has not imposed any constraint
15051 * on the child. It can be whatever size it wants.
15052 */
15053 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
15054
15055 /**
15056 * Measure specification mode: The parent has determined an exact size
15057 * for the child. The child is going to be given those bounds regardless
15058 * of how big it wants to be.
15059 */
15060 public static final int EXACTLY = 1 << MODE_SHIFT;
15061
15062 /**
15063 * Measure specification mode: The child can be as large as it wants up
15064 * to the specified size.
15065 */
15066 public static final int AT_MOST = 2 << MODE_SHIFT;
15067
15068 /**
15069 * Creates a measure specification based on the supplied size and mode.
15070 *
15071 * The mode must always be one of the following:
15072 * <ul>
15073 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
15074 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
15075 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
15076 * </ul>
15077 *
15078 * @param size the size of the measure specification
15079 * @param mode the mode of the measure specification
15080 * @return the measure specification based on size and mode
15081 */
15082 public static int makeMeasureSpec(int size, int mode) {
15083 return size + mode;
15084 }
15085
15086 /**
15087 * Extracts the mode from the supplied measure specification.
15088 *
15089 * @param measureSpec the measure specification to extract the mode from
15090 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
15091 * {@link android.view.View.MeasureSpec#AT_MOST} or
15092 * {@link android.view.View.MeasureSpec#EXACTLY}
15093 */
15094 public static int getMode(int measureSpec) {
15095 return (measureSpec & MODE_MASK);
15096 }
15097
15098 /**
15099 * Extracts the size from the supplied measure specification.
15100 *
15101 * @param measureSpec the measure specification to extract the size from
15102 * @return the size in pixels defined in the supplied measure specification
15103 */
15104 public static int getSize(int measureSpec) {
15105 return (measureSpec & ~MODE_MASK);
15106 }
15107
15108 /**
15109 * Returns a String representation of the specified measure
15110 * specification.
15111 *
15112 * @param measureSpec the measure specification to convert to a String
15113 * @return a String with the following format: "MeasureSpec: MODE SIZE"
15114 */
15115 public static String toString(int measureSpec) {
15116 int mode = getMode(measureSpec);
15117 int size = getSize(measureSpec);
15118
15119 StringBuilder sb = new StringBuilder("MeasureSpec: ");
15120
15121 if (mode == UNSPECIFIED)
15122 sb.append("UNSPECIFIED ");
15123 else if (mode == EXACTLY)
15124 sb.append("EXACTLY ");
15125 else if (mode == AT_MOST)
15126 sb.append("AT_MOST ");
15127 else
15128 sb.append(mode).append(" ");
15129
15130 sb.append(size);
15131 return sb.toString();
15132 }
15133 }
15134
15135 class CheckForLongPress implements Runnable {
15136
15137 private int mOriginalWindowAttachCount;
15138
15139 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070015140 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015141 && mOriginalWindowAttachCount == mWindowAttachCount) {
15142 if (performLongClick()) {
15143 mHasPerformedLongPress = true;
15144 }
15145 }
15146 }
15147
15148 public void rememberWindowAttachCount() {
15149 mOriginalWindowAttachCount = mWindowAttachCount;
15150 }
15151 }
Joe Malin32736f02011-01-19 16:14:20 -080015152
Adam Powelle14579b2009-12-16 18:39:52 -080015153 private final class CheckForTap implements Runnable {
15154 public void run() {
15155 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080015156 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015157 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080015158 }
15159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015160
Adam Powella35d7682010-03-12 14:48:13 -080015161 private final class PerformClick implements Runnable {
15162 public void run() {
15163 performClick();
15164 }
15165 }
15166
Dianne Hackborn63042d62011-01-26 18:56:29 -080015167 /** @hide */
15168 public void hackTurnOffWindowResizeAnim(boolean off) {
15169 mAttachInfo.mTurnOffWindowResizeAnim = off;
15170 }
Joe Malin32736f02011-01-19 16:14:20 -080015171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015172 /**
Chet Haasea00f3862011-02-22 06:34:40 -080015173 * This method returns a ViewPropertyAnimator object, which can be used to animate
15174 * specific properties on this View.
15175 *
15176 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15177 */
15178 public ViewPropertyAnimator animate() {
15179 if (mAnimator == null) {
15180 mAnimator = new ViewPropertyAnimator(this);
15181 }
15182 return mAnimator;
15183 }
15184
15185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015186 * Interface definition for a callback to be invoked when a key event is
15187 * dispatched to this view. The callback will be invoked before the key
15188 * event is given to the view.
15189 */
15190 public interface OnKeyListener {
15191 /**
15192 * Called when a key is dispatched to a view. This allows listeners to
15193 * get a chance to respond before the target view.
15194 *
15195 * @param v The view the key has been dispatched to.
15196 * @param keyCode The code for the physical key that was pressed
15197 * @param event The KeyEvent object containing full information about
15198 * the event.
15199 * @return True if the listener has consumed the event, false otherwise.
15200 */
15201 boolean onKey(View v, int keyCode, KeyEvent event);
15202 }
15203
15204 /**
15205 * Interface definition for a callback to be invoked when a touch event is
15206 * dispatched to this view. The callback will be invoked before the touch
15207 * event is given to the view.
15208 */
15209 public interface OnTouchListener {
15210 /**
15211 * Called when a touch event is dispatched to a view. This allows listeners to
15212 * get a chance to respond before the target view.
15213 *
15214 * @param v The view the touch event has been dispatched to.
15215 * @param event The MotionEvent object containing full information about
15216 * the event.
15217 * @return True if the listener has consumed the event, false otherwise.
15218 */
15219 boolean onTouch(View v, MotionEvent event);
15220 }
15221
15222 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015223 * Interface definition for a callback to be invoked when a hover event is
15224 * dispatched to this view. The callback will be invoked before the hover
15225 * event is given to the view.
15226 */
15227 public interface OnHoverListener {
15228 /**
15229 * Called when a hover event is dispatched to a view. This allows listeners to
15230 * get a chance to respond before the target view.
15231 *
15232 * @param v The view the hover event has been dispatched to.
15233 * @param event The MotionEvent object containing full information about
15234 * the event.
15235 * @return True if the listener has consumed the event, false otherwise.
15236 */
15237 boolean onHover(View v, MotionEvent event);
15238 }
15239
15240 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015241 * Interface definition for a callback to be invoked when a generic motion event is
15242 * dispatched to this view. The callback will be invoked before the generic motion
15243 * event is given to the view.
15244 */
15245 public interface OnGenericMotionListener {
15246 /**
15247 * Called when a generic motion event is dispatched to a view. This allows listeners to
15248 * get a chance to respond before the target view.
15249 *
15250 * @param v The view the generic motion event has been dispatched to.
15251 * @param event The MotionEvent object containing full information about
15252 * the event.
15253 * @return True if the listener has consumed the event, false otherwise.
15254 */
15255 boolean onGenericMotion(View v, MotionEvent event);
15256 }
15257
15258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015259 * Interface definition for a callback to be invoked when a view has been clicked and held.
15260 */
15261 public interface OnLongClickListener {
15262 /**
15263 * Called when a view has been clicked and held.
15264 *
15265 * @param v The view that was clicked and held.
15266 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015267 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015268 */
15269 boolean onLongClick(View v);
15270 }
15271
15272 /**
Chris Tate32affef2010-10-18 15:29:21 -070015273 * Interface definition for a callback to be invoked when a drag is being dispatched
15274 * to this view. The callback will be invoked before the hosting view's own
15275 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15276 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015277 *
15278 * <div class="special reference">
15279 * <h3>Developer Guides</h3>
15280 * <p>For a guide to implementing drag and drop features, read the
15281 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15282 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015283 */
15284 public interface OnDragListener {
15285 /**
15286 * Called when a drag event is dispatched to a view. This allows listeners
15287 * to get a chance to override base View behavior.
15288 *
Joe Malin32736f02011-01-19 16:14:20 -080015289 * @param v The View that received the drag event.
15290 * @param event The {@link android.view.DragEvent} object for the drag event.
15291 * @return {@code true} if the drag event was handled successfully, or {@code false}
15292 * if the drag event was not handled. Note that {@code false} will trigger the View
15293 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015294 */
15295 boolean onDrag(View v, DragEvent event);
15296 }
15297
15298 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015299 * Interface definition for a callback to be invoked when the focus state of
15300 * a view changed.
15301 */
15302 public interface OnFocusChangeListener {
15303 /**
15304 * Called when the focus state of a view has changed.
15305 *
15306 * @param v The view whose state has changed.
15307 * @param hasFocus The new focus state of v.
15308 */
15309 void onFocusChange(View v, boolean hasFocus);
15310 }
15311
15312 /**
15313 * Interface definition for a callback to be invoked when a view is clicked.
15314 */
15315 public interface OnClickListener {
15316 /**
15317 * Called when a view has been clicked.
15318 *
15319 * @param v The view that was clicked.
15320 */
15321 void onClick(View v);
15322 }
15323
15324 /**
15325 * Interface definition for a callback to be invoked when the context menu
15326 * for this view is being built.
15327 */
15328 public interface OnCreateContextMenuListener {
15329 /**
15330 * Called when the context menu for this view is being built. It is not
15331 * safe to hold onto the menu after this method returns.
15332 *
15333 * @param menu The context menu that is being built
15334 * @param v The view for which the context menu is being built
15335 * @param menuInfo Extra information about the item for which the
15336 * context menu should be shown. This information will vary
15337 * depending on the class of v.
15338 */
15339 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15340 }
15341
Joe Onorato664644d2011-01-23 17:53:23 -080015342 /**
15343 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015344 * visibility. This reports <strong>global</strong> changes to the system UI
15345 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015346 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070015347 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015348 */
15349 public interface OnSystemUiVisibilityChangeListener {
15350 /**
15351 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015352 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015353 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015354 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015355 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15356 * <strong>global</strong> state of the UI visibility flags, not what your
15357 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015358 */
15359 public void onSystemUiVisibilityChange(int visibility);
15360 }
15361
Adam Powell4afd62b2011-02-18 15:02:18 -080015362 /**
15363 * Interface definition for a callback to be invoked when this view is attached
15364 * or detached from its window.
15365 */
15366 public interface OnAttachStateChangeListener {
15367 /**
15368 * Called when the view is attached to a window.
15369 * @param v The view that was attached
15370 */
15371 public void onViewAttachedToWindow(View v);
15372 /**
15373 * Called when the view is detached from a window.
15374 * @param v The view that was detached
15375 */
15376 public void onViewDetachedFromWindow(View v);
15377 }
15378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015379 private final class UnsetPressedState implements Runnable {
15380 public void run() {
15381 setPressed(false);
15382 }
15383 }
15384
15385 /**
15386 * Base class for derived classes that want to save and restore their own
15387 * state in {@link android.view.View#onSaveInstanceState()}.
15388 */
15389 public static class BaseSavedState extends AbsSavedState {
15390 /**
15391 * Constructor used when reading from a parcel. Reads the state of the superclass.
15392 *
15393 * @param source
15394 */
15395 public BaseSavedState(Parcel source) {
15396 super(source);
15397 }
15398
15399 /**
15400 * Constructor called by derived classes when creating their SavedState objects
15401 *
15402 * @param superState The state of the superclass of this view
15403 */
15404 public BaseSavedState(Parcelable superState) {
15405 super(superState);
15406 }
15407
15408 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15409 new Parcelable.Creator<BaseSavedState>() {
15410 public BaseSavedState createFromParcel(Parcel in) {
15411 return new BaseSavedState(in);
15412 }
15413
15414 public BaseSavedState[] newArray(int size) {
15415 return new BaseSavedState[size];
15416 }
15417 };
15418 }
15419
15420 /**
15421 * A set of information given to a view when it is attached to its parent
15422 * window.
15423 */
15424 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015425 interface Callbacks {
15426 void playSoundEffect(int effectId);
15427 boolean performHapticFeedback(int effectId, boolean always);
15428 }
15429
15430 /**
15431 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
15432 * to a Handler. This class contains the target (View) to invalidate and
15433 * the coordinates of the dirty rectangle.
15434 *
15435 * For performance purposes, this class also implements a pool of up to
15436 * POOL_LIMIT objects that get reused. This reduces memory allocations
15437 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015438 */
Romain Guyd928d682009-03-31 17:52:16 -070015439 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015440 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070015441 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
15442 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070015443 public InvalidateInfo newInstance() {
15444 return new InvalidateInfo();
15445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015446
Romain Guyd928d682009-03-31 17:52:16 -070015447 public void onAcquired(InvalidateInfo element) {
15448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015449
Romain Guyd928d682009-03-31 17:52:16 -070015450 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070015451 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070015452 }
15453 }, POOL_LIMIT)
15454 );
15455
15456 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015457 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015458
15459 View target;
15460
15461 int left;
15462 int top;
15463 int right;
15464 int bottom;
15465
Romain Guyd928d682009-03-31 17:52:16 -070015466 public void setNextPoolable(InvalidateInfo element) {
15467 mNext = element;
15468 }
15469
15470 public InvalidateInfo getNextPoolable() {
15471 return mNext;
15472 }
15473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015474 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070015475 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015476 }
15477
15478 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070015479 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015480 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015481
15482 public boolean isPooled() {
15483 return mIsPooled;
15484 }
15485
15486 public void setPooled(boolean isPooled) {
15487 mIsPooled = isPooled;
15488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015489 }
15490
15491 final IWindowSession mSession;
15492
15493 final IWindow mWindow;
15494
15495 final IBinder mWindowToken;
15496
15497 final Callbacks mRootCallbacks;
15498
Romain Guy59a12ca2011-06-09 17:48:21 -070015499 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080015500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015501 /**
15502 * The top view of the hierarchy.
15503 */
15504 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070015505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015506 IBinder mPanelParentWindowToken;
15507 Surface mSurface;
15508
Romain Guyb051e892010-09-28 19:09:36 -070015509 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080015510 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070015511 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080015512
Romain Guy7e4e5612012-03-05 14:37:29 -080015513 boolean mScreenOn;
15514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 /**
Romain Guy8506ab42009-06-11 17:35:47 -070015516 * Scale factor used by the compatibility mode
15517 */
15518 float mApplicationScale;
15519
15520 /**
15521 * Indicates whether the application is in compatibility mode
15522 */
15523 boolean mScalingRequired;
15524
15525 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015526 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080015527 */
15528 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080015529
Dianne Hackborn63042d62011-01-26 18:56:29 -080015530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015531 * Left position of this view's window
15532 */
15533 int mWindowLeft;
15534
15535 /**
15536 * Top position of this view's window
15537 */
15538 int mWindowTop;
15539
15540 /**
Adam Powell26153a32010-11-08 15:22:27 -080015541 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070015542 */
Adam Powell26153a32010-11-08 15:22:27 -080015543 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015544
15545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015546 * For windows that are full-screen but using insets to layout inside
15547 * of the screen decorations, these are the current insets for the
15548 * content of the window.
15549 */
15550 final Rect mContentInsets = new Rect();
15551
15552 /**
15553 * For windows that are full-screen but using insets to layout inside
15554 * of the screen decorations, these are the current insets for the
15555 * actual visible parts of the window.
15556 */
15557 final Rect mVisibleInsets = new Rect();
15558
15559 /**
15560 * The internal insets given by this window. This value is
15561 * supplied by the client (through
15562 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15563 * be given to the window manager when changed to be used in laying
15564 * out windows behind it.
15565 */
15566 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15567 = new ViewTreeObserver.InternalInsetsInfo();
15568
15569 /**
15570 * All views in the window's hierarchy that serve as scroll containers,
15571 * used to determine if the window can be resized or must be panned
15572 * to adjust for a soft input area.
15573 */
15574 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15575
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015576 final KeyEvent.DispatcherState mKeyDispatchState
15577 = new KeyEvent.DispatcherState();
15578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015579 /**
15580 * Indicates whether the view's window currently has the focus.
15581 */
15582 boolean mHasWindowFocus;
15583
15584 /**
15585 * The current visibility of the window.
15586 */
15587 int mWindowVisibility;
15588
15589 /**
15590 * Indicates the time at which drawing started to occur.
15591 */
15592 long mDrawingTime;
15593
15594 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015595 * Indicates whether or not ignoring the DIRTY_MASK flags.
15596 */
15597 boolean mIgnoreDirtyState;
15598
15599 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015600 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15601 * to avoid clearing that flag prematurely.
15602 */
15603 boolean mSetIgnoreDirtyState = false;
15604
15605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015606 * Indicates whether the view's window is currently in touch mode.
15607 */
15608 boolean mInTouchMode;
15609
15610 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015611 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015612 * the next time it performs a traversal
15613 */
15614 boolean mRecomputeGlobalAttributes;
15615
15616 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015617 * Always report new attributes at next traversal.
15618 */
15619 boolean mForceReportNewAttributes;
15620
15621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015622 * Set during a traveral if any views want to keep the screen on.
15623 */
15624 boolean mKeepScreenOn;
15625
15626 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015627 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15628 */
15629 int mSystemUiVisibility;
15630
15631 /**
15632 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15633 * attached.
15634 */
15635 boolean mHasSystemUiListeners;
15636
15637 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015638 * Set if the visibility of any views has changed.
15639 */
15640 boolean mViewVisibilityChanged;
15641
15642 /**
15643 * Set to true if a view has been scrolled.
15644 */
15645 boolean mViewScrollChanged;
15646
15647 /**
15648 * Global to the view hierarchy used as a temporary for dealing with
15649 * x/y points in the transparent region computations.
15650 */
15651 final int[] mTransparentLocation = new int[2];
15652
15653 /**
15654 * Global to the view hierarchy used as a temporary for dealing with
15655 * x/y points in the ViewGroup.invalidateChild implementation.
15656 */
15657 final int[] mInvalidateChildLocation = new int[2];
15658
Chet Haasec3aa3612010-06-17 08:50:37 -070015659
15660 /**
15661 * Global to the view hierarchy used as a temporary for dealing with
15662 * x/y location when view is transformed.
15663 */
15664 final float[] mTmpTransformLocation = new float[2];
15665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015666 /**
15667 * The view tree observer used to dispatch global events like
15668 * layout, pre-draw, touch mode change, etc.
15669 */
15670 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15671
15672 /**
15673 * A Canvas used by the view hierarchy to perform bitmap caching.
15674 */
15675 Canvas mCanvas;
15676
15677 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015678 * The view root impl.
15679 */
15680 final ViewRootImpl mViewRootImpl;
15681
15682 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015683 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015684 * handler can be used to pump events in the UI events queue.
15685 */
15686 final Handler mHandler;
15687
15688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015689 * Temporary for use in computing invalidate rectangles while
15690 * calling up the hierarchy.
15691 */
15692 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015693
15694 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015695 * Temporary for use in computing hit areas with transformed views
15696 */
15697 final RectF mTmpTransformRect = new RectF();
15698
15699 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015700 * Temporary list for use in collecting focusable descendents of a view.
15701 */
15702 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015704 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015705 * The id of the window for accessibility purposes.
15706 */
15707 int mAccessibilityWindowId = View.NO_ID;
15708
15709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015710 * Creates a new set of attachment information with the specified
15711 * events handler and thread.
15712 *
15713 * @param handler the events handler the view must use
15714 */
15715 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015716 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015717 mSession = session;
15718 mWindow = window;
15719 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015720 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015721 mHandler = handler;
15722 mRootCallbacks = effectPlayer;
15723 }
15724 }
15725
15726 /**
15727 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15728 * is supported. This avoids keeping too many unused fields in most
15729 * instances of View.</p>
15730 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015731 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015732
Mike Cleronf116bf82009-09-27 19:14:12 -070015733 /**
15734 * Scrollbars are not visible
15735 */
15736 public static final int OFF = 0;
15737
15738 /**
15739 * Scrollbars are visible
15740 */
15741 public static final int ON = 1;
15742
15743 /**
15744 * Scrollbars are fading away
15745 */
15746 public static final int FADING = 2;
15747
15748 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015750 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015751 public int scrollBarDefaultDelayBeforeFade;
15752 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015753
15754 public int scrollBarSize;
15755 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015756 public float[] interpolatorValues;
15757 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015758
15759 public final Paint paint;
15760 public final Matrix matrix;
15761 public Shader shader;
15762
Mike Cleronf116bf82009-09-27 19:14:12 -070015763 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15764
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015765 private static final float[] OPAQUE = { 255 };
15766 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015767
Mike Cleronf116bf82009-09-27 19:14:12 -070015768 /**
15769 * When fading should start. This time moves into the future every time
15770 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15771 */
15772 public long fadeStartTime;
15773
15774
15775 /**
15776 * The current state of the scrollbars: ON, OFF, or FADING
15777 */
15778 public int state = OFF;
15779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015780 private int mLastColor;
15781
Mike Cleronf116bf82009-09-27 19:14:12 -070015782 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015783 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15784 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015785 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15786 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015787
15788 paint = new Paint();
15789 matrix = new Matrix();
15790 // use use a height of 1, and then wack the matrix each time we
15791 // actually use it.
15792 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015794 paint.setShader(shader);
15795 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015796 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015797 }
Romain Guy8506ab42009-06-11 17:35:47 -070015798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015799 public void setFadeColor(int color) {
15800 if (color != 0 && color != mLastColor) {
15801 mLastColor = color;
15802 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015803
Romain Guye55e1a72009-08-27 10:42:26 -070015804 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15805 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015807 paint.setShader(shader);
15808 // Restore the default transfer mode (src_over)
15809 paint.setXfermode(null);
15810 }
15811 }
Joe Malin32736f02011-01-19 16:14:20 -080015812
Mike Cleronf116bf82009-09-27 19:14:12 -070015813 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015814 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015815 if (now >= fadeStartTime) {
15816
15817 // the animation fades the scrollbars out by changing
15818 // the opacity (alpha) from fully opaque to fully
15819 // transparent
15820 int nextFrame = (int) now;
15821 int framesCount = 0;
15822
15823 Interpolator interpolator = scrollBarInterpolator;
15824
15825 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015826 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015827
15828 // End transparent
15829 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015830 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015831
15832 state = FADING;
15833
15834 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015835 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015836 }
15837 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015838 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015839
Svetoslav Ganova0156172011-06-26 17:55:44 -070015840 /**
15841 * Resuable callback for sending
15842 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15843 */
15844 private class SendViewScrolledAccessibilityEvent implements Runnable {
15845 public volatile boolean mIsPending;
15846
15847 public void run() {
15848 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15849 mIsPending = false;
15850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015851 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015852
15853 /**
15854 * <p>
15855 * This class represents a delegate that can be registered in a {@link View}
15856 * to enhance accessibility support via composition rather via inheritance.
15857 * It is specifically targeted to widget developers that extend basic View
15858 * classes i.e. classes in package android.view, that would like their
15859 * applications to be backwards compatible.
15860 * </p>
15861 * <p>
15862 * A scenario in which a developer would like to use an accessibility delegate
15863 * is overriding a method introduced in a later API version then the minimal API
15864 * version supported by the application. For example, the method
15865 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15866 * in API version 4 when the accessibility APIs were first introduced. If a
15867 * developer would like his application to run on API version 4 devices (assuming
15868 * all other APIs used by the application are version 4 or lower) and take advantage
15869 * of this method, instead of overriding the method which would break the application's
15870 * backwards compatibility, he can override the corresponding method in this
15871 * delegate and register the delegate in the target View if the API version of
15872 * the system is high enough i.e. the API version is same or higher to the API
15873 * version that introduced
15874 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15875 * </p>
15876 * <p>
15877 * Here is an example implementation:
15878 * </p>
15879 * <code><pre><p>
15880 * if (Build.VERSION.SDK_INT >= 14) {
15881 * // If the API version is equal of higher than the version in
15882 * // which onInitializeAccessibilityNodeInfo was introduced we
15883 * // register a delegate with a customized implementation.
15884 * View view = findViewById(R.id.view_id);
15885 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15886 * public void onInitializeAccessibilityNodeInfo(View host,
15887 * AccessibilityNodeInfo info) {
15888 * // Let the default implementation populate the info.
15889 * super.onInitializeAccessibilityNodeInfo(host, info);
15890 * // Set some other information.
15891 * info.setEnabled(host.isEnabled());
15892 * }
15893 * });
15894 * }
15895 * </code></pre></p>
15896 * <p>
15897 * This delegate contains methods that correspond to the accessibility methods
15898 * in View. If a delegate has been specified the implementation in View hands
15899 * off handling to the corresponding method in this delegate. The default
15900 * implementation the delegate methods behaves exactly as the corresponding
15901 * method in View for the case of no accessibility delegate been set. Hence,
15902 * to customize the behavior of a View method, clients can override only the
15903 * corresponding delegate method without altering the behavior of the rest
15904 * accessibility related methods of the host view.
15905 * </p>
15906 */
15907 public static class AccessibilityDelegate {
15908
15909 /**
15910 * Sends an accessibility event of the given type. If accessibility is not
15911 * enabled this method has no effect.
15912 * <p>
15913 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15914 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15915 * been set.
15916 * </p>
15917 *
15918 * @param host The View hosting the delegate.
15919 * @param eventType The type of the event to send.
15920 *
15921 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15922 */
15923 public void sendAccessibilityEvent(View host, int eventType) {
15924 host.sendAccessibilityEventInternal(eventType);
15925 }
15926
15927 /**
15928 * Sends an accessibility event. This method behaves exactly as
15929 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15930 * empty {@link AccessibilityEvent} and does not perform a check whether
15931 * accessibility is enabled.
15932 * <p>
15933 * The default implementation behaves as
15934 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15935 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15936 * the case of no accessibility delegate been set.
15937 * </p>
15938 *
15939 * @param host The View hosting the delegate.
15940 * @param event The event to send.
15941 *
15942 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15943 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15944 */
15945 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15946 host.sendAccessibilityEventUncheckedInternal(event);
15947 }
15948
15949 /**
15950 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15951 * to its children for adding their text content to the event.
15952 * <p>
15953 * The default implementation behaves as
15954 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15955 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15956 * the case of no accessibility delegate been set.
15957 * </p>
15958 *
15959 * @param host The View hosting the delegate.
15960 * @param event The event.
15961 * @return True if the event population was completed.
15962 *
15963 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15964 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15965 */
15966 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15967 return host.dispatchPopulateAccessibilityEventInternal(event);
15968 }
15969
15970 /**
15971 * Gives a chance to the host View to populate the accessibility event with its
15972 * text content.
15973 * <p>
15974 * The default implementation behaves as
15975 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15976 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15977 * the case of no accessibility delegate been set.
15978 * </p>
15979 *
15980 * @param host The View hosting the delegate.
15981 * @param event The accessibility event which to populate.
15982 *
15983 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15984 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15985 */
15986 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15987 host.onPopulateAccessibilityEventInternal(event);
15988 }
15989
15990 /**
15991 * Initializes an {@link AccessibilityEvent} with information about the
15992 * the host View which is the event source.
15993 * <p>
15994 * The default implementation behaves as
15995 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15996 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15997 * the case of no accessibility delegate been set.
15998 * </p>
15999 *
16000 * @param host The View hosting the delegate.
16001 * @param event The event to initialize.
16002 *
16003 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
16004 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
16005 */
16006 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
16007 host.onInitializeAccessibilityEventInternal(event);
16008 }
16009
16010 /**
16011 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
16012 * <p>
16013 * The default implementation behaves as
16014 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16015 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
16016 * the case of no accessibility delegate been set.
16017 * </p>
16018 *
16019 * @param host The View hosting the delegate.
16020 * @param info The instance to initialize.
16021 *
16022 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16023 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16024 */
16025 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
16026 host.onInitializeAccessibilityNodeInfoInternal(info);
16027 }
16028
16029 /**
16030 * Called when a child of the host View has requested sending an
16031 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
16032 * to augment the event.
16033 * <p>
16034 * The default implementation behaves as
16035 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16036 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
16037 * the case of no accessibility delegate been set.
16038 * </p>
16039 *
16040 * @param host The View hosting the delegate.
16041 * @param child The child which requests sending the event.
16042 * @param event The event to be sent.
16043 * @return True if the event should be sent
16044 *
16045 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16046 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16047 */
16048 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
16049 AccessibilityEvent event) {
16050 return host.onRequestSendAccessibilityEventInternal(child, event);
16051 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070016052
16053 /**
16054 * Gets the provider for managing a virtual view hierarchy rooted at this View
16055 * and reported to {@link android.accessibilityservice.AccessibilityService}s
16056 * that explore the window content.
16057 * <p>
16058 * The default implementation behaves as
16059 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
16060 * the case of no accessibility delegate been set.
16061 * </p>
16062 *
16063 * @return The provider.
16064 *
16065 * @see AccessibilityNodeProvider
16066 */
16067 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
16068 return null;
16069 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016071}