blob: e6d7c026277d3d7393735059d1ebad6786ea3156 [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;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070049import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070065import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070066import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070068import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080069import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
74
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070076import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070077
Doug Feltcb3791202011-07-07 11:57:48 -070078import com.android.internal.R;
79import com.android.internal.util.Predicate;
80import com.android.internal.view.menu.MenuBuilder;
81
Christopher Tatea0374192010-10-05 13:06:41 -070082import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070083import java.lang.reflect.InvocationTargetException;
84import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.ArrayList;
86import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070087import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080088import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90/**
91 * <p>
92 * This class represents the basic building block for user interface components. A View
93 * occupies a rectangular area on the screen and is responsible for drawing and
94 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070095 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
97 * are invisible containers that hold other Views (or other ViewGroups) and define
98 * their layout properties.
99 * </p>
100 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For information about using this class to develop your application's user interface,
104 * 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 -0800105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700132 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700176 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700195 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700202 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700249 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700342 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
343 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
345 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * </p>
347 *
348 * <p>
349 * Even though a view can define a padding, it does not provide any support for
350 * margins. However, view groups provide such a support. Refer to
351 * {@link android.view.ViewGroup} and
352 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
353 * </p>
354 *
355 * <a name="Layout"></a>
356 * <h3>Layout</h3>
357 * <p>
358 * Layout is a two pass process: a measure pass and a layout pass. The measuring
359 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
360 * of the view tree. Each view pushes dimension specifications down the tree
361 * during the recursion. At the end of the measure pass, every view has stored
362 * its measurements. The second pass happens in
363 * {@link #layout(int,int,int,int)} and is also top-down. During
364 * this pass each parent is responsible for positioning all of its children
365 * using the sizes computed in the measure pass.
366 * </p>
367 *
368 * <p>
369 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
370 * {@link #getMeasuredHeight()} values must be set, along with those for all of
371 * that view's descendants. A view's measured width and measured height values
372 * must respect the constraints imposed by the view's parents. This guarantees
373 * that at the end of the measure pass, all parents accept all of their
374 * children's measurements. A parent view may call measure() more than once on
375 * its children. For example, the parent may measure each child once with
376 * unspecified dimensions to find out how big they want to be, then call
377 * measure() on them again with actual numbers if the sum of all the children's
378 * unconstrained sizes is too big or too small.
379 * </p>
380 *
381 * <p>
382 * The measure pass uses two classes to communicate dimensions. The
383 * {@link MeasureSpec} class is used by views to tell their parents how they
384 * want to be measured and positioned. The base LayoutParams class just
385 * describes how big the view wants to be for both width and height. For each
386 * dimension, it can specify one of:
387 * <ul>
388 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800389 * <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 -0800390 * (minus padding)
391 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
392 * enclose its content (plus padding).
393 * </ul>
394 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
395 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
396 * an X and Y value.
397 * </p>
398 *
399 * <p>
400 * MeasureSpecs are used to push requirements down the tree from parent to
401 * child. A MeasureSpec can be in one of three modes:
402 * <ul>
403 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
404 * of a child view. For example, a LinearLayout may call measure() on its child
405 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
406 * tall the child view wants to be given a width of 240 pixels.
407 * <li>EXACTLY: This is used by the parent to impose an exact size on the
408 * child. The child must use this size, and guarantee that all of its
409 * descendants will fit within this size.
410 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
411 * child. The child must gurantee that it and all of its descendants will fit
412 * within this size.
413 * </ul>
414 * </p>
415 *
416 * <p>
417 * To intiate a layout, call {@link #requestLayout}. This method is typically
418 * called by a view on itself when it believes that is can no longer fit within
419 * its current bounds.
420 * </p>
421 *
422 * <a name="Drawing"></a>
423 * <h3>Drawing</h3>
424 * <p>
425 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700426 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * this means that parents will draw before (i.e., behind) their children, with
428 * siblings drawn in the order they appear in the tree.
429 * If you set a background drawable for a View, then the View will draw it for you
430 * before calling back to its <code>onDraw()</code> method.
431 * </p>
432 *
433 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700434 * 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 -0800435 * </p>
436 *
437 * <p>
438 * To force a view to draw, call {@link #invalidate()}.
439 * </p>
440 *
441 * <a name="EventHandlingThreading"></a>
442 * <h3>Event Handling and Threading</h3>
443 * <p>
444 * The basic cycle of a view is as follows:
445 * <ol>
446 * <li>An event comes in and is dispatched to the appropriate view. The view
447 * handles the event and notifies any listeners.</li>
448 * <li>If in the course of processing the event, the view's bounds may need
449 * to be changed, the view will call {@link #requestLayout()}.</li>
450 * <li>Similarly, if in the course of processing the event the view's appearance
451 * may need to be changed, the view will call {@link #invalidate()}.</li>
452 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
453 * the framework will take care of measuring, laying out, and drawing the tree
454 * as appropriate.</li>
455 * </ol>
456 * </p>
457 *
458 * <p><em>Note: The entire view tree is single threaded. You must always be on
459 * the UI thread when calling any method on any view.</em>
460 * If you are doing work on other threads and want to update the state of a view
461 * from that thread, you should use a {@link Handler}.
462 * </p>
463 *
464 * <a name="FocusHandling"></a>
465 * <h3>Focus Handling</h3>
466 * <p>
467 * The framework will handle routine focus movement in response to user input.
468 * This includes changing the focus as views are removed or hidden, or as new
469 * views become available. Views indicate their willingness to take focus
470 * through the {@link #isFocusable} method. To change whether a view can take
471 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
472 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
473 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
474 * </p>
475 * <p>
476 * Focus movement is based on an algorithm which finds the nearest neighbor in a
477 * given direction. In rare cases, the default algorithm may not match the
478 * intended behavior of the developer. In these situations, you can provide
479 * explicit overrides by using these XML attributes in the layout file:
480 * <pre>
481 * nextFocusDown
482 * nextFocusLeft
483 * nextFocusRight
484 * nextFocusUp
485 * </pre>
486 * </p>
487 *
488 *
489 * <p>
490 * To get a particular view to take focus, call {@link #requestFocus()}.
491 * </p>
492 *
493 * <a name="TouchMode"></a>
494 * <h3>Touch Mode</h3>
495 * <p>
496 * When a user is navigating a user interface via directional keys such as a D-pad, it is
497 * necessary to give focus to actionable items such as buttons so the user can see
498 * what will take input. If the device has touch capabilities, however, and the user
499 * begins interacting with the interface by touching it, it is no longer necessary to
500 * always highlight, or give focus to, a particular view. This motivates a mode
501 * for interaction named 'touch mode'.
502 * </p>
503 * <p>
504 * For a touch capable device, once the user touches the screen, the device
505 * will enter touch mode. From this point onward, only views for which
506 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
507 * Other views that are touchable, like buttons, will not take focus when touched; they will
508 * only fire the on click listeners.
509 * </p>
510 * <p>
511 * Any time a user hits a directional key, such as a D-pad direction, the view device will
512 * exit touch mode, and find a view to take focus, so that the user may resume interacting
513 * with the user interface without touching the screen again.
514 * </p>
515 * <p>
516 * The touch mode state is maintained across {@link android.app.Activity}s. Call
517 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
518 * </p>
519 *
520 * <a name="Scrolling"></a>
521 * <h3>Scrolling</h3>
522 * <p>
523 * The framework provides basic support for views that wish to internally
524 * scroll their content. This includes keeping track of the X and Y scroll
525 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800526 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700527 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 * </p>
529 *
530 * <a name="Tags"></a>
531 * <h3>Tags</h3>
532 * <p>
533 * Unlike IDs, tags are not used to identify views. Tags are essentially an
534 * extra piece of information that can be associated with a view. They are most
535 * often used as a convenience to store data related to views in the views
536 * themselves rather than by putting them in a separate structure.
537 * </p>
538 *
539 * <a name="Animation"></a>
540 * <h3>Animation</h3>
541 * <p>
542 * You can attach an {@link Animation} object to a view using
543 * {@link #setAnimation(Animation)} or
544 * {@link #startAnimation(Animation)}. The animation can alter the scale,
545 * rotation, translation and alpha of a view over time. If the animation is
546 * attached to a view that has children, the animation will affect the entire
547 * subtree rooted by that node. When an animation is started, the framework will
548 * take care of redrawing the appropriate views until the animation completes.
549 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800550 * <p>
551 * Starting with Android 3.0, the preferred way of animating views is to use the
552 * {@link android.animation} package APIs.
553 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 *
Jeff Brown85a31762010-09-01 17:01:00 -0700555 * <a name="Security"></a>
556 * <h3>Security</h3>
557 * <p>
558 * Sometimes it is essential that an application be able to verify that an action
559 * is being performed with the full knowledge and consent of the user, such as
560 * granting a permission request, making a purchase or clicking on an advertisement.
561 * Unfortunately, a malicious application could try to spoof the user into
562 * performing these actions, unaware, by concealing the intended purpose of the view.
563 * As a remedy, the framework offers a touch filtering mechanism that can be used to
564 * improve the security of views that provide access to sensitive functionality.
565 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700566 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800567 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700568 * will discard touches that are received whenever the view's window is obscured by
569 * another visible window. As a result, the view will not receive touches whenever a
570 * toast, dialog or other window appears above the view's window.
571 * </p><p>
572 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700573 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
574 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700575 * </p>
576 *
Romain Guy171c5922011-01-06 10:04:23 -0800577 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700578 * @attr ref android.R.styleable#View_background
579 * @attr ref android.R.styleable#View_clickable
580 * @attr ref android.R.styleable#View_contentDescription
581 * @attr ref android.R.styleable#View_drawingCacheQuality
582 * @attr ref android.R.styleable#View_duplicateParentState
583 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700584 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700585 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700586 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700587 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700589 * @attr ref android.R.styleable#View_isScrollContainer
590 * @attr ref android.R.styleable#View_focusable
591 * @attr ref android.R.styleable#View_focusableInTouchMode
592 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
593 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800594 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700595 * @attr ref android.R.styleable#View_longClickable
596 * @attr ref android.R.styleable#View_minHeight
597 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @attr ref android.R.styleable#View_nextFocusDown
599 * @attr ref android.R.styleable#View_nextFocusLeft
600 * @attr ref android.R.styleable#View_nextFocusRight
601 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_onClick
603 * @attr ref android.R.styleable#View_padding
604 * @attr ref android.R.styleable#View_paddingBottom
605 * @attr ref android.R.styleable#View_paddingLeft
606 * @attr ref android.R.styleable#View_paddingRight
607 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800608 * @attr ref android.R.styleable#View_paddingStart
609 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800611 * @attr ref android.R.styleable#View_rotation
612 * @attr ref android.R.styleable#View_rotationX
613 * @attr ref android.R.styleable#View_rotationY
614 * @attr ref android.R.styleable#View_scaleX
615 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_scrollX
617 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_scrollbarSize
619 * @attr ref android.R.styleable#View_scrollbarStyle
620 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700621 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
622 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
624 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarThumbVertical
626 * @attr ref android.R.styleable#View_scrollbarTrackVertical
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_soundEffectsEnabled
630 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700631 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800632 * @attr ref android.R.styleable#View_transformPivotX
633 * @attr ref android.R.styleable#View_transformPivotY
634 * @attr ref android.R.styleable#View_translationX
635 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700636 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 *
638 * @see android.view.ViewGroup
639 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700640public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
641 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private static final boolean DBG = false;
643
644 /**
645 * The logging tag used by this class with android.util.Log.
646 */
647 protected static final String VIEW_LOG_TAG = "View";
648
649 /**
650 * Used to mark a View that has no ID.
651 */
652 public static final int NO_ID = -1;
653
654 /**
655 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
656 * calling setFlags.
657 */
658 private static final int NOT_FOCUSABLE = 0x00000000;
659
660 /**
661 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
662 * setFlags.
663 */
664 private static final int FOCUSABLE = 0x00000001;
665
666 /**
667 * Mask for use with setFlags indicating bits used for focus.
668 */
669 private static final int FOCUSABLE_MASK = 0x00000001;
670
671 /**
672 * This view will adjust its padding to fit sytem windows (e.g. status bar)
673 */
674 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
675
676 /**
Scott Main812634c22011-07-27 13:22:35 -0700677 * This view is visible.
678 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
679 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 */
681 public static final int VISIBLE = 0x00000000;
682
683 /**
684 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700685 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
686 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 */
688 public static final int INVISIBLE = 0x00000004;
689
690 /**
691 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700692 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
693 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 */
695 public static final int GONE = 0x00000008;
696
697 /**
698 * Mask for use with setFlags indicating bits used for visibility.
699 * {@hide}
700 */
701 static final int VISIBILITY_MASK = 0x0000000C;
702
703 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
704
705 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700706 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 * Use with ENABLED_MASK when calling setFlags.
708 * {@hide}
709 */
710 static final int ENABLED = 0x00000000;
711
712 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700713 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * Use with ENABLED_MASK when calling setFlags.
715 * {@hide}
716 */
717 static final int DISABLED = 0x00000020;
718
719 /**
720 * Mask for use with setFlags indicating bits used for indicating whether
721 * this view is enabled
722 * {@hide}
723 */
724 static final int ENABLED_MASK = 0x00000020;
725
726 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700727 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
728 * called and further optimizations will be performed. It is okay to have
729 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 * {@hide}
731 */
732 static final int WILL_NOT_DRAW = 0x00000080;
733
734 /**
735 * Mask for use with setFlags indicating bits used for indicating whether
736 * this view is will draw
737 * {@hide}
738 */
739 static final int DRAW_MASK = 0x00000080;
740
741 /**
742 * <p>This view doesn't show scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_NONE = 0x00000000;
746
747 /**
748 * <p>This view shows horizontal scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
752
753 /**
754 * <p>This view shows vertical scrollbars.</p>
755 * {@hide}
756 */
757 static final int SCROLLBARS_VERTICAL = 0x00000200;
758
759 /**
760 * <p>Mask for use with setFlags indicating bits used for indicating which
761 * scrollbars are enabled.</p>
762 * {@hide}
763 */
764 static final int SCROLLBARS_MASK = 0x00000300;
765
Jeff Brown85a31762010-09-01 17:01:00 -0700766 /**
767 * Indicates that the view should filter touches when its window is obscured.
768 * Refer to the class comments for more information about this security feature.
769 * {@hide}
770 */
771 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
772
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700773 /**
774 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
775 * that they are optional and should be skipped if the window has
776 * requested system UI flags that ignore those insets for layout.
777 */
778 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
780 /**
781 * <p>This view doesn't show fading edges.</p>
782 * {@hide}
783 */
784 static final int FADING_EDGE_NONE = 0x00000000;
785
786 /**
787 * <p>This view shows horizontal fading edges.</p>
788 * {@hide}
789 */
790 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
791
792 /**
793 * <p>This view shows vertical fading edges.</p>
794 * {@hide}
795 */
796 static final int FADING_EDGE_VERTICAL = 0x00002000;
797
798 /**
799 * <p>Mask for use with setFlags indicating bits used for indicating which
800 * fading edges are enabled.</p>
801 * {@hide}
802 */
803 static final int FADING_EDGE_MASK = 0x00003000;
804
805 /**
806 * <p>Indicates this view can be clicked. When clickable, a View reacts
807 * to clicks by notifying the OnClickListener.<p>
808 * {@hide}
809 */
810 static final int CLICKABLE = 0x00004000;
811
812 /**
813 * <p>Indicates this view is caching its drawing into a bitmap.</p>
814 * {@hide}
815 */
816 static final int DRAWING_CACHE_ENABLED = 0x00008000;
817
818 /**
819 * <p>Indicates that no icicle should be saved for this view.<p>
820 * {@hide}
821 */
822 static final int SAVE_DISABLED = 0x000010000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
826 * property.</p>
827 * {@hide}
828 */
829 static final int SAVE_DISABLED_MASK = 0x000010000;
830
831 /**
832 * <p>Indicates that no drawing cache should ever be created for this view.<p>
833 * {@hide}
834 */
835 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
836
837 /**
838 * <p>Indicates this view can take / keep focus when int touch mode.</p>
839 * {@hide}
840 */
841 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
842
843 /**
844 * <p>Enables low quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
847
848 /**
849 * <p>Enables high quality mode for the drawing cache.</p>
850 */
851 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
852
853 /**
854 * <p>Enables automatic quality mode for the drawing cache.</p>
855 */
856 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
857
858 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
859 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
860 };
861
862 /**
863 * <p>Mask for use with setFlags indicating bits used for the cache
864 * quality property.</p>
865 * {@hide}
866 */
867 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
868
869 /**
870 * <p>
871 * Indicates this view can be long clicked. When long clickable, a View
872 * reacts to long clicks by notifying the OnLongClickListener or showing a
873 * context menu.
874 * </p>
875 * {@hide}
876 */
877 static final int LONG_CLICKABLE = 0x00200000;
878
879 /**
880 * <p>Indicates that this view gets its drawable states from its direct parent
881 * and ignores its original internal states.</p>
882 *
883 * @hide
884 */
885 static final int DUPLICATE_PARENT_STATE = 0x00400000;
886
887 /**
888 * The scrollbar style to display the scrollbars inside the content area,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency on the view's content.
891 */
892 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
893
894 /**
895 * The scrollbar style to display the scrollbars inside the padded area,
896 * increasing the padding of the view. The scrollbars will not overlap the
897 * content area of the view.
898 */
899 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
900
901 /**
902 * The scrollbar style to display the scrollbars at the edge of the view,
903 * without increasing the padding. The scrollbars will be overlaid with
904 * translucency.
905 */
906 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
907
908 /**
909 * The scrollbar style to display the scrollbars at the edge of the view,
910 * increasing the padding of the view. The scrollbars will only overlap the
911 * background, if any.
912 */
913 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
914
915 /**
916 * Mask to check if the scrollbar style is overlay or inset.
917 * {@hide}
918 */
919 static final int SCROLLBARS_INSET_MASK = 0x01000000;
920
921 /**
922 * Mask to check if the scrollbar style is inside or outside.
923 * {@hide}
924 */
925 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
926
927 /**
928 * Mask for scrollbar style.
929 * {@hide}
930 */
931 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
932
933 /**
934 * View flag indicating that the screen should remain on while the
935 * window containing this view is visible to the user. This effectively
936 * takes care of automatically setting the WindowManager's
937 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
938 */
939 public static final int KEEP_SCREEN_ON = 0x04000000;
940
941 /**
942 * View flag indicating whether this view should have sound effects enabled
943 * for events such as clicking and touching.
944 */
945 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
946
947 /**
948 * View flag indicating whether this view should have haptic feedback
949 * enabled for events such as long presses.
950 */
951 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
952
953 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700954 * <p>Indicates that the view hierarchy should stop saving state when
955 * it reaches this view. If state saving is initiated immediately at
956 * the view, it will be allowed.
957 * {@hide}
958 */
959 static final int PARENT_SAVE_DISABLED = 0x20000000;
960
961 /**
962 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
963 * {@hide}
964 */
965 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
966
967 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700968 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
969 * should add all focusable Views regardless if they are focusable in touch mode.
970 */
971 public static final int FOCUSABLES_ALL = 0x00000000;
972
973 /**
974 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
975 * should add only Views focusable in touch mode.
976 */
977 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
978
979 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700980 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
981 * should add only accessibility focusable Views.
982 *
983 * @hide
984 */
985 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
986
987 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700988 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 * item.
990 */
991 public static final int FOCUS_BACKWARD = 0x00000001;
992
993 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700994 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * item.
996 */
997 public static final int FOCUS_FORWARD = 0x00000002;
998
999 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001000 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 */
1002 public static final int FOCUS_LEFT = 0x00000011;
1003
1004 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001005 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 */
1007 public static final int FOCUS_UP = 0x00000021;
1008
1009 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001010 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 */
1012 public static final int FOCUS_RIGHT = 0x00000042;
1013
1014 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001015 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 */
1017 public static final int FOCUS_DOWN = 0x00000082;
1018
Svetoslav Ganov42138042012-03-20 11:51:39 -07001019 // Accessibility focus directions.
1020
1021 /**
1022 * The accessibility focus which is the current user position when
1023 * interacting with the accessibility framework.
1024 */
1025 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1026
1027 /**
1028 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1029 */
1030 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1031
1032 /**
1033 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1034 */
1035 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1036
1037 /**
1038 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1039 */
1040 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1041
1042 /**
1043 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1044 */
1045 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1046
1047 /**
1048 * Use with {@link #focusSearch(int)}. Move acessibility focus to the next view.
1049 */
1050 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1051
1052 /**
1053 * Use with {@link #focusSearch(int)}. Move acessibility focus to the previous view.
1054 */
1055 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1056
1057 /**
1058 * Use with {@link #focusSearch(int)}. Move acessibility focus in a view.
1059 */
1060 public static final int ACCESSIBILITY_FOCUS_IN = 0x00000004 | FOCUS_ACCESSIBILITY;
1061
1062 /**
1063 * Use with {@link #focusSearch(int)}. Move acessibility focus out of a view.
1064 */
1065 public static final int ACCESSIBILITY_FOCUS_OUT = 0x00000008 | FOCUS_ACCESSIBILITY;
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001068 * Bits of {@link #getMeasuredWidthAndState()} and
1069 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1070 */
1071 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1072
1073 /**
1074 * Bits of {@link #getMeasuredWidthAndState()} and
1075 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1076 */
1077 public static final int MEASURED_STATE_MASK = 0xff000000;
1078
1079 /**
1080 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1081 * for functions that combine both width and height into a single int,
1082 * such as {@link #getMeasuredState()} and the childState argument of
1083 * {@link #resolveSizeAndState(int, int, int)}.
1084 */
1085 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1086
1087 /**
1088 * Bit of {@link #getMeasuredWidthAndState()} and
1089 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1090 * is smaller that the space the view would like to have.
1091 */
1092 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1093
1094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 * Base View state sets
1096 */
1097 // Singles
1098 /**
1099 * Indicates the view has no states set. States are used with
1100 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1101 * view depending on its state.
1102 *
1103 * @see android.graphics.drawable.Drawable
1104 * @see #getDrawableState()
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is enabled. States are used with
1109 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1110 * view depending on its state.
1111 *
1112 * @see android.graphics.drawable.Drawable
1113 * @see #getDrawableState()
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is focused. States are used with
1118 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1119 * view depending on its state.
1120 *
1121 * @see android.graphics.drawable.Drawable
1122 * @see #getDrawableState()
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 /**
1126 * Indicates the view is selected. States are used with
1127 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1128 * view depending on its state.
1129 *
1130 * @see android.graphics.drawable.Drawable
1131 * @see #getDrawableState()
1132 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001133 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 /**
1135 * Indicates the view is pressed. States are used with
1136 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1137 * view depending on its state.
1138 *
1139 * @see android.graphics.drawable.Drawable
1140 * @see #getDrawableState()
1141 * @hide
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view's window has focus. States are used with
1146 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1147 * view depending on its state.
1148 *
1149 * @see android.graphics.drawable.Drawable
1150 * @see #getDrawableState()
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 // Doubles
1154 /**
1155 * Indicates the view is enabled and has the focus.
1156 *
1157 * @see #ENABLED_STATE_SET
1158 * @see #FOCUSED_STATE_SET
1159 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001160 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 /**
1162 * Indicates the view is enabled and selected.
1163 *
1164 * @see #ENABLED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is enabled and that its window has focus.
1170 *
1171 * @see #ENABLED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 /**
1176 * Indicates the view is focused and selected.
1177 *
1178 * @see #FOCUSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view has the focus and that its window has the focus.
1184 *
1185 * @see #FOCUSED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is selected and that its window has the focus.
1191 *
1192 * @see #SELECTED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 // Triples
1197 /**
1198 * Indicates the view is enabled, focused and selected.
1199 *
1200 * @see #ENABLED_STATE_SET
1201 * @see #FOCUSED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is enabled, focused and its window has the focus.
1207 *
1208 * @see #ENABLED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 * @see #WINDOW_FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is enabled, selected and its window has the focus.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is focused, selected and its window has the focus.
1223 *
1224 * @see #FOCUSED_STATE_SET
1225 * @see #SELECTED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, focused, selected and its window
1231 * has the focus.
1232 *
1233 * @see #ENABLED_STATE_SET
1234 * @see #FOCUSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 * @see #WINDOW_FOCUSED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed and its window has the focus.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #WINDOW_FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed and selected.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #SELECTED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, selected and its window has the focus.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #SELECTED_STATE_SET
1258 * @see #WINDOW_FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed and focused.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed, focused and its window has the focus.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #FOCUSED_STATE_SET
1273 * @see #WINDOW_FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, focused and selected.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #SELECTED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused, selected and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed and enabled.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, enabled and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #WINDOW_FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled and selected.
1310 *
1311 * @see #PRESSED_STATE_SET
1312 * @see #ENABLED_STATE_SET
1313 * @see #SELECTED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled, selected and its window has the
1318 * focus.
1319 *
1320 * @see #PRESSED_STATE_SET
1321 * @see #ENABLED_STATE_SET
1322 * @see #SELECTED_STATE_SET
1323 * @see #WINDOW_FOCUSED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled and focused.
1328 *
1329 * @see #PRESSED_STATE_SET
1330 * @see #ENABLED_STATE_SET
1331 * @see #FOCUSED_STATE_SET
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Indicates the view is pressed, enabled, focused and its window has the
1336 * focus.
1337 *
1338 * @see #PRESSED_STATE_SET
1339 * @see #ENABLED_STATE_SET
1340 * @see #FOCUSED_STATE_SET
1341 * @see #WINDOW_FOCUSED_STATE_SET
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 /**
1345 * Indicates the view is pressed, enabled, focused and selected.
1346 *
1347 * @see #PRESSED_STATE_SET
1348 * @see #ENABLED_STATE_SET
1349 * @see #SELECTED_STATE_SET
1350 * @see #FOCUSED_STATE_SET
1351 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001352 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 /**
1354 * Indicates the view is pressed, enabled, focused, selected and its window
1355 * has the focus.
1356 *
1357 * @see #PRESSED_STATE_SET
1358 * @see #ENABLED_STATE_SET
1359 * @see #SELECTED_STATE_SET
1360 * @see #FOCUSED_STATE_SET
1361 * @see #WINDOW_FOCUSED_STATE_SET
1362 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001363 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364
1365 /**
1366 * The order here is very important to {@link #getDrawableState()}
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 private static final int[][] VIEW_STATE_SETS;
1369
Romain Guyb051e892010-09-28 19:09:36 -07001370 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1371 static final int VIEW_STATE_SELECTED = 1 << 1;
1372 static final int VIEW_STATE_FOCUSED = 1 << 2;
1373 static final int VIEW_STATE_ENABLED = 1 << 3;
1374 static final int VIEW_STATE_PRESSED = 1 << 4;
1375 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001377 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001378 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1379 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380
1381 static final int[] VIEW_STATE_IDS = new int[] {
1382 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1383 R.attr.state_selected, VIEW_STATE_SELECTED,
1384 R.attr.state_focused, VIEW_STATE_FOCUSED,
1385 R.attr.state_enabled, VIEW_STATE_ENABLED,
1386 R.attr.state_pressed, VIEW_STATE_PRESSED,
1387 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001388 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001389 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001390 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001391 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 };
1393
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001395 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1396 throw new IllegalStateException(
1397 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1398 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001401 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001402 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001403 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001404 orderedIds[i * 2] = viewState;
1405 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001406 }
1407 }
1408 }
Romain Guyb051e892010-09-28 19:09:36 -07001409 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1410 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1411 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001412 int numBits = Integer.bitCount(i);
1413 int[] set = new int[numBits];
1414 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001415 for (int j = 0; j < orderedIds.length; j += 2) {
1416 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 set[pos++] = orderedIds[j];
1418 }
1419 }
1420 VIEW_STATE_SETS[i] = set;
1421 }
1422
1423 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1424 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1425 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1426 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1428 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1429 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1431 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1433 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1435 | VIEW_STATE_FOCUSED];
1436 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1437 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1439 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1441 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1443 | VIEW_STATE_ENABLED];
1444 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1446 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_ENABLED];
1449 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1451 | VIEW_STATE_ENABLED];
1452 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1454 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1455
1456 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1457 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1459 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1461 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1466 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_PRESSED];
1469 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_PRESSED];
1472 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1474 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1482 | VIEW_STATE_PRESSED];
1483 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1484 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1485 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1486 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1487 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1488 | VIEW_STATE_PRESSED];
1489 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1490 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1491 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1492 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1493 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1494 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1495 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1496 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1497 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 }
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001502 * Accessibility event types that are dispatched for text population.
1503 */
1504 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1505 AccessibilityEvent.TYPE_VIEW_CLICKED
1506 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1507 | AccessibilityEvent.TYPE_VIEW_SELECTED
1508 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1509 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1510 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001511 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001512 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001513 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1514 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001515
1516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 * Temporary Rect currently for use in setBackground(). This will probably
1518 * be extended in the future to hold our own class with more than just
1519 * a Rect. :)
1520 */
1521 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001522
1523 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001524 * Temporary flag, used to enable processing of View properties in the native DisplayList
1525 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1526 * apps.
1527 * @hide
1528 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001529 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001530
1531 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001532 * Map used to store views' tags.
1533 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001534 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001537 * The next available accessiiblity id.
1538 */
1539 private static int sNextAccessibilityViewId;
1540
1541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * The animation currently associated with this view.
1543 * @hide
1544 */
1545 protected Animation mCurrentAnimation = null;
1546
1547 /**
1548 * Width as measured during measure pass.
1549 * {@hide}
1550 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001551 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001552 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553
1554 /**
1555 * Height as measured during measure pass.
1556 * {@hide}
1557 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001558 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001559 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560
1561 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001562 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1563 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1564 * its display list. This flag, used only when hw accelerated, allows us to clear the
1565 * flag while retaining this information until it's needed (at getDisplayList() time and
1566 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1567 *
1568 * {@hide}
1569 */
1570 boolean mRecreateDisplayList = false;
1571
1572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 * The view's identifier.
1574 * {@hide}
1575 *
1576 * @see #setId(int)
1577 * @see #getId()
1578 */
1579 @ViewDebug.ExportedProperty(resolveId = true)
1580 int mID = NO_ID;
1581
1582 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001583 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001584 */
1585 int mAccessibilityViewId = NO_ID;
1586
1587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 * The view's tag.
1589 * {@hide}
1590 *
1591 * @see #setTag(Object)
1592 * @see #getTag()
1593 */
1594 protected Object mTag;
1595
1596 // for mPrivateFlags:
1597 /** {@hide} */
1598 static final int WANTS_FOCUS = 0x00000001;
1599 /** {@hide} */
1600 static final int FOCUSED = 0x00000002;
1601 /** {@hide} */
1602 static final int SELECTED = 0x00000004;
1603 /** {@hide} */
1604 static final int IS_ROOT_NAMESPACE = 0x00000008;
1605 /** {@hide} */
1606 static final int HAS_BOUNDS = 0x00000010;
1607 /** {@hide} */
1608 static final int DRAWN = 0x00000020;
1609 /**
1610 * When this flag is set, this view is running an animation on behalf of its
1611 * children and should therefore not cancel invalidate requests, even if they
1612 * lie outside of this view's bounds.
1613 *
1614 * {@hide}
1615 */
1616 static final int DRAW_ANIMATION = 0x00000040;
1617 /** {@hide} */
1618 static final int SKIP_DRAW = 0x00000080;
1619 /** {@hide} */
1620 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1621 /** {@hide} */
1622 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1623 /** {@hide} */
1624 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1625 /** {@hide} */
1626 static final int MEASURED_DIMENSION_SET = 0x00000800;
1627 /** {@hide} */
1628 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001629 /** {@hide} */
1630 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
1632 private static final int PRESSED = 0x00004000;
1633
1634 /** {@hide} */
1635 static final int DRAWING_CACHE_VALID = 0x00008000;
1636 /**
1637 * Flag used to indicate that this view should be drawn once more (and only once
1638 * more) after its animation has completed.
1639 * {@hide}
1640 */
1641 static final int ANIMATION_STARTED = 0x00010000;
1642
1643 private static final int SAVE_STATE_CALLED = 0x00020000;
1644
1645 /**
1646 * Indicates that the View returned true when onSetAlpha() was called and that
1647 * the alpha must be restored.
1648 * {@hide}
1649 */
1650 static final int ALPHA_SET = 0x00040000;
1651
1652 /**
1653 * Set by {@link #setScrollContainer(boolean)}.
1654 */
1655 static final int SCROLL_CONTAINER = 0x00080000;
1656
1657 /**
1658 * Set by {@link #setScrollContainer(boolean)}.
1659 */
1660 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1661
1662 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001663 * View flag indicating whether this view was invalidated (fully or partially.)
1664 *
1665 * @hide
1666 */
1667 static final int DIRTY = 0x00200000;
1668
1669 /**
1670 * View flag indicating whether this view was invalidated by an opaque
1671 * invalidate request.
1672 *
1673 * @hide
1674 */
1675 static final int DIRTY_OPAQUE = 0x00400000;
1676
1677 /**
1678 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1679 *
1680 * @hide
1681 */
1682 static final int DIRTY_MASK = 0x00600000;
1683
1684 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001685 * Indicates whether the background is opaque.
1686 *
1687 * @hide
1688 */
1689 static final int OPAQUE_BACKGROUND = 0x00800000;
1690
1691 /**
1692 * Indicates whether the scrollbars are opaque.
1693 *
1694 * @hide
1695 */
1696 static final int OPAQUE_SCROLLBARS = 0x01000000;
1697
1698 /**
1699 * Indicates whether the view is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001704
Adam Powelle14579b2009-12-16 18:39:52 -08001705 /**
1706 * Indicates a prepressed state;
1707 * the short time between ACTION_DOWN and recognizing
1708 * a 'real' press. Prepressed is used to recognize quick taps
1709 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001710 *
Adam Powelle14579b2009-12-16 18:39:52 -08001711 * @hide
1712 */
1713 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001714
Adam Powellc9fbaab2010-02-16 17:16:19 -08001715 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001716 * Indicates whether the view is temporarily detached.
1717 *
1718 * @hide
1719 */
1720 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001721
Adam Powell8568c3a2010-04-19 14:26:11 -07001722 /**
1723 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001725 * @hide
1726 */
1727 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001728
1729 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001730 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1731 * @hide
1732 */
1733 private static final int HOVERED = 0x10000000;
1734
1735 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001736 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1737 * for transform operations
1738 *
1739 * @hide
1740 */
Adam Powellf37df072010-09-17 16:22:49 -07001741 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001742
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001743 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001744 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001745
Chet Haasefd2b0022010-08-06 13:08:56 -07001746 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001747 * Indicates that this view was specifically invalidated, not just dirtied because some
1748 * child view was invalidated. The flag is used to determine when we need to recreate
1749 * a view's display list (as opposed to just returning a reference to its existing
1750 * display list).
1751 *
1752 * @hide
1753 */
1754 static final int INVALIDATED = 0x80000000;
1755
Christopher Tate3d4bf172011-03-28 16:16:46 -07001756 /* Masks for mPrivateFlags2 */
1757
1758 /**
1759 * Indicates that this view has reported that it can accept the current drag's content.
1760 * Cleared when the drag operation concludes.
1761 * @hide
1762 */
1763 static final int DRAG_CAN_ACCEPT = 0x00000001;
1764
1765 /**
1766 * Indicates that this view is currently directly under the drag location in a
1767 * drag-and-drop operation involving content that it can accept. Cleared when
1768 * the drag exits the view, or when the drag operation concludes.
1769 * @hide
1770 */
1771 static final int DRAG_HOVERED = 0x00000002;
1772
Cibu Johny86666632010-02-22 13:01:02 -08001773 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001774 * Horizontal layout direction of this view is from Left to Right.
1775 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001776 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001777 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001778
1779 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780 * Horizontal layout direction of this view is from Right to Left.
1781 * Use with {@link #setLayoutDirection}.
1782 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001783 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001784
1785 /**
1786 * Horizontal layout direction of this view is inherited from its parent.
1787 * Use with {@link #setLayoutDirection}.
1788 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001789 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001790
1791 /**
1792 * Horizontal layout direction of this view is from deduced from the default language
1793 * script for the locale. Use with {@link #setLayoutDirection}.
1794 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001795 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001796
1797 /**
1798 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001799 * @hide
1800 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001801 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1802
1803 /**
1804 * Mask for use with private flags indicating bits used for horizontal layout direction.
1805 * @hide
1806 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001807 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001808
1809 /**
1810 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1811 * right-to-left direction.
1812 * @hide
1813 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001814 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001815
1816 /**
1817 * Indicates whether the view horizontal layout direction has been resolved.
1818 * @hide
1819 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001820 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001821
1822 /**
1823 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1824 * @hide
1825 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001826 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001827
1828 /*
1829 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1830 * flag value.
1831 * @hide
1832 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001833 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1834 LAYOUT_DIRECTION_LTR,
1835 LAYOUT_DIRECTION_RTL,
1836 LAYOUT_DIRECTION_INHERIT,
1837 LAYOUT_DIRECTION_LOCALE
1838 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Default horizontal layout direction.
1842 * @hide
1843 */
1844 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001845
Adam Powell539ee872012-02-03 19:00:49 -08001846 /**
1847 * Indicates that the view is tracking some sort of transient state
1848 * that the app should not need to be aware of, but that the framework
1849 * should take special care to preserve.
1850 *
1851 * @hide
1852 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001853 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001854
1855
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001856 /**
1857 * Text direction is inherited thru {@link ViewGroup}
1858 */
1859 public static final int TEXT_DIRECTION_INHERIT = 0;
1860
1861 /**
1862 * Text direction is using "first strong algorithm". The first strong directional character
1863 * determines the paragraph direction. If there is no strong directional character, the
1864 * paragraph direction is the view's resolved layout direction.
1865 */
1866 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1867
1868 /**
1869 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1870 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1871 * If there are neither, the paragraph direction is the view's resolved layout direction.
1872 */
1873 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1874
1875 /**
1876 * Text direction is forced to LTR.
1877 */
1878 public static final int TEXT_DIRECTION_LTR = 3;
1879
1880 /**
1881 * Text direction is forced to RTL.
1882 */
1883 public static final int TEXT_DIRECTION_RTL = 4;
1884
1885 /**
1886 * Text direction is coming from the system Locale.
1887 */
1888 public static final int TEXT_DIRECTION_LOCALE = 5;
1889
1890 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001891 * Default text direction is inherited
1892 */
1893 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1894
1895 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001896 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1897 * @hide
1898 */
1899 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1900
1901 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001902 * Mask for use with private flags indicating bits used for text direction.
1903 * @hide
1904 */
1905 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1906
1907 /**
1908 * Array of text direction flags for mapping attribute "textDirection" to correct
1909 * flag value.
1910 * @hide
1911 */
1912 private static final int[] TEXT_DIRECTION_FLAGS = {
1913 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1914 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1915 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1916 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1917 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1918 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1919 };
1920
1921 /**
1922 * Indicates whether the view text direction has been resolved.
1923 * @hide
1924 */
1925 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1926
1927 /**
1928 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1929 * @hide
1930 */
1931 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1932
1933 /**
1934 * Mask for use with private flags indicating bits used for resolved text direction.
1935 * @hide
1936 */
1937 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1938
1939 /**
1940 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1941 * @hide
1942 */
1943 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1944 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1945
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001946 /*
1947 * Default text alignment. The text alignment of this View is inherited from its parent.
1948 * Use with {@link #setTextAlignment(int)}
1949 */
1950 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1951
1952 /**
1953 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1954 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1955 *
1956 * Use with {@link #setTextAlignment(int)}
1957 */
1958 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1959
1960 /**
1961 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1962 *
1963 * Use with {@link #setTextAlignment(int)}
1964 */
1965 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1966
1967 /**
1968 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1969 *
1970 * Use with {@link #setTextAlignment(int)}
1971 */
1972 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1973
1974 /**
1975 * Center the paragraph, e.g. ALIGN_CENTER.
1976 *
1977 * Use with {@link #setTextAlignment(int)}
1978 */
1979 public static final int TEXT_ALIGNMENT_CENTER = 4;
1980
1981 /**
1982 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1983 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1984 *
1985 * Use with {@link #setTextAlignment(int)}
1986 */
1987 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1988
1989 /**
1990 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1991 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1992 *
1993 * Use with {@link #setTextAlignment(int)}
1994 */
1995 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1996
1997 /**
1998 * Default text alignment is inherited
1999 */
2000 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2001
2002 /**
2003 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2004 * @hide
2005 */
2006 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2007
2008 /**
2009 * Mask for use with private flags indicating bits used for text alignment.
2010 * @hide
2011 */
2012 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2013
2014 /**
2015 * Array of text direction flags for mapping attribute "textAlignment" to correct
2016 * flag value.
2017 * @hide
2018 */
2019 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2020 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2021 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2022 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2023 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2024 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2025 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2026 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2027 };
2028
2029 /**
2030 * Indicates whether the view text alignment has been resolved.
2031 * @hide
2032 */
2033 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2034
2035 /**
2036 * Bit shift to get the resolved text alignment.
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2046
2047 /**
2048 * Indicates whether if the view text alignment has been resolved to gravity
2049 */
2050 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2051 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2052
Svetoslav Ganov42138042012-03-20 11:51:39 -07002053 // Accessiblity constants for mPrivateFlags2
2054
2055 /**
2056 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2057 */
2058 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2059
2060 /**
2061 * Automatically determine whether a view is important for accessibility.
2062 */
2063 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2064
2065 /**
2066 * The view is important for accessibility.
2067 */
2068 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2069
2070 /**
2071 * The view is not important for accessibility.
2072 */
2073 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2074
2075 /**
2076 * The default whether the view is important for accessiblity.
2077 */
2078 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2079
2080 /**
2081 * Mask for obtainig the bits which specify how to determine
2082 * whether a view is important for accessibility.
2083 */
2084 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2085 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2086 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2087
2088 /**
2089 * Flag indicating whether a view has accessibility focus.
2090 */
2091 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2092
2093 /**
2094 * Flag indicating whether a view state for accessibility has changed.
2095 */
2096 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002097
Christopher Tate3d4bf172011-03-28 16:16:46 -07002098 /* End of masks for mPrivateFlags2 */
2099
2100 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2101
Chet Haasedaf98e92011-01-10 14:10:36 -08002102 /**
Adam Powell637d3372010-08-25 14:37:03 -07002103 * Always allow a user to over-scroll this view, provided it is a
2104 * view that can scroll.
2105 *
2106 * @see #getOverScrollMode()
2107 * @see #setOverScrollMode(int)
2108 */
2109 public static final int OVER_SCROLL_ALWAYS = 0;
2110
2111 /**
2112 * Allow a user to over-scroll this view only if the content is large
2113 * enough to meaningfully scroll, provided it is a view that can scroll.
2114 *
2115 * @see #getOverScrollMode()
2116 * @see #setOverScrollMode(int)
2117 */
2118 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2119
2120 /**
2121 * Never allow a user to over-scroll this view.
2122 *
2123 * @see #getOverScrollMode()
2124 * @see #setOverScrollMode(int)
2125 */
2126 public static final int OVER_SCROLL_NEVER = 2;
2127
2128 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002129 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2130 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002131 *
Joe Malin32736f02011-01-19 16:14:20 -08002132 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002133 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002134 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002135
2136 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002137 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2138 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002139 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002140 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002141 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002142 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002143 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002144 *
Joe Malin32736f02011-01-19 16:14:20 -08002145 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002146 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002147 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2148
2149 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002150 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2151 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002152 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002153 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002154 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2155 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2156 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002157 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002158 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2159 * window flags) for displaying content using every last pixel on the display.
2160 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * <p>There is a limitation: because navigation controls are so important, the least user
2162 * interaction will cause them to reappear immediately. When this happens, both
2163 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2164 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002165 *
2166 * @see #setSystemUiVisibility(int)
2167 */
2168 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2169
2170 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002171 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2172 * into the normal fullscreen mode so that its content can take over the screen
2173 * while still allowing the user to interact with the application.
2174 *
2175 * <p>This has the same visual effect as
2176 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2177 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2178 * meaning that non-critical screen decorations (such as the status bar) will be
2179 * hidden while the user is in the View's window, focusing the experience on
2180 * that content. Unlike the window flag, if you are using ActionBar in
2181 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2182 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2183 * hide the action bar.
2184 *
2185 * <p>This approach to going fullscreen is best used over the window flag when
2186 * it is a transient state -- that is, the application does this at certain
2187 * points in its user interaction where it wants to allow the user to focus
2188 * on content, but not as a continuous state. For situations where the application
2189 * would like to simply stay full screen the entire time (such as a game that
2190 * wants to take over the screen), the
2191 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2192 * is usually a better approach. The state set here will be removed by the system
2193 * in various situations (such as the user moving to another application) like
2194 * the other system UI states.
2195 *
2196 * <p>When using this flag, the application should provide some easy facility
2197 * for the user to go out of it. A common example would be in an e-book
2198 * reader, where tapping on the screen brings back whatever screen and UI
2199 * decorations that had been hidden while the user was immersed in reading
2200 * the book.
2201 *
2202 * @see #setSystemUiVisibility(int)
2203 */
2204 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2205
2206 /**
2207 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2208 * flags, we would like a stable view of the content insets given to
2209 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2210 * will always represent the worst case that the application can expect
2211 * as a continue state. In practice this means with any of system bar,
2212 * nav bar, and status bar shown, but not the space that would be needed
2213 * for an input method.
2214 *
2215 * <p>If you are using ActionBar in
2216 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2217 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2218 * insets it adds to those given to the application.
2219 */
2220 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2221
2222 /**
2223 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2224 * to be layed out as if it has requested
2225 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2226 * allows it to avoid artifacts when switching in and out of that mode, at
2227 * the expense that some of its user interface may be covered by screen
2228 * decorations when they are shown. You can perform layout of your inner
2229 * UI elements to account for the navagation system UI through the
2230 * {@link #fitSystemWindows(Rect)} method.
2231 */
2232 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2233
2234 /**
2235 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2236 * to be layed out as if it has requested
2237 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2238 * allows it to avoid artifacts when switching in and out of that mode, at
2239 * the expense that some of its user interface may be covered by screen
2240 * decorations when they are shown. You can perform layout of your inner
2241 * UI elements to account for non-fullscreen system UI through the
2242 * {@link #fitSystemWindows(Rect)} method.
2243 */
2244 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2245
2246 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002247 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2248 */
2249 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2250
2251 /**
2252 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2253 */
2254 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002255
2256 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002257 * @hide
2258 *
2259 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2260 * out of the public fields to keep the undefined bits out of the developer's way.
2261 *
2262 * Flag to make the status bar not expandable. Unless you also
2263 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2264 */
2265 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2266
2267 /**
2268 * @hide
2269 *
2270 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2271 * out of the public fields to keep the undefined bits out of the developer's way.
2272 *
2273 * Flag to hide notification icons and scrolling ticker text.
2274 */
2275 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2276
2277 /**
2278 * @hide
2279 *
2280 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2281 * out of the public fields to keep the undefined bits out of the developer's way.
2282 *
2283 * Flag to disable incoming notification alerts. This will not block
2284 * icons, but it will block sound, vibrating and other visual or aural notifications.
2285 */
2286 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2287
2288 /**
2289 * @hide
2290 *
2291 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2292 * out of the public fields to keep the undefined bits out of the developer's way.
2293 *
2294 * Flag to hide only the scrolling ticker. Note that
2295 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2296 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2297 */
2298 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2299
2300 /**
2301 * @hide
2302 *
2303 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2304 * out of the public fields to keep the undefined bits out of the developer's way.
2305 *
2306 * Flag to hide the center system info area.
2307 */
2308 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2309
2310 /**
2311 * @hide
2312 *
2313 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2314 * out of the public fields to keep the undefined bits out of the developer's way.
2315 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002316 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002317 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2318 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002319 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002320
2321 /**
2322 * @hide
2323 *
2324 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2325 * out of the public fields to keep the undefined bits out of the developer's way.
2326 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002327 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002328 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2329 */
2330 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2331
2332 /**
2333 * @hide
2334 *
2335 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2336 * out of the public fields to keep the undefined bits out of the developer's way.
2337 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002338 * Flag to hide only the clock. You might use this if your activity has
2339 * its own clock making the status bar's clock redundant.
2340 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002341 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2342
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002343 /**
2344 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002345 *
2346 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2347 * out of the public fields to keep the undefined bits out of the developer's way.
2348 *
2349 * Flag to hide only the recent apps button. Don't use this
2350 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2351 */
2352 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2353
2354 /**
2355 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002357 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002358
2359 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002360 * These are the system UI flags that can be cleared by events outside
2361 * of an application. Currently this is just the ability to tap on the
2362 * screen while hiding the navigation bar to have it return.
2363 * @hide
2364 */
2365 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002366 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2367 | SYSTEM_UI_FLAG_FULLSCREEN;
2368
2369 /**
2370 * Flags that can impact the layout in relation to system UI.
2371 */
2372 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2373 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2374 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002375
2376 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002377 * Find views that render the specified text.
2378 *
2379 * @see #findViewsWithText(ArrayList, CharSequence, int)
2380 */
2381 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2382
2383 /**
2384 * Find find views that contain the specified content description.
2385 *
2386 * @see #findViewsWithText(ArrayList, CharSequence, int)
2387 */
2388 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2389
2390 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002391 * Find views that contain {@link AccessibilityNodeProvider}. Such
2392 * a View is a root of virtual view hierarchy and may contain the searched
2393 * text. If this flag is set Views with providers are automatically
2394 * added and it is a responsibility of the client to call the APIs of
2395 * the provider to determine whether the virtual tree rooted at this View
2396 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2397 * represeting the virtual views with this text.
2398 *
2399 * @see #findViewsWithText(ArrayList, CharSequence, int)
2400 *
2401 * @hide
2402 */
2403 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2404
2405 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002406 * Indicates that the screen has changed state and is now off.
2407 *
2408 * @see #onScreenStateChanged(int)
2409 */
2410 public static final int SCREEN_STATE_OFF = 0x0;
2411
2412 /**
2413 * Indicates that the screen has changed state and is now on.
2414 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002415 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002416 */
2417 public static final int SCREEN_STATE_ON = 0x1;
2418
2419 /**
Adam Powell637d3372010-08-25 14:37:03 -07002420 * Controls the over-scroll mode for this view.
2421 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2422 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2423 * and {@link #OVER_SCROLL_NEVER}.
2424 */
2425 private int mOverScrollMode;
2426
2427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 * The parent this view is attached to.
2429 * {@hide}
2430 *
2431 * @see #getParent()
2432 */
2433 protected ViewParent mParent;
2434
2435 /**
2436 * {@hide}
2437 */
2438 AttachInfo mAttachInfo;
2439
2440 /**
2441 * {@hide}
2442 */
Romain Guy809a7f62009-05-14 15:44:42 -07002443 @ViewDebug.ExportedProperty(flagMapping = {
2444 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2445 name = "FORCE_LAYOUT"),
2446 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2447 name = "LAYOUT_REQUIRED"),
2448 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002449 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002450 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2451 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2452 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2453 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2454 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002456 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457
2458 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002459 * This view's request for the visibility of the status bar.
2460 * @hide
2461 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002462 @ViewDebug.ExportedProperty(flagMapping = {
2463 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2464 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2465 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2466 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2467 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2468 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2469 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2470 equals = SYSTEM_UI_FLAG_VISIBLE,
2471 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2472 })
Joe Onorato664644d2011-01-23 17:53:23 -08002473 int mSystemUiVisibility;
2474
2475 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002476 * Reference count for transient state.
2477 * @see #setHasTransientState(boolean)
2478 */
2479 int mTransientStateCount = 0;
2480
2481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 * Count of how many windows this view has been attached to.
2483 */
2484 int mWindowAttachCount;
2485
2486 /**
2487 * The layout parameters associated with this view and used by the parent
2488 * {@link android.view.ViewGroup} to determine how this view should be
2489 * laid out.
2490 * {@hide}
2491 */
2492 protected ViewGroup.LayoutParams mLayoutParams;
2493
2494 /**
2495 * The view flags hold various views states.
2496 * {@hide}
2497 */
2498 @ViewDebug.ExportedProperty
2499 int mViewFlags;
2500
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002501 static class TransformationInfo {
2502 /**
2503 * The transform matrix for the View. This transform is calculated internally
2504 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2505 * is used by default. Do *not* use this variable directly; instead call
2506 * getMatrix(), which will automatically recalculate the matrix if necessary
2507 * to get the correct matrix based on the latest rotation and scale properties.
2508 */
2509 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002510
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002511 /**
2512 * The transform matrix for the View. This transform is calculated internally
2513 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2514 * is used by default. Do *not* use this variable directly; instead call
2515 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2516 * to get the correct matrix based on the latest rotation and scale properties.
2517 */
2518 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002519
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002520 /**
2521 * An internal variable that tracks whether we need to recalculate the
2522 * transform matrix, based on whether the rotation or scaleX/Y properties
2523 * have changed since the matrix was last calculated.
2524 */
2525 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002526
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002527 /**
2528 * An internal variable that tracks whether we need to recalculate the
2529 * transform matrix, based on whether the rotation or scaleX/Y properties
2530 * have changed since the matrix was last calculated.
2531 */
2532 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002533
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002534 /**
2535 * A variable that tracks whether we need to recalculate the
2536 * transform matrix, based on whether the rotation or scaleX/Y properties
2537 * have changed since the matrix was last calculated. This variable
2538 * is only valid after a call to updateMatrix() or to a function that
2539 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2540 */
2541 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002542
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002543 /**
2544 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2545 */
2546 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002547
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002548 /**
2549 * This matrix is used when computing the matrix for 3D rotations.
2550 */
2551 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * These prev values are used to recalculate a centered pivot point when necessary. The
2555 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2556 * set), so thes values are only used then as well.
2557 */
2558 private int mPrevWidth = -1;
2559 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002560
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002561 /**
2562 * The degrees rotation around the vertical axis through the pivot point.
2563 */
2564 @ViewDebug.ExportedProperty
2565 float mRotationY = 0f;
2566
2567 /**
2568 * The degrees rotation around the horizontal axis through the pivot point.
2569 */
2570 @ViewDebug.ExportedProperty
2571 float mRotationX = 0f;
2572
2573 /**
2574 * The degrees rotation around the pivot point.
2575 */
2576 @ViewDebug.ExportedProperty
2577 float mRotation = 0f;
2578
2579 /**
2580 * The amount of translation of the object away from its left property (post-layout).
2581 */
2582 @ViewDebug.ExportedProperty
2583 float mTranslationX = 0f;
2584
2585 /**
2586 * The amount of translation of the object away from its top property (post-layout).
2587 */
2588 @ViewDebug.ExportedProperty
2589 float mTranslationY = 0f;
2590
2591 /**
2592 * The amount of scale in the x direction around the pivot point. A
2593 * value of 1 means no scaling is applied.
2594 */
2595 @ViewDebug.ExportedProperty
2596 float mScaleX = 1f;
2597
2598 /**
2599 * The amount of scale in the y direction around the pivot point. A
2600 * value of 1 means no scaling is applied.
2601 */
2602 @ViewDebug.ExportedProperty
2603 float mScaleY = 1f;
2604
2605 /**
Chet Haasea33de552012-02-03 16:28:24 -08002606 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002607 */
2608 @ViewDebug.ExportedProperty
2609 float mPivotX = 0f;
2610
2611 /**
Chet Haasea33de552012-02-03 16:28:24 -08002612 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002613 */
2614 @ViewDebug.ExportedProperty
2615 float mPivotY = 0f;
2616
2617 /**
2618 * The opacity of the View. This is a value from 0 to 1, where 0 means
2619 * completely transparent and 1 means completely opaque.
2620 */
2621 @ViewDebug.ExportedProperty
2622 float mAlpha = 1f;
2623 }
2624
2625 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002626
Joe Malin32736f02011-01-19 16:14:20 -08002627 private boolean mLastIsOpaque;
2628
Chet Haasefd2b0022010-08-06 13:08:56 -07002629 /**
2630 * Convenience value to check for float values that are close enough to zero to be considered
2631 * zero.
2632 */
Romain Guy2542d192010-08-18 11:47:12 -07002633 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002634
2635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 * The distance in pixels from the left edge of this view's parent
2637 * to the left edge of this view.
2638 * {@hide}
2639 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002640 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 protected int mLeft;
2642 /**
2643 * The distance in pixels from the left edge of this view's parent
2644 * to the right edge of this view.
2645 * {@hide}
2646 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002647 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 protected int mRight;
2649 /**
2650 * The distance in pixels from the top edge of this view's parent
2651 * to the top edge of this view.
2652 * {@hide}
2653 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002654 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 protected int mTop;
2656 /**
2657 * The distance in pixels from the top edge of this view's parent
2658 * to the bottom edge of this view.
2659 * {@hide}
2660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002661 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 protected int mBottom;
2663
2664 /**
2665 * The offset, in pixels, by which the content of this view is scrolled
2666 * horizontally.
2667 * {@hide}
2668 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002669 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 protected int mScrollX;
2671 /**
2672 * The offset, in pixels, by which the content of this view is scrolled
2673 * vertically.
2674 * {@hide}
2675 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002676 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 protected int mScrollY;
2678
2679 /**
2680 * The left padding in pixels, that is the distance in pixels between the
2681 * left edge of this view and the left edge of its content.
2682 * {@hide}
2683 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002684 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 protected int mPaddingLeft;
2686 /**
2687 * The right padding in pixels, that is the distance in pixels between the
2688 * right edge of this view and the right edge of its content.
2689 * {@hide}
2690 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002691 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 protected int mPaddingRight;
2693 /**
2694 * The top padding in pixels, that is the distance in pixels between the
2695 * top edge of this view and the top edge of its content.
2696 * {@hide}
2697 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002698 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 protected int mPaddingTop;
2700 /**
2701 * The bottom padding in pixels, that is the distance in pixels between the
2702 * bottom edge of this view and the bottom edge of its content.
2703 * {@hide}
2704 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002705 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 protected int mPaddingBottom;
2707
2708 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002709 * The layout insets in pixels, that is the distance in pixels between the
2710 * visible edges of this view its bounds.
2711 */
2712 private Insets mLayoutInsets;
2713
2714 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002715 * Briefly describes the view and is primarily used for accessibility support.
2716 */
2717 private CharSequence mContentDescription;
2718
2719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002721 *
2722 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002725 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726
2727 /**
2728 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002729 *
2730 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002732 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002733 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002735 /**
Adam Powell20232d02010-12-08 21:08:53 -08002736 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002737 *
2738 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002739 */
2740 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002741 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002742
2743 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002744 * Cache if the user padding is relative.
2745 *
2746 */
2747 @ViewDebug.ExportedProperty(category = "padding")
2748 boolean mUserPaddingRelative;
2749
2750 /**
2751 * Cache the paddingStart set by the user to append to the scrollbar's size.
2752 *
2753 */
2754 @ViewDebug.ExportedProperty(category = "padding")
2755 int mUserPaddingStart;
2756
2757 /**
2758 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2759 *
2760 */
2761 @ViewDebug.ExportedProperty(category = "padding")
2762 int mUserPaddingEnd;
2763
2764 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002765 * @hide
2766 */
2767 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2768 /**
2769 * @hide
2770 */
2771 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772
Philip Milne6c8ea062012-04-03 17:38:43 -07002773 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774
2775 private int mBackgroundResource;
2776 private boolean mBackgroundSizeChanged;
2777
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002778 static class ListenerInfo {
2779 /**
2780 * Listener used to dispatch focus change events.
2781 * This field should be made private, so it is hidden from the SDK.
2782 * {@hide}
2783 */
2784 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002786 /**
2787 * Listeners for layout change events.
2788 */
2789 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002790
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002791 /**
2792 * Listeners for attach events.
2793 */
2794 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002795
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002796 /**
2797 * Listener used to dispatch click events.
2798 * This field should be made private, so it is hidden from the SDK.
2799 * {@hide}
2800 */
2801 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002803 /**
2804 * Listener used to dispatch long click events.
2805 * This field should be made private, so it is hidden from the SDK.
2806 * {@hide}
2807 */
2808 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002810 /**
2811 * Listener used to build the context menu.
2812 * This field should be made private, so it is hidden from the SDK.
2813 * {@hide}
2814 */
2815 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002817 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002819 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002821 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002822
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002823 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002824
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002825 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002826
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002827 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2828 }
2829
2830 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 /**
2833 * The application environment this view lives in.
2834 * This field should be made private, so it is hidden from the SDK.
2835 * {@hide}
2836 */
2837 protected Context mContext;
2838
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002839 private final Resources mResources;
2840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 private ScrollabilityCache mScrollCache;
2842
2843 private int[] mDrawableState = null;
2844
Romain Guy0211a0a2011-02-14 16:34:59 -08002845 /**
2846 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002847 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002848 * @hide
2849 */
2850 public boolean mCachingFailed;
2851
Romain Guy02890fd2010-08-06 17:58:44 -07002852 private Bitmap mDrawingCache;
2853 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002854 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002855 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
2857 /**
2858 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2859 * the user may specify which view to go to next.
2860 */
2861 private int mNextFocusLeftId = View.NO_ID;
2862
2863 /**
2864 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2865 * the user may specify which view to go to next.
2866 */
2867 private int mNextFocusRightId = View.NO_ID;
2868
2869 /**
2870 * When this view has focus and the next focus is {@link #FOCUS_UP},
2871 * the user may specify which view to go to next.
2872 */
2873 private int mNextFocusUpId = View.NO_ID;
2874
2875 /**
2876 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2877 * the user may specify which view to go to next.
2878 */
2879 private int mNextFocusDownId = View.NO_ID;
2880
Jeff Brown4e6319b2010-12-13 10:36:51 -08002881 /**
2882 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2883 * the user may specify which view to go to next.
2884 */
2885 int mNextFocusForwardId = View.NO_ID;
2886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002888 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002889 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002890 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 private UnsetPressedState mUnsetPressedState;
2893
2894 /**
2895 * Whether the long press's action has been invoked. The tap's action is invoked on the
2896 * up event while a long press is invoked as soon as the long press duration is reached, so
2897 * a long press could be performed before the tap is checked, in which case the tap's action
2898 * should not be invoked.
2899 */
2900 private boolean mHasPerformedLongPress;
2901
2902 /**
2903 * The minimum height of the view. We'll try our best to have the height
2904 * of this view to at least this amount.
2905 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002906 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 private int mMinHeight;
2908
2909 /**
2910 * The minimum width of the view. We'll try our best to have the width
2911 * of this view to at least this amount.
2912 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002913 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 private int mMinWidth;
2915
2916 /**
2917 * The delegate to handle touch events that are physically in this view
2918 * but should be handled by another view.
2919 */
2920 private TouchDelegate mTouchDelegate = null;
2921
2922 /**
2923 * Solid color to use as a background when creating the drawing cache. Enables
2924 * the cache to use 16 bit bitmaps instead of 32 bit.
2925 */
2926 private int mDrawingCacheBackgroundColor = 0;
2927
2928 /**
2929 * Special tree observer used when mAttachInfo is null.
2930 */
2931 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002932
Adam Powelle14579b2009-12-16 18:39:52 -08002933 /**
2934 * Cache the touch slop from the context that created the view.
2935 */
2936 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002939 * Object that handles automatic animation of view properties.
2940 */
2941 private ViewPropertyAnimator mAnimator = null;
2942
2943 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002944 * Flag indicating that a drag can cross window boundaries. When
2945 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2946 * with this flag set, all visible applications will be able to participate
2947 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002948 *
2949 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002950 */
2951 public static final int DRAG_FLAG_GLOBAL = 1;
2952
2953 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002954 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2955 */
2956 private float mVerticalScrollFactor;
2957
2958 /**
Adam Powell20232d02010-12-08 21:08:53 -08002959 * Position of the vertical scroll bar.
2960 */
2961 private int mVerticalScrollbarPosition;
2962
2963 /**
2964 * Position the scroll bar at the default position as determined by the system.
2965 */
2966 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2967
2968 /**
2969 * Position the scroll bar along the left edge.
2970 */
2971 public static final int SCROLLBAR_POSITION_LEFT = 1;
2972
2973 /**
2974 * Position the scroll bar along the right edge.
2975 */
2976 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2977
2978 /**
Romain Guy171c5922011-01-06 10:04:23 -08002979 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002980 *
2981 * @see #getLayerType()
2982 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002983 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002984 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002985 */
2986 public static final int LAYER_TYPE_NONE = 0;
2987
2988 /**
2989 * <p>Indicates that the view has a software layer. A software layer is backed
2990 * by a bitmap and causes the view to be rendered using Android's software
2991 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002992 *
Romain Guy171c5922011-01-06 10:04:23 -08002993 * <p>Software layers have various usages:</p>
2994 * <p>When the application is not using hardware acceleration, a software layer
2995 * is useful to apply a specific color filter and/or blending mode and/or
2996 * translucency to a view and all its children.</p>
2997 * <p>When the application is using hardware acceleration, a software layer
2998 * is useful to render drawing primitives not supported by the hardware
2999 * accelerated pipeline. It can also be used to cache a complex view tree
3000 * into a texture and reduce the complexity of drawing operations. For instance,
3001 * when animating a complex view tree with a translation, a software layer can
3002 * be used to render the view tree only once.</p>
3003 * <p>Software layers should be avoided when the affected view tree updates
3004 * often. Every update will require to re-render the software layer, which can
3005 * potentially be slow (particularly when hardware acceleration is turned on
3006 * since the layer will have to be uploaded into a hardware texture after every
3007 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003008 *
3009 * @see #getLayerType()
3010 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003011 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003012 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003013 */
3014 public static final int LAYER_TYPE_SOFTWARE = 1;
3015
3016 /**
3017 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3018 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3019 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3020 * rendering pipeline, but only if hardware acceleration is turned on for the
3021 * view hierarchy. When hardware acceleration is turned off, hardware layers
3022 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003023 *
Romain Guy171c5922011-01-06 10:04:23 -08003024 * <p>A hardware layer is useful to apply a specific color filter and/or
3025 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003026 * <p>A hardware layer can be used to cache a complex view tree into a
3027 * texture and reduce the complexity of drawing operations. For instance,
3028 * when animating a complex view tree with a translation, a hardware layer can
3029 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003030 * <p>A hardware layer can also be used to increase the rendering quality when
3031 * rotation transformations are applied on a view. It can also be used to
3032 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003033 *
3034 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003035 * @see #setLayerType(int, android.graphics.Paint)
3036 * @see #LAYER_TYPE_NONE
3037 * @see #LAYER_TYPE_SOFTWARE
3038 */
3039 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003040
Romain Guy3aaff3a2011-01-12 14:18:47 -08003041 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3042 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3043 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3044 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3045 })
Romain Guy171c5922011-01-06 10:04:23 -08003046 int mLayerType = LAYER_TYPE_NONE;
3047 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003048 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003049
3050 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003051 * Set to true when the view is sending hover accessibility events because it
3052 * is the innermost hovered view.
3053 */
3054 private boolean mSendingHoverAccessibilityEvents;
3055
3056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 * Simple constructor to use when creating a view from code.
3058 *
3059 * @param context The Context the view is running in, through which it can
3060 * access the current theme, resources, etc.
3061 */
3062 public View(Context context) {
3063 mContext = context;
3064 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003065 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003066 // Set layout and text direction defaults
3067 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003068 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003069 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3070 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003071 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003072 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003073 mUserPaddingStart = -1;
3074 mUserPaddingEnd = -1;
3075 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
3077
3078 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003079 * Delegate for injecting accessiblity functionality.
3080 */
3081 AccessibilityDelegate mAccessibilityDelegate;
3082
3083 /**
3084 * Consistency verifier for debugging purposes.
3085 * @hide
3086 */
3087 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3088 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3089 new InputEventConsistencyVerifier(this, 0) : null;
3090
3091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 * Constructor that is called when inflating a view from XML. This is called
3093 * when a view is being constructed from an XML file, supplying attributes
3094 * that were specified in the XML file. This version uses a default style of
3095 * 0, so the only attribute values applied are those in the Context's Theme
3096 * and the given AttributeSet.
3097 *
3098 * <p>
3099 * The method onFinishInflate() will be called after all children have been
3100 * added.
3101 *
3102 * @param context The Context the view is running in, through which it can
3103 * access the current theme, resources, etc.
3104 * @param attrs The attributes of the XML tag that is inflating the view.
3105 * @see #View(Context, AttributeSet, int)
3106 */
3107 public View(Context context, AttributeSet attrs) {
3108 this(context, attrs, 0);
3109 }
3110
3111 /**
3112 * Perform inflation from XML and apply a class-specific base style. This
3113 * constructor of View allows subclasses to use their own base style when
3114 * they are inflating. For example, a Button class's constructor would call
3115 * this version of the super class constructor and supply
3116 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3117 * the theme's button style to modify all of the base view attributes (in
3118 * particular its background) as well as the Button class's attributes.
3119 *
3120 * @param context The Context the view is running in, through which it can
3121 * access the current theme, resources, etc.
3122 * @param attrs The attributes of the XML tag that is inflating the view.
3123 * @param defStyle The default style to apply to this view. If 0, no style
3124 * will be applied (beyond what is included in the theme). This may
3125 * either be an attribute resource, whose value will be retrieved
3126 * from the current theme, or an explicit style resource.
3127 * @see #View(Context, AttributeSet)
3128 */
3129 public View(Context context, AttributeSet attrs, int defStyle) {
3130 this(context);
3131
3132 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3133 defStyle, 0);
3134
3135 Drawable background = null;
3136
3137 int leftPadding = -1;
3138 int topPadding = -1;
3139 int rightPadding = -1;
3140 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003141 int startPadding = -1;
3142 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143
3144 int padding = -1;
3145
3146 int viewFlagValues = 0;
3147 int viewFlagMasks = 0;
3148
3149 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 int x = 0;
3152 int y = 0;
3153
Chet Haase73066682010-11-29 15:55:32 -08003154 float tx = 0;
3155 float ty = 0;
3156 float rotation = 0;
3157 float rotationX = 0;
3158 float rotationY = 0;
3159 float sx = 1f;
3160 float sy = 1f;
3161 boolean transformSet = false;
3162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3164
Adam Powell637d3372010-08-25 14:37:03 -07003165 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 final int N = a.getIndexCount();
3167 for (int i = 0; i < N; i++) {
3168 int attr = a.getIndex(i);
3169 switch (attr) {
3170 case com.android.internal.R.styleable.View_background:
3171 background = a.getDrawable(attr);
3172 break;
3173 case com.android.internal.R.styleable.View_padding:
3174 padding = a.getDimensionPixelSize(attr, -1);
3175 break;
3176 case com.android.internal.R.styleable.View_paddingLeft:
3177 leftPadding = a.getDimensionPixelSize(attr, -1);
3178 break;
3179 case com.android.internal.R.styleable.View_paddingTop:
3180 topPadding = a.getDimensionPixelSize(attr, -1);
3181 break;
3182 case com.android.internal.R.styleable.View_paddingRight:
3183 rightPadding = a.getDimensionPixelSize(attr, -1);
3184 break;
3185 case com.android.internal.R.styleable.View_paddingBottom:
3186 bottomPadding = a.getDimensionPixelSize(attr, -1);
3187 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003188 case com.android.internal.R.styleable.View_paddingStart:
3189 startPadding = a.getDimensionPixelSize(attr, -1);
3190 break;
3191 case com.android.internal.R.styleable.View_paddingEnd:
3192 endPadding = a.getDimensionPixelSize(attr, -1);
3193 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 case com.android.internal.R.styleable.View_scrollX:
3195 x = a.getDimensionPixelOffset(attr, 0);
3196 break;
3197 case com.android.internal.R.styleable.View_scrollY:
3198 y = a.getDimensionPixelOffset(attr, 0);
3199 break;
Chet Haase73066682010-11-29 15:55:32 -08003200 case com.android.internal.R.styleable.View_alpha:
3201 setAlpha(a.getFloat(attr, 1f));
3202 break;
3203 case com.android.internal.R.styleable.View_transformPivotX:
3204 setPivotX(a.getDimensionPixelOffset(attr, 0));
3205 break;
3206 case com.android.internal.R.styleable.View_transformPivotY:
3207 setPivotY(a.getDimensionPixelOffset(attr, 0));
3208 break;
3209 case com.android.internal.R.styleable.View_translationX:
3210 tx = a.getDimensionPixelOffset(attr, 0);
3211 transformSet = true;
3212 break;
3213 case com.android.internal.R.styleable.View_translationY:
3214 ty = a.getDimensionPixelOffset(attr, 0);
3215 transformSet = true;
3216 break;
3217 case com.android.internal.R.styleable.View_rotation:
3218 rotation = a.getFloat(attr, 0);
3219 transformSet = true;
3220 break;
3221 case com.android.internal.R.styleable.View_rotationX:
3222 rotationX = a.getFloat(attr, 0);
3223 transformSet = true;
3224 break;
3225 case com.android.internal.R.styleable.View_rotationY:
3226 rotationY = a.getFloat(attr, 0);
3227 transformSet = true;
3228 break;
3229 case com.android.internal.R.styleable.View_scaleX:
3230 sx = a.getFloat(attr, 1f);
3231 transformSet = true;
3232 break;
3233 case com.android.internal.R.styleable.View_scaleY:
3234 sy = a.getFloat(attr, 1f);
3235 transformSet = true;
3236 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 case com.android.internal.R.styleable.View_id:
3238 mID = a.getResourceId(attr, NO_ID);
3239 break;
3240 case com.android.internal.R.styleable.View_tag:
3241 mTag = a.getText(attr);
3242 break;
3243 case com.android.internal.R.styleable.View_fitsSystemWindows:
3244 if (a.getBoolean(attr, false)) {
3245 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3246 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3247 }
3248 break;
3249 case com.android.internal.R.styleable.View_focusable:
3250 if (a.getBoolean(attr, false)) {
3251 viewFlagValues |= FOCUSABLE;
3252 viewFlagMasks |= FOCUSABLE_MASK;
3253 }
3254 break;
3255 case com.android.internal.R.styleable.View_focusableInTouchMode:
3256 if (a.getBoolean(attr, false)) {
3257 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3258 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3259 }
3260 break;
3261 case com.android.internal.R.styleable.View_clickable:
3262 if (a.getBoolean(attr, false)) {
3263 viewFlagValues |= CLICKABLE;
3264 viewFlagMasks |= CLICKABLE;
3265 }
3266 break;
3267 case com.android.internal.R.styleable.View_longClickable:
3268 if (a.getBoolean(attr, false)) {
3269 viewFlagValues |= LONG_CLICKABLE;
3270 viewFlagMasks |= LONG_CLICKABLE;
3271 }
3272 break;
3273 case com.android.internal.R.styleable.View_saveEnabled:
3274 if (!a.getBoolean(attr, true)) {
3275 viewFlagValues |= SAVE_DISABLED;
3276 viewFlagMasks |= SAVE_DISABLED_MASK;
3277 }
3278 break;
3279 case com.android.internal.R.styleable.View_duplicateParentState:
3280 if (a.getBoolean(attr, false)) {
3281 viewFlagValues |= DUPLICATE_PARENT_STATE;
3282 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3283 }
3284 break;
3285 case com.android.internal.R.styleable.View_visibility:
3286 final int visibility = a.getInt(attr, 0);
3287 if (visibility != 0) {
3288 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3289 viewFlagMasks |= VISIBILITY_MASK;
3290 }
3291 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003292 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003293 // Clear any layout direction flags (included resolved bits) already set
3294 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3295 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003296 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003297 final int value = (layoutDirection != -1) ?
3298 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3299 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003300 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 case com.android.internal.R.styleable.View_drawingCacheQuality:
3302 final int cacheQuality = a.getInt(attr, 0);
3303 if (cacheQuality != 0) {
3304 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3305 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3306 }
3307 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003308 case com.android.internal.R.styleable.View_contentDescription:
3309 mContentDescription = a.getString(attr);
3310 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3312 if (!a.getBoolean(attr, true)) {
3313 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3314 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3315 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003316 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3318 if (!a.getBoolean(attr, true)) {
3319 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3320 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3321 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003322 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 case R.styleable.View_scrollbars:
3324 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3325 if (scrollbars != SCROLLBARS_NONE) {
3326 viewFlagValues |= scrollbars;
3327 viewFlagMasks |= SCROLLBARS_MASK;
3328 initializeScrollbars(a);
3329 }
3330 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003331 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003333 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3334 // Ignore the attribute starting with ICS
3335 break;
3336 }
3337 // With builds < ICS, fall through and apply fading edges
3338 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3340 if (fadingEdge != FADING_EDGE_NONE) {
3341 viewFlagValues |= fadingEdge;
3342 viewFlagMasks |= FADING_EDGE_MASK;
3343 initializeFadingEdge(a);
3344 }
3345 break;
3346 case R.styleable.View_scrollbarStyle:
3347 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3348 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3349 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3350 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3351 }
3352 break;
3353 case R.styleable.View_isScrollContainer:
3354 setScrollContainer = true;
3355 if (a.getBoolean(attr, false)) {
3356 setScrollContainer(true);
3357 }
3358 break;
3359 case com.android.internal.R.styleable.View_keepScreenOn:
3360 if (a.getBoolean(attr, false)) {
3361 viewFlagValues |= KEEP_SCREEN_ON;
3362 viewFlagMasks |= KEEP_SCREEN_ON;
3363 }
3364 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003365 case R.styleable.View_filterTouchesWhenObscured:
3366 if (a.getBoolean(attr, false)) {
3367 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3368 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3369 }
3370 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 case R.styleable.View_nextFocusLeft:
3372 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3373 break;
3374 case R.styleable.View_nextFocusRight:
3375 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3376 break;
3377 case R.styleable.View_nextFocusUp:
3378 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3379 break;
3380 case R.styleable.View_nextFocusDown:
3381 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3382 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003383 case R.styleable.View_nextFocusForward:
3384 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3385 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 case R.styleable.View_minWidth:
3387 mMinWidth = a.getDimensionPixelSize(attr, 0);
3388 break;
3389 case R.styleable.View_minHeight:
3390 mMinHeight = a.getDimensionPixelSize(attr, 0);
3391 break;
Romain Guy9a817362009-05-01 10:57:14 -07003392 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003393 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003394 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003395 + "be used within a restricted context");
3396 }
3397
Romain Guy9a817362009-05-01 10:57:14 -07003398 final String handlerName = a.getString(attr);
3399 if (handlerName != null) {
3400 setOnClickListener(new OnClickListener() {
3401 private Method mHandler;
3402
3403 public void onClick(View v) {
3404 if (mHandler == null) {
3405 try {
3406 mHandler = getContext().getClass().getMethod(handlerName,
3407 View.class);
3408 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003409 int id = getId();
3410 String idText = id == NO_ID ? "" : " with id '"
3411 + getContext().getResources().getResourceEntryName(
3412 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003413 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003414 handlerName + "(View) in the activity "
3415 + getContext().getClass() + " for onClick handler"
3416 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003417 }
3418 }
3419
3420 try {
3421 mHandler.invoke(getContext(), View.this);
3422 } catch (IllegalAccessException e) {
3423 throw new IllegalStateException("Could not execute non "
3424 + "public method of the activity", e);
3425 } catch (InvocationTargetException e) {
3426 throw new IllegalStateException("Could not execute "
3427 + "method of the activity", e);
3428 }
3429 }
3430 });
3431 }
3432 break;
Adam Powell637d3372010-08-25 14:37:03 -07003433 case R.styleable.View_overScrollMode:
3434 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3435 break;
Adam Powell20232d02010-12-08 21:08:53 -08003436 case R.styleable.View_verticalScrollbarPosition:
3437 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3438 break;
Romain Guy171c5922011-01-06 10:04:23 -08003439 case R.styleable.View_layerType:
3440 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3441 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003442 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003443 // Clear any text direction flag already set
3444 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3445 // Set the text direction flags depending on the value of the attribute
3446 final int textDirection = a.getInt(attr, -1);
3447 if (textDirection != -1) {
3448 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3449 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003450 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003451 case R.styleable.View_textAlignment:
3452 // Clear any text alignment flag already set
3453 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3454 // Set the text alignment flag depending on the value of the attribute
3455 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3456 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3457 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003458 case R.styleable.View_importantForAccessibility:
3459 setImportantForAccessibility(a.getInt(attr,
3460 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 }
3462 }
3463
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003464 a.recycle();
3465
Adam Powell637d3372010-08-25 14:37:03 -07003466 setOverScrollMode(overScrollMode);
3467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003469 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 }
3471
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003472 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3473 // layout direction). Those cached values will be used later during padding resolution.
3474 mUserPaddingStart = startPadding;
3475 mUserPaddingEnd = endPadding;
3476
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003477 updateUserPaddingRelative();
3478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 if (padding >= 0) {
3480 leftPadding = padding;
3481 topPadding = padding;
3482 rightPadding = padding;
3483 bottomPadding = padding;
3484 }
3485
3486 // If the user specified the padding (either with android:padding or
3487 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3488 // use the default padding or the padding from the background drawable
3489 // (stored at this point in mPadding*)
3490 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3491 topPadding >= 0 ? topPadding : mPaddingTop,
3492 rightPadding >= 0 ? rightPadding : mPaddingRight,
3493 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3494
3495 if (viewFlagMasks != 0) {
3496 setFlags(viewFlagValues, viewFlagMasks);
3497 }
3498
3499 // Needs to be called after mViewFlags is set
3500 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3501 recomputePadding();
3502 }
3503
3504 if (x != 0 || y != 0) {
3505 scrollTo(x, y);
3506 }
3507
Chet Haase73066682010-11-29 15:55:32 -08003508 if (transformSet) {
3509 setTranslationX(tx);
3510 setTranslationY(ty);
3511 setRotation(rotation);
3512 setRotationX(rotationX);
3513 setRotationY(rotationY);
3514 setScaleX(sx);
3515 setScaleY(sy);
3516 }
3517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3519 setScrollContainer(true);
3520 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003521
3522 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003525 private void updateUserPaddingRelative() {
3526 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3527 }
3528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 /**
3530 * Non-public constructor for use in testing
3531 */
3532 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003533 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 }
3535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 /**
3537 * <p>
3538 * Initializes the fading edges from a given set of styled attributes. This
3539 * method should be called by subclasses that need fading edges and when an
3540 * instance of these subclasses is created programmatically rather than
3541 * being inflated from XML. This method is automatically called when the XML
3542 * is inflated.
3543 * </p>
3544 *
3545 * @param a the styled attributes set to initialize the fading edges from
3546 */
3547 protected void initializeFadingEdge(TypedArray a) {
3548 initScrollCache();
3549
3550 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3551 R.styleable.View_fadingEdgeLength,
3552 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3553 }
3554
3555 /**
3556 * Returns the size of the vertical faded edges used to indicate that more
3557 * content in this view is visible.
3558 *
3559 * @return The size in pixels of the vertical faded edge or 0 if vertical
3560 * faded edges are not enabled for this view.
3561 * @attr ref android.R.styleable#View_fadingEdgeLength
3562 */
3563 public int getVerticalFadingEdgeLength() {
3564 if (isVerticalFadingEdgeEnabled()) {
3565 ScrollabilityCache cache = mScrollCache;
3566 if (cache != null) {
3567 return cache.fadingEdgeLength;
3568 }
3569 }
3570 return 0;
3571 }
3572
3573 /**
3574 * Set the size of the faded edge used to indicate that more content in this
3575 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003576 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3577 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3578 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 *
3580 * @param length The size in pixels of the faded edge used to indicate that more
3581 * content in this view is visible.
3582 */
3583 public void setFadingEdgeLength(int length) {
3584 initScrollCache();
3585 mScrollCache.fadingEdgeLength = length;
3586 }
3587
3588 /**
3589 * Returns the size of the horizontal faded edges used to indicate that more
3590 * content in this view is visible.
3591 *
3592 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3593 * faded edges are not enabled for this view.
3594 * @attr ref android.R.styleable#View_fadingEdgeLength
3595 */
3596 public int getHorizontalFadingEdgeLength() {
3597 if (isHorizontalFadingEdgeEnabled()) {
3598 ScrollabilityCache cache = mScrollCache;
3599 if (cache != null) {
3600 return cache.fadingEdgeLength;
3601 }
3602 }
3603 return 0;
3604 }
3605
3606 /**
3607 * Returns the width of the vertical scrollbar.
3608 *
3609 * @return The width in pixels of the vertical scrollbar or 0 if there
3610 * is no vertical scrollbar.
3611 */
3612 public int getVerticalScrollbarWidth() {
3613 ScrollabilityCache cache = mScrollCache;
3614 if (cache != null) {
3615 ScrollBarDrawable scrollBar = cache.scrollBar;
3616 if (scrollBar != null) {
3617 int size = scrollBar.getSize(true);
3618 if (size <= 0) {
3619 size = cache.scrollBarSize;
3620 }
3621 return size;
3622 }
3623 return 0;
3624 }
3625 return 0;
3626 }
3627
3628 /**
3629 * Returns the height of the horizontal scrollbar.
3630 *
3631 * @return The height in pixels of the horizontal scrollbar or 0 if
3632 * there is no horizontal scrollbar.
3633 */
3634 protected int getHorizontalScrollbarHeight() {
3635 ScrollabilityCache cache = mScrollCache;
3636 if (cache != null) {
3637 ScrollBarDrawable scrollBar = cache.scrollBar;
3638 if (scrollBar != null) {
3639 int size = scrollBar.getSize(false);
3640 if (size <= 0) {
3641 size = cache.scrollBarSize;
3642 }
3643 return size;
3644 }
3645 return 0;
3646 }
3647 return 0;
3648 }
3649
3650 /**
3651 * <p>
3652 * Initializes the scrollbars from a given set of styled attributes. This
3653 * method should be called by subclasses that need scrollbars and when an
3654 * instance of these subclasses is created programmatically rather than
3655 * being inflated from XML. This method is automatically called when the XML
3656 * is inflated.
3657 * </p>
3658 *
3659 * @param a the styled attributes set to initialize the scrollbars from
3660 */
3661 protected void initializeScrollbars(TypedArray a) {
3662 initScrollCache();
3663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003665
Mike Cleronf116bf82009-09-27 19:14:12 -07003666 if (scrollabilityCache.scrollBar == null) {
3667 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3668 }
Joe Malin32736f02011-01-19 16:14:20 -08003669
Romain Guy8bda2482010-03-02 11:42:11 -08003670 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671
Mike Cleronf116bf82009-09-27 19:14:12 -07003672 if (!fadeScrollbars) {
3673 scrollabilityCache.state = ScrollabilityCache.ON;
3674 }
3675 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003676
3677
Mike Cleronf116bf82009-09-27 19:14:12 -07003678 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3679 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3680 .getScrollBarFadeDuration());
3681 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3682 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3683 ViewConfiguration.getScrollDefaultDelay());
3684
Joe Malin32736f02011-01-19 16:14:20 -08003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3687 com.android.internal.R.styleable.View_scrollbarSize,
3688 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3689
3690 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3691 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3692
3693 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3694 if (thumb != null) {
3695 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3696 }
3697
3698 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3699 false);
3700 if (alwaysDraw) {
3701 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3702 }
3703
3704 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3705 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3706
3707 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3708 if (thumb != null) {
3709 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3710 }
3711
3712 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3713 false);
3714 if (alwaysDraw) {
3715 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3716 }
3717
3718 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003719 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
3721
3722 /**
3723 * <p>
3724 * Initalizes the scrollability cache if necessary.
3725 * </p>
3726 */
3727 private void initScrollCache() {
3728 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003729 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 }
3731 }
3732
Philip Milne6c8ea062012-04-03 17:38:43 -07003733 private ScrollabilityCache getScrollCache() {
3734 initScrollCache();
3735 return mScrollCache;
3736 }
3737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 /**
Adam Powell20232d02010-12-08 21:08:53 -08003739 * Set the position of the vertical scroll bar. Should be one of
3740 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3741 * {@link #SCROLLBAR_POSITION_RIGHT}.
3742 *
3743 * @param position Where the vertical scroll bar should be positioned.
3744 */
3745 public void setVerticalScrollbarPosition(int position) {
3746 if (mVerticalScrollbarPosition != position) {
3747 mVerticalScrollbarPosition = position;
3748 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003749 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003750 }
3751 }
3752
3753 /**
3754 * @return The position where the vertical scroll bar will show, if applicable.
3755 * @see #setVerticalScrollbarPosition(int)
3756 */
3757 public int getVerticalScrollbarPosition() {
3758 return mVerticalScrollbarPosition;
3759 }
3760
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003761 ListenerInfo getListenerInfo() {
3762 if (mListenerInfo != null) {
3763 return mListenerInfo;
3764 }
3765 mListenerInfo = new ListenerInfo();
3766 return mListenerInfo;
3767 }
3768
Adam Powell20232d02010-12-08 21:08:53 -08003769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 * Register a callback to be invoked when focus of this view changed.
3771 *
3772 * @param l The callback that will run.
3773 */
3774 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003775 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 }
3777
3778 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003779 * Add a listener that will be called when the bounds of the view change due to
3780 * layout processing.
3781 *
3782 * @param listener The listener that will be called when layout bounds change.
3783 */
3784 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003785 ListenerInfo li = getListenerInfo();
3786 if (li.mOnLayoutChangeListeners == null) {
3787 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003788 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003789 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3790 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003791 }
Chet Haase21cd1382010-09-01 17:42:29 -07003792 }
3793
3794 /**
3795 * Remove a listener for layout changes.
3796 *
3797 * @param listener The listener for layout bounds change.
3798 */
3799 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003800 ListenerInfo li = mListenerInfo;
3801 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003802 return;
3803 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003804 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003805 }
3806
3807 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003808 * Add a listener for attach state changes.
3809 *
3810 * This listener will be called whenever this view is attached or detached
3811 * from a window. Remove the listener using
3812 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3813 *
3814 * @param listener Listener to attach
3815 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3816 */
3817 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 ListenerInfo li = getListenerInfo();
3819 if (li.mOnAttachStateChangeListeners == null) {
3820 li.mOnAttachStateChangeListeners
3821 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003822 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003823 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003824 }
3825
3826 /**
3827 * Remove a listener for attach state changes. The listener will receive no further
3828 * notification of window attach/detach events.
3829 *
3830 * @param listener Listener to remove
3831 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3832 */
3833 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003834 ListenerInfo li = mListenerInfo;
3835 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003836 return;
3837 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003838 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003839 }
3840
3841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 * Returns the focus-change callback registered for this view.
3843 *
3844 * @return The callback, or null if one is not registered.
3845 */
3846 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003847 ListenerInfo li = mListenerInfo;
3848 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 }
3850
3851 /**
3852 * Register a callback to be invoked when this view is clicked. If this view is not
3853 * clickable, it becomes clickable.
3854 *
3855 * @param l The callback that will run
3856 *
3857 * @see #setClickable(boolean)
3858 */
3859 public void setOnClickListener(OnClickListener l) {
3860 if (!isClickable()) {
3861 setClickable(true);
3862 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003863 getListenerInfo().mOnClickListener = l;
3864 }
3865
3866 /**
3867 * Return whether this view has an attached OnClickListener. Returns
3868 * true if there is a listener, false if there is none.
3869 */
3870 public boolean hasOnClickListeners() {
3871 ListenerInfo li = mListenerInfo;
3872 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 }
3874
3875 /**
3876 * Register a callback to be invoked when this view is clicked and held. If this view is not
3877 * long clickable, it becomes long clickable.
3878 *
3879 * @param l The callback that will run
3880 *
3881 * @see #setLongClickable(boolean)
3882 */
3883 public void setOnLongClickListener(OnLongClickListener l) {
3884 if (!isLongClickable()) {
3885 setLongClickable(true);
3886 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003887 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889
3890 /**
3891 * Register a callback to be invoked when the context menu for this view is
3892 * being built. If this view is not long clickable, it becomes long clickable.
3893 *
3894 * @param l The callback that will run
3895 *
3896 */
3897 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3898 if (!isLongClickable()) {
3899 setLongClickable(true);
3900 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003901 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003905 * Call this view's OnClickListener, if it is defined. Performs all normal
3906 * actions associated with clicking: reporting accessibility event, playing
3907 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 *
3909 * @return True there was an assigned OnClickListener that was called, false
3910 * otherwise is returned.
3911 */
3912 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003913 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3914
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003915 ListenerInfo li = mListenerInfo;
3916 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003918 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 return true;
3920 }
3921
3922 return false;
3923 }
3924
3925 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003926 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3927 * this only calls the listener, and does not do any associated clicking
3928 * actions like reporting an accessibility event.
3929 *
3930 * @return True there was an assigned OnClickListener that was called, false
3931 * otherwise is returned.
3932 */
3933 public boolean callOnClick() {
3934 ListenerInfo li = mListenerInfo;
3935 if (li != null && li.mOnClickListener != null) {
3936 li.mOnClickListener.onClick(this);
3937 return true;
3938 }
3939 return false;
3940 }
3941
3942 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003943 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3944 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003946 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 */
3948 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003949 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 ListenerInfo li = mListenerInfo;
3953 if (li != null && li.mOnLongClickListener != null) {
3954 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 }
3956 if (!handled) {
3957 handled = showContextMenu();
3958 }
3959 if (handled) {
3960 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3961 }
3962 return handled;
3963 }
3964
3965 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003966 * Performs button-related actions during a touch down event.
3967 *
3968 * @param event The event.
3969 * @return True if the down was consumed.
3970 *
3971 * @hide
3972 */
3973 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3974 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3975 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3976 return true;
3977 }
3978 }
3979 return false;
3980 }
3981
3982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 * Bring up the context menu for this view.
3984 *
3985 * @return Whether a context menu was displayed.
3986 */
3987 public boolean showContextMenu() {
3988 return getParent().showContextMenuForChild(this);
3989 }
3990
3991 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003992 * Bring up the context menu for this view, referring to the item under the specified point.
3993 *
3994 * @param x The referenced x coordinate.
3995 * @param y The referenced y coordinate.
3996 * @param metaState The keyboard modifiers that were pressed.
3997 * @return Whether a context menu was displayed.
3998 *
3999 * @hide
4000 */
4001 public boolean showContextMenu(float x, float y, int metaState) {
4002 return showContextMenu();
4003 }
4004
4005 /**
Adam Powell6e346362010-07-23 10:18:23 -07004006 * Start an action mode.
4007 *
4008 * @param callback Callback that will control the lifecycle of the action mode
4009 * @return The new action mode if it is started, null otherwise
4010 *
4011 * @see ActionMode
4012 */
4013 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004014 ViewParent parent = getParent();
4015 if (parent == null) return null;
4016 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004017 }
4018
4019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 * Register a callback to be invoked when a key is pressed in this view.
4021 * @param l the key listener to attach to this view
4022 */
4023 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004024 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
4026
4027 /**
4028 * Register a callback to be invoked when a touch event is sent to this view.
4029 * @param l the touch listener to attach to this view
4030 */
4031 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004032 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 }
4034
4035 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004036 * Register a callback to be invoked when a generic motion event is sent to this view.
4037 * @param l the generic motion listener to attach to this view
4038 */
4039 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004040 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004041 }
4042
4043 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004044 * Register a callback to be invoked when a hover event is sent to this view.
4045 * @param l the hover listener to attach to this view
4046 */
4047 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004048 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004049 }
4050
4051 /**
Joe Malin32736f02011-01-19 16:14:20 -08004052 * Register a drag event listener callback object for this View. The parameter is
4053 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4054 * View, the system calls the
4055 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4056 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004057 */
4058 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004059 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004060 }
4061
4062 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004063 * Give this view focus. This will cause
4064 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 *
4066 * Note: this does not check whether this {@link View} should get focus, it just
4067 * gives it focus no matter what. It should only be called internally by framework
4068 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4069 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004070 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4071 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 * focus moved when requestFocus() is called. It may not always
4073 * apply, in which case use the default View.FOCUS_DOWN.
4074 * @param previouslyFocusedRect The rectangle of the view that had focus
4075 * prior in this View's coordinate system.
4076 */
4077 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4078 if (DBG) {
4079 System.out.println(this + " requestFocus()");
4080 }
4081
4082 if ((mPrivateFlags & FOCUSED) == 0) {
4083 mPrivateFlags |= FOCUSED;
4084
4085 if (mParent != null) {
4086 mParent.requestChildFocus(this, this);
4087 }
4088
4089 onFocusChanged(true, direction, previouslyFocusedRect);
4090 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004091
4092 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4093 notifyAccessibilityStateChanged();
4094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
4096 }
4097
4098 /**
4099 * Request that a rectangle of this view be visible on the screen,
4100 * scrolling if necessary just enough.
4101 *
4102 * <p>A View should call this if it maintains some notion of which part
4103 * of its content is interesting. For example, a text editing view
4104 * should call this when its cursor moves.
4105 *
4106 * @param rectangle The rectangle.
4107 * @return Whether any parent scrolled.
4108 */
4109 public boolean requestRectangleOnScreen(Rect rectangle) {
4110 return requestRectangleOnScreen(rectangle, false);
4111 }
4112
4113 /**
4114 * Request that a rectangle of this view be visible on the screen,
4115 * scrolling if necessary just enough.
4116 *
4117 * <p>A View should call this if it maintains some notion of which part
4118 * of its content is interesting. For example, a text editing view
4119 * should call this when its cursor moves.
4120 *
4121 * <p>When <code>immediate</code> is set to true, scrolling will not be
4122 * animated.
4123 *
4124 * @param rectangle The rectangle.
4125 * @param immediate True to forbid animated scrolling, false otherwise
4126 * @return Whether any parent scrolled.
4127 */
4128 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4129 View child = this;
4130 ViewParent parent = mParent;
4131 boolean scrolled = false;
4132 while (parent != null) {
4133 scrolled |= parent.requestChildRectangleOnScreen(child,
4134 rectangle, immediate);
4135
4136 // offset rect so next call has the rectangle in the
4137 // coordinate system of its direct child.
4138 rectangle.offset(child.getLeft(), child.getTop());
4139 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4140
4141 if (!(parent instanceof View)) {
4142 break;
4143 }
Romain Guy8506ab42009-06-11 17:35:47 -07004144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 child = (View) parent;
4146 parent = child.getParent();
4147 }
4148 return scrolled;
4149 }
4150
4151 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004152 * Called when this view wants to give up focus. If focus is cleared
4153 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4154 * <p>
4155 * <strong>Note:</strong> When a View clears focus the framework is trying
4156 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004157 * View is the first from the top that can take focus, then all callbacks
4158 * related to clearing focus will be invoked after wich the framework will
4159 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004160 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 */
4162 public void clearFocus() {
4163 if (DBG) {
4164 System.out.println(this + " clearFocus()");
4165 }
4166
4167 if ((mPrivateFlags & FOCUSED) != 0) {
4168 mPrivateFlags &= ~FOCUSED;
4169
4170 if (mParent != null) {
4171 mParent.clearChildFocus(this);
4172 }
4173
4174 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004177
4178 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004179
4180 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4181 notifyAccessibilityStateChanged();
4182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 }
4184 }
4185
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004186 void ensureInputFocusOnFirstFocusable() {
4187 View root = getRootView();
4188 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004189 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191 }
4192
4193 /**
4194 * Called internally by the view system when a new view is getting focus.
4195 * This is what clears the old focus.
4196 */
4197 void unFocus() {
4198 if (DBG) {
4199 System.out.println(this + " unFocus()");
4200 }
4201
4202 if ((mPrivateFlags & FOCUSED) != 0) {
4203 mPrivateFlags &= ~FOCUSED;
4204
4205 onFocusChanged(false, 0, null);
4206 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004207
4208 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4209 notifyAccessibilityStateChanged();
4210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 }
4212 }
4213
4214 /**
4215 * Returns true if this view has focus iteself, or is the ancestor of the
4216 * view that has focus.
4217 *
4218 * @return True if this view has or contains focus, false otherwise.
4219 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004220 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 public boolean hasFocus() {
4222 return (mPrivateFlags & FOCUSED) != 0;
4223 }
4224
4225 /**
4226 * Returns true if this view is focusable or if it contains a reachable View
4227 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4228 * is a View whose parents do not block descendants focus.
4229 *
4230 * Only {@link #VISIBLE} views are considered focusable.
4231 *
4232 * @return True if the view is focusable or if the view contains a focusable
4233 * View, false otherwise.
4234 *
4235 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4236 */
4237 public boolean hasFocusable() {
4238 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4239 }
4240
4241 /**
4242 * Called by the view system when the focus state of this view changes.
4243 * When the focus change event is caused by directional navigation, direction
4244 * and previouslyFocusedRect provide insight into where the focus is coming from.
4245 * When overriding, be sure to call up through to the super class so that
4246 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 * @param gainFocus True if the View has focus; false otherwise.
4249 * @param direction The direction focus has moved when requestFocus()
4250 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004251 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4252 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4253 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4255 * system, of the previously focused view. If applicable, this will be
4256 * passed in as finer grained information about where the focus is coming
4257 * from (in addition to direction). Will be <code>null</code> otherwise.
4258 */
4259 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004260 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004261 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4262 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4263 requestAccessibilityFocus();
4264 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004265 }
4266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 InputMethodManager imm = InputMethodManager.peekInstance();
4268 if (!gainFocus) {
4269 if (isPressed()) {
4270 setPressed(false);
4271 }
4272 if (imm != null && mAttachInfo != null
4273 && mAttachInfo.mHasWindowFocus) {
4274 imm.focusOut(this);
4275 }
Romain Guya2431d02009-04-30 16:30:00 -07004276 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 } else if (imm != null && mAttachInfo != null
4278 && mAttachInfo.mHasWindowFocus) {
4279 imm.focusIn(this);
4280 }
Romain Guy8506ab42009-06-11 17:35:47 -07004281
Romain Guy0fd89bf2011-01-26 15:41:30 -08004282 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004283 ListenerInfo li = mListenerInfo;
4284 if (li != null && li.mOnFocusChangeListener != null) {
4285 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
Joe Malin32736f02011-01-19 16:14:20 -08004287
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004288 if (mAttachInfo != null) {
4289 mAttachInfo.mKeyDispatchState.reset(this);
4290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 }
4292
4293 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004294 * Sends an accessibility event of the given type. If accessiiblity is
4295 * not enabled this method has no effect. The default implementation calls
4296 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4297 * to populate information about the event source (this View), then calls
4298 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4299 * populate the text content of the event source including its descendants,
4300 * and last calls
4301 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4302 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004303 * <p>
4304 * If an {@link AccessibilityDelegate} has been specified via calling
4305 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4306 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4307 * responsible for handling this call.
4308 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004309 *
Scott Mainb303d832011-10-12 16:45:18 -07004310 * @param eventType The type of the event to send, as defined by several types from
4311 * {@link android.view.accessibility.AccessibilityEvent}, such as
4312 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4313 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004314 *
4315 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4316 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4317 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004318 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004319 */
4320 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004321 if (mAccessibilityDelegate != null) {
4322 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4323 } else {
4324 sendAccessibilityEventInternal(eventType);
4325 }
4326 }
4327
4328 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004329 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4330 * {@link AccessibilityEvent} to make an announcement which is related to some
4331 * sort of a context change for which none of the events representing UI transitions
4332 * is a good fit. For example, announcing a new page in a book. If accessibility
4333 * is not enabled this method does nothing.
4334 *
4335 * @param text The announcement text.
4336 */
4337 public void announceForAccessibility(CharSequence text) {
4338 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4339 AccessibilityEvent event = AccessibilityEvent.obtain(
4340 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4341 event.getText().add(text);
4342 sendAccessibilityEventUnchecked(event);
4343 }
4344 }
4345
4346 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004347 * @see #sendAccessibilityEvent(int)
4348 *
4349 * Note: Called from the default {@link AccessibilityDelegate}.
4350 */
4351 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004352 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4353 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4354 }
4355 }
4356
4357 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004358 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4359 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004360 * perform a check whether accessibility is enabled.
4361 * <p>
4362 * If an {@link AccessibilityDelegate} has been specified via calling
4363 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4364 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4365 * is responsible for handling this call.
4366 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004367 *
4368 * @param event The event to send.
4369 *
4370 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004371 */
4372 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004373 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004374 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004375 } else {
4376 sendAccessibilityEventUncheckedInternal(event);
4377 }
4378 }
4379
4380 /**
4381 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4382 *
4383 * Note: Called from the default {@link AccessibilityDelegate}.
4384 */
4385 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004386 if (!isShown()) {
4387 return;
4388 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004389 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004390 // Only a subset of accessibility events populates text content.
4391 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4392 dispatchPopulateAccessibilityEvent(event);
4393 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004394 // Intercept accessibility focus events fired by virtual nodes to keep
4395 // track of accessibility focus position in such nodes.
4396 final int eventType = event.getEventType();
4397 switch (eventType) {
4398 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4399 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4400 event.getSourceNodeId());
4401 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4402 ViewRootImpl viewRootImpl = getViewRootImpl();
4403 if (viewRootImpl != null) {
4404 viewRootImpl.setAccessibilityFocusedHost(this);
4405 }
4406 }
4407 } break;
4408 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4409 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4410 event.getSourceNodeId());
4411 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4412 ViewRootImpl viewRootImpl = getViewRootImpl();
4413 if (viewRootImpl != null) {
4414 viewRootImpl.setAccessibilityFocusedHost(null);
4415 }
4416 }
4417 } break;
4418 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004419 // In the beginning we called #isShown(), so we know that getParent() is not null.
4420 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004421 }
4422
4423 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004424 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4425 * to its children for adding their text content to the event. Note that the
4426 * event text is populated in a separate dispatch path since we add to the
4427 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004428 * A typical implementation will call
4429 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4430 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4431 * on each child. Override this method if custom population of the event text
4432 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004433 * <p>
4434 * If an {@link AccessibilityDelegate} has been specified via calling
4435 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4436 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4437 * is responsible for handling this call.
4438 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004439 * <p>
4440 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4441 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4442 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004443 *
4444 * @param event The event.
4445 *
4446 * @return True if the event population was completed.
4447 */
4448 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004449 if (mAccessibilityDelegate != null) {
4450 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4451 } else {
4452 return dispatchPopulateAccessibilityEventInternal(event);
4453 }
4454 }
4455
4456 /**
4457 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4458 *
4459 * Note: Called from the default {@link AccessibilityDelegate}.
4460 */
4461 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004462 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004463 return false;
4464 }
4465
4466 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004467 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004468 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004469 * text content. While this method is free to modify event
4470 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004471 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4472 * <p>
4473 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004474 * to the text added by the super implementation:
4475 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004476 * super.onPopulateAccessibilityEvent(event);
4477 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4478 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4479 * mCurrentDate.getTimeInMillis(), flags);
4480 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004481 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004482 * <p>
4483 * If an {@link AccessibilityDelegate} has been specified via calling
4484 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4485 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4486 * is responsible for handling this call.
4487 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004488 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4489 * information to the event, in case the default implementation has basic information to add.
4490 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004491 *
4492 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004493 *
4494 * @see #sendAccessibilityEvent(int)
4495 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004496 */
4497 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004498 if (mAccessibilityDelegate != null) {
4499 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4500 } else {
4501 onPopulateAccessibilityEventInternal(event);
4502 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004503 }
4504
4505 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004506 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4507 *
4508 * Note: Called from the default {@link AccessibilityDelegate}.
4509 */
4510 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4511
4512 }
4513
4514 /**
4515 * Initializes an {@link AccessibilityEvent} with information about
4516 * this View which is the event source. In other words, the source of
4517 * an accessibility event is the view whose state change triggered firing
4518 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004519 * <p>
4520 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004521 * to properties set by the super implementation:
4522 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4523 * super.onInitializeAccessibilityEvent(event);
4524 * event.setPassword(true);
4525 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004526 * <p>
4527 * If an {@link AccessibilityDelegate} has been specified via calling
4528 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4529 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4530 * is responsible for handling this call.
4531 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004532 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4533 * information to the event, in case the default implementation has basic information to add.
4534 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004535 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004536 *
4537 * @see #sendAccessibilityEvent(int)
4538 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4539 */
4540 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004541 if (mAccessibilityDelegate != null) {
4542 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4543 } else {
4544 onInitializeAccessibilityEventInternal(event);
4545 }
4546 }
4547
4548 /**
4549 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4550 *
4551 * Note: Called from the default {@link AccessibilityDelegate}.
4552 */
4553 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004554 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004555 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004556 event.setPackageName(getContext().getPackageName());
4557 event.setEnabled(isEnabled());
4558 event.setContentDescription(mContentDescription);
4559
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004560 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004561 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004562 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4563 FOCUSABLES_ALL);
4564 event.setItemCount(focusablesTempList.size());
4565 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4566 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004567 }
4568 }
4569
4570 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004571 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4572 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4573 * This method is responsible for obtaining an accessibility node info from a
4574 * pool of reusable instances and calling
4575 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4576 * initialize the former.
4577 * <p>
4578 * Note: The client is responsible for recycling the obtained instance by calling
4579 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4580 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004581 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004582 * @return A populated {@link AccessibilityNodeInfo}.
4583 *
4584 * @see AccessibilityNodeInfo
4585 */
4586 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004587 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4588 if (provider != null) {
4589 return provider.createAccessibilityNodeInfo(View.NO_ID);
4590 } else {
4591 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4592 onInitializeAccessibilityNodeInfo(info);
4593 return info;
4594 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004595 }
4596
4597 /**
4598 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4599 * The base implementation sets:
4600 * <ul>
4601 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004602 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4603 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004604 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4605 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4606 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4607 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4608 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4609 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4610 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4611 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4612 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4613 * </ul>
4614 * <p>
4615 * Subclasses should override this method, call the super implementation,
4616 * and set additional attributes.
4617 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004618 * <p>
4619 * If an {@link AccessibilityDelegate} has been specified via calling
4620 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4621 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4622 * is responsible for handling this call.
4623 * </p>
4624 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004625 * @param info The instance to initialize.
4626 */
4627 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004628 if (mAccessibilityDelegate != null) {
4629 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4630 } else {
4631 onInitializeAccessibilityNodeInfoInternal(info);
4632 }
4633 }
4634
4635 /**
4636 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4637 *
4638 * Note: Called from the default {@link AccessibilityDelegate}.
4639 */
4640 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004641 Rect bounds = mAttachInfo.mTmpInvalRect;
4642 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004643 info.setBoundsInParent(bounds);
4644
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004645 getGlobalVisibleRect(bounds);
4646 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004647 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004648
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004649 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004650 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004651 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004652 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004653 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004654 }
4655
4656 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004657 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004658 info.setContentDescription(getContentDescription());
4659
4660 info.setEnabled(isEnabled());
4661 info.setClickable(isClickable());
4662 info.setFocusable(isFocusable());
4663 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004664 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004665 info.setSelected(isSelected());
4666 info.setLongClickable(isLongClickable());
4667
4668 // TODO: These make sense only if we are in an AdapterView but all
4669 // views can be selected. Maybe from accessiiblity perspective
4670 // we should report as selectable view in an AdapterView.
4671 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4672 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4673
4674 if (isFocusable()) {
4675 if (isFocused()) {
4676 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4677 } else {
4678 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4679 }
4680 }
4681 }
4682
4683 /**
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004684 * Computes whether this view is visible on the screen.
4685 *
4686 * @return Whether the view is visible on the screen.
4687 */
4688 boolean isDisplayedOnScreen() {
4689 // The first two checks are made also made by isShown() which
4690 // however traverses the tree up to the parent to catch that.
4691 // Therefore, we do some fail fast check to minimize the up
4692 // tree traversal.
4693 return (mAttachInfo != null
4694 && mAttachInfo.mWindowVisibility == View.VISIBLE
4695 && getAlpha() > 0
4696 && isShown()
4697 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4698 }
4699
4700 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004701 * Sets a delegate for implementing accessibility support via compositon as
4702 * opposed to inheritance. The delegate's primary use is for implementing
4703 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4704 *
4705 * @param delegate The delegate instance.
4706 *
4707 * @see AccessibilityDelegate
4708 */
4709 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4710 mAccessibilityDelegate = delegate;
4711 }
4712
4713 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004714 * Gets the provider for managing a virtual view hierarchy rooted at this View
4715 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4716 * that explore the window content.
4717 * <p>
4718 * If this method returns an instance, this instance is responsible for managing
4719 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4720 * View including the one representing the View itself. Similarly the returned
4721 * instance is responsible for performing accessibility actions on any virtual
4722 * view or the root view itself.
4723 * </p>
4724 * <p>
4725 * If an {@link AccessibilityDelegate} has been specified via calling
4726 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4727 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4728 * is responsible for handling this call.
4729 * </p>
4730 *
4731 * @return The provider.
4732 *
4733 * @see AccessibilityNodeProvider
4734 */
4735 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4736 if (mAccessibilityDelegate != null) {
4737 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4738 } else {
4739 return null;
4740 }
4741 }
4742
4743 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004744 * Gets the unique identifier of this view on the screen for accessibility purposes.
4745 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4746 *
4747 * @return The view accessibility id.
4748 *
4749 * @hide
4750 */
4751 public int getAccessibilityViewId() {
4752 if (mAccessibilityViewId == NO_ID) {
4753 mAccessibilityViewId = sNextAccessibilityViewId++;
4754 }
4755 return mAccessibilityViewId;
4756 }
4757
4758 /**
4759 * Gets the unique identifier of the window in which this View reseides.
4760 *
4761 * @return The window accessibility id.
4762 *
4763 * @hide
4764 */
4765 public int getAccessibilityWindowId() {
4766 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4767 }
4768
4769 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004770 * Gets the {@link View} description. It briefly describes the view and is
4771 * primarily used for accessibility support. Set this property to enable
4772 * better accessibility support for your application. This is especially
4773 * true for views that do not have textual representation (For example,
4774 * ImageButton).
4775 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004776 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004777 *
4778 * @attr ref android.R.styleable#View_contentDescription
4779 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004780 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004781 public CharSequence getContentDescription() {
4782 return mContentDescription;
4783 }
4784
4785 /**
4786 * Sets the {@link View} description. It briefly describes the view and is
4787 * primarily used for accessibility support. Set this property to enable
4788 * better accessibility support for your application. This is especially
4789 * true for views that do not have textual representation (For example,
4790 * ImageButton).
4791 *
4792 * @param contentDescription The content description.
4793 *
4794 * @attr ref android.R.styleable#View_contentDescription
4795 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004796 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004797 public void setContentDescription(CharSequence contentDescription) {
4798 mContentDescription = contentDescription;
4799 }
4800
4801 /**
Romain Guya2431d02009-04-30 16:30:00 -07004802 * Invoked whenever this view loses focus, either by losing window focus or by losing
4803 * focus within its window. This method can be used to clear any state tied to the
4804 * focus. For instance, if a button is held pressed with the trackball and the window
4805 * loses focus, this method can be used to cancel the press.
4806 *
4807 * Subclasses of View overriding this method should always call super.onFocusLost().
4808 *
4809 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004810 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004811 *
4812 * @hide pending API council approval
4813 */
4814 protected void onFocusLost() {
4815 resetPressedState();
4816 }
4817
4818 private void resetPressedState() {
4819 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4820 return;
4821 }
4822
4823 if (isPressed()) {
4824 setPressed(false);
4825
4826 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004827 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004828 }
4829 }
4830 }
4831
4832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 * Returns true if this view has focus
4834 *
4835 * @return True if this view has focus, false otherwise.
4836 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004837 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 public boolean isFocused() {
4839 return (mPrivateFlags & FOCUSED) != 0;
4840 }
4841
4842 /**
4843 * Find the view in the hierarchy rooted at this view that currently has
4844 * focus.
4845 *
4846 * @return The view that currently has focus, or null if no focused view can
4847 * be found.
4848 */
4849 public View findFocus() {
4850 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4851 }
4852
4853 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004854 * Indicates whether this view is one of the set of scrollable containers in
4855 * its window.
4856 *
4857 * @return whether this view is one of the set of scrollable containers in
4858 * its window
4859 *
4860 * @attr ref android.R.styleable#View_isScrollContainer
4861 */
4862 public boolean isScrollContainer() {
4863 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4864 }
4865
4866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 * Change whether this view is one of the set of scrollable containers in
4868 * its window. This will be used to determine whether the window can
4869 * resize or must pan when a soft input area is open -- scrollable
4870 * containers allow the window to use resize mode since the container
4871 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004872 *
4873 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 */
4875 public void setScrollContainer(boolean isScrollContainer) {
4876 if (isScrollContainer) {
4877 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4878 mAttachInfo.mScrollContainers.add(this);
4879 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4880 }
4881 mPrivateFlags |= SCROLL_CONTAINER;
4882 } else {
4883 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4884 mAttachInfo.mScrollContainers.remove(this);
4885 }
4886 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4887 }
4888 }
4889
4890 /**
4891 * Returns the quality of the drawing cache.
4892 *
4893 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4894 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4895 *
4896 * @see #setDrawingCacheQuality(int)
4897 * @see #setDrawingCacheEnabled(boolean)
4898 * @see #isDrawingCacheEnabled()
4899 *
4900 * @attr ref android.R.styleable#View_drawingCacheQuality
4901 */
4902 public int getDrawingCacheQuality() {
4903 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4904 }
4905
4906 /**
4907 * Set the drawing cache quality of this view. This value is used only when the
4908 * drawing cache is enabled
4909 *
4910 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4911 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4912 *
4913 * @see #getDrawingCacheQuality()
4914 * @see #setDrawingCacheEnabled(boolean)
4915 * @see #isDrawingCacheEnabled()
4916 *
4917 * @attr ref android.R.styleable#View_drawingCacheQuality
4918 */
4919 public void setDrawingCacheQuality(int quality) {
4920 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4921 }
4922
4923 /**
4924 * Returns whether the screen should remain on, corresponding to the current
4925 * value of {@link #KEEP_SCREEN_ON}.
4926 *
4927 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4928 *
4929 * @see #setKeepScreenOn(boolean)
4930 *
4931 * @attr ref android.R.styleable#View_keepScreenOn
4932 */
4933 public boolean getKeepScreenOn() {
4934 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4935 }
4936
4937 /**
4938 * Controls whether the screen should remain on, modifying the
4939 * value of {@link #KEEP_SCREEN_ON}.
4940 *
4941 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4942 *
4943 * @see #getKeepScreenOn()
4944 *
4945 * @attr ref android.R.styleable#View_keepScreenOn
4946 */
4947 public void setKeepScreenOn(boolean keepScreenOn) {
4948 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4949 }
4950
4951 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004952 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4953 * @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 -08004954 *
4955 * @attr ref android.R.styleable#View_nextFocusLeft
4956 */
4957 public int getNextFocusLeftId() {
4958 return mNextFocusLeftId;
4959 }
4960
4961 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004962 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4963 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4964 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 *
4966 * @attr ref android.R.styleable#View_nextFocusLeft
4967 */
4968 public void setNextFocusLeftId(int nextFocusLeftId) {
4969 mNextFocusLeftId = nextFocusLeftId;
4970 }
4971
4972 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004973 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4974 * @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 -08004975 *
4976 * @attr ref android.R.styleable#View_nextFocusRight
4977 */
4978 public int getNextFocusRightId() {
4979 return mNextFocusRightId;
4980 }
4981
4982 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004983 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4984 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4985 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 *
4987 * @attr ref android.R.styleable#View_nextFocusRight
4988 */
4989 public void setNextFocusRightId(int nextFocusRightId) {
4990 mNextFocusRightId = nextFocusRightId;
4991 }
4992
4993 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004994 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4995 * @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 -08004996 *
4997 * @attr ref android.R.styleable#View_nextFocusUp
4998 */
4999 public int getNextFocusUpId() {
5000 return mNextFocusUpId;
5001 }
5002
5003 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005004 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5005 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5006 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 *
5008 * @attr ref android.R.styleable#View_nextFocusUp
5009 */
5010 public void setNextFocusUpId(int nextFocusUpId) {
5011 mNextFocusUpId = nextFocusUpId;
5012 }
5013
5014 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005015 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5016 * @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 -08005017 *
5018 * @attr ref android.R.styleable#View_nextFocusDown
5019 */
5020 public int getNextFocusDownId() {
5021 return mNextFocusDownId;
5022 }
5023
5024 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005025 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5026 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5027 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 *
5029 * @attr ref android.R.styleable#View_nextFocusDown
5030 */
5031 public void setNextFocusDownId(int nextFocusDownId) {
5032 mNextFocusDownId = nextFocusDownId;
5033 }
5034
5035 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005036 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5037 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5038 *
5039 * @attr ref android.R.styleable#View_nextFocusForward
5040 */
5041 public int getNextFocusForwardId() {
5042 return mNextFocusForwardId;
5043 }
5044
5045 /**
5046 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5047 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5048 * decide automatically.
5049 *
5050 * @attr ref android.R.styleable#View_nextFocusForward
5051 */
5052 public void setNextFocusForwardId(int nextFocusForwardId) {
5053 mNextFocusForwardId = nextFocusForwardId;
5054 }
5055
5056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 * Returns the visibility of this view and all of its ancestors
5058 *
5059 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5060 */
5061 public boolean isShown() {
5062 View current = this;
5063 //noinspection ConstantConditions
5064 do {
5065 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5066 return false;
5067 }
5068 ViewParent parent = current.mParent;
5069 if (parent == null) {
5070 return false; // We are not attached to the view root
5071 }
5072 if (!(parent instanceof View)) {
5073 return true;
5074 }
5075 current = (View) parent;
5076 } while (current != null);
5077
5078 return false;
5079 }
5080
5081 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005082 * Called by the view hierarchy when the content insets for a window have
5083 * changed, to allow it to adjust its content to fit within those windows.
5084 * The content insets tell you the space that the status bar, input method,
5085 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005087 * <p>You do not normally need to deal with this function, since the default
5088 * window decoration given to applications takes care of applying it to the
5089 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5090 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5091 * and your content can be placed under those system elements. You can then
5092 * use this method within your view hierarchy if you have parts of your UI
5093 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005095 * <p>The default implementation of this method simply applies the content
5096 * inset's to the view's padding. This can be enabled through
5097 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5098 * the method and handle the insets however you would like. Note that the
5099 * insets provided by the framework are always relative to the far edges
5100 * of the window, not accounting for the location of the called view within
5101 * that window. (In fact when this method is called you do not yet know
5102 * where the layout will place the view, as it is done before layout happens.)
5103 *
5104 * <p>Note: unlike many View methods, there is no dispatch phase to this
5105 * call. If you are overriding it in a ViewGroup and want to allow the
5106 * call to continue to your children, you must be sure to call the super
5107 * implementation.
5108 *
5109 * @param insets Current content insets of the window. Prior to
5110 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5111 * the insets or else you and Android will be unhappy.
5112 *
5113 * @return Return true if this view applied the insets and it should not
5114 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 */
5116 protected boolean fitSystemWindows(Rect insets) {
5117 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005118 mUserPaddingStart = -1;
5119 mUserPaddingEnd = -1;
5120 mUserPaddingRelative = false;
5121 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5122 || mAttachInfo == null
5123 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5124 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5125 return true;
5126 } else {
5127 internalSetPadding(0, 0, 0, 0);
5128 return false;
5129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
5131 return false;
5132 }
5133
5134 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005135 * Set whether or not this view should account for system screen decorations
5136 * such as the status bar and inset its content. This allows this view to be
5137 * positioned in absolute screen coordinates and remain visible to the user.
5138 *
5139 * <p>This should only be used by top-level window decor views.
5140 *
5141 * @param fitSystemWindows true to inset content for system screen decorations, false for
5142 * default behavior.
5143 *
5144 * @attr ref android.R.styleable#View_fitsSystemWindows
5145 */
5146 public void setFitsSystemWindows(boolean fitSystemWindows) {
5147 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5148 }
5149
5150 /**
5151 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5152 * will account for system screen decorations such as the status bar and inset its
5153 * content. This allows the view to be positioned in absolute screen coordinates
5154 * and remain visible to the user.
5155 *
5156 * @return true if this view will adjust its content bounds for system screen decorations.
5157 *
5158 * @attr ref android.R.styleable#View_fitsSystemWindows
5159 */
5160 public boolean fitsSystemWindows() {
5161 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5162 }
5163
5164 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005165 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5166 */
5167 public void requestFitSystemWindows() {
5168 if (mParent != null) {
5169 mParent.requestFitSystemWindows();
5170 }
5171 }
5172
5173 /**
5174 * For use by PhoneWindow to make its own system window fitting optional.
5175 * @hide
5176 */
5177 public void makeOptionalFitsSystemWindows() {
5178 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5179 }
5180
5181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 * Returns the visibility status for this view.
5183 *
5184 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5185 * @attr ref android.R.styleable#View_visibility
5186 */
5187 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005188 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5189 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5190 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 })
5192 public int getVisibility() {
5193 return mViewFlags & VISIBILITY_MASK;
5194 }
5195
5196 /**
5197 * Set the enabled state of this view.
5198 *
5199 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5200 * @attr ref android.R.styleable#View_visibility
5201 */
5202 @RemotableViewMethod
5203 public void setVisibility(int visibility) {
5204 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005205 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 }
5207
5208 /**
5209 * Returns the enabled status for this view. The interpretation of the
5210 * enabled state varies by subclass.
5211 *
5212 * @return True if this view is enabled, false otherwise.
5213 */
5214 @ViewDebug.ExportedProperty
5215 public boolean isEnabled() {
5216 return (mViewFlags & ENABLED_MASK) == ENABLED;
5217 }
5218
5219 /**
5220 * Set the enabled state of this view. The interpretation of the enabled
5221 * state varies by subclass.
5222 *
5223 * @param enabled True if this view is enabled, false otherwise.
5224 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005225 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005227 if (enabled == isEnabled()) return;
5228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5230
5231 /*
5232 * The View most likely has to change its appearance, so refresh
5233 * the drawable state.
5234 */
5235 refreshDrawableState();
5236
5237 // Invalidate too, since the default behavior for views is to be
5238 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005239 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
5241
5242 /**
5243 * Set whether this view can receive the focus.
5244 *
5245 * Setting this to false will also ensure that this view is not focusable
5246 * in touch mode.
5247 *
5248 * @param focusable If true, this view can receive the focus.
5249 *
5250 * @see #setFocusableInTouchMode(boolean)
5251 * @attr ref android.R.styleable#View_focusable
5252 */
5253 public void setFocusable(boolean focusable) {
5254 if (!focusable) {
5255 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5256 }
5257 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5258 }
5259
5260 /**
5261 * Set whether this view can receive focus while in touch mode.
5262 *
5263 * Setting this to true will also ensure that this view is focusable.
5264 *
5265 * @param focusableInTouchMode If true, this view can receive the focus while
5266 * in touch mode.
5267 *
5268 * @see #setFocusable(boolean)
5269 * @attr ref android.R.styleable#View_focusableInTouchMode
5270 */
5271 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5272 // Focusable in touch mode should always be set before the focusable flag
5273 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5274 // which, in touch mode, will not successfully request focus on this view
5275 // because the focusable in touch mode flag is not set
5276 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5277 if (focusableInTouchMode) {
5278 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5279 }
5280 }
5281
5282 /**
5283 * Set whether this view should have sound effects enabled for events such as
5284 * clicking and touching.
5285 *
5286 * <p>You may wish to disable sound effects for a view if you already play sounds,
5287 * for instance, a dial key that plays dtmf tones.
5288 *
5289 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5290 * @see #isSoundEffectsEnabled()
5291 * @see #playSoundEffect(int)
5292 * @attr ref android.R.styleable#View_soundEffectsEnabled
5293 */
5294 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5295 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5296 }
5297
5298 /**
5299 * @return whether this view should have sound effects enabled for events such as
5300 * clicking and touching.
5301 *
5302 * @see #setSoundEffectsEnabled(boolean)
5303 * @see #playSoundEffect(int)
5304 * @attr ref android.R.styleable#View_soundEffectsEnabled
5305 */
5306 @ViewDebug.ExportedProperty
5307 public boolean isSoundEffectsEnabled() {
5308 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5309 }
5310
5311 /**
5312 * Set whether this view should have haptic feedback for events such as
5313 * long presses.
5314 *
5315 * <p>You may wish to disable haptic feedback if your view already controls
5316 * its own haptic feedback.
5317 *
5318 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5319 * @see #isHapticFeedbackEnabled()
5320 * @see #performHapticFeedback(int)
5321 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5322 */
5323 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5324 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5325 }
5326
5327 /**
5328 * @return whether this view should have haptic feedback enabled for events
5329 * long presses.
5330 *
5331 * @see #setHapticFeedbackEnabled(boolean)
5332 * @see #performHapticFeedback(int)
5333 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5334 */
5335 @ViewDebug.ExportedProperty
5336 public boolean isHapticFeedbackEnabled() {
5337 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5338 }
5339
5340 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005341 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005342 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005343 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5344 * {@link #LAYOUT_DIRECTION_RTL},
5345 * {@link #LAYOUT_DIRECTION_INHERIT} or
5346 * {@link #LAYOUT_DIRECTION_LOCALE}.
5347 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005348 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005349 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005350 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5351 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5352 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5353 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005354 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005355 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005356 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005357 }
5358
5359 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005360 * Set the layout direction for this view. This will propagate a reset of layout direction
5361 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005362 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005363 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5364 * {@link #LAYOUT_DIRECTION_RTL},
5365 * {@link #LAYOUT_DIRECTION_INHERIT} or
5366 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005367 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005368 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005369 */
5370 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005371 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005372 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005373 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005374 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005375 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005376 // Set the new layout direction (filtered) and ask for a layout pass
5377 mPrivateFlags2 |=
5378 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5379 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005380 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005381 }
5382
5383 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005384 * Returns the resolved layout direction for this view.
5385 *
5386 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005387 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005388 */
5389 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005390 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5391 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005392 })
5393 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005394 // The layout diretion will be resolved only if needed
5395 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5396 resolveLayoutDirection();
5397 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005398 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005399 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5400 }
5401
5402 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005403 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5404 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005405 *
5406 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005407 */
5408 @ViewDebug.ExportedProperty(category = "layout")
5409 public boolean isLayoutRtl() {
5410 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5411 }
5412
5413 /**
Adam Powell539ee872012-02-03 19:00:49 -08005414 * Indicates whether the view is currently tracking transient state that the
5415 * app should not need to concern itself with saving and restoring, but that
5416 * the framework should take special note to preserve when possible.
5417 *
5418 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005419 */
5420 @ViewDebug.ExportedProperty(category = "layout")
5421 public boolean hasTransientState() {
5422 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5423 }
5424
5425 /**
5426 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005427 * framework should attempt to preserve when possible. This flag is reference counted,
5428 * so every call to setHasTransientState(true) should be paired with a later call
5429 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005430 *
5431 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005432 */
5433 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005434 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5435 mTransientStateCount - 1;
5436 if (mTransientStateCount < 0) {
5437 mTransientStateCount = 0;
5438 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5439 "unmatched pair of setHasTransientState calls");
5440 }
5441 if ((hasTransientState && mTransientStateCount == 1) ||
5442 (hasTransientState && mTransientStateCount == 0)) {
5443 // update flag if we've just incremented up from 0 or decremented down to 0
5444 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5445 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5446 if (mParent != null) {
5447 try {
5448 mParent.childHasTransientStateChanged(this, hasTransientState);
5449 } catch (AbstractMethodError e) {
5450 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5451 " does not fully implement ViewParent", e);
5452 }
Adam Powell539ee872012-02-03 19:00:49 -08005453 }
5454 }
5455 }
5456
5457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 * If this view doesn't do any drawing on its own, set this flag to
5459 * allow further optimizations. By default, this flag is not set on
5460 * View, but could be set on some View subclasses such as ViewGroup.
5461 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005462 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5463 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 *
5465 * @param willNotDraw whether or not this View draw on its own
5466 */
5467 public void setWillNotDraw(boolean willNotDraw) {
5468 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5469 }
5470
5471 /**
5472 * Returns whether or not this View draws on its own.
5473 *
5474 * @return true if this view has nothing to draw, false otherwise
5475 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005476 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 public boolean willNotDraw() {
5478 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5479 }
5480
5481 /**
5482 * When a View's drawing cache is enabled, drawing is redirected to an
5483 * offscreen bitmap. Some views, like an ImageView, must be able to
5484 * bypass this mechanism if they already draw a single bitmap, to avoid
5485 * unnecessary usage of the memory.
5486 *
5487 * @param willNotCacheDrawing true if this view does not cache its
5488 * drawing, false otherwise
5489 */
5490 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5491 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5492 }
5493
5494 /**
5495 * Returns whether or not this View can cache its drawing or not.
5496 *
5497 * @return true if this view does not cache its drawing, false otherwise
5498 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005499 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 public boolean willNotCacheDrawing() {
5501 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5502 }
5503
5504 /**
5505 * Indicates whether this view reacts to click events or not.
5506 *
5507 * @return true if the view is clickable, false otherwise
5508 *
5509 * @see #setClickable(boolean)
5510 * @attr ref android.R.styleable#View_clickable
5511 */
5512 @ViewDebug.ExportedProperty
5513 public boolean isClickable() {
5514 return (mViewFlags & CLICKABLE) == CLICKABLE;
5515 }
5516
5517 /**
5518 * Enables or disables click events for this view. When a view
5519 * is clickable it will change its state to "pressed" on every click.
5520 * Subclasses should set the view clickable to visually react to
5521 * user's clicks.
5522 *
5523 * @param clickable true to make the view clickable, false otherwise
5524 *
5525 * @see #isClickable()
5526 * @attr ref android.R.styleable#View_clickable
5527 */
5528 public void setClickable(boolean clickable) {
5529 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5530 }
5531
5532 /**
5533 * Indicates whether this view reacts to long click events or not.
5534 *
5535 * @return true if the view is long clickable, false otherwise
5536 *
5537 * @see #setLongClickable(boolean)
5538 * @attr ref android.R.styleable#View_longClickable
5539 */
5540 public boolean isLongClickable() {
5541 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5542 }
5543
5544 /**
5545 * Enables or disables long click events for this view. When a view is long
5546 * clickable it reacts to the user holding down the button for a longer
5547 * duration than a tap. This event can either launch the listener or a
5548 * context menu.
5549 *
5550 * @param longClickable true to make the view long clickable, false otherwise
5551 * @see #isLongClickable()
5552 * @attr ref android.R.styleable#View_longClickable
5553 */
5554 public void setLongClickable(boolean longClickable) {
5555 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5556 }
5557
5558 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005559 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 *
5561 * @see #isClickable()
5562 * @see #setClickable(boolean)
5563 *
5564 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5565 * the View's internal state from a previously set "pressed" state.
5566 */
5567 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005568 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 if (pressed) {
5571 mPrivateFlags |= PRESSED;
5572 } else {
5573 mPrivateFlags &= ~PRESSED;
5574 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005575
5576 if (needsRefresh) {
5577 refreshDrawableState();
5578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 dispatchSetPressed(pressed);
5580 }
5581
5582 /**
5583 * Dispatch setPressed to all of this View's children.
5584 *
5585 * @see #setPressed(boolean)
5586 *
5587 * @param pressed The new pressed state
5588 */
5589 protected void dispatchSetPressed(boolean pressed) {
5590 }
5591
5592 /**
5593 * Indicates whether the view is currently in pressed state. Unless
5594 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5595 * the pressed state.
5596 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005597 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 * @see #isClickable()
5599 * @see #setClickable(boolean)
5600 *
5601 * @return true if the view is currently pressed, false otherwise
5602 */
5603 public boolean isPressed() {
5604 return (mPrivateFlags & PRESSED) == PRESSED;
5605 }
5606
5607 /**
5608 * Indicates whether this view will save its state (that is,
5609 * whether its {@link #onSaveInstanceState} method will be called).
5610 *
5611 * @return Returns true if the view state saving is enabled, else false.
5612 *
5613 * @see #setSaveEnabled(boolean)
5614 * @attr ref android.R.styleable#View_saveEnabled
5615 */
5616 public boolean isSaveEnabled() {
5617 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5618 }
5619
5620 /**
5621 * Controls whether the saving of this view's state is
5622 * enabled (that is, whether its {@link #onSaveInstanceState} method
5623 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005624 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 * for its state to be saved. This flag can only disable the
5626 * saving of this view; any child views may still have their state saved.
5627 *
5628 * @param enabled Set to false to <em>disable</em> state saving, or true
5629 * (the default) to allow it.
5630 *
5631 * @see #isSaveEnabled()
5632 * @see #setId(int)
5633 * @see #onSaveInstanceState()
5634 * @attr ref android.R.styleable#View_saveEnabled
5635 */
5636 public void setSaveEnabled(boolean enabled) {
5637 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5638 }
5639
Jeff Brown85a31762010-09-01 17:01:00 -07005640 /**
5641 * Gets whether the framework should discard touches when the view's
5642 * window is obscured by another visible window.
5643 * Refer to the {@link View} security documentation for more details.
5644 *
5645 * @return True if touch filtering is enabled.
5646 *
5647 * @see #setFilterTouchesWhenObscured(boolean)
5648 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5649 */
5650 @ViewDebug.ExportedProperty
5651 public boolean getFilterTouchesWhenObscured() {
5652 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5653 }
5654
5655 /**
5656 * Sets whether the framework should discard touches when the view's
5657 * window is obscured by another visible window.
5658 * Refer to the {@link View} security documentation for more details.
5659 *
5660 * @param enabled True if touch filtering should be enabled.
5661 *
5662 * @see #getFilterTouchesWhenObscured
5663 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5664 */
5665 public void setFilterTouchesWhenObscured(boolean enabled) {
5666 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5667 FILTER_TOUCHES_WHEN_OBSCURED);
5668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669
5670 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005671 * Indicates whether the entire hierarchy under this view will save its
5672 * state when a state saving traversal occurs from its parent. The default
5673 * is true; if false, these views will not be saved unless
5674 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5675 *
5676 * @return Returns true if the view state saving from parent is enabled, else false.
5677 *
5678 * @see #setSaveFromParentEnabled(boolean)
5679 */
5680 public boolean isSaveFromParentEnabled() {
5681 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5682 }
5683
5684 /**
5685 * Controls whether the entire hierarchy under this view will save its
5686 * state when a state saving traversal occurs from its parent. The default
5687 * is true; if false, these views will not be saved unless
5688 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5689 *
5690 * @param enabled Set to false to <em>disable</em> state saving, or true
5691 * (the default) to allow it.
5692 *
5693 * @see #isSaveFromParentEnabled()
5694 * @see #setId(int)
5695 * @see #onSaveInstanceState()
5696 */
5697 public void setSaveFromParentEnabled(boolean enabled) {
5698 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5699 }
5700
5701
5702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 * Returns whether this View is able to take focus.
5704 *
5705 * @return True if this view can take focus, or false otherwise.
5706 * @attr ref android.R.styleable#View_focusable
5707 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005708 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 public final boolean isFocusable() {
5710 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5711 }
5712
5713 /**
5714 * When a view is focusable, it may not want to take focus when in touch mode.
5715 * For example, a button would like focus when the user is navigating via a D-pad
5716 * so that the user can click on it, but once the user starts touching the screen,
5717 * the button shouldn't take focus
5718 * @return Whether the view is focusable in touch mode.
5719 * @attr ref android.R.styleable#View_focusableInTouchMode
5720 */
5721 @ViewDebug.ExportedProperty
5722 public final boolean isFocusableInTouchMode() {
5723 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5724 }
5725
5726 /**
5727 * Find the nearest view in the specified direction that can take focus.
5728 * This does not actually give focus to that view.
5729 *
5730 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5731 *
5732 * @return The nearest focusable in the specified direction, or null if none
5733 * can be found.
5734 */
5735 public View focusSearch(int direction) {
5736 if (mParent != null) {
5737 return mParent.focusSearch(this, direction);
5738 } else {
5739 return null;
5740 }
5741 }
5742
5743 /**
5744 * This method is the last chance for the focused view and its ancestors to
5745 * respond to an arrow key. This is called when the focused view did not
5746 * consume the key internally, nor could the view system find a new view in
5747 * the requested direction to give focus to.
5748 *
5749 * @param focused The currently focused view.
5750 * @param direction The direction focus wants to move. One of FOCUS_UP,
5751 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5752 * @return True if the this view consumed this unhandled move.
5753 */
5754 public boolean dispatchUnhandledMove(View focused, int direction) {
5755 return false;
5756 }
5757
5758 /**
5759 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005760 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005762 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5763 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 * @return The user specified next view, or null if there is none.
5765 */
5766 View findUserSetNextFocus(View root, int direction) {
5767 switch (direction) {
5768 case FOCUS_LEFT:
5769 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005770 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 case FOCUS_RIGHT:
5772 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005773 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 case FOCUS_UP:
5775 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005776 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 case FOCUS_DOWN:
5778 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005779 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005780 case FOCUS_FORWARD:
5781 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005782 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005783 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005784 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005785 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005786 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005787 @Override
5788 public boolean apply(View t) {
5789 return t.mNextFocusForwardId == id;
5790 }
5791 });
5792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 }
5794 return null;
5795 }
5796
Jeff Brown4dfbec22011-08-15 14:55:37 -07005797 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5798 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5799 @Override
5800 public boolean apply(View t) {
5801 return t.mID == childViewId;
5802 }
5803 });
5804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 if (result == null) {
5806 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5807 + "by user for id " + childViewId);
5808 }
5809 return result;
5810 }
5811
5812 /**
5813 * Find and return all focusable views that are descendants of this view,
5814 * possibly including this view if it is focusable itself.
5815 *
5816 * @param direction The direction of the focus
5817 * @return A list of focusable views
5818 */
5819 public ArrayList<View> getFocusables(int direction) {
5820 ArrayList<View> result = new ArrayList<View>(24);
5821 addFocusables(result, direction);
5822 return result;
5823 }
5824
5825 /**
5826 * Add any focusable views that are descendants of this view (possibly
5827 * including this view if it is focusable itself) to views. If we are in touch mode,
5828 * only add views that are also focusable in touch mode.
5829 *
5830 * @param views Focusable views found so far
5831 * @param direction The direction of the focus
5832 */
5833 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005834 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836
svetoslavganov75986cf2009-05-14 22:28:01 -07005837 /**
5838 * Adds any focusable views that are descendants of this view (possibly
5839 * including this view if it is focusable itself) to views. This method
5840 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005841 * only views focusable in touch mode if we are in touch mode or
5842 * only views that can take accessibility focus if accessibility is enabeld
5843 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005844 *
5845 * @param views Focusable views found so far or null if all we are interested is
5846 * the number of focusables.
5847 * @param direction The direction of the focus.
5848 * @param focusableMode The type of focusables to be added.
5849 *
5850 * @see #FOCUSABLES_ALL
5851 * @see #FOCUSABLES_TOUCH_MODE
5852 */
5853 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005854 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005855 return;
5856 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005857 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5858 if (AccessibilityManager.getInstance(mContext).isEnabled()
5859 && includeForAccessibility()) {
5860 views.add(this);
5861 return;
5862 }
5863 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005864 if (!isFocusable()) {
5865 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005866 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005867 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5868 && isInTouchMode() && !isFocusableInTouchMode()) {
5869 return;
5870 }
5871 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 }
5873
5874 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005875 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005876 * The search is performed by either the text that the View renders or the content
5877 * description that describes the view for accessibility purposes and the view does
5878 * not render or both. Clients can specify how the search is to be performed via
5879 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5880 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005881 *
5882 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005883 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005884 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005885 * @see #FIND_VIEWS_WITH_TEXT
5886 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5887 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005888 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005889 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005890 if (getAccessibilityNodeProvider() != null) {
5891 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5892 outViews.add(this);
5893 }
5894 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5895 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005896 String searchedLowerCase = searched.toString().toLowerCase();
5897 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5898 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5899 outViews.add(this);
5900 }
5901 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005902 }
5903
5904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 * Find and return all touchable views that are descendants of this view,
5906 * possibly including this view if it is touchable itself.
5907 *
5908 * @return A list of touchable views
5909 */
5910 public ArrayList<View> getTouchables() {
5911 ArrayList<View> result = new ArrayList<View>();
5912 addTouchables(result);
5913 return result;
5914 }
5915
5916 /**
5917 * Add any touchable views that are descendants of this view (possibly
5918 * including this view if it is touchable itself) to views.
5919 *
5920 * @param views Touchable views found so far
5921 */
5922 public void addTouchables(ArrayList<View> views) {
5923 final int viewFlags = mViewFlags;
5924
5925 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5926 && (viewFlags & ENABLED_MASK) == ENABLED) {
5927 views.add(this);
5928 }
5929 }
5930
5931 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005932 * Returns whether this View is accessibility focused.
5933 *
5934 * @return True if this View is accessibility focused.
5935 */
5936 boolean isAccessibilityFocused() {
5937 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5938 }
5939
5940 /**
5941 * Call this to try to give accessibility focus to this view.
5942 *
5943 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5944 * returns false or the view is no visible or the view already has accessibility
5945 * focus.
5946 *
5947 * See also {@link #focusSearch(int)}, which is what you call to say that you
5948 * have focus, and you want your parent to look for the next one.
5949 *
5950 * @return Whether this view actually took accessibility focus.
5951 *
5952 * @hide
5953 */
5954 public boolean requestAccessibilityFocus() {
5955 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5956 return false;
5957 }
5958 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5959 return false;
5960 }
5961 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5962 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5963 ViewRootImpl viewRootImpl = getViewRootImpl();
5964 if (viewRootImpl != null) {
5965 viewRootImpl.setAccessibilityFocusedHost(this);
5966 }
5967 invalidate();
5968 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5969 notifyAccessibilityStateChanged();
5970 // Try to give input focus to this view - not a descendant.
5971 requestFocusNoSearch(View.FOCUS_DOWN, null);
5972 return true;
5973 }
5974 return false;
5975 }
5976
5977 /**
5978 * Call this to try to clear accessibility focus of this view.
5979 *
5980 * See also {@link #focusSearch(int)}, which is what you call to say that you
5981 * have focus, and you want your parent to look for the next one.
5982 *
5983 * @hide
5984 */
5985 public void clearAccessibilityFocus() {
5986 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5987 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5988 ViewRootImpl viewRootImpl = getViewRootImpl();
5989 if (viewRootImpl != null) {
5990 viewRootImpl.setAccessibilityFocusedHost(null);
5991 }
5992 invalidate();
5993 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5994 notifyAccessibilityStateChanged();
5995 // Try to move accessibility focus to the input focus.
5996 View rootView = getRootView();
5997 if (rootView != null) {
5998 View inputFocus = rootView.findFocus();
5999 if (inputFocus != null) {
6000 inputFocus.requestAccessibilityFocus();
6001 }
6002 }
6003 }
6004 }
6005
6006 /**
6007 * Find the best view to take accessibility focus from a hover.
6008 * This function finds the deepest actionable view and if that
6009 * fails ask the parent to take accessibility focus from hover.
6010 *
6011 * @param x The X hovered location in this view coorditantes.
6012 * @param y The Y hovered location in this view coorditantes.
6013 * @return Whether the request was handled.
6014 *
6015 * @hide
6016 */
6017 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6018 if (onRequestAccessibilityFocusFromHover(x, y)) {
6019 return true;
6020 }
6021 ViewParent parent = mParent;
6022 if (parent instanceof View) {
6023 View parentView = (View) parent;
6024
6025 float[] position = mAttachInfo.mTmpTransformLocation;
6026 position[0] = x;
6027 position[1] = y;
6028
6029 // Compensate for the transformation of the current matrix.
6030 if (!hasIdentityMatrix()) {
6031 getMatrix().mapPoints(position);
6032 }
6033
6034 // Compensate for the parent scroll and the offset
6035 // of this view stop from the parent top.
6036 position[0] += mLeft - parentView.mScrollX;
6037 position[1] += mTop - parentView.mScrollY;
6038
6039 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6040 }
6041 return false;
6042 }
6043
6044 /**
6045 * Requests to give this View focus from hover.
6046 *
6047 * @param x The X hovered location in this view coorditantes.
6048 * @param y The Y hovered location in this view coorditantes.
6049 * @return Whether the request was handled.
6050 *
6051 * @hide
6052 */
6053 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6054 if (includeForAccessibility()
6055 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6056 return requestAccessibilityFocus();
6057 }
6058 return false;
6059 }
6060
6061 /**
6062 * Clears accessibility focus without calling any callback methods
6063 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6064 * is used for clearing accessibility focus when giving this focus to
6065 * another view.
6066 */
6067 void clearAccessibilityFocusNoCallbacks() {
6068 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6069 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6070 invalidate();
6071 }
6072 }
6073
6074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 * Call this to try to give focus to a specific view or to one of its
6076 * descendants.
6077 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006078 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6079 * false), or if it is focusable and it is not focusable in touch mode
6080 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006082 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 * have focus, and you want your parent to look for the next one.
6084 *
6085 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6086 * {@link #FOCUS_DOWN} and <code>null</code>.
6087 *
6088 * @return Whether this view or one of its descendants actually took focus.
6089 */
6090 public final boolean requestFocus() {
6091 return requestFocus(View.FOCUS_DOWN);
6092 }
6093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 /**
6095 * Call this to try to give focus to a specific view or to one of its
6096 * descendants and give it a hint about what direction focus is heading.
6097 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006098 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6099 * false), or if it is focusable and it is not focusable in touch mode
6100 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006102 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006103 * have focus, and you want your parent to look for the next one.
6104 *
6105 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6106 * <code>null</code> set for the previously focused rectangle.
6107 *
6108 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6109 * @return Whether this view or one of its descendants actually took focus.
6110 */
6111 public final boolean requestFocus(int direction) {
6112 return requestFocus(direction, null);
6113 }
6114
6115 /**
6116 * Call this to try to give focus to a specific view or to one of its descendants
6117 * and give it hints about the direction and a specific rectangle that the focus
6118 * is coming from. The rectangle can help give larger views a finer grained hint
6119 * about where focus is coming from, and therefore, where to show selection, or
6120 * forward focus change internally.
6121 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006122 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6123 * false), or if it is focusable and it is not focusable in touch mode
6124 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 *
6126 * A View will not take focus if it is not visible.
6127 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006128 * A View will not take focus if one of its parents has
6129 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6130 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006132 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 * have focus, and you want your parent to look for the next one.
6134 *
6135 * You may wish to override this method if your custom {@link View} has an internal
6136 * {@link View} that it wishes to forward the request to.
6137 *
6138 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6139 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6140 * to give a finer grained hint about where focus is coming from. May be null
6141 * if there is no hint.
6142 * @return Whether this view or one of its descendants actually took focus.
6143 */
6144 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006145 return requestFocusNoSearch(direction, previouslyFocusedRect);
6146 }
6147
6148 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 // need to be focusable
6150 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6151 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6152 return false;
6153 }
6154
6155 // need to be focusable in touch mode if in touch mode
6156 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006157 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6158 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 }
6160
6161 // need to not have any parents blocking us
6162 if (hasAncestorThatBlocksDescendantFocus()) {
6163 return false;
6164 }
6165
6166 handleFocusGainInternal(direction, previouslyFocusedRect);
6167 return true;
6168 }
6169
6170 /**
6171 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6172 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6173 * touch mode to request focus when they are touched.
6174 *
6175 * @return Whether this view or one of its descendants actually took focus.
6176 *
6177 * @see #isInTouchMode()
6178 *
6179 */
6180 public final boolean requestFocusFromTouch() {
6181 // Leave touch mode if we need to
6182 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006183 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006184 if (viewRoot != null) {
6185 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 }
6187 }
6188 return requestFocus(View.FOCUS_DOWN);
6189 }
6190
6191 /**
6192 * @return Whether any ancestor of this view blocks descendant focus.
6193 */
6194 private boolean hasAncestorThatBlocksDescendantFocus() {
6195 ViewParent ancestor = mParent;
6196 while (ancestor instanceof ViewGroup) {
6197 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6198 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6199 return true;
6200 } else {
6201 ancestor = vgAncestor.getParent();
6202 }
6203 }
6204 return false;
6205 }
6206
6207 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006208 * Gets the mode for determining whether this View is important for accessibility
6209 * which is if it fires accessibility events and if it is reported to
6210 * accessibility services that query the screen.
6211 *
6212 * @return The mode for determining whether a View is important for accessibility.
6213 *
6214 * @attr ref android.R.styleable#View_importantForAccessibility
6215 *
6216 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6217 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6218 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6219 */
6220 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6221 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6222 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6223 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6224 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6225 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6226 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6227 })
6228 public int getImportantForAccessibility() {
6229 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6230 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6231 }
6232
6233 /**
6234 * Sets how to determine whether this view is important for accessibility
6235 * which is if it fires accessibility events and if it is reported to
6236 * accessibility services that query the screen.
6237 *
6238 * @param mode How to determine whether this view is important for accessibility.
6239 *
6240 * @attr ref android.R.styleable#View_importantForAccessibility
6241 *
6242 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6243 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6244 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6245 */
6246 public void setImportantForAccessibility(int mode) {
6247 if (mode != getImportantForAccessibility()) {
6248 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6249 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6250 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6251 notifyAccessibilityStateChanged();
6252 }
6253 }
6254
6255 /**
6256 * Gets whether this view should be exposed for accessibility.
6257 *
6258 * @return Whether the view is exposed for accessibility.
6259 *
6260 * @hide
6261 */
6262 public boolean isImportantForAccessibility() {
6263 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6264 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6265 switch (mode) {
6266 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6267 return true;
6268 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6269 return false;
6270 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6271 return isActionableForAccessibility() || hasListenersForAccessibility();
6272 default:
6273 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6274 + mode);
6275 }
6276 }
6277
6278 /**
6279 * Gets the parent for accessibility purposes. Note that the parent for
6280 * accessibility is not necessary the immediate parent. It is the first
6281 * predecessor that is important for accessibility.
6282 *
6283 * @return The parent for accessibility purposes.
6284 */
6285 public ViewParent getParentForAccessibility() {
6286 if (mParent instanceof View) {
6287 View parentView = (View) mParent;
6288 if (parentView.includeForAccessibility()) {
6289 return mParent;
6290 } else {
6291 return mParent.getParentForAccessibility();
6292 }
6293 }
6294 return null;
6295 }
6296
6297 /**
6298 * Adds the children of a given View for accessibility. Since some Views are
6299 * not important for accessibility the children for accessibility are not
6300 * necessarily direct children of the riew, rather they are the first level of
6301 * descendants important for accessibility.
6302 *
6303 * @param children The list of children for accessibility.
6304 */
6305 public void addChildrenForAccessibility(ArrayList<View> children) {
6306 if (includeForAccessibility()) {
6307 children.add(this);
6308 }
6309 }
6310
6311 /**
6312 * Whether to regard this view for accessibility. A view is regarded for
6313 * accessibility if it is important for accessibility or the querying
6314 * accessibility service has explicitly requested that view not
6315 * important for accessibility are regarded.
6316 *
6317 * @return Whether to regard the view for accessibility.
6318 */
6319 boolean includeForAccessibility() {
6320 if (mAttachInfo != null) {
6321 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006322 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006323 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006324 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006325 }
6326 }
6327 return false;
6328 }
6329
6330 /**
6331 * Returns whether the View is considered actionable from
6332 * accessibility perspective. Such view are important for
6333 * accessiiblity.
6334 *
6335 * @return True if the view is actionable for accessibility.
6336 */
6337 private boolean isActionableForAccessibility() {
6338 return (isClickable() || isLongClickable() || isFocusable());
6339 }
6340
6341 /**
6342 * Returns whether the View has registered callbacks wich makes it
6343 * important for accessiiblity.
6344 *
6345 * @return True if the view is actionable for accessibility.
6346 */
6347 private boolean hasListenersForAccessibility() {
6348 ListenerInfo info = getListenerInfo();
6349 return mTouchDelegate != null || info.mOnKeyListener != null
6350 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6351 || info.mOnHoverListener != null || info.mOnDragListener != null;
6352 }
6353
6354 /**
6355 * Notifies accessibility services that some view's important for
6356 * accessibility state has changed. Note that such notifications
6357 * are made at most once every
6358 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6359 * to avoid unnecessary load to the system. Also once a view has
6360 * made a notifucation this method is a NOP until the notification has
6361 * been sent to clients.
6362 *
6363 * @hide
6364 *
6365 * TODO: Makse sure this method is called for any view state change
6366 * that is interesting for accessilility purposes.
6367 */
6368 public void notifyAccessibilityStateChanged() {
6369 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6370 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6371 if (mParent != null) {
6372 mParent.childAccessibilityStateChanged(this);
6373 }
6374 }
6375 }
6376
6377 /**
6378 * Reset the state indicating the this view has requested clients
6379 * interested in its accessiblity state to be notified.
6380 *
6381 * @hide
6382 */
6383 public void resetAccessibilityStateChanged() {
6384 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6385 }
6386
6387 /**
6388 * Performs the specified accessibility action on the view. For
6389 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6390 *
6391 * @param action The action to perform.
6392 * @return Whether the action was performed.
6393 */
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07006394 public boolean performAccessibilityAction(int action, Bundle args) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006395 switch (action) {
6396 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006397 if (isClickable()) {
6398 performClick();
6399 }
6400 } break;
6401 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6402 if (isLongClickable()) {
6403 performLongClick();
6404 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006405 } break;
6406 case AccessibilityNodeInfo.ACTION_FOCUS: {
6407 if (!hasFocus()) {
6408 // Get out of touch mode since accessibility
6409 // wants to move focus around.
6410 getViewRootImpl().ensureTouchMode(false);
6411 return requestFocus();
6412 }
6413 } break;
6414 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6415 if (hasFocus()) {
6416 clearFocus();
6417 return !isFocused();
6418 }
6419 } break;
6420 case AccessibilityNodeInfo.ACTION_SELECT: {
6421 if (!isSelected()) {
6422 setSelected(true);
6423 return isSelected();
6424 }
6425 } break;
6426 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6427 if (isSelected()) {
6428 setSelected(false);
6429 return !isSelected();
6430 }
6431 } break;
6432 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6433 if (!isAccessibilityFocused()) {
6434 return requestAccessibilityFocus();
6435 }
6436 } break;
6437 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6438 if (isAccessibilityFocused()) {
6439 clearAccessibilityFocus();
6440 return true;
6441 }
6442 } break;
6443 }
6444 return false;
6445 }
6446
6447 /**
Romain Guya440b002010-02-24 15:57:54 -08006448 * @hide
6449 */
6450 public void dispatchStartTemporaryDetach() {
6451 onStartTemporaryDetach();
6452 }
6453
6454 /**
6455 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6457 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006458 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 */
6460 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006461 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006462 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006463 }
6464
6465 /**
6466 * @hide
6467 */
6468 public void dispatchFinishTemporaryDetach() {
6469 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 }
Romain Guy8506ab42009-06-11 17:35:47 -07006471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 /**
6473 * Called after {@link #onStartTemporaryDetach} when the container is done
6474 * changing the view.
6475 */
6476 public void onFinishTemporaryDetach() {
6477 }
Romain Guy8506ab42009-06-11 17:35:47 -07006478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006480 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6481 * for this view's window. Returns null if the view is not currently attached
6482 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006483 * just use the standard high-level event callbacks like
6484 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006485 */
6486 public KeyEvent.DispatcherState getKeyDispatcherState() {
6487 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6488 }
Joe Malin32736f02011-01-19 16:14:20 -08006489
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 * Dispatch a key event before it is processed by any input method
6492 * associated with the view hierarchy. This can be used to intercept
6493 * key events in special situations before the IME consumes them; a
6494 * typical example would be handling the BACK key to update the application's
6495 * UI instead of allowing the IME to see it and close itself.
6496 *
6497 * @param event The key event to be dispatched.
6498 * @return True if the event was handled, false otherwise.
6499 */
6500 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6501 return onKeyPreIme(event.getKeyCode(), event);
6502 }
6503
6504 /**
6505 * Dispatch a key event to the next view on the focus path. This path runs
6506 * from the top of the view tree down to the currently focused view. If this
6507 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6508 * the next node down the focus path. This method also fires any key
6509 * listeners.
6510 *
6511 * @param event The key event to be dispatched.
6512 * @return True if the event was handled, false otherwise.
6513 */
6514 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006515 if (mInputEventConsistencyVerifier != null) {
6516 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518
Jeff Brown21bc5c92011-02-28 18:27:14 -08006519 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006520 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006521 ListenerInfo li = mListenerInfo;
6522 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6523 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 return true;
6525 }
6526
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006527 if (event.dispatch(this, mAttachInfo != null
6528 ? mAttachInfo.mKeyDispatchState : null, this)) {
6529 return true;
6530 }
6531
6532 if (mInputEventConsistencyVerifier != null) {
6533 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6534 }
6535 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 }
6537
6538 /**
6539 * Dispatches a key shortcut event.
6540 *
6541 * @param event The key event to be dispatched.
6542 * @return True if the event was handled by the view, false otherwise.
6543 */
6544 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6545 return onKeyShortcut(event.getKeyCode(), event);
6546 }
6547
6548 /**
6549 * Pass the touch screen motion event down to the target view, or this
6550 * view if it is the target.
6551 *
6552 * @param event The motion event to be dispatched.
6553 * @return True if the event was handled by the view, false otherwise.
6554 */
6555 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006556 if (mInputEventConsistencyVerifier != null) {
6557 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6558 }
6559
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006560 if (onFilterTouchEventForSecurity(event)) {
6561 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006562 ListenerInfo li = mListenerInfo;
6563 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6564 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006565 return true;
6566 }
6567
6568 if (onTouchEvent(event)) {
6569 return true;
6570 }
Jeff Brown85a31762010-09-01 17:01:00 -07006571 }
6572
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006573 if (mInputEventConsistencyVerifier != null) {
6574 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006576 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 }
6578
6579 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006580 * Filter the touch event to apply security policies.
6581 *
6582 * @param event The motion event to be filtered.
6583 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006584 *
Jeff Brown85a31762010-09-01 17:01:00 -07006585 * @see #getFilterTouchesWhenObscured
6586 */
6587 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006588 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006589 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6590 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6591 // Window is obscured, drop this touch.
6592 return false;
6593 }
6594 return true;
6595 }
6596
6597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 * Pass a trackball motion event down to the focused view.
6599 *
6600 * @param event The motion event to be dispatched.
6601 * @return True if the event was handled by the view, false otherwise.
6602 */
6603 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006604 if (mInputEventConsistencyVerifier != null) {
6605 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6606 }
6607
Romain Guy02ccac62011-06-24 13:20:23 -07006608 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 }
6610
6611 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006612 * Dispatch a generic motion event.
6613 * <p>
6614 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6615 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006616 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006617 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006618 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006619 *
6620 * @param event The motion event to be dispatched.
6621 * @return True if the event was handled by the view, false otherwise.
6622 */
6623 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006624 if (mInputEventConsistencyVerifier != null) {
6625 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6626 }
6627
Jeff Browna032cc02011-03-07 16:56:21 -08006628 final int source = event.getSource();
6629 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6630 final int action = event.getAction();
6631 if (action == MotionEvent.ACTION_HOVER_ENTER
6632 || action == MotionEvent.ACTION_HOVER_MOVE
6633 || action == MotionEvent.ACTION_HOVER_EXIT) {
6634 if (dispatchHoverEvent(event)) {
6635 return true;
6636 }
6637 } else if (dispatchGenericPointerEvent(event)) {
6638 return true;
6639 }
6640 } else if (dispatchGenericFocusedEvent(event)) {
6641 return true;
6642 }
6643
Jeff Brown10b62902011-06-20 16:40:37 -07006644 if (dispatchGenericMotionEventInternal(event)) {
6645 return true;
6646 }
6647
6648 if (mInputEventConsistencyVerifier != null) {
6649 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6650 }
6651 return false;
6652 }
6653
6654 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006655 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006656 ListenerInfo li = mListenerInfo;
6657 if (li != null && li.mOnGenericMotionListener != null
6658 && (mViewFlags & ENABLED_MASK) == ENABLED
6659 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006660 return true;
6661 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006662
6663 if (onGenericMotionEvent(event)) {
6664 return true;
6665 }
6666
6667 if (mInputEventConsistencyVerifier != null) {
6668 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6669 }
6670 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006671 }
6672
6673 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006674 * Dispatch a hover event.
6675 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006676 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006677 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006678 * </p>
6679 *
6680 * @param event The motion event to be dispatched.
6681 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006682 */
6683 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006684 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006685 ListenerInfo li = mListenerInfo;
6686 if (li != null && li.mOnHoverListener != null
6687 && (mViewFlags & ENABLED_MASK) == ENABLED
6688 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006689 return true;
6690 }
6691
Jeff Browna032cc02011-03-07 16:56:21 -08006692 return onHoverEvent(event);
6693 }
6694
6695 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006696 * Returns true if the view has a child to which it has recently sent
6697 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6698 * it does not have a hovered child, then it must be the innermost hovered view.
6699 * @hide
6700 */
6701 protected boolean hasHoveredChild() {
6702 return false;
6703 }
6704
6705 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006706 * Dispatch a generic motion event to the view under the first pointer.
6707 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006708 * Do not call this method directly.
6709 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006710 * </p>
6711 *
6712 * @param event The motion event to be dispatched.
6713 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006714 */
6715 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6716 return false;
6717 }
6718
6719 /**
6720 * Dispatch a generic motion event to the currently focused view.
6721 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006722 * Do not call this method directly.
6723 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006724 * </p>
6725 *
6726 * @param event The motion event to be dispatched.
6727 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006728 */
6729 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6730 return false;
6731 }
6732
6733 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006734 * Dispatch a pointer event.
6735 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006736 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6737 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6738 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006739 * and should not be expected to handle other pointing device features.
6740 * </p>
6741 *
6742 * @param event The motion event to be dispatched.
6743 * @return True if the event was handled by the view, false otherwise.
6744 * @hide
6745 */
6746 public final boolean dispatchPointerEvent(MotionEvent event) {
6747 if (event.isTouchEvent()) {
6748 return dispatchTouchEvent(event);
6749 } else {
6750 return dispatchGenericMotionEvent(event);
6751 }
6752 }
6753
6754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 * Called when the window containing this view gains or loses window focus.
6756 * ViewGroups should override to route to their children.
6757 *
6758 * @param hasFocus True if the window containing this view now has focus,
6759 * false otherwise.
6760 */
6761 public void dispatchWindowFocusChanged(boolean hasFocus) {
6762 onWindowFocusChanged(hasFocus);
6763 }
6764
6765 /**
6766 * Called when the window containing this view gains or loses focus. Note
6767 * that this is separate from view focus: to receive key events, both
6768 * your view and its window must have focus. If a window is displayed
6769 * on top of yours that takes input focus, then your own window will lose
6770 * focus but the view focus will remain unchanged.
6771 *
6772 * @param hasWindowFocus True if the window containing this view now has
6773 * focus, false otherwise.
6774 */
6775 public void onWindowFocusChanged(boolean hasWindowFocus) {
6776 InputMethodManager imm = InputMethodManager.peekInstance();
6777 if (!hasWindowFocus) {
6778 if (isPressed()) {
6779 setPressed(false);
6780 }
6781 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6782 imm.focusOut(this);
6783 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006784 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006785 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006786 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6788 imm.focusIn(this);
6789 }
6790 refreshDrawableState();
6791 }
6792
6793 /**
6794 * Returns true if this view is in a window that currently has window focus.
6795 * Note that this is not the same as the view itself having focus.
6796 *
6797 * @return True if this view is in a window that currently has window focus.
6798 */
6799 public boolean hasWindowFocus() {
6800 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6801 }
6802
6803 /**
Adam Powell326d8082009-12-09 15:10:07 -08006804 * Dispatch a view visibility change down the view hierarchy.
6805 * ViewGroups should override to route to their children.
6806 * @param changedView The view whose visibility changed. Could be 'this' or
6807 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006808 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6809 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006810 */
6811 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6812 onVisibilityChanged(changedView, visibility);
6813 }
6814
6815 /**
6816 * Called when the visibility of the view or an ancestor of the view is changed.
6817 * @param changedView The view whose visibility changed. Could be 'this' or
6818 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006819 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6820 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006821 */
6822 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006823 if (visibility == VISIBLE) {
6824 if (mAttachInfo != null) {
6825 initialAwakenScrollBars();
6826 } else {
6827 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6828 }
6829 }
Adam Powell326d8082009-12-09 15:10:07 -08006830 }
6831
6832 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006833 * Dispatch a hint about whether this view is displayed. For instance, when
6834 * a View moves out of the screen, it might receives a display hint indicating
6835 * the view is not displayed. Applications should not <em>rely</em> on this hint
6836 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006837 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006838 * @param hint A hint about whether or not this view is displayed:
6839 * {@link #VISIBLE} or {@link #INVISIBLE}.
6840 */
6841 public void dispatchDisplayHint(int hint) {
6842 onDisplayHint(hint);
6843 }
6844
6845 /**
6846 * Gives this view a hint about whether is displayed or not. For instance, when
6847 * a View moves out of the screen, it might receives a display hint indicating
6848 * the view is not displayed. Applications should not <em>rely</em> on this hint
6849 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006850 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006851 * @param hint A hint about whether or not this view is displayed:
6852 * {@link #VISIBLE} or {@link #INVISIBLE}.
6853 */
6854 protected void onDisplayHint(int hint) {
6855 }
6856
6857 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 * Dispatch a window visibility change down the view hierarchy.
6859 * ViewGroups should override to route to their children.
6860 *
6861 * @param visibility The new visibility of the window.
6862 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006863 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 */
6865 public void dispatchWindowVisibilityChanged(int visibility) {
6866 onWindowVisibilityChanged(visibility);
6867 }
6868
6869 /**
6870 * Called when the window containing has change its visibility
6871 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6872 * that this tells you whether or not your window is being made visible
6873 * to the window manager; this does <em>not</em> tell you whether or not
6874 * your window is obscured by other windows on the screen, even if it
6875 * is itself visible.
6876 *
6877 * @param visibility The new visibility of the window.
6878 */
6879 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006880 if (visibility == VISIBLE) {
6881 initialAwakenScrollBars();
6882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 }
6884
6885 /**
6886 * Returns the current visibility of the window this view is attached to
6887 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6888 *
6889 * @return Returns the current visibility of the view's window.
6890 */
6891 public int getWindowVisibility() {
6892 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6893 }
6894
6895 /**
6896 * Retrieve the overall visible display size in which the window this view is
6897 * attached to has been positioned in. This takes into account screen
6898 * decorations above the window, for both cases where the window itself
6899 * is being position inside of them or the window is being placed under
6900 * then and covered insets are used for the window to position its content
6901 * inside. In effect, this tells you the available area where content can
6902 * be placed and remain visible to users.
6903 *
6904 * <p>This function requires an IPC back to the window manager to retrieve
6905 * the requested information, so should not be used in performance critical
6906 * code like drawing.
6907 *
6908 * @param outRect Filled in with the visible display frame. If the view
6909 * is not attached to a window, this is simply the raw display size.
6910 */
6911 public void getWindowVisibleDisplayFrame(Rect outRect) {
6912 if (mAttachInfo != null) {
6913 try {
6914 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6915 } catch (RemoteException e) {
6916 return;
6917 }
6918 // XXX This is really broken, and probably all needs to be done
6919 // in the window manager, and we need to know more about whether
6920 // we want the area behind or in front of the IME.
6921 final Rect insets = mAttachInfo.mVisibleInsets;
6922 outRect.left += insets.left;
6923 outRect.top += insets.top;
6924 outRect.right -= insets.right;
6925 outRect.bottom -= insets.bottom;
6926 return;
6927 }
6928 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006929 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 }
6931
6932 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006933 * Dispatch a notification about a resource configuration change down
6934 * the view hierarchy.
6935 * ViewGroups should override to route to their children.
6936 *
6937 * @param newConfig The new resource configuration.
6938 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006939 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006940 */
6941 public void dispatchConfigurationChanged(Configuration newConfig) {
6942 onConfigurationChanged(newConfig);
6943 }
6944
6945 /**
6946 * Called when the current configuration of the resources being used
6947 * by the application have changed. You can use this to decide when
6948 * to reload resources that can changed based on orientation and other
6949 * configuration characterstics. You only need to use this if you are
6950 * not relying on the normal {@link android.app.Activity} mechanism of
6951 * recreating the activity instance upon a configuration change.
6952 *
6953 * @param newConfig The new resource configuration.
6954 */
6955 protected void onConfigurationChanged(Configuration newConfig) {
6956 }
6957
6958 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 * Private function to aggregate all per-view attributes in to the view
6960 * root.
6961 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006962 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6963 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 }
6965
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006966 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6967 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006968 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006969 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006970 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006971 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006972 ListenerInfo li = mListenerInfo;
6973 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006974 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 }
6977 }
6978
6979 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006980 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006982 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6983 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 ai.mRecomputeGlobalAttributes = true;
6985 }
6986 }
6987 }
6988
6989 /**
6990 * Returns whether the device is currently in touch mode. Touch mode is entered
6991 * once the user begins interacting with the device by touch, and affects various
6992 * things like whether focus is always visible to the user.
6993 *
6994 * @return Whether the device is in touch mode.
6995 */
6996 @ViewDebug.ExportedProperty
6997 public boolean isInTouchMode() {
6998 if (mAttachInfo != null) {
6999 return mAttachInfo.mInTouchMode;
7000 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007001 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 }
7003 }
7004
7005 /**
7006 * Returns the context the view is running in, through which it can
7007 * access the current theme, resources, etc.
7008 *
7009 * @return The view's Context.
7010 */
7011 @ViewDebug.CapturedViewProperty
7012 public final Context getContext() {
7013 return mContext;
7014 }
7015
7016 /**
7017 * Handle a key event before it is processed by any input method
7018 * associated with the view hierarchy. This can be used to intercept
7019 * key events in special situations before the IME consumes them; a
7020 * typical example would be handling the BACK key to update the application's
7021 * UI instead of allowing the IME to see it and close itself.
7022 *
7023 * @param keyCode The value in event.getKeyCode().
7024 * @param event Description of the key event.
7025 * @return If you handled the event, return true. If you want to allow the
7026 * event to be handled by the next receiver, return false.
7027 */
7028 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7029 return false;
7030 }
7031
7032 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007033 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7034 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007035 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7036 * is released, if the view is enabled and clickable.
7037 *
7038 * @param keyCode A key code that represents the button pressed, from
7039 * {@link android.view.KeyEvent}.
7040 * @param event The KeyEvent object that defines the button action.
7041 */
7042 public boolean onKeyDown(int keyCode, KeyEvent event) {
7043 boolean result = false;
7044
7045 switch (keyCode) {
7046 case KeyEvent.KEYCODE_DPAD_CENTER:
7047 case KeyEvent.KEYCODE_ENTER: {
7048 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7049 return true;
7050 }
7051 // Long clickable items don't necessarily have to be clickable
7052 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7053 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7054 (event.getRepeatCount() == 0)) {
7055 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007056 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057 return true;
7058 }
7059 break;
7060 }
7061 }
7062 return result;
7063 }
7064
7065 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007066 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7067 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7068 * the event).
7069 */
7070 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7071 return false;
7072 }
7073
7074 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007075 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7076 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7078 * {@link KeyEvent#KEYCODE_ENTER} is released.
7079 *
7080 * @param keyCode A key code that represents the button pressed, from
7081 * {@link android.view.KeyEvent}.
7082 * @param event The KeyEvent object that defines the button action.
7083 */
7084 public boolean onKeyUp(int keyCode, KeyEvent event) {
7085 boolean result = false;
7086
7087 switch (keyCode) {
7088 case KeyEvent.KEYCODE_DPAD_CENTER:
7089 case KeyEvent.KEYCODE_ENTER: {
7090 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7091 return true;
7092 }
7093 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7094 setPressed(false);
7095
7096 if (!mHasPerformedLongPress) {
7097 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007098 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099
7100 result = performClick();
7101 }
7102 }
7103 break;
7104 }
7105 }
7106 return result;
7107 }
7108
7109 /**
7110 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7111 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7112 * the event).
7113 *
7114 * @param keyCode A key code that represents the button pressed, from
7115 * {@link android.view.KeyEvent}.
7116 * @param repeatCount The number of times the action was made.
7117 * @param event The KeyEvent object that defines the button action.
7118 */
7119 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7120 return false;
7121 }
7122
7123 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007124 * Called on the focused view when a key shortcut event is not handled.
7125 * Override this method to implement local key shortcuts for the View.
7126 * Key shortcuts can also be implemented by setting the
7127 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 *
7129 * @param keyCode The value in event.getKeyCode().
7130 * @param event Description of the key event.
7131 * @return If you handled the event, return true. If you want to allow the
7132 * event to be handled by the next receiver, return false.
7133 */
7134 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7135 return false;
7136 }
7137
7138 /**
7139 * Check whether the called view is a text editor, in which case it
7140 * would make sense to automatically display a soft input window for
7141 * it. Subclasses should override this if they implement
7142 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007143 * a call on that method would return a non-null InputConnection, and
7144 * they are really a first-class editor that the user would normally
7145 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007146 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007147 * <p>The default implementation always returns false. This does
7148 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7149 * will not be called or the user can not otherwise perform edits on your
7150 * view; it is just a hint to the system that this is not the primary
7151 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 * @return Returns true if this view is a text editor, else false.
7154 */
7155 public boolean onCheckIsTextEditor() {
7156 return false;
7157 }
Romain Guy8506ab42009-06-11 17:35:47 -07007158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 /**
7160 * Create a new InputConnection for an InputMethod to interact
7161 * with the view. The default implementation returns null, since it doesn't
7162 * support input methods. You can override this to implement such support.
7163 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007164 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 * <p>When implementing this, you probably also want to implement
7166 * {@link #onCheckIsTextEditor()} to indicate you will return a
7167 * non-null InputConnection.
7168 *
7169 * @param outAttrs Fill in with attribute information about the connection.
7170 */
7171 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7172 return null;
7173 }
7174
7175 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007176 * Called by the {@link android.view.inputmethod.InputMethodManager}
7177 * when a view who is not the current
7178 * input connection target is trying to make a call on the manager. The
7179 * default implementation returns false; you can override this to return
7180 * true for certain views if you are performing InputConnection proxying
7181 * to them.
7182 * @param view The View that is making the InputMethodManager call.
7183 * @return Return true to allow the call, false to reject.
7184 */
7185 public boolean checkInputConnectionProxy(View view) {
7186 return false;
7187 }
Romain Guy8506ab42009-06-11 17:35:47 -07007188
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 * Show the context menu for this view. It is not safe to hold on to the
7191 * menu after returning from this method.
7192 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007193 * You should normally not overload this method. Overload
7194 * {@link #onCreateContextMenu(ContextMenu)} or define an
7195 * {@link OnCreateContextMenuListener} to add items to the context menu.
7196 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 * @param menu The context menu to populate
7198 */
7199 public void createContextMenu(ContextMenu menu) {
7200 ContextMenuInfo menuInfo = getContextMenuInfo();
7201
7202 // Sets the current menu info so all items added to menu will have
7203 // my extra info set.
7204 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7205
7206 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007207 ListenerInfo li = mListenerInfo;
7208 if (li != null && li.mOnCreateContextMenuListener != null) {
7209 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 }
7211
7212 // Clear the extra information so subsequent items that aren't mine don't
7213 // have my extra info.
7214 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7215
7216 if (mParent != null) {
7217 mParent.createContextMenu(menu);
7218 }
7219 }
7220
7221 /**
7222 * Views should implement this if they have extra information to associate
7223 * with the context menu. The return result is supplied as a parameter to
7224 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7225 * callback.
7226 *
7227 * @return Extra information about the item for which the context menu
7228 * should be shown. This information will vary across different
7229 * subclasses of View.
7230 */
7231 protected ContextMenuInfo getContextMenuInfo() {
7232 return null;
7233 }
7234
7235 /**
7236 * Views should implement this if the view itself is going to add items to
7237 * the context menu.
7238 *
7239 * @param menu the context menu to populate
7240 */
7241 protected void onCreateContextMenu(ContextMenu menu) {
7242 }
7243
7244 /**
7245 * Implement this method to handle trackball motion events. The
7246 * <em>relative</em> movement of the trackball since the last event
7247 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7248 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7249 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7250 * they will often be fractional values, representing the more fine-grained
7251 * movement information available from a trackball).
7252 *
7253 * @param event The motion event.
7254 * @return True if the event was handled, false otherwise.
7255 */
7256 public boolean onTrackballEvent(MotionEvent event) {
7257 return false;
7258 }
7259
7260 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007261 * Implement this method to handle generic motion events.
7262 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007263 * Generic motion events describe joystick movements, mouse hovers, track pad
7264 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007265 * {@link MotionEvent#getSource() source} of the motion event specifies
7266 * the class of input that was received. Implementations of this method
7267 * must examine the bits in the source before processing the event.
7268 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007269 * </p><p>
7270 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7271 * are delivered to the view under the pointer. All other generic motion events are
7272 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007273 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007274 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007275 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007276 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7277 * // process the joystick movement...
7278 * return true;
7279 * }
7280 * }
7281 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7282 * switch (event.getAction()) {
7283 * case MotionEvent.ACTION_HOVER_MOVE:
7284 * // process the mouse hover movement...
7285 * return true;
7286 * case MotionEvent.ACTION_SCROLL:
7287 * // process the scroll wheel movement...
7288 * return true;
7289 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007290 * }
7291 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007292 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007293 *
7294 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007295 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007296 */
7297 public boolean onGenericMotionEvent(MotionEvent event) {
7298 return false;
7299 }
7300
7301 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007302 * Implement this method to handle hover events.
7303 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007304 * This method is called whenever a pointer is hovering into, over, or out of the
7305 * bounds of a view and the view is not currently being touched.
7306 * Hover events are represented as pointer events with action
7307 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7308 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7309 * </p>
7310 * <ul>
7311 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7312 * when the pointer enters the bounds of the view.</li>
7313 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7314 * when the pointer has already entered the bounds of the view and has moved.</li>
7315 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7316 * when the pointer has exited the bounds of the view or when the pointer is
7317 * about to go down due to a button click, tap, or similar user action that
7318 * causes the view to be touched.</li>
7319 * </ul>
7320 * <p>
7321 * The view should implement this method to return true to indicate that it is
7322 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007323 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007324 * The default implementation calls {@link #setHovered} to update the hovered state
7325 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007326 * is enabled and is clickable. The default implementation also sends hover
7327 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007328 * </p>
7329 *
7330 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007331 * @return True if the view handled the hover event.
7332 *
7333 * @see #isHovered
7334 * @see #setHovered
7335 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007336 */
7337 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007338 // The root view may receive hover (or touch) events that are outside the bounds of
7339 // the window. This code ensures that we only send accessibility events for
7340 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007341 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007342 if (!mSendingHoverAccessibilityEvents) {
7343 if ((action == MotionEvent.ACTION_HOVER_ENTER
7344 || action == MotionEvent.ACTION_HOVER_MOVE)
7345 && !hasHoveredChild()
7346 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007347 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007348 mSendingHoverAccessibilityEvents = true;
7349 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007350 }
7351 } else {
7352 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007353 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007354 && !pointInView(event.getX(), event.getY()))) {
7355 mSendingHoverAccessibilityEvents = false;
7356 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007357 // If the window does not have input focus we take away accessibility
7358 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007359 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007360 getViewRootImpl().setAccessibilityFocusedHost(null);
7361 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007362 }
Jeff Browna1b24182011-07-28 13:38:24 -07007363 }
7364
Jeff Brown87b7f802011-06-21 18:35:45 -07007365 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007366 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007367 case MotionEvent.ACTION_HOVER_ENTER:
7368 setHovered(true);
7369 break;
7370 case MotionEvent.ACTION_HOVER_EXIT:
7371 setHovered(false);
7372 break;
7373 }
Jeff Browna1b24182011-07-28 13:38:24 -07007374
7375 // Dispatch the event to onGenericMotionEvent before returning true.
7376 // This is to provide compatibility with existing applications that
7377 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7378 // break because of the new default handling for hoverable views
7379 // in onHoverEvent.
7380 // Note that onGenericMotionEvent will be called by default when
7381 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7382 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007383 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007384 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007385
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007386 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007387 }
7388
7389 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007390 * Returns true if the view should handle {@link #onHoverEvent}
7391 * by calling {@link #setHovered} to change its hovered state.
7392 *
7393 * @return True if the view is hoverable.
7394 */
7395 private boolean isHoverable() {
7396 final int viewFlags = mViewFlags;
7397 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7398 return false;
7399 }
7400
7401 return (viewFlags & CLICKABLE) == CLICKABLE
7402 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7403 }
7404
7405 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007406 * Returns true if the view is currently hovered.
7407 *
7408 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007409 *
7410 * @see #setHovered
7411 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007412 */
Jeff Brown10b62902011-06-20 16:40:37 -07007413 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007414 public boolean isHovered() {
7415 return (mPrivateFlags & HOVERED) != 0;
7416 }
7417
7418 /**
7419 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007420 * <p>
7421 * Calling this method also changes the drawable state of the view. This
7422 * enables the view to react to hover by using different drawable resources
7423 * to change its appearance.
7424 * </p><p>
7425 * The {@link #onHoverChanged} method is called when the hovered state changes.
7426 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007427 *
7428 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007429 *
7430 * @see #isHovered
7431 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007432 */
7433 public void setHovered(boolean hovered) {
7434 if (hovered) {
7435 if ((mPrivateFlags & HOVERED) == 0) {
7436 mPrivateFlags |= HOVERED;
7437 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007438 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007439 }
7440 } else {
7441 if ((mPrivateFlags & HOVERED) != 0) {
7442 mPrivateFlags &= ~HOVERED;
7443 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007444 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007445 }
7446 }
7447 }
7448
7449 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007450 * Implement this method to handle hover state changes.
7451 * <p>
7452 * This method is called whenever the hover state changes as a result of a
7453 * call to {@link #setHovered}.
7454 * </p>
7455 *
7456 * @param hovered The current hover state, as returned by {@link #isHovered}.
7457 *
7458 * @see #isHovered
7459 * @see #setHovered
7460 */
7461 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007462 }
7463
7464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 * Implement this method to handle touch screen motion events.
7466 *
7467 * @param event The motion event.
7468 * @return True if the event was handled, false otherwise.
7469 */
7470 public boolean onTouchEvent(MotionEvent event) {
7471 final int viewFlags = mViewFlags;
7472
7473 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007474 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007475 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 // A disabled view that is clickable still consumes the touch
7478 // events, it just doesn't respond to them.
7479 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7480 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7481 }
7482
7483 if (mTouchDelegate != null) {
7484 if (mTouchDelegate.onTouchEvent(event)) {
7485 return true;
7486 }
7487 }
7488
7489 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7490 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7491 switch (event.getAction()) {
7492 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007493 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7494 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 // take focus if we don't have it already and we should in
7496 // touch mode.
7497 boolean focusTaken = false;
7498 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7499 focusTaken = requestFocus();
7500 }
7501
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007502 if (prepressed) {
7503 // The button is being released before we actually
7504 // showed it as pressed. Make it show the pressed
7505 // state now (before scheduling the click) to ensure
7506 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007507 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007508 }
Joe Malin32736f02011-01-19 16:14:20 -08007509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 if (!mHasPerformedLongPress) {
7511 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007512 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513
7514 // Only perform take click actions if we were in the pressed state
7515 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007516 // Use a Runnable and post this rather than calling
7517 // performClick directly. This lets other visual state
7518 // of the view update before click actions start.
7519 if (mPerformClick == null) {
7520 mPerformClick = new PerformClick();
7521 }
7522 if (!post(mPerformClick)) {
7523 performClick();
7524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 }
7526 }
7527
7528 if (mUnsetPressedState == null) {
7529 mUnsetPressedState = new UnsetPressedState();
7530 }
7531
Adam Powelle14579b2009-12-16 18:39:52 -08007532 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007533 postDelayed(mUnsetPressedState,
7534 ViewConfiguration.getPressedStateDuration());
7535 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007536 // If the post failed, unpress right now
7537 mUnsetPressedState.run();
7538 }
Adam Powelle14579b2009-12-16 18:39:52 -08007539 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540 }
7541 break;
7542
7543 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007544 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007545
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007546 if (performButtonActionOnTouchDown(event)) {
7547 break;
7548 }
7549
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007550 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007551 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007552
7553 // For views inside a scrolling container, delay the pressed feedback for
7554 // a short period in case this is a scroll.
7555 if (isInScrollingContainer) {
7556 mPrivateFlags |= PREPRESSED;
7557 if (mPendingCheckForTap == null) {
7558 mPendingCheckForTap = new CheckForTap();
7559 }
7560 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7561 } else {
7562 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007563 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007564 checkForLongClick(0);
7565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 break;
7567
7568 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007569 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007570 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 break;
7572
7573 case MotionEvent.ACTION_MOVE:
7574 final int x = (int) event.getX();
7575 final int y = (int) event.getY();
7576
7577 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007578 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007580 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007582 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007583 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584
Adam Powell4d6f0662012-02-21 15:11:11 -08007585 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 }
7588 break;
7589 }
7590 return true;
7591 }
7592
7593 return false;
7594 }
7595
7596 /**
Adam Powell10298662011-08-14 18:26:30 -07007597 * @hide
7598 */
7599 public boolean isInScrollingContainer() {
7600 ViewParent p = getParent();
7601 while (p != null && p instanceof ViewGroup) {
7602 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7603 return true;
7604 }
7605 p = p.getParent();
7606 }
7607 return false;
7608 }
7609
7610 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007611 * Remove the longpress detection timer.
7612 */
7613 private void removeLongPressCallback() {
7614 if (mPendingCheckForLongPress != null) {
7615 removeCallbacks(mPendingCheckForLongPress);
7616 }
7617 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007618
7619 /**
7620 * Remove the pending click action
7621 */
7622 private void removePerformClickCallback() {
7623 if (mPerformClick != null) {
7624 removeCallbacks(mPerformClick);
7625 }
7626 }
7627
Adam Powelle14579b2009-12-16 18:39:52 -08007628 /**
Romain Guya440b002010-02-24 15:57:54 -08007629 * Remove the prepress detection timer.
7630 */
7631 private void removeUnsetPressCallback() {
7632 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7633 setPressed(false);
7634 removeCallbacks(mUnsetPressedState);
7635 }
7636 }
7637
7638 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007639 * Remove the tap detection timer.
7640 */
7641 private void removeTapCallback() {
7642 if (mPendingCheckForTap != null) {
7643 mPrivateFlags &= ~PREPRESSED;
7644 removeCallbacks(mPendingCheckForTap);
7645 }
7646 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007647
7648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 * Cancels a pending long press. Your subclass can use this if you
7650 * want the context menu to come up if the user presses and holds
7651 * at the same place, but you don't want it to come up if they press
7652 * and then move around enough to cause scrolling.
7653 */
7654 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007655 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007656
7657 /*
7658 * The prepressed state handled by the tap callback is a display
7659 * construct, but the tap callback will post a long press callback
7660 * less its own timeout. Remove it here.
7661 */
7662 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 }
7664
7665 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007666 * Remove the pending callback for sending a
7667 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7668 */
7669 private void removeSendViewScrolledAccessibilityEventCallback() {
7670 if (mSendViewScrolledAccessibilityEvent != null) {
7671 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7672 }
7673 }
7674
7675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 * Sets the TouchDelegate for this View.
7677 */
7678 public void setTouchDelegate(TouchDelegate delegate) {
7679 mTouchDelegate = delegate;
7680 }
7681
7682 /**
7683 * Gets the TouchDelegate for this View.
7684 */
7685 public TouchDelegate getTouchDelegate() {
7686 return mTouchDelegate;
7687 }
7688
7689 /**
7690 * Set flags controlling behavior of this view.
7691 *
7692 * @param flags Constant indicating the value which should be set
7693 * @param mask Constant indicating the bit range that should be changed
7694 */
7695 void setFlags(int flags, int mask) {
7696 int old = mViewFlags;
7697 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7698
7699 int changed = mViewFlags ^ old;
7700 if (changed == 0) {
7701 return;
7702 }
7703 int privateFlags = mPrivateFlags;
7704
7705 /* Check if the FOCUSABLE bit has changed */
7706 if (((changed & FOCUSABLE_MASK) != 0) &&
7707 ((privateFlags & HAS_BOUNDS) !=0)) {
7708 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7709 && ((privateFlags & FOCUSED) != 0)) {
7710 /* Give up focus if we are no longer focusable */
7711 clearFocus();
7712 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7713 && ((privateFlags & FOCUSED) == 0)) {
7714 /*
7715 * Tell the view system that we are now available to take focus
7716 * if no one else already has it.
7717 */
7718 if (mParent != null) mParent.focusableViewAvailable(this);
7719 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007720 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7721 notifyAccessibilityStateChanged();
7722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 }
7724
7725 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7726 if ((changed & VISIBILITY_MASK) != 0) {
7727 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007728 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007729 * it was not visible. Marking it drawn ensures that the invalidation will
7730 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007732 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007733 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734
7735 needGlobalAttributesUpdate(true);
7736
7737 // a view becoming visible is worth notifying the parent
7738 // about in case nothing has focus. even if this specific view
7739 // isn't focusable, it may contain something that is, so let
7740 // the root view try to give this focus if nothing else does.
7741 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7742 mParent.focusableViewAvailable(this);
7743 }
7744 }
7745 }
7746
7747 /* Check if the GONE bit has changed */
7748 if ((changed & GONE) != 0) {
7749 needGlobalAttributesUpdate(false);
7750 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751
Romain Guyecd80ee2009-12-03 17:13:02 -08007752 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7753 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007754 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007755 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007756 if (mParent instanceof View) {
7757 // GONE views noop invalidation, so invalidate the parent
7758 ((View) mParent).invalidate(true);
7759 }
7760 // Mark the view drawn to ensure that it gets invalidated properly the next
7761 // time it is visible and gets invalidated
7762 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 }
7764 if (mAttachInfo != null) {
7765 mAttachInfo.mViewVisibilityChanged = true;
7766 }
7767 }
7768
7769 /* Check if the VISIBLE bit has changed */
7770 if ((changed & INVISIBLE) != 0) {
7771 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007772 /*
7773 * If this view is becoming invisible, set the DRAWN flag so that
7774 * the next invalidate() will not be skipped.
7775 */
7776 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777
7778 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007779 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007780 if (getRootView() != this) {
7781 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007782 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 }
7784 }
7785 if (mAttachInfo != null) {
7786 mAttachInfo.mViewVisibilityChanged = true;
7787 }
7788 }
7789
Adam Powell326d8082009-12-09 15:10:07 -08007790 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007791 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007792 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7793 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007794 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007795 } else if (mParent != null) {
7796 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007797 }
Adam Powell326d8082009-12-09 15:10:07 -08007798 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7799 }
7800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7802 destroyDrawingCache();
7803 }
7804
7805 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7806 destroyDrawingCache();
7807 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007808 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 }
7810
7811 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7812 destroyDrawingCache();
7813 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7814 }
7815
7816 if ((changed & DRAW_MASK) != 0) {
7817 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007818 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 mPrivateFlags &= ~SKIP_DRAW;
7820 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7821 } else {
7822 mPrivateFlags |= SKIP_DRAW;
7823 }
7824 } else {
7825 mPrivateFlags &= ~SKIP_DRAW;
7826 }
7827 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007828 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 }
7830
7831 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007832 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 mParent.recomputeViewAttributes(this);
7834 }
7835 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007836
7837 if (AccessibilityManager.getInstance(mContext).isEnabled()
7838 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7839 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7840 notifyAccessibilityStateChanged();
7841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 }
7843
7844 /**
7845 * Change the view's z order in the tree, so it's on top of other sibling
7846 * views
7847 */
7848 public void bringToFront() {
7849 if (mParent != null) {
7850 mParent.bringChildToFront(this);
7851 }
7852 }
7853
7854 /**
7855 * This is called in response to an internal scroll in this view (i.e., the
7856 * view scrolled its own contents). This is typically as a result of
7857 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7858 * called.
7859 *
7860 * @param l Current horizontal scroll origin.
7861 * @param t Current vertical scroll origin.
7862 * @param oldl Previous horizontal scroll origin.
7863 * @param oldt Previous vertical scroll origin.
7864 */
7865 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007866 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7867 postSendViewScrolledAccessibilityEventCallback();
7868 }
7869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 mBackgroundSizeChanged = true;
7871
7872 final AttachInfo ai = mAttachInfo;
7873 if (ai != null) {
7874 ai.mViewScrollChanged = true;
7875 }
7876 }
7877
7878 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007879 * Interface definition for a callback to be invoked when the layout bounds of a view
7880 * changes due to layout processing.
7881 */
7882 public interface OnLayoutChangeListener {
7883 /**
7884 * Called when the focus state of a view has changed.
7885 *
7886 * @param v The view whose state has changed.
7887 * @param left The new value of the view's left property.
7888 * @param top The new value of the view's top property.
7889 * @param right The new value of the view's right property.
7890 * @param bottom The new value of the view's bottom property.
7891 * @param oldLeft The previous value of the view's left property.
7892 * @param oldTop The previous value of the view's top property.
7893 * @param oldRight The previous value of the view's right property.
7894 * @param oldBottom The previous value of the view's bottom property.
7895 */
7896 void onLayoutChange(View v, int left, int top, int right, int bottom,
7897 int oldLeft, int oldTop, int oldRight, int oldBottom);
7898 }
7899
7900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 * This is called during layout when the size of this view has changed. If
7902 * you were just added to the view hierarchy, you're called with the old
7903 * values of 0.
7904 *
7905 * @param w Current width of this view.
7906 * @param h Current height of this view.
7907 * @param oldw Old width of this view.
7908 * @param oldh Old height of this view.
7909 */
7910 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7911 }
7912
7913 /**
7914 * Called by draw to draw the child views. This may be overridden
7915 * by derived classes to gain control just before its children are drawn
7916 * (but after its own view has been drawn).
7917 * @param canvas the canvas on which to draw the view
7918 */
7919 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 }
7922
7923 /**
7924 * Gets the parent of this view. Note that the parent is a
7925 * ViewParent and not necessarily a View.
7926 *
7927 * @return Parent of this view.
7928 */
7929 public final ViewParent getParent() {
7930 return mParent;
7931 }
7932
7933 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007934 * Set the horizontal scrolled position of your view. This will cause a call to
7935 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7936 * invalidated.
7937 * @param value the x position to scroll to
7938 */
7939 public void setScrollX(int value) {
7940 scrollTo(value, mScrollY);
7941 }
7942
7943 /**
7944 * Set the vertical scrolled position of your view. This will cause a call to
7945 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7946 * invalidated.
7947 * @param value the y position to scroll to
7948 */
7949 public void setScrollY(int value) {
7950 scrollTo(mScrollX, value);
7951 }
7952
7953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 * Return the scrolled left position of this view. This is the left edge of
7955 * the displayed part of your view. You do not need to draw any pixels
7956 * farther left, since those are outside of the frame of your view on
7957 * screen.
7958 *
7959 * @return The left edge of the displayed part of your view, in pixels.
7960 */
7961 public final int getScrollX() {
7962 return mScrollX;
7963 }
7964
7965 /**
7966 * Return the scrolled top position of this view. This is the top edge of
7967 * the displayed part of your view. You do not need to draw any pixels above
7968 * it, since those are outside of the frame of your view on screen.
7969 *
7970 * @return The top edge of the displayed part of your view, in pixels.
7971 */
7972 public final int getScrollY() {
7973 return mScrollY;
7974 }
7975
7976 /**
7977 * Return the width of the your view.
7978 *
7979 * @return The width of your view, in pixels.
7980 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007981 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 public final int getWidth() {
7983 return mRight - mLeft;
7984 }
7985
7986 /**
7987 * Return the height of your view.
7988 *
7989 * @return The height of your view, in pixels.
7990 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007991 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007992 public final int getHeight() {
7993 return mBottom - mTop;
7994 }
7995
7996 /**
7997 * Return the visible drawing bounds of your view. Fills in the output
7998 * rectangle with the values from getScrollX(), getScrollY(),
7999 * getWidth(), and getHeight().
8000 *
8001 * @param outRect The (scrolled) drawing bounds of the view.
8002 */
8003 public void getDrawingRect(Rect outRect) {
8004 outRect.left = mScrollX;
8005 outRect.top = mScrollY;
8006 outRect.right = mScrollX + (mRight - mLeft);
8007 outRect.bottom = mScrollY + (mBottom - mTop);
8008 }
8009
8010 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008011 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8012 * raw width component (that is the result is masked by
8013 * {@link #MEASURED_SIZE_MASK}).
8014 *
8015 * @return The raw measured width of this view.
8016 */
8017 public final int getMeasuredWidth() {
8018 return mMeasuredWidth & MEASURED_SIZE_MASK;
8019 }
8020
8021 /**
8022 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008023 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008024 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 * This should be used during measurement and layout calculations only. Use
8026 * {@link #getWidth()} to see how wide a view is after layout.
8027 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008028 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008030 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 return mMeasuredWidth;
8032 }
8033
8034 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008035 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8036 * raw width component (that is the result is masked by
8037 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008039 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 */
8041 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008042 return mMeasuredHeight & MEASURED_SIZE_MASK;
8043 }
8044
8045 /**
8046 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008047 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008048 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8049 * This should be used during measurement and layout calculations only. Use
8050 * {@link #getHeight()} to see how wide a view is after layout.
8051 *
8052 * @return The measured width of this view as a bit mask.
8053 */
8054 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 return mMeasuredHeight;
8056 }
8057
8058 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008059 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8060 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8061 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8062 * and the height component is at the shifted bits
8063 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8064 */
8065 public final int getMeasuredState() {
8066 return (mMeasuredWidth&MEASURED_STATE_MASK)
8067 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8068 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8069 }
8070
8071 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008072 * The transform matrix of this view, which is calculated based on the current
8073 * roation, scale, and pivot properties.
8074 *
8075 * @see #getRotation()
8076 * @see #getScaleX()
8077 * @see #getScaleY()
8078 * @see #getPivotX()
8079 * @see #getPivotY()
8080 * @return The current transform matrix for the view
8081 */
8082 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008083 if (mTransformationInfo != null) {
8084 updateMatrix();
8085 return mTransformationInfo.mMatrix;
8086 }
8087 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008088 }
8089
8090 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008091 * Utility function to determine if the value is far enough away from zero to be
8092 * considered non-zero.
8093 * @param value A floating point value to check for zero-ness
8094 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8095 */
8096 private static boolean nonzero(float value) {
8097 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8098 }
8099
8100 /**
Jeff Brown86671742010-09-30 20:00:15 -07008101 * Returns true if the transform matrix is the identity matrix.
8102 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008103 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008104 * @return True if the transform matrix is the identity matrix, false otherwise.
8105 */
Jeff Brown86671742010-09-30 20:00:15 -07008106 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008107 if (mTransformationInfo != null) {
8108 updateMatrix();
8109 return mTransformationInfo.mMatrixIsIdentity;
8110 }
8111 return true;
8112 }
8113
8114 void ensureTransformationInfo() {
8115 if (mTransformationInfo == null) {
8116 mTransformationInfo = new TransformationInfo();
8117 }
Jeff Brown86671742010-09-30 20:00:15 -07008118 }
8119
8120 /**
8121 * Recomputes the transform matrix if necessary.
8122 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008123 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008124 final TransformationInfo info = mTransformationInfo;
8125 if (info == null) {
8126 return;
8127 }
8128 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008129 // transform-related properties have changed since the last time someone
8130 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008131
8132 // Figure out if we need to update the pivot point
8133 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008134 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8135 info.mPrevWidth = mRight - mLeft;
8136 info.mPrevHeight = mBottom - mTop;
8137 info.mPivotX = info.mPrevWidth / 2f;
8138 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008139 }
8140 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008141 info.mMatrix.reset();
8142 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8143 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8144 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8145 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008146 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008147 if (info.mCamera == null) {
8148 info.mCamera = new Camera();
8149 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008150 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008151 info.mCamera.save();
8152 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8153 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8154 info.mCamera.getMatrix(info.matrix3D);
8155 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8156 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8157 info.mPivotY + info.mTranslationY);
8158 info.mMatrix.postConcat(info.matrix3D);
8159 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008160 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008161 info.mMatrixDirty = false;
8162 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8163 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008164 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008165 }
8166
8167 /**
8168 * Utility method to retrieve the inverse of the current mMatrix property.
8169 * We cache the matrix to avoid recalculating it when transform properties
8170 * have not changed.
8171 *
8172 * @return The inverse of the current matrix of this view.
8173 */
Jeff Brown86671742010-09-30 20:00:15 -07008174 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008175 final TransformationInfo info = mTransformationInfo;
8176 if (info != null) {
8177 updateMatrix();
8178 if (info.mInverseMatrixDirty) {
8179 if (info.mInverseMatrix == null) {
8180 info.mInverseMatrix = new Matrix();
8181 }
8182 info.mMatrix.invert(info.mInverseMatrix);
8183 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008184 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008185 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008186 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008187 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008188 }
8189
8190 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008191 * Gets the distance along the Z axis from the camera to this view.
8192 *
8193 * @see #setCameraDistance(float)
8194 *
8195 * @return The distance along the Z axis.
8196 */
8197 public float getCameraDistance() {
8198 ensureTransformationInfo();
8199 final float dpi = mResources.getDisplayMetrics().densityDpi;
8200 final TransformationInfo info = mTransformationInfo;
8201 if (info.mCamera == null) {
8202 info.mCamera = new Camera();
8203 info.matrix3D = new Matrix();
8204 }
8205 return -(info.mCamera.getLocationZ() * dpi);
8206 }
8207
8208 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008209 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8210 * views are drawn) from the camera to this view. The camera's distance
8211 * affects 3D transformations, for instance rotations around the X and Y
8212 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008213 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008214 * use a camera distance that's greater than the height (X axis rotation) or
8215 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008216 *
Romain Guya5364ee2011-02-24 14:46:04 -08008217 * <p>The distance of the camera from the view plane can have an affect on the
8218 * perspective distortion of the view when it is rotated around the x or y axis.
8219 * For example, a large distance will result in a large viewing angle, and there
8220 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008221 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008222 * also result in some drawing artifacts if the rotated view ends up partially
8223 * behind the camera (which is why the recommendation is to use a distance at
8224 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008225 *
Romain Guya5364ee2011-02-24 14:46:04 -08008226 * <p>The distance is expressed in "depth pixels." The default distance depends
8227 * on the screen density. For instance, on a medium density display, the
8228 * default distance is 1280. On a high density display, the default distance
8229 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008230 *
Romain Guya5364ee2011-02-24 14:46:04 -08008231 * <p>If you want to specify a distance that leads to visually consistent
8232 * results across various densities, use the following formula:</p>
8233 * <pre>
8234 * float scale = context.getResources().getDisplayMetrics().density;
8235 * view.setCameraDistance(distance * scale);
8236 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008237 *
Romain Guya5364ee2011-02-24 14:46:04 -08008238 * <p>The density scale factor of a high density display is 1.5,
8239 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008240 *
Romain Guya5364ee2011-02-24 14:46:04 -08008241 * @param distance The distance in "depth pixels", if negative the opposite
8242 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008243 *
8244 * @see #setRotationX(float)
8245 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008246 */
8247 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008248 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008249
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008250 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008251 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008252 final TransformationInfo info = mTransformationInfo;
8253 if (info.mCamera == null) {
8254 info.mCamera = new Camera();
8255 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008256 }
8257
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008258 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8259 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008260
Chet Haase9d1992d2012-03-13 11:03:25 -07008261 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008262 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008263 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008264 }
Romain Guya5364ee2011-02-24 14:46:04 -08008265 }
8266
8267 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008268 * The degrees that the view is rotated around the pivot point.
8269 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008270 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008271 * @see #getPivotX()
8272 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008273 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008274 * @return The degrees of rotation.
8275 */
Chet Haasea5531132012-02-02 13:41:44 -08008276 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008277 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008278 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008279 }
8280
8281 /**
Chet Haase897247b2010-09-09 14:54:47 -07008282 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8283 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008284 *
8285 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008286 *
8287 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008288 * @see #getPivotX()
8289 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008290 * @see #setRotationX(float)
8291 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008292 *
8293 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008294 */
8295 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008296 ensureTransformationInfo();
8297 final TransformationInfo info = mTransformationInfo;
8298 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008299 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008300 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008301 info.mRotation = rotation;
8302 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008303 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008304 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8305 mDisplayList.setRotation(rotation);
8306 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008307 }
8308 }
8309
8310 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008311 * The degrees that the view is rotated around the vertical axis through the pivot point.
8312 *
8313 * @see #getPivotX()
8314 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008315 * @see #setRotationY(float)
8316 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008317 * @return The degrees of Y rotation.
8318 */
Chet Haasea5531132012-02-02 13:41:44 -08008319 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008320 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008321 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008322 }
8323
8324 /**
Chet Haase897247b2010-09-09 14:54:47 -07008325 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8326 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8327 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008328 *
Romain Guya5364ee2011-02-24 14:46:04 -08008329 * When rotating large views, it is recommended to adjust the camera distance
8330 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008331 *
8332 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008333 *
8334 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008335 * @see #getPivotX()
8336 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008337 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008338 * @see #setRotationX(float)
8339 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008340 *
8341 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008342 */
8343 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008344 ensureTransformationInfo();
8345 final TransformationInfo info = mTransformationInfo;
8346 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008347 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008348 info.mRotationY = rotationY;
8349 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008350 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008351 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8352 mDisplayList.setRotationY(rotationY);
8353 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008354 }
8355 }
8356
8357 /**
8358 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8359 *
8360 * @see #getPivotX()
8361 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008362 * @see #setRotationX(float)
8363 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008364 * @return The degrees of X rotation.
8365 */
Chet Haasea5531132012-02-02 13:41:44 -08008366 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008367 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008368 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008369 }
8370
8371 /**
Chet Haase897247b2010-09-09 14:54:47 -07008372 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8373 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8374 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008375 *
Romain Guya5364ee2011-02-24 14:46:04 -08008376 * When rotating large views, it is recommended to adjust the camera distance
8377 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008378 *
8379 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008380 *
8381 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008382 * @see #getPivotX()
8383 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008384 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008385 * @see #setRotationY(float)
8386 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008387 *
8388 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008389 */
8390 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008391 ensureTransformationInfo();
8392 final TransformationInfo info = mTransformationInfo;
8393 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008394 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008395 info.mRotationX = rotationX;
8396 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008397 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008398 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8399 mDisplayList.setRotationX(rotationX);
8400 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008401 }
8402 }
8403
8404 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008405 * The amount that the view is scaled in x around the pivot point, as a proportion of
8406 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8407 *
Joe Onorato93162322010-09-16 15:42:01 -04008408 * <p>By default, this is 1.0f.
8409 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008410 * @see #getPivotX()
8411 * @see #getPivotY()
8412 * @return The scaling factor.
8413 */
Chet Haasea5531132012-02-02 13:41:44 -08008414 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008415 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008416 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008417 }
8418
8419 /**
8420 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8421 * the view's unscaled width. A value of 1 means that no scaling is applied.
8422 *
8423 * @param scaleX The scaling factor.
8424 * @see #getPivotX()
8425 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008426 *
8427 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008428 */
8429 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008430 ensureTransformationInfo();
8431 final TransformationInfo info = mTransformationInfo;
8432 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008433 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008434 info.mScaleX = scaleX;
8435 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008436 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008437 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8438 mDisplayList.setScaleX(scaleX);
8439 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008440 }
8441 }
8442
8443 /**
8444 * The amount that the view is scaled in y around the pivot point, as a proportion of
8445 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8446 *
Joe Onorato93162322010-09-16 15:42:01 -04008447 * <p>By default, this is 1.0f.
8448 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008449 * @see #getPivotX()
8450 * @see #getPivotY()
8451 * @return The scaling factor.
8452 */
Chet Haasea5531132012-02-02 13:41:44 -08008453 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008454 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008455 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008456 }
8457
8458 /**
8459 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8460 * the view's unscaled width. A value of 1 means that no scaling is applied.
8461 *
8462 * @param scaleY The scaling factor.
8463 * @see #getPivotX()
8464 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008465 *
8466 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008467 */
8468 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008469 ensureTransformationInfo();
8470 final TransformationInfo info = mTransformationInfo;
8471 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008472 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008473 info.mScaleY = scaleY;
8474 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008475 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008476 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8477 mDisplayList.setScaleY(scaleY);
8478 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008479 }
8480 }
8481
8482 /**
8483 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8484 * and {@link #setScaleX(float) scaled}.
8485 *
8486 * @see #getRotation()
8487 * @see #getScaleX()
8488 * @see #getScaleY()
8489 * @see #getPivotY()
8490 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008491 *
8492 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008493 */
Chet Haasea5531132012-02-02 13:41:44 -08008494 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008495 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008496 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008497 }
8498
8499 /**
8500 * Sets the x location of the point around which the view is
8501 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008502 * By default, the pivot point is centered on the object.
8503 * Setting this property disables this behavior and causes the view to use only the
8504 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008505 *
8506 * @param pivotX The x location of the pivot point.
8507 * @see #getRotation()
8508 * @see #getScaleX()
8509 * @see #getScaleY()
8510 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008511 *
8512 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008513 */
8514 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008515 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008516 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008517 final TransformationInfo info = mTransformationInfo;
8518 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008519 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008520 info.mPivotX = pivotX;
8521 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008522 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008523 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8524 mDisplayList.setPivotX(pivotX);
8525 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008526 }
8527 }
8528
8529 /**
8530 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8531 * and {@link #setScaleY(float) scaled}.
8532 *
8533 * @see #getRotation()
8534 * @see #getScaleX()
8535 * @see #getScaleY()
8536 * @see #getPivotY()
8537 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008538 *
8539 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008540 */
Chet Haasea5531132012-02-02 13:41:44 -08008541 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008542 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008543 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008544 }
8545
8546 /**
8547 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008548 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8549 * Setting this property disables this behavior and causes the view to use only the
8550 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008551 *
8552 * @param pivotY The y location of the pivot point.
8553 * @see #getRotation()
8554 * @see #getScaleX()
8555 * @see #getScaleY()
8556 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008557 *
8558 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008559 */
8560 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008561 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008562 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008563 final TransformationInfo info = mTransformationInfo;
8564 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008565 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008566 info.mPivotY = pivotY;
8567 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008568 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008569 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8570 mDisplayList.setPivotY(pivotY);
8571 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008572 }
8573 }
8574
8575 /**
8576 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8577 * completely transparent and 1 means the view is completely opaque.
8578 *
Joe Onorato93162322010-09-16 15:42:01 -04008579 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008580 * @return The opacity of the view.
8581 */
Chet Haasea5531132012-02-02 13:41:44 -08008582 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008583 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008584 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008585 }
8586
8587 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008588 * Returns whether this View has content which overlaps. This function, intended to be
8589 * overridden by specific View types, is an optimization when alpha is set on a view. If
8590 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8591 * and then composited it into place, which can be expensive. If the view has no overlapping
8592 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8593 * An example of overlapping rendering is a TextView with a background image, such as a
8594 * Button. An example of non-overlapping rendering is a TextView with no background, or
8595 * an ImageView with only the foreground image. The default implementation returns true;
8596 * subclasses should override if they have cases which can be optimized.
8597 *
8598 * @return true if the content in this view might overlap, false otherwise.
8599 */
8600 public boolean hasOverlappingRendering() {
8601 return true;
8602 }
8603
8604 /**
Romain Guy171c5922011-01-06 10:04:23 -08008605 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8606 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008607 *
Romain Guy171c5922011-01-06 10:04:23 -08008608 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8609 * responsible for applying the opacity itself. Otherwise, calling this method is
8610 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008611 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008612 *
Chet Haasea5531132012-02-02 13:41:44 -08008613 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8614 * performance implications. It is generally best to use the alpha property sparingly and
8615 * transiently, as in the case of fading animations.</p>
8616 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008617 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008618 *
Joe Malin32736f02011-01-19 16:14:20 -08008619 * @see #setLayerType(int, android.graphics.Paint)
8620 *
Chet Haase73066682010-11-29 15:55:32 -08008621 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008622 */
8623 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008624 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008625 if (mTransformationInfo.mAlpha != alpha) {
8626 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008627 if (onSetAlpha((int) (alpha * 255))) {
8628 mPrivateFlags |= ALPHA_SET;
8629 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008630 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008631 invalidate(true);
8632 } else {
8633 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008634 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008635 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8636 mDisplayList.setAlpha(alpha);
8637 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008638 }
Chet Haaseed032702010-10-01 14:05:54 -07008639 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008640 }
8641
8642 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008643 * Faster version of setAlpha() which performs the same steps except there are
8644 * no calls to invalidate(). The caller of this function should perform proper invalidation
8645 * on the parent and this object. The return value indicates whether the subclass handles
8646 * alpha (the return value for onSetAlpha()).
8647 *
8648 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008649 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8650 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008651 */
8652 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008653 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008654 if (mTransformationInfo.mAlpha != alpha) {
8655 mTransformationInfo.mAlpha = alpha;
8656 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8657 if (subclassHandlesAlpha) {
8658 mPrivateFlags |= ALPHA_SET;
8659 return true;
8660 } else {
8661 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008662 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8663 mDisplayList.setAlpha(alpha);
8664 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008665 }
Chet Haasea00f3862011-02-22 06:34:40 -08008666 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008667 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008668 }
8669
8670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 * Top position of this view relative to its parent.
8672 *
8673 * @return The top of this view, in pixels.
8674 */
8675 @ViewDebug.CapturedViewProperty
8676 public final int getTop() {
8677 return mTop;
8678 }
8679
8680 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008681 * Sets the top position of this view relative to its parent. This method is meant to be called
8682 * by the layout system and should not generally be called otherwise, because the property
8683 * may be changed at any time by the layout.
8684 *
8685 * @param top The top of this view, in pixels.
8686 */
8687 public final void setTop(int top) {
8688 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008689 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008690 final boolean matrixIsIdentity = mTransformationInfo == null
8691 || mTransformationInfo.mMatrixIsIdentity;
8692 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008693 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008694 int minTop;
8695 int yLoc;
8696 if (top < mTop) {
8697 minTop = top;
8698 yLoc = top - mTop;
8699 } else {
8700 minTop = mTop;
8701 yLoc = 0;
8702 }
Chet Haasee9140a72011-02-16 16:23:29 -08008703 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008704 }
8705 } else {
8706 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008707 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008708 }
8709
Chet Haaseed032702010-10-01 14:05:54 -07008710 int width = mRight - mLeft;
8711 int oldHeight = mBottom - mTop;
8712
Chet Haase21cd1382010-09-01 17:42:29 -07008713 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008714 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8715 mDisplayList.setTop(mTop);
8716 }
Chet Haase21cd1382010-09-01 17:42:29 -07008717
Chet Haaseed032702010-10-01 14:05:54 -07008718 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8719
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008720 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008721 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8722 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008723 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008724 }
Chet Haase21cd1382010-09-01 17:42:29 -07008725 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008726 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008727 }
Chet Haase55dbb652010-12-21 20:15:08 -08008728 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008729 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008730 }
8731 }
8732
8733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008734 * Bottom position of this view relative to its parent.
8735 *
8736 * @return The bottom of this view, in pixels.
8737 */
8738 @ViewDebug.CapturedViewProperty
8739 public final int getBottom() {
8740 return mBottom;
8741 }
8742
8743 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008744 * True if this view has changed since the last time being drawn.
8745 *
8746 * @return The dirty state of this view.
8747 */
8748 public boolean isDirty() {
8749 return (mPrivateFlags & DIRTY_MASK) != 0;
8750 }
8751
8752 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008753 * Sets the bottom position of this view relative to its parent. This method is meant to be
8754 * called by the layout system and should not generally be called otherwise, because the
8755 * property may be changed at any time by the layout.
8756 *
8757 * @param bottom The bottom of this view, in pixels.
8758 */
8759 public final void setBottom(int bottom) {
8760 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008761 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008762 final boolean matrixIsIdentity = mTransformationInfo == null
8763 || mTransformationInfo.mMatrixIsIdentity;
8764 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008765 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008766 int maxBottom;
8767 if (bottom < mBottom) {
8768 maxBottom = mBottom;
8769 } else {
8770 maxBottom = bottom;
8771 }
Chet Haasee9140a72011-02-16 16:23:29 -08008772 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008773 }
8774 } else {
8775 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008776 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008777 }
8778
Chet Haaseed032702010-10-01 14:05:54 -07008779 int width = mRight - mLeft;
8780 int oldHeight = mBottom - mTop;
8781
Chet Haase21cd1382010-09-01 17:42:29 -07008782 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008783 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8784 mDisplayList.setBottom(mBottom);
8785 }
Chet Haase21cd1382010-09-01 17:42:29 -07008786
Chet Haaseed032702010-10-01 14:05:54 -07008787 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8788
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008789 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008790 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8791 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008792 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008793 }
Chet Haase21cd1382010-09-01 17:42:29 -07008794 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008795 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008796 }
Chet Haase55dbb652010-12-21 20:15:08 -08008797 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008798 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008799 }
8800 }
8801
8802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 * Left position of this view relative to its parent.
8804 *
8805 * @return The left edge of this view, in pixels.
8806 */
8807 @ViewDebug.CapturedViewProperty
8808 public final int getLeft() {
8809 return mLeft;
8810 }
8811
8812 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008813 * Sets the left position of this view relative to its parent. This method is meant to be called
8814 * by the layout system and should not generally be called otherwise, because the property
8815 * may be changed at any time by the layout.
8816 *
8817 * @param left The bottom of this view, in pixels.
8818 */
8819 public final void setLeft(int left) {
8820 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008821 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008822 final boolean matrixIsIdentity = mTransformationInfo == null
8823 || mTransformationInfo.mMatrixIsIdentity;
8824 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008825 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008826 int minLeft;
8827 int xLoc;
8828 if (left < mLeft) {
8829 minLeft = left;
8830 xLoc = left - mLeft;
8831 } else {
8832 minLeft = mLeft;
8833 xLoc = 0;
8834 }
Chet Haasee9140a72011-02-16 16:23:29 -08008835 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008836 }
8837 } else {
8838 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008839 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008840 }
8841
Chet Haaseed032702010-10-01 14:05:54 -07008842 int oldWidth = mRight - mLeft;
8843 int height = mBottom - mTop;
8844
Chet Haase21cd1382010-09-01 17:42:29 -07008845 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008846 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8847 mDisplayList.setLeft(left);
8848 }
Chet Haase21cd1382010-09-01 17:42:29 -07008849
Chet Haaseed032702010-10-01 14:05:54 -07008850 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8851
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008852 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008853 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8854 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008855 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008856 }
Chet Haase21cd1382010-09-01 17:42:29 -07008857 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008858 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008859 }
Chet Haase55dbb652010-12-21 20:15:08 -08008860 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008861 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008862 if (USE_DISPLAY_LIST_PROPERTIES) {
8863
8864 }
Chet Haase21cd1382010-09-01 17:42:29 -07008865 }
8866 }
8867
8868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869 * Right position of this view relative to its parent.
8870 *
8871 * @return The right edge of this view, in pixels.
8872 */
8873 @ViewDebug.CapturedViewProperty
8874 public final int getRight() {
8875 return mRight;
8876 }
8877
8878 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008879 * Sets the right position of this view relative to its parent. This method is meant to be called
8880 * by the layout system and should not generally be called otherwise, because the property
8881 * may be changed at any time by the layout.
8882 *
8883 * @param right The bottom of this view, in pixels.
8884 */
8885 public final void setRight(int right) {
8886 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008887 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008888 final boolean matrixIsIdentity = mTransformationInfo == null
8889 || mTransformationInfo.mMatrixIsIdentity;
8890 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008891 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008892 int maxRight;
8893 if (right < mRight) {
8894 maxRight = mRight;
8895 } else {
8896 maxRight = right;
8897 }
Chet Haasee9140a72011-02-16 16:23:29 -08008898 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008899 }
8900 } else {
8901 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008902 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008903 }
8904
Chet Haaseed032702010-10-01 14:05:54 -07008905 int oldWidth = mRight - mLeft;
8906 int height = mBottom - mTop;
8907
Chet Haase21cd1382010-09-01 17:42:29 -07008908 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008909 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8910 mDisplayList.setRight(mRight);
8911 }
Chet Haase21cd1382010-09-01 17:42:29 -07008912
Chet Haaseed032702010-10-01 14:05:54 -07008913 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8914
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008915 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008916 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8917 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008918 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008919 }
Chet Haase21cd1382010-09-01 17:42:29 -07008920 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008921 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008922 }
Chet Haase55dbb652010-12-21 20:15:08 -08008923 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008924 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008925 }
8926 }
8927
8928 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008929 * The visual x position of this view, in pixels. This is equivalent to the
8930 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008931 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008932 *
Chet Haasedf030d22010-07-30 17:22:38 -07008933 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008934 */
Chet Haasea5531132012-02-02 13:41:44 -08008935 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008936 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008937 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008938 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008939
Chet Haasedf030d22010-07-30 17:22:38 -07008940 /**
8941 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8942 * {@link #setTranslationX(float) translationX} property to be the difference between
8943 * the x value passed in and the current {@link #getLeft() left} property.
8944 *
8945 * @param x The visual x position of this view, in pixels.
8946 */
8947 public void setX(float x) {
8948 setTranslationX(x - mLeft);
8949 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008950
Chet Haasedf030d22010-07-30 17:22:38 -07008951 /**
8952 * The visual y position of this view, in pixels. This is equivalent to the
8953 * {@link #setTranslationY(float) translationY} property plus the current
8954 * {@link #getTop() top} property.
8955 *
8956 * @return The visual y position of this view, in pixels.
8957 */
Chet Haasea5531132012-02-02 13:41:44 -08008958 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008959 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008960 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008961 }
8962
8963 /**
8964 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8965 * {@link #setTranslationY(float) translationY} property to be the difference between
8966 * the y value passed in and the current {@link #getTop() top} property.
8967 *
8968 * @param y The visual y position of this view, in pixels.
8969 */
8970 public void setY(float y) {
8971 setTranslationY(y - mTop);
8972 }
8973
8974
8975 /**
8976 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8977 * This position is post-layout, in addition to wherever the object's
8978 * layout placed it.
8979 *
8980 * @return The horizontal position of this view relative to its left position, in pixels.
8981 */
Chet Haasea5531132012-02-02 13:41:44 -08008982 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008983 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008984 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008985 }
8986
8987 /**
8988 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8989 * This effectively positions the object post-layout, in addition to wherever the object's
8990 * layout placed it.
8991 *
8992 * @param translationX The horizontal position of this view relative to its left position,
8993 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008994 *
8995 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008996 */
8997 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008998 ensureTransformationInfo();
8999 final TransformationInfo info = mTransformationInfo;
9000 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009001 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009002 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009003 info.mTranslationX = translationX;
9004 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009005 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009006 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9007 mDisplayList.setTranslationX(translationX);
9008 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009009 }
9010 }
9011
9012 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009013 * The horizontal location of this view relative to its {@link #getTop() top} position.
9014 * This position is post-layout, in addition to wherever the object's
9015 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009016 *
Chet Haasedf030d22010-07-30 17:22:38 -07009017 * @return The vertical position of this view relative to its top position,
9018 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009019 */
Chet Haasea5531132012-02-02 13:41:44 -08009020 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009021 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009022 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009023 }
9024
9025 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009026 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9027 * This effectively positions the object post-layout, in addition to wherever the object's
9028 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009029 *
Chet Haasedf030d22010-07-30 17:22:38 -07009030 * @param translationY The vertical position of this view relative to its top position,
9031 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009032 *
9033 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009034 */
Chet Haasedf030d22010-07-30 17:22:38 -07009035 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009036 ensureTransformationInfo();
9037 final TransformationInfo info = mTransformationInfo;
9038 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009039 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009040 info.mTranslationY = translationY;
9041 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009042 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009043 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9044 mDisplayList.setTranslationY(translationY);
9045 }
Chet Haasedf030d22010-07-30 17:22:38 -07009046 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009047 }
9048
9049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 * Hit rectangle in parent's coordinates
9051 *
9052 * @param outRect The hit rectangle of the view.
9053 */
9054 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009055 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009056 final TransformationInfo info = mTransformationInfo;
9057 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009058 outRect.set(mLeft, mTop, mRight, mBottom);
9059 } else {
9060 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009061 tmpRect.set(-info.mPivotX, -info.mPivotY,
9062 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9063 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009064 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9065 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009066 }
9067 }
9068
9069 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009070 * Determines whether the given point, in local coordinates is inside the view.
9071 */
9072 /*package*/ final boolean pointInView(float localX, float localY) {
9073 return localX >= 0 && localX < (mRight - mLeft)
9074 && localY >= 0 && localY < (mBottom - mTop);
9075 }
9076
9077 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009078 * Utility method to determine whether the given point, in local coordinates,
9079 * is inside the view, where the area of the view is expanded by the slop factor.
9080 * This method is called while processing touch-move events to determine if the event
9081 * is still within the view.
9082 */
9083 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009084 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009085 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 }
9087
9088 /**
9089 * When a view has focus and the user navigates away from it, the next view is searched for
9090 * starting from the rectangle filled in by this method.
9091 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009092 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9093 * of the view. However, if your view maintains some idea of internal selection,
9094 * such as a cursor, or a selected row or column, you should override this method and
9095 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096 *
9097 * @param r The rectangle to fill in, in this view's coordinates.
9098 */
9099 public void getFocusedRect(Rect r) {
9100 getDrawingRect(r);
9101 }
9102
9103 /**
9104 * If some part of this view is not clipped by any of its parents, then
9105 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009106 * coordinates (without taking possible View rotations into account), offset
9107 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9108 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 *
9110 * @param r If true is returned, r holds the global coordinates of the
9111 * visible portion of this view.
9112 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9113 * between this view and its root. globalOffet may be null.
9114 * @return true if r is non-empty (i.e. part of the view is visible at the
9115 * root level.
9116 */
9117 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9118 int width = mRight - mLeft;
9119 int height = mBottom - mTop;
9120 if (width > 0 && height > 0) {
9121 r.set(0, 0, width, height);
9122 if (globalOffset != null) {
9123 globalOffset.set(-mScrollX, -mScrollY);
9124 }
9125 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9126 }
9127 return false;
9128 }
9129
9130 public final boolean getGlobalVisibleRect(Rect r) {
9131 return getGlobalVisibleRect(r, null);
9132 }
9133
9134 public final boolean getLocalVisibleRect(Rect r) {
9135 Point offset = new Point();
9136 if (getGlobalVisibleRect(r, offset)) {
9137 r.offset(-offset.x, -offset.y); // make r local
9138 return true;
9139 }
9140 return false;
9141 }
9142
9143 /**
9144 * Offset this view's vertical location by the specified number of pixels.
9145 *
9146 * @param offset the number of pixels to offset the view by
9147 */
9148 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009149 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009150 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009151 final boolean matrixIsIdentity = mTransformationInfo == null
9152 || mTransformationInfo.mMatrixIsIdentity;
9153 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009154 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9155 invalidateViewProperty(false, false);
9156 } else {
9157 final ViewParent p = mParent;
9158 if (p != null && mAttachInfo != null) {
9159 final Rect r = mAttachInfo.mTmpInvalRect;
9160 int minTop;
9161 int maxBottom;
9162 int yLoc;
9163 if (offset < 0) {
9164 minTop = mTop + offset;
9165 maxBottom = mBottom;
9166 yLoc = offset;
9167 } else {
9168 minTop = mTop;
9169 maxBottom = mBottom + offset;
9170 yLoc = 0;
9171 }
9172 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9173 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009174 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009175 }
9176 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009177 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009178 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009179
Chet Haasec3aa3612010-06-17 08:50:37 -07009180 mTop += offset;
9181 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009182 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9183 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009184 invalidateViewProperty(false, false);
9185 } else {
9186 if (!matrixIsIdentity) {
9187 invalidateViewProperty(false, true);
9188 }
9189 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009190 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009192 }
9193
9194 /**
9195 * Offset this view's horizontal location by the specified amount of pixels.
9196 *
9197 * @param offset the numer of pixels to offset the view by
9198 */
9199 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009200 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009201 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009202 final boolean matrixIsIdentity = mTransformationInfo == null
9203 || mTransformationInfo.mMatrixIsIdentity;
9204 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009205 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9206 invalidateViewProperty(false, false);
9207 } else {
9208 final ViewParent p = mParent;
9209 if (p != null && mAttachInfo != null) {
9210 final Rect r = mAttachInfo.mTmpInvalRect;
9211 int minLeft;
9212 int maxRight;
9213 if (offset < 0) {
9214 minLeft = mLeft + offset;
9215 maxRight = mRight;
9216 } else {
9217 minLeft = mLeft;
9218 maxRight = mRight + offset;
9219 }
9220 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9221 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009222 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009223 }
9224 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009225 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009226 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009227
Chet Haasec3aa3612010-06-17 08:50:37 -07009228 mLeft += offset;
9229 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009230 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9231 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009232 invalidateViewProperty(false, false);
9233 } else {
9234 if (!matrixIsIdentity) {
9235 invalidateViewProperty(false, true);
9236 }
9237 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009238 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 }
9241
9242 /**
9243 * Get the LayoutParams associated with this view. All views should have
9244 * layout parameters. These supply parameters to the <i>parent</i> of this
9245 * view specifying how it should be arranged. There are many subclasses of
9246 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9247 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009248 *
9249 * This method may return null if this View is not attached to a parent
9250 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9251 * was not invoked successfully. When a View is attached to a parent
9252 * ViewGroup, this method must not return null.
9253 *
9254 * @return The LayoutParams associated with this view, or null if no
9255 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009256 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009257 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 public ViewGroup.LayoutParams getLayoutParams() {
9259 return mLayoutParams;
9260 }
9261
9262 /**
9263 * Set the layout parameters associated with this view. These supply
9264 * parameters to the <i>parent</i> of this view specifying how it should be
9265 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9266 * correspond to the different subclasses of ViewGroup that are responsible
9267 * for arranging their children.
9268 *
Romain Guy01c174b2011-02-22 11:51:06 -08009269 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 */
9271 public void setLayoutParams(ViewGroup.LayoutParams params) {
9272 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009273 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274 }
9275 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009276 if (mParent instanceof ViewGroup) {
9277 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 requestLayout();
9280 }
9281
9282 /**
9283 * Set the scrolled position of your view. This will cause a call to
9284 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9285 * invalidated.
9286 * @param x the x position to scroll to
9287 * @param y the y position to scroll to
9288 */
9289 public void scrollTo(int x, int y) {
9290 if (mScrollX != x || mScrollY != y) {
9291 int oldX = mScrollX;
9292 int oldY = mScrollY;
9293 mScrollX = x;
9294 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009295 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009297 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009298 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 }
9301 }
9302
9303 /**
9304 * Move the scrolled position of your view. This will cause a call to
9305 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9306 * invalidated.
9307 * @param x the amount of pixels to scroll by horizontally
9308 * @param y the amount of pixels to scroll by vertically
9309 */
9310 public void scrollBy(int x, int y) {
9311 scrollTo(mScrollX + x, mScrollY + y);
9312 }
9313
9314 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009315 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9316 * animation to fade the scrollbars out after a default delay. If a subclass
9317 * provides animated scrolling, the start delay should equal the duration
9318 * of the scrolling animation.</p>
9319 *
9320 * <p>The animation starts only if at least one of the scrollbars is
9321 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9322 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9323 * this method returns true, and false otherwise. If the animation is
9324 * started, this method calls {@link #invalidate()}; in that case the
9325 * caller should not call {@link #invalidate()}.</p>
9326 *
9327 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009328 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009329 *
9330 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9331 * and {@link #scrollTo(int, int)}.</p>
9332 *
9333 * @return true if the animation is played, false otherwise
9334 *
9335 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009336 * @see #scrollBy(int, int)
9337 * @see #scrollTo(int, int)
9338 * @see #isHorizontalScrollBarEnabled()
9339 * @see #isVerticalScrollBarEnabled()
9340 * @see #setHorizontalScrollBarEnabled(boolean)
9341 * @see #setVerticalScrollBarEnabled(boolean)
9342 */
9343 protected boolean awakenScrollBars() {
9344 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009345 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009346 }
9347
9348 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009349 * Trigger the scrollbars to draw.
9350 * This method differs from awakenScrollBars() only in its default duration.
9351 * initialAwakenScrollBars() will show the scroll bars for longer than
9352 * usual to give the user more of a chance to notice them.
9353 *
9354 * @return true if the animation is played, false otherwise.
9355 */
9356 private boolean initialAwakenScrollBars() {
9357 return mScrollCache != null &&
9358 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9359 }
9360
9361 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009362 * <p>
9363 * Trigger the scrollbars to draw. When invoked this method starts an
9364 * animation to fade the scrollbars out after a fixed delay. If a subclass
9365 * provides animated scrolling, the start delay should equal the duration of
9366 * the scrolling animation.
9367 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009368 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009369 * <p>
9370 * The animation starts only if at least one of the scrollbars is enabled,
9371 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9372 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9373 * this method returns true, and false otherwise. If the animation is
9374 * started, this method calls {@link #invalidate()}; in that case the caller
9375 * should not call {@link #invalidate()}.
9376 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009377 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009378 * <p>
9379 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009380 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009381 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009382 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009383 * @param startDelay the delay, in milliseconds, after which the animation
9384 * should start; when the delay is 0, the animation starts
9385 * immediately
9386 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009387 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009388 * @see #scrollBy(int, int)
9389 * @see #scrollTo(int, int)
9390 * @see #isHorizontalScrollBarEnabled()
9391 * @see #isVerticalScrollBarEnabled()
9392 * @see #setHorizontalScrollBarEnabled(boolean)
9393 * @see #setVerticalScrollBarEnabled(boolean)
9394 */
9395 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009396 return awakenScrollBars(startDelay, true);
9397 }
Joe Malin32736f02011-01-19 16:14:20 -08009398
Mike Cleron290947b2009-09-29 18:34:32 -07009399 /**
9400 * <p>
9401 * Trigger the scrollbars to draw. When invoked this method starts an
9402 * animation to fade the scrollbars out after a fixed delay. If a subclass
9403 * provides animated scrolling, the start delay should equal the duration of
9404 * the scrolling animation.
9405 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009406 *
Mike Cleron290947b2009-09-29 18:34:32 -07009407 * <p>
9408 * The animation starts only if at least one of the scrollbars is enabled,
9409 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9410 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9411 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009412 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009413 * is set to true; in that case the caller
9414 * should not call {@link #invalidate()}.
9415 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009416 *
Mike Cleron290947b2009-09-29 18:34:32 -07009417 * <p>
9418 * This method should be invoked everytime a subclass directly updates the
9419 * scroll parameters.
9420 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009421 *
Mike Cleron290947b2009-09-29 18:34:32 -07009422 * @param startDelay the delay, in milliseconds, after which the animation
9423 * should start; when the delay is 0, the animation starts
9424 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009425 *
Mike Cleron290947b2009-09-29 18:34:32 -07009426 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009427 *
Mike Cleron290947b2009-09-29 18:34:32 -07009428 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009429 *
Mike Cleron290947b2009-09-29 18:34:32 -07009430 * @see #scrollBy(int, int)
9431 * @see #scrollTo(int, int)
9432 * @see #isHorizontalScrollBarEnabled()
9433 * @see #isVerticalScrollBarEnabled()
9434 * @see #setHorizontalScrollBarEnabled(boolean)
9435 * @see #setVerticalScrollBarEnabled(boolean)
9436 */
9437 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009438 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009439
Mike Cleronf116bf82009-09-27 19:14:12 -07009440 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9441 return false;
9442 }
9443
9444 if (scrollCache.scrollBar == null) {
9445 scrollCache.scrollBar = new ScrollBarDrawable();
9446 }
9447
9448 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9449
Mike Cleron290947b2009-09-29 18:34:32 -07009450 if (invalidate) {
9451 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009452 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009453 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009454
9455 if (scrollCache.state == ScrollabilityCache.OFF) {
9456 // FIXME: this is copied from WindowManagerService.
9457 // We should get this value from the system when it
9458 // is possible to do so.
9459 final int KEY_REPEAT_FIRST_DELAY = 750;
9460 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9461 }
9462
9463 // Tell mScrollCache when we should start fading. This may
9464 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009465 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009466 scrollCache.fadeStartTime = fadeStartTime;
9467 scrollCache.state = ScrollabilityCache.ON;
9468
9469 // Schedule our fader to run, unscheduling any old ones first
9470 if (mAttachInfo != null) {
9471 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9472 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9473 }
9474
9475 return true;
9476 }
9477
9478 return false;
9479 }
9480
9481 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009482 * Do not invalidate views which are not visible and which are not running an animation. They
9483 * will not get drawn and they should not set dirty flags as if they will be drawn
9484 */
9485 private boolean skipInvalidate() {
9486 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9487 (!(mParent instanceof ViewGroup) ||
9488 !((ViewGroup) mParent).isViewTransitioning(this));
9489 }
9490 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009491 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009492 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9493 * in the future. This must be called from a UI thread. To call from a non-UI
9494 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009495 *
9496 * WARNING: This method is destructive to dirty.
9497 * @param dirty the rectangle representing the bounds of the dirty region
9498 */
9499 public void invalidate(Rect dirty) {
9500 if (ViewDebug.TRACE_HIERARCHY) {
9501 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9502 }
9503
Chet Haaseaceafe62011-08-26 15:44:33 -07009504 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009505 return;
9506 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009507 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009508 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9509 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009511 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009512 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 final ViewParent p = mParent;
9514 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009515 //noinspection PointlessBooleanExpression,ConstantConditions
9516 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9517 if (p != null && ai != null && ai.mHardwareAccelerated) {
9518 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009519 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009520 p.invalidateChild(this, null);
9521 return;
9522 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 if (p != null && ai != null) {
9525 final int scrollX = mScrollX;
9526 final int scrollY = mScrollY;
9527 final Rect r = ai.mTmpInvalRect;
9528 r.set(dirty.left - scrollX, dirty.top - scrollY,
9529 dirty.right - scrollX, dirty.bottom - scrollY);
9530 mParent.invalidateChild(this, r);
9531 }
9532 }
9533 }
9534
9535 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009536 * 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 -08009537 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009538 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9539 * will be called at some point in the future. This must be called from
9540 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 * @param l the left position of the dirty region
9542 * @param t the top position of the dirty region
9543 * @param r the right position of the dirty region
9544 * @param b the bottom position of the dirty region
9545 */
9546 public void invalidate(int l, int t, int r, int b) {
9547 if (ViewDebug.TRACE_HIERARCHY) {
9548 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9549 }
9550
Chet Haaseaceafe62011-08-26 15:44:33 -07009551 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009552 return;
9553 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009554 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009555 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9556 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009558 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009559 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 final ViewParent p = mParent;
9561 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009562 //noinspection PointlessBooleanExpression,ConstantConditions
9563 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9564 if (p != null && ai != null && ai.mHardwareAccelerated) {
9565 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009566 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009567 p.invalidateChild(this, null);
9568 return;
9569 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009571 if (p != null && ai != null && l < r && t < b) {
9572 final int scrollX = mScrollX;
9573 final int scrollY = mScrollY;
9574 final Rect tmpr = ai.mTmpInvalRect;
9575 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9576 p.invalidateChild(this, tmpr);
9577 }
9578 }
9579 }
9580
9581 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009582 * Invalidate the whole view. If the view is visible,
9583 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9584 * the future. This must be called from a UI thread. To call from a non-UI thread,
9585 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 */
9587 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009588 invalidate(true);
9589 }
Joe Malin32736f02011-01-19 16:14:20 -08009590
Chet Haaseed032702010-10-01 14:05:54 -07009591 /**
9592 * This is where the invalidate() work actually happens. A full invalidate()
9593 * causes the drawing cache to be invalidated, but this function can be called with
9594 * invalidateCache set to false to skip that invalidation step for cases that do not
9595 * need it (for example, a component that remains at the same dimensions with the same
9596 * content).
9597 *
9598 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9599 * well. This is usually true for a full invalidate, but may be set to false if the
9600 * View's contents or dimensions have not changed.
9601 */
Romain Guy849d0a32011-02-01 17:20:48 -08009602 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 if (ViewDebug.TRACE_HIERARCHY) {
9604 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9605 }
9606
Chet Haaseaceafe62011-08-26 15:44:33 -07009607 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009608 return;
9609 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009610 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009611 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009612 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9613 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009614 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009615 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009616 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009617 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009618 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009621 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009622 //noinspection PointlessBooleanExpression,ConstantConditions
9623 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9624 if (p != null && ai != null && ai.mHardwareAccelerated) {
9625 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009626 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009627 p.invalidateChild(this, null);
9628 return;
9629 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009630 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 if (p != null && ai != null) {
9633 final Rect r = ai.mTmpInvalRect;
9634 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9635 // Don't call invalidate -- we don't want to internally scroll
9636 // our own bounds
9637 p.invalidateChild(this, r);
9638 }
9639 }
9640 }
9641
9642 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009643 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9644 * set any flags or handle all of the cases handled by the default invalidation methods.
9645 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9646 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9647 * walk up the hierarchy, transforming the dirty rect as necessary.
9648 *
9649 * The method also handles normal invalidation logic if display list properties are not
9650 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9651 * backup approach, to handle these cases used in the various property-setting methods.
9652 *
9653 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9654 * are not being used in this view
9655 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9656 * list properties are not being used in this view
9657 */
9658 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9659 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9660 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9661 if (invalidateParent) {
9662 invalidateParentCaches();
9663 }
9664 if (forceRedraw) {
9665 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9666 }
9667 invalidate(false);
9668 } else {
9669 final AttachInfo ai = mAttachInfo;
9670 final ViewParent p = mParent;
9671 if (p != null && ai != null) {
9672 final Rect r = ai.mTmpInvalRect;
9673 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9674 if (mParent instanceof ViewGroup) {
9675 ((ViewGroup) mParent).invalidateChildFast(this, r);
9676 } else {
9677 mParent.invalidateChild(this, r);
9678 }
9679 }
9680 }
9681 }
9682
9683 /**
9684 * Utility method to transform a given Rect by the current matrix of this view.
9685 */
9686 void transformRect(final Rect rect) {
9687 if (!getMatrix().isIdentity()) {
9688 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9689 boundingRect.set(rect);
9690 getMatrix().mapRect(boundingRect);
9691 rect.set((int) (boundingRect.left - 0.5f),
9692 (int) (boundingRect.top - 0.5f),
9693 (int) (boundingRect.right + 0.5f),
9694 (int) (boundingRect.bottom + 0.5f));
9695 }
9696 }
9697
9698 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009699 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009700 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9701 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009702 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9703 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009704 *
9705 * @hide
9706 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009707 protected void invalidateParentCaches() {
9708 if (mParent instanceof View) {
9709 ((View) mParent).mPrivateFlags |= INVALIDATED;
9710 }
9711 }
Joe Malin32736f02011-01-19 16:14:20 -08009712
Romain Guy0fd89bf2011-01-26 15:41:30 -08009713 /**
9714 * Used to indicate that the parent of this view should be invalidated. This functionality
9715 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9716 * which is necessary when various parent-managed properties of the view change, such as
9717 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9718 * an invalidation event to the parent.
9719 *
9720 * @hide
9721 */
9722 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009723 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009724 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009725 }
9726 }
9727
9728 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009729 * Indicates whether this View is opaque. An opaque View guarantees that it will
9730 * draw all the pixels overlapping its bounds using a fully opaque color.
9731 *
9732 * Subclasses of View should override this method whenever possible to indicate
9733 * whether an instance is opaque. Opaque Views are treated in a special way by
9734 * the View hierarchy, possibly allowing it to perform optimizations during
9735 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009736 *
Romain Guy24443ea2009-05-11 11:56:30 -07009737 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009738 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009739 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009740 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009741 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009742 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9743 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009744 }
9745
Adam Powell20232d02010-12-08 21:08:53 -08009746 /**
9747 * @hide
9748 */
9749 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009750 // Opaque if:
9751 // - Has a background
9752 // - Background is opaque
9753 // - Doesn't have scrollbars or scrollbars are inside overlay
9754
Philip Milne6c8ea062012-04-03 17:38:43 -07009755 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009756 mPrivateFlags |= OPAQUE_BACKGROUND;
9757 } else {
9758 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9759 }
9760
9761 final int flags = mViewFlags;
9762 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9763 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9764 mPrivateFlags |= OPAQUE_SCROLLBARS;
9765 } else {
9766 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9767 }
9768 }
9769
9770 /**
9771 * @hide
9772 */
9773 protected boolean hasOpaqueScrollbars() {
9774 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009775 }
9776
9777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 * @return A handler associated with the thread running the View. This
9779 * handler can be used to pump events in the UI events queue.
9780 */
9781 public Handler getHandler() {
9782 if (mAttachInfo != null) {
9783 return mAttachInfo.mHandler;
9784 }
9785 return null;
9786 }
9787
9788 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009789 * Gets the view root associated with the View.
9790 * @return The view root, or null if none.
9791 * @hide
9792 */
9793 public ViewRootImpl getViewRootImpl() {
9794 if (mAttachInfo != null) {
9795 return mAttachInfo.mViewRootImpl;
9796 }
9797 return null;
9798 }
9799
9800 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009801 * <p>Causes the Runnable to be added to the message queue.
9802 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009803 *
Romain Guye63a4f32011-08-11 11:33:31 -07009804 * <p>This method can be invoked from outside of the UI thread
9805 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 *
9807 * @param action The Runnable that will be executed.
9808 *
9809 * @return Returns true if the Runnable was successfully placed in to the
9810 * message queue. Returns false on failure, usually because the
9811 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009812 *
9813 * @see #postDelayed
9814 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 */
9816 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009817 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009818 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009819 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009821 // Assume that post will succeed later
9822 ViewRootImpl.getRunQueue().post(action);
9823 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 }
9825
9826 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009827 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009829 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009830 *
Romain Guye63a4f32011-08-11 11:33:31 -07009831 * <p>This method can be invoked from outside of the UI thread
9832 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 *
9834 * @param action The Runnable that will be executed.
9835 * @param delayMillis The delay (in milliseconds) until the Runnable
9836 * will be executed.
9837 *
9838 * @return true if the Runnable was successfully placed in to the
9839 * message queue. Returns false on failure, usually because the
9840 * looper processing the message queue is exiting. Note that a
9841 * result of true does not mean the Runnable will be processed --
9842 * if the looper is quit before the delivery time of the message
9843 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009844 *
9845 * @see #post
9846 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009847 */
9848 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009849 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009850 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009851 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009853 // Assume that post will succeed later
9854 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9855 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 }
9857
9858 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009859 * <p>Causes the Runnable to execute on the next animation time step.
9860 * The runnable will be run on the user interface thread.</p>
9861 *
9862 * <p>This method can be invoked from outside of the UI thread
9863 * only when this View is attached to a window.</p>
9864 *
9865 * @param action The Runnable that will be executed.
9866 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009867 * @see #postOnAnimationDelayed
9868 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009869 */
9870 public void postOnAnimation(Runnable action) {
9871 final AttachInfo attachInfo = mAttachInfo;
9872 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009873 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9874 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009875 } else {
9876 // Assume that post will succeed later
9877 ViewRootImpl.getRunQueue().post(action);
9878 }
9879 }
9880
9881 /**
9882 * <p>Causes the Runnable to execute on the next animation time step,
9883 * after the specified amount of time elapses.
9884 * The runnable will be run on the user interface thread.</p>
9885 *
9886 * <p>This method can be invoked from outside of the UI thread
9887 * only when this View is attached to a window.</p>
9888 *
9889 * @param action The Runnable that will be executed.
9890 * @param delayMillis The delay (in milliseconds) until the Runnable
9891 * will be executed.
9892 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009893 * @see #postOnAnimation
9894 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009895 */
9896 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9897 final AttachInfo attachInfo = mAttachInfo;
9898 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009899 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9900 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009901 } else {
9902 // Assume that post will succeed later
9903 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9904 }
9905 }
9906
9907 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009908 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009909 *
Romain Guye63a4f32011-08-11 11:33:31 -07009910 * <p>This method can be invoked from outside of the UI thread
9911 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 *
9913 * @param action The Runnable to remove from the message handling queue
9914 *
9915 * @return true if this view could ask the Handler to remove the Runnable,
9916 * false otherwise. When the returned value is true, the Runnable
9917 * may or may not have been actually removed from the message queue
9918 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009919 *
9920 * @see #post
9921 * @see #postDelayed
9922 * @see #postOnAnimation
9923 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 */
9925 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009926 if (action != null) {
9927 final AttachInfo attachInfo = mAttachInfo;
9928 if (attachInfo != null) {
9929 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009930 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9931 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009932 } else {
9933 // Assume that post will succeed later
9934 ViewRootImpl.getRunQueue().removeCallbacks(action);
9935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009937 return true;
9938 }
9939
9940 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009941 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9942 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 *
Romain Guye63a4f32011-08-11 11:33:31 -07009944 * <p>This method can be invoked from outside of the UI thread
9945 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009946 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009948 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 */
9950 public void postInvalidate() {
9951 postInvalidateDelayed(0);
9952 }
9953
9954 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009955 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9956 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009957 *
Romain Guye63a4f32011-08-11 11:33:31 -07009958 * <p>This method can be invoked from outside of the UI thread
9959 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 *
9961 * @param left The left coordinate of the rectangle to invalidate.
9962 * @param top The top coordinate of the rectangle to invalidate.
9963 * @param right The right coordinate of the rectangle to invalidate.
9964 * @param bottom The bottom coordinate of the rectangle to invalidate.
9965 *
9966 * @see #invalidate(int, int, int, int)
9967 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009968 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 */
9970 public void postInvalidate(int left, int top, int right, int bottom) {
9971 postInvalidateDelayed(0, left, top, right, bottom);
9972 }
9973
9974 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009975 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9976 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009977 *
Romain Guye63a4f32011-08-11 11:33:31 -07009978 * <p>This method can be invoked from outside of the UI thread
9979 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 *
9981 * @param delayMilliseconds the duration in milliseconds to delay the
9982 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009983 *
9984 * @see #invalidate()
9985 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 */
9987 public void postInvalidateDelayed(long delayMilliseconds) {
9988 // We try only with the AttachInfo because there's no point in invalidating
9989 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009990 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009991 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009992 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 }
9994 }
9995
9996 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009997 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9998 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009999 *
Romain Guye63a4f32011-08-11 11:33:31 -070010000 * <p>This method can be invoked from outside of the UI thread
10001 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 *
10003 * @param delayMilliseconds the duration in milliseconds to delay the
10004 * invalidation by
10005 * @param left The left coordinate of the rectangle to invalidate.
10006 * @param top The top coordinate of the rectangle to invalidate.
10007 * @param right The right coordinate of the rectangle to invalidate.
10008 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010009 *
10010 * @see #invalidate(int, int, int, int)
10011 * @see #invalidate(Rect)
10012 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 */
10014 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10015 int right, int bottom) {
10016
10017 // We try only with the AttachInfo because there's no point in invalidating
10018 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010019 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010020 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10022 info.target = this;
10023 info.left = left;
10024 info.top = top;
10025 info.right = right;
10026 info.bottom = bottom;
10027
Jeff Browna175a5b2012-02-15 19:18:31 -080010028 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 }
10030 }
10031
10032 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010033 * <p>Cause an invalidate to happen on the next animation time step, typically the
10034 * next display frame.</p>
10035 *
10036 * <p>This method can be invoked from outside of the UI thread
10037 * only when this View is attached to a window.</p>
10038 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010039 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010040 */
10041 public void postInvalidateOnAnimation() {
10042 // We try only with the AttachInfo because there's no point in invalidating
10043 // if we are not attached to our window
10044 final AttachInfo attachInfo = mAttachInfo;
10045 if (attachInfo != null) {
10046 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10047 }
10048 }
10049
10050 /**
10051 * <p>Cause an invalidate of the specified area to happen on the next animation
10052 * time step, typically the next display frame.</p>
10053 *
10054 * <p>This method can be invoked from outside of the UI thread
10055 * only when this View is attached to a window.</p>
10056 *
10057 * @param left The left coordinate of the rectangle to invalidate.
10058 * @param top The top coordinate of the rectangle to invalidate.
10059 * @param right The right coordinate of the rectangle to invalidate.
10060 * @param bottom The bottom coordinate of the rectangle to invalidate.
10061 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010062 * @see #invalidate(int, int, int, int)
10063 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010064 */
10065 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10066 // We try only with the AttachInfo because there's no point in invalidating
10067 // if we are not attached to our window
10068 final AttachInfo attachInfo = mAttachInfo;
10069 if (attachInfo != null) {
10070 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10071 info.target = this;
10072 info.left = left;
10073 info.top = top;
10074 info.right = right;
10075 info.bottom = bottom;
10076
10077 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10078 }
10079 }
10080
10081 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010082 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10083 * This event is sent at most once every
10084 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10085 */
10086 private void postSendViewScrolledAccessibilityEventCallback() {
10087 if (mSendViewScrolledAccessibilityEvent == null) {
10088 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10089 }
10090 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10091 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10092 postDelayed(mSendViewScrolledAccessibilityEvent,
10093 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10094 }
10095 }
10096
10097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 * Called by a parent to request that a child update its values for mScrollX
10099 * and mScrollY if necessary. This will typically be done if the child is
10100 * animating a scroll using a {@link android.widget.Scroller Scroller}
10101 * object.
10102 */
10103 public void computeScroll() {
10104 }
10105
10106 /**
10107 * <p>Indicate whether the horizontal edges are faded when the view is
10108 * scrolled horizontally.</p>
10109 *
10110 * @return true if the horizontal edges should are faded on scroll, false
10111 * otherwise
10112 *
10113 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010114 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010115 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010116 */
10117 public boolean isHorizontalFadingEdgeEnabled() {
10118 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10119 }
10120
10121 /**
10122 * <p>Define whether the horizontal edges should be faded when this view
10123 * is scrolled horizontally.</p>
10124 *
10125 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10126 * be faded when the view is scrolled
10127 * horizontally
10128 *
10129 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010130 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010131 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 */
10133 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10134 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10135 if (horizontalFadingEdgeEnabled) {
10136 initScrollCache();
10137 }
10138
10139 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10140 }
10141 }
10142
10143 /**
10144 * <p>Indicate whether the vertical edges are faded when the view is
10145 * scrolled horizontally.</p>
10146 *
10147 * @return true if the vertical edges should are faded on scroll, false
10148 * otherwise
10149 *
10150 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010151 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010152 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010153 */
10154 public boolean isVerticalFadingEdgeEnabled() {
10155 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10156 }
10157
10158 /**
10159 * <p>Define whether the vertical edges should be faded when this view
10160 * is scrolled vertically.</p>
10161 *
10162 * @param verticalFadingEdgeEnabled true if the vertical edges should
10163 * be faded when the view is scrolled
10164 * vertically
10165 *
10166 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010167 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010168 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010169 */
10170 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10171 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10172 if (verticalFadingEdgeEnabled) {
10173 initScrollCache();
10174 }
10175
10176 mViewFlags ^= FADING_EDGE_VERTICAL;
10177 }
10178 }
10179
10180 /**
10181 * Returns the strength, or intensity, of the top faded edge. The strength is
10182 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10183 * returns 0.0 or 1.0 but no value in between.
10184 *
10185 * Subclasses should override this method to provide a smoother fade transition
10186 * when scrolling occurs.
10187 *
10188 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10189 */
10190 protected float getTopFadingEdgeStrength() {
10191 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10192 }
10193
10194 /**
10195 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10196 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10197 * returns 0.0 or 1.0 but no value in between.
10198 *
10199 * Subclasses should override this method to provide a smoother fade transition
10200 * when scrolling occurs.
10201 *
10202 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10203 */
10204 protected float getBottomFadingEdgeStrength() {
10205 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10206 computeVerticalScrollRange() ? 1.0f : 0.0f;
10207 }
10208
10209 /**
10210 * Returns the strength, or intensity, of the left faded edge. The strength is
10211 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10212 * returns 0.0 or 1.0 but no value in between.
10213 *
10214 * Subclasses should override this method to provide a smoother fade transition
10215 * when scrolling occurs.
10216 *
10217 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10218 */
10219 protected float getLeftFadingEdgeStrength() {
10220 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10221 }
10222
10223 /**
10224 * Returns the strength, or intensity, of the right faded edge. The strength is
10225 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10226 * returns 0.0 or 1.0 but no value in between.
10227 *
10228 * Subclasses should override this method to provide a smoother fade transition
10229 * when scrolling occurs.
10230 *
10231 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10232 */
10233 protected float getRightFadingEdgeStrength() {
10234 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10235 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10236 }
10237
10238 /**
10239 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10240 * scrollbar is not drawn by default.</p>
10241 *
10242 * @return true if the horizontal scrollbar should be painted, false
10243 * otherwise
10244 *
10245 * @see #setHorizontalScrollBarEnabled(boolean)
10246 */
10247 public boolean isHorizontalScrollBarEnabled() {
10248 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10249 }
10250
10251 /**
10252 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10253 * scrollbar is not drawn by default.</p>
10254 *
10255 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10256 * be painted
10257 *
10258 * @see #isHorizontalScrollBarEnabled()
10259 */
10260 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10261 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10262 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010263 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010264 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 }
10266 }
10267
10268 /**
10269 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10270 * scrollbar is not drawn by default.</p>
10271 *
10272 * @return true if the vertical scrollbar should be painted, false
10273 * otherwise
10274 *
10275 * @see #setVerticalScrollBarEnabled(boolean)
10276 */
10277 public boolean isVerticalScrollBarEnabled() {
10278 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10279 }
10280
10281 /**
10282 * <p>Define whether the vertical scrollbar should be drawn or not. The
10283 * scrollbar is not drawn by default.</p>
10284 *
10285 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10286 * be painted
10287 *
10288 * @see #isVerticalScrollBarEnabled()
10289 */
10290 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10291 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10292 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010293 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010294 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 }
10296 }
10297
Adam Powell20232d02010-12-08 21:08:53 -080010298 /**
10299 * @hide
10300 */
10301 protected void recomputePadding() {
10302 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010303 }
Joe Malin32736f02011-01-19 16:14:20 -080010304
Mike Cleronfe81d382009-09-28 14:22:16 -070010305 /**
10306 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010307 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010308 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010309 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010310 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010311 */
10312 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10313 initScrollCache();
10314 final ScrollabilityCache scrollabilityCache = mScrollCache;
10315 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010316 if (fadeScrollbars) {
10317 scrollabilityCache.state = ScrollabilityCache.OFF;
10318 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010319 scrollabilityCache.state = ScrollabilityCache.ON;
10320 }
10321 }
Joe Malin32736f02011-01-19 16:14:20 -080010322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 /**
Joe Malin32736f02011-01-19 16:14:20 -080010324 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010325 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010326 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010327 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010328 *
10329 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010330 */
10331 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010332 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010333 }
Joe Malin32736f02011-01-19 16:14:20 -080010334
Mike Cleron52f0a642009-09-28 18:21:37 -070010335 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010336 *
10337 * Returns the delay before scrollbars fade.
10338 *
10339 * @return the delay before scrollbars fade
10340 *
10341 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10342 */
10343 public int getScrollBarDefaultDelayBeforeFade() {
10344 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10345 mScrollCache.scrollBarDefaultDelayBeforeFade;
10346 }
10347
10348 /**
10349 * Define the delay before scrollbars fade.
10350 *
10351 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10352 *
10353 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10354 */
10355 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10356 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10357 }
10358
10359 /**
10360 *
10361 * Returns the scrollbar fade duration.
10362 *
10363 * @return the scrollbar fade duration
10364 *
10365 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10366 */
10367 public int getScrollBarFadeDuration() {
10368 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10369 mScrollCache.scrollBarFadeDuration;
10370 }
10371
10372 /**
10373 * Define the scrollbar fade duration.
10374 *
10375 * @param scrollBarFadeDuration - the scrollbar fade duration
10376 *
10377 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10378 */
10379 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10380 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10381 }
10382
10383 /**
10384 *
10385 * Returns the scrollbar size.
10386 *
10387 * @return the scrollbar size
10388 *
10389 * @attr ref android.R.styleable#View_scrollbarSize
10390 */
10391 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010392 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010393 mScrollCache.scrollBarSize;
10394 }
10395
10396 /**
10397 * Define the scrollbar size.
10398 *
10399 * @param scrollBarSize - the scrollbar size
10400 *
10401 * @attr ref android.R.styleable#View_scrollbarSize
10402 */
10403 public void setScrollBarSize(int scrollBarSize) {
10404 getScrollCache().scrollBarSize = scrollBarSize;
10405 }
10406
10407 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10409 * inset. When inset, they add to the padding of the view. And the scrollbars
10410 * can be drawn inside the padding area or on the edge of the view. For example,
10411 * if a view has a background drawable and you want to draw the scrollbars
10412 * inside the padding specified by the drawable, you can use
10413 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10414 * appear at the edge of the view, ignoring the padding, then you can use
10415 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10416 * @param style the style of the scrollbars. Should be one of
10417 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10418 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10419 * @see #SCROLLBARS_INSIDE_OVERLAY
10420 * @see #SCROLLBARS_INSIDE_INSET
10421 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10422 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010423 *
10424 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 */
10426 public void setScrollBarStyle(int style) {
10427 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10428 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010429 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010430 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 }
10432 }
10433
10434 /**
10435 * <p>Returns the current scrollbar style.</p>
10436 * @return the current scrollbar style
10437 * @see #SCROLLBARS_INSIDE_OVERLAY
10438 * @see #SCROLLBARS_INSIDE_INSET
10439 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10440 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010441 *
10442 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010444 @ViewDebug.ExportedProperty(mapping = {
10445 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10446 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10447 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10448 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10449 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 public int getScrollBarStyle() {
10451 return mViewFlags & SCROLLBARS_STYLE_MASK;
10452 }
10453
10454 /**
10455 * <p>Compute the horizontal range that the horizontal scrollbar
10456 * represents.</p>
10457 *
10458 * <p>The range is expressed in arbitrary units that must be the same as the
10459 * units used by {@link #computeHorizontalScrollExtent()} and
10460 * {@link #computeHorizontalScrollOffset()}.</p>
10461 *
10462 * <p>The default range is the drawing width of this view.</p>
10463 *
10464 * @return the total horizontal range represented by the horizontal
10465 * scrollbar
10466 *
10467 * @see #computeHorizontalScrollExtent()
10468 * @see #computeHorizontalScrollOffset()
10469 * @see android.widget.ScrollBarDrawable
10470 */
10471 protected int computeHorizontalScrollRange() {
10472 return getWidth();
10473 }
10474
10475 /**
10476 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10477 * within the horizontal range. This value is used to compute the position
10478 * of the thumb within the scrollbar's track.</p>
10479 *
10480 * <p>The range is expressed in arbitrary units that must be the same as the
10481 * units used by {@link #computeHorizontalScrollRange()} and
10482 * {@link #computeHorizontalScrollExtent()}.</p>
10483 *
10484 * <p>The default offset is the scroll offset of this view.</p>
10485 *
10486 * @return the horizontal offset of the scrollbar's thumb
10487 *
10488 * @see #computeHorizontalScrollRange()
10489 * @see #computeHorizontalScrollExtent()
10490 * @see android.widget.ScrollBarDrawable
10491 */
10492 protected int computeHorizontalScrollOffset() {
10493 return mScrollX;
10494 }
10495
10496 /**
10497 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10498 * within the horizontal range. This value is used to compute the length
10499 * of the thumb within the scrollbar's track.</p>
10500 *
10501 * <p>The range is expressed in arbitrary units that must be the same as the
10502 * units used by {@link #computeHorizontalScrollRange()} and
10503 * {@link #computeHorizontalScrollOffset()}.</p>
10504 *
10505 * <p>The default extent is the drawing width of this view.</p>
10506 *
10507 * @return the horizontal extent of the scrollbar's thumb
10508 *
10509 * @see #computeHorizontalScrollRange()
10510 * @see #computeHorizontalScrollOffset()
10511 * @see android.widget.ScrollBarDrawable
10512 */
10513 protected int computeHorizontalScrollExtent() {
10514 return getWidth();
10515 }
10516
10517 /**
10518 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10519 *
10520 * <p>The range is expressed in arbitrary units that must be the same as the
10521 * units used by {@link #computeVerticalScrollExtent()} and
10522 * {@link #computeVerticalScrollOffset()}.</p>
10523 *
10524 * @return the total vertical range represented by the vertical scrollbar
10525 *
10526 * <p>The default range is the drawing height of this view.</p>
10527 *
10528 * @see #computeVerticalScrollExtent()
10529 * @see #computeVerticalScrollOffset()
10530 * @see android.widget.ScrollBarDrawable
10531 */
10532 protected int computeVerticalScrollRange() {
10533 return getHeight();
10534 }
10535
10536 /**
10537 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10538 * within the horizontal range. This value is used to compute the position
10539 * of the thumb within the scrollbar's track.</p>
10540 *
10541 * <p>The range is expressed in arbitrary units that must be the same as the
10542 * units used by {@link #computeVerticalScrollRange()} and
10543 * {@link #computeVerticalScrollExtent()}.</p>
10544 *
10545 * <p>The default offset is the scroll offset of this view.</p>
10546 *
10547 * @return the vertical offset of the scrollbar's thumb
10548 *
10549 * @see #computeVerticalScrollRange()
10550 * @see #computeVerticalScrollExtent()
10551 * @see android.widget.ScrollBarDrawable
10552 */
10553 protected int computeVerticalScrollOffset() {
10554 return mScrollY;
10555 }
10556
10557 /**
10558 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10559 * within the vertical range. This value is used to compute the length
10560 * of the thumb within the scrollbar's track.</p>
10561 *
10562 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010563 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 * {@link #computeVerticalScrollOffset()}.</p>
10565 *
10566 * <p>The default extent is the drawing height of this view.</p>
10567 *
10568 * @return the vertical extent of the scrollbar's thumb
10569 *
10570 * @see #computeVerticalScrollRange()
10571 * @see #computeVerticalScrollOffset()
10572 * @see android.widget.ScrollBarDrawable
10573 */
10574 protected int computeVerticalScrollExtent() {
10575 return getHeight();
10576 }
10577
10578 /**
Adam Powell69159442011-06-13 17:53:06 -070010579 * Check if this view can be scrolled horizontally in a certain direction.
10580 *
10581 * @param direction Negative to check scrolling left, positive to check scrolling right.
10582 * @return true if this view can be scrolled in the specified direction, false otherwise.
10583 */
10584 public boolean canScrollHorizontally(int direction) {
10585 final int offset = computeHorizontalScrollOffset();
10586 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10587 if (range == 0) return false;
10588 if (direction < 0) {
10589 return offset > 0;
10590 } else {
10591 return offset < range - 1;
10592 }
10593 }
10594
10595 /**
10596 * Check if this view can be scrolled vertically in a certain direction.
10597 *
10598 * @param direction Negative to check scrolling up, positive to check scrolling down.
10599 * @return true if this view can be scrolled in the specified direction, false otherwise.
10600 */
10601 public boolean canScrollVertically(int direction) {
10602 final int offset = computeVerticalScrollOffset();
10603 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10604 if (range == 0) return false;
10605 if (direction < 0) {
10606 return offset > 0;
10607 } else {
10608 return offset < range - 1;
10609 }
10610 }
10611
10612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10614 * scrollbars are painted only if they have been awakened first.</p>
10615 *
10616 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010617 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010618 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010620 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621 // scrollbars are drawn only when the animation is running
10622 final ScrollabilityCache cache = mScrollCache;
10623 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010624
Mike Cleronf116bf82009-09-27 19:14:12 -070010625 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010626
Mike Cleronf116bf82009-09-27 19:14:12 -070010627 if (state == ScrollabilityCache.OFF) {
10628 return;
10629 }
Joe Malin32736f02011-01-19 16:14:20 -080010630
Mike Cleronf116bf82009-09-27 19:14:12 -070010631 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010632
Mike Cleronf116bf82009-09-27 19:14:12 -070010633 if (state == ScrollabilityCache.FADING) {
10634 // We're fading -- get our fade interpolation
10635 if (cache.interpolatorValues == null) {
10636 cache.interpolatorValues = new float[1];
10637 }
Joe Malin32736f02011-01-19 16:14:20 -080010638
Mike Cleronf116bf82009-09-27 19:14:12 -070010639 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010640
Mike Cleronf116bf82009-09-27 19:14:12 -070010641 // Stops the animation if we're done
10642 if (cache.scrollBarInterpolator.timeToValues(values) ==
10643 Interpolator.Result.FREEZE_END) {
10644 cache.state = ScrollabilityCache.OFF;
10645 } else {
10646 cache.scrollBar.setAlpha(Math.round(values[0]));
10647 }
Joe Malin32736f02011-01-19 16:14:20 -080010648
10649 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010650 // drawing. We only want this when we're fading so that
10651 // we prevent excessive redraws
10652 invalidate = true;
10653 } else {
10654 // We're just on -- but we may have been fading before so
10655 // reset alpha
10656 cache.scrollBar.setAlpha(255);
10657 }
10658
Joe Malin32736f02011-01-19 16:14:20 -080010659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 final int viewFlags = mViewFlags;
10661
10662 final boolean drawHorizontalScrollBar =
10663 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10664 final boolean drawVerticalScrollBar =
10665 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10666 && !isVerticalScrollBarHidden();
10667
10668 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10669 final int width = mRight - mLeft;
10670 final int height = mBottom - mTop;
10671
10672 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673
Mike Reede8853fc2009-09-04 14:01:48 -040010674 final int scrollX = mScrollX;
10675 final int scrollY = mScrollY;
10676 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10677
Mike Cleronf116bf82009-09-27 19:14:12 -070010678 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010680 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010681 int size = scrollBar.getSize(false);
10682 if (size <= 0) {
10683 size = cache.scrollBarSize;
10684 }
10685
Mike Cleronf116bf82009-09-27 19:14:12 -070010686 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010687 computeHorizontalScrollOffset(),
10688 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010689 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010690 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010691 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010692 left = scrollX + (mPaddingLeft & inside);
10693 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10694 bottom = top + size;
10695 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10696 if (invalidate) {
10697 invalidate(left, top, right, bottom);
10698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 }
10700
10701 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010702 int size = scrollBar.getSize(true);
10703 if (size <= 0) {
10704 size = cache.scrollBarSize;
10705 }
10706
Mike Reede8853fc2009-09-04 14:01:48 -040010707 scrollBar.setParameters(computeVerticalScrollRange(),
10708 computeVerticalScrollOffset(),
10709 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010710 switch (mVerticalScrollbarPosition) {
10711 default:
10712 case SCROLLBAR_POSITION_DEFAULT:
10713 case SCROLLBAR_POSITION_RIGHT:
10714 left = scrollX + width - size - (mUserPaddingRight & inside);
10715 break;
10716 case SCROLLBAR_POSITION_LEFT:
10717 left = scrollX + (mUserPaddingLeft & inside);
10718 break;
10719 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010720 top = scrollY + (mPaddingTop & inside);
10721 right = left + size;
10722 bottom = scrollY + height - (mUserPaddingBottom & inside);
10723 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10724 if (invalidate) {
10725 invalidate(left, top, right, bottom);
10726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010727 }
10728 }
10729 }
10730 }
Romain Guy8506ab42009-06-11 17:35:47 -070010731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010733 * 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 -080010734 * FastScroller is visible.
10735 * @return whether to temporarily hide the vertical scrollbar
10736 * @hide
10737 */
10738 protected boolean isVerticalScrollBarHidden() {
10739 return false;
10740 }
10741
10742 /**
10743 * <p>Draw the horizontal scrollbar if
10744 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10745 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 * @param canvas the canvas on which to draw the scrollbar
10747 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 *
10749 * @see #isHorizontalScrollBarEnabled()
10750 * @see #computeHorizontalScrollRange()
10751 * @see #computeHorizontalScrollExtent()
10752 * @see #computeHorizontalScrollOffset()
10753 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010754 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010755 */
Romain Guy8fb95422010-08-17 18:38:51 -070010756 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10757 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010758 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010759 scrollBar.draw(canvas);
10760 }
Mike Reede8853fc2009-09-04 14:01:48 -040010761
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10764 * returns true.</p>
10765 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 * @param canvas the canvas on which to draw the scrollbar
10767 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 *
10769 * @see #isVerticalScrollBarEnabled()
10770 * @see #computeVerticalScrollRange()
10771 * @see #computeVerticalScrollExtent()
10772 * @see #computeVerticalScrollOffset()
10773 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010774 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 */
Romain Guy8fb95422010-08-17 18:38:51 -070010776 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10777 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010778 scrollBar.setBounds(l, t, r, b);
10779 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 }
10781
10782 /**
10783 * Implement this to do your drawing.
10784 *
10785 * @param canvas the canvas on which the background will be drawn
10786 */
10787 protected void onDraw(Canvas canvas) {
10788 }
10789
10790 /*
10791 * Caller is responsible for calling requestLayout if necessary.
10792 * (This allows addViewInLayout to not request a new layout.)
10793 */
10794 void assignParent(ViewParent parent) {
10795 if (mParent == null) {
10796 mParent = parent;
10797 } else if (parent == null) {
10798 mParent = null;
10799 } else {
10800 throw new RuntimeException("view " + this + " being added, but"
10801 + " it already has a parent");
10802 }
10803 }
10804
10805 /**
10806 * This is called when the view is attached to a window. At this point it
10807 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010808 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10809 * however it may be called any time before the first onDraw -- including
10810 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 *
10812 * @see #onDetachedFromWindow()
10813 */
10814 protected void onAttachedToWindow() {
10815 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10816 mParent.requestTransparentRegion(this);
10817 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010818 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10819 initialAwakenScrollBars();
10820 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10821 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010822 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010823 // Order is important here: LayoutDirection MUST be resolved before Padding
10824 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010825 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010826 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010827 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010828 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010829 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010830 if (isFocused()) {
10831 InputMethodManager imm = InputMethodManager.peekInstance();
10832 imm.focusIn(this);
10833 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010834 }
Cibu Johny86666632010-02-22 13:01:02 -080010835
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010836 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010837 * @see #onScreenStateChanged(int)
10838 */
10839 void dispatchScreenStateChanged(int screenState) {
10840 onScreenStateChanged(screenState);
10841 }
10842
10843 /**
10844 * This method is called whenever the state of the screen this view is
10845 * attached to changes. A state change will usually occurs when the screen
10846 * turns on or off (whether it happens automatically or the user does it
10847 * manually.)
10848 *
10849 * @param screenState The new state of the screen. Can be either
10850 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10851 */
10852 public void onScreenStateChanged(int screenState) {
10853 }
10854
10855 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010856 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10857 */
10858 private boolean hasRtlSupport() {
10859 return mContext.getApplicationInfo().hasRtlSupport();
10860 }
10861
10862 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010863 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10864 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010865 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010866 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010867 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010868 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010869 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010870
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010871 if (hasRtlSupport()) {
10872 // Set resolved depending on layout direction
10873 switch (getLayoutDirection()) {
10874 case LAYOUT_DIRECTION_INHERIT:
10875 // If this is root view, no need to look at parent's layout dir.
10876 if (canResolveLayoutDirection()) {
10877 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010878
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010879 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10880 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10881 }
10882 } else {
10883 // Nothing to do, LTR by default
10884 }
10885 break;
10886 case LAYOUT_DIRECTION_RTL:
10887 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10888 break;
10889 case LAYOUT_DIRECTION_LOCALE:
10890 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010891 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10892 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010893 break;
10894 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010895 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010896 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010897 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010898
10899 // Set to resolved
10900 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010901 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010902 // Resolve padding
10903 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010904 }
10905
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010906 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010907 * Called when layout direction has been resolved.
10908 *
10909 * The default implementation does nothing.
10910 */
10911 public void onResolvedLayoutDirectionChanged() {
10912 }
10913
10914 /**
10915 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010916 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010917 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010918 // If the user specified the absolute padding (either with android:padding or
10919 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10920 // use the default padding or the padding from the background drawable
10921 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010922 int resolvedLayoutDirection = getResolvedLayoutDirection();
10923 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010924 case LAYOUT_DIRECTION_RTL:
10925 // Start user padding override Right user padding. Otherwise, if Right user
10926 // padding is not defined, use the default Right padding. If Right user padding
10927 // is defined, just use it.
10928 if (mUserPaddingStart >= 0) {
10929 mUserPaddingRight = mUserPaddingStart;
10930 } else if (mUserPaddingRight < 0) {
10931 mUserPaddingRight = mPaddingRight;
10932 }
10933 if (mUserPaddingEnd >= 0) {
10934 mUserPaddingLeft = mUserPaddingEnd;
10935 } else if (mUserPaddingLeft < 0) {
10936 mUserPaddingLeft = mPaddingLeft;
10937 }
10938 break;
10939 case LAYOUT_DIRECTION_LTR:
10940 default:
10941 // Start user padding override Left user padding. Otherwise, if Left user
10942 // padding is not defined, use the default left padding. If Left user padding
10943 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010944 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010945 mUserPaddingLeft = mUserPaddingStart;
10946 } else if (mUserPaddingLeft < 0) {
10947 mUserPaddingLeft = mPaddingLeft;
10948 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010949 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010950 mUserPaddingRight = mUserPaddingEnd;
10951 } else if (mUserPaddingRight < 0) {
10952 mUserPaddingRight = mPaddingRight;
10953 }
10954 }
10955
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010956 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10957
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010958 if(isPaddingRelative()) {
10959 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10960 } else {
10961 recomputePadding();
10962 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010963 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010964 }
10965
10966 /**
10967 * Resolve padding depending on the layout direction. Subclasses that care about
10968 * padding resolution should override this method. The default implementation does
10969 * nothing.
10970 *
10971 * @param layoutDirection the direction of the layout
10972 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010973 * @see {@link #LAYOUT_DIRECTION_LTR}
10974 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010975 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010976 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010977 }
10978
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010979 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010980 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010981 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010982 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010983 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010984 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010985 switch (getLayoutDirection()) {
10986 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010987 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010988 default:
10989 return true;
10990 }
10991 }
10992
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010993 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010994 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10995 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010996 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010997 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010998 // Reset the current resolved bits
10999 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011000 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011001 // Reset also the text direction
11002 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011003 }
11004
11005 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011006 * Called during reset of resolved layout direction.
11007 *
11008 * Subclasses need to override this method to clear cached information that depends on the
11009 * resolved layout direction, or to inform child views that inherit their layout direction.
11010 *
11011 * The default implementation does nothing.
11012 */
11013 public void onResolvedLayoutDirectionReset() {
11014 }
11015
11016 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011017 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011018 *
11019 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011020 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011021 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011022 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011023 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 }
11025
11026 /**
11027 * This is called when the view is detached from a window. At this point it
11028 * no longer has a surface for drawing.
11029 *
11030 * @see #onAttachedToWindow()
11031 */
11032 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011033 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011034
Romain Guya440b002010-02-24 15:57:54 -080011035 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011036 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011037 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011038 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011041
Romain Guya998dff2012-03-23 18:58:36 -070011042 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011043
11044 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011045 if (mDisplayList != null) {
11046 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11047 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011048 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011049 } else {
11050 if (mDisplayList != null) {
11051 // Should never happen
11052 mDisplayList.invalidate();
11053 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011054 }
11055
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011056 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011057
11058 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011059 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011060 resetAccessibilityStateChanged();
11061 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 }
11063
11064 /**
11065 * @return The number of times this view has been attached to a window
11066 */
11067 protected int getWindowAttachCount() {
11068 return mWindowAttachCount;
11069 }
11070
11071 /**
11072 * Retrieve a unique token identifying the window this view is attached to.
11073 * @return Return the window's token for use in
11074 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11075 */
11076 public IBinder getWindowToken() {
11077 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11078 }
11079
11080 /**
11081 * Retrieve a unique token identifying the top-level "real" window of
11082 * the window that this view is attached to. That is, this is like
11083 * {@link #getWindowToken}, except if the window this view in is a panel
11084 * window (attached to another containing window), then the token of
11085 * the containing window is returned instead.
11086 *
11087 * @return Returns the associated window token, either
11088 * {@link #getWindowToken()} or the containing window's token.
11089 */
11090 public IBinder getApplicationWindowToken() {
11091 AttachInfo ai = mAttachInfo;
11092 if (ai != null) {
11093 IBinder appWindowToken = ai.mPanelParentWindowToken;
11094 if (appWindowToken == null) {
11095 appWindowToken = ai.mWindowToken;
11096 }
11097 return appWindowToken;
11098 }
11099 return null;
11100 }
11101
11102 /**
11103 * Retrieve private session object this view hierarchy is using to
11104 * communicate with the window manager.
11105 * @return the session object to communicate with the window manager
11106 */
11107 /*package*/ IWindowSession getWindowSession() {
11108 return mAttachInfo != null ? mAttachInfo.mSession : null;
11109 }
11110
11111 /**
11112 * @param info the {@link android.view.View.AttachInfo} to associated with
11113 * this view
11114 */
11115 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11116 //System.out.println("Attached! " + this);
11117 mAttachInfo = info;
11118 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011119 // We will need to evaluate the drawable state at least once.
11120 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121 if (mFloatingTreeObserver != null) {
11122 info.mTreeObserver.merge(mFloatingTreeObserver);
11123 mFloatingTreeObserver = null;
11124 }
11125 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11126 mAttachInfo.mScrollContainers.add(this);
11127 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11128 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011129 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011130 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011131
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011132 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011133 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011134 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011135 if (listeners != null && listeners.size() > 0) {
11136 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11137 // perform the dispatching. The iterator is a safe guard against listeners that
11138 // could mutate the list by calling the various add/remove methods. This prevents
11139 // the array from being modified while we iterate it.
11140 for (OnAttachStateChangeListener listener : listeners) {
11141 listener.onViewAttachedToWindow(this);
11142 }
11143 }
11144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 int vis = info.mWindowVisibility;
11146 if (vis != GONE) {
11147 onWindowVisibilityChanged(vis);
11148 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011149 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11150 // If nobody has evaluated the drawable state yet, then do it now.
11151 refreshDrawableState();
11152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011153 }
11154
11155 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011156 AttachInfo info = mAttachInfo;
11157 if (info != null) {
11158 int vis = info.mWindowVisibility;
11159 if (vis != GONE) {
11160 onWindowVisibilityChanged(GONE);
11161 }
11162 }
11163
11164 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011165
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011166 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011167 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011168 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011169 if (listeners != null && listeners.size() > 0) {
11170 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11171 // perform the dispatching. The iterator is a safe guard against listeners that
11172 // could mutate the list by calling the various add/remove methods. This prevents
11173 // the array from being modified while we iterate it.
11174 for (OnAttachStateChangeListener listener : listeners) {
11175 listener.onViewDetachedFromWindow(this);
11176 }
11177 }
11178
Romain Guy01d5edc2011-01-28 11:28:53 -080011179 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011180 mAttachInfo.mScrollContainers.remove(this);
11181 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11182 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011184 mAttachInfo = null;
11185 }
11186
11187 /**
11188 * Store this view hierarchy's frozen state into the given container.
11189 *
11190 * @param container The SparseArray in which to save the view's state.
11191 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011192 * @see #restoreHierarchyState(android.util.SparseArray)
11193 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11194 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011195 */
11196 public void saveHierarchyState(SparseArray<Parcelable> container) {
11197 dispatchSaveInstanceState(container);
11198 }
11199
11200 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011201 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11202 * this view and its children. May be overridden to modify how freezing happens to a
11203 * 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 -080011204 *
11205 * @param container The SparseArray in which to save the view's state.
11206 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011207 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11208 * @see #saveHierarchyState(android.util.SparseArray)
11209 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 */
11211 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11212 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11213 mPrivateFlags &= ~SAVE_STATE_CALLED;
11214 Parcelable state = onSaveInstanceState();
11215 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11216 throw new IllegalStateException(
11217 "Derived class did not call super.onSaveInstanceState()");
11218 }
11219 if (state != null) {
11220 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11221 // + ": " + state);
11222 container.put(mID, state);
11223 }
11224 }
11225 }
11226
11227 /**
11228 * Hook allowing a view to generate a representation of its internal state
11229 * that can later be used to create a new instance with that same state.
11230 * This state should only contain information that is not persistent or can
11231 * not be reconstructed later. For example, you will never store your
11232 * current position on screen because that will be computed again when a
11233 * new instance of the view is placed in its view hierarchy.
11234 * <p>
11235 * Some examples of things you may store here: the current cursor position
11236 * in a text view (but usually not the text itself since that is stored in a
11237 * content provider or other persistent storage), the currently selected
11238 * item in a list view.
11239 *
11240 * @return Returns a Parcelable object containing the view's current dynamic
11241 * state, or null if there is nothing interesting to save. The
11242 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011243 * @see #onRestoreInstanceState(android.os.Parcelable)
11244 * @see #saveHierarchyState(android.util.SparseArray)
11245 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 * @see #setSaveEnabled(boolean)
11247 */
11248 protected Parcelable onSaveInstanceState() {
11249 mPrivateFlags |= SAVE_STATE_CALLED;
11250 return BaseSavedState.EMPTY_STATE;
11251 }
11252
11253 /**
11254 * Restore this view hierarchy's frozen state from the given container.
11255 *
11256 * @param container The SparseArray which holds previously frozen states.
11257 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011258 * @see #saveHierarchyState(android.util.SparseArray)
11259 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11260 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 */
11262 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11263 dispatchRestoreInstanceState(container);
11264 }
11265
11266 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011267 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11268 * state for this view and its children. May be overridden to modify how restoring
11269 * happens to a view's children; for example, some views may want to not store state
11270 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 *
11272 * @param container The SparseArray which holds previously saved state.
11273 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011274 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11275 * @see #restoreHierarchyState(android.util.SparseArray)
11276 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 */
11278 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11279 if (mID != NO_ID) {
11280 Parcelable state = container.get(mID);
11281 if (state != null) {
11282 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11283 // + ": " + state);
11284 mPrivateFlags &= ~SAVE_STATE_CALLED;
11285 onRestoreInstanceState(state);
11286 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11287 throw new IllegalStateException(
11288 "Derived class did not call super.onRestoreInstanceState()");
11289 }
11290 }
11291 }
11292 }
11293
11294 /**
11295 * Hook allowing a view to re-apply a representation of its internal state that had previously
11296 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11297 * null state.
11298 *
11299 * @param state The frozen state that had previously been returned by
11300 * {@link #onSaveInstanceState}.
11301 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011302 * @see #onSaveInstanceState()
11303 * @see #restoreHierarchyState(android.util.SparseArray)
11304 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 */
11306 protected void onRestoreInstanceState(Parcelable state) {
11307 mPrivateFlags |= SAVE_STATE_CALLED;
11308 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011309 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11310 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011311 + "when two views of different type have the same id in the same hierarchy. "
11312 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011313 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011314 }
11315 }
11316
11317 /**
11318 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11319 *
11320 * @return the drawing start time in milliseconds
11321 */
11322 public long getDrawingTime() {
11323 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11324 }
11325
11326 /**
11327 * <p>Enables or disables the duplication of the parent's state into this view. When
11328 * duplication is enabled, this view gets its drawable state from its parent rather
11329 * than from its own internal properties.</p>
11330 *
11331 * <p>Note: in the current implementation, setting this property to true after the
11332 * view was added to a ViewGroup might have no effect at all. This property should
11333 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11334 *
11335 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11336 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011337 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011338 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11339 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 *
11341 * @param enabled True to enable duplication of the parent's drawable state, false
11342 * to disable it.
11343 *
11344 * @see #getDrawableState()
11345 * @see #isDuplicateParentStateEnabled()
11346 */
11347 public void setDuplicateParentStateEnabled(boolean enabled) {
11348 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11349 }
11350
11351 /**
11352 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11353 *
11354 * @return True if this view's drawable state is duplicated from the parent,
11355 * false otherwise
11356 *
11357 * @see #getDrawableState()
11358 * @see #setDuplicateParentStateEnabled(boolean)
11359 */
11360 public boolean isDuplicateParentStateEnabled() {
11361 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11362 }
11363
11364 /**
Romain Guy171c5922011-01-06 10:04:23 -080011365 * <p>Specifies the type of layer backing this view. The layer can be
11366 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11367 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011368 *
Romain Guy171c5922011-01-06 10:04:23 -080011369 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11370 * instance that controls how the layer is composed on screen. The following
11371 * properties of the paint are taken into account when composing the layer:</p>
11372 * <ul>
11373 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11374 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11375 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11376 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011377 *
Romain Guy171c5922011-01-06 10:04:23 -080011378 * <p>If this view has an alpha value set to < 1.0 by calling
11379 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11380 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11381 * equivalent to setting a hardware layer on this view and providing a paint with
11382 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011383 *
Romain Guy171c5922011-01-06 10:04:23 -080011384 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11385 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11386 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011387 *
Romain Guy171c5922011-01-06 10:04:23 -080011388 * @param layerType The ype of layer to use with this view, must be one of
11389 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11390 * {@link #LAYER_TYPE_HARDWARE}
11391 * @param paint The paint used to compose the layer. This argument is optional
11392 * and can be null. It is ignored when the layer type is
11393 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011394 *
11395 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011396 * @see #LAYER_TYPE_NONE
11397 * @see #LAYER_TYPE_SOFTWARE
11398 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011399 * @see #setAlpha(float)
11400 *
Romain Guy171c5922011-01-06 10:04:23 -080011401 * @attr ref android.R.styleable#View_layerType
11402 */
11403 public void setLayerType(int layerType, Paint paint) {
11404 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011405 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011406 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11407 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011408
Romain Guyd6cd5722011-01-17 14:42:41 -080011409 if (layerType == mLayerType) {
11410 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11411 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011412 invalidateParentCaches();
11413 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011414 }
11415 return;
11416 }
Romain Guy171c5922011-01-06 10:04:23 -080011417
11418 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011419 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011420 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011421 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011422 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011423 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011424 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011425 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011426 default:
11427 break;
Romain Guy171c5922011-01-06 10:04:23 -080011428 }
11429
11430 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011431 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11432 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11433 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011434
Romain Guy0fd89bf2011-01-26 15:41:30 -080011435 invalidateParentCaches();
11436 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011437 }
11438
11439 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011440 * Indicates whether this view has a static layer. A view with layer type
11441 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11442 * dynamic.
11443 */
11444 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011445 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011446 }
11447
11448 /**
Romain Guy171c5922011-01-06 10:04:23 -080011449 * Indicates what type of layer is currently associated with this view. By default
11450 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11451 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11452 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011453 *
Romain Guy171c5922011-01-06 10:04:23 -080011454 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11455 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011456 *
11457 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011458 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011459 * @see #LAYER_TYPE_NONE
11460 * @see #LAYER_TYPE_SOFTWARE
11461 * @see #LAYER_TYPE_HARDWARE
11462 */
11463 public int getLayerType() {
11464 return mLayerType;
11465 }
Joe Malin32736f02011-01-19 16:14:20 -080011466
Romain Guy6c319ca2011-01-11 14:29:25 -080011467 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011468 * Forces this view's layer to be created and this view to be rendered
11469 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11470 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011471 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011472 * This method can for instance be used to render a view into its layer before
11473 * starting an animation. If this view is complex, rendering into the layer
11474 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011475 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011476 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011477 *
11478 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011479 */
11480 public void buildLayer() {
11481 if (mLayerType == LAYER_TYPE_NONE) return;
11482
11483 if (mAttachInfo == null) {
11484 throw new IllegalStateException("This view must be attached to a window first");
11485 }
11486
11487 switch (mLayerType) {
11488 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011489 if (mAttachInfo.mHardwareRenderer != null &&
11490 mAttachInfo.mHardwareRenderer.isEnabled() &&
11491 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011492 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011493 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011494 break;
11495 case LAYER_TYPE_SOFTWARE:
11496 buildDrawingCache(true);
11497 break;
11498 }
11499 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011500
Romain Guy9c4b79a2011-11-10 19:23:58 -080011501 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11502 void flushLayer() {
11503 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11504 mHardwareLayer.flush();
11505 }
11506 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011507
11508 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011509 * <p>Returns a hardware layer that can be used to draw this view again
11510 * without executing its draw method.</p>
11511 *
11512 * @return A HardwareLayer ready to render, or null if an error occurred.
11513 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011514 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011515 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11516 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011517 return null;
11518 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011519
Romain Guy9c4b79a2011-11-10 19:23:58 -080011520 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011521
11522 final int width = mRight - mLeft;
11523 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011524
Romain Guy6c319ca2011-01-11 14:29:25 -080011525 if (width == 0 || height == 0) {
11526 return null;
11527 }
11528
11529 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11530 if (mHardwareLayer == null) {
11531 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11532 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011533 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011534 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11535 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011536 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011537 }
11538
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011539 // The layer is not valid if the underlying GPU resources cannot be allocated
11540 if (!mHardwareLayer.isValid()) {
11541 return null;
11542 }
11543
Chet Haasea1cff502012-02-21 13:43:44 -080011544 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011545 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011546 }
11547
11548 return mHardwareLayer;
11549 }
Romain Guy171c5922011-01-06 10:04:23 -080011550
Romain Guy589b0bb2011-10-10 13:57:47 -070011551 /**
11552 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011553 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011554 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011555 *
11556 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011557 * @see #LAYER_TYPE_HARDWARE
11558 */
Romain Guya998dff2012-03-23 18:58:36 -070011559 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011560 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011561 AttachInfo info = mAttachInfo;
11562 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011563 info.mHardwareRenderer.isEnabled() &&
11564 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011565 mHardwareLayer.destroy();
11566 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011567
Romain Guy9c4b79a2011-11-10 19:23:58 -080011568 invalidate(true);
11569 invalidateParentCaches();
11570 }
Romain Guy65b345f2011-07-27 18:51:50 -070011571 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011572 }
Romain Guy65b345f2011-07-27 18:51:50 -070011573 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011574 }
11575
Romain Guy171c5922011-01-06 10:04:23 -080011576 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011577 * Destroys all hardware rendering resources. This method is invoked
11578 * when the system needs to reclaim resources. Upon execution of this
11579 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011580 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011581 * Note: you <strong>must</strong> call
11582 * <code>super.destroyHardwareResources()</code> when overriding
11583 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011584 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011585 * @hide
11586 */
11587 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011588 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011589 }
11590
11591 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11593 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11594 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11595 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11596 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11597 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011598 *
Romain Guy171c5922011-01-06 10:04:23 -080011599 * <p>Enabling the drawing cache is similar to
11600 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011601 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11602 * drawing cache has no effect on rendering because the system uses a different mechanism
11603 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11604 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11605 * for information on how to enable software and hardware layers.</p>
11606 *
11607 * <p>This API can be used to manually generate
11608 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11609 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 *
11611 * @param enabled true to enable the drawing cache, false otherwise
11612 *
11613 * @see #isDrawingCacheEnabled()
11614 * @see #getDrawingCache()
11615 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011616 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 */
11618 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011619 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011620 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11621 }
11622
11623 /**
11624 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11625 *
11626 * @return true if the drawing cache is enabled
11627 *
11628 * @see #setDrawingCacheEnabled(boolean)
11629 * @see #getDrawingCache()
11630 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011631 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 public boolean isDrawingCacheEnabled() {
11633 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11634 }
11635
11636 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011637 * Debugging utility which recursively outputs the dirty state of a view and its
11638 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011639 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011640 * @hide
11641 */
Romain Guy676b1732011-02-14 14:45:33 -080011642 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011643 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11644 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11645 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11646 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11647 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11648 if (clear) {
11649 mPrivateFlags &= clearMask;
11650 }
11651 if (this instanceof ViewGroup) {
11652 ViewGroup parent = (ViewGroup) this;
11653 final int count = parent.getChildCount();
11654 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011655 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011656 child.outputDirtyFlags(indent + " ", clear, clearMask);
11657 }
11658 }
11659 }
11660
11661 /**
11662 * This method is used by ViewGroup to cause its children to restore or recreate their
11663 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11664 * to recreate its own display list, which would happen if it went through the normal
11665 * draw/dispatchDraw mechanisms.
11666 *
11667 * @hide
11668 */
11669 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011670
11671 /**
11672 * A view that is not attached or hardware accelerated cannot create a display list.
11673 * This method checks these conditions and returns the appropriate result.
11674 *
11675 * @return true if view has the ability to create a display list, false otherwise.
11676 *
11677 * @hide
11678 */
11679 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011680 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011681 }
Joe Malin32736f02011-01-19 16:14:20 -080011682
Chet Haasedaf98e92011-01-10 14:10:36 -080011683 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011684 * @return The HardwareRenderer associated with that view or null if hardware rendering
11685 * is not supported or this this has not been attached to a window.
11686 *
11687 * @hide
11688 */
11689 public HardwareRenderer getHardwareRenderer() {
11690 if (mAttachInfo != null) {
11691 return mAttachInfo.mHardwareRenderer;
11692 }
11693 return null;
11694 }
11695
11696 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011697 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11698 * Otherwise, the same display list will be returned (after having been rendered into
11699 * along the way, depending on the invalidation state of the view).
11700 *
11701 * @param displayList The previous version of this displayList, could be null.
11702 * @param isLayer Whether the requester of the display list is a layer. If so,
11703 * the view will avoid creating a layer inside the resulting display list.
11704 * @return A new or reused DisplayList object.
11705 */
11706 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11707 if (!canHaveDisplayList()) {
11708 return null;
11709 }
11710
11711 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11712 displayList == null || !displayList.isValid() ||
11713 (!isLayer && mRecreateDisplayList))) {
11714 // Don't need to recreate the display list, just need to tell our
11715 // children to restore/recreate theirs
11716 if (displayList != null && displayList.isValid() &&
11717 !isLayer && !mRecreateDisplayList) {
11718 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11719 mPrivateFlags &= ~DIRTY_MASK;
11720 dispatchGetDisplayList();
11721
11722 return displayList;
11723 }
11724
11725 if (!isLayer) {
11726 // If we got here, we're recreating it. Mark it as such to ensure that
11727 // we copy in child display lists into ours in drawChild()
11728 mRecreateDisplayList = true;
11729 }
11730 if (displayList == null) {
11731 final String name = getClass().getSimpleName();
11732 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11733 // If we're creating a new display list, make sure our parent gets invalidated
11734 // since they will need to recreate their display list to account for this
11735 // new child display list.
11736 invalidateParentCaches();
11737 }
11738
11739 boolean caching = false;
11740 final HardwareCanvas canvas = displayList.start();
11741 int restoreCount = 0;
11742 int width = mRight - mLeft;
11743 int height = mBottom - mTop;
11744
11745 try {
11746 canvas.setViewport(width, height);
11747 // The dirty rect should always be null for a display list
11748 canvas.onPreDraw(null);
11749 int layerType = (
11750 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11751 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011752 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11753 if (layerType == LAYER_TYPE_HARDWARE) {
11754 final HardwareLayer layer = getHardwareLayer();
11755 if (layer != null && layer.isValid()) {
11756 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11757 } else {
11758 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11759 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11760 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11761 }
11762 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011763 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011764 buildDrawingCache(true);
11765 Bitmap cache = getDrawingCache(true);
11766 if (cache != null) {
11767 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11768 caching = true;
11769 }
Chet Haasea1cff502012-02-21 13:43:44 -080011770 }
Chet Haasea1cff502012-02-21 13:43:44 -080011771 } else {
11772
11773 computeScroll();
11774
11775 if (!USE_DISPLAY_LIST_PROPERTIES) {
11776 restoreCount = canvas.save();
11777 }
11778 canvas.translate(-mScrollX, -mScrollY);
11779 if (!isLayer) {
11780 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11781 mPrivateFlags &= ~DIRTY_MASK;
11782 }
11783
11784 // Fast path for layouts with no backgrounds
11785 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11786 dispatchDraw(canvas);
11787 } else {
11788 draw(canvas);
11789 }
11790 }
11791 } finally {
11792 if (USE_DISPLAY_LIST_PROPERTIES) {
11793 canvas.restoreToCount(restoreCount);
11794 }
11795 canvas.onPostDraw();
11796
11797 displayList.end();
11798 if (USE_DISPLAY_LIST_PROPERTIES) {
11799 displayList.setCaching(caching);
11800 }
11801 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11802 displayList.setLeftTopRightBottom(0, 0, width, height);
11803 } else {
11804 setDisplayListProperties(displayList);
11805 }
11806 }
11807 } else if (!isLayer) {
11808 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11809 mPrivateFlags &= ~DIRTY_MASK;
11810 }
11811
11812 return displayList;
11813 }
11814
11815 /**
11816 * Get the DisplayList for the HardwareLayer
11817 *
11818 * @param layer The HardwareLayer whose DisplayList we want
11819 * @return A DisplayList fopr the specified HardwareLayer
11820 */
11821 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11822 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11823 layer.setDisplayList(displayList);
11824 return displayList;
11825 }
11826
11827
11828 /**
Romain Guyb051e892010-09-28 19:09:36 -070011829 * <p>Returns a display list that can be used to draw this view again
11830 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011831 *
Romain Guyb051e892010-09-28 19:09:36 -070011832 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011833 *
11834 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011835 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011836 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011837 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011838 return mDisplayList;
11839 }
11840
11841 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011842 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011843 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011844 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011845 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011846 * @see #getDrawingCache(boolean)
11847 */
11848 public Bitmap getDrawingCache() {
11849 return getDrawingCache(false);
11850 }
11851
11852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11854 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11855 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11856 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11857 * request the drawing cache by calling this method and draw it on screen if the
11858 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011859 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011860 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11861 * this method will create a bitmap of the same size as this view. Because this bitmap
11862 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11863 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11864 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11865 * size than the view. This implies that your application must be able to handle this
11866 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011867 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011868 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11869 * the current density of the screen when the application is in compatibility
11870 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011871 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011872 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011873 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874 * @see #setDrawingCacheEnabled(boolean)
11875 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011876 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877 * @see #destroyDrawingCache()
11878 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011879 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011880 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11881 return null;
11882 }
11883 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011884 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011885 }
Romain Guy02890fd2010-08-06 17:58:44 -070011886 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 }
11888
11889 /**
11890 * <p>Frees the resources used by the drawing cache. If you call
11891 * {@link #buildDrawingCache()} manually without calling
11892 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11893 * should cleanup the cache with this method afterwards.</p>
11894 *
11895 * @see #setDrawingCacheEnabled(boolean)
11896 * @see #buildDrawingCache()
11897 * @see #getDrawingCache()
11898 */
11899 public void destroyDrawingCache() {
11900 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011901 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011902 mDrawingCache = null;
11903 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011904 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011905 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011906 mUnscaledDrawingCache = null;
11907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011908 }
11909
11910 /**
11911 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011912 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011913 * view will always be drawn on top of a solid color.
11914 *
11915 * @param color The background color to use for the drawing cache's bitmap
11916 *
11917 * @see #setDrawingCacheEnabled(boolean)
11918 * @see #buildDrawingCache()
11919 * @see #getDrawingCache()
11920 */
11921 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011922 if (color != mDrawingCacheBackgroundColor) {
11923 mDrawingCacheBackgroundColor = color;
11924 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 }
11927
11928 /**
11929 * @see #setDrawingCacheBackgroundColor(int)
11930 *
11931 * @return The background color to used for the drawing cache's bitmap
11932 */
11933 public int getDrawingCacheBackgroundColor() {
11934 return mDrawingCacheBackgroundColor;
11935 }
11936
11937 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011938 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011939 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011940 * @see #buildDrawingCache(boolean)
11941 */
11942 public void buildDrawingCache() {
11943 buildDrawingCache(false);
11944 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011945
Romain Guyfbd8f692009-06-26 14:51:58 -070011946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011947 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11948 *
11949 * <p>If you call {@link #buildDrawingCache()} manually without calling
11950 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11951 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011952 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011953 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11954 * this method will create a bitmap of the same size as this view. Because this bitmap
11955 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11956 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11957 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11958 * size than the view. This implies that your application must be able to handle this
11959 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011960 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011961 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11962 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011963 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011964 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011965 *
11966 * @see #getDrawingCache()
11967 * @see #destroyDrawingCache()
11968 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011969 public void buildDrawingCache(boolean autoScale) {
11970 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011971 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011972 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011973
11974 if (ViewDebug.TRACE_HIERARCHY) {
11975 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011977
Romain Guy8506ab42009-06-11 17:35:47 -070011978 int width = mRight - mLeft;
11979 int height = mBottom - mTop;
11980
11981 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011982 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011983
Romain Guye1123222009-06-29 14:24:56 -070011984 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011985 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11986 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988
11989 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011990 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011991 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992
11993 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011994 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011995 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11997 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011998 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 return;
12000 }
12001
12002 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012003 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012004
12005 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012006 Bitmap.Config quality;
12007 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012008 // Never pick ARGB_4444 because it looks awful
12009 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12011 case DRAWING_CACHE_QUALITY_AUTO:
12012 quality = Bitmap.Config.ARGB_8888;
12013 break;
12014 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012015 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012016 break;
12017 case DRAWING_CACHE_QUALITY_HIGH:
12018 quality = Bitmap.Config.ARGB_8888;
12019 break;
12020 default:
12021 quality = Bitmap.Config.ARGB_8888;
12022 break;
12023 }
12024 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012025 // Optimization for translucent windows
12026 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012027 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012028 }
12029
12030 // Try to cleanup memory
12031 if (bitmap != null) bitmap.recycle();
12032
12033 try {
12034 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012035 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012036 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012037 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012038 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012039 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012040 }
Adam Powell26153a32010-11-08 15:22:27 -080012041 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 } catch (OutOfMemoryError e) {
12043 // If there is not enough memory to create the bitmap cache, just
12044 // ignore the issue as bitmap caches are not required to draw the
12045 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012046 if (autoScale) {
12047 mDrawingCache = null;
12048 } else {
12049 mUnscaledDrawingCache = null;
12050 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012051 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012052 return;
12053 }
12054
12055 clear = drawingCacheBackgroundColor != 0;
12056 }
12057
12058 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012059 if (attachInfo != null) {
12060 canvas = attachInfo.mCanvas;
12061 if (canvas == null) {
12062 canvas = new Canvas();
12063 }
12064 canvas.setBitmap(bitmap);
12065 // Temporarily clobber the cached Canvas in case one of our children
12066 // is also using a drawing cache. Without this, the children would
12067 // steal the canvas by attaching their own bitmap to it and bad, bad
12068 // thing would happen (invisible views, corrupted drawings, etc.)
12069 attachInfo.mCanvas = null;
12070 } else {
12071 // This case should hopefully never or seldom happen
12072 canvas = new Canvas(bitmap);
12073 }
12074
12075 if (clear) {
12076 bitmap.eraseColor(drawingCacheBackgroundColor);
12077 }
12078
12079 computeScroll();
12080 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012081
Romain Guye1123222009-06-29 14:24:56 -070012082 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012083 final float scale = attachInfo.mApplicationScale;
12084 canvas.scale(scale, scale);
12085 }
Joe Malin32736f02011-01-19 16:14:20 -080012086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087 canvas.translate(-mScrollX, -mScrollY);
12088
Romain Guy5bcdff42009-05-14 21:27:18 -070012089 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012090 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12091 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012092 mPrivateFlags |= DRAWING_CACHE_VALID;
12093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012094
12095 // Fast path for layouts with no backgrounds
12096 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12097 if (ViewDebug.TRACE_HIERARCHY) {
12098 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12099 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012100 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012101 dispatchDraw(canvas);
12102 } else {
12103 draw(canvas);
12104 }
12105
12106 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012107 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012108
12109 if (attachInfo != null) {
12110 // Restore the cached Canvas for our siblings
12111 attachInfo.mCanvas = canvas;
12112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 }
12114 }
12115
12116 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012117 * Create a snapshot of the view into a bitmap. We should probably make
12118 * some form of this public, but should think about the API.
12119 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012120 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012121 int width = mRight - mLeft;
12122 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012123
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012124 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012125 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012126 width = (int) ((width * scale) + 0.5f);
12127 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012128
Romain Guy8c11e312009-09-14 15:15:30 -070012129 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012130 if (bitmap == null) {
12131 throw new OutOfMemoryError();
12132 }
12133
Romain Guyc529d8d2011-09-06 15:01:39 -070012134 Resources resources = getResources();
12135 if (resources != null) {
12136 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12137 }
Joe Malin32736f02011-01-19 16:14:20 -080012138
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012139 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012140 if (attachInfo != null) {
12141 canvas = attachInfo.mCanvas;
12142 if (canvas == null) {
12143 canvas = new Canvas();
12144 }
12145 canvas.setBitmap(bitmap);
12146 // Temporarily clobber the cached Canvas in case one of our children
12147 // is also using a drawing cache. Without this, the children would
12148 // steal the canvas by attaching their own bitmap to it and bad, bad
12149 // things would happen (invisible views, corrupted drawings, etc.)
12150 attachInfo.mCanvas = null;
12151 } else {
12152 // This case should hopefully never or seldom happen
12153 canvas = new Canvas(bitmap);
12154 }
12155
Romain Guy5bcdff42009-05-14 21:27:18 -070012156 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012157 bitmap.eraseColor(backgroundColor);
12158 }
12159
12160 computeScroll();
12161 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012162 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012163 canvas.translate(-mScrollX, -mScrollY);
12164
Romain Guy5bcdff42009-05-14 21:27:18 -070012165 // Temporarily remove the dirty mask
12166 int flags = mPrivateFlags;
12167 mPrivateFlags &= ~DIRTY_MASK;
12168
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012169 // Fast path for layouts with no backgrounds
12170 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12171 dispatchDraw(canvas);
12172 } else {
12173 draw(canvas);
12174 }
12175
Romain Guy5bcdff42009-05-14 21:27:18 -070012176 mPrivateFlags = flags;
12177
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012178 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012179 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012180
12181 if (attachInfo != null) {
12182 // Restore the cached Canvas for our siblings
12183 attachInfo.mCanvas = canvas;
12184 }
Romain Guy8506ab42009-06-11 17:35:47 -070012185
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012186 return bitmap;
12187 }
12188
12189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 * Indicates whether this View is currently in edit mode. A View is usually
12191 * in edit mode when displayed within a developer tool. For instance, if
12192 * this View is being drawn by a visual user interface builder, this method
12193 * should return true.
12194 *
12195 * Subclasses should check the return value of this method to provide
12196 * different behaviors if their normal behavior might interfere with the
12197 * host environment. For instance: the class spawns a thread in its
12198 * constructor, the drawing code relies on device-specific features, etc.
12199 *
12200 * This method is usually checked in the drawing code of custom widgets.
12201 *
12202 * @return True if this View is in edit mode, false otherwise.
12203 */
12204 public boolean isInEditMode() {
12205 return false;
12206 }
12207
12208 /**
12209 * If the View draws content inside its padding and enables fading edges,
12210 * it needs to support padding offsets. Padding offsets are added to the
12211 * fading edges to extend the length of the fade so that it covers pixels
12212 * drawn inside the padding.
12213 *
12214 * Subclasses of this class should override this method if they need
12215 * to draw content inside the padding.
12216 *
12217 * @return True if padding offset must be applied, false otherwise.
12218 *
12219 * @see #getLeftPaddingOffset()
12220 * @see #getRightPaddingOffset()
12221 * @see #getTopPaddingOffset()
12222 * @see #getBottomPaddingOffset()
12223 *
12224 * @since CURRENT
12225 */
12226 protected boolean isPaddingOffsetRequired() {
12227 return false;
12228 }
12229
12230 /**
12231 * Amount by which to extend the left fading region. Called only when
12232 * {@link #isPaddingOffsetRequired()} returns true.
12233 *
12234 * @return The left padding offset in pixels.
12235 *
12236 * @see #isPaddingOffsetRequired()
12237 *
12238 * @since CURRENT
12239 */
12240 protected int getLeftPaddingOffset() {
12241 return 0;
12242 }
12243
12244 /**
12245 * Amount by which to extend the right fading region. Called only when
12246 * {@link #isPaddingOffsetRequired()} returns true.
12247 *
12248 * @return The right padding offset in pixels.
12249 *
12250 * @see #isPaddingOffsetRequired()
12251 *
12252 * @since CURRENT
12253 */
12254 protected int getRightPaddingOffset() {
12255 return 0;
12256 }
12257
12258 /**
12259 * Amount by which to extend the top fading region. Called only when
12260 * {@link #isPaddingOffsetRequired()} returns true.
12261 *
12262 * @return The top padding offset in pixels.
12263 *
12264 * @see #isPaddingOffsetRequired()
12265 *
12266 * @since CURRENT
12267 */
12268 protected int getTopPaddingOffset() {
12269 return 0;
12270 }
12271
12272 /**
12273 * Amount by which to extend the bottom fading region. Called only when
12274 * {@link #isPaddingOffsetRequired()} returns true.
12275 *
12276 * @return The bottom padding offset in pixels.
12277 *
12278 * @see #isPaddingOffsetRequired()
12279 *
12280 * @since CURRENT
12281 */
12282 protected int getBottomPaddingOffset() {
12283 return 0;
12284 }
12285
12286 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012287 * @hide
12288 * @param offsetRequired
12289 */
12290 protected int getFadeTop(boolean offsetRequired) {
12291 int top = mPaddingTop;
12292 if (offsetRequired) top += getTopPaddingOffset();
12293 return top;
12294 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012295
Romain Guyf2fc4602011-07-19 15:20:03 -070012296 /**
12297 * @hide
12298 * @param offsetRequired
12299 */
12300 protected int getFadeHeight(boolean offsetRequired) {
12301 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012302 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012303 return mBottom - mTop - mPaddingBottom - padding;
12304 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012305
Romain Guyf2fc4602011-07-19 15:20:03 -070012306 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012307 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012308 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012309 *
Romain Guy2bffd262010-09-12 17:40:02 -070012310 * <p>Even if this method returns true, it does not mean that every call
12311 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12312 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012313 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012314 * window is hardware accelerated,
12315 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12316 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012317 *
Romain Guy2bffd262010-09-12 17:40:02 -070012318 * @return True if the view is attached to a window and the window is
12319 * hardware accelerated; false in any other case.
12320 */
12321 public boolean isHardwareAccelerated() {
12322 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12323 }
Joe Malin32736f02011-01-19 16:14:20 -080012324
Romain Guy2bffd262010-09-12 17:40:02 -070012325 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012326 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12327 * case of an active Animation being run on the view.
12328 */
12329 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12330 Animation a, boolean scalingRequired) {
12331 Transformation invalidationTransform;
12332 final int flags = parent.mGroupFlags;
12333 final boolean initialized = a.isInitialized();
12334 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012335 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012336 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12337 onAnimationStart();
12338 }
12339
12340 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12341 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12342 if (parent.mInvalidationTransformation == null) {
12343 parent.mInvalidationTransformation = new Transformation();
12344 }
12345 invalidationTransform = parent.mInvalidationTransformation;
12346 a.getTransformation(drawingTime, invalidationTransform, 1f);
12347 } else {
12348 invalidationTransform = parent.mChildTransformation;
12349 }
12350 if (more) {
12351 if (!a.willChangeBounds()) {
12352 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12353 parent.FLAG_OPTIMIZE_INVALIDATE) {
12354 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12355 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12356 // The child need to draw an animation, potentially offscreen, so
12357 // make sure we do not cancel invalidate requests
12358 parent.mPrivateFlags |= DRAW_ANIMATION;
12359 parent.invalidate(mLeft, mTop, mRight, mBottom);
12360 }
12361 } else {
12362 if (parent.mInvalidateRegion == null) {
12363 parent.mInvalidateRegion = new RectF();
12364 }
12365 final RectF region = parent.mInvalidateRegion;
12366 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12367 invalidationTransform);
12368
12369 // The child need to draw an animation, potentially offscreen, so
12370 // make sure we do not cancel invalidate requests
12371 parent.mPrivateFlags |= DRAW_ANIMATION;
12372
12373 final int left = mLeft + (int) region.left;
12374 final int top = mTop + (int) region.top;
12375 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12376 top + (int) (region.height() + .5f));
12377 }
12378 }
12379 return more;
12380 }
12381
Chet Haasea1cff502012-02-21 13:43:44 -080012382 void setDisplayListProperties() {
12383 setDisplayListProperties(mDisplayList);
12384 }
12385
12386 /**
12387 * This method is called by getDisplayList() when a display list is created or re-rendered.
12388 * It sets or resets the current value of all properties on that display list (resetting is
12389 * necessary when a display list is being re-created, because we need to make sure that
12390 * previously-set transform values
12391 */
12392 void setDisplayListProperties(DisplayList displayList) {
12393 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
12394 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012395 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012396 if (mParent instanceof ViewGroup) {
12397 displayList.setClipChildren(
12398 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12399 }
Chet Haase9420abd2012-03-29 16:28:32 -070012400 float alpha = 1;
12401 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12402 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12403 ViewGroup parentVG = (ViewGroup) mParent;
12404 final boolean hasTransform =
12405 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12406 if (hasTransform) {
12407 Transformation transform = parentVG.mChildTransformation;
12408 final int transformType = parentVG.mChildTransformation.getTransformationType();
12409 if (transformType != Transformation.TYPE_IDENTITY) {
12410 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12411 alpha = transform.getAlpha();
12412 }
12413 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12414 displayList.setStaticMatrix(transform.getMatrix());
12415 }
12416 }
12417 }
Chet Haasea1cff502012-02-21 13:43:44 -080012418 }
12419 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012420 alpha *= mTransformationInfo.mAlpha;
12421 if (alpha < 1) {
12422 final int multipliedAlpha = (int) (255 * alpha);
12423 if (onSetAlpha(multipliedAlpha)) {
12424 alpha = 1;
12425 }
12426 }
12427 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012428 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12429 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12430 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12431 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012432 if (mTransformationInfo.mCamera == null) {
12433 mTransformationInfo.mCamera = new Camera();
12434 mTransformationInfo.matrix3D = new Matrix();
12435 }
12436 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012437 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12438 displayList.setPivotX(getPivotX());
12439 displayList.setPivotY(getPivotY());
12440 }
Chet Haase9420abd2012-03-29 16:28:32 -070012441 } else if (alpha < 1) {
12442 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012443 }
12444 }
12445 }
12446
Chet Haasebcca79a2012-02-14 08:45:14 -080012447 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012448 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12449 * This draw() method is an implementation detail and is not intended to be overridden or
12450 * to be called from anywhere else other than ViewGroup.drawChild().
12451 */
12452 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080012453 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
12454 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012455 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012456 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012457 final int flags = parent.mGroupFlags;
12458
Chet Haasea1cff502012-02-21 13:43:44 -080012459 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012460 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012461 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012462 }
12463
12464 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012465 boolean concatMatrix = false;
12466
12467 boolean scalingRequired = false;
12468 boolean caching;
12469 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12470
12471 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012472 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12473 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012474 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012475 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012476 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12477 } else {
12478 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12479 }
12480
Chet Haasebcca79a2012-02-14 08:45:14 -080012481 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012482 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012483 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012484 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012485 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012486 } else if (!useDisplayListProperties &&
12487 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012488 final boolean hasTransform =
12489 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012490 if (hasTransform) {
12491 final int transformType = parent.mChildTransformation.getTransformationType();
12492 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12493 parent.mChildTransformation : null;
12494 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12495 }
12496 }
12497
12498 concatMatrix |= !childHasIdentityMatrix;
12499
12500 // Sets the flag as early as possible to allow draw() implementations
12501 // to call invalidate() successfully when doing animations
12502 mPrivateFlags |= DRAWN;
12503
Chet Haasebcca79a2012-02-14 08:45:14 -080012504 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012505 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12506 return more;
12507 }
12508
12509 if (hardwareAccelerated) {
12510 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12511 // retain the flag's value temporarily in the mRecreateDisplayList flag
12512 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12513 mPrivateFlags &= ~INVALIDATED;
12514 }
12515
12516 computeScroll();
12517
12518 final int sx = mScrollX;
12519 final int sy = mScrollY;
12520
12521 DisplayList displayList = null;
12522 Bitmap cache = null;
12523 boolean hasDisplayList = false;
12524 if (caching) {
12525 if (!hardwareAccelerated) {
12526 if (layerType != LAYER_TYPE_NONE) {
12527 layerType = LAYER_TYPE_SOFTWARE;
12528 buildDrawingCache(true);
12529 }
12530 cache = getDrawingCache(true);
12531 } else {
12532 switch (layerType) {
12533 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012534 if (useDisplayListProperties) {
12535 hasDisplayList = canHaveDisplayList();
12536 } else {
12537 buildDrawingCache(true);
12538 cache = getDrawingCache(true);
12539 }
Chet Haase64a48c12012-02-13 16:33:29 -080012540 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012541 case LAYER_TYPE_HARDWARE:
12542 if (useDisplayListProperties) {
12543 hasDisplayList = canHaveDisplayList();
12544 }
12545 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012546 case LAYER_TYPE_NONE:
12547 // Delay getting the display list until animation-driven alpha values are
12548 // set up and possibly passed on to the view
12549 hasDisplayList = canHaveDisplayList();
12550 break;
12551 }
12552 }
12553 }
Chet Haasea1cff502012-02-21 13:43:44 -080012554 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012555 if (useDisplayListProperties) {
12556 displayList = getDisplayList();
12557 if (!displayList.isValid()) {
12558 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12559 // to getDisplayList(), the display list will be marked invalid and we should not
12560 // try to use it again.
12561 displayList = null;
12562 hasDisplayList = false;
12563 useDisplayListProperties = false;
12564 }
12565 }
Chet Haase64a48c12012-02-13 16:33:29 -080012566
12567 final boolean hasNoCache = cache == null || hasDisplayList;
12568 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12569 layerType != LAYER_TYPE_HARDWARE;
12570
Chet Haasea1cff502012-02-21 13:43:44 -080012571 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012572 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012573 restoreTo = canvas.save();
12574 }
Chet Haase64a48c12012-02-13 16:33:29 -080012575 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012576 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012577 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012578 if (!useDisplayListProperties) {
12579 canvas.translate(mLeft, mTop);
12580 }
Chet Haase64a48c12012-02-13 16:33:29 -080012581 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012582 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012583 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012584 restoreTo = canvas.save();
12585 }
Chet Haase64a48c12012-02-13 16:33:29 -080012586 // mAttachInfo cannot be null, otherwise scalingRequired == false
12587 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12588 canvas.scale(scale, scale);
12589 }
12590 }
12591
Chet Haasea1cff502012-02-21 13:43:44 -080012592 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012593 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012594 if (transformToApply != null || !childHasIdentityMatrix) {
12595 int transX = 0;
12596 int transY = 0;
12597
12598 if (offsetForScroll) {
12599 transX = -sx;
12600 transY = -sy;
12601 }
12602
12603 if (transformToApply != null) {
12604 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012605 if (useDisplayListProperties) {
12606 displayList.setAnimationMatrix(transformToApply.getMatrix());
12607 } else {
12608 // Undo the scroll translation, apply the transformation matrix,
12609 // then redo the scroll translate to get the correct result.
12610 canvas.translate(-transX, -transY);
12611 canvas.concat(transformToApply.getMatrix());
12612 canvas.translate(transX, transY);
12613 }
Chet Haasea1cff502012-02-21 13:43:44 -080012614 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012615 }
12616
12617 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012618 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012619 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012620 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012621 }
12622 }
12623
Chet Haasea1cff502012-02-21 13:43:44 -080012624 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012625 canvas.translate(-transX, -transY);
12626 canvas.concat(getMatrix());
12627 canvas.translate(transX, transY);
12628 }
12629 }
12630
Chet Haase9420abd2012-03-29 16:28:32 -070012631 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012632 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012633 if (hasNoCache) {
12634 final int multipliedAlpha = (int) (255 * alpha);
12635 if (!onSetAlpha(multipliedAlpha)) {
12636 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012637 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012638 layerType != LAYER_TYPE_NONE) {
12639 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12640 }
Chet Haase9420abd2012-03-29 16:28:32 -070012641 if (useDisplayListProperties) {
12642 displayList.setAlpha(alpha * getAlpha());
12643 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012644 final int scrollX = hasDisplayList ? 0 : sx;
12645 final int scrollY = hasDisplayList ? 0 : sy;
12646 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12647 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012648 }
12649 } else {
12650 // Alpha is handled by the child directly, clobber the layer's alpha
12651 mPrivateFlags |= ALPHA_SET;
12652 }
12653 }
12654 }
12655 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12656 onSetAlpha(255);
12657 mPrivateFlags &= ~ALPHA_SET;
12658 }
12659
Chet Haasea1cff502012-02-21 13:43:44 -080012660 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12661 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012662 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012663 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012664 } else {
12665 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012666 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012667 } else {
12668 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12669 }
12670 }
12671 }
12672
Chet Haase9420abd2012-03-29 16:28:32 -070012673 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012674 displayList = getDisplayList();
12675 if (!displayList.isValid()) {
12676 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12677 // to getDisplayList(), the display list will be marked invalid and we should not
12678 // try to use it again.
12679 displayList = null;
12680 hasDisplayList = false;
12681 }
12682 }
12683
12684 if (hasNoCache) {
12685 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012686 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012687 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012688 if (layer != null && layer.isValid()) {
12689 mLayerPaint.setAlpha((int) (alpha * 255));
12690 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12691 layerRendered = true;
12692 } else {
12693 final int scrollX = hasDisplayList ? 0 : sx;
12694 final int scrollY = hasDisplayList ? 0 : sy;
12695 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012696 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012697 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12698 }
12699 }
12700
12701 if (!layerRendered) {
12702 if (!hasDisplayList) {
12703 // Fast path for layouts with no backgrounds
12704 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12705 if (ViewDebug.TRACE_HIERARCHY) {
12706 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12707 }
12708 mPrivateFlags &= ~DIRTY_MASK;
12709 dispatchDraw(canvas);
12710 } else {
12711 draw(canvas);
12712 }
12713 } else {
12714 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012715 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012716 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012717 }
12718 }
12719 } else if (cache != null) {
12720 mPrivateFlags &= ~DIRTY_MASK;
12721 Paint cachePaint;
12722
12723 if (layerType == LAYER_TYPE_NONE) {
12724 cachePaint = parent.mCachePaint;
12725 if (cachePaint == null) {
12726 cachePaint = new Paint();
12727 cachePaint.setDither(false);
12728 parent.mCachePaint = cachePaint;
12729 }
Chet Haase9420abd2012-03-29 16:28:32 -070012730 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012731 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012732 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12733 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012734 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012735 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012736 }
12737 } else {
12738 cachePaint = mLayerPaint;
12739 cachePaint.setAlpha((int) (alpha * 255));
12740 }
12741 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12742 }
12743
Chet Haasea1cff502012-02-21 13:43:44 -080012744 if (restoreTo >= 0) {
12745 canvas.restoreToCount(restoreTo);
12746 }
Chet Haase64a48c12012-02-13 16:33:29 -080012747
12748 if (a != null && !more) {
12749 if (!hardwareAccelerated && !a.getFillAfter()) {
12750 onSetAlpha(255);
12751 }
12752 parent.finishAnimatingView(this, a);
12753 }
12754
12755 if (more && hardwareAccelerated) {
12756 // invalidation is the trigger to recreate display lists, so if we're using
12757 // display lists to render, force an invalidate to allow the animation to
12758 // continue drawing another frame
12759 parent.invalidate(true);
12760 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12761 // alpha animations should cause the child to recreate its display list
12762 invalidate(true);
12763 }
12764 }
12765
12766 mRecreateDisplayList = false;
12767
12768 return more;
12769 }
12770
12771 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012772 * Manually render this view (and all of its children) to the given Canvas.
12773 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012774 * called. When implementing a view, implement
12775 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12776 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 *
12778 * @param canvas The Canvas to which the View is rendered.
12779 */
12780 public void draw(Canvas canvas) {
12781 if (ViewDebug.TRACE_HIERARCHY) {
12782 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12783 }
12784
Romain Guy5bcdff42009-05-14 21:27:18 -070012785 final int privateFlags = mPrivateFlags;
12786 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12787 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12788 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 /*
12791 * Draw traversal performs several drawing steps which must be executed
12792 * in the appropriate order:
12793 *
12794 * 1. Draw the background
12795 * 2. If necessary, save the canvas' layers to prepare for fading
12796 * 3. Draw view's content
12797 * 4. Draw children
12798 * 5. If necessary, draw the fading edges and restore layers
12799 * 6. Draw decorations (scrollbars for instance)
12800 */
12801
12802 // Step 1, draw the background, if needed
12803 int saveCount;
12804
Romain Guy24443ea2009-05-11 11:56:30 -070012805 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012806 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012807 if (background != null) {
12808 final int scrollX = mScrollX;
12809 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810
Romain Guy24443ea2009-05-11 11:56:30 -070012811 if (mBackgroundSizeChanged) {
12812 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12813 mBackgroundSizeChanged = false;
12814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815
Romain Guy24443ea2009-05-11 11:56:30 -070012816 if ((scrollX | scrollY) == 0) {
12817 background.draw(canvas);
12818 } else {
12819 canvas.translate(scrollX, scrollY);
12820 background.draw(canvas);
12821 canvas.translate(-scrollX, -scrollY);
12822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823 }
12824 }
12825
12826 // skip step 2 & 5 if possible (common case)
12827 final int viewFlags = mViewFlags;
12828 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12829 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12830 if (!verticalEdges && !horizontalEdges) {
12831 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012832 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833
12834 // Step 4, draw the children
12835 dispatchDraw(canvas);
12836
12837 // Step 6, draw decorations (scrollbars)
12838 onDrawScrollBars(canvas);
12839
12840 // we're done...
12841 return;
12842 }
12843
12844 /*
12845 * Here we do the full fledged routine...
12846 * (this is an uncommon case where speed matters less,
12847 * this is why we repeat some of the tests that have been
12848 * done above)
12849 */
12850
12851 boolean drawTop = false;
12852 boolean drawBottom = false;
12853 boolean drawLeft = false;
12854 boolean drawRight = false;
12855
12856 float topFadeStrength = 0.0f;
12857 float bottomFadeStrength = 0.0f;
12858 float leftFadeStrength = 0.0f;
12859 float rightFadeStrength = 0.0f;
12860
12861 // Step 2, save the canvas' layers
12862 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012863
12864 final boolean offsetRequired = isPaddingOffsetRequired();
12865 if (offsetRequired) {
12866 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012867 }
12868
12869 int left = mScrollX + paddingLeft;
12870 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012871 int top = mScrollY + getFadeTop(offsetRequired);
12872 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873
12874 if (offsetRequired) {
12875 right += getRightPaddingOffset();
12876 bottom += getBottomPaddingOffset();
12877 }
12878
12879 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012880 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012881 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882
12883 // clip the fade length if top and bottom fades overlap
12884 // overlapping fades produce odd-looking artifacts
12885 if (verticalEdges && (top + length > bottom - length)) {
12886 length = (bottom - top) / 2;
12887 }
12888
12889 // also clip horizontal fades if necessary
12890 if (horizontalEdges && (left + length > right - length)) {
12891 length = (right - left) / 2;
12892 }
12893
12894 if (verticalEdges) {
12895 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012896 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012897 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012898 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012899 }
12900
12901 if (horizontalEdges) {
12902 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012903 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012904 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012905 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906 }
12907
12908 saveCount = canvas.getSaveCount();
12909
12910 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012911 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012912 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12913
12914 if (drawTop) {
12915 canvas.saveLayer(left, top, right, top + length, null, flags);
12916 }
12917
12918 if (drawBottom) {
12919 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12920 }
12921
12922 if (drawLeft) {
12923 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12924 }
12925
12926 if (drawRight) {
12927 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12928 }
12929 } else {
12930 scrollabilityCache.setFadeColor(solidColor);
12931 }
12932
12933 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012934 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012935
12936 // Step 4, draw the children
12937 dispatchDraw(canvas);
12938
12939 // Step 5, draw the fade effect and restore layers
12940 final Paint p = scrollabilityCache.paint;
12941 final Matrix matrix = scrollabilityCache.matrix;
12942 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012943
12944 if (drawTop) {
12945 matrix.setScale(1, fadeHeight * topFadeStrength);
12946 matrix.postTranslate(left, top);
12947 fade.setLocalMatrix(matrix);
12948 canvas.drawRect(left, top, right, top + length, p);
12949 }
12950
12951 if (drawBottom) {
12952 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12953 matrix.postRotate(180);
12954 matrix.postTranslate(left, bottom);
12955 fade.setLocalMatrix(matrix);
12956 canvas.drawRect(left, bottom - length, right, bottom, p);
12957 }
12958
12959 if (drawLeft) {
12960 matrix.setScale(1, fadeHeight * leftFadeStrength);
12961 matrix.postRotate(-90);
12962 matrix.postTranslate(left, top);
12963 fade.setLocalMatrix(matrix);
12964 canvas.drawRect(left, top, left + length, bottom, p);
12965 }
12966
12967 if (drawRight) {
12968 matrix.setScale(1, fadeHeight * rightFadeStrength);
12969 matrix.postRotate(90);
12970 matrix.postTranslate(right, top);
12971 fade.setLocalMatrix(matrix);
12972 canvas.drawRect(right - length, top, right, bottom, p);
12973 }
12974
12975 canvas.restoreToCount(saveCount);
12976
12977 // Step 6, draw decorations (scrollbars)
12978 onDrawScrollBars(canvas);
12979 }
12980
12981 /**
12982 * Override this if your view is known to always be drawn on top of a solid color background,
12983 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12984 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12985 * should be set to 0xFF.
12986 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012987 * @see #setVerticalFadingEdgeEnabled(boolean)
12988 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012989 *
12990 * @return The known solid color background for this view, or 0 if the color may vary
12991 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012992 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012993 public int getSolidColor() {
12994 return 0;
12995 }
12996
12997 /**
12998 * Build a human readable string representation of the specified view flags.
12999 *
13000 * @param flags the view flags to convert to a string
13001 * @return a String representing the supplied flags
13002 */
13003 private static String printFlags(int flags) {
13004 String output = "";
13005 int numFlags = 0;
13006 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13007 output += "TAKES_FOCUS";
13008 numFlags++;
13009 }
13010
13011 switch (flags & VISIBILITY_MASK) {
13012 case INVISIBLE:
13013 if (numFlags > 0) {
13014 output += " ";
13015 }
13016 output += "INVISIBLE";
13017 // USELESS HERE numFlags++;
13018 break;
13019 case GONE:
13020 if (numFlags > 0) {
13021 output += " ";
13022 }
13023 output += "GONE";
13024 // USELESS HERE numFlags++;
13025 break;
13026 default:
13027 break;
13028 }
13029 return output;
13030 }
13031
13032 /**
13033 * Build a human readable string representation of the specified private
13034 * view flags.
13035 *
13036 * @param privateFlags the private view flags to convert to a string
13037 * @return a String representing the supplied flags
13038 */
13039 private static String printPrivateFlags(int privateFlags) {
13040 String output = "";
13041 int numFlags = 0;
13042
13043 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13044 output += "WANTS_FOCUS";
13045 numFlags++;
13046 }
13047
13048 if ((privateFlags & FOCUSED) == FOCUSED) {
13049 if (numFlags > 0) {
13050 output += " ";
13051 }
13052 output += "FOCUSED";
13053 numFlags++;
13054 }
13055
13056 if ((privateFlags & SELECTED) == SELECTED) {
13057 if (numFlags > 0) {
13058 output += " ";
13059 }
13060 output += "SELECTED";
13061 numFlags++;
13062 }
13063
13064 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13065 if (numFlags > 0) {
13066 output += " ";
13067 }
13068 output += "IS_ROOT_NAMESPACE";
13069 numFlags++;
13070 }
13071
13072 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13073 if (numFlags > 0) {
13074 output += " ";
13075 }
13076 output += "HAS_BOUNDS";
13077 numFlags++;
13078 }
13079
13080 if ((privateFlags & DRAWN) == DRAWN) {
13081 if (numFlags > 0) {
13082 output += " ";
13083 }
13084 output += "DRAWN";
13085 // USELESS HERE numFlags++;
13086 }
13087 return output;
13088 }
13089
13090 /**
13091 * <p>Indicates whether or not this view's layout will be requested during
13092 * the next hierarchy layout pass.</p>
13093 *
13094 * @return true if the layout will be forced during next layout pass
13095 */
13096 public boolean isLayoutRequested() {
13097 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13098 }
13099
13100 /**
13101 * Assign a size and position to a view and all of its
13102 * descendants
13103 *
13104 * <p>This is the second phase of the layout mechanism.
13105 * (The first is measuring). In this phase, each parent calls
13106 * layout on all of its children to position them.
13107 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013108 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013109 *
Chet Haase9c087442011-01-12 16:20:16 -080013110 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013111 * Derived classes with children should override
13112 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013113 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013114 *
13115 * @param l Left position, relative to parent
13116 * @param t Top position, relative to parent
13117 * @param r Right position, relative to parent
13118 * @param b Bottom position, relative to parent
13119 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013120 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013121 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013122 int oldL = mLeft;
13123 int oldT = mTop;
13124 int oldB = mBottom;
13125 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013126 boolean changed = setFrame(l, t, r, b);
13127 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13128 if (ViewDebug.TRACE_HIERARCHY) {
13129 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13130 }
13131
13132 onLayout(changed, l, t, r, b);
13133 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013134
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013135 ListenerInfo li = mListenerInfo;
13136 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013137 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013138 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013139 int numListeners = listenersCopy.size();
13140 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013141 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013142 }
13143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013144 }
13145 mPrivateFlags &= ~FORCE_LAYOUT;
13146 }
13147
13148 /**
13149 * Called from layout when this view should
13150 * assign a size and position to each of its children.
13151 *
13152 * Derived classes with children should override
13153 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013154 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013155 * @param changed This is a new size or position for this view
13156 * @param left Left position, relative to parent
13157 * @param top Top position, relative to parent
13158 * @param right Right position, relative to parent
13159 * @param bottom Bottom position, relative to parent
13160 */
13161 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13162 }
13163
13164 /**
13165 * Assign a size and position to this view.
13166 *
13167 * This is called from layout.
13168 *
13169 * @param left Left position, relative to parent
13170 * @param top Top position, relative to parent
13171 * @param right Right position, relative to parent
13172 * @param bottom Bottom position, relative to parent
13173 * @return true if the new size and position are different than the
13174 * previous ones
13175 * {@hide}
13176 */
13177 protected boolean setFrame(int left, int top, int right, int bottom) {
13178 boolean changed = false;
13179
13180 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013181 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013182 + right + "," + bottom + ")");
13183 }
13184
13185 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13186 changed = true;
13187
13188 // Remember our drawn bit
13189 int drawn = mPrivateFlags & DRAWN;
13190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013191 int oldWidth = mRight - mLeft;
13192 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013193 int newWidth = right - left;
13194 int newHeight = bottom - top;
13195 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13196
13197 // Invalidate our old position
13198 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013199
13200 mLeft = left;
13201 mTop = top;
13202 mRight = right;
13203 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080013204 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
13205 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013207
13208 mPrivateFlags |= HAS_BOUNDS;
13209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013210
Chet Haase75755e22011-07-18 17:48:25 -070013211 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013212 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13213 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013214 if (mTransformationInfo != null) {
13215 mTransformationInfo.mMatrixDirty = true;
13216 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013218 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13219 }
13220
13221 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13222 // If we are visible, force the DRAWN bit to on so that
13223 // this invalidate will go through (at least to our parent).
13224 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013225 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013226 // the DRAWN bit.
13227 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013228 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013229 // parent display list may need to be recreated based on a change in the bounds
13230 // of any child
13231 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013232 }
13233
13234 // Reset drawn bit to original value (invalidate turns it off)
13235 mPrivateFlags |= drawn;
13236
13237 mBackgroundSizeChanged = true;
13238 }
13239 return changed;
13240 }
13241
13242 /**
13243 * Finalize inflating a view from XML. This is called as the last phase
13244 * of inflation, after all child views have been added.
13245 *
13246 * <p>Even if the subclass overrides onFinishInflate, they should always be
13247 * sure to call the super method, so that we get called.
13248 */
13249 protected void onFinishInflate() {
13250 }
13251
13252 /**
13253 * Returns the resources associated with this view.
13254 *
13255 * @return Resources object.
13256 */
13257 public Resources getResources() {
13258 return mResources;
13259 }
13260
13261 /**
13262 * Invalidates the specified Drawable.
13263 *
13264 * @param drawable the drawable to invalidate
13265 */
13266 public void invalidateDrawable(Drawable drawable) {
13267 if (verifyDrawable(drawable)) {
13268 final Rect dirty = drawable.getBounds();
13269 final int scrollX = mScrollX;
13270 final int scrollY = mScrollY;
13271
13272 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13273 dirty.right + scrollX, dirty.bottom + scrollY);
13274 }
13275 }
13276
13277 /**
13278 * Schedules an action on a drawable to occur at a specified time.
13279 *
13280 * @param who the recipient of the action
13281 * @param what the action to run on the drawable
13282 * @param when the time at which the action must occur. Uses the
13283 * {@link SystemClock#uptimeMillis} timebase.
13284 */
13285 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013286 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013287 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013288 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013289 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13290 Choreographer.CALLBACK_ANIMATION, what, who,
13291 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013292 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013293 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013295 }
13296 }
13297
13298 /**
13299 * Cancels a scheduled action on a drawable.
13300 *
13301 * @param who the recipient of the action
13302 * @param what the action to cancel
13303 */
13304 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013305 if (verifyDrawable(who) && what != null) {
13306 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013307 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13308 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013309 } else {
13310 ViewRootImpl.getRunQueue().removeCallbacks(what);
13311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312 }
13313 }
13314
13315 /**
13316 * Unschedule any events associated with the given Drawable. This can be
13317 * used when selecting a new Drawable into a view, so that the previous
13318 * one is completely unscheduled.
13319 *
13320 * @param who The Drawable to unschedule.
13321 *
13322 * @see #drawableStateChanged
13323 */
13324 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013325 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013326 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13327 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013328 }
13329 }
13330
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013331 /**
13332 * Return the layout direction of a given Drawable.
13333 *
13334 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013335 */
13336 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013337 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013338 }
13339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013340 /**
13341 * If your view subclass is displaying its own Drawable objects, it should
13342 * override this function and return true for any Drawable it is
13343 * displaying. This allows animations for those drawables to be
13344 * scheduled.
13345 *
13346 * <p>Be sure to call through to the super class when overriding this
13347 * function.
13348 *
13349 * @param who The Drawable to verify. Return true if it is one you are
13350 * displaying, else return the result of calling through to the
13351 * super class.
13352 *
13353 * @return boolean If true than the Drawable is being displayed in the
13354 * view; else false and it is not allowed to animate.
13355 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013356 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13357 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013358 */
13359 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013360 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013361 }
13362
13363 /**
13364 * This function is called whenever the state of the view changes in such
13365 * a way that it impacts the state of drawables being shown.
13366 *
13367 * <p>Be sure to call through to the superclass when overriding this
13368 * function.
13369 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013370 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013371 */
13372 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013373 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013374 if (d != null && d.isStateful()) {
13375 d.setState(getDrawableState());
13376 }
13377 }
13378
13379 /**
13380 * Call this to force a view to update its drawable state. This will cause
13381 * drawableStateChanged to be called on this view. Views that are interested
13382 * in the new state should call getDrawableState.
13383 *
13384 * @see #drawableStateChanged
13385 * @see #getDrawableState
13386 */
13387 public void refreshDrawableState() {
13388 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13389 drawableStateChanged();
13390
13391 ViewParent parent = mParent;
13392 if (parent != null) {
13393 parent.childDrawableStateChanged(this);
13394 }
13395 }
13396
13397 /**
13398 * Return an array of resource IDs of the drawable states representing the
13399 * current state of the view.
13400 *
13401 * @return The current drawable state
13402 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013403 * @see Drawable#setState(int[])
13404 * @see #drawableStateChanged()
13405 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013406 */
13407 public final int[] getDrawableState() {
13408 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13409 return mDrawableState;
13410 } else {
13411 mDrawableState = onCreateDrawableState(0);
13412 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13413 return mDrawableState;
13414 }
13415 }
13416
13417 /**
13418 * Generate the new {@link android.graphics.drawable.Drawable} state for
13419 * this view. This is called by the view
13420 * system when the cached Drawable state is determined to be invalid. To
13421 * retrieve the current state, you should use {@link #getDrawableState}.
13422 *
13423 * @param extraSpace if non-zero, this is the number of extra entries you
13424 * would like in the returned array in which you can place your own
13425 * states.
13426 *
13427 * @return Returns an array holding the current {@link Drawable} state of
13428 * the view.
13429 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013430 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013431 */
13432 protected int[] onCreateDrawableState(int extraSpace) {
13433 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13434 mParent instanceof View) {
13435 return ((View) mParent).onCreateDrawableState(extraSpace);
13436 }
13437
13438 int[] drawableState;
13439
13440 int privateFlags = mPrivateFlags;
13441
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013442 int viewStateIndex = 0;
13443 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13444 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13445 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013446 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013447 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13448 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013449 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13450 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013451 // This is set if HW acceleration is requested, even if the current
13452 // process doesn't allow it. This is just to allow app preview
13453 // windows to better match their app.
13454 viewStateIndex |= VIEW_STATE_ACCELERATED;
13455 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013456 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013457
Christopher Tate3d4bf172011-03-28 16:16:46 -070013458 final int privateFlags2 = mPrivateFlags2;
13459 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13460 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013462 drawableState = VIEW_STATE_SETS[viewStateIndex];
13463
13464 //noinspection ConstantIfStatement
13465 if (false) {
13466 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13467 Log.i("View", toString()
13468 + " pressed=" + ((privateFlags & PRESSED) != 0)
13469 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13470 + " fo=" + hasFocus()
13471 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013472 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013473 + ": " + Arrays.toString(drawableState));
13474 }
13475
13476 if (extraSpace == 0) {
13477 return drawableState;
13478 }
13479
13480 final int[] fullState;
13481 if (drawableState != null) {
13482 fullState = new int[drawableState.length + extraSpace];
13483 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13484 } else {
13485 fullState = new int[extraSpace];
13486 }
13487
13488 return fullState;
13489 }
13490
13491 /**
13492 * Merge your own state values in <var>additionalState</var> into the base
13493 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013494 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013495 *
13496 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013497 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013498 * own additional state values.
13499 *
13500 * @param additionalState The additional state values you would like
13501 * added to <var>baseState</var>; this array is not modified.
13502 *
13503 * @return As a convenience, the <var>baseState</var> array you originally
13504 * passed into the function is returned.
13505 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013506 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013507 */
13508 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13509 final int N = baseState.length;
13510 int i = N - 1;
13511 while (i >= 0 && baseState[i] == 0) {
13512 i--;
13513 }
13514 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13515 return baseState;
13516 }
13517
13518 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013519 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13520 * on all Drawable objects associated with this view.
13521 */
13522 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013523 if (mBackground != null) {
13524 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013525 }
13526 }
13527
13528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013529 * Sets the background color for this view.
13530 * @param color the color of the background
13531 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013532 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013533 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013534 if (mBackground instanceof ColorDrawable) {
13535 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013536 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013537 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013539 }
13540
13541 /**
13542 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013543 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013544 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013546 * @attr ref android.R.styleable#View_background
13547 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013548 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013549 public void setBackgroundResource(int resid) {
13550 if (resid != 0 && resid == mBackgroundResource) {
13551 return;
13552 }
13553
13554 Drawable d= null;
13555 if (resid != 0) {
13556 d = mResources.getDrawable(resid);
13557 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013558 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013559
13560 mBackgroundResource = resid;
13561 }
13562
13563 /**
13564 * Set the background to a given Drawable, or remove the background. If the
13565 * background has padding, this View's padding is set to the background's
13566 * padding. However, when a background is removed, this View's padding isn't
13567 * touched. If setting the padding is desired, please use
13568 * {@link #setPadding(int, int, int, int)}.
13569 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013570 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013571 * background
13572 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013573 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013574 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013575 setBackgroundDrawable(background);
13576 }
13577
13578 /**
13579 * @deprecated use {@link #setBackground(Drawable)} instead
13580 */
13581 @Deprecated
13582 public void setBackgroundDrawable(Drawable background) {
13583 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013584 return;
13585 }
13586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013587 boolean requestLayout = false;
13588
13589 mBackgroundResource = 0;
13590
13591 /*
13592 * Regardless of whether we're setting a new background or not, we want
13593 * to clear the previous drawable.
13594 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013595 if (mBackground != null) {
13596 mBackground.setCallback(null);
13597 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013598 }
13599
Philip Milne6c8ea062012-04-03 17:38:43 -070013600 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013601 Rect padding = sThreadLocal.get();
13602 if (padding == null) {
13603 padding = new Rect();
13604 sThreadLocal.set(padding);
13605 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013606 if (background.getPadding(padding)) {
13607 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013608 case LAYOUT_DIRECTION_RTL:
13609 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13610 break;
13611 case LAYOUT_DIRECTION_LTR:
13612 default:
13613 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013615 }
13616
13617 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13618 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013619 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13620 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013621 requestLayout = true;
13622 }
13623
Philip Milne6c8ea062012-04-03 17:38:43 -070013624 background.setCallback(this);
13625 if (background.isStateful()) {
13626 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013627 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013628 background.setVisible(getVisibility() == VISIBLE, false);
13629 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630
13631 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13632 mPrivateFlags &= ~SKIP_DRAW;
13633 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13634 requestLayout = true;
13635 }
13636 } else {
13637 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013638 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013639
13640 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13641 /*
13642 * This view ONLY drew the background before and we're removing
13643 * the background, so now it won't draw anything
13644 * (hence we SKIP_DRAW)
13645 */
13646 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13647 mPrivateFlags |= SKIP_DRAW;
13648 }
13649
13650 /*
13651 * When the background is set, we try to apply its padding to this
13652 * View. When the background is removed, we don't touch this View's
13653 * padding. This is noted in the Javadocs. Hence, we don't need to
13654 * requestLayout(), the invalidate() below is sufficient.
13655 */
13656
13657 // The old background's minimum size could have affected this
13658 // View's layout, so let's requestLayout
13659 requestLayout = true;
13660 }
13661
Romain Guy8f1344f52009-05-15 16:03:59 -070013662 computeOpaqueFlags();
13663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013664 if (requestLayout) {
13665 requestLayout();
13666 }
13667
13668 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013669 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013670 }
13671
13672 /**
13673 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013675 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013676 *
13677 * @see #setBackground(Drawable)
13678 *
13679 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013680 */
13681 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013682 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013683 }
13684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 /**
13686 * Sets the padding. The view may add on the space required to display
13687 * the scrollbars, depending on the style and visibility of the scrollbars.
13688 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13689 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13690 * from the values set in this call.
13691 *
13692 * @attr ref android.R.styleable#View_padding
13693 * @attr ref android.R.styleable#View_paddingBottom
13694 * @attr ref android.R.styleable#View_paddingLeft
13695 * @attr ref android.R.styleable#View_paddingRight
13696 * @attr ref android.R.styleable#View_paddingTop
13697 * @param left the left padding in pixels
13698 * @param top the top padding in pixels
13699 * @param right the right padding in pixels
13700 * @param bottom the bottom padding in pixels
13701 */
13702 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013703 mUserPaddingStart = -1;
13704 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013705 mUserPaddingRelative = false;
13706
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013707 internalSetPadding(left, top, right, bottom);
13708 }
13709
13710 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013711 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013712 mUserPaddingRight = right;
13713 mUserPaddingBottom = bottom;
13714
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013715 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013716 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013717
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013718 // Common case is there are no scroll bars.
13719 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013720 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013721 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013722 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013723 switch (mVerticalScrollbarPosition) {
13724 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013725 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13726 left += offset;
13727 } else {
13728 right += offset;
13729 }
13730 break;
Adam Powell20232d02010-12-08 21:08:53 -080013731 case SCROLLBAR_POSITION_RIGHT:
13732 right += offset;
13733 break;
13734 case SCROLLBAR_POSITION_LEFT:
13735 left += offset;
13736 break;
13737 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013738 }
Adam Powell20232d02010-12-08 21:08:53 -080013739 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013740 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13741 ? 0 : getHorizontalScrollbarHeight();
13742 }
13743 }
Romain Guy8506ab42009-06-11 17:35:47 -070013744
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013745 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013746 changed = true;
13747 mPaddingLeft = left;
13748 }
13749 if (mPaddingTop != top) {
13750 changed = true;
13751 mPaddingTop = top;
13752 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013753 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013754 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013755 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013756 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013757 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013759 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013760 }
13761
13762 if (changed) {
13763 requestLayout();
13764 }
13765 }
13766
13767 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013768 * Sets the relative padding. The view may add on the space required to display
13769 * the scrollbars, depending on the style and visibility of the scrollbars.
13770 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13771 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13772 * from the values set in this call.
13773 *
13774 * @attr ref android.R.styleable#View_padding
13775 * @attr ref android.R.styleable#View_paddingBottom
13776 * @attr ref android.R.styleable#View_paddingStart
13777 * @attr ref android.R.styleable#View_paddingEnd
13778 * @attr ref android.R.styleable#View_paddingTop
13779 * @param start the start padding in pixels
13780 * @param top the top padding in pixels
13781 * @param end the end padding in pixels
13782 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013783 */
13784 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013785 mUserPaddingStart = start;
13786 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013787 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013788
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013789 switch(getResolvedLayoutDirection()) {
13790 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013791 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013792 break;
13793 case LAYOUT_DIRECTION_LTR:
13794 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013795 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013796 }
13797 }
13798
13799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013800 * Returns the top padding of this view.
13801 *
13802 * @return the top padding in pixels
13803 */
13804 public int getPaddingTop() {
13805 return mPaddingTop;
13806 }
13807
13808 /**
13809 * Returns the bottom padding of this view. If there are inset and enabled
13810 * scrollbars, this value may include the space required to display the
13811 * scrollbars as well.
13812 *
13813 * @return the bottom padding in pixels
13814 */
13815 public int getPaddingBottom() {
13816 return mPaddingBottom;
13817 }
13818
13819 /**
13820 * Returns the left padding of this view. If there are inset and enabled
13821 * scrollbars, this value may include the space required to display the
13822 * scrollbars as well.
13823 *
13824 * @return the left padding in pixels
13825 */
13826 public int getPaddingLeft() {
13827 return mPaddingLeft;
13828 }
13829
13830 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013831 * Returns the start padding of this view depending on its resolved layout direction.
13832 * If there are inset and enabled scrollbars, this value may include the space
13833 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013834 *
13835 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013836 */
13837 public int getPaddingStart() {
13838 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13839 mPaddingRight : mPaddingLeft;
13840 }
13841
13842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013843 * Returns the right padding of this view. If there are inset and enabled
13844 * scrollbars, this value may include the space required to display the
13845 * scrollbars as well.
13846 *
13847 * @return the right padding in pixels
13848 */
13849 public int getPaddingRight() {
13850 return mPaddingRight;
13851 }
13852
13853 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013854 * Returns the end padding of this view depending on its resolved layout direction.
13855 * If there are inset and enabled scrollbars, this value may include the space
13856 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013857 *
13858 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013859 */
13860 public int getPaddingEnd() {
13861 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13862 mPaddingLeft : mPaddingRight;
13863 }
13864
13865 /**
13866 * Return if the padding as been set thru relative values
13867 * {@link #setPaddingRelative(int, int, int, int)} or thru
13868 * @attr ref android.R.styleable#View_paddingStart or
13869 * @attr ref android.R.styleable#View_paddingEnd
13870 *
13871 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013872 */
13873 public boolean isPaddingRelative() {
13874 return mUserPaddingRelative;
13875 }
13876
13877 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013878 * @hide
13879 */
13880 public Insets getLayoutInsets() {
13881 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013882 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013883 }
13884 return mLayoutInsets;
13885 }
13886
13887 /**
13888 * @hide
13889 */
13890 public void setLayoutInsets(Insets layoutInsets) {
13891 mLayoutInsets = layoutInsets;
13892 }
13893
13894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 * Changes the selection state of this view. A view can be selected or not.
13896 * Note that selection is not the same as focus. Views are typically
13897 * selected in the context of an AdapterView like ListView or GridView;
13898 * the selected view is the view that is highlighted.
13899 *
13900 * @param selected true if the view must be selected, false otherwise
13901 */
13902 public void setSelected(boolean selected) {
13903 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13904 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013905 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013906 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 refreshDrawableState();
13908 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013909 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13910 notifyAccessibilityStateChanged();
13911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 }
13913 }
13914
13915 /**
13916 * Dispatch setSelected to all of this View's children.
13917 *
13918 * @see #setSelected(boolean)
13919 *
13920 * @param selected The new selected state
13921 */
13922 protected void dispatchSetSelected(boolean selected) {
13923 }
13924
13925 /**
13926 * Indicates the selection state of this view.
13927 *
13928 * @return true if the view is selected, false otherwise
13929 */
13930 @ViewDebug.ExportedProperty
13931 public boolean isSelected() {
13932 return (mPrivateFlags & SELECTED) != 0;
13933 }
13934
13935 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013936 * Changes the activated state of this view. A view can be activated or not.
13937 * Note that activation is not the same as selection. Selection is
13938 * a transient property, representing the view (hierarchy) the user is
13939 * currently interacting with. Activation is a longer-term state that the
13940 * user can move views in and out of. For example, in a list view with
13941 * single or multiple selection enabled, the views in the current selection
13942 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13943 * here.) The activated state is propagated down to children of the view it
13944 * is set on.
13945 *
13946 * @param activated true if the view must be activated, false otherwise
13947 */
13948 public void setActivated(boolean activated) {
13949 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13950 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013951 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013952 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013953 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013954 }
13955 }
13956
13957 /**
13958 * Dispatch setActivated to all of this View's children.
13959 *
13960 * @see #setActivated(boolean)
13961 *
13962 * @param activated The new activated state
13963 */
13964 protected void dispatchSetActivated(boolean activated) {
13965 }
13966
13967 /**
13968 * Indicates the activation state of this view.
13969 *
13970 * @return true if the view is activated, false otherwise
13971 */
13972 @ViewDebug.ExportedProperty
13973 public boolean isActivated() {
13974 return (mPrivateFlags & ACTIVATED) != 0;
13975 }
13976
13977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013978 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13979 * observer can be used to get notifications when global events, like
13980 * layout, happen.
13981 *
13982 * The returned ViewTreeObserver observer is not guaranteed to remain
13983 * valid for the lifetime of this View. If the caller of this method keeps
13984 * a long-lived reference to ViewTreeObserver, it should always check for
13985 * the return value of {@link ViewTreeObserver#isAlive()}.
13986 *
13987 * @return The ViewTreeObserver for this view's hierarchy.
13988 */
13989 public ViewTreeObserver getViewTreeObserver() {
13990 if (mAttachInfo != null) {
13991 return mAttachInfo.mTreeObserver;
13992 }
13993 if (mFloatingTreeObserver == null) {
13994 mFloatingTreeObserver = new ViewTreeObserver();
13995 }
13996 return mFloatingTreeObserver;
13997 }
13998
13999 /**
14000 * <p>Finds the topmost view in the current view hierarchy.</p>
14001 *
14002 * @return the topmost view containing this view
14003 */
14004 public View getRootView() {
14005 if (mAttachInfo != null) {
14006 final View v = mAttachInfo.mRootView;
14007 if (v != null) {
14008 return v;
14009 }
14010 }
Romain Guy8506ab42009-06-11 17:35:47 -070014011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 View parent = this;
14013
14014 while (parent.mParent != null && parent.mParent instanceof View) {
14015 parent = (View) parent.mParent;
14016 }
14017
14018 return parent;
14019 }
14020
14021 /**
14022 * <p>Computes the coordinates of this view on the screen. The argument
14023 * must be an array of two integers. After the method returns, the array
14024 * contains the x and y location in that order.</p>
14025 *
14026 * @param location an array of two integers in which to hold the coordinates
14027 */
14028 public void getLocationOnScreen(int[] location) {
14029 getLocationInWindow(location);
14030
14031 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014032 if (info != null) {
14033 location[0] += info.mWindowLeft;
14034 location[1] += info.mWindowTop;
14035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014036 }
14037
14038 /**
14039 * <p>Computes the coordinates of this view in its window. The argument
14040 * must be an array of two integers. After the method returns, the array
14041 * contains the x and y location in that order.</p>
14042 *
14043 * @param location an array of two integers in which to hold the coordinates
14044 */
14045 public void getLocationInWindow(int[] location) {
14046 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014047 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014048 }
14049
Gilles Debunne6583ce52011-12-06 18:09:02 -080014050 if (mAttachInfo == null) {
14051 // When the view is not attached to a window, this method does not make sense
14052 location[0] = location[1] = 0;
14053 return;
14054 }
14055
Gilles Debunnecea45132011-11-24 02:19:27 +010014056 float[] position = mAttachInfo.mTmpTransformLocation;
14057 position[0] = position[1] = 0.0f;
14058
14059 if (!hasIdentityMatrix()) {
14060 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014062
Gilles Debunnecea45132011-11-24 02:19:27 +010014063 position[0] += mLeft;
14064 position[1] += mTop;
14065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014066 ViewParent viewParent = mParent;
14067 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014068 final View view = (View) viewParent;
14069
14070 position[0] -= view.mScrollX;
14071 position[1] -= view.mScrollY;
14072
14073 if (!view.hasIdentityMatrix()) {
14074 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014075 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014076
14077 position[0] += view.mLeft;
14078 position[1] += view.mTop;
14079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014081 }
Romain Guy8506ab42009-06-11 17:35:47 -070014082
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014083 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014084 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014085 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14086 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014088
14089 location[0] = (int) (position[0] + 0.5f);
14090 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091 }
14092
14093 /**
14094 * {@hide}
14095 * @param id the id of the view to be found
14096 * @return the view of the specified id, null if cannot be found
14097 */
14098 protected View findViewTraversal(int id) {
14099 if (id == mID) {
14100 return this;
14101 }
14102 return null;
14103 }
14104
14105 /**
14106 * {@hide}
14107 * @param tag the tag of the view to be found
14108 * @return the view of specified tag, null if cannot be found
14109 */
14110 protected View findViewWithTagTraversal(Object tag) {
14111 if (tag != null && tag.equals(mTag)) {
14112 return this;
14113 }
14114 return null;
14115 }
14116
14117 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014118 * {@hide}
14119 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014120 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014121 * @return The first view that matches the predicate or null.
14122 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014123 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014124 if (predicate.apply(this)) {
14125 return this;
14126 }
14127 return null;
14128 }
14129
14130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 * Look for a child view with the given id. If this view has the given
14132 * id, return this view.
14133 *
14134 * @param id The id to search for.
14135 * @return The view that has the given id in the hierarchy or null
14136 */
14137 public final View findViewById(int id) {
14138 if (id < 0) {
14139 return null;
14140 }
14141 return findViewTraversal(id);
14142 }
14143
14144 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014145 * Finds a view by its unuque and stable accessibility id.
14146 *
14147 * @param accessibilityId The searched accessibility id.
14148 * @return The found view.
14149 */
14150 final View findViewByAccessibilityId(int accessibilityId) {
14151 if (accessibilityId < 0) {
14152 return null;
14153 }
14154 return findViewByAccessibilityIdTraversal(accessibilityId);
14155 }
14156
14157 /**
14158 * Performs the traversal to find a view by its unuque and stable accessibility id.
14159 *
14160 * <strong>Note:</strong>This method does not stop at the root namespace
14161 * boundary since the user can touch the screen at an arbitrary location
14162 * potentially crossing the root namespace bounday which will send an
14163 * accessibility event to accessibility services and they should be able
14164 * to obtain the event source. Also accessibility ids are guaranteed to be
14165 * unique in the window.
14166 *
14167 * @param accessibilityId The accessibility id.
14168 * @return The found view.
14169 */
14170 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14171 if (getAccessibilityViewId() == accessibilityId) {
14172 return this;
14173 }
14174 return null;
14175 }
14176
14177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014178 * Look for a child view with the given tag. If this view has the given
14179 * tag, return this view.
14180 *
14181 * @param tag The tag to search for, using "tag.equals(getTag())".
14182 * @return The View that has the given tag in the hierarchy or null
14183 */
14184 public final View findViewWithTag(Object tag) {
14185 if (tag == null) {
14186 return null;
14187 }
14188 return findViewWithTagTraversal(tag);
14189 }
14190
14191 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014192 * {@hide}
14193 * Look for a child view that matches the specified predicate.
14194 * If this view matches the predicate, return this view.
14195 *
14196 * @param predicate The predicate to evaluate.
14197 * @return The first view that matches the predicate or null.
14198 */
14199 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014200 return findViewByPredicateTraversal(predicate, null);
14201 }
14202
14203 /**
14204 * {@hide}
14205 * Look for a child view that matches the specified predicate,
14206 * starting with the specified view and its descendents and then
14207 * recusively searching the ancestors and siblings of that view
14208 * until this view is reached.
14209 *
14210 * This method is useful in cases where the predicate does not match
14211 * a single unique view (perhaps multiple views use the same id)
14212 * and we are trying to find the view that is "closest" in scope to the
14213 * starting view.
14214 *
14215 * @param start The view to start from.
14216 * @param predicate The predicate to evaluate.
14217 * @return The first view that matches the predicate or null.
14218 */
14219 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14220 View childToSkip = null;
14221 for (;;) {
14222 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14223 if (view != null || start == this) {
14224 return view;
14225 }
14226
14227 ViewParent parent = start.getParent();
14228 if (parent == null || !(parent instanceof View)) {
14229 return null;
14230 }
14231
14232 childToSkip = start;
14233 start = (View) parent;
14234 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014235 }
14236
14237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014238 * Sets the identifier for this view. The identifier does not have to be
14239 * unique in this view's hierarchy. The identifier should be a positive
14240 * number.
14241 *
14242 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014243 * @see #getId()
14244 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014245 *
14246 * @param id a number used to identify the view
14247 *
14248 * @attr ref android.R.styleable#View_id
14249 */
14250 public void setId(int id) {
14251 mID = id;
14252 }
14253
14254 /**
14255 * {@hide}
14256 *
14257 * @param isRoot true if the view belongs to the root namespace, false
14258 * otherwise
14259 */
14260 public void setIsRootNamespace(boolean isRoot) {
14261 if (isRoot) {
14262 mPrivateFlags |= IS_ROOT_NAMESPACE;
14263 } else {
14264 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14265 }
14266 }
14267
14268 /**
14269 * {@hide}
14270 *
14271 * @return true if the view belongs to the root namespace, false otherwise
14272 */
14273 public boolean isRootNamespace() {
14274 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14275 }
14276
14277 /**
14278 * Returns this view's identifier.
14279 *
14280 * @return a positive integer used to identify the view or {@link #NO_ID}
14281 * if the view has no ID
14282 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014283 * @see #setId(int)
14284 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 * @attr ref android.R.styleable#View_id
14286 */
14287 @ViewDebug.CapturedViewProperty
14288 public int getId() {
14289 return mID;
14290 }
14291
14292 /**
14293 * Returns this view's tag.
14294 *
14295 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014296 *
14297 * @see #setTag(Object)
14298 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014299 */
14300 @ViewDebug.ExportedProperty
14301 public Object getTag() {
14302 return mTag;
14303 }
14304
14305 /**
14306 * Sets the tag associated with this view. A tag can be used to mark
14307 * a view in its hierarchy and does not have to be unique within the
14308 * hierarchy. Tags can also be used to store data within a view without
14309 * resorting to another data structure.
14310 *
14311 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014312 *
14313 * @see #getTag()
14314 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014315 */
14316 public void setTag(final Object tag) {
14317 mTag = tag;
14318 }
14319
14320 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014321 * Returns the tag associated with this view and the specified key.
14322 *
14323 * @param key The key identifying the tag
14324 *
14325 * @return the Object stored in this view as a tag
14326 *
14327 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014328 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014329 */
14330 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014331 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014332 return null;
14333 }
14334
14335 /**
14336 * Sets a tag associated with this view and a key. A tag can be used
14337 * to mark a view in its hierarchy and does not have to be unique within
14338 * the hierarchy. Tags can also be used to store data within a view
14339 * without resorting to another data structure.
14340 *
14341 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014342 * application to ensure it is unique (see the <a
14343 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14344 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014345 * the Android framework or not associated with any package will cause
14346 * an {@link IllegalArgumentException} to be thrown.
14347 *
14348 * @param key The key identifying the tag
14349 * @param tag An Object to tag the view with
14350 *
14351 * @throws IllegalArgumentException If they specified key is not valid
14352 *
14353 * @see #setTag(Object)
14354 * @see #getTag(int)
14355 */
14356 public void setTag(int key, final Object tag) {
14357 // If the package id is 0x00 or 0x01, it's either an undefined package
14358 // or a framework id
14359 if ((key >>> 24) < 2) {
14360 throw new IllegalArgumentException("The key must be an application-specific "
14361 + "resource id.");
14362 }
14363
Adam Powell2b2f6d62011-09-23 11:15:39 -070014364 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014365 }
14366
14367 /**
14368 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14369 * framework id.
14370 *
14371 * @hide
14372 */
14373 public void setTagInternal(int key, Object tag) {
14374 if ((key >>> 24) != 0x1) {
14375 throw new IllegalArgumentException("The key must be a framework-specific "
14376 + "resource id.");
14377 }
14378
Adam Powell2b2f6d62011-09-23 11:15:39 -070014379 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014380 }
14381
Adam Powell2b2f6d62011-09-23 11:15:39 -070014382 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014383 if (mKeyedTags == null) {
14384 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014385 }
14386
Adam Powell7db82ac2011-09-22 19:44:04 -070014387 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014388 }
14389
14390 /**
Romain Guy13922e02009-05-12 17:56:14 -070014391 * @param consistency The type of consistency. See ViewDebug for more information.
14392 *
14393 * @hide
14394 */
14395 protected boolean dispatchConsistencyCheck(int consistency) {
14396 return onConsistencyCheck(consistency);
14397 }
14398
14399 /**
14400 * Method that subclasses should implement to check their consistency. The type of
14401 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014402 *
Romain Guy13922e02009-05-12 17:56:14 -070014403 * @param consistency The type of consistency. See ViewDebug for more information.
14404 *
14405 * @throws IllegalStateException if the view is in an inconsistent state.
14406 *
14407 * @hide
14408 */
14409 protected boolean onConsistencyCheck(int consistency) {
14410 boolean result = true;
14411
14412 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14413 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14414
14415 if (checkLayout) {
14416 if (getParent() == null) {
14417 result = false;
14418 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14419 "View " + this + " does not have a parent.");
14420 }
14421
14422 if (mAttachInfo == null) {
14423 result = false;
14424 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14425 "View " + this + " is not attached to a window.");
14426 }
14427 }
14428
14429 if (checkDrawing) {
14430 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14431 // from their draw() method
14432
14433 if ((mPrivateFlags & DRAWN) != DRAWN &&
14434 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14435 result = false;
14436 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14437 "View " + this + " was invalidated but its drawing cache is valid.");
14438 }
14439 }
14440
14441 return result;
14442 }
14443
14444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014445 * Prints information about this view in the log output, with the tag
14446 * {@link #VIEW_LOG_TAG}.
14447 *
14448 * @hide
14449 */
14450 public void debug() {
14451 debug(0);
14452 }
14453
14454 /**
14455 * Prints information about this view in the log output, with the tag
14456 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14457 * indentation defined by the <code>depth</code>.
14458 *
14459 * @param depth the indentation level
14460 *
14461 * @hide
14462 */
14463 protected void debug(int depth) {
14464 String output = debugIndent(depth - 1);
14465
14466 output += "+ " + this;
14467 int id = getId();
14468 if (id != -1) {
14469 output += " (id=" + id + ")";
14470 }
14471 Object tag = getTag();
14472 if (tag != null) {
14473 output += " (tag=" + tag + ")";
14474 }
14475 Log.d(VIEW_LOG_TAG, output);
14476
14477 if ((mPrivateFlags & FOCUSED) != 0) {
14478 output = debugIndent(depth) + " FOCUSED";
14479 Log.d(VIEW_LOG_TAG, output);
14480 }
14481
14482 output = debugIndent(depth);
14483 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14484 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14485 + "} ";
14486 Log.d(VIEW_LOG_TAG, output);
14487
14488 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14489 || mPaddingBottom != 0) {
14490 output = debugIndent(depth);
14491 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14492 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14493 Log.d(VIEW_LOG_TAG, output);
14494 }
14495
14496 output = debugIndent(depth);
14497 output += "mMeasureWidth=" + mMeasuredWidth +
14498 " mMeasureHeight=" + mMeasuredHeight;
14499 Log.d(VIEW_LOG_TAG, output);
14500
14501 output = debugIndent(depth);
14502 if (mLayoutParams == null) {
14503 output += "BAD! no layout params";
14504 } else {
14505 output = mLayoutParams.debug(output);
14506 }
14507 Log.d(VIEW_LOG_TAG, output);
14508
14509 output = debugIndent(depth);
14510 output += "flags={";
14511 output += View.printFlags(mViewFlags);
14512 output += "}";
14513 Log.d(VIEW_LOG_TAG, output);
14514
14515 output = debugIndent(depth);
14516 output += "privateFlags={";
14517 output += View.printPrivateFlags(mPrivateFlags);
14518 output += "}";
14519 Log.d(VIEW_LOG_TAG, output);
14520 }
14521
14522 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014523 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 *
14525 * @param depth the indentation level
14526 * @return a String containing (depth * 2 + 3) * 2 white spaces
14527 *
14528 * @hide
14529 */
14530 protected static String debugIndent(int depth) {
14531 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14532 for (int i = 0; i < (depth * 2) + 3; i++) {
14533 spaces.append(' ').append(' ');
14534 }
14535 return spaces.toString();
14536 }
14537
14538 /**
14539 * <p>Return the offset of the widget's text baseline from the widget's top
14540 * boundary. If this widget does not support baseline alignment, this
14541 * method returns -1. </p>
14542 *
14543 * @return the offset of the baseline within the widget's bounds or -1
14544 * if baseline alignment is not supported
14545 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014546 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014547 public int getBaseline() {
14548 return -1;
14549 }
14550
14551 /**
14552 * Call this when something has changed which has invalidated the
14553 * layout of this view. This will schedule a layout pass of the view
14554 * tree.
14555 */
14556 public void requestLayout() {
14557 if (ViewDebug.TRACE_HIERARCHY) {
14558 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14559 }
14560
14561 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014562 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014563
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014564 if (mLayoutParams != null) {
14565 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14566 }
14567
14568 if (mParent != null && !mParent.isLayoutRequested()) {
14569 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014570 }
14571 }
14572
14573 /**
14574 * Forces this view to be laid out during the next layout pass.
14575 * This method does not call requestLayout() or forceLayout()
14576 * on the parent.
14577 */
14578 public void forceLayout() {
14579 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014580 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014581 }
14582
14583 /**
14584 * <p>
14585 * This is called to find out how big a view should be. The parent
14586 * supplies constraint information in the width and height parameters.
14587 * </p>
14588 *
14589 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014590 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014591 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014592 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014593 * </p>
14594 *
14595 *
14596 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14597 * parent
14598 * @param heightMeasureSpec Vertical space requirements as imposed by the
14599 * parent
14600 *
14601 * @see #onMeasure(int, int)
14602 */
14603 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14604 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14605 widthMeasureSpec != mOldWidthMeasureSpec ||
14606 heightMeasureSpec != mOldHeightMeasureSpec) {
14607
14608 // first clears the measured dimension flag
14609 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14610
14611 if (ViewDebug.TRACE_HIERARCHY) {
14612 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14613 }
14614
14615 // measure ourselves, this should set the measured dimension flag back
14616 onMeasure(widthMeasureSpec, heightMeasureSpec);
14617
14618 // flag not set, setMeasuredDimension() was not invoked, we raise
14619 // an exception to warn the developer
14620 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14621 throw new IllegalStateException("onMeasure() did not set the"
14622 + " measured dimension by calling"
14623 + " setMeasuredDimension()");
14624 }
14625
14626 mPrivateFlags |= LAYOUT_REQUIRED;
14627 }
14628
14629 mOldWidthMeasureSpec = widthMeasureSpec;
14630 mOldHeightMeasureSpec = heightMeasureSpec;
14631 }
14632
14633 /**
14634 * <p>
14635 * Measure the view and its content to determine the measured width and the
14636 * measured height. This method is invoked by {@link #measure(int, int)} and
14637 * should be overriden by subclasses to provide accurate and efficient
14638 * measurement of their contents.
14639 * </p>
14640 *
14641 * <p>
14642 * <strong>CONTRACT:</strong> When overriding this method, you
14643 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14644 * measured width and height of this view. Failure to do so will trigger an
14645 * <code>IllegalStateException</code>, thrown by
14646 * {@link #measure(int, int)}. Calling the superclass'
14647 * {@link #onMeasure(int, int)} is a valid use.
14648 * </p>
14649 *
14650 * <p>
14651 * The base class implementation of measure defaults to the background size,
14652 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14653 * override {@link #onMeasure(int, int)} to provide better measurements of
14654 * their content.
14655 * </p>
14656 *
14657 * <p>
14658 * If this method is overridden, it is the subclass's responsibility to make
14659 * sure the measured height and width are at least the view's minimum height
14660 * and width ({@link #getSuggestedMinimumHeight()} and
14661 * {@link #getSuggestedMinimumWidth()}).
14662 * </p>
14663 *
14664 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14665 * The requirements are encoded with
14666 * {@link android.view.View.MeasureSpec}.
14667 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14668 * The requirements are encoded with
14669 * {@link android.view.View.MeasureSpec}.
14670 *
14671 * @see #getMeasuredWidth()
14672 * @see #getMeasuredHeight()
14673 * @see #setMeasuredDimension(int, int)
14674 * @see #getSuggestedMinimumHeight()
14675 * @see #getSuggestedMinimumWidth()
14676 * @see android.view.View.MeasureSpec#getMode(int)
14677 * @see android.view.View.MeasureSpec#getSize(int)
14678 */
14679 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14680 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14681 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14682 }
14683
14684 /**
14685 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14686 * measured width and measured height. Failing to do so will trigger an
14687 * exception at measurement time.</p>
14688 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014689 * @param measuredWidth The measured width of this view. May be a complex
14690 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14691 * {@link #MEASURED_STATE_TOO_SMALL}.
14692 * @param measuredHeight The measured height of this view. May be a complex
14693 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14694 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014695 */
14696 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14697 mMeasuredWidth = measuredWidth;
14698 mMeasuredHeight = measuredHeight;
14699
14700 mPrivateFlags |= MEASURED_DIMENSION_SET;
14701 }
14702
14703 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014704 * Merge two states as returned by {@link #getMeasuredState()}.
14705 * @param curState The current state as returned from a view or the result
14706 * of combining multiple views.
14707 * @param newState The new view state to combine.
14708 * @return Returns a new integer reflecting the combination of the two
14709 * states.
14710 */
14711 public static int combineMeasuredStates(int curState, int newState) {
14712 return curState | newState;
14713 }
14714
14715 /**
14716 * Version of {@link #resolveSizeAndState(int, int, int)}
14717 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14718 */
14719 public static int resolveSize(int size, int measureSpec) {
14720 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14721 }
14722
14723 /**
14724 * Utility to reconcile a desired size and state, with constraints imposed
14725 * by a MeasureSpec. Will take the desired size, unless a different size
14726 * is imposed by the constraints. The returned value is a compound integer,
14727 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14728 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14729 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014730 *
14731 * @param size How big the view wants to be
14732 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014733 * @return Size information bit mask as defined by
14734 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014735 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014736 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014737 int result = size;
14738 int specMode = MeasureSpec.getMode(measureSpec);
14739 int specSize = MeasureSpec.getSize(measureSpec);
14740 switch (specMode) {
14741 case MeasureSpec.UNSPECIFIED:
14742 result = size;
14743 break;
14744 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014745 if (specSize < size) {
14746 result = specSize | MEASURED_STATE_TOO_SMALL;
14747 } else {
14748 result = size;
14749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014750 break;
14751 case MeasureSpec.EXACTLY:
14752 result = specSize;
14753 break;
14754 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014755 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014756 }
14757
14758 /**
14759 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014760 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014761 * by the MeasureSpec.
14762 *
14763 * @param size Default size for this view
14764 * @param measureSpec Constraints imposed by the parent
14765 * @return The size this view should be.
14766 */
14767 public static int getDefaultSize(int size, int measureSpec) {
14768 int result = size;
14769 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014770 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014771
14772 switch (specMode) {
14773 case MeasureSpec.UNSPECIFIED:
14774 result = size;
14775 break;
14776 case MeasureSpec.AT_MOST:
14777 case MeasureSpec.EXACTLY:
14778 result = specSize;
14779 break;
14780 }
14781 return result;
14782 }
14783
14784 /**
14785 * Returns the suggested minimum height that the view should use. This
14786 * returns the maximum of the view's minimum height
14787 * and the background's minimum height
14788 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14789 * <p>
14790 * When being used in {@link #onMeasure(int, int)}, the caller should still
14791 * ensure the returned height is within the requirements of the parent.
14792 *
14793 * @return The suggested minimum height of the view.
14794 */
14795 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014796 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798 }
14799
14800 /**
14801 * Returns the suggested minimum width that the view should use. This
14802 * returns the maximum of the view's minimum width)
14803 * and the background's minimum width
14804 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14805 * <p>
14806 * When being used in {@link #onMeasure(int, int)}, the caller should still
14807 * ensure the returned width is within the requirements of the parent.
14808 *
14809 * @return The suggested minimum width of the view.
14810 */
14811 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014812 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014814
Philip Milne6c8ea062012-04-03 17:38:43 -070014815 /**
14816 * Returns the minimum height of the view.
14817 *
14818 * @return the minimum height the view will try to be.
14819 *
14820 * @see #setMinimumHeight(int)
14821 *
14822 * @attr ref android.R.styleable#View_minHeight
14823 */
14824 public int getMinimumHeight() {
14825 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014826 }
14827
14828 /**
14829 * Sets the minimum height of the view. It is not guaranteed the view will
14830 * be able to achieve this minimum height (for example, if its parent layout
14831 * constrains it with less available height).
14832 *
14833 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014834 *
14835 * @see #getMinimumHeight()
14836 *
14837 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 */
14839 public void setMinimumHeight(int minHeight) {
14840 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014841 requestLayout();
14842 }
14843
14844 /**
14845 * Returns the minimum width of the view.
14846 *
14847 * @return the minimum width the view will try to be.
14848 *
14849 * @see #setMinimumWidth(int)
14850 *
14851 * @attr ref android.R.styleable#View_minWidth
14852 */
14853 public int getMinimumWidth() {
14854 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014855 }
14856
14857 /**
14858 * Sets the minimum width of the view. It is not guaranteed the view will
14859 * be able to achieve this minimum width (for example, if its parent layout
14860 * constrains it with less available width).
14861 *
14862 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014863 *
14864 * @see #getMinimumWidth()
14865 *
14866 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014867 */
14868 public void setMinimumWidth(int minWidth) {
14869 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014870 requestLayout();
14871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014872 }
14873
14874 /**
14875 * Get the animation currently associated with this view.
14876 *
14877 * @return The animation that is currently playing or
14878 * scheduled to play for this view.
14879 */
14880 public Animation getAnimation() {
14881 return mCurrentAnimation;
14882 }
14883
14884 /**
14885 * Start the specified animation now.
14886 *
14887 * @param animation the animation to start now
14888 */
14889 public void startAnimation(Animation animation) {
14890 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14891 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014892 invalidateParentCaches();
14893 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014894 }
14895
14896 /**
14897 * Cancels any animations for this view.
14898 */
14899 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014900 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014901 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014903 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014904 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014905 }
14906
14907 /**
14908 * Sets the next animation to play for this view.
14909 * If you want the animation to play immediately, use
14910 * startAnimation. This method provides allows fine-grained
14911 * control over the start time and invalidation, but you
14912 * must make sure that 1) the animation has a start time set, and
14913 * 2) the view will be invalidated when the animation is supposed to
14914 * start.
14915 *
14916 * @param animation The next animation, or null.
14917 */
14918 public void setAnimation(Animation animation) {
14919 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014921 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014922 // If the screen is off assume the animation start time is now instead of
14923 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14924 // would cause the animation to start when the screen turns back on
14925 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14926 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14927 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014929 animation.reset();
14930 }
14931 }
14932
14933 /**
14934 * Invoked by a parent ViewGroup to notify the start of the animation
14935 * currently associated with this view. If you override this method,
14936 * always call super.onAnimationStart();
14937 *
14938 * @see #setAnimation(android.view.animation.Animation)
14939 * @see #getAnimation()
14940 */
14941 protected void onAnimationStart() {
14942 mPrivateFlags |= ANIMATION_STARTED;
14943 }
14944
14945 /**
14946 * Invoked by a parent ViewGroup to notify the end of the animation
14947 * currently associated with this view. If you override this method,
14948 * always call super.onAnimationEnd();
14949 *
14950 * @see #setAnimation(android.view.animation.Animation)
14951 * @see #getAnimation()
14952 */
14953 protected void onAnimationEnd() {
14954 mPrivateFlags &= ~ANIMATION_STARTED;
14955 }
14956
14957 /**
14958 * Invoked if there is a Transform that involves alpha. Subclass that can
14959 * draw themselves with the specified alpha should return true, and then
14960 * respect that alpha when their onDraw() is called. If this returns false
14961 * then the view may be redirected to draw into an offscreen buffer to
14962 * fulfill the request, which will look fine, but may be slower than if the
14963 * subclass handles it internally. The default implementation returns false.
14964 *
14965 * @param alpha The alpha (0..255) to apply to the view's drawing
14966 * @return true if the view can draw with the specified alpha.
14967 */
14968 protected boolean onSetAlpha(int alpha) {
14969 return false;
14970 }
14971
14972 /**
14973 * This is used by the RootView to perform an optimization when
14974 * the view hierarchy contains one or several SurfaceView.
14975 * SurfaceView is always considered transparent, but its children are not,
14976 * therefore all View objects remove themselves from the global transparent
14977 * region (passed as a parameter to this function).
14978 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014979 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014980 *
14981 * @return Returns true if the effective visibility of the view at this
14982 * point is opaque, regardless of the transparent region; returns false
14983 * if it is possible for underlying windows to be seen behind the view.
14984 *
14985 * {@hide}
14986 */
14987 public boolean gatherTransparentRegion(Region region) {
14988 final AttachInfo attachInfo = mAttachInfo;
14989 if (region != null && attachInfo != null) {
14990 final int pflags = mPrivateFlags;
14991 if ((pflags & SKIP_DRAW) == 0) {
14992 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14993 // remove it from the transparent region.
14994 final int[] location = attachInfo.mTransparentLocation;
14995 getLocationInWindow(location);
14996 region.op(location[0], location[1], location[0] + mRight - mLeft,
14997 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014998 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014999 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15000 // exists, so we remove the background drawable's non-transparent
15001 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015002 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015003 }
15004 }
15005 return true;
15006 }
15007
15008 /**
15009 * Play a sound effect for this view.
15010 *
15011 * <p>The framework will play sound effects for some built in actions, such as
15012 * clicking, but you may wish to play these effects in your widget,
15013 * for instance, for internal navigation.
15014 *
15015 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15016 * {@link #isSoundEffectsEnabled()} is true.
15017 *
15018 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15019 */
15020 public void playSoundEffect(int soundConstant) {
15021 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15022 return;
15023 }
15024 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15025 }
15026
15027 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015028 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015029 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015030 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015031 *
15032 * <p>The framework will provide haptic feedback for some built in actions,
15033 * such as long presses, but you may wish to provide feedback for your
15034 * own widget.
15035 *
15036 * <p>The feedback will only be performed if
15037 * {@link #isHapticFeedbackEnabled()} is true.
15038 *
15039 * @param feedbackConstant One of the constants defined in
15040 * {@link HapticFeedbackConstants}
15041 */
15042 public boolean performHapticFeedback(int feedbackConstant) {
15043 return performHapticFeedback(feedbackConstant, 0);
15044 }
15045
15046 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015047 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015048 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015049 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015050 *
15051 * @param feedbackConstant One of the constants defined in
15052 * {@link HapticFeedbackConstants}
15053 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15054 */
15055 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15056 if (mAttachInfo == null) {
15057 return false;
15058 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015059 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015060 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015061 && !isHapticFeedbackEnabled()) {
15062 return false;
15063 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015064 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15065 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015066 }
15067
15068 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015069 * Request that the visibility of the status bar or other screen/window
15070 * decorations be changed.
15071 *
15072 * <p>This method is used to put the over device UI into temporary modes
15073 * where the user's attention is focused more on the application content,
15074 * by dimming or hiding surrounding system affordances. This is typically
15075 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15076 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15077 * to be placed behind the action bar (and with these flags other system
15078 * affordances) so that smooth transitions between hiding and showing them
15079 * can be done.
15080 *
15081 * <p>Two representative examples of the use of system UI visibility is
15082 * implementing a content browsing application (like a magazine reader)
15083 * and a video playing application.
15084 *
15085 * <p>The first code shows a typical implementation of a View in a content
15086 * browsing application. In this implementation, the application goes
15087 * into a content-oriented mode by hiding the status bar and action bar,
15088 * and putting the navigation elements into lights out mode. The user can
15089 * then interact with content while in this mode. Such an application should
15090 * provide an easy way for the user to toggle out of the mode (such as to
15091 * check information in the status bar or access notifications). In the
15092 * implementation here, this is done simply by tapping on the content.
15093 *
15094 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15095 * content}
15096 *
15097 * <p>This second code sample shows a typical implementation of a View
15098 * in a video playing application. In this situation, while the video is
15099 * playing the application would like to go into a complete full-screen mode,
15100 * to use as much of the display as possible for the video. When in this state
15101 * the user can not interact with the application; the system intercepts
15102 * touching on the screen to pop the UI out of full screen mode.
15103 *
15104 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15105 * content}
15106 *
15107 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15108 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15109 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15110 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015111 */
15112 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015113 if (visibility != mSystemUiVisibility) {
15114 mSystemUiVisibility = visibility;
15115 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15116 mParent.recomputeViewAttributes(this);
15117 }
Joe Onorato664644d2011-01-23 17:53:23 -080015118 }
15119 }
15120
15121 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015122 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15123 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15124 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15125 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15126 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015127 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015128 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015129 return mSystemUiVisibility;
15130 }
15131
Scott Mainec6331b2011-05-24 16:55:56 -070015132 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015133 * Returns the current system UI visibility that is currently set for
15134 * the entire window. This is the combination of the
15135 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15136 * views in the window.
15137 */
15138 public int getWindowSystemUiVisibility() {
15139 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15140 }
15141
15142 /**
15143 * Override to find out when the window's requested system UI visibility
15144 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15145 * This is different from the callbacks recieved through
15146 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15147 * in that this is only telling you about the local request of the window,
15148 * not the actual values applied by the system.
15149 */
15150 public void onWindowSystemUiVisibilityChanged(int visible) {
15151 }
15152
15153 /**
15154 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15155 * the view hierarchy.
15156 */
15157 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15158 onWindowSystemUiVisibilityChanged(visible);
15159 }
15160
15161 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015162 * Set a listener to receive callbacks when the visibility of the system bar changes.
15163 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15164 */
Joe Onorato664644d2011-01-23 17:53:23 -080015165 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015166 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015167 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15168 mParent.recomputeViewAttributes(this);
15169 }
15170 }
15171
15172 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015173 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15174 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015175 */
15176 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015177 ListenerInfo li = mListenerInfo;
15178 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15179 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015180 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015181 }
15182 }
15183
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015184 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15185 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15186 if (val != mSystemUiVisibility) {
15187 setSystemUiVisibility(val);
15188 }
15189 }
15190
Joe Onorato664644d2011-01-23 17:53:23 -080015191 /**
Joe Malin32736f02011-01-19 16:14:20 -080015192 * Creates an image that the system displays during the drag and drop
15193 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15194 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15195 * appearance as the given View. The default also positions the center of the drag shadow
15196 * directly under the touch point. If no View is provided (the constructor with no parameters
15197 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15198 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15199 * default is an invisible drag shadow.
15200 * <p>
15201 * You are not required to use the View you provide to the constructor as the basis of the
15202 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15203 * anything you want as the drag shadow.
15204 * </p>
15205 * <p>
15206 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15207 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15208 * size and position of the drag shadow. It uses this data to construct a
15209 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15210 * so that your application can draw the shadow image in the Canvas.
15211 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015212 *
15213 * <div class="special reference">
15214 * <h3>Developer Guides</h3>
15215 * <p>For a guide to implementing drag and drop features, read the
15216 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15217 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015218 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015219 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015220 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015221
15222 /**
Joe Malin32736f02011-01-19 16:14:20 -080015223 * Constructs a shadow image builder based on a View. By default, the resulting drag
15224 * shadow will have the same appearance and dimensions as the View, with the touch point
15225 * over the center of the View.
15226 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015227 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015228 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015229 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015230 }
15231
Christopher Tate17ed60c2011-01-18 12:50:26 -080015232 /**
15233 * Construct a shadow builder object with no associated View. This
15234 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15235 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15236 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015237 * reference to any View object. If they are not overridden, then the result is an
15238 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015239 */
15240 public DragShadowBuilder() {
15241 mView = new WeakReference<View>(null);
15242 }
15243
15244 /**
15245 * Returns the View object that had been passed to the
15246 * {@link #View.DragShadowBuilder(View)}
15247 * constructor. If that View parameter was {@code null} or if the
15248 * {@link #View.DragShadowBuilder()}
15249 * constructor was used to instantiate the builder object, this method will return
15250 * null.
15251 *
15252 * @return The View object associate with this builder object.
15253 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015254 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015255 final public View getView() {
15256 return mView.get();
15257 }
15258
Christopher Tate2c095f32010-10-04 14:13:40 -070015259 /**
Joe Malin32736f02011-01-19 16:14:20 -080015260 * Provides the metrics for the shadow image. These include the dimensions of
15261 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015262 * be centered under the touch location while dragging.
15263 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015264 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015265 * same as the dimensions of the View itself and centers the shadow under
15266 * the touch point.
15267 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015268 *
Joe Malin32736f02011-01-19 16:14:20 -080015269 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15270 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15271 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15272 * image.
15273 *
15274 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15275 * shadow image that should be underneath the touch point during the drag and drop
15276 * operation. Your application must set {@link android.graphics.Point#x} to the
15277 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015278 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015279 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015280 final View view = mView.get();
15281 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015282 shadowSize.set(view.getWidth(), view.getHeight());
15283 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015284 } else {
15285 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15286 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015287 }
15288
15289 /**
Joe Malin32736f02011-01-19 16:14:20 -080015290 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15291 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015292 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015293 *
Joe Malin32736f02011-01-19 16:14:20 -080015294 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015295 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015296 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015297 final View view = mView.get();
15298 if (view != null) {
15299 view.draw(canvas);
15300 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015301 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015302 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015303 }
15304 }
15305
15306 /**
Joe Malin32736f02011-01-19 16:14:20 -080015307 * Starts a drag and drop operation. When your application calls this method, it passes a
15308 * {@link android.view.View.DragShadowBuilder} object to the system. The
15309 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15310 * to get metrics for the drag shadow, and then calls the object's
15311 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15312 * <p>
15313 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15314 * drag events to all the View objects in your application that are currently visible. It does
15315 * this either by calling the View object's drag listener (an implementation of
15316 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15317 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15318 * Both are passed a {@link android.view.DragEvent} object that has a
15319 * {@link android.view.DragEvent#getAction()} value of
15320 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15321 * </p>
15322 * <p>
15323 * Your application can invoke startDrag() on any attached View object. The View object does not
15324 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15325 * be related to the View the user selected for dragging.
15326 * </p>
15327 * @param data A {@link android.content.ClipData} object pointing to the data to be
15328 * transferred by the drag and drop operation.
15329 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15330 * drag shadow.
15331 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15332 * drop operation. This Object is put into every DragEvent object sent by the system during the
15333 * current drag.
15334 * <p>
15335 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15336 * to the target Views. For example, it can contain flags that differentiate between a
15337 * a copy operation and a move operation.
15338 * </p>
15339 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15340 * so the parameter should be set to 0.
15341 * @return {@code true} if the method completes successfully, or
15342 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15343 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015344 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015345 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015346 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015347 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015348 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015349 }
15350 boolean okay = false;
15351
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015352 Point shadowSize = new Point();
15353 Point shadowTouchPoint = new Point();
15354 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015355
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015356 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15357 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15358 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015359 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015360
Chris Tatea32dcf72010-10-14 12:13:50 -070015361 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015362 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15363 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015364 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015365 Surface surface = new Surface();
15366 try {
15367 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015368 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015369 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015370 + " surface=" + surface);
15371 if (token != null) {
15372 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015373 try {
Chris Tate6b391282010-10-14 15:48:59 -070015374 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015375 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015376 } finally {
15377 surface.unlockCanvasAndPost(canvas);
15378 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015379
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015380 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015381
15382 // Cache the local state object for delivery with DragEvents
15383 root.setLocalDragState(myLocalState);
15384
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015385 // repurpose 'shadowSize' for the last touch point
15386 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015387
Christopher Tatea53146c2010-09-07 11:57:52 -070015388 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015389 shadowSize.x, shadowSize.y,
15390 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015391 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015392
15393 // Off and running! Release our local surface instance; the drag
15394 // shadow surface is now managed by the system process.
15395 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015396 }
15397 } catch (Exception e) {
15398 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15399 surface.destroy();
15400 }
15401
15402 return okay;
15403 }
15404
Christopher Tatea53146c2010-09-07 11:57:52 -070015405 /**
Joe Malin32736f02011-01-19 16:14:20 -080015406 * Handles drag events sent by the system following a call to
15407 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15408 *<p>
15409 * When the system calls this method, it passes a
15410 * {@link android.view.DragEvent} object. A call to
15411 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15412 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15413 * operation.
15414 * @param event The {@link android.view.DragEvent} sent by the system.
15415 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15416 * in DragEvent, indicating the type of drag event represented by this object.
15417 * @return {@code true} if the method was successful, otherwise {@code false}.
15418 * <p>
15419 * The method should return {@code true} in response to an action type of
15420 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15421 * operation.
15422 * </p>
15423 * <p>
15424 * The method should also return {@code true} in response to an action type of
15425 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15426 * {@code false} if it didn't.
15427 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015428 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015429 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015430 return false;
15431 }
15432
15433 /**
Joe Malin32736f02011-01-19 16:14:20 -080015434 * Detects if this View is enabled and has a drag event listener.
15435 * If both are true, then it calls the drag event listener with the
15436 * {@link android.view.DragEvent} it received. If the drag event listener returns
15437 * {@code true}, then dispatchDragEvent() returns {@code true}.
15438 * <p>
15439 * For all other cases, the method calls the
15440 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15441 * method and returns its result.
15442 * </p>
15443 * <p>
15444 * This ensures that a drag event is always consumed, even if the View does not have a drag
15445 * event listener. However, if the View has a listener and the listener returns true, then
15446 * onDragEvent() is not called.
15447 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015448 */
15449 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015450 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015451 ListenerInfo li = mListenerInfo;
15452 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15453 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015454 return true;
15455 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015456 return onDragEvent(event);
15457 }
15458
Christopher Tate3d4bf172011-03-28 16:16:46 -070015459 boolean canAcceptDrag() {
15460 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15461 }
15462
Christopher Tatea53146c2010-09-07 11:57:52 -070015463 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015464 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15465 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015466 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015467 */
15468 public void onCloseSystemDialogs(String reason) {
15469 }
Joe Malin32736f02011-01-19 16:14:20 -080015470
Dianne Hackbornffa42482009-09-23 22:20:11 -070015471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015472 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015473 * update a Region being computed for
15474 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015475 * that any non-transparent parts of the Drawable are removed from the
15476 * given transparent region.
15477 *
15478 * @param dr The Drawable whose transparency is to be applied to the region.
15479 * @param region A Region holding the current transparency information,
15480 * where any parts of the region that are set are considered to be
15481 * transparent. On return, this region will be modified to have the
15482 * transparency information reduced by the corresponding parts of the
15483 * Drawable that are not transparent.
15484 * {@hide}
15485 */
15486 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15487 if (DBG) {
15488 Log.i("View", "Getting transparent region for: " + this);
15489 }
15490 final Region r = dr.getTransparentRegion();
15491 final Rect db = dr.getBounds();
15492 final AttachInfo attachInfo = mAttachInfo;
15493 if (r != null && attachInfo != null) {
15494 final int w = getRight()-getLeft();
15495 final int h = getBottom()-getTop();
15496 if (db.left > 0) {
15497 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15498 r.op(0, 0, db.left, h, Region.Op.UNION);
15499 }
15500 if (db.right < w) {
15501 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15502 r.op(db.right, 0, w, h, Region.Op.UNION);
15503 }
15504 if (db.top > 0) {
15505 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15506 r.op(0, 0, w, db.top, Region.Op.UNION);
15507 }
15508 if (db.bottom < h) {
15509 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15510 r.op(0, db.bottom, w, h, Region.Op.UNION);
15511 }
15512 final int[] location = attachInfo.mTransparentLocation;
15513 getLocationInWindow(location);
15514 r.translate(location[0], location[1]);
15515 region.op(r, Region.Op.INTERSECT);
15516 } else {
15517 region.op(db, Region.Op.DIFFERENCE);
15518 }
15519 }
15520
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015521 private void checkForLongClick(int delayOffset) {
15522 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15523 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015524
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015525 if (mPendingCheckForLongPress == null) {
15526 mPendingCheckForLongPress = new CheckForLongPress();
15527 }
15528 mPendingCheckForLongPress.rememberWindowAttachCount();
15529 postDelayed(mPendingCheckForLongPress,
15530 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015532 }
15533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015534 /**
15535 * Inflate a view from an XML resource. This convenience method wraps the {@link
15536 * LayoutInflater} class, which provides a full range of options for view inflation.
15537 *
15538 * @param context The Context object for your activity or application.
15539 * @param resource The resource ID to inflate
15540 * @param root A view group that will be the parent. Used to properly inflate the
15541 * layout_* parameters.
15542 * @see LayoutInflater
15543 */
15544 public static View inflate(Context context, int resource, ViewGroup root) {
15545 LayoutInflater factory = LayoutInflater.from(context);
15546 return factory.inflate(resource, root);
15547 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015549 /**
Adam Powell637d3372010-08-25 14:37:03 -070015550 * Scroll the view with standard behavior for scrolling beyond the normal
15551 * content boundaries. Views that call this method should override
15552 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15553 * results of an over-scroll operation.
15554 *
15555 * Views can use this method to handle any touch or fling-based scrolling.
15556 *
15557 * @param deltaX Change in X in pixels
15558 * @param deltaY Change in Y in pixels
15559 * @param scrollX Current X scroll value in pixels before applying deltaX
15560 * @param scrollY Current Y scroll value in pixels before applying deltaY
15561 * @param scrollRangeX Maximum content scroll range along the X axis
15562 * @param scrollRangeY Maximum content scroll range along the Y axis
15563 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15564 * along the X axis.
15565 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15566 * along the Y axis.
15567 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15568 * @return true if scrolling was clamped to an over-scroll boundary along either
15569 * axis, false otherwise.
15570 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015571 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015572 protected boolean overScrollBy(int deltaX, int deltaY,
15573 int scrollX, int scrollY,
15574 int scrollRangeX, int scrollRangeY,
15575 int maxOverScrollX, int maxOverScrollY,
15576 boolean isTouchEvent) {
15577 final int overScrollMode = mOverScrollMode;
15578 final boolean canScrollHorizontal =
15579 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15580 final boolean canScrollVertical =
15581 computeVerticalScrollRange() > computeVerticalScrollExtent();
15582 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15583 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15584 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15585 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15586
15587 int newScrollX = scrollX + deltaX;
15588 if (!overScrollHorizontal) {
15589 maxOverScrollX = 0;
15590 }
15591
15592 int newScrollY = scrollY + deltaY;
15593 if (!overScrollVertical) {
15594 maxOverScrollY = 0;
15595 }
15596
15597 // Clamp values if at the limits and record
15598 final int left = -maxOverScrollX;
15599 final int right = maxOverScrollX + scrollRangeX;
15600 final int top = -maxOverScrollY;
15601 final int bottom = maxOverScrollY + scrollRangeY;
15602
15603 boolean clampedX = false;
15604 if (newScrollX > right) {
15605 newScrollX = right;
15606 clampedX = true;
15607 } else if (newScrollX < left) {
15608 newScrollX = left;
15609 clampedX = true;
15610 }
15611
15612 boolean clampedY = false;
15613 if (newScrollY > bottom) {
15614 newScrollY = bottom;
15615 clampedY = true;
15616 } else if (newScrollY < top) {
15617 newScrollY = top;
15618 clampedY = true;
15619 }
15620
15621 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15622
15623 return clampedX || clampedY;
15624 }
15625
15626 /**
15627 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15628 * respond to the results of an over-scroll operation.
15629 *
15630 * @param scrollX New X scroll value in pixels
15631 * @param scrollY New Y scroll value in pixels
15632 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15633 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15634 */
15635 protected void onOverScrolled(int scrollX, int scrollY,
15636 boolean clampedX, boolean clampedY) {
15637 // Intentionally empty.
15638 }
15639
15640 /**
15641 * Returns the over-scroll mode for this view. The result will be
15642 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15643 * (allow over-scrolling only if the view content is larger than the container),
15644 * or {@link #OVER_SCROLL_NEVER}.
15645 *
15646 * @return This view's over-scroll mode.
15647 */
15648 public int getOverScrollMode() {
15649 return mOverScrollMode;
15650 }
15651
15652 /**
15653 * Set the over-scroll mode for this view. Valid over-scroll modes are
15654 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15655 * (allow over-scrolling only if the view content is larger than the container),
15656 * or {@link #OVER_SCROLL_NEVER}.
15657 *
15658 * Setting the over-scroll mode of a view will have an effect only if the
15659 * view is capable of scrolling.
15660 *
15661 * @param overScrollMode The new over-scroll mode for this view.
15662 */
15663 public void setOverScrollMode(int overScrollMode) {
15664 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15665 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15666 overScrollMode != OVER_SCROLL_NEVER) {
15667 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15668 }
15669 mOverScrollMode = overScrollMode;
15670 }
15671
15672 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015673 * Gets a scale factor that determines the distance the view should scroll
15674 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15675 * @return The vertical scroll scale factor.
15676 * @hide
15677 */
15678 protected float getVerticalScrollFactor() {
15679 if (mVerticalScrollFactor == 0) {
15680 TypedValue outValue = new TypedValue();
15681 if (!mContext.getTheme().resolveAttribute(
15682 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15683 throw new IllegalStateException(
15684 "Expected theme to define listPreferredItemHeight.");
15685 }
15686 mVerticalScrollFactor = outValue.getDimension(
15687 mContext.getResources().getDisplayMetrics());
15688 }
15689 return mVerticalScrollFactor;
15690 }
15691
15692 /**
15693 * Gets a scale factor that determines the distance the view should scroll
15694 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15695 * @return The horizontal scroll scale factor.
15696 * @hide
15697 */
15698 protected float getHorizontalScrollFactor() {
15699 // TODO: Should use something else.
15700 return getVerticalScrollFactor();
15701 }
15702
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015703 /**
15704 * Return the value specifying the text direction or policy that was set with
15705 * {@link #setTextDirection(int)}.
15706 *
15707 * @return the defined text direction. It can be one of:
15708 *
15709 * {@link #TEXT_DIRECTION_INHERIT},
15710 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15711 * {@link #TEXT_DIRECTION_ANY_RTL},
15712 * {@link #TEXT_DIRECTION_LTR},
15713 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015714 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015715 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015716 @ViewDebug.ExportedProperty(category = "text", mapping = {
15717 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15718 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15719 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15720 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15721 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15722 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15723 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015724 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015725 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015726 }
15727
15728 /**
15729 * Set the text direction.
15730 *
15731 * @param textDirection the direction to set. Should be one of:
15732 *
15733 * {@link #TEXT_DIRECTION_INHERIT},
15734 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15735 * {@link #TEXT_DIRECTION_ANY_RTL},
15736 * {@link #TEXT_DIRECTION_LTR},
15737 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015738 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015739 */
15740 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015741 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015742 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015743 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015744 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015745 // Set the new text direction
15746 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015747 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015748 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015749 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015750 }
15751 }
15752
15753 /**
15754 * Return the resolved text direction.
15755 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015756 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15757 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15758 * up the parent chain of the view. if there is no parent, then it will return the default
15759 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15760 *
15761 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015762 *
Doug Feltcb3791202011-07-07 11:57:48 -070015763 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15764 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015765 * {@link #TEXT_DIRECTION_LTR},
15766 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015767 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015768 */
15769 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015770 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015771 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015772 resolveTextDirection();
15773 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015774 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015775 }
15776
15777 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015778 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15779 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015780 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015781 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015782 // Reset any previous text direction resolution
15783 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15784
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015785 if (hasRtlSupport()) {
15786 // Set resolved text direction flag depending on text direction flag
15787 final int textDirection = getTextDirection();
15788 switch(textDirection) {
15789 case TEXT_DIRECTION_INHERIT:
15790 if (canResolveTextDirection()) {
15791 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015792
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015793 // Set current resolved direction to the same value as the parent's one
15794 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15795 switch (parentResolvedDirection) {
15796 case TEXT_DIRECTION_FIRST_STRONG:
15797 case TEXT_DIRECTION_ANY_RTL:
15798 case TEXT_DIRECTION_LTR:
15799 case TEXT_DIRECTION_RTL:
15800 case TEXT_DIRECTION_LOCALE:
15801 mPrivateFlags2 |=
15802 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15803 break;
15804 default:
15805 // Default resolved direction is "first strong" heuristic
15806 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15807 }
15808 } else {
15809 // We cannot do the resolution if there is no parent, so use the default one
15810 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015811 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015812 break;
15813 case TEXT_DIRECTION_FIRST_STRONG:
15814 case TEXT_DIRECTION_ANY_RTL:
15815 case TEXT_DIRECTION_LTR:
15816 case TEXT_DIRECTION_RTL:
15817 case TEXT_DIRECTION_LOCALE:
15818 // Resolved direction is the same as text direction
15819 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15820 break;
15821 default:
15822 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015823 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015824 }
15825 } else {
15826 // Default resolved direction is "first strong" heuristic
15827 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015828 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015829
15830 // Set to resolved
15831 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015832 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015833 }
15834
15835 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015836 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015837 * resolution should override this method.
15838 *
15839 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015840 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015841 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015842 }
15843
15844 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015845 * Check if text direction resolution can be done.
15846 *
15847 * @return true if text direction resolution can be done otherwise return false.
15848 */
15849 public boolean canResolveTextDirection() {
15850 switch (getTextDirection()) {
15851 case TEXT_DIRECTION_INHERIT:
15852 return (mParent != null) && (mParent instanceof ViewGroup);
15853 default:
15854 return true;
15855 }
15856 }
15857
15858 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015859 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015860 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015861 * reset is done.
15862 */
15863 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015864 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015865 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015866 }
15867
15868 /**
15869 * Called when text direction is reset. Subclasses that care about text direction reset should
15870 * override this method and do a reset of the text direction of their children. The default
15871 * implementation does nothing.
15872 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015873 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015874 }
15875
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015876 /**
15877 * Return the value specifying the text alignment or policy that was set with
15878 * {@link #setTextAlignment(int)}.
15879 *
15880 * @return the defined text alignment. It can be one of:
15881 *
15882 * {@link #TEXT_ALIGNMENT_INHERIT},
15883 * {@link #TEXT_ALIGNMENT_GRAVITY},
15884 * {@link #TEXT_ALIGNMENT_CENTER},
15885 * {@link #TEXT_ALIGNMENT_TEXT_START},
15886 * {@link #TEXT_ALIGNMENT_TEXT_END},
15887 * {@link #TEXT_ALIGNMENT_VIEW_START},
15888 * {@link #TEXT_ALIGNMENT_VIEW_END}
15889 */
15890 @ViewDebug.ExportedProperty(category = "text", mapping = {
15891 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15892 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15893 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15894 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15895 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15896 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15897 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15898 })
15899 public int getTextAlignment() {
15900 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15901 }
15902
15903 /**
15904 * Set the text alignment.
15905 *
15906 * @param textAlignment The text alignment to set. Should be one of
15907 *
15908 * {@link #TEXT_ALIGNMENT_INHERIT},
15909 * {@link #TEXT_ALIGNMENT_GRAVITY},
15910 * {@link #TEXT_ALIGNMENT_CENTER},
15911 * {@link #TEXT_ALIGNMENT_TEXT_START},
15912 * {@link #TEXT_ALIGNMENT_TEXT_END},
15913 * {@link #TEXT_ALIGNMENT_VIEW_START},
15914 * {@link #TEXT_ALIGNMENT_VIEW_END}
15915 *
15916 * @attr ref android.R.styleable#View_textAlignment
15917 */
15918 public void setTextAlignment(int textAlignment) {
15919 if (textAlignment != getTextAlignment()) {
15920 // Reset the current and resolved text alignment
15921 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15922 resetResolvedTextAlignment();
15923 // Set the new text alignment
15924 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15925 // Refresh
15926 requestLayout();
15927 invalidate(true);
15928 }
15929 }
15930
15931 /**
15932 * Return the resolved text alignment.
15933 *
15934 * The resolved text alignment. This needs resolution if the value is
15935 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15936 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15937 *
15938 * @return the resolved text alignment. Returns one of:
15939 *
15940 * {@link #TEXT_ALIGNMENT_GRAVITY},
15941 * {@link #TEXT_ALIGNMENT_CENTER},
15942 * {@link #TEXT_ALIGNMENT_TEXT_START},
15943 * {@link #TEXT_ALIGNMENT_TEXT_END},
15944 * {@link #TEXT_ALIGNMENT_VIEW_START},
15945 * {@link #TEXT_ALIGNMENT_VIEW_END}
15946 */
15947 @ViewDebug.ExportedProperty(category = "text", mapping = {
15948 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15949 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15950 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15951 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15952 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15953 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15954 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15955 })
15956 public int getResolvedTextAlignment() {
15957 // If text alignment is not resolved, then resolve it
15958 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15959 resolveTextAlignment();
15960 }
15961 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15962 }
15963
15964 /**
15965 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15966 * resolution is done.
15967 */
15968 public void resolveTextAlignment() {
15969 // Reset any previous text alignment resolution
15970 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15971
15972 if (hasRtlSupport()) {
15973 // Set resolved text alignment flag depending on text alignment flag
15974 final int textAlignment = getTextAlignment();
15975 switch (textAlignment) {
15976 case TEXT_ALIGNMENT_INHERIT:
15977 // Check if we can resolve the text alignment
15978 if (canResolveLayoutDirection() && mParent instanceof View) {
15979 View view = (View) mParent;
15980
15981 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15982 switch (parentResolvedTextAlignment) {
15983 case TEXT_ALIGNMENT_GRAVITY:
15984 case TEXT_ALIGNMENT_TEXT_START:
15985 case TEXT_ALIGNMENT_TEXT_END:
15986 case TEXT_ALIGNMENT_CENTER:
15987 case TEXT_ALIGNMENT_VIEW_START:
15988 case TEXT_ALIGNMENT_VIEW_END:
15989 // Resolved text alignment is the same as the parent resolved
15990 // text alignment
15991 mPrivateFlags2 |=
15992 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15993 break;
15994 default:
15995 // Use default resolved text alignment
15996 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15997 }
15998 }
15999 else {
16000 // We cannot do the resolution if there is no parent so use the default
16001 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16002 }
16003 break;
16004 case TEXT_ALIGNMENT_GRAVITY:
16005 case TEXT_ALIGNMENT_TEXT_START:
16006 case TEXT_ALIGNMENT_TEXT_END:
16007 case TEXT_ALIGNMENT_CENTER:
16008 case TEXT_ALIGNMENT_VIEW_START:
16009 case TEXT_ALIGNMENT_VIEW_END:
16010 // Resolved text alignment is the same as text alignment
16011 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16012 break;
16013 default:
16014 // Use default resolved text alignment
16015 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16016 }
16017 } else {
16018 // Use default resolved text alignment
16019 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16020 }
16021
16022 // Set the resolved
16023 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16024 onResolvedTextAlignmentChanged();
16025 }
16026
16027 /**
16028 * Check if text alignment resolution can be done.
16029 *
16030 * @return true if text alignment resolution can be done otherwise return false.
16031 */
16032 public boolean canResolveTextAlignment() {
16033 switch (getTextAlignment()) {
16034 case TEXT_DIRECTION_INHERIT:
16035 return (mParent != null);
16036 default:
16037 return true;
16038 }
16039 }
16040
16041 /**
16042 * Called when text alignment has been resolved. Subclasses that care about text alignment
16043 * resolution should override this method.
16044 *
16045 * The default implementation does nothing.
16046 */
16047 public void onResolvedTextAlignmentChanged() {
16048 }
16049
16050 /**
16051 * Reset resolved text alignment. Text alignment can be resolved with a call to
16052 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16053 * reset is done.
16054 */
16055 public void resetResolvedTextAlignment() {
16056 // Reset any previous text alignment resolution
16057 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16058 onResolvedTextAlignmentReset();
16059 }
16060
16061 /**
16062 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16063 * override this method and do a reset of the text alignment of their children. The default
16064 * implementation does nothing.
16065 */
16066 public void onResolvedTextAlignmentReset() {
16067 }
16068
Chet Haaseb39f0512011-05-24 14:36:40 -070016069 //
16070 // Properties
16071 //
16072 /**
16073 * A Property wrapper around the <code>alpha</code> functionality handled by the
16074 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16075 */
Chet Haased47f1532011-12-16 11:18:52 -080016076 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016077 @Override
16078 public void setValue(View object, float value) {
16079 object.setAlpha(value);
16080 }
16081
16082 @Override
16083 public Float get(View object) {
16084 return object.getAlpha();
16085 }
16086 };
16087
16088 /**
16089 * A Property wrapper around the <code>translationX</code> functionality handled by the
16090 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16091 */
Chet Haased47f1532011-12-16 11:18:52 -080016092 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016093 @Override
16094 public void setValue(View object, float value) {
16095 object.setTranslationX(value);
16096 }
16097
16098 @Override
16099 public Float get(View object) {
16100 return object.getTranslationX();
16101 }
16102 };
16103
16104 /**
16105 * A Property wrapper around the <code>translationY</code> functionality handled by the
16106 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16107 */
Chet Haased47f1532011-12-16 11:18:52 -080016108 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016109 @Override
16110 public void setValue(View object, float value) {
16111 object.setTranslationY(value);
16112 }
16113
16114 @Override
16115 public Float get(View object) {
16116 return object.getTranslationY();
16117 }
16118 };
16119
16120 /**
16121 * A Property wrapper around the <code>x</code> functionality handled by the
16122 * {@link View#setX(float)} and {@link View#getX()} methods.
16123 */
Chet Haased47f1532011-12-16 11:18:52 -080016124 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016125 @Override
16126 public void setValue(View object, float value) {
16127 object.setX(value);
16128 }
16129
16130 @Override
16131 public Float get(View object) {
16132 return object.getX();
16133 }
16134 };
16135
16136 /**
16137 * A Property wrapper around the <code>y</code> functionality handled by the
16138 * {@link View#setY(float)} and {@link View#getY()} methods.
16139 */
Chet Haased47f1532011-12-16 11:18:52 -080016140 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016141 @Override
16142 public void setValue(View object, float value) {
16143 object.setY(value);
16144 }
16145
16146 @Override
16147 public Float get(View object) {
16148 return object.getY();
16149 }
16150 };
16151
16152 /**
16153 * A Property wrapper around the <code>rotation</code> functionality handled by the
16154 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16155 */
Chet Haased47f1532011-12-16 11:18:52 -080016156 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016157 @Override
16158 public void setValue(View object, float value) {
16159 object.setRotation(value);
16160 }
16161
16162 @Override
16163 public Float get(View object) {
16164 return object.getRotation();
16165 }
16166 };
16167
16168 /**
16169 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16170 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16171 */
Chet Haased47f1532011-12-16 11:18:52 -080016172 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016173 @Override
16174 public void setValue(View object, float value) {
16175 object.setRotationX(value);
16176 }
16177
16178 @Override
16179 public Float get(View object) {
16180 return object.getRotationX();
16181 }
16182 };
16183
16184 /**
16185 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16186 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16187 */
Chet Haased47f1532011-12-16 11:18:52 -080016188 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016189 @Override
16190 public void setValue(View object, float value) {
16191 object.setRotationY(value);
16192 }
16193
16194 @Override
16195 public Float get(View object) {
16196 return object.getRotationY();
16197 }
16198 };
16199
16200 /**
16201 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16202 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16203 */
Chet Haased47f1532011-12-16 11:18:52 -080016204 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016205 @Override
16206 public void setValue(View object, float value) {
16207 object.setScaleX(value);
16208 }
16209
16210 @Override
16211 public Float get(View object) {
16212 return object.getScaleX();
16213 }
16214 };
16215
16216 /**
16217 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16218 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16219 */
Chet Haased47f1532011-12-16 11:18:52 -080016220 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016221 @Override
16222 public void setValue(View object, float value) {
16223 object.setScaleY(value);
16224 }
16225
16226 @Override
16227 public Float get(View object) {
16228 return object.getScaleY();
16229 }
16230 };
16231
Jeff Brown33bbfd22011-02-24 20:55:35 -080016232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016233 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16234 * Each MeasureSpec represents a requirement for either the width or the height.
16235 * A MeasureSpec is comprised of a size and a mode. There are three possible
16236 * modes:
16237 * <dl>
16238 * <dt>UNSPECIFIED</dt>
16239 * <dd>
16240 * The parent has not imposed any constraint on the child. It can be whatever size
16241 * it wants.
16242 * </dd>
16243 *
16244 * <dt>EXACTLY</dt>
16245 * <dd>
16246 * The parent has determined an exact size for the child. The child is going to be
16247 * given those bounds regardless of how big it wants to be.
16248 * </dd>
16249 *
16250 * <dt>AT_MOST</dt>
16251 * <dd>
16252 * The child can be as large as it wants up to the specified size.
16253 * </dd>
16254 * </dl>
16255 *
16256 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16257 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16258 */
16259 public static class MeasureSpec {
16260 private static final int MODE_SHIFT = 30;
16261 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16262
16263 /**
16264 * Measure specification mode: The parent has not imposed any constraint
16265 * on the child. It can be whatever size it wants.
16266 */
16267 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16268
16269 /**
16270 * Measure specification mode: The parent has determined an exact size
16271 * for the child. The child is going to be given those bounds regardless
16272 * of how big it wants to be.
16273 */
16274 public static final int EXACTLY = 1 << MODE_SHIFT;
16275
16276 /**
16277 * Measure specification mode: The child can be as large as it wants up
16278 * to the specified size.
16279 */
16280 public static final int AT_MOST = 2 << MODE_SHIFT;
16281
16282 /**
16283 * Creates a measure specification based on the supplied size and mode.
16284 *
16285 * The mode must always be one of the following:
16286 * <ul>
16287 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16288 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16289 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16290 * </ul>
16291 *
16292 * @param size the size of the measure specification
16293 * @param mode the mode of the measure specification
16294 * @return the measure specification based on size and mode
16295 */
16296 public static int makeMeasureSpec(int size, int mode) {
16297 return size + mode;
16298 }
16299
16300 /**
16301 * Extracts the mode from the supplied measure specification.
16302 *
16303 * @param measureSpec the measure specification to extract the mode from
16304 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16305 * {@link android.view.View.MeasureSpec#AT_MOST} or
16306 * {@link android.view.View.MeasureSpec#EXACTLY}
16307 */
16308 public static int getMode(int measureSpec) {
16309 return (measureSpec & MODE_MASK);
16310 }
16311
16312 /**
16313 * Extracts the size from the supplied measure specification.
16314 *
16315 * @param measureSpec the measure specification to extract the size from
16316 * @return the size in pixels defined in the supplied measure specification
16317 */
16318 public static int getSize(int measureSpec) {
16319 return (measureSpec & ~MODE_MASK);
16320 }
16321
16322 /**
16323 * Returns a String representation of the specified measure
16324 * specification.
16325 *
16326 * @param measureSpec the measure specification to convert to a String
16327 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16328 */
16329 public static String toString(int measureSpec) {
16330 int mode = getMode(measureSpec);
16331 int size = getSize(measureSpec);
16332
16333 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16334
16335 if (mode == UNSPECIFIED)
16336 sb.append("UNSPECIFIED ");
16337 else if (mode == EXACTLY)
16338 sb.append("EXACTLY ");
16339 else if (mode == AT_MOST)
16340 sb.append("AT_MOST ");
16341 else
16342 sb.append(mode).append(" ");
16343
16344 sb.append(size);
16345 return sb.toString();
16346 }
16347 }
16348
16349 class CheckForLongPress implements Runnable {
16350
16351 private int mOriginalWindowAttachCount;
16352
16353 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016354 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016355 && mOriginalWindowAttachCount == mWindowAttachCount) {
16356 if (performLongClick()) {
16357 mHasPerformedLongPress = true;
16358 }
16359 }
16360 }
16361
16362 public void rememberWindowAttachCount() {
16363 mOriginalWindowAttachCount = mWindowAttachCount;
16364 }
16365 }
Joe Malin32736f02011-01-19 16:14:20 -080016366
Adam Powelle14579b2009-12-16 18:39:52 -080016367 private final class CheckForTap implements Runnable {
16368 public void run() {
16369 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016370 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016371 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016372 }
16373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016374
Adam Powella35d7682010-03-12 14:48:13 -080016375 private final class PerformClick implements Runnable {
16376 public void run() {
16377 performClick();
16378 }
16379 }
16380
Dianne Hackborn63042d62011-01-26 18:56:29 -080016381 /** @hide */
16382 public void hackTurnOffWindowResizeAnim(boolean off) {
16383 mAttachInfo.mTurnOffWindowResizeAnim = off;
16384 }
Joe Malin32736f02011-01-19 16:14:20 -080016385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016386 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016387 * This method returns a ViewPropertyAnimator object, which can be used to animate
16388 * specific properties on this View.
16389 *
16390 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16391 */
16392 public ViewPropertyAnimator animate() {
16393 if (mAnimator == null) {
16394 mAnimator = new ViewPropertyAnimator(this);
16395 }
16396 return mAnimator;
16397 }
16398
16399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016400 * Interface definition for a callback to be invoked when a key event is
16401 * dispatched to this view. The callback will be invoked before the key
16402 * event is given to the view.
16403 */
16404 public interface OnKeyListener {
16405 /**
16406 * Called when a key is dispatched to a view. This allows listeners to
16407 * get a chance to respond before the target view.
16408 *
16409 * @param v The view the key has been dispatched to.
16410 * @param keyCode The code for the physical key that was pressed
16411 * @param event The KeyEvent object containing full information about
16412 * the event.
16413 * @return True if the listener has consumed the event, false otherwise.
16414 */
16415 boolean onKey(View v, int keyCode, KeyEvent event);
16416 }
16417
16418 /**
16419 * Interface definition for a callback to be invoked when a touch event is
16420 * dispatched to this view. The callback will be invoked before the touch
16421 * event is given to the view.
16422 */
16423 public interface OnTouchListener {
16424 /**
16425 * Called when a touch event is dispatched to a view. This allows listeners to
16426 * get a chance to respond before the target view.
16427 *
16428 * @param v The view the touch event has been dispatched to.
16429 * @param event The MotionEvent object containing full information about
16430 * the event.
16431 * @return True if the listener has consumed the event, false otherwise.
16432 */
16433 boolean onTouch(View v, MotionEvent event);
16434 }
16435
16436 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016437 * Interface definition for a callback to be invoked when a hover event is
16438 * dispatched to this view. The callback will be invoked before the hover
16439 * event is given to the view.
16440 */
16441 public interface OnHoverListener {
16442 /**
16443 * Called when a hover event is dispatched to a view. This allows listeners to
16444 * get a chance to respond before the target view.
16445 *
16446 * @param v The view the hover event has been dispatched to.
16447 * @param event The MotionEvent object containing full information about
16448 * the event.
16449 * @return True if the listener has consumed the event, false otherwise.
16450 */
16451 boolean onHover(View v, MotionEvent event);
16452 }
16453
16454 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016455 * Interface definition for a callback to be invoked when a generic motion event is
16456 * dispatched to this view. The callback will be invoked before the generic motion
16457 * event is given to the view.
16458 */
16459 public interface OnGenericMotionListener {
16460 /**
16461 * Called when a generic motion event is dispatched to a view. This allows listeners to
16462 * get a chance to respond before the target view.
16463 *
16464 * @param v The view the generic motion event has been dispatched to.
16465 * @param event The MotionEvent object containing full information about
16466 * the event.
16467 * @return True if the listener has consumed the event, false otherwise.
16468 */
16469 boolean onGenericMotion(View v, MotionEvent event);
16470 }
16471
16472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016473 * Interface definition for a callback to be invoked when a view has been clicked and held.
16474 */
16475 public interface OnLongClickListener {
16476 /**
16477 * Called when a view has been clicked and held.
16478 *
16479 * @param v The view that was clicked and held.
16480 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016481 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016482 */
16483 boolean onLongClick(View v);
16484 }
16485
16486 /**
Chris Tate32affef2010-10-18 15:29:21 -070016487 * Interface definition for a callback to be invoked when a drag is being dispatched
16488 * to this view. The callback will be invoked before the hosting view's own
16489 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16490 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016491 *
16492 * <div class="special reference">
16493 * <h3>Developer Guides</h3>
16494 * <p>For a guide to implementing drag and drop features, read the
16495 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16496 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016497 */
16498 public interface OnDragListener {
16499 /**
16500 * Called when a drag event is dispatched to a view. This allows listeners
16501 * to get a chance to override base View behavior.
16502 *
Joe Malin32736f02011-01-19 16:14:20 -080016503 * @param v The View that received the drag event.
16504 * @param event The {@link android.view.DragEvent} object for the drag event.
16505 * @return {@code true} if the drag event was handled successfully, or {@code false}
16506 * if the drag event was not handled. Note that {@code false} will trigger the View
16507 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016508 */
16509 boolean onDrag(View v, DragEvent event);
16510 }
16511
16512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016513 * Interface definition for a callback to be invoked when the focus state of
16514 * a view changed.
16515 */
16516 public interface OnFocusChangeListener {
16517 /**
16518 * Called when the focus state of a view has changed.
16519 *
16520 * @param v The view whose state has changed.
16521 * @param hasFocus The new focus state of v.
16522 */
16523 void onFocusChange(View v, boolean hasFocus);
16524 }
16525
16526 /**
16527 * Interface definition for a callback to be invoked when a view is clicked.
16528 */
16529 public interface OnClickListener {
16530 /**
16531 * Called when a view has been clicked.
16532 *
16533 * @param v The view that was clicked.
16534 */
16535 void onClick(View v);
16536 }
16537
16538 /**
16539 * Interface definition for a callback to be invoked when the context menu
16540 * for this view is being built.
16541 */
16542 public interface OnCreateContextMenuListener {
16543 /**
16544 * Called when the context menu for this view is being built. It is not
16545 * safe to hold onto the menu after this method returns.
16546 *
16547 * @param menu The context menu that is being built
16548 * @param v The view for which the context menu is being built
16549 * @param menuInfo Extra information about the item for which the
16550 * context menu should be shown. This information will vary
16551 * depending on the class of v.
16552 */
16553 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16554 }
16555
Joe Onorato664644d2011-01-23 17:53:23 -080016556 /**
16557 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016558 * visibility. This reports <strong>global</strong> changes to the system UI
16559 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016560 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016561 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016562 */
16563 public interface OnSystemUiVisibilityChangeListener {
16564 /**
16565 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016566 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016567 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016568 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016569 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16570 * <strong>global</strong> state of the UI visibility flags, not what your
16571 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016572 */
16573 public void onSystemUiVisibilityChange(int visibility);
16574 }
16575
Adam Powell4afd62b2011-02-18 15:02:18 -080016576 /**
16577 * Interface definition for a callback to be invoked when this view is attached
16578 * or detached from its window.
16579 */
16580 public interface OnAttachStateChangeListener {
16581 /**
16582 * Called when the view is attached to a window.
16583 * @param v The view that was attached
16584 */
16585 public void onViewAttachedToWindow(View v);
16586 /**
16587 * Called when the view is detached from a window.
16588 * @param v The view that was detached
16589 */
16590 public void onViewDetachedFromWindow(View v);
16591 }
16592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016593 private final class UnsetPressedState implements Runnable {
16594 public void run() {
16595 setPressed(false);
16596 }
16597 }
16598
16599 /**
16600 * Base class for derived classes that want to save and restore their own
16601 * state in {@link android.view.View#onSaveInstanceState()}.
16602 */
16603 public static class BaseSavedState extends AbsSavedState {
16604 /**
16605 * Constructor used when reading from a parcel. Reads the state of the superclass.
16606 *
16607 * @param source
16608 */
16609 public BaseSavedState(Parcel source) {
16610 super(source);
16611 }
16612
16613 /**
16614 * Constructor called by derived classes when creating their SavedState objects
16615 *
16616 * @param superState The state of the superclass of this view
16617 */
16618 public BaseSavedState(Parcelable superState) {
16619 super(superState);
16620 }
16621
16622 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16623 new Parcelable.Creator<BaseSavedState>() {
16624 public BaseSavedState createFromParcel(Parcel in) {
16625 return new BaseSavedState(in);
16626 }
16627
16628 public BaseSavedState[] newArray(int size) {
16629 return new BaseSavedState[size];
16630 }
16631 };
16632 }
16633
16634 /**
16635 * A set of information given to a view when it is attached to its parent
16636 * window.
16637 */
16638 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016639 interface Callbacks {
16640 void playSoundEffect(int effectId);
16641 boolean performHapticFeedback(int effectId, boolean always);
16642 }
16643
16644 /**
16645 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16646 * to a Handler. This class contains the target (View) to invalidate and
16647 * the coordinates of the dirty rectangle.
16648 *
16649 * For performance purposes, this class also implements a pool of up to
16650 * POOL_LIMIT objects that get reused. This reduces memory allocations
16651 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016652 */
Romain Guyd928d682009-03-31 17:52:16 -070016653 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016654 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016655 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16656 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016657 public InvalidateInfo newInstance() {
16658 return new InvalidateInfo();
16659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016660
Romain Guyd928d682009-03-31 17:52:16 -070016661 public void onAcquired(InvalidateInfo element) {
16662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016663
Romain Guyd928d682009-03-31 17:52:16 -070016664 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016665 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016666 }
16667 }, POOL_LIMIT)
16668 );
16669
16670 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016671 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016672
16673 View target;
16674
16675 int left;
16676 int top;
16677 int right;
16678 int bottom;
16679
Romain Guyd928d682009-03-31 17:52:16 -070016680 public void setNextPoolable(InvalidateInfo element) {
16681 mNext = element;
16682 }
16683
16684 public InvalidateInfo getNextPoolable() {
16685 return mNext;
16686 }
16687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016688 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016689 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016690 }
16691
16692 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016693 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016694 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016695
16696 public boolean isPooled() {
16697 return mIsPooled;
16698 }
16699
16700 public void setPooled(boolean isPooled) {
16701 mIsPooled = isPooled;
16702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016703 }
16704
16705 final IWindowSession mSession;
16706
16707 final IWindow mWindow;
16708
16709 final IBinder mWindowToken;
16710
16711 final Callbacks mRootCallbacks;
16712
Romain Guy59a12ca2011-06-09 17:48:21 -070016713 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016715 /**
16716 * The top view of the hierarchy.
16717 */
16718 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016720 IBinder mPanelParentWindowToken;
16721 Surface mSurface;
16722
Romain Guyb051e892010-09-28 19:09:36 -070016723 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016724 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016725 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016726
Romain Guy7e4e5612012-03-05 14:37:29 -080016727 boolean mScreenOn;
16728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016729 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016730 * Scale factor used by the compatibility mode
16731 */
16732 float mApplicationScale;
16733
16734 /**
16735 * Indicates whether the application is in compatibility mode
16736 */
16737 boolean mScalingRequired;
16738
16739 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016740 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016741 */
16742 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016743
Dianne Hackborn63042d62011-01-26 18:56:29 -080016744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016745 * Left position of this view's window
16746 */
16747 int mWindowLeft;
16748
16749 /**
16750 * Top position of this view's window
16751 */
16752 int mWindowTop;
16753
16754 /**
Adam Powell26153a32010-11-08 15:22:27 -080016755 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016756 */
Adam Powell26153a32010-11-08 15:22:27 -080016757 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016758
16759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016760 * For windows that are full-screen but using insets to layout inside
16761 * of the screen decorations, these are the current insets for the
16762 * content of the window.
16763 */
16764 final Rect mContentInsets = new Rect();
16765
16766 /**
16767 * For windows that are full-screen but using insets to layout inside
16768 * of the screen decorations, these are the current insets for the
16769 * actual visible parts of the window.
16770 */
16771 final Rect mVisibleInsets = new Rect();
16772
16773 /**
16774 * The internal insets given by this window. This value is
16775 * supplied by the client (through
16776 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16777 * be given to the window manager when changed to be used in laying
16778 * out windows behind it.
16779 */
16780 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16781 = new ViewTreeObserver.InternalInsetsInfo();
16782
16783 /**
16784 * All views in the window's hierarchy that serve as scroll containers,
16785 * used to determine if the window can be resized or must be panned
16786 * to adjust for a soft input area.
16787 */
16788 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16789
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016790 final KeyEvent.DispatcherState mKeyDispatchState
16791 = new KeyEvent.DispatcherState();
16792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016793 /**
16794 * Indicates whether the view's window currently has the focus.
16795 */
16796 boolean mHasWindowFocus;
16797
16798 /**
16799 * The current visibility of the window.
16800 */
16801 int mWindowVisibility;
16802
16803 /**
16804 * Indicates the time at which drawing started to occur.
16805 */
16806 long mDrawingTime;
16807
16808 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016809 * Indicates whether or not ignoring the DIRTY_MASK flags.
16810 */
16811 boolean mIgnoreDirtyState;
16812
16813 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016814 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16815 * to avoid clearing that flag prematurely.
16816 */
16817 boolean mSetIgnoreDirtyState = false;
16818
16819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016820 * Indicates whether the view's window is currently in touch mode.
16821 */
16822 boolean mInTouchMode;
16823
16824 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016825 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016826 * the next time it performs a traversal
16827 */
16828 boolean mRecomputeGlobalAttributes;
16829
16830 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016831 * Always report new attributes at next traversal.
16832 */
16833 boolean mForceReportNewAttributes;
16834
16835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016836 * Set during a traveral if any views want to keep the screen on.
16837 */
16838 boolean mKeepScreenOn;
16839
16840 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016841 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16842 */
16843 int mSystemUiVisibility;
16844
16845 /**
16846 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16847 * attached.
16848 */
16849 boolean mHasSystemUiListeners;
16850
16851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016852 * Set if the visibility of any views has changed.
16853 */
16854 boolean mViewVisibilityChanged;
16855
16856 /**
16857 * Set to true if a view has been scrolled.
16858 */
16859 boolean mViewScrollChanged;
16860
16861 /**
16862 * Global to the view hierarchy used as a temporary for dealing with
16863 * x/y points in the transparent region computations.
16864 */
16865 final int[] mTransparentLocation = new int[2];
16866
16867 /**
16868 * Global to the view hierarchy used as a temporary for dealing with
16869 * x/y points in the ViewGroup.invalidateChild implementation.
16870 */
16871 final int[] mInvalidateChildLocation = new int[2];
16872
Chet Haasec3aa3612010-06-17 08:50:37 -070016873
16874 /**
16875 * Global to the view hierarchy used as a temporary for dealing with
16876 * x/y location when view is transformed.
16877 */
16878 final float[] mTmpTransformLocation = new float[2];
16879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016880 /**
16881 * The view tree observer used to dispatch global events like
16882 * layout, pre-draw, touch mode change, etc.
16883 */
16884 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16885
16886 /**
16887 * A Canvas used by the view hierarchy to perform bitmap caching.
16888 */
16889 Canvas mCanvas;
16890
16891 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016892 * The view root impl.
16893 */
16894 final ViewRootImpl mViewRootImpl;
16895
16896 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016897 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016898 * handler can be used to pump events in the UI events queue.
16899 */
16900 final Handler mHandler;
16901
16902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016903 * Temporary for use in computing invalidate rectangles while
16904 * calling up the hierarchy.
16905 */
16906 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016907
16908 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016909 * Temporary for use in computing hit areas with transformed views
16910 */
16911 final RectF mTmpTransformRect = new RectF();
16912
16913 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016914 * Temporary list for use in collecting focusable descendents of a view.
16915 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016916 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016918 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016919 * The id of the window for accessibility purposes.
16920 */
16921 int mAccessibilityWindowId = View.NO_ID;
16922
16923 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016924 * Whether to ingore not exposed for accessibility Views when
16925 * reporting the view tree to accessibility services.
16926 */
16927 boolean mIncludeNotImportantViews;
16928
16929 /**
16930 * The drawable for highlighting accessibility focus.
16931 */
16932 Drawable mAccessibilityFocusDrawable;
16933
16934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016935 * Creates a new set of attachment information with the specified
16936 * events handler and thread.
16937 *
16938 * @param handler the events handler the view must use
16939 */
16940 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016941 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016942 mSession = session;
16943 mWindow = window;
16944 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016945 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016946 mHandler = handler;
16947 mRootCallbacks = effectPlayer;
16948 }
16949 }
16950
16951 /**
16952 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16953 * is supported. This avoids keeping too many unused fields in most
16954 * instances of View.</p>
16955 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016956 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016957
Mike Cleronf116bf82009-09-27 19:14:12 -070016958 /**
16959 * Scrollbars are not visible
16960 */
16961 public static final int OFF = 0;
16962
16963 /**
16964 * Scrollbars are visible
16965 */
16966 public static final int ON = 1;
16967
16968 /**
16969 * Scrollbars are fading away
16970 */
16971 public static final int FADING = 2;
16972
16973 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016975 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016976 public int scrollBarDefaultDelayBeforeFade;
16977 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016978
16979 public int scrollBarSize;
16980 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016981 public float[] interpolatorValues;
16982 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016983
16984 public final Paint paint;
16985 public final Matrix matrix;
16986 public Shader shader;
16987
Mike Cleronf116bf82009-09-27 19:14:12 -070016988 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16989
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016990 private static final float[] OPAQUE = { 255 };
16991 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016992
Mike Cleronf116bf82009-09-27 19:14:12 -070016993 /**
16994 * When fading should start. This time moves into the future every time
16995 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16996 */
16997 public long fadeStartTime;
16998
16999
17000 /**
17001 * The current state of the scrollbars: ON, OFF, or FADING
17002 */
17003 public int state = OFF;
17004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017005 private int mLastColor;
17006
Mike Cleronf116bf82009-09-27 19:14:12 -070017007 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017008 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17009 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017010 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17011 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017012
17013 paint = new Paint();
17014 matrix = new Matrix();
17015 // use use a height of 1, and then wack the matrix each time we
17016 // actually use it.
17017 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017019 paint.setShader(shader);
17020 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017021 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017022 }
Romain Guy8506ab42009-06-11 17:35:47 -070017023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017024 public void setFadeColor(int color) {
17025 if (color != 0 && color != mLastColor) {
17026 mLastColor = color;
17027 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017028
Romain Guye55e1a72009-08-27 10:42:26 -070017029 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17030 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017032 paint.setShader(shader);
17033 // Restore the default transfer mode (src_over)
17034 paint.setXfermode(null);
17035 }
17036 }
Joe Malin32736f02011-01-19 16:14:20 -080017037
Mike Cleronf116bf82009-09-27 19:14:12 -070017038 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017039 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017040 if (now >= fadeStartTime) {
17041
17042 // the animation fades the scrollbars out by changing
17043 // the opacity (alpha) from fully opaque to fully
17044 // transparent
17045 int nextFrame = (int) now;
17046 int framesCount = 0;
17047
17048 Interpolator interpolator = scrollBarInterpolator;
17049
17050 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017051 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017052
17053 // End transparent
17054 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017055 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017056
17057 state = FADING;
17058
17059 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017060 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017061 }
17062 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017063 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017064
Svetoslav Ganova0156172011-06-26 17:55:44 -070017065 /**
17066 * Resuable callback for sending
17067 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17068 */
17069 private class SendViewScrolledAccessibilityEvent implements Runnable {
17070 public volatile boolean mIsPending;
17071
17072 public void run() {
17073 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17074 mIsPending = false;
17075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017076 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017077
17078 /**
17079 * <p>
17080 * This class represents a delegate that can be registered in a {@link View}
17081 * to enhance accessibility support via composition rather via inheritance.
17082 * It is specifically targeted to widget developers that extend basic View
17083 * classes i.e. classes in package android.view, that would like their
17084 * applications to be backwards compatible.
17085 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017086 * <div class="special reference">
17087 * <h3>Developer Guides</h3>
17088 * <p>For more information about making applications accessible, read the
17089 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17090 * developer guide.</p>
17091 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017092 * <p>
17093 * A scenario in which a developer would like to use an accessibility delegate
17094 * is overriding a method introduced in a later API version then the minimal API
17095 * version supported by the application. For example, the method
17096 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17097 * in API version 4 when the accessibility APIs were first introduced. If a
17098 * developer would like his application to run on API version 4 devices (assuming
17099 * all other APIs used by the application are version 4 or lower) and take advantage
17100 * of this method, instead of overriding the method which would break the application's
17101 * backwards compatibility, he can override the corresponding method in this
17102 * delegate and register the delegate in the target View if the API version of
17103 * the system is high enough i.e. the API version is same or higher to the API
17104 * version that introduced
17105 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17106 * </p>
17107 * <p>
17108 * Here is an example implementation:
17109 * </p>
17110 * <code><pre><p>
17111 * if (Build.VERSION.SDK_INT >= 14) {
17112 * // If the API version is equal of higher than the version in
17113 * // which onInitializeAccessibilityNodeInfo was introduced we
17114 * // register a delegate with a customized implementation.
17115 * View view = findViewById(R.id.view_id);
17116 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17117 * public void onInitializeAccessibilityNodeInfo(View host,
17118 * AccessibilityNodeInfo info) {
17119 * // Let the default implementation populate the info.
17120 * super.onInitializeAccessibilityNodeInfo(host, info);
17121 * // Set some other information.
17122 * info.setEnabled(host.isEnabled());
17123 * }
17124 * });
17125 * }
17126 * </code></pre></p>
17127 * <p>
17128 * This delegate contains methods that correspond to the accessibility methods
17129 * in View. If a delegate has been specified the implementation in View hands
17130 * off handling to the corresponding method in this delegate. The default
17131 * implementation the delegate methods behaves exactly as the corresponding
17132 * method in View for the case of no accessibility delegate been set. Hence,
17133 * to customize the behavior of a View method, clients can override only the
17134 * corresponding delegate method without altering the behavior of the rest
17135 * accessibility related methods of the host view.
17136 * </p>
17137 */
17138 public static class AccessibilityDelegate {
17139
17140 /**
17141 * Sends an accessibility event of the given type. If accessibility is not
17142 * enabled this method has no effect.
17143 * <p>
17144 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17145 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17146 * been set.
17147 * </p>
17148 *
17149 * @param host The View hosting the delegate.
17150 * @param eventType The type of the event to send.
17151 *
17152 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17153 */
17154 public void sendAccessibilityEvent(View host, int eventType) {
17155 host.sendAccessibilityEventInternal(eventType);
17156 }
17157
17158 /**
17159 * Sends an accessibility event. This method behaves exactly as
17160 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17161 * empty {@link AccessibilityEvent} and does not perform a check whether
17162 * accessibility is enabled.
17163 * <p>
17164 * The default implementation behaves as
17165 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17166 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17167 * the case of no accessibility delegate been set.
17168 * </p>
17169 *
17170 * @param host The View hosting the delegate.
17171 * @param event The event to send.
17172 *
17173 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17174 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17175 */
17176 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17177 host.sendAccessibilityEventUncheckedInternal(event);
17178 }
17179
17180 /**
17181 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17182 * to its children for adding their text content to the event.
17183 * <p>
17184 * The default implementation behaves as
17185 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17186 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17187 * the case of no accessibility delegate been set.
17188 * </p>
17189 *
17190 * @param host The View hosting the delegate.
17191 * @param event The event.
17192 * @return True if the event population was completed.
17193 *
17194 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17195 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17196 */
17197 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17198 return host.dispatchPopulateAccessibilityEventInternal(event);
17199 }
17200
17201 /**
17202 * Gives a chance to the host View to populate the accessibility event with its
17203 * text content.
17204 * <p>
17205 * The default implementation behaves as
17206 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17207 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17208 * the case of no accessibility delegate been set.
17209 * </p>
17210 *
17211 * @param host The View hosting the delegate.
17212 * @param event The accessibility event which to populate.
17213 *
17214 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17215 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17216 */
17217 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17218 host.onPopulateAccessibilityEventInternal(event);
17219 }
17220
17221 /**
17222 * Initializes an {@link AccessibilityEvent} with information about the
17223 * the host View which is the event source.
17224 * <p>
17225 * The default implementation behaves as
17226 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17227 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17228 * the case of no accessibility delegate been set.
17229 * </p>
17230 *
17231 * @param host The View hosting the delegate.
17232 * @param event The event to initialize.
17233 *
17234 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17235 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17236 */
17237 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17238 host.onInitializeAccessibilityEventInternal(event);
17239 }
17240
17241 /**
17242 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17243 * <p>
17244 * The default implementation behaves as
17245 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17246 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17247 * the case of no accessibility delegate been set.
17248 * </p>
17249 *
17250 * @param host The View hosting the delegate.
17251 * @param info The instance to initialize.
17252 *
17253 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17254 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17255 */
17256 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17257 host.onInitializeAccessibilityNodeInfoInternal(info);
17258 }
17259
17260 /**
17261 * Called when a child of the host View has requested sending an
17262 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17263 * to augment the event.
17264 * <p>
17265 * The default implementation behaves as
17266 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17267 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17268 * the case of no accessibility delegate been set.
17269 * </p>
17270 *
17271 * @param host The View hosting the delegate.
17272 * @param child The child which requests sending the event.
17273 * @param event The event to be sent.
17274 * @return True if the event should be sent
17275 *
17276 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17277 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17278 */
17279 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17280 AccessibilityEvent event) {
17281 return host.onRequestSendAccessibilityEventInternal(child, event);
17282 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017283
17284 /**
17285 * Gets the provider for managing a virtual view hierarchy rooted at this View
17286 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17287 * that explore the window content.
17288 * <p>
17289 * The default implementation behaves as
17290 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17291 * the case of no accessibility delegate been set.
17292 * </p>
17293 *
17294 * @return The provider.
17295 *
17296 * @see AccessibilityNodeProvider
17297 */
17298 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17299 return null;
17300 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017302}