blob: 23c3ad3d36b79b92d93b771a59d92de0d629a33f [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;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
48import android.os.RemoteException;
49import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070050import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070051import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070053import android.util.FloatProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080055import android.util.Pools.SynchronizedPool;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070060import android.view.AccessibilityIterators.TextSegmentIterator;
61import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
62import android.view.AccessibilityIterators.WordTextSegmentIterator;
63import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityEventSource;
66import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070067import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070068import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070070import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080071import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.view.inputmethod.InputConnection;
74import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.widget.ScrollBarDrawable;
76
Romain Guy1ef3fdb2011-09-09 15:30:30 -070077import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070078import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079
Doug Feltcb3791202011-07-07 11:57:48 -070080import com.android.internal.R;
81import com.android.internal.util.Predicate;
82import com.android.internal.view.menu.MenuBuilder;
Jeff Sharkey36901b62012-09-19 19:06:22 -070083import com.google.android.collect.Lists;
84import com.google.android.collect.Maps;
Doug Feltcb3791202011-07-07 11:57:48 -070085
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
Jeff Sharkey36901b62012-09-19 19:06:22 -070087import java.lang.reflect.Field;
svetoslavganov75986cf2009-05-14 22:28:01 -070088import java.lang.reflect.InvocationTargetException;
89import java.lang.reflect.Method;
Jeff Sharkey36901b62012-09-19 19:06:22 -070090import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.ArrayList;
92import java.util.Arrays;
Jeff Sharkey36901b62012-09-19 19:06:22 -070093import java.util.Collections;
94import java.util.HashMap;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070095import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080096import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070097import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99/**
100 * <p>
101 * This class represents the basic building block for user interface components. A View
102 * occupies a rectangular area on the screen and is responsible for drawing and
103 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700104 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
106 * are invisible containers that hold other Views (or other ViewGroups) and define
107 * their layout properties.
108 * </p>
109 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700110 * <div class="special reference">
111 * <h3>Developer Guides</h3>
112 * <p>For information about using this class to develop your application's user interface,
113 * 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 -0800114 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * <a name="Using"></a>
117 * <h3>Using Views</h3>
118 * <p>
119 * All of the views in a window are arranged in a single tree. You can add views
120 * either from code or by specifying a tree of views in one or more XML layout
121 * files. There are many specialized subclasses of views that act as controls or
122 * are capable of displaying text, images, or other content.
123 * </p>
124 * <p>
125 * Once you have created a tree of views, there are typically a few types of
126 * common operations you may wish to perform:
127 * <ul>
128 * <li><strong>Set properties:</strong> for example setting the text of a
129 * {@link android.widget.TextView}. The available properties and the methods
130 * that set them will vary among the different subclasses of views. Note that
131 * properties that are known at build time can be set in the XML layout
132 * files.</li>
133 * <li><strong>Set focus:</strong> The framework will handled moving focus in
134 * response to user input. To force focus to a specific view, call
135 * {@link #requestFocus}.</li>
136 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
137 * that will be notified when something interesting happens to the view. For
138 * example, all views will let you set a listener to be notified when the view
139 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700140 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700141 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700142 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700144 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * </ul>
146 * </p>
147 * <p><em>
148 * Note: The Android framework is responsible for measuring, laying out and
149 * drawing views. You should not call methods that perform these actions on
150 * views yourself unless you are actually implementing a
151 * {@link android.view.ViewGroup}.
152 * </em></p>
153 *
154 * <a name="Lifecycle"></a>
155 * <h3>Implementing a Custom View</h3>
156 *
157 * <p>
158 * To implement a custom view, you will usually begin by providing overrides for
159 * some of the standard methods that the framework calls on all views. You do
160 * not need to override all of these methods. In fact, you can start by just
161 * overriding {@link #onDraw(android.graphics.Canvas)}.
162 * <table border="2" width="85%" align="center" cellpadding="5">
163 * <thead>
164 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
165 * </thead>
166 *
167 * <tbody>
168 * <tr>
169 * <td rowspan="2">Creation</td>
170 * <td>Constructors</td>
171 * <td>There is a form of the constructor that are called when the view
172 * is created from code and a form that is called when the view is
173 * inflated from a layout file. The second form should parse and apply
174 * any attributes defined in the layout file.
175 * </td>
176 * </tr>
177 * <tr>
178 * <td><code>{@link #onFinishInflate()}</code></td>
179 * <td>Called after a view and all of its children has been inflated
180 * from XML.</td>
181 * </tr>
182 *
183 * <tr>
184 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called to determine the size requirements for this view and all
187 * of its children.
188 * </td>
189 * </tr>
190 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700191 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <td>Called when this view should assign a size and position to all
193 * of its children.
194 * </td>
195 * </tr>
196 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700197 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * <td>Called when the size of this view has changed.
199 * </td>
200 * </tr>
201 *
202 * <tr>
203 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when the view should render its content.
206 * </td>
207 * </tr>
208 *
209 * <tr>
210 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900212 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900217 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a trackball motion event occurs.
223 * </td>
224 * </tr>
225 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700226 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * <td>Called when a touch screen motion event occurs.
228 * </td>
229 * </tr>
230 *
231 * <tr>
232 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700233 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * <td>Called when the view gains or loses focus.
235 * </td>
236 * </tr>
237 *
238 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700239 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * <td>Called when the window containing the view gains or loses focus.
241 * </td>
242 * </tr>
243 *
244 * <tr>
245 * <td rowspan="3">Attaching</td>
246 * <td><code>{@link #onAttachedToWindow()}</code></td>
247 * <td>Called when the view is attached to a window.
248 * </td>
249 * </tr>
250 *
251 * <tr>
252 * <td><code>{@link #onDetachedFromWindow}</code></td>
253 * <td>Called when the view is detached from its window.
254 * </td>
255 * </tr>
256 *
257 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700258 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * <td>Called when the visibility of the window containing the view
260 * has changed.
261 * </td>
262 * </tr>
263 * </tbody>
264 *
265 * </table>
266 * </p>
267 *
268 * <a name="IDs"></a>
269 * <h3>IDs</h3>
270 * Views may have an integer id associated with them. These ids are typically
271 * assigned in the layout XML files, and are used to find specific views within
272 * the view tree. A common pattern is to:
273 * <ul>
274 * <li>Define a Button in the layout file and assign it a unique ID.
275 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700276 * &lt;Button
277 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 * android:layout_width="wrap_content"
279 * android:layout_height="wrap_content"
280 * android:text="@string/my_button_text"/&gt;
281 * </pre></li>
282 * <li>From the onCreate method of an Activity, find the Button
283 * <pre class="prettyprint">
284 * Button myButton = (Button) findViewById(R.id.my_button);
285 * </pre></li>
286 * </ul>
287 * <p>
288 * View IDs need not be unique throughout the tree, but it is good practice to
289 * ensure that they are at least unique within the part of the tree you are
290 * searching.
291 * </p>
292 *
293 * <a name="Position"></a>
294 * <h3>Position</h3>
295 * <p>
296 * The geometry of a view is that of a rectangle. A view has a location,
297 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
298 * two dimensions, expressed as a width and a height. The unit for location
299 * and dimensions is the pixel.
300 * </p>
301 *
302 * <p>
303 * It is possible to retrieve the location of a view by invoking the methods
304 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
305 * coordinate of the rectangle representing the view. The latter returns the
306 * top, or Y, coordinate of the rectangle representing the view. These methods
307 * both return the location of the view relative to its parent. For instance,
308 * when getLeft() returns 20, that means the view is located 20 pixels to the
309 * right of the left edge of its direct parent.
310 * </p>
311 *
312 * <p>
313 * In addition, several convenience methods are offered to avoid unnecessary
314 * computations, namely {@link #getRight()} and {@link #getBottom()}.
315 * These methods return the coordinates of the right and bottom edges of the
316 * rectangle representing the view. For instance, calling {@link #getRight()}
317 * is similar to the following computation: <code>getLeft() + getWidth()</code>
318 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
319 * </p>
320 *
321 * <a name="SizePaddingMargins"></a>
322 * <h3>Size, padding and margins</h3>
323 * <p>
324 * The size of a view is expressed with a width and a height. A view actually
325 * possess two pairs of width and height values.
326 * </p>
327 *
328 * <p>
329 * The first pair is known as <em>measured width</em> and
330 * <em>measured height</em>. These dimensions define how big a view wants to be
331 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
332 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
333 * and {@link #getMeasuredHeight()}.
334 * </p>
335 *
336 * <p>
337 * The second pair is simply known as <em>width</em> and <em>height</em>, or
338 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
339 * dimensions define the actual size of the view on screen, at drawing time and
340 * after layout. These values may, but do not have to, be different from the
341 * measured width and height. The width and height can be obtained by calling
342 * {@link #getWidth()} and {@link #getHeight()}.
343 * </p>
344 *
345 * <p>
346 * To measure its dimensions, a view takes into account its padding. The padding
347 * is expressed in pixels for the left, top, right and bottom parts of the view.
348 * Padding can be used to offset the content of the view by a specific amount of
349 * pixels. For instance, a left padding of 2 will push the view's content by
350 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700351 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
352 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
353 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
354 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * </p>
356 *
357 * <p>
358 * Even though a view can define a padding, it does not provide any support for
359 * margins. However, view groups provide such a support. Refer to
360 * {@link android.view.ViewGroup} and
361 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
362 * </p>
363 *
364 * <a name="Layout"></a>
365 * <h3>Layout</h3>
366 * <p>
367 * Layout is a two pass process: a measure pass and a layout pass. The measuring
368 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
369 * of the view tree. Each view pushes dimension specifications down the tree
370 * during the recursion. At the end of the measure pass, every view has stored
371 * its measurements. The second pass happens in
372 * {@link #layout(int,int,int,int)} and is also top-down. During
373 * this pass each parent is responsible for positioning all of its children
374 * using the sizes computed in the measure pass.
375 * </p>
376 *
377 * <p>
378 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
379 * {@link #getMeasuredHeight()} values must be set, along with those for all of
380 * that view's descendants. A view's measured width and measured height values
381 * must respect the constraints imposed by the view's parents. This guarantees
382 * that at the end of the measure pass, all parents accept all of their
383 * children's measurements. A parent view may call measure() more than once on
384 * its children. For example, the parent may measure each child once with
385 * unspecified dimensions to find out how big they want to be, then call
386 * measure() on them again with actual numbers if the sum of all the children's
387 * unconstrained sizes is too big or too small.
388 * </p>
389 *
390 * <p>
391 * The measure pass uses two classes to communicate dimensions. The
392 * {@link MeasureSpec} class is used by views to tell their parents how they
393 * want to be measured and positioned. The base LayoutParams class just
394 * describes how big the view wants to be for both width and height. For each
395 * dimension, it can specify one of:
396 * <ul>
397 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800398 * <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 -0800399 * (minus padding)
400 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
401 * enclose its content (plus padding).
402 * </ul>
403 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
404 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
405 * an X and Y value.
406 * </p>
407 *
408 * <p>
409 * MeasureSpecs are used to push requirements down the tree from parent to
410 * child. A MeasureSpec can be in one of three modes:
411 * <ul>
412 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
413 * of a child view. For example, a LinearLayout may call measure() on its child
414 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
415 * tall the child view wants to be given a width of 240 pixels.
416 * <li>EXACTLY: This is used by the parent to impose an exact size on the
417 * child. The child must use this size, and guarantee that all of its
418 * descendants will fit within this size.
419 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
420 * child. The child must gurantee that it and all of its descendants will fit
421 * within this size.
422 * </ul>
423 * </p>
424 *
425 * <p>
426 * To intiate a layout, call {@link #requestLayout}. This method is typically
427 * called by a view on itself when it believes that is can no longer fit within
428 * its current bounds.
429 * </p>
430 *
431 * <a name="Drawing"></a>
432 * <h3>Drawing</h3>
433 * <p>
434 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700435 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * this means that parents will draw before (i.e., behind) their children, with
437 * siblings drawn in the order they appear in the tree.
438 * If you set a background drawable for a View, then the View will draw it for you
439 * before calling back to its <code>onDraw()</code> method.
440 * </p>
441 *
442 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700443 * 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 -0800444 * </p>
445 *
446 * <p>
447 * To force a view to draw, call {@link #invalidate()}.
448 * </p>
449 *
450 * <a name="EventHandlingThreading"></a>
451 * <h3>Event Handling and Threading</h3>
452 * <p>
453 * The basic cycle of a view is as follows:
454 * <ol>
455 * <li>An event comes in and is dispatched to the appropriate view. The view
456 * handles the event and notifies any listeners.</li>
457 * <li>If in the course of processing the event, the view's bounds may need
458 * to be changed, the view will call {@link #requestLayout()}.</li>
459 * <li>Similarly, if in the course of processing the event the view's appearance
460 * may need to be changed, the view will call {@link #invalidate()}.</li>
461 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
462 * the framework will take care of measuring, laying out, and drawing the tree
463 * as appropriate.</li>
464 * </ol>
465 * </p>
466 *
467 * <p><em>Note: The entire view tree is single threaded. You must always be on
468 * the UI thread when calling any method on any view.</em>
469 * If you are doing work on other threads and want to update the state of a view
470 * from that thread, you should use a {@link Handler}.
471 * </p>
472 *
473 * <a name="FocusHandling"></a>
474 * <h3>Focus Handling</h3>
475 * <p>
476 * The framework will handle routine focus movement in response to user input.
477 * This includes changing the focus as views are removed or hidden, or as new
478 * views become available. Views indicate their willingness to take focus
479 * through the {@link #isFocusable} method. To change whether a view can take
480 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
481 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
482 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
483 * </p>
484 * <p>
485 * Focus movement is based on an algorithm which finds the nearest neighbor in a
486 * given direction. In rare cases, the default algorithm may not match the
487 * intended behavior of the developer. In these situations, you can provide
488 * explicit overrides by using these XML attributes in the layout file:
489 * <pre>
490 * nextFocusDown
491 * nextFocusLeft
492 * nextFocusRight
493 * nextFocusUp
494 * </pre>
495 * </p>
496 *
497 *
498 * <p>
499 * To get a particular view to take focus, call {@link #requestFocus()}.
500 * </p>
501 *
502 * <a name="TouchMode"></a>
503 * <h3>Touch Mode</h3>
504 * <p>
505 * When a user is navigating a user interface via directional keys such as a D-pad, it is
506 * necessary to give focus to actionable items such as buttons so the user can see
507 * what will take input. If the device has touch capabilities, however, and the user
508 * begins interacting with the interface by touching it, it is no longer necessary to
509 * always highlight, or give focus to, a particular view. This motivates a mode
510 * for interaction named 'touch mode'.
511 * </p>
512 * <p>
513 * For a touch capable device, once the user touches the screen, the device
514 * will enter touch mode. From this point onward, only views for which
515 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
516 * Other views that are touchable, like buttons, will not take focus when touched; they will
517 * only fire the on click listeners.
518 * </p>
519 * <p>
520 * Any time a user hits a directional key, such as a D-pad direction, the view device will
521 * exit touch mode, and find a view to take focus, so that the user may resume interacting
522 * with the user interface without touching the screen again.
523 * </p>
524 * <p>
525 * The touch mode state is maintained across {@link android.app.Activity}s. Call
526 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
527 * </p>
528 *
529 * <a name="Scrolling"></a>
530 * <h3>Scrolling</h3>
531 * <p>
532 * The framework provides basic support for views that wish to internally
533 * scroll their content. This includes keeping track of the X and Y scroll
534 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800535 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700536 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 * </p>
538 *
539 * <a name="Tags"></a>
540 * <h3>Tags</h3>
541 * <p>
542 * Unlike IDs, tags are not used to identify views. Tags are essentially an
543 * extra piece of information that can be associated with a view. They are most
544 * often used as a convenience to store data related to views in the views
545 * themselves rather than by putting them in a separate structure.
546 * </p>
547 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700548 * <a name="Properties"></a>
549 * <h3>Properties</h3>
550 * <p>
551 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
552 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
553 * available both in the {@link Property} form as well as in similarly-named setter/getter
554 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
555 * be used to set persistent state associated with these rendering-related properties on the view.
556 * The properties and methods can also be used in conjunction with
557 * {@link android.animation.Animator Animator}-based animations, described more in the
558 * <a href="#Animation">Animation</a> section.
559 * </p>
560 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * <a name="Animation"></a>
562 * <h3>Animation</h3>
563 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700564 * Starting with Android 3.0, the preferred way of animating views is to use the
565 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
566 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
567 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
568 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
569 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
570 * makes animating these View properties particularly easy and efficient.
571 * </p>
572 * <p>
573 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 * You can attach an {@link Animation} object to a view using
575 * {@link #setAnimation(Animation)} or
576 * {@link #startAnimation(Animation)}. The animation can alter the scale,
577 * rotation, translation and alpha of a view over time. If the animation is
578 * attached to a view that has children, the animation will affect the entire
579 * subtree rooted by that node. When an animation is started, the framework will
580 * take care of redrawing the appropriate views until the animation completes.
581 * </p>
582 *
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * <a name="Security"></a>
584 * <h3>Security</h3>
585 * <p>
586 * Sometimes it is essential that an application be able to verify that an action
587 * is being performed with the full knowledge and consent of the user, such as
588 * granting a permission request, making a purchase or clicking on an advertisement.
589 * Unfortunately, a malicious application could try to spoof the user into
590 * performing these actions, unaware, by concealing the intended purpose of the view.
591 * As a remedy, the framework offers a touch filtering mechanism that can be used to
592 * improve the security of views that provide access to sensitive functionality.
593 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700594 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800595 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700596 * will discard touches that are received whenever the view's window is obscured by
597 * another visible window. As a result, the view will not receive touches whenever a
598 * toast, dialog or other window appears above the view's window.
599 * </p><p>
600 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700601 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
602 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700603 * </p>
604 *
Romain Guy171c5922011-01-06 10:04:23 -0800605 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_background
607 * @attr ref android.R.styleable#View_clickable
608 * @attr ref android.R.styleable#View_contentDescription
609 * @attr ref android.R.styleable#View_drawingCacheQuality
610 * @attr ref android.R.styleable#View_duplicateParentState
611 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700612 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700613 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700615 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_isScrollContainer
618 * @attr ref android.R.styleable#View_focusable
619 * @attr ref android.R.styleable#View_focusableInTouchMode
620 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
621 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800622 * @attr ref android.R.styleable#View_layerType
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800623 * @attr ref android.R.styleable#View_layoutDirection
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_longClickable
625 * @attr ref android.R.styleable#View_minHeight
626 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @attr ref android.R.styleable#View_nextFocusDown
628 * @attr ref android.R.styleable#View_nextFocusLeft
629 * @attr ref android.R.styleable#View_nextFocusRight
630 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_onClick
632 * @attr ref android.R.styleable#View_padding
633 * @attr ref android.R.styleable#View_paddingBottom
634 * @attr ref android.R.styleable#View_paddingLeft
635 * @attr ref android.R.styleable#View_paddingRight
636 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800637 * @attr ref android.R.styleable#View_paddingStart
638 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700639 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800640 * @attr ref android.R.styleable#View_rotation
641 * @attr ref android.R.styleable#View_rotationX
642 * @attr ref android.R.styleable#View_rotationY
643 * @attr ref android.R.styleable#View_scaleX
644 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollX
646 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700647 * @attr ref android.R.styleable#View_scrollbarSize
648 * @attr ref android.R.styleable#View_scrollbarStyle
649 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700650 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
651 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
653 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * @attr ref android.R.styleable#View_scrollbarThumbVertical
655 * @attr ref android.R.styleable#View_scrollbarTrackVertical
656 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
657 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700658 * @attr ref android.R.styleable#View_soundEffectsEnabled
659 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700660 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800661 * @attr ref android.R.styleable#View_textDirection
Chet Haase73066682010-11-29 15:55:32 -0800662 * @attr ref android.R.styleable#View_transformPivotX
663 * @attr ref android.R.styleable#View_transformPivotY
664 * @attr ref android.R.styleable#View_translationX
665 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700666 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 *
668 * @see android.view.ViewGroup
669 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700670public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700671 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 private static final boolean DBG = false;
673
674 /**
675 * The logging tag used by this class with android.util.Log.
676 */
677 protected static final String VIEW_LOG_TAG = "View";
678
679 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700680 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700681 *
682 * @hide
683 */
684 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
685
686 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 * Used to mark a View that has no ID.
688 */
689 public static final int NO_ID = -1;
690
691 /**
692 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
693 * calling setFlags.
694 */
695 private static final int NOT_FOCUSABLE = 0x00000000;
696
697 /**
698 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
699 * setFlags.
700 */
701 private static final int FOCUSABLE = 0x00000001;
702
703 /**
704 * Mask for use with setFlags indicating bits used for focus.
705 */
706 private static final int FOCUSABLE_MASK = 0x00000001;
707
708 /**
709 * This view will adjust its padding to fit sytem windows (e.g. status bar)
710 */
711 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
712
713 /**
Scott Main812634c22011-07-27 13:22:35 -0700714 * This view is visible.
715 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
716 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
718 public static final int VISIBLE = 0x00000000;
719
720 /**
721 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700722 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
723 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
725 public static final int INVISIBLE = 0x00000004;
726
727 /**
728 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700729 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
730 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 */
732 public static final int GONE = 0x00000008;
733
734 /**
735 * Mask for use with setFlags indicating bits used for visibility.
736 * {@hide}
737 */
738 static final int VISIBILITY_MASK = 0x0000000C;
739
740 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
741
742 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700743 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 * Use with ENABLED_MASK when calling setFlags.
745 * {@hide}
746 */
747 static final int ENABLED = 0x00000000;
748
749 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700750 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 * Use with ENABLED_MASK when calling setFlags.
752 * {@hide}
753 */
754 static final int DISABLED = 0x00000020;
755
756 /**
757 * Mask for use with setFlags indicating bits used for indicating whether
758 * this view is enabled
759 * {@hide}
760 */
761 static final int ENABLED_MASK = 0x00000020;
762
763 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700764 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
765 * called and further optimizations will be performed. It is okay to have
766 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 * {@hide}
768 */
769 static final int WILL_NOT_DRAW = 0x00000080;
770
771 /**
772 * Mask for use with setFlags indicating bits used for indicating whether
773 * this view is will draw
774 * {@hide}
775 */
776 static final int DRAW_MASK = 0x00000080;
777
778 /**
779 * <p>This view doesn't show scrollbars.</p>
780 * {@hide}
781 */
782 static final int SCROLLBARS_NONE = 0x00000000;
783
784 /**
785 * <p>This view shows horizontal scrollbars.</p>
786 * {@hide}
787 */
788 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
789
790 /**
791 * <p>This view shows vertical scrollbars.</p>
792 * {@hide}
793 */
794 static final int SCROLLBARS_VERTICAL = 0x00000200;
795
796 /**
797 * <p>Mask for use with setFlags indicating bits used for indicating which
798 * scrollbars are enabled.</p>
799 * {@hide}
800 */
801 static final int SCROLLBARS_MASK = 0x00000300;
802
Jeff Brown85a31762010-09-01 17:01:00 -0700803 /**
804 * Indicates that the view should filter touches when its window is obscured.
805 * Refer to the class comments for more information about this security feature.
806 * {@hide}
807 */
808 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
809
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700810 /**
811 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
812 * that they are optional and should be skipped if the window has
813 * requested system UI flags that ignore those insets for layout.
814 */
815 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816
817 /**
818 * <p>This view doesn't show fading edges.</p>
819 * {@hide}
820 */
821 static final int FADING_EDGE_NONE = 0x00000000;
822
823 /**
824 * <p>This view shows horizontal fading edges.</p>
825 * {@hide}
826 */
827 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
828
829 /**
830 * <p>This view shows vertical fading edges.</p>
831 * {@hide}
832 */
833 static final int FADING_EDGE_VERTICAL = 0x00002000;
834
835 /**
836 * <p>Mask for use with setFlags indicating bits used for indicating which
837 * fading edges are enabled.</p>
838 * {@hide}
839 */
840 static final int FADING_EDGE_MASK = 0x00003000;
841
842 /**
843 * <p>Indicates this view can be clicked. When clickable, a View reacts
844 * to clicks by notifying the OnClickListener.<p>
845 * {@hide}
846 */
847 static final int CLICKABLE = 0x00004000;
848
849 /**
850 * <p>Indicates this view is caching its drawing into a bitmap.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_ENABLED = 0x00008000;
854
855 /**
856 * <p>Indicates that no icicle should be saved for this view.<p>
857 * {@hide}
858 */
859 static final int SAVE_DISABLED = 0x000010000;
860
861 /**
862 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
863 * property.</p>
864 * {@hide}
865 */
866 static final int SAVE_DISABLED_MASK = 0x000010000;
867
868 /**
869 * <p>Indicates that no drawing cache should ever be created for this view.<p>
870 * {@hide}
871 */
872 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
873
874 /**
875 * <p>Indicates this view can take / keep focus when int touch mode.</p>
876 * {@hide}
877 */
878 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
879
880 /**
881 * <p>Enables low quality mode for the drawing cache.</p>
882 */
883 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
884
885 /**
886 * <p>Enables high quality mode for the drawing cache.</p>
887 */
888 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
889
890 /**
891 * <p>Enables automatic quality mode for the drawing cache.</p>
892 */
893 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
894
895 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
896 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
897 };
898
899 /**
900 * <p>Mask for use with setFlags indicating bits used for the cache
901 * quality property.</p>
902 * {@hide}
903 */
904 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
905
906 /**
907 * <p>
908 * Indicates this view can be long clicked. When long clickable, a View
909 * reacts to long clicks by notifying the OnLongClickListener or showing a
910 * context menu.
911 * </p>
912 * {@hide}
913 */
914 static final int LONG_CLICKABLE = 0x00200000;
915
916 /**
917 * <p>Indicates that this view gets its drawable states from its direct parent
918 * and ignores its original internal states.</p>
919 *
920 * @hide
921 */
922 static final int DUPLICATE_PARENT_STATE = 0x00400000;
923
924 /**
925 * The scrollbar style to display the scrollbars inside the content area,
926 * without increasing the padding. The scrollbars will be overlaid with
927 * translucency on the view's content.
928 */
929 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
930
931 /**
932 * The scrollbar style to display the scrollbars inside the padded area,
933 * increasing the padding of the view. The scrollbars will not overlap the
934 * content area of the view.
935 */
936 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
937
938 /**
939 * The scrollbar style to display the scrollbars at the edge of the view,
940 * without increasing the padding. The scrollbars will be overlaid with
941 * translucency.
942 */
943 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
944
945 /**
946 * The scrollbar style to display the scrollbars at the edge of the view,
947 * increasing the padding of the view. The scrollbars will only overlap the
948 * background, if any.
949 */
950 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
951
952 /**
953 * Mask to check if the scrollbar style is overlay or inset.
954 * {@hide}
955 */
956 static final int SCROLLBARS_INSET_MASK = 0x01000000;
957
958 /**
959 * Mask to check if the scrollbar style is inside or outside.
960 * {@hide}
961 */
962 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
963
964 /**
965 * Mask for scrollbar style.
966 * {@hide}
967 */
968 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
969
970 /**
971 * View flag indicating that the screen should remain on while the
972 * window containing this view is visible to the user. This effectively
973 * takes care of automatically setting the WindowManager's
974 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
975 */
976 public static final int KEEP_SCREEN_ON = 0x04000000;
977
978 /**
979 * View flag indicating whether this view should have sound effects enabled
980 * for events such as clicking and touching.
981 */
982 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
983
984 /**
985 * View flag indicating whether this view should have haptic feedback
986 * enabled for events such as long presses.
987 */
988 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
989
990 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700991 * <p>Indicates that the view hierarchy should stop saving state when
992 * it reaches this view. If state saving is initiated immediately at
993 * the view, it will be allowed.
994 * {@hide}
995 */
996 static final int PARENT_SAVE_DISABLED = 0x20000000;
997
998 /**
999 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
1000 * {@hide}
1001 */
1002 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
1003
1004 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001005 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1006 * should add all focusable Views regardless if they are focusable in touch mode.
1007 */
1008 public static final int FOCUSABLES_ALL = 0x00000000;
1009
1010 /**
1011 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1012 * should add only Views focusable in touch mode.
1013 */
1014 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_BACKWARD = 0x00000001;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * item.
1025 */
1026 public static final int FOCUS_FORWARD = 0x00000002;
1027
1028 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001029 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final int FOCUS_LEFT = 0x00000011;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_UP = 0x00000021;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_RIGHT = 0x00000042;
1042
1043 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001044 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 */
1046 public static final int FOCUS_DOWN = 0x00000082;
1047
Svetoslav Ganov42138042012-03-20 11:51:39 -07001048 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001049 * Bits of {@link #getMeasuredWidthAndState()} and
1050 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1051 */
1052 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1053
1054 /**
1055 * Bits of {@link #getMeasuredWidthAndState()} and
1056 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1057 */
1058 public static final int MEASURED_STATE_MASK = 0xff000000;
1059
1060 /**
1061 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1062 * for functions that combine both width and height into a single int,
1063 * such as {@link #getMeasuredState()} and the childState argument of
1064 * {@link #resolveSizeAndState(int, int, int)}.
1065 */
1066 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1067
1068 /**
1069 * Bit of {@link #getMeasuredWidthAndState()} and
1070 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1071 * is smaller that the space the view would like to have.
1072 */
1073 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1074
1075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 * Base View state sets
1077 */
1078 // Singles
1079 /**
1080 * Indicates the view has no states set. States are used with
1081 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1082 * view depending on its state.
1083 *
1084 * @see android.graphics.drawable.Drawable
1085 * @see #getDrawableState()
1086 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001087 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 /**
1089 * Indicates the view is enabled. States are used with
1090 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1091 * view depending on its state.
1092 *
1093 * @see android.graphics.drawable.Drawable
1094 * @see #getDrawableState()
1095 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001096 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 /**
1098 * Indicates the view is focused. States are used with
1099 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1100 * view depending on its state.
1101 *
1102 * @see android.graphics.drawable.Drawable
1103 * @see #getDrawableState()
1104 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001105 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 /**
1107 * Indicates the view is selected. States are used with
1108 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1109 * view depending on its state.
1110 *
1111 * @see android.graphics.drawable.Drawable
1112 * @see #getDrawableState()
1113 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001114 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 /**
1116 * Indicates the view is pressed. States are used with
1117 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1118 * view depending on its state.
1119 *
1120 * @see android.graphics.drawable.Drawable
1121 * @see #getDrawableState()
1122 * @hide
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 /**
1126 * Indicates the view's window has focus. 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[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 // Doubles
1135 /**
1136 * Indicates the view is enabled and has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled and selected.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #SELECTED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is enabled and that its window has focus.
1151 *
1152 * @see #ENABLED_STATE_SET
1153 * @see #WINDOW_FOCUSED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is focused and selected.
1158 *
1159 * @see #FOCUSED_STATE_SET
1160 * @see #SELECTED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view has the focus and that its window has the focus.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #WINDOW_FOCUSED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view is selected and that its window has the focus.
1172 *
1173 * @see #SELECTED_STATE_SET
1174 * @see #WINDOW_FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 // Triples
1178 /**
1179 * Indicates the view is enabled, focused and selected.
1180 *
1181 * @see #ENABLED_STATE_SET
1182 * @see #FOCUSED_STATE_SET
1183 * @see #SELECTED_STATE_SET
1184 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001185 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 /**
1187 * Indicates the view is enabled, focused and its window has the focus.
1188 *
1189 * @see #ENABLED_STATE_SET
1190 * @see #FOCUSED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 /**
1195 * Indicates the view is enabled, selected and its window has the focus.
1196 *
1197 * @see #ENABLED_STATE_SET
1198 * @see #SELECTED_STATE_SET
1199 * @see #WINDOW_FOCUSED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is focused, selected and its window has the focus.
1204 *
1205 * @see #FOCUSED_STATE_SET
1206 * @see #SELECTED_STATE_SET
1207 * @see #WINDOW_FOCUSED_STATE_SET
1208 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001209 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 /**
1211 * Indicates the view is enabled, focused, selected and its window
1212 * has the focus.
1213 *
1214 * @see #ENABLED_STATE_SET
1215 * @see #FOCUSED_STATE_SET
1216 * @see #SELECTED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is pressed and its window has the focus.
1222 *
1223 * @see #PRESSED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed and selected.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed, selected and its window has the focus.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #SELECTED_STATE_SET
1239 * @see #WINDOW_FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed and focused.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed, focused and its window has the focus.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #FOCUSED_STATE_SET
1254 * @see #WINDOW_FOCUSED_STATE_SET
1255 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001256 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 /**
1258 * Indicates the view is pressed, focused and selected.
1259 *
1260 * @see #PRESSED_STATE_SET
1261 * @see #SELECTED_STATE_SET
1262 * @see #FOCUSED_STATE_SET
1263 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001264 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 /**
1266 * Indicates the view is pressed, focused, selected and its window has the focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 * @see #SELECTED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed and enabled.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, enabled and its window has the focus.
1283 *
1284 * @see #PRESSED_STATE_SET
1285 * @see #ENABLED_STATE_SET
1286 * @see #WINDOW_FOCUSED_STATE_SET
1287 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001288 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 /**
1290 * Indicates the view is pressed, enabled and selected.
1291 *
1292 * @see #PRESSED_STATE_SET
1293 * @see #ENABLED_STATE_SET
1294 * @see #SELECTED_STATE_SET
1295 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001296 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 /**
1298 * Indicates the view is pressed, enabled, selected and its window has the
1299 * focus.
1300 *
1301 * @see #PRESSED_STATE_SET
1302 * @see #ENABLED_STATE_SET
1303 * @see #SELECTED_STATE_SET
1304 * @see #WINDOW_FOCUSED_STATE_SET
1305 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001306 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 /**
1308 * Indicates the view is pressed, enabled and focused.
1309 *
1310 * @see #PRESSED_STATE_SET
1311 * @see #ENABLED_STATE_SET
1312 * @see #FOCUSED_STATE_SET
1313 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001314 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 /**
1316 * Indicates the view is pressed, enabled, focused and its window has the
1317 * focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #FOCUSED_STATE_SET
1322 * @see #WINDOW_FOCUSED_STATE_SET
1323 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 /**
1326 * Indicates the view is pressed, enabled, focused and selected.
1327 *
1328 * @see #PRESSED_STATE_SET
1329 * @see #ENABLED_STATE_SET
1330 * @see #SELECTED_STATE_SET
1331 * @see #FOCUSED_STATE_SET
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Indicates the view is pressed, enabled, focused, selected and its window
1336 * has the focus.
1337 *
1338 * @see #PRESSED_STATE_SET
1339 * @see #ENABLED_STATE_SET
1340 * @see #SELECTED_STATE_SET
1341 * @see #FOCUSED_STATE_SET
1342 * @see #WINDOW_FOCUSED_STATE_SET
1343 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345
1346 /**
1347 * The order here is very important to {@link #getDrawableState()}
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 private static final int[][] VIEW_STATE_SETS;
1350
Romain Guyb051e892010-09-28 19:09:36 -07001351 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1352 static final int VIEW_STATE_SELECTED = 1 << 1;
1353 static final int VIEW_STATE_FOCUSED = 1 << 2;
1354 static final int VIEW_STATE_ENABLED = 1 << 3;
1355 static final int VIEW_STATE_PRESSED = 1 << 4;
1356 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001357 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001358 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001359 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1360 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361
1362 static final int[] VIEW_STATE_IDS = new int[] {
1363 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1364 R.attr.state_selected, VIEW_STATE_SELECTED,
1365 R.attr.state_focused, VIEW_STATE_FOCUSED,
1366 R.attr.state_enabled, VIEW_STATE_ENABLED,
1367 R.attr.state_pressed, VIEW_STATE_PRESSED,
1368 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001369 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001370 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001371 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001372 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 };
1374
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1377 throw new IllegalStateException(
1378 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1379 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001381 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001383 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001385 orderedIds[i * 2] = viewState;
1386 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 }
1388 }
1389 }
Romain Guyb051e892010-09-28 19:09:36 -07001390 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1391 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1392 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 int numBits = Integer.bitCount(i);
1394 int[] set = new int[numBits];
1395 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001396 for (int j = 0; j < orderedIds.length; j += 2) {
1397 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001398 set[pos++] = orderedIds[j];
1399 }
1400 }
1401 VIEW_STATE_SETS[i] = set;
1402 }
1403
1404 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1405 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1406 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1407 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1409 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1410 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1412 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1414 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1416 | VIEW_STATE_FOCUSED];
1417 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1418 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1420 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1422 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1424 | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1432 | VIEW_STATE_ENABLED];
1433 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1435 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1436
1437 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1438 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1440 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1442 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1452 | VIEW_STATE_PRESSED];
1453 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1455 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1460 | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1466 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1469 | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1472 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1475 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1476 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1477 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1478 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1479 | VIEW_STATE_PRESSED];
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001483 * Accessibility event types that are dispatched for text population.
1484 */
1485 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1486 AccessibilityEvent.TYPE_VIEW_CLICKED
1487 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1488 | AccessibilityEvent.TYPE_VIEW_SELECTED
1489 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1490 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1491 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001492 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001493 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001494 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001495 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1496 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001497
1498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * Temporary Rect currently for use in setBackground(). This will probably
1500 * be extended in the future to hold our own class with more than just
1501 * a Rect. :)
1502 */
1503 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001504
1505 /**
1506 * Map used to store views' tags.
1507 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001508 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001511 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001512 */
1513 private static int sNextAccessibilityViewId;
1514
1515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * The animation currently associated with this view.
1517 * @hide
1518 */
1519 protected Animation mCurrentAnimation = null;
1520
1521 /**
1522 * Width as measured during measure pass.
1523 * {@hide}
1524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001525 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001526 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 /**
1529 * Height as measured during measure pass.
1530 * {@hide}
1531 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001532 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001533 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534
1535 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001536 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1537 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1538 * its display list. This flag, used only when hw accelerated, allows us to clear the
1539 * flag while retaining this information until it's needed (at getDisplayList() time and
1540 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1541 *
1542 * {@hide}
1543 */
1544 boolean mRecreateDisplayList = false;
1545
1546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 * The view's identifier.
1548 * {@hide}
1549 *
1550 * @see #setId(int)
1551 * @see #getId()
1552 */
1553 @ViewDebug.ExportedProperty(resolveId = true)
1554 int mID = NO_ID;
1555
1556 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001557 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001558 */
1559 int mAccessibilityViewId = NO_ID;
1560
1561 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001562 * @hide
1563 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001564 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001565
1566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 * The view's tag.
1568 * {@hide}
1569 *
1570 * @see #setTag(Object)
1571 * @see #getTag()
1572 */
1573 protected Object mTag;
1574
1575 // for mPrivateFlags:
1576 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001577 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001579 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001581 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001583 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001585 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001587 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 /**
1589 * When this flag is set, this view is running an animation on behalf of its
1590 * children and should therefore not cancel invalidate requests, even if they
1591 * lie outside of this view's bounds.
1592 *
1593 * {@hide}
1594 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001595 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001597 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001599 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001601 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001603 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001605 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001607 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001608 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001609 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610
Dianne Hackborn4702a852012-08-17 15:18:29 -07001611 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612
1613 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001614 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 /**
1616 * Flag used to indicate that this view should be drawn once more (and only once
1617 * more) after its animation has completed.
1618 * {@hide}
1619 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001620 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621
Dianne Hackborn4702a852012-08-17 15:18:29 -07001622 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623
1624 /**
1625 * Indicates that the View returned true when onSetAlpha() was called and that
1626 * the alpha must be restored.
1627 * {@hide}
1628 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001629 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630
1631 /**
1632 * Set by {@link #setScrollContainer(boolean)}.
1633 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001634 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635
1636 /**
1637 * Set by {@link #setScrollContainer(boolean)}.
1638 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001639 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640
1641 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001642 * View flag indicating whether this view was invalidated (fully or partially.)
1643 *
1644 * @hide
1645 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001646 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001647
1648 /**
1649 * View flag indicating whether this view was invalidated by an opaque
1650 * invalidate request.
1651 *
1652 * @hide
1653 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001654 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001655
1656 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001657 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001658 *
1659 * @hide
1660 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001661 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001662
1663 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001664 * Indicates whether the background is opaque.
1665 *
1666 * @hide
1667 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001668 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001669
1670 /**
1671 * Indicates whether the scrollbars are opaque.
1672 *
1673 * @hide
1674 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001675 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001676
1677 /**
1678 * Indicates whether the view is opaque.
1679 *
1680 * @hide
1681 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001682 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001683
Adam Powelle14579b2009-12-16 18:39:52 -08001684 /**
1685 * Indicates a prepressed state;
1686 * the short time between ACTION_DOWN and recognizing
1687 * a 'real' press. Prepressed is used to recognize quick taps
1688 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001689 *
Adam Powelle14579b2009-12-16 18:39:52 -08001690 * @hide
1691 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001692 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001693
Adam Powellc9fbaab2010-02-16 17:16:19 -08001694 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001695 * Indicates whether the view is temporarily detached.
1696 *
1697 * @hide
1698 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001699 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001700
Adam Powell8568c3a2010-04-19 14:26:11 -07001701 /**
1702 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001703 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001704 * @hide
1705 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001706 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001707
1708 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001709 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1710 * @hide
1711 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001712 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001713
1714 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001715 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1716 * for transform operations
1717 *
1718 * @hide
1719 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001720 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001721
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001722 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001723 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001724
Chet Haasefd2b0022010-08-06 13:08:56 -07001725 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001726 * Indicates that this view was specifically invalidated, not just dirtied because some
1727 * child view was invalidated. The flag is used to determine when we need to recreate
1728 * a view's display list (as opposed to just returning a reference to its existing
1729 * display list).
1730 *
1731 * @hide
1732 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001733 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001734
Jeff Sharkey36901b62012-09-19 19:06:22 -07001735 /**
1736 * Masks for mPrivateFlags2, as generated by dumpFlags():
1737 *
1738 * -------|-------|-------|-------|
1739 * PFLAG2_TEXT_ALIGNMENT_FLAGS[0]
1740 * PFLAG2_TEXT_DIRECTION_FLAGS[0]
1741 * 1 PFLAG2_DRAG_CAN_ACCEPT
1742 * 1 PFLAG2_DRAG_HOVERED
1743 * 1 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
1744 * 11 PFLAG2_TEXT_DIRECTION_MASK_SHIFT
1745 * 1 1 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT
1746 * 11 PFLAG2_LAYOUT_DIRECTION_MASK
1747 * 11 1 PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
1748 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
1749 * 1 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT
1750 * 1 1 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT
1751 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED
1752 * 11 PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
1753 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[1]
1754 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[2]
1755 * 11 PFLAG2_TEXT_DIRECTION_FLAGS[3]
1756 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[4]
1757 * 1 1 PFLAG2_TEXT_DIRECTION_FLAGS[5]
1758 * 111 PFLAG2_TEXT_DIRECTION_MASK
1759 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED
1760 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT
1761 * 111 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK
1762 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[1]
1763 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[2]
1764 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[3]
1765 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[4]
1766 * 1 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[5]
1767 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[6]
1768 * 111 PFLAG2_TEXT_ALIGNMENT_MASK
1769 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED
1770 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT
1771 * 111 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK
1772 * 11 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK
1773 * 1 PFLAG2_HAS_TRANSIENT_STATE
1774 * 1 PFLAG2_ACCESSIBILITY_FOCUSED
1775 * 1 PFLAG2_ACCESSIBILITY_STATE_CHANGED
1776 * 1 PFLAG2_VIEW_QUICK_REJECTED
1777 * 1 PFLAG2_PADDING_RESOLVED
1778 * -------|-------|-------|-------|
1779 */
Christopher Tate3d4bf172011-03-28 16:16:46 -07001780
1781 /**
1782 * Indicates that this view has reported that it can accept the current drag's content.
1783 * Cleared when the drag operation concludes.
1784 * @hide
1785 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001786 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001787
1788 /**
1789 * Indicates that this view is currently directly under the drag location in a
1790 * drag-and-drop operation involving content that it can accept. Cleared when
1791 * the drag exits the view, or when the drag operation concludes.
1792 * @hide
1793 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001794 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001795
Cibu Johny86666632010-02-22 13:01:02 -08001796 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001797 * Horizontal layout direction of this view is from Left to Right.
1798 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001799 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001800 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001801
1802 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001803 * Horizontal layout direction of this view is from Right to Left.
1804 * Use with {@link #setLayoutDirection}.
1805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is inherited from its parent.
1810 * Use with {@link #setLayoutDirection}.
1811 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001812 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001813
1814 /**
1815 * Horizontal layout direction of this view is from deduced from the default language
1816 * script for the locale. Use with {@link #setLayoutDirection}.
1817 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001818 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819
1820 /**
1821 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001822 * @hide
1823 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001824 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001825
1826 /**
1827 * Mask for use with private flags indicating bits used for horizontal layout direction.
1828 * @hide
1829 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001830 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001831
1832 /**
1833 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1834 * right-to-left direction.
1835 * @hide
1836 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001837 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001838
1839 /**
1840 * Indicates whether the view horizontal layout direction has been resolved.
1841 * @hide
1842 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001843 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001844
1845 /**
1846 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1847 * @hide
1848 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001849 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1850 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001851
1852 /*
1853 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1854 * flag value.
1855 * @hide
1856 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001857 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1858 LAYOUT_DIRECTION_LTR,
1859 LAYOUT_DIRECTION_RTL,
1860 LAYOUT_DIRECTION_INHERIT,
1861 LAYOUT_DIRECTION_LOCALE
1862 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001863
1864 /**
1865 * Default horizontal layout direction.
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001866 */
1867 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001868
Adam Powell539ee872012-02-03 19:00:49 -08001869 /**
1870 * Indicates that the view is tracking some sort of transient state
1871 * that the app should not need to be aware of, but that the framework
1872 * should take special care to preserve.
1873 *
1874 * @hide
1875 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001876 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001877
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001878 /**
1879 * Text direction is inherited thru {@link ViewGroup}
1880 */
1881 public static final int TEXT_DIRECTION_INHERIT = 0;
1882
1883 /**
1884 * Text direction is using "first strong algorithm". The first strong directional character
1885 * determines the paragraph direction. If there is no strong directional character, the
1886 * paragraph direction is the view's resolved layout direction.
1887 */
1888 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1889
1890 /**
1891 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1892 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1893 * If there are neither, the paragraph direction is the view's resolved layout direction.
1894 */
1895 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1896
1897 /**
1898 * Text direction is forced to LTR.
1899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
1904 */
1905 public static final int TEXT_DIRECTION_RTL = 4;
1906
1907 /**
1908 * Text direction is coming from the system Locale.
1909 */
1910 public static final int TEXT_DIRECTION_LOCALE = 5;
1911
1912 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001913 * Default text direction is inherited
1914 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07001915 private static final int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001916
1917 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001918 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1919 * @hide
1920 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001921 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001922
1923 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001924 * Mask for use with private flags indicating bits used for text direction.
1925 * @hide
1926 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001927 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1928 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001929
1930 /**
1931 * Array of text direction flags for mapping attribute "textDirection" to correct
1932 * flag value.
1933 * @hide
1934 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001935 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1936 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1937 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1938 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001942 };
1943
1944 /**
1945 * Indicates whether the view text direction has been resolved.
1946 * @hide
1947 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001948 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1949 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001950
1951 /**
1952 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1953 * @hide
1954 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001955 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001956
1957 /**
1958 * Mask for use with private flags indicating bits used for resolved text direction.
1959 * @hide
1960 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001961 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1962 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001968 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
1974 */
1975 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1976
1977 /**
1978 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1979 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1980 *
1981 * Use with {@link #setTextAlignment(int)}
1982 */
1983 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1984
1985 /**
1986 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1987 *
1988 * Use with {@link #setTextAlignment(int)}
1989 */
1990 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1991
1992 /**
1993 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1994 *
1995 * Use with {@link #setTextAlignment(int)}
1996 */
1997 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1998
1999 /**
2000 * Center the paragraph, e.g. ALIGN_CENTER.
2001 *
2002 * Use with {@link #setTextAlignment(int)}
2003 */
2004 public static final int TEXT_ALIGNMENT_CENTER = 4;
2005
2006 /**
2007 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2008 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2009 *
2010 * Use with {@link #setTextAlignment(int)}
2011 */
2012 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2013
2014 /**
2015 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2016 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2017 *
2018 * Use with {@link #setTextAlignment(int)}
2019 */
2020 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2021
2022 /**
2023 * Default text alignment is inherited
2024 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07002025 private static final int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026
2027 /**
2028 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2029 * @hide
2030 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002031 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032
2033 /**
2034 * Mask for use with private flags indicating bits used for text alignment.
2035 * @hide
2036 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002037 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002038
2039 /**
2040 * Array of text direction flags for mapping attribute "textAlignment" to correct
2041 * flag value.
2042 * @hide
2043 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002044 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2045 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2046 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2047 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2048 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002052 };
2053
2054 /**
2055 * Indicates whether the view text alignment has been resolved.
2056 * @hide
2057 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002058 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002059
2060 /**
2061 * Bit shift to get the resolved text alignment.
2062 * @hide
2063 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002064 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002065
2066 /**
2067 * Mask for use with private flags indicating bits used for text alignment.
2068 * @hide
2069 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002070 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2071 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002072
2073 /**
2074 * Indicates whether if the view text alignment has been resolved to gravity
2075 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002076 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2077 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002078
Svetoslav Ganov42138042012-03-20 11:51:39 -07002079 // Accessiblity constants for mPrivateFlags2
2080
2081 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002082 * Shift for the bits in {@link #mPrivateFlags2} related to the
2083 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002084 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002085 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086
2087 /**
2088 * Automatically determine whether a view is important for accessibility.
2089 */
2090 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2091
2092 /**
2093 * The view is important for accessibility.
2094 */
2095 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2096
2097 /**
2098 * The view is not important for accessibility.
2099 */
2100 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2101
2102 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002103 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002104 */
2105 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2106
2107 /**
2108 * Mask for obtainig the bits which specify how to determine
2109 * whether a view is important for accessibility.
2110 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002111 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002112 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002113 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002114
2115 /**
2116 * Flag indicating whether a view has accessibility focus.
2117 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002118 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002119
2120 /**
2121 * Flag indicating whether a view state for accessibility has changed.
2122 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002123 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2124 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002125
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002126 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002127 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2128 * is used to check whether later changes to the view's transform should invalidate the
2129 * view to force the quickReject test to run again.
2130 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002131 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002132
Adam Powell0090f202012-08-07 17:15:30 -07002133 /**
2134 * Flag indicating that start/end padding has been resolved into left/right padding
2135 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2136 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2137 * during measurement. In some special cases this is required such as when an adapter-based
2138 * view measures prospective children without attaching them to a window.
2139 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002140 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002141
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002142 /**
2143 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2144 */
2145 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2146
2147 /**
2148 * Group of bits indicating that RTL properties resolution is done.
2149 */
2150 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002151 PFLAG2_TEXT_DIRECTION_RESOLVED |
2152 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2153 PFLAG2_PADDING_RESOLVED |
2154 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002155
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002156 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002157
Christopher Tate3d4bf172011-03-28 16:16:46 -07002158 /* End of masks for mPrivateFlags2 */
2159
Chet Haase21433372012-06-05 07:54:09 -07002160 /* Masks for mPrivateFlags3 */
2161
2162 /**
2163 * Flag indicating that view has a transform animation set on it. This is used to track whether
2164 * an animation is cleared between successive frames, in order to tell the associated
2165 * DisplayList to clear its animation matrix.
2166 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002167 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002168
2169 /**
2170 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2171 * animation is cleared between successive frames, in order to tell the associated
2172 * DisplayList to restore its alpha value.
2173 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002174 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002175
2176
2177 /* End of masks for mPrivateFlags3 */
2178
Dianne Hackborn4702a852012-08-17 15:18:29 -07002179 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002180
Chet Haasedaf98e92011-01-10 14:10:36 -08002181 /**
Adam Powell637d3372010-08-25 14:37:03 -07002182 * Always allow a user to over-scroll this view, provided it is a
2183 * view that can scroll.
2184 *
2185 * @see #getOverScrollMode()
2186 * @see #setOverScrollMode(int)
2187 */
2188 public static final int OVER_SCROLL_ALWAYS = 0;
2189
2190 /**
2191 * Allow a user to over-scroll this view only if the content is large
2192 * enough to meaningfully scroll, provided it is a view that can scroll.
2193 *
2194 * @see #getOverScrollMode()
2195 * @see #setOverScrollMode(int)
2196 */
2197 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2198
2199 /**
2200 * Never allow a user to over-scroll this view.
2201 *
2202 * @see #getOverScrollMode()
2203 * @see #setOverScrollMode(int)
2204 */
2205 public static final int OVER_SCROLL_NEVER = 2;
2206
2207 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002208 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2209 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002210 *
Joe Malin32736f02011-01-19 16:14:20 -08002211 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002212 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002213 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002214
2215 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002216 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2217 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002218 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002219 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002220 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002221 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002222 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002223 *
Joe Malin32736f02011-01-19 16:14:20 -08002224 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002225 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002226 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2227
2228 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002229 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2230 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002231 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002232 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002233 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2234 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2235 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002236 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002237 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2238 * window flags) for displaying content using every last pixel on the display.
2239 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002240 * <p>There is a limitation: because navigation controls are so important, the least user
2241 * interaction will cause them to reappear immediately. When this happens, both
2242 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2243 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002244 *
2245 * @see #setSystemUiVisibility(int)
2246 */
2247 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2248
2249 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002250 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2251 * into the normal fullscreen mode so that its content can take over the screen
2252 * while still allowing the user to interact with the application.
2253 *
2254 * <p>This has the same visual effect as
2255 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2256 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2257 * meaning that non-critical screen decorations (such as the status bar) will be
2258 * hidden while the user is in the View's window, focusing the experience on
2259 * that content. Unlike the window flag, if you are using ActionBar in
2260 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2261 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2262 * hide the action bar.
2263 *
2264 * <p>This approach to going fullscreen is best used over the window flag when
2265 * it is a transient state -- that is, the application does this at certain
2266 * points in its user interaction where it wants to allow the user to focus
2267 * on content, but not as a continuous state. For situations where the application
2268 * would like to simply stay full screen the entire time (such as a game that
2269 * wants to take over the screen), the
2270 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2271 * is usually a better approach. The state set here will be removed by the system
2272 * in various situations (such as the user moving to another application) like
2273 * the other system UI states.
2274 *
2275 * <p>When using this flag, the application should provide some easy facility
2276 * for the user to go out of it. A common example would be in an e-book
2277 * reader, where tapping on the screen brings back whatever screen and UI
2278 * decorations that had been hidden while the user was immersed in reading
2279 * the book.
2280 *
2281 * @see #setSystemUiVisibility(int)
2282 */
2283 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2284
2285 /**
2286 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2287 * flags, we would like a stable view of the content insets given to
2288 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2289 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002290 * as a continuous state. In the stock Android UI this is the space for
2291 * the system bar, nav bar, and status bar, but not more transient elements
2292 * such as an input method.
2293 *
2294 * The stable layout your UI sees is based on the system UI modes you can
2295 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2296 * then you will get a stable layout for changes of the
2297 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2298 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2299 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2300 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2301 * with a stable layout. (Note that you should avoid using
2302 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2303 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002304 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002305 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2306 * then a hidden status bar will be considered a "stable" state for purposes
2307 * here. This allows your UI to continually hide the status bar, while still
2308 * using the system UI flags to hide the action bar while still retaining
2309 * a stable layout. Note that changing the window fullscreen flag will never
2310 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002311 *
2312 * <p>If you are using ActionBar in
2313 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2314 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2315 * insets it adds to those given to the application.
2316 */
2317 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2318
2319 /**
2320 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2321 * to be layed out as if it has requested
2322 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2323 * allows it to avoid artifacts when switching in and out of that mode, at
2324 * the expense that some of its user interface may be covered by screen
2325 * decorations when they are shown. You can perform layout of your inner
2326 * UI elements to account for the navagation system UI through the
2327 * {@link #fitSystemWindows(Rect)} method.
2328 */
2329 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2330
2331 /**
2332 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2333 * to be layed out as if it has requested
2334 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2335 * allows it to avoid artifacts when switching in and out of that mode, at
2336 * the expense that some of its user interface may be covered by screen
2337 * decorations when they are shown. You can perform layout of your inner
2338 * UI elements to account for non-fullscreen system UI through the
2339 * {@link #fitSystemWindows(Rect)} method.
2340 */
2341 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2342
2343 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002344 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2345 */
2346 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2347
2348 /**
2349 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2350 */
2351 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002352
2353 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002354 * @hide
2355 *
2356 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2357 * out of the public fields to keep the undefined bits out of the developer's way.
2358 *
2359 * Flag to make the status bar not expandable. Unless you also
2360 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2361 */
2362 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2363
2364 /**
2365 * @hide
2366 *
2367 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2368 * out of the public fields to keep the undefined bits out of the developer's way.
2369 *
2370 * Flag to hide notification icons and scrolling ticker text.
2371 */
2372 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2373
2374 /**
2375 * @hide
2376 *
2377 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2378 * out of the public fields to keep the undefined bits out of the developer's way.
2379 *
2380 * Flag to disable incoming notification alerts. This will not block
2381 * icons, but it will block sound, vibrating and other visual or aural notifications.
2382 */
2383 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2384
2385 /**
2386 * @hide
2387 *
2388 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2389 * out of the public fields to keep the undefined bits out of the developer's way.
2390 *
2391 * Flag to hide only the scrolling ticker. Note that
2392 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2393 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2394 */
2395 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2396
2397 /**
2398 * @hide
2399 *
2400 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2401 * out of the public fields to keep the undefined bits out of the developer's way.
2402 *
2403 * Flag to hide the center system info area.
2404 */
2405 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2406
2407 /**
2408 * @hide
2409 *
2410 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2411 * out of the public fields to keep the undefined bits out of the developer's way.
2412 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002413 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002414 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2415 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002416 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002417
2418 /**
2419 * @hide
2420 *
2421 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2422 * out of the public fields to keep the undefined bits out of the developer's way.
2423 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002424 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002425 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2426 */
2427 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2428
2429 /**
2430 * @hide
2431 *
2432 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2433 * out of the public fields to keep the undefined bits out of the developer's way.
2434 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002435 * Flag to hide only the clock. You might use this if your activity has
2436 * its own clock making the status bar's clock redundant.
2437 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002438 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2439
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002440 /**
2441 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002442 *
2443 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2444 * out of the public fields to keep the undefined bits out of the developer's way.
2445 *
2446 * Flag to hide only the recent apps button. Don't use this
2447 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2448 */
2449 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2450
2451 /**
2452 * @hide
Daniel Sandlerd5483c32012-10-19 16:44:15 -04002453 *
2454 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2455 * out of the public fields to keep the undefined bits out of the developer's way.
2456 *
2457 * Flag to disable the global search gesture. Don't use this
2458 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2459 */
2460 public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
2461
2462 /**
2463 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002464 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002465 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002466
2467 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002468 * These are the system UI flags that can be cleared by events outside
2469 * of an application. Currently this is just the ability to tap on the
2470 * screen while hiding the navigation bar to have it return.
2471 * @hide
2472 */
2473 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002474 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2475 | SYSTEM_UI_FLAG_FULLSCREEN;
2476
2477 /**
2478 * Flags that can impact the layout in relation to system UI.
2479 */
2480 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2481 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2482 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002483
2484 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002485 * Find views that render the specified text.
2486 *
2487 * @see #findViewsWithText(ArrayList, CharSequence, int)
2488 */
2489 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2490
2491 /**
2492 * Find find views that contain the specified content description.
2493 *
2494 * @see #findViewsWithText(ArrayList, CharSequence, int)
2495 */
2496 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2497
2498 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002499 * Find views that contain {@link AccessibilityNodeProvider}. Such
2500 * a View is a root of virtual view hierarchy and may contain the searched
2501 * text. If this flag is set Views with providers are automatically
2502 * added and it is a responsibility of the client to call the APIs of
2503 * the provider to determine whether the virtual tree rooted at this View
2504 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2505 * represeting the virtual views with this text.
2506 *
2507 * @see #findViewsWithText(ArrayList, CharSequence, int)
2508 *
2509 * @hide
2510 */
2511 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2512
2513 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002514 * The undefined cursor position.
2515 */
2516 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2517
2518 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002519 * Indicates that the screen has changed state and is now off.
2520 *
2521 * @see #onScreenStateChanged(int)
2522 */
2523 public static final int SCREEN_STATE_OFF = 0x0;
2524
2525 /**
2526 * Indicates that the screen has changed state and is now on.
2527 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002528 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002529 */
2530 public static final int SCREEN_STATE_ON = 0x1;
2531
2532 /**
Adam Powell637d3372010-08-25 14:37:03 -07002533 * Controls the over-scroll mode for this view.
2534 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2535 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2536 * and {@link #OVER_SCROLL_NEVER}.
2537 */
2538 private int mOverScrollMode;
2539
2540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 * The parent this view is attached to.
2542 * {@hide}
2543 *
2544 * @see #getParent()
2545 */
2546 protected ViewParent mParent;
2547
2548 /**
2549 * {@hide}
2550 */
2551 AttachInfo mAttachInfo;
2552
2553 /**
2554 * {@hide}
2555 */
Romain Guy809a7f62009-05-14 15:44:42 -07002556 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002557 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002558 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002559 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002560 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002561 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002562 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002563 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2564 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2565 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2566 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002567 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002569 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002570 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571
2572 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002573 * This view's request for the visibility of the status bar.
2574 * @hide
2575 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002576 @ViewDebug.ExportedProperty(flagMapping = {
2577 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2578 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2579 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2580 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2581 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2582 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2583 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2584 equals = SYSTEM_UI_FLAG_VISIBLE,
2585 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2586 })
Joe Onorato664644d2011-01-23 17:53:23 -08002587 int mSystemUiVisibility;
2588
2589 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002590 * Reference count for transient state.
2591 * @see #setHasTransientState(boolean)
2592 */
2593 int mTransientStateCount = 0;
2594
2595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 * Count of how many windows this view has been attached to.
2597 */
2598 int mWindowAttachCount;
2599
2600 /**
2601 * The layout parameters associated with this view and used by the parent
2602 * {@link android.view.ViewGroup} to determine how this view should be
2603 * laid out.
2604 * {@hide}
2605 */
2606 protected ViewGroup.LayoutParams mLayoutParams;
2607
2608 /**
2609 * The view flags hold various views states.
2610 * {@hide}
2611 */
2612 @ViewDebug.ExportedProperty
2613 int mViewFlags;
2614
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002615 static class TransformationInfo {
2616 /**
2617 * The transform matrix for the View. This transform is calculated internally
2618 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2619 * is used by default. Do *not* use this variable directly; instead call
2620 * getMatrix(), which will automatically recalculate the matrix if necessary
2621 * to get the correct matrix based on the latest rotation and scale properties.
2622 */
2623 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002624
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002625 /**
2626 * The transform matrix for the View. This transform is calculated internally
2627 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2628 * is used by default. Do *not* use this variable directly; instead call
2629 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2630 * to get the correct matrix based on the latest rotation and scale properties.
2631 */
2632 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002633
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002634 /**
2635 * An internal variable that tracks whether we need to recalculate the
2636 * transform matrix, based on whether the rotation or scaleX/Y properties
2637 * have changed since the matrix was last calculated.
2638 */
2639 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002640
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002641 /**
2642 * An internal variable that tracks whether we need to recalculate the
2643 * transform matrix, based on whether the rotation or scaleX/Y properties
2644 * have changed since the matrix was last calculated.
2645 */
2646 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002647
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002648 /**
2649 * A variable that tracks whether we need to recalculate the
2650 * transform matrix, based on whether the rotation or scaleX/Y properties
2651 * have changed since the matrix was last calculated. This variable
2652 * is only valid after a call to updateMatrix() or to a function that
2653 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2654 */
2655 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002656
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002657 /**
2658 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2659 */
2660 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002661
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002662 /**
2663 * This matrix is used when computing the matrix for 3D rotations.
2664 */
2665 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002666
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002667 /**
2668 * These prev values are used to recalculate a centered pivot point when necessary. The
2669 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2670 * set), so thes values are only used then as well.
2671 */
2672 private int mPrevWidth = -1;
2673 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002674
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002675 /**
2676 * The degrees rotation around the vertical axis through the pivot point.
2677 */
2678 @ViewDebug.ExportedProperty
2679 float mRotationY = 0f;
2680
2681 /**
2682 * The degrees rotation around the horizontal axis through the pivot point.
2683 */
2684 @ViewDebug.ExportedProperty
2685 float mRotationX = 0f;
2686
2687 /**
2688 * The degrees rotation around the pivot point.
2689 */
2690 @ViewDebug.ExportedProperty
2691 float mRotation = 0f;
2692
2693 /**
2694 * The amount of translation of the object away from its left property (post-layout).
2695 */
2696 @ViewDebug.ExportedProperty
2697 float mTranslationX = 0f;
2698
2699 /**
2700 * The amount of translation of the object away from its top property (post-layout).
2701 */
2702 @ViewDebug.ExportedProperty
2703 float mTranslationY = 0f;
2704
2705 /**
2706 * The amount of scale in the x direction around the pivot point. A
2707 * value of 1 means no scaling is applied.
2708 */
2709 @ViewDebug.ExportedProperty
2710 float mScaleX = 1f;
2711
2712 /**
2713 * The amount of scale in the y direction around the pivot point. A
2714 * value of 1 means no scaling is applied.
2715 */
2716 @ViewDebug.ExportedProperty
2717 float mScaleY = 1f;
2718
2719 /**
Chet Haasea33de552012-02-03 16:28:24 -08002720 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002721 */
2722 @ViewDebug.ExportedProperty
2723 float mPivotX = 0f;
2724
2725 /**
Chet Haasea33de552012-02-03 16:28:24 -08002726 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002727 */
2728 @ViewDebug.ExportedProperty
2729 float mPivotY = 0f;
2730
2731 /**
2732 * The opacity of the View. This is a value from 0 to 1, where 0 means
2733 * completely transparent and 1 means completely opaque.
2734 */
2735 @ViewDebug.ExportedProperty
2736 float mAlpha = 1f;
2737 }
2738
2739 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002740
Joe Malin32736f02011-01-19 16:14:20 -08002741 private boolean mLastIsOpaque;
2742
Chet Haasefd2b0022010-08-06 13:08:56 -07002743 /**
2744 * Convenience value to check for float values that are close enough to zero to be considered
2745 * zero.
2746 */
Romain Guy2542d192010-08-18 11:47:12 -07002747 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002748
2749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 * The distance in pixels from the left edge of this view's parent
2751 * to the left edge of this view.
2752 * {@hide}
2753 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002754 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 protected int mLeft;
2756 /**
2757 * The distance in pixels from the left edge of this view's parent
2758 * to the right edge of this view.
2759 * {@hide}
2760 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002761 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 protected int mRight;
2763 /**
2764 * The distance in pixels from the top edge of this view's parent
2765 * to the top edge of this view.
2766 * {@hide}
2767 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002768 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 protected int mTop;
2770 /**
2771 * The distance in pixels from the top edge of this view's parent
2772 * to the bottom edge of this view.
2773 * {@hide}
2774 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002775 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 protected int mBottom;
2777
2778 /**
2779 * The offset, in pixels, by which the content of this view is scrolled
2780 * horizontally.
2781 * {@hide}
2782 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002783 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 protected int mScrollX;
2785 /**
2786 * The offset, in pixels, by which the content of this view is scrolled
2787 * vertically.
2788 * {@hide}
2789 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002790 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 protected int mScrollY;
2792
2793 /**
2794 * The left padding in pixels, that is the distance in pixels between the
2795 * left edge of this view and the left edge of its content.
2796 * {@hide}
2797 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002798 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002799 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 /**
2801 * The right padding in pixels, that is the distance in pixels between the
2802 * right edge of this view and the right edge of its content.
2803 * {@hide}
2804 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002805 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002806 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 /**
2808 * The top padding in pixels, that is the distance in pixels between the
2809 * top edge of this view and the top edge of its content.
2810 * {@hide}
2811 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002812 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 protected int mPaddingTop;
2814 /**
2815 * The bottom padding in pixels, that is the distance in pixels between the
2816 * bottom edge of this view and the bottom edge of its content.
2817 * {@hide}
2818 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002819 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 protected int mPaddingBottom;
2821
2822 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002823 * The layout insets in pixels, that is the distance in pixels between the
2824 * visible edges of this view its bounds.
2825 */
2826 private Insets mLayoutInsets;
2827
2828 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002829 * Briefly describes the view and is primarily used for accessibility support.
2830 */
2831 private CharSequence mContentDescription;
2832
2833 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002834 * Specifies the id of a view for which this view serves as a label for
2835 * accessibility purposes.
2836 */
2837 private int mLabelForId = View.NO_ID;
2838
2839 /**
2840 * Predicate for matching labeled view id with its label for
2841 * accessibility purposes.
2842 */
2843 private MatchLabelForPredicate mMatchLabelForPredicate;
2844
2845 /**
2846 * Predicate for matching a view by its id.
2847 */
2848 private MatchIdPredicate mMatchIdPredicate;
2849
2850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002852 *
2853 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002855 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002856 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857
2858 /**
2859 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002860 *
2861 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002863 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002864 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002866 /**
Adam Powell20232d02010-12-08 21:08:53 -08002867 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002868 *
2869 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002870 */
2871 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002872 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002873
2874 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002875 * Cache the paddingStart set by the user to append to the scrollbar's size.
2876 *
2877 */
2878 @ViewDebug.ExportedProperty(category = "padding")
2879 int mUserPaddingStart;
2880
2881 /**
2882 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2883 *
2884 */
2885 @ViewDebug.ExportedProperty(category = "padding")
2886 int mUserPaddingEnd;
2887
2888 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002889 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002890 *
2891 * @hide
2892 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002893 int mUserPaddingLeftInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002894
2895 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002896 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002897 *
2898 * @hide
2899 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002900 int mUserPaddingRightInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002901
2902 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002903 * Default undefined padding
2904 */
2905 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2906
2907 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002908 * @hide
2909 */
2910 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2911 /**
2912 * @hide
2913 */
2914 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915
Romain Guydfab3632012-10-03 14:53:25 -07002916 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002917 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918
2919 private int mBackgroundResource;
2920 private boolean mBackgroundSizeChanged;
2921
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002922 static class ListenerInfo {
2923 /**
2924 * Listener used to dispatch focus change events.
2925 * This field should be made private, so it is hidden from the SDK.
2926 * {@hide}
2927 */
2928 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002930 /**
2931 * Listeners for layout change events.
2932 */
2933 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002934
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002935 /**
2936 * Listeners for attach events.
2937 */
2938 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002939
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002940 /**
2941 * Listener used to dispatch click events.
2942 * This field should be made private, so it is hidden from the SDK.
2943 * {@hide}
2944 */
2945 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002947 /**
2948 * Listener used to dispatch long click events.
2949 * This field should be made private, so it is hidden from the SDK.
2950 * {@hide}
2951 */
2952 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002954 /**
2955 * Listener used to build the context menu.
2956 * This field should be made private, so it is hidden from the SDK.
2957 * {@hide}
2958 */
2959 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002961 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002963 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002965 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002966
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002967 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002968
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002969 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002970
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002971 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2972 }
2973
2974 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 /**
2977 * The application environment this view lives in.
2978 * This field should be made private, so it is hidden from the SDK.
2979 * {@hide}
2980 */
2981 protected Context mContext;
2982
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002983 private final Resources mResources;
2984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 private ScrollabilityCache mScrollCache;
2986
2987 private int[] mDrawableState = null;
2988
Romain Guy0211a0a2011-02-14 16:34:59 -08002989 /**
2990 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002991 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002992 * @hide
2993 */
2994 public boolean mCachingFailed;
2995
Romain Guy02890fd2010-08-06 17:58:44 -07002996 private Bitmap mDrawingCache;
2997 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002998 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002999 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000
3001 /**
3002 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3003 * the user may specify which view to go to next.
3004 */
3005 private int mNextFocusLeftId = View.NO_ID;
3006
3007 /**
3008 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3009 * the user may specify which view to go to next.
3010 */
3011 private int mNextFocusRightId = View.NO_ID;
3012
3013 /**
3014 * When this view has focus and the next focus is {@link #FOCUS_UP},
3015 * the user may specify which view to go to next.
3016 */
3017 private int mNextFocusUpId = View.NO_ID;
3018
3019 /**
3020 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3021 * the user may specify which view to go to next.
3022 */
3023 private int mNextFocusDownId = View.NO_ID;
3024
Jeff Brown4e6319b2010-12-13 10:36:51 -08003025 /**
3026 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3027 * the user may specify which view to go to next.
3028 */
3029 int mNextFocusForwardId = View.NO_ID;
3030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003032 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003033 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003034 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 private UnsetPressedState mUnsetPressedState;
3037
3038 /**
3039 * Whether the long press's action has been invoked. The tap's action is invoked on the
3040 * up event while a long press is invoked as soon as the long press duration is reached, so
3041 * a long press could be performed before the tap is checked, in which case the tap's action
3042 * should not be invoked.
3043 */
3044 private boolean mHasPerformedLongPress;
3045
3046 /**
3047 * The minimum height of the view. We'll try our best to have the height
3048 * of this view to at least this amount.
3049 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003050 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 private int mMinHeight;
3052
3053 /**
3054 * The minimum width of the view. We'll try our best to have the width
3055 * of this view to at least this amount.
3056 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003057 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 private int mMinWidth;
3059
3060 /**
3061 * The delegate to handle touch events that are physically in this view
3062 * but should be handled by another view.
3063 */
3064 private TouchDelegate mTouchDelegate = null;
3065
3066 /**
3067 * Solid color to use as a background when creating the drawing cache. Enables
3068 * the cache to use 16 bit bitmaps instead of 32 bit.
3069 */
3070 private int mDrawingCacheBackgroundColor = 0;
3071
3072 /**
3073 * Special tree observer used when mAttachInfo is null.
3074 */
3075 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003076
Adam Powelle14579b2009-12-16 18:39:52 -08003077 /**
3078 * Cache the touch slop from the context that created the view.
3079 */
3080 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003083 * Object that handles automatic animation of view properties.
3084 */
3085 private ViewPropertyAnimator mAnimator = null;
3086
3087 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003088 * Flag indicating that a drag can cross window boundaries. When
3089 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3090 * with this flag set, all visible applications will be able to participate
3091 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003092 *
3093 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003094 */
3095 public static final int DRAG_FLAG_GLOBAL = 1;
3096
3097 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003098 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3099 */
3100 private float mVerticalScrollFactor;
3101
3102 /**
Adam Powell20232d02010-12-08 21:08:53 -08003103 * Position of the vertical scroll bar.
3104 */
3105 private int mVerticalScrollbarPosition;
3106
3107 /**
3108 * Position the scroll bar at the default position as determined by the system.
3109 */
3110 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3111
3112 /**
3113 * Position the scroll bar along the left edge.
3114 */
3115 public static final int SCROLLBAR_POSITION_LEFT = 1;
3116
3117 /**
3118 * Position the scroll bar along the right edge.
3119 */
3120 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3121
3122 /**
Romain Guy171c5922011-01-06 10:04:23 -08003123 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003124 *
3125 * @see #getLayerType()
3126 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003127 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003128 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003129 */
3130 public static final int LAYER_TYPE_NONE = 0;
3131
3132 /**
3133 * <p>Indicates that the view has a software layer. A software layer is backed
3134 * by a bitmap and causes the view to be rendered using Android's software
3135 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003136 *
Romain Guy171c5922011-01-06 10:04:23 -08003137 * <p>Software layers have various usages:</p>
3138 * <p>When the application is not using hardware acceleration, a software layer
3139 * is useful to apply a specific color filter and/or blending mode and/or
3140 * translucency to a view and all its children.</p>
3141 * <p>When the application is using hardware acceleration, a software layer
3142 * is useful to render drawing primitives not supported by the hardware
3143 * accelerated pipeline. It can also be used to cache a complex view tree
3144 * into a texture and reduce the complexity of drawing operations. For instance,
3145 * when animating a complex view tree with a translation, a software layer can
3146 * be used to render the view tree only once.</p>
3147 * <p>Software layers should be avoided when the affected view tree updates
3148 * often. Every update will require to re-render the software layer, which can
3149 * potentially be slow (particularly when hardware acceleration is turned on
3150 * since the layer will have to be uploaded into a hardware texture after every
3151 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003152 *
3153 * @see #getLayerType()
3154 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003155 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003156 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003157 */
3158 public static final int LAYER_TYPE_SOFTWARE = 1;
3159
3160 /**
3161 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3162 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3163 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3164 * rendering pipeline, but only if hardware acceleration is turned on for the
3165 * view hierarchy. When hardware acceleration is turned off, hardware layers
3166 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003167 *
Romain Guy171c5922011-01-06 10:04:23 -08003168 * <p>A hardware layer is useful to apply a specific color filter and/or
3169 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003170 * <p>A hardware layer can be used to cache a complex view tree into a
3171 * texture and reduce the complexity of drawing operations. For instance,
3172 * when animating a complex view tree with a translation, a hardware layer can
3173 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003174 * <p>A hardware layer can also be used to increase the rendering quality when
3175 * rotation transformations are applied on a view. It can also be used to
3176 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003177 *
3178 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003179 * @see #setLayerType(int, android.graphics.Paint)
3180 * @see #LAYER_TYPE_NONE
3181 * @see #LAYER_TYPE_SOFTWARE
3182 */
3183 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003184
Romain Guy3aaff3a2011-01-12 14:18:47 -08003185 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3186 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3187 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3188 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3189 })
Romain Guy171c5922011-01-06 10:04:23 -08003190 int mLayerType = LAYER_TYPE_NONE;
3191 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003192 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003193
3194 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003195 * Set to true when the view is sending hover accessibility events because it
3196 * is the innermost hovered view.
3197 */
3198 private boolean mSendingHoverAccessibilityEvents;
3199
Dianne Hackborn4702a852012-08-17 15:18:29 -07003200 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003201 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003202 */
3203 AccessibilityDelegate mAccessibilityDelegate;
3204
3205 /**
3206 * Consistency verifier for debugging purposes.
3207 * @hide
3208 */
3209 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3210 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3211 new InputEventConsistencyVerifier(this, 0) : null;
3212
Adam Powella9108a22012-07-18 11:18:09 -07003213 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3214
Jeff Brown87b7f802011-06-21 18:35:45 -07003215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 * Simple constructor to use when creating a view from code.
3217 *
3218 * @param context The Context the view is running in, through which it can
3219 * access the current theme, resources, etc.
3220 */
3221 public View(Context context) {
3222 mContext = context;
3223 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003224 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07003225 // Set some flags defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003226 mPrivateFlags2 =
3227 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003228 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003229 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003230 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003231 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003232 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003233 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003234 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003235 mUserPaddingStart = UNDEFINED_PADDING;
3236 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 }
3238
3239 /**
3240 * Constructor that is called when inflating a view from XML. This is called
3241 * when a view is being constructed from an XML file, supplying attributes
3242 * that were specified in the XML file. This version uses a default style of
3243 * 0, so the only attribute values applied are those in the Context's Theme
3244 * and the given AttributeSet.
3245 *
3246 * <p>
3247 * The method onFinishInflate() will be called after all children have been
3248 * added.
3249 *
3250 * @param context The Context the view is running in, through which it can
3251 * access the current theme, resources, etc.
3252 * @param attrs The attributes of the XML tag that is inflating the view.
3253 * @see #View(Context, AttributeSet, int)
3254 */
3255 public View(Context context, AttributeSet attrs) {
3256 this(context, attrs, 0);
3257 }
3258
3259 /**
3260 * Perform inflation from XML and apply a class-specific base style. This
3261 * constructor of View allows subclasses to use their own base style when
3262 * they are inflating. For example, a Button class's constructor would call
3263 * this version of the super class constructor and supply
3264 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3265 * the theme's button style to modify all of the base view attributes (in
3266 * particular its background) as well as the Button class's attributes.
3267 *
3268 * @param context The Context the view is running in, through which it can
3269 * access the current theme, resources, etc.
3270 * @param attrs The attributes of the XML tag that is inflating the view.
3271 * @param defStyle The default style to apply to this view. If 0, no style
3272 * will be applied (beyond what is included in the theme). This may
3273 * either be an attribute resource, whose value will be retrieved
3274 * from the current theme, or an explicit style resource.
3275 * @see #View(Context, AttributeSet)
3276 */
3277 public View(Context context, AttributeSet attrs, int defStyle) {
3278 this(context);
3279
3280 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3281 defStyle, 0);
3282
3283 Drawable background = null;
3284
3285 int leftPadding = -1;
3286 int topPadding = -1;
3287 int rightPadding = -1;
3288 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003289 int startPadding = UNDEFINED_PADDING;
3290 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291
3292 int padding = -1;
3293
3294 int viewFlagValues = 0;
3295 int viewFlagMasks = 0;
3296
3297 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 int x = 0;
3300 int y = 0;
3301
Chet Haase73066682010-11-29 15:55:32 -08003302 float tx = 0;
3303 float ty = 0;
3304 float rotation = 0;
3305 float rotationX = 0;
3306 float rotationY = 0;
3307 float sx = 1f;
3308 float sy = 1f;
3309 boolean transformSet = false;
3310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003312 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003313 boolean initializeScrollbars = false;
3314
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003315 boolean leftPaddingDefined = false;
3316 boolean rightPaddingDefined = false;
3317 boolean startPaddingDefined = false;
3318 boolean endPaddingDefined = false;
3319
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003320 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 final int N = a.getIndexCount();
3323 for (int i = 0; i < N; i++) {
3324 int attr = a.getIndex(i);
3325 switch (attr) {
3326 case com.android.internal.R.styleable.View_background:
3327 background = a.getDrawable(attr);
3328 break;
3329 case com.android.internal.R.styleable.View_padding:
3330 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003331 mUserPaddingLeftInitial = padding;
3332 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003333 leftPaddingDefined = true;
3334 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 break;
3336 case com.android.internal.R.styleable.View_paddingLeft:
3337 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003338 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003339 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 break;
3341 case com.android.internal.R.styleable.View_paddingTop:
3342 topPadding = a.getDimensionPixelSize(attr, -1);
3343 break;
3344 case com.android.internal.R.styleable.View_paddingRight:
3345 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003346 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003347 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 break;
3349 case com.android.internal.R.styleable.View_paddingBottom:
3350 bottomPadding = a.getDimensionPixelSize(attr, -1);
3351 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003352 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003353 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003354 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003355 break;
3356 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003357 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003358 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003359 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 case com.android.internal.R.styleable.View_scrollX:
3361 x = a.getDimensionPixelOffset(attr, 0);
3362 break;
3363 case com.android.internal.R.styleable.View_scrollY:
3364 y = a.getDimensionPixelOffset(attr, 0);
3365 break;
Chet Haase73066682010-11-29 15:55:32 -08003366 case com.android.internal.R.styleable.View_alpha:
3367 setAlpha(a.getFloat(attr, 1f));
3368 break;
3369 case com.android.internal.R.styleable.View_transformPivotX:
3370 setPivotX(a.getDimensionPixelOffset(attr, 0));
3371 break;
3372 case com.android.internal.R.styleable.View_transformPivotY:
3373 setPivotY(a.getDimensionPixelOffset(attr, 0));
3374 break;
3375 case com.android.internal.R.styleable.View_translationX:
3376 tx = a.getDimensionPixelOffset(attr, 0);
3377 transformSet = true;
3378 break;
3379 case com.android.internal.R.styleable.View_translationY:
3380 ty = a.getDimensionPixelOffset(attr, 0);
3381 transformSet = true;
3382 break;
3383 case com.android.internal.R.styleable.View_rotation:
3384 rotation = a.getFloat(attr, 0);
3385 transformSet = true;
3386 break;
3387 case com.android.internal.R.styleable.View_rotationX:
3388 rotationX = a.getFloat(attr, 0);
3389 transformSet = true;
3390 break;
3391 case com.android.internal.R.styleable.View_rotationY:
3392 rotationY = a.getFloat(attr, 0);
3393 transformSet = true;
3394 break;
3395 case com.android.internal.R.styleable.View_scaleX:
3396 sx = a.getFloat(attr, 1f);
3397 transformSet = true;
3398 break;
3399 case com.android.internal.R.styleable.View_scaleY:
3400 sy = a.getFloat(attr, 1f);
3401 transformSet = true;
3402 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 case com.android.internal.R.styleable.View_id:
3404 mID = a.getResourceId(attr, NO_ID);
3405 break;
3406 case com.android.internal.R.styleable.View_tag:
3407 mTag = a.getText(attr);
3408 break;
3409 case com.android.internal.R.styleable.View_fitsSystemWindows:
3410 if (a.getBoolean(attr, false)) {
3411 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3412 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3413 }
3414 break;
3415 case com.android.internal.R.styleable.View_focusable:
3416 if (a.getBoolean(attr, false)) {
3417 viewFlagValues |= FOCUSABLE;
3418 viewFlagMasks |= FOCUSABLE_MASK;
3419 }
3420 break;
3421 case com.android.internal.R.styleable.View_focusableInTouchMode:
3422 if (a.getBoolean(attr, false)) {
3423 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3424 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3425 }
3426 break;
3427 case com.android.internal.R.styleable.View_clickable:
3428 if (a.getBoolean(attr, false)) {
3429 viewFlagValues |= CLICKABLE;
3430 viewFlagMasks |= CLICKABLE;
3431 }
3432 break;
3433 case com.android.internal.R.styleable.View_longClickable:
3434 if (a.getBoolean(attr, false)) {
3435 viewFlagValues |= LONG_CLICKABLE;
3436 viewFlagMasks |= LONG_CLICKABLE;
3437 }
3438 break;
3439 case com.android.internal.R.styleable.View_saveEnabled:
3440 if (!a.getBoolean(attr, true)) {
3441 viewFlagValues |= SAVE_DISABLED;
3442 viewFlagMasks |= SAVE_DISABLED_MASK;
3443 }
3444 break;
3445 case com.android.internal.R.styleable.View_duplicateParentState:
3446 if (a.getBoolean(attr, false)) {
3447 viewFlagValues |= DUPLICATE_PARENT_STATE;
3448 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3449 }
3450 break;
3451 case com.android.internal.R.styleable.View_visibility:
3452 final int visibility = a.getInt(attr, 0);
3453 if (visibility != 0) {
3454 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3455 viewFlagMasks |= VISIBILITY_MASK;
3456 }
3457 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003458 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003459 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003460 mPrivateFlags2 &=
3461 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003462 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003463 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003464 final int value = (layoutDirection != -1) ?
3465 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003466 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003467 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 case com.android.internal.R.styleable.View_drawingCacheQuality:
3469 final int cacheQuality = a.getInt(attr, 0);
3470 if (cacheQuality != 0) {
3471 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3472 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3473 }
3474 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003475 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003476 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003477 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003478 case com.android.internal.R.styleable.View_labelFor:
3479 setLabelFor(a.getResourceId(attr, NO_ID));
3480 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3482 if (!a.getBoolean(attr, true)) {
3483 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3484 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3485 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003486 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3488 if (!a.getBoolean(attr, true)) {
3489 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3490 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3491 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003492 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 case R.styleable.View_scrollbars:
3494 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3495 if (scrollbars != SCROLLBARS_NONE) {
3496 viewFlagValues |= scrollbars;
3497 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003498 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003501 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003503 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003504 // Ignore the attribute starting with ICS
3505 break;
3506 }
3507 // With builds < ICS, fall through and apply fading edges
3508 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3510 if (fadingEdge != FADING_EDGE_NONE) {
3511 viewFlagValues |= fadingEdge;
3512 viewFlagMasks |= FADING_EDGE_MASK;
3513 initializeFadingEdge(a);
3514 }
3515 break;
3516 case R.styleable.View_scrollbarStyle:
3517 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3518 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3519 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3520 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3521 }
3522 break;
3523 case R.styleable.View_isScrollContainer:
3524 setScrollContainer = true;
3525 if (a.getBoolean(attr, false)) {
3526 setScrollContainer(true);
3527 }
3528 break;
3529 case com.android.internal.R.styleable.View_keepScreenOn:
3530 if (a.getBoolean(attr, false)) {
3531 viewFlagValues |= KEEP_SCREEN_ON;
3532 viewFlagMasks |= KEEP_SCREEN_ON;
3533 }
3534 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003535 case R.styleable.View_filterTouchesWhenObscured:
3536 if (a.getBoolean(attr, false)) {
3537 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3538 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3539 }
3540 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 case R.styleable.View_nextFocusLeft:
3542 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3543 break;
3544 case R.styleable.View_nextFocusRight:
3545 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3546 break;
3547 case R.styleable.View_nextFocusUp:
3548 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3549 break;
3550 case R.styleable.View_nextFocusDown:
3551 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3552 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003553 case R.styleable.View_nextFocusForward:
3554 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3555 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 case R.styleable.View_minWidth:
3557 mMinWidth = a.getDimensionPixelSize(attr, 0);
3558 break;
3559 case R.styleable.View_minHeight:
3560 mMinHeight = a.getDimensionPixelSize(attr, 0);
3561 break;
Romain Guy9a817362009-05-01 10:57:14 -07003562 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003563 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003564 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003565 + "be used within a restricted context");
3566 }
3567
Romain Guy9a817362009-05-01 10:57:14 -07003568 final String handlerName = a.getString(attr);
3569 if (handlerName != null) {
3570 setOnClickListener(new OnClickListener() {
3571 private Method mHandler;
3572
3573 public void onClick(View v) {
3574 if (mHandler == null) {
3575 try {
3576 mHandler = getContext().getClass().getMethod(handlerName,
3577 View.class);
3578 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003579 int id = getId();
3580 String idText = id == NO_ID ? "" : " with id '"
3581 + getContext().getResources().getResourceEntryName(
3582 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003583 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003584 handlerName + "(View) in the activity "
3585 + getContext().getClass() + " for onClick handler"
3586 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003587 }
3588 }
3589
3590 try {
3591 mHandler.invoke(getContext(), View.this);
3592 } catch (IllegalAccessException e) {
3593 throw new IllegalStateException("Could not execute non "
3594 + "public method of the activity", e);
3595 } catch (InvocationTargetException e) {
3596 throw new IllegalStateException("Could not execute "
3597 + "method of the activity", e);
3598 }
3599 }
3600 });
3601 }
3602 break;
Adam Powell637d3372010-08-25 14:37:03 -07003603 case R.styleable.View_overScrollMode:
3604 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3605 break;
Adam Powell20232d02010-12-08 21:08:53 -08003606 case R.styleable.View_verticalScrollbarPosition:
3607 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3608 break;
Romain Guy171c5922011-01-06 10:04:23 -08003609 case R.styleable.View_layerType:
3610 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3611 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003612 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003613 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003614 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003615 // Set the text direction flags depending on the value of the attribute
3616 final int textDirection = a.getInt(attr, -1);
3617 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003618 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003619 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003620 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003621 case R.styleable.View_textAlignment:
3622 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003623 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003624 // Set the text alignment flag depending on the value of the attribute
3625 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003626 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003627 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003628 case R.styleable.View_importantForAccessibility:
3629 setImportantForAccessibility(a.getInt(attr,
3630 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003631 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633 }
3634
Adam Powell637d3372010-08-25 14:37:03 -07003635 setOverScrollMode(overScrollMode);
3636
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003637 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3638 // the resolved layout direction). Those cached values will be used later during padding
3639 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003640 mUserPaddingStart = startPadding;
3641 mUserPaddingEnd = endPadding;
3642
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003643 if (background != null) {
3644 setBackground(background);
3645 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 if (padding >= 0) {
3648 leftPadding = padding;
3649 topPadding = padding;
3650 rightPadding = padding;
3651 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003652 mUserPaddingLeftInitial = padding;
3653 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 }
3655
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003656 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003657 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3658 // left / right padding are used if defined (meaning here nothing to do). If they are not
3659 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3660 // start / end and resolve them as left / right (layout direction is not taken into account).
3661 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3662 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3663 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003664 if (!leftPaddingDefined && startPaddingDefined) {
3665 leftPadding = startPadding;
3666 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003667 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003668 if (!rightPaddingDefined && endPaddingDefined) {
3669 rightPadding = endPadding;
3670 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003671 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003672 } else {
3673 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3674 // values defined. Otherwise, left /right values are used.
3675 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3676 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3677 // defined.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003678 if (leftPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003679 mUserPaddingLeftInitial = leftPadding;
3680 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003681 if (rightPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003682 mUserPaddingRightInitial = rightPadding;
3683 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003684 }
3685
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003686 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003687 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003689 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3691
3692 if (viewFlagMasks != 0) {
3693 setFlags(viewFlagValues, viewFlagMasks);
3694 }
3695
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003696 if (initializeScrollbars) {
3697 initializeScrollbars(a);
3698 }
3699
3700 a.recycle();
3701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 // Needs to be called after mViewFlags is set
3703 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3704 recomputePadding();
3705 }
3706
3707 if (x != 0 || y != 0) {
3708 scrollTo(x, y);
3709 }
3710
Chet Haase73066682010-11-29 15:55:32 -08003711 if (transformSet) {
3712 setTranslationX(tx);
3713 setTranslationY(ty);
3714 setRotation(rotation);
3715 setRotationX(rotationX);
3716 setRotationY(rotationY);
3717 setScaleX(sx);
3718 setScaleY(sy);
3719 }
3720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3722 setScrollContainer(true);
3723 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003724
3725 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 }
3727
3728 /**
3729 * Non-public constructor for use in testing
3730 */
3731 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003732 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 }
3734
Dianne Hackborn4702a852012-08-17 15:18:29 -07003735 public String toString() {
3736 StringBuilder out = new StringBuilder(128);
3737 out.append(getClass().getName());
3738 out.append('{');
3739 out.append(Integer.toHexString(System.identityHashCode(this)));
3740 out.append(' ');
3741 switch (mViewFlags&VISIBILITY_MASK) {
3742 case VISIBLE: out.append('V'); break;
3743 case INVISIBLE: out.append('I'); break;
3744 case GONE: out.append('G'); break;
3745 default: out.append('.'); break;
3746 }
3747 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3748 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3749 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3750 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3751 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3752 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3753 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3754 out.append(' ');
3755 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3756 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3757 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3758 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3759 out.append('p');
3760 } else {
3761 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3762 }
3763 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3764 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3765 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3766 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3767 out.append(' ');
3768 out.append(mLeft);
3769 out.append(',');
3770 out.append(mTop);
3771 out.append('-');
3772 out.append(mRight);
3773 out.append(',');
3774 out.append(mBottom);
3775 final int id = getId();
3776 if (id != NO_ID) {
3777 out.append(" #");
3778 out.append(Integer.toHexString(id));
3779 final Resources r = mResources;
3780 if (id != 0 && r != null) {
3781 try {
3782 String pkgname;
3783 switch (id&0xff000000) {
3784 case 0x7f000000:
3785 pkgname="app";
3786 break;
3787 case 0x01000000:
3788 pkgname="android";
3789 break;
3790 default:
3791 pkgname = r.getResourcePackageName(id);
3792 break;
3793 }
3794 String typename = r.getResourceTypeName(id);
3795 String entryname = r.getResourceEntryName(id);
3796 out.append(" ");
3797 out.append(pkgname);
3798 out.append(":");
3799 out.append(typename);
3800 out.append("/");
3801 out.append(entryname);
3802 } catch (Resources.NotFoundException e) {
3803 }
3804 }
3805 }
3806 out.append("}");
3807 return out.toString();
3808 }
3809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 /**
3811 * <p>
3812 * Initializes the fading edges from a given set of styled attributes. This
3813 * method should be called by subclasses that need fading edges and when an
3814 * instance of these subclasses is created programmatically rather than
3815 * being inflated from XML. This method is automatically called when the XML
3816 * is inflated.
3817 * </p>
3818 *
3819 * @param a the styled attributes set to initialize the fading edges from
3820 */
3821 protected void initializeFadingEdge(TypedArray a) {
3822 initScrollCache();
3823
3824 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3825 R.styleable.View_fadingEdgeLength,
3826 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3827 }
3828
3829 /**
3830 * Returns the size of the vertical faded edges used to indicate that more
3831 * content in this view is visible.
3832 *
3833 * @return The size in pixels of the vertical faded edge or 0 if vertical
3834 * faded edges are not enabled for this view.
3835 * @attr ref android.R.styleable#View_fadingEdgeLength
3836 */
3837 public int getVerticalFadingEdgeLength() {
3838 if (isVerticalFadingEdgeEnabled()) {
3839 ScrollabilityCache cache = mScrollCache;
3840 if (cache != null) {
3841 return cache.fadingEdgeLength;
3842 }
3843 }
3844 return 0;
3845 }
3846
3847 /**
3848 * Set the size of the faded edge used to indicate that more content in this
3849 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003850 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3851 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3852 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 *
3854 * @param length The size in pixels of the faded edge used to indicate that more
3855 * content in this view is visible.
3856 */
3857 public void setFadingEdgeLength(int length) {
3858 initScrollCache();
3859 mScrollCache.fadingEdgeLength = length;
3860 }
3861
3862 /**
3863 * Returns the size of the horizontal faded edges used to indicate that more
3864 * content in this view is visible.
3865 *
3866 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3867 * faded edges are not enabled for this view.
3868 * @attr ref android.R.styleable#View_fadingEdgeLength
3869 */
3870 public int getHorizontalFadingEdgeLength() {
3871 if (isHorizontalFadingEdgeEnabled()) {
3872 ScrollabilityCache cache = mScrollCache;
3873 if (cache != null) {
3874 return cache.fadingEdgeLength;
3875 }
3876 }
3877 return 0;
3878 }
3879
3880 /**
3881 * Returns the width of the vertical scrollbar.
3882 *
3883 * @return The width in pixels of the vertical scrollbar or 0 if there
3884 * is no vertical scrollbar.
3885 */
3886 public int getVerticalScrollbarWidth() {
3887 ScrollabilityCache cache = mScrollCache;
3888 if (cache != null) {
3889 ScrollBarDrawable scrollBar = cache.scrollBar;
3890 if (scrollBar != null) {
3891 int size = scrollBar.getSize(true);
3892 if (size <= 0) {
3893 size = cache.scrollBarSize;
3894 }
3895 return size;
3896 }
3897 return 0;
3898 }
3899 return 0;
3900 }
3901
3902 /**
3903 * Returns the height of the horizontal scrollbar.
3904 *
3905 * @return The height in pixels of the horizontal scrollbar or 0 if
3906 * there is no horizontal scrollbar.
3907 */
3908 protected int getHorizontalScrollbarHeight() {
3909 ScrollabilityCache cache = mScrollCache;
3910 if (cache != null) {
3911 ScrollBarDrawable scrollBar = cache.scrollBar;
3912 if (scrollBar != null) {
3913 int size = scrollBar.getSize(false);
3914 if (size <= 0) {
3915 size = cache.scrollBarSize;
3916 }
3917 return size;
3918 }
3919 return 0;
3920 }
3921 return 0;
3922 }
3923
3924 /**
3925 * <p>
3926 * Initializes the scrollbars from a given set of styled attributes. This
3927 * method should be called by subclasses that need scrollbars and when an
3928 * instance of these subclasses is created programmatically rather than
3929 * being inflated from XML. This method is automatically called when the XML
3930 * is inflated.
3931 * </p>
3932 *
3933 * @param a the styled attributes set to initialize the scrollbars from
3934 */
3935 protected void initializeScrollbars(TypedArray a) {
3936 initScrollCache();
3937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003939
Mike Cleronf116bf82009-09-27 19:14:12 -07003940 if (scrollabilityCache.scrollBar == null) {
3941 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3942 }
Joe Malin32736f02011-01-19 16:14:20 -08003943
Romain Guy8bda2482010-03-02 11:42:11 -08003944 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945
Mike Cleronf116bf82009-09-27 19:14:12 -07003946 if (!fadeScrollbars) {
3947 scrollabilityCache.state = ScrollabilityCache.ON;
3948 }
3949 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003950
3951
Mike Cleronf116bf82009-09-27 19:14:12 -07003952 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3953 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3954 .getScrollBarFadeDuration());
3955 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3956 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3957 ViewConfiguration.getScrollDefaultDelay());
3958
Joe Malin32736f02011-01-19 16:14:20 -08003959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3961 com.android.internal.R.styleable.View_scrollbarSize,
3962 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3963
3964 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3965 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3966
3967 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3968 if (thumb != null) {
3969 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3970 }
3971
3972 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3973 false);
3974 if (alwaysDraw) {
3975 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3976 }
3977
3978 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3979 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3980
3981 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3982 if (thumb != null) {
3983 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3984 }
3985
3986 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3987 false);
3988 if (alwaysDraw) {
3989 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3990 }
3991
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003992 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07003993 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003994 if (track != null) {
3995 track.setLayoutDirection(layoutDirection);
3996 }
3997 if (thumb != null) {
3998 thumb.setLayoutDirection(layoutDirection);
3999 }
4000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004002 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 }
4004
4005 /**
4006 * <p>
4007 * Initalizes the scrollability cache if necessary.
4008 * </p>
4009 */
4010 private void initScrollCache() {
4011 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004012 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014 }
4015
Philip Milne6c8ea062012-04-03 17:38:43 -07004016 private ScrollabilityCache getScrollCache() {
4017 initScrollCache();
4018 return mScrollCache;
4019 }
4020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 /**
Adam Powell20232d02010-12-08 21:08:53 -08004022 * Set the position of the vertical scroll bar. Should be one of
4023 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4024 * {@link #SCROLLBAR_POSITION_RIGHT}.
4025 *
4026 * @param position Where the vertical scroll bar should be positioned.
4027 */
4028 public void setVerticalScrollbarPosition(int position) {
4029 if (mVerticalScrollbarPosition != position) {
4030 mVerticalScrollbarPosition = position;
4031 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004032 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004033 }
4034 }
4035
4036 /**
4037 * @return The position where the vertical scroll bar will show, if applicable.
4038 * @see #setVerticalScrollbarPosition(int)
4039 */
4040 public int getVerticalScrollbarPosition() {
4041 return mVerticalScrollbarPosition;
4042 }
4043
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004044 ListenerInfo getListenerInfo() {
4045 if (mListenerInfo != null) {
4046 return mListenerInfo;
4047 }
4048 mListenerInfo = new ListenerInfo();
4049 return mListenerInfo;
4050 }
4051
Adam Powell20232d02010-12-08 21:08:53 -08004052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 * Register a callback to be invoked when focus of this view changed.
4054 *
4055 * @param l The callback that will run.
4056 */
4057 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004058 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 }
4060
4061 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004062 * Add a listener that will be called when the bounds of the view change due to
4063 * layout processing.
4064 *
4065 * @param listener The listener that will be called when layout bounds change.
4066 */
4067 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004068 ListenerInfo li = getListenerInfo();
4069 if (li.mOnLayoutChangeListeners == null) {
4070 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004071 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004072 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4073 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004074 }
Chet Haase21cd1382010-09-01 17:42:29 -07004075 }
4076
4077 /**
4078 * Remove a listener for layout changes.
4079 *
4080 * @param listener The listener for layout bounds change.
4081 */
4082 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004083 ListenerInfo li = mListenerInfo;
4084 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004085 return;
4086 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004087 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004088 }
4089
4090 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004091 * Add a listener for attach state changes.
4092 *
4093 * This listener will be called whenever this view is attached or detached
4094 * from a window. Remove the listener using
4095 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4096 *
4097 * @param listener Listener to attach
4098 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4099 */
4100 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004101 ListenerInfo li = getListenerInfo();
4102 if (li.mOnAttachStateChangeListeners == null) {
4103 li.mOnAttachStateChangeListeners
4104 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004105 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004106 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004107 }
4108
4109 /**
4110 * Remove a listener for attach state changes. The listener will receive no further
4111 * notification of window attach/detach events.
4112 *
4113 * @param listener Listener to remove
4114 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4115 */
4116 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004117 ListenerInfo li = mListenerInfo;
4118 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004119 return;
4120 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004121 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004122 }
4123
4124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 * Returns the focus-change callback registered for this view.
4126 *
4127 * @return The callback, or null if one is not registered.
4128 */
4129 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004130 ListenerInfo li = mListenerInfo;
4131 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
4133
4134 /**
4135 * Register a callback to be invoked when this view is clicked. If this view is not
4136 * clickable, it becomes clickable.
4137 *
4138 * @param l The callback that will run
4139 *
4140 * @see #setClickable(boolean)
4141 */
4142 public void setOnClickListener(OnClickListener l) {
4143 if (!isClickable()) {
4144 setClickable(true);
4145 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004146 getListenerInfo().mOnClickListener = l;
4147 }
4148
4149 /**
4150 * Return whether this view has an attached OnClickListener. Returns
4151 * true if there is a listener, false if there is none.
4152 */
4153 public boolean hasOnClickListeners() {
4154 ListenerInfo li = mListenerInfo;
4155 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
4157
4158 /**
4159 * Register a callback to be invoked when this view is clicked and held. If this view is not
4160 * long clickable, it becomes long clickable.
4161 *
4162 * @param l The callback that will run
4163 *
4164 * @see #setLongClickable(boolean)
4165 */
4166 public void setOnLongClickListener(OnLongClickListener l) {
4167 if (!isLongClickable()) {
4168 setLongClickable(true);
4169 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004170 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 }
4172
4173 /**
4174 * Register a callback to be invoked when the context menu for this view is
4175 * being built. If this view is not long clickable, it becomes long clickable.
4176 *
4177 * @param l The callback that will run
4178 *
4179 */
4180 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4181 if (!isLongClickable()) {
4182 setLongClickable(true);
4183 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004184 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 }
4186
4187 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004188 * Call this view's OnClickListener, if it is defined. Performs all normal
4189 * actions associated with clicking: reporting accessibility event, playing
4190 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 *
4192 * @return True there was an assigned OnClickListener that was called, false
4193 * otherwise is returned.
4194 */
4195 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004196 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4197
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004198 ListenerInfo li = mListenerInfo;
4199 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004201 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 return true;
4203 }
4204
4205 return false;
4206 }
4207
4208 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004209 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4210 * this only calls the listener, and does not do any associated clicking
4211 * actions like reporting an accessibility event.
4212 *
4213 * @return True there was an assigned OnClickListener that was called, false
4214 * otherwise is returned.
4215 */
4216 public boolean callOnClick() {
4217 ListenerInfo li = mListenerInfo;
4218 if (li != null && li.mOnClickListener != null) {
4219 li.mOnClickListener.onClick(this);
4220 return true;
4221 }
4222 return false;
4223 }
4224
4225 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004226 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4227 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004229 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 */
4231 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004232 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004235 ListenerInfo li = mListenerInfo;
4236 if (li != null && li.mOnLongClickListener != null) {
4237 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 }
4239 if (!handled) {
4240 handled = showContextMenu();
4241 }
4242 if (handled) {
4243 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4244 }
4245 return handled;
4246 }
4247
4248 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004249 * Performs button-related actions during a touch down event.
4250 *
4251 * @param event The event.
4252 * @return True if the down was consumed.
4253 *
4254 * @hide
4255 */
4256 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4257 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4258 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4259 return true;
4260 }
4261 }
4262 return false;
4263 }
4264
4265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 * Bring up the context menu for this view.
4267 *
4268 * @return Whether a context menu was displayed.
4269 */
4270 public boolean showContextMenu() {
4271 return getParent().showContextMenuForChild(this);
4272 }
4273
4274 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004275 * Bring up the context menu for this view, referring to the item under the specified point.
4276 *
4277 * @param x The referenced x coordinate.
4278 * @param y The referenced y coordinate.
4279 * @param metaState The keyboard modifiers that were pressed.
4280 * @return Whether a context menu was displayed.
4281 *
4282 * @hide
4283 */
4284 public boolean showContextMenu(float x, float y, int metaState) {
4285 return showContextMenu();
4286 }
4287
4288 /**
Adam Powell6e346362010-07-23 10:18:23 -07004289 * Start an action mode.
4290 *
4291 * @param callback Callback that will control the lifecycle of the action mode
4292 * @return The new action mode if it is started, null otherwise
4293 *
4294 * @see ActionMode
4295 */
4296 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004297 ViewParent parent = getParent();
4298 if (parent == null) return null;
4299 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004300 }
4301
4302 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004303 * Register a callback to be invoked when a hardware key is pressed in this view.
4304 * Key presses in software input methods will generally not trigger the methods of
4305 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 * @param l the key listener to attach to this view
4307 */
4308 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004309 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 }
4311
4312 /**
4313 * Register a callback to be invoked when a touch event is sent to this view.
4314 * @param l the touch listener to attach to this view
4315 */
4316 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004317 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 }
4319
4320 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004321 * Register a callback to be invoked when a generic motion event is sent to this view.
4322 * @param l the generic motion listener to attach to this view
4323 */
4324 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004325 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004326 }
4327
4328 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004329 * Register a callback to be invoked when a hover event is sent to this view.
4330 * @param l the hover listener to attach to this view
4331 */
4332 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004333 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004334 }
4335
4336 /**
Joe Malin32736f02011-01-19 16:14:20 -08004337 * Register a drag event listener callback object for this View. The parameter is
4338 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4339 * View, the system calls the
4340 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4341 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004342 */
4343 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004344 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004345 }
4346
4347 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004348 * Give this view focus. This will cause
4349 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 *
4351 * Note: this does not check whether this {@link View} should get focus, it just
4352 * gives it focus no matter what. It should only be called internally by framework
4353 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4354 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004355 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4356 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 * focus moved when requestFocus() is called. It may not always
4358 * apply, in which case use the default View.FOCUS_DOWN.
4359 * @param previouslyFocusedRect The rectangle of the view that had focus
4360 * prior in this View's coordinate system.
4361 */
4362 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4363 if (DBG) {
4364 System.out.println(this + " requestFocus()");
4365 }
4366
Dianne Hackborn4702a852012-08-17 15:18:29 -07004367 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4368 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369
4370 if (mParent != null) {
4371 mParent.requestChildFocus(this, this);
4372 }
4373
4374 onFocusChanged(true, direction, previouslyFocusedRect);
4375 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004376
4377 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4378 notifyAccessibilityStateChanged();
4379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 }
4381 }
4382
4383 /**
4384 * Request that a rectangle of this view be visible on the screen,
4385 * scrolling if necessary just enough.
4386 *
4387 * <p>A View should call this if it maintains some notion of which part
4388 * of its content is interesting. For example, a text editing view
4389 * should call this when its cursor moves.
4390 *
4391 * @param rectangle The rectangle.
4392 * @return Whether any parent scrolled.
4393 */
4394 public boolean requestRectangleOnScreen(Rect rectangle) {
4395 return requestRectangleOnScreen(rectangle, false);
4396 }
4397
4398 /**
4399 * Request that a rectangle of this view be visible on the screen,
4400 * scrolling if necessary just enough.
4401 *
4402 * <p>A View should call this if it maintains some notion of which part
4403 * of its content is interesting. For example, a text editing view
4404 * should call this when its cursor moves.
4405 *
4406 * <p>When <code>immediate</code> is set to true, scrolling will not be
4407 * animated.
4408 *
4409 * @param rectangle The rectangle.
4410 * @param immediate True to forbid animated scrolling, false otherwise
4411 * @return Whether any parent scrolled.
4412 */
4413 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004414 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004415 return false;
4416 }
4417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004419
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004420 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004421 position.set(rectangle);
4422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 ViewParent parent = mParent;
4424 boolean scrolled = false;
4425 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004426 rectangle.set((int) position.left, (int) position.top,
4427 (int) position.right, (int) position.bottom);
4428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 scrolled |= parent.requestChildRectangleOnScreen(child,
4430 rectangle, immediate);
4431
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004432 if (!child.hasIdentityMatrix()) {
4433 child.getMatrix().mapRect(position);
4434 }
4435
4436 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437
4438 if (!(parent instanceof View)) {
4439 break;
4440 }
Romain Guy8506ab42009-06-11 17:35:47 -07004441
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004442 View parentView = (View) parent;
4443
4444 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4445
4446 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 parent = child.getParent();
4448 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 return scrolled;
4451 }
4452
4453 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004454 * Called when this view wants to give up focus. If focus is cleared
4455 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4456 * <p>
4457 * <strong>Note:</strong> When a View clears focus the framework is trying
4458 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004459 * View is the first from the top that can take focus, then all callbacks
4460 * related to clearing focus will be invoked after wich the framework will
4461 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004462 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 */
4464 public void clearFocus() {
4465 if (DBG) {
4466 System.out.println(this + " clearFocus()");
4467 }
4468
Dianne Hackborn4702a852012-08-17 15:18:29 -07004469 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4470 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471
4472 if (mParent != null) {
4473 mParent.clearChildFocus(this);
4474 }
4475
4476 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004479
4480 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004481
4482 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4483 notifyAccessibilityStateChanged();
4484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 }
4486 }
4487
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004488 void ensureInputFocusOnFirstFocusable() {
4489 View root = getRootView();
4490 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004491 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 }
4493 }
4494
4495 /**
4496 * Called internally by the view system when a new view is getting focus.
4497 * This is what clears the old focus.
4498 */
4499 void unFocus() {
4500 if (DBG) {
4501 System.out.println(this + " unFocus()");
4502 }
4503
Dianne Hackborn4702a852012-08-17 15:18:29 -07004504 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4505 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506
4507 onFocusChanged(false, 0, null);
4508 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004509
4510 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4511 notifyAccessibilityStateChanged();
4512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 }
4514 }
4515
4516 /**
4517 * Returns true if this view has focus iteself, or is the ancestor of the
4518 * view that has focus.
4519 *
4520 * @return True if this view has or contains focus, false otherwise.
4521 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004522 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004524 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 }
4526
4527 /**
4528 * Returns true if this view is focusable or if it contains a reachable View
4529 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4530 * is a View whose parents do not block descendants focus.
4531 *
4532 * Only {@link #VISIBLE} views are considered focusable.
4533 *
4534 * @return True if the view is focusable or if the view contains a focusable
4535 * View, false otherwise.
4536 *
4537 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4538 */
4539 public boolean hasFocusable() {
4540 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4541 }
4542
4543 /**
4544 * Called by the view system when the focus state of this view changes.
4545 * When the focus change event is caused by directional navigation, direction
4546 * and previouslyFocusedRect provide insight into where the focus is coming from.
4547 * When overriding, be sure to call up through to the super class so that
4548 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004549 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 * @param gainFocus True if the View has focus; false otherwise.
4551 * @param direction The direction focus has moved when requestFocus()
4552 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004553 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4554 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4555 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4557 * system, of the previously focused view. If applicable, this will be
4558 * passed in as finer grained information about where the focus is coming
4559 * from (in addition to direction). Will be <code>null</code> otherwise.
4560 */
4561 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004562 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004563 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4564 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004565 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004566 }
4567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 InputMethodManager imm = InputMethodManager.peekInstance();
4569 if (!gainFocus) {
4570 if (isPressed()) {
4571 setPressed(false);
4572 }
4573 if (imm != null && mAttachInfo != null
4574 && mAttachInfo.mHasWindowFocus) {
4575 imm.focusOut(this);
4576 }
Romain Guya2431d02009-04-30 16:30:00 -07004577 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 } else if (imm != null && mAttachInfo != null
4579 && mAttachInfo.mHasWindowFocus) {
4580 imm.focusIn(this);
4581 }
Romain Guy8506ab42009-06-11 17:35:47 -07004582
Romain Guy0fd89bf2011-01-26 15:41:30 -08004583 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004584 ListenerInfo li = mListenerInfo;
4585 if (li != null && li.mOnFocusChangeListener != null) {
4586 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 }
Joe Malin32736f02011-01-19 16:14:20 -08004588
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004589 if (mAttachInfo != null) {
4590 mAttachInfo.mKeyDispatchState.reset(this);
4591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 }
4593
4594 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004595 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004596 * not enabled this method has no effect. The default implementation calls
4597 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4598 * to populate information about the event source (this View), then calls
4599 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4600 * populate the text content of the event source including its descendants,
4601 * and last calls
4602 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4603 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004604 * <p>
4605 * If an {@link AccessibilityDelegate} has been specified via calling
4606 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4607 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4608 * responsible for handling this call.
4609 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004610 *
Scott Mainb303d832011-10-12 16:45:18 -07004611 * @param eventType The type of the event to send, as defined by several types from
4612 * {@link android.view.accessibility.AccessibilityEvent}, such as
4613 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4614 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004615 *
4616 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4617 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4618 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004619 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004620 */
4621 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004622 if (mAccessibilityDelegate != null) {
4623 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4624 } else {
4625 sendAccessibilityEventInternal(eventType);
4626 }
4627 }
4628
4629 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004630 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4631 * {@link AccessibilityEvent} to make an announcement which is related to some
4632 * sort of a context change for which none of the events representing UI transitions
4633 * is a good fit. For example, announcing a new page in a book. If accessibility
4634 * is not enabled this method does nothing.
4635 *
4636 * @param text The announcement text.
4637 */
4638 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004639 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004640 AccessibilityEvent event = AccessibilityEvent.obtain(
4641 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004642 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004643 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004644 event.setContentDescription(null);
4645 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004646 }
4647 }
4648
4649 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004650 * @see #sendAccessibilityEvent(int)
4651 *
4652 * Note: Called from the default {@link AccessibilityDelegate}.
4653 */
4654 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004655 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4656 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4657 }
4658 }
4659
4660 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004661 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4662 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004663 * perform a check whether accessibility is enabled.
4664 * <p>
4665 * If an {@link AccessibilityDelegate} has been specified via calling
4666 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4667 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4668 * is responsible for handling this call.
4669 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004670 *
4671 * @param event The event to send.
4672 *
4673 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004674 */
4675 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004676 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004677 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004678 } else {
4679 sendAccessibilityEventUncheckedInternal(event);
4680 }
4681 }
4682
4683 /**
4684 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4685 *
4686 * Note: Called from the default {@link AccessibilityDelegate}.
4687 */
4688 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004689 if (!isShown()) {
4690 return;
4691 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004692 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004693 // Only a subset of accessibility events populates text content.
4694 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4695 dispatchPopulateAccessibilityEvent(event);
4696 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004697 // In the beginning we called #isShown(), so we know that getParent() is not null.
4698 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004699 }
4700
4701 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004702 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4703 * to its children for adding their text content to the event. Note that the
4704 * event text is populated in a separate dispatch path since we add to the
4705 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004706 * A typical implementation will call
4707 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4708 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4709 * on each child. Override this method if custom population of the event text
4710 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004711 * <p>
4712 * If an {@link AccessibilityDelegate} has been specified via calling
4713 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4714 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4715 * is responsible for handling this call.
4716 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004717 * <p>
4718 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4719 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4720 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004721 *
4722 * @param event The event.
4723 *
4724 * @return True if the event population was completed.
4725 */
4726 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004727 if (mAccessibilityDelegate != null) {
4728 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4729 } else {
4730 return dispatchPopulateAccessibilityEventInternal(event);
4731 }
4732 }
4733
4734 /**
4735 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4736 *
4737 * Note: Called from the default {@link AccessibilityDelegate}.
4738 */
4739 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004740 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004741 return false;
4742 }
4743
4744 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004745 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004746 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004747 * text content. While this method is free to modify event
4748 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004749 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4750 * <p>
4751 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004752 * to the text added by the super implementation:
4753 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004754 * super.onPopulateAccessibilityEvent(event);
4755 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4756 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4757 * mCurrentDate.getTimeInMillis(), flags);
4758 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004759 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004760 * <p>
4761 * If an {@link AccessibilityDelegate} has been specified via calling
4762 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4763 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4764 * is responsible for handling this call.
4765 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004766 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4767 * information to the event, in case the default implementation has basic information to add.
4768 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004769 *
4770 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004771 *
4772 * @see #sendAccessibilityEvent(int)
4773 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004774 */
4775 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004776 if (mAccessibilityDelegate != null) {
4777 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4778 } else {
4779 onPopulateAccessibilityEventInternal(event);
4780 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004781 }
4782
4783 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004784 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4785 *
4786 * Note: Called from the default {@link AccessibilityDelegate}.
4787 */
4788 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4789
4790 }
4791
4792 /**
4793 * Initializes an {@link AccessibilityEvent} with information about
4794 * this View which is the event source. In other words, the source of
4795 * an accessibility event is the view whose state change triggered firing
4796 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004797 * <p>
4798 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004799 * to properties set by the super implementation:
4800 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4801 * super.onInitializeAccessibilityEvent(event);
4802 * event.setPassword(true);
4803 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004804 * <p>
4805 * If an {@link AccessibilityDelegate} has been specified via calling
4806 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4807 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4808 * is responsible for handling this call.
4809 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004810 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4811 * information to the event, in case the default implementation has basic information to add.
4812 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004813 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004814 *
4815 * @see #sendAccessibilityEvent(int)
4816 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4817 */
4818 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004819 if (mAccessibilityDelegate != null) {
4820 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4821 } else {
4822 onInitializeAccessibilityEventInternal(event);
4823 }
4824 }
4825
4826 /**
4827 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4828 *
4829 * Note: Called from the default {@link AccessibilityDelegate}.
4830 */
4831 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004832 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004833 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004834 event.setPackageName(getContext().getPackageName());
4835 event.setEnabled(isEnabled());
4836 event.setContentDescription(mContentDescription);
4837
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004838 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004839 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004840 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4841 FOCUSABLES_ALL);
4842 event.setItemCount(focusablesTempList.size());
4843 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4844 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004845 }
4846 }
4847
4848 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004849 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4850 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4851 * This method is responsible for obtaining an accessibility node info from a
4852 * pool of reusable instances and calling
4853 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4854 * initialize the former.
4855 * <p>
4856 * Note: The client is responsible for recycling the obtained instance by calling
4857 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4858 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004859 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004860 * @return A populated {@link AccessibilityNodeInfo}.
4861 *
4862 * @see AccessibilityNodeInfo
4863 */
4864 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004865 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4866 if (provider != null) {
4867 return provider.createAccessibilityNodeInfo(View.NO_ID);
4868 } else {
4869 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4870 onInitializeAccessibilityNodeInfo(info);
4871 return info;
4872 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004873 }
4874
4875 /**
4876 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4877 * The base implementation sets:
4878 * <ul>
4879 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004880 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4881 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004882 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4883 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4884 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4885 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4886 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4887 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4888 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4889 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4890 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4891 * </ul>
4892 * <p>
4893 * Subclasses should override this method, call the super implementation,
4894 * and set additional attributes.
4895 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004896 * <p>
4897 * If an {@link AccessibilityDelegate} has been specified via calling
4898 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4899 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4900 * is responsible for handling this call.
4901 * </p>
4902 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004903 * @param info The instance to initialize.
4904 */
4905 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004906 if (mAccessibilityDelegate != null) {
4907 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4908 } else {
4909 onInitializeAccessibilityNodeInfoInternal(info);
4910 }
4911 }
4912
4913 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004914 * Gets the location of this view in screen coordintates.
4915 *
4916 * @param outRect The output location
4917 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004918 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004919 if (mAttachInfo == null) {
4920 return;
4921 }
4922
4923 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004924 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004925
4926 if (!hasIdentityMatrix()) {
4927 getMatrix().mapRect(position);
4928 }
4929
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004930 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004931
4932 ViewParent parent = mParent;
4933 while (parent instanceof View) {
4934 View parentView = (View) parent;
4935
4936 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4937
4938 if (!parentView.hasIdentityMatrix()) {
4939 parentView.getMatrix().mapRect(position);
4940 }
4941
4942 position.offset(parentView.mLeft, parentView.mTop);
4943
4944 parent = parentView.mParent;
4945 }
4946
4947 if (parent instanceof ViewRootImpl) {
4948 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4949 position.offset(0, -viewRootImpl.mCurScrollY);
4950 }
4951
4952 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4953
4954 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4955 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4956 }
4957
4958 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004959 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4960 *
4961 * Note: Called from the default {@link AccessibilityDelegate}.
4962 */
4963 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004964 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004965
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004966 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004967 info.setBoundsInParent(bounds);
4968
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004969 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004970 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004971
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004972 ViewParent parent = getParentForAccessibility();
4973 if (parent instanceof View) {
4974 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004975 }
4976
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004977 if (mID != View.NO_ID) {
4978 View rootView = getRootView();
4979 if (rootView == null) {
4980 rootView = this;
4981 }
4982 View label = rootView.findLabelForView(this, mID);
4983 if (label != null) {
4984 info.setLabeledBy(label);
4985 }
4986 }
4987
4988 if (mLabelForId != View.NO_ID) {
4989 View rootView = getRootView();
4990 if (rootView == null) {
4991 rootView = this;
4992 }
4993 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4994 if (labeled != null) {
4995 info.setLabelFor(labeled);
4996 }
4997 }
4998
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004999 info.setVisibleToUser(isVisibleToUser());
5000
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005001 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005002 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005003 info.setContentDescription(getContentDescription());
5004
5005 info.setEnabled(isEnabled());
5006 info.setClickable(isClickable());
5007 info.setFocusable(isFocusable());
5008 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005009 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005010 info.setSelected(isSelected());
5011 info.setLongClickable(isLongClickable());
5012
5013 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005014 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005015 // we should report as selectable view in an AdapterView.
5016 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5017 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5018
5019 if (isFocusable()) {
5020 if (isFocused()) {
5021 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5022 } else {
5023 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5024 }
5025 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005026
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005027 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005028 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005029 } else {
5030 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5031 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005032
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005033 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005034 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5035 }
5036
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005037 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005038 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5039 }
5040
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005041 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005042 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5043 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5044 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005045 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5046 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005047 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005048 }
5049
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005050 private View findLabelForView(View view, int labeledId) {
5051 if (mMatchLabelForPredicate == null) {
5052 mMatchLabelForPredicate = new MatchLabelForPredicate();
5053 }
5054 mMatchLabelForPredicate.mLabeledId = labeledId;
5055 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5056 }
5057
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005058 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005059 * Computes whether this view is visible to the user. Such a view is
5060 * attached, visible, all its predecessors are visible, it is not clipped
5061 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005062 *
5063 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005064 *
5065 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005066 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005067 protected boolean isVisibleToUser() {
5068 return isVisibleToUser(null);
5069 }
5070
5071 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005072 * Computes whether the given portion of this view is visible to the user.
5073 * Such a view is attached, visible, all its predecessors are visible,
5074 * has an alpha greater than zero, and the specified portion is not
5075 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005076 *
5077 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5078 * <code>null</code>, and the entire view will be tested in this case.
5079 * When <code>true</code> is returned by the function, the actual visible
5080 * region will be stored in this parameter; that is, if boundInView is fully
5081 * contained within the view, no modification will be made, otherwise regions
5082 * outside of the visible area of the view will be clipped.
5083 *
5084 * @return Whether the specified portion of the view is visible on the screen.
5085 *
5086 * @hide
5087 */
5088 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005089 if (mAttachInfo != null) {
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005090 // Attached to invisible window means this view is not visible.
5091 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
5092 return false;
5093 }
5094 // An invisible predecessor or one with alpha zero means
5095 // that this view is not visible to the user.
5096 Object current = this;
5097 while (current instanceof View) {
5098 View view = (View) current;
5099 // We have attach info so this view is attached and there is no
5100 // need to check whether we reach to ViewRootImpl on the way up.
5101 if (view.getAlpha() <= 0 || view.getVisibility() != VISIBLE) {
5102 return false;
5103 }
5104 current = view.mParent;
5105 }
5106 // Check if the view is entirely covered by its predecessors.
Romain Guyf0af1d52012-07-11 18:31:21 -07005107 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5108 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005109 if (!getGlobalVisibleRect(visibleRect, offset)) {
5110 return false;
Guang Zhu0d607fb2012-05-11 19:34:56 -07005111 }
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005112 // Check if the visible portion intersects the rectangle of interest.
5113 if (boundInView != null) {
5114 visibleRect.offset(-offset.x, -offset.y);
5115 return boundInView.intersect(visibleRect);
5116 }
5117 return true;
Romain Guyf0af1d52012-07-11 18:31:21 -07005118 }
Romain Guyf0af1d52012-07-11 18:31:21 -07005119 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005120 }
5121
5122 /**
alanvb72fe7a2012-08-27 16:44:25 -07005123 * Returns the delegate for implementing accessibility support via
5124 * composition. For more details see {@link AccessibilityDelegate}.
5125 *
5126 * @return The delegate, or null if none set.
5127 *
5128 * @hide
5129 */
5130 public AccessibilityDelegate getAccessibilityDelegate() {
5131 return mAccessibilityDelegate;
5132 }
5133
5134 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005135 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005136 * opposed to inheritance. The delegate's primary use is for implementing
5137 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5138 *
5139 * @param delegate The delegate instance.
5140 *
5141 * @see AccessibilityDelegate
5142 */
5143 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5144 mAccessibilityDelegate = delegate;
5145 }
5146
5147 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005148 * Gets the provider for managing a virtual view hierarchy rooted at this View
5149 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5150 * that explore the window content.
5151 * <p>
5152 * If this method returns an instance, this instance is responsible for managing
5153 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5154 * View including the one representing the View itself. Similarly the returned
5155 * instance is responsible for performing accessibility actions on any virtual
5156 * view or the root view itself.
5157 * </p>
5158 * <p>
5159 * If an {@link AccessibilityDelegate} has been specified via calling
5160 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5161 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5162 * is responsible for handling this call.
5163 * </p>
5164 *
5165 * @return The provider.
5166 *
5167 * @see AccessibilityNodeProvider
5168 */
5169 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5170 if (mAccessibilityDelegate != null) {
5171 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5172 } else {
5173 return null;
5174 }
5175 }
5176
5177 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005178 * Gets the unique identifier of this view on the screen for accessibility purposes.
5179 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5180 *
5181 * @return The view accessibility id.
5182 *
5183 * @hide
5184 */
5185 public int getAccessibilityViewId() {
5186 if (mAccessibilityViewId == NO_ID) {
5187 mAccessibilityViewId = sNextAccessibilityViewId++;
5188 }
5189 return mAccessibilityViewId;
5190 }
5191
5192 /**
5193 * Gets the unique identifier of the window in which this View reseides.
5194 *
5195 * @return The window accessibility id.
5196 *
5197 * @hide
5198 */
5199 public int getAccessibilityWindowId() {
5200 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5201 }
5202
5203 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005204 * Gets the {@link View} description. It briefly describes the view and is
5205 * primarily used for accessibility support. Set this property to enable
5206 * better accessibility support for your application. This is especially
5207 * true for views that do not have textual representation (For example,
5208 * ImageButton).
5209 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005210 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005211 *
5212 * @attr ref android.R.styleable#View_contentDescription
5213 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005214 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005215 public CharSequence getContentDescription() {
5216 return mContentDescription;
5217 }
5218
5219 /**
5220 * Sets the {@link View} description. It briefly describes the view and is
5221 * primarily used for accessibility support. Set this property to enable
5222 * better accessibility support for your application. This is especially
5223 * true for views that do not have textual representation (For example,
5224 * ImageButton).
5225 *
5226 * @param contentDescription The content description.
5227 *
5228 * @attr ref android.R.styleable#View_contentDescription
5229 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005230 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005231 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005232 if (mContentDescription == null) {
5233 if (contentDescription == null) {
5234 return;
5235 }
5236 } else if (mContentDescription.equals(contentDescription)) {
5237 return;
5238 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005239 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005240 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5241 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5242 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5243 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005244 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005245 }
5246
5247 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005248 * Gets the id of a view for which this view serves as a label for
5249 * accessibility purposes.
5250 *
5251 * @return The labeled view id.
5252 */
5253 @ViewDebug.ExportedProperty(category = "accessibility")
5254 public int getLabelFor() {
5255 return mLabelForId;
5256 }
5257
5258 /**
5259 * Sets the id of a view for which this view serves as a label for
5260 * accessibility purposes.
5261 *
5262 * @param id The labeled view id.
5263 */
5264 @RemotableViewMethod
5265 public void setLabelFor(int id) {
5266 mLabelForId = id;
5267 if (mLabelForId != View.NO_ID
5268 && mID == View.NO_ID) {
5269 mID = generateViewId();
5270 }
5271 }
5272
5273 /**
Romain Guya2431d02009-04-30 16:30:00 -07005274 * Invoked whenever this view loses focus, either by losing window focus or by losing
5275 * focus within its window. This method can be used to clear any state tied to the
5276 * focus. For instance, if a button is held pressed with the trackball and the window
5277 * loses focus, this method can be used to cancel the press.
5278 *
5279 * Subclasses of View overriding this method should always call super.onFocusLost().
5280 *
5281 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005282 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005283 *
5284 * @hide pending API council approval
5285 */
5286 protected void onFocusLost() {
5287 resetPressedState();
5288 }
5289
5290 private void resetPressedState() {
5291 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5292 return;
5293 }
5294
5295 if (isPressed()) {
5296 setPressed(false);
5297
5298 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005299 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005300 }
5301 }
5302 }
5303
5304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 * Returns true if this view has focus
5306 *
5307 * @return True if this view has focus, false otherwise.
5308 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005309 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005311 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 }
5313
5314 /**
5315 * Find the view in the hierarchy rooted at this view that currently has
5316 * focus.
5317 *
5318 * @return The view that currently has focus, or null if no focused view can
5319 * be found.
5320 */
5321 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005322 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 }
5324
5325 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005326 * Indicates whether this view is one of the set of scrollable containers in
5327 * its window.
5328 *
5329 * @return whether this view is one of the set of scrollable containers in
5330 * its window
5331 *
5332 * @attr ref android.R.styleable#View_isScrollContainer
5333 */
5334 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005335 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005336 }
5337
5338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 * Change whether this view is one of the set of scrollable containers in
5340 * its window. This will be used to determine whether the window can
5341 * resize or must pan when a soft input area is open -- scrollable
5342 * containers allow the window to use resize mode since the container
5343 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005344 *
5345 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 */
5347 public void setScrollContainer(boolean isScrollContainer) {
5348 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005349 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005351 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005353 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005355 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 mAttachInfo.mScrollContainers.remove(this);
5357 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005358 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 }
5360 }
5361
5362 /**
5363 * Returns the quality of the drawing cache.
5364 *
5365 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5366 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5367 *
5368 * @see #setDrawingCacheQuality(int)
5369 * @see #setDrawingCacheEnabled(boolean)
5370 * @see #isDrawingCacheEnabled()
5371 *
5372 * @attr ref android.R.styleable#View_drawingCacheQuality
5373 */
5374 public int getDrawingCacheQuality() {
5375 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5376 }
5377
5378 /**
5379 * Set the drawing cache quality of this view. This value is used only when the
5380 * drawing cache is enabled
5381 *
5382 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5383 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5384 *
5385 * @see #getDrawingCacheQuality()
5386 * @see #setDrawingCacheEnabled(boolean)
5387 * @see #isDrawingCacheEnabled()
5388 *
5389 * @attr ref android.R.styleable#View_drawingCacheQuality
5390 */
5391 public void setDrawingCacheQuality(int quality) {
5392 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5393 }
5394
5395 /**
5396 * Returns whether the screen should remain on, corresponding to the current
5397 * value of {@link #KEEP_SCREEN_ON}.
5398 *
5399 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5400 *
5401 * @see #setKeepScreenOn(boolean)
5402 *
5403 * @attr ref android.R.styleable#View_keepScreenOn
5404 */
5405 public boolean getKeepScreenOn() {
5406 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5407 }
5408
5409 /**
5410 * Controls whether the screen should remain on, modifying the
5411 * value of {@link #KEEP_SCREEN_ON}.
5412 *
5413 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5414 *
5415 * @see #getKeepScreenOn()
5416 *
5417 * @attr ref android.R.styleable#View_keepScreenOn
5418 */
5419 public void setKeepScreenOn(boolean keepScreenOn) {
5420 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5421 }
5422
5423 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005424 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5425 * @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 -08005426 *
5427 * @attr ref android.R.styleable#View_nextFocusLeft
5428 */
5429 public int getNextFocusLeftId() {
5430 return mNextFocusLeftId;
5431 }
5432
5433 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005434 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5435 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5436 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 *
5438 * @attr ref android.R.styleable#View_nextFocusLeft
5439 */
5440 public void setNextFocusLeftId(int nextFocusLeftId) {
5441 mNextFocusLeftId = nextFocusLeftId;
5442 }
5443
5444 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005445 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5446 * @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 -08005447 *
5448 * @attr ref android.R.styleable#View_nextFocusRight
5449 */
5450 public int getNextFocusRightId() {
5451 return mNextFocusRightId;
5452 }
5453
5454 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005455 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5456 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5457 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 *
5459 * @attr ref android.R.styleable#View_nextFocusRight
5460 */
5461 public void setNextFocusRightId(int nextFocusRightId) {
5462 mNextFocusRightId = nextFocusRightId;
5463 }
5464
5465 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005466 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5467 * @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 -08005468 *
5469 * @attr ref android.R.styleable#View_nextFocusUp
5470 */
5471 public int getNextFocusUpId() {
5472 return mNextFocusUpId;
5473 }
5474
5475 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005476 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5477 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5478 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 *
5480 * @attr ref android.R.styleable#View_nextFocusUp
5481 */
5482 public void setNextFocusUpId(int nextFocusUpId) {
5483 mNextFocusUpId = nextFocusUpId;
5484 }
5485
5486 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005487 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5488 * @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 -08005489 *
5490 * @attr ref android.R.styleable#View_nextFocusDown
5491 */
5492 public int getNextFocusDownId() {
5493 return mNextFocusDownId;
5494 }
5495
5496 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005497 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5498 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5499 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 *
5501 * @attr ref android.R.styleable#View_nextFocusDown
5502 */
5503 public void setNextFocusDownId(int nextFocusDownId) {
5504 mNextFocusDownId = nextFocusDownId;
5505 }
5506
5507 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005508 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5509 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5510 *
5511 * @attr ref android.R.styleable#View_nextFocusForward
5512 */
5513 public int getNextFocusForwardId() {
5514 return mNextFocusForwardId;
5515 }
5516
5517 /**
5518 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5519 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5520 * decide automatically.
5521 *
5522 * @attr ref android.R.styleable#View_nextFocusForward
5523 */
5524 public void setNextFocusForwardId(int nextFocusForwardId) {
5525 mNextFocusForwardId = nextFocusForwardId;
5526 }
5527
5528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 * Returns the visibility of this view and all of its ancestors
5530 *
5531 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5532 */
5533 public boolean isShown() {
5534 View current = this;
5535 //noinspection ConstantConditions
5536 do {
5537 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5538 return false;
5539 }
5540 ViewParent parent = current.mParent;
5541 if (parent == null) {
5542 return false; // We are not attached to the view root
5543 }
5544 if (!(parent instanceof View)) {
5545 return true;
5546 }
5547 current = (View) parent;
5548 } while (current != null);
5549
5550 return false;
5551 }
5552
5553 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005554 * Called by the view hierarchy when the content insets for a window have
5555 * changed, to allow it to adjust its content to fit within those windows.
5556 * The content insets tell you the space that the status bar, input method,
5557 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005559 * <p>You do not normally need to deal with this function, since the default
5560 * window decoration given to applications takes care of applying it to the
5561 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5562 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5563 * and your content can be placed under those system elements. You can then
5564 * use this method within your view hierarchy if you have parts of your UI
5565 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005567 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005568 * inset's to the view's padding, consuming that content (modifying the
5569 * insets to be 0), and returning true. This behavior is off by default, but can
5570 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5571 *
5572 * <p>This function's traversal down the hierarchy is depth-first. The same content
5573 * insets object is propagated down the hierarchy, so any changes made to it will
5574 * be seen by all following views (including potentially ones above in
5575 * the hierarchy since this is a depth-first traversal). The first view
5576 * that returns true will abort the entire traversal.
5577 *
5578 * <p>The default implementation works well for a situation where it is
5579 * used with a container that covers the entire window, allowing it to
5580 * apply the appropriate insets to its content on all edges. If you need
5581 * a more complicated layout (such as two different views fitting system
5582 * windows, one on the top of the window, and one on the bottom),
5583 * you can override the method and handle the insets however you would like.
5584 * Note that the insets provided by the framework are always relative to the
5585 * far edges of the window, not accounting for the location of the called view
5586 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005587 * where the layout will place the view, as it is done before layout happens.)
5588 *
5589 * <p>Note: unlike many View methods, there is no dispatch phase to this
5590 * call. If you are overriding it in a ViewGroup and want to allow the
5591 * call to continue to your children, you must be sure to call the super
5592 * implementation.
5593 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005594 * <p>Here is a sample layout that makes use of fitting system windows
5595 * to have controls for a video view placed inside of the window decorations
5596 * that it hides and shows. This can be used with code like the second
5597 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5598 *
5599 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5600 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005601 * @param insets Current content insets of the window. Prior to
5602 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5603 * the insets or else you and Android will be unhappy.
5604 *
5605 * @return Return true if this view applied the insets and it should not
5606 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005607 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005608 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005609 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 */
5611 protected boolean fitSystemWindows(Rect insets) {
5612 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005613 mUserPaddingStart = UNDEFINED_PADDING;
5614 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005615 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5616 || mAttachInfo == null
5617 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5618 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5619 return true;
5620 } else {
5621 internalSetPadding(0, 0, 0, 0);
5622 return false;
5623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 }
5625 return false;
5626 }
5627
5628 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005629 * Sets whether or not this view should account for system screen decorations
5630 * such as the status bar and inset its content; that is, controlling whether
5631 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5632 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005633 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005634 * <p>Note that if you are providing your own implementation of
5635 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5636 * flag to true -- your implementation will be overriding the default
5637 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005638 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005639 * @param fitSystemWindows If true, then the default implementation of
5640 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005641 *
5642 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005643 * @see #getFitsSystemWindows()
5644 * @see #fitSystemWindows(Rect)
5645 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005646 */
5647 public void setFitsSystemWindows(boolean fitSystemWindows) {
5648 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5649 }
5650
5651 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005652 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005653 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5654 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005655 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005656 * @return Returns true if the default implementation of
5657 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005658 *
5659 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005660 * @see #setFitsSystemWindows()
5661 * @see #fitSystemWindows(Rect)
5662 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005663 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005664 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005665 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5666 }
5667
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005668 /** @hide */
5669 public boolean fitsSystemWindows() {
5670 return getFitsSystemWindows();
5671 }
5672
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005673 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005674 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5675 */
5676 public void requestFitSystemWindows() {
5677 if (mParent != null) {
5678 mParent.requestFitSystemWindows();
5679 }
5680 }
5681
5682 /**
5683 * For use by PhoneWindow to make its own system window fitting optional.
5684 * @hide
5685 */
5686 public void makeOptionalFitsSystemWindows() {
5687 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5688 }
5689
5690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 * Returns the visibility status for this view.
5692 *
5693 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5694 * @attr ref android.R.styleable#View_visibility
5695 */
5696 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005697 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5698 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5699 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 })
5701 public int getVisibility() {
5702 return mViewFlags & VISIBILITY_MASK;
5703 }
5704
5705 /**
5706 * Set the enabled state of this view.
5707 *
5708 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5709 * @attr ref android.R.styleable#View_visibility
5710 */
5711 @RemotableViewMethod
5712 public void setVisibility(int visibility) {
5713 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005714 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 }
5716
5717 /**
5718 * Returns the enabled status for this view. The interpretation of the
5719 * enabled state varies by subclass.
5720 *
5721 * @return True if this view is enabled, false otherwise.
5722 */
5723 @ViewDebug.ExportedProperty
5724 public boolean isEnabled() {
5725 return (mViewFlags & ENABLED_MASK) == ENABLED;
5726 }
5727
5728 /**
5729 * Set the enabled state of this view. The interpretation of the enabled
5730 * state varies by subclass.
5731 *
5732 * @param enabled True if this view is enabled, false otherwise.
5733 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005734 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005736 if (enabled == isEnabled()) return;
5737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5739
5740 /*
5741 * The View most likely has to change its appearance, so refresh
5742 * the drawable state.
5743 */
5744 refreshDrawableState();
5745
5746 // Invalidate too, since the default behavior for views is to be
5747 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005748 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 }
5750
5751 /**
5752 * Set whether this view can receive the focus.
5753 *
5754 * Setting this to false will also ensure that this view is not focusable
5755 * in touch mode.
5756 *
5757 * @param focusable If true, this view can receive the focus.
5758 *
5759 * @see #setFocusableInTouchMode(boolean)
5760 * @attr ref android.R.styleable#View_focusable
5761 */
5762 public void setFocusable(boolean focusable) {
5763 if (!focusable) {
5764 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5765 }
5766 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5767 }
5768
5769 /**
5770 * Set whether this view can receive focus while in touch mode.
5771 *
5772 * Setting this to true will also ensure that this view is focusable.
5773 *
5774 * @param focusableInTouchMode If true, this view can receive the focus while
5775 * in touch mode.
5776 *
5777 * @see #setFocusable(boolean)
5778 * @attr ref android.R.styleable#View_focusableInTouchMode
5779 */
5780 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5781 // Focusable in touch mode should always be set before the focusable flag
5782 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5783 // which, in touch mode, will not successfully request focus on this view
5784 // because the focusable in touch mode flag is not set
5785 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5786 if (focusableInTouchMode) {
5787 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5788 }
5789 }
5790
5791 /**
5792 * Set whether this view should have sound effects enabled for events such as
5793 * clicking and touching.
5794 *
5795 * <p>You may wish to disable sound effects for a view if you already play sounds,
5796 * for instance, a dial key that plays dtmf tones.
5797 *
5798 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5799 * @see #isSoundEffectsEnabled()
5800 * @see #playSoundEffect(int)
5801 * @attr ref android.R.styleable#View_soundEffectsEnabled
5802 */
5803 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5804 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5805 }
5806
5807 /**
5808 * @return whether this view should have sound effects enabled for events such as
5809 * clicking and touching.
5810 *
5811 * @see #setSoundEffectsEnabled(boolean)
5812 * @see #playSoundEffect(int)
5813 * @attr ref android.R.styleable#View_soundEffectsEnabled
5814 */
5815 @ViewDebug.ExportedProperty
5816 public boolean isSoundEffectsEnabled() {
5817 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5818 }
5819
5820 /**
5821 * Set whether this view should have haptic feedback for events such as
5822 * long presses.
5823 *
5824 * <p>You may wish to disable haptic feedback if your view already controls
5825 * its own haptic feedback.
5826 *
5827 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5828 * @see #isHapticFeedbackEnabled()
5829 * @see #performHapticFeedback(int)
5830 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5831 */
5832 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5833 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5834 }
5835
5836 /**
5837 * @return whether this view should have haptic feedback enabled for events
5838 * long presses.
5839 *
5840 * @see #setHapticFeedbackEnabled(boolean)
5841 * @see #performHapticFeedback(int)
5842 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5843 */
5844 @ViewDebug.ExportedProperty
5845 public boolean isHapticFeedbackEnabled() {
5846 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5847 }
5848
5849 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005850 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005851 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005852 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5853 * {@link #LAYOUT_DIRECTION_RTL},
5854 * {@link #LAYOUT_DIRECTION_INHERIT} or
5855 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005856 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005857 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005858 *
5859 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005860 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005861 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005862 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5863 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5864 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5865 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005866 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005867 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005868 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005869 }
5870
5871 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005872 * Set the layout direction for this view. This will propagate a reset of layout direction
5873 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005874 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005875 * @param layoutDirection the layout direction to set. Should be one of:
5876 *
5877 * {@link #LAYOUT_DIRECTION_LTR},
5878 * {@link #LAYOUT_DIRECTION_RTL},
5879 * {@link #LAYOUT_DIRECTION_INHERIT},
5880 * {@link #LAYOUT_DIRECTION_LOCALE}.
5881 *
5882 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5883 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5884 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005885 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005886 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005887 */
5888 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005889 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005890 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005891 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005892 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005893 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005894 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005895 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005896 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005897 // We need to resolve all RTL properties as they all depend on layout direction
5898 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005899 requestLayout();
5900 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005901 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005902 }
5903
5904 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005905 * Returns the resolved layout direction for this view.
5906 *
5907 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005908 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005909 *
5910 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5911 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005912 *
5913 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005914 */
5915 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005916 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5917 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005918 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005919 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005920 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5921 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005922 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005923 return LAYOUT_DIRECTION_LTR;
5924 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005925 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5926 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005927 }
5928
5929 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005930 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5931 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005932 *
5933 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005934 *
5935 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005936 */
5937 @ViewDebug.ExportedProperty(category = "layout")
5938 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005939 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005940 }
5941
5942 /**
Adam Powell539ee872012-02-03 19:00:49 -08005943 * Indicates whether the view is currently tracking transient state that the
5944 * app should not need to concern itself with saving and restoring, but that
5945 * the framework should take special note to preserve when possible.
5946 *
Adam Powell785c4472012-05-02 21:25:39 -07005947 * <p>A view with transient state cannot be trivially rebound from an external
5948 * data source, such as an adapter binding item views in a list. This may be
5949 * because the view is performing an animation, tracking user selection
5950 * of content, or similar.</p>
5951 *
Adam Powell539ee872012-02-03 19:00:49 -08005952 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005953 */
5954 @ViewDebug.ExportedProperty(category = "layout")
5955 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005956 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005957 }
5958
5959 /**
5960 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005961 * framework should attempt to preserve when possible. This flag is reference counted,
5962 * so every call to setHasTransientState(true) should be paired with a later call
5963 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005964 *
Adam Powell785c4472012-05-02 21:25:39 -07005965 * <p>A view with transient state cannot be trivially rebound from an external
5966 * data source, such as an adapter binding item views in a list. This may be
5967 * because the view is performing an animation, tracking user selection
5968 * of content, or similar.</p>
5969 *
Adam Powell539ee872012-02-03 19:00:49 -08005970 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005971 */
5972 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005973 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5974 mTransientStateCount - 1;
5975 if (mTransientStateCount < 0) {
5976 mTransientStateCount = 0;
5977 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5978 "unmatched pair of setHasTransientState calls");
5979 }
5980 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005981 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005982 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005983 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5984 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005985 if (mParent != null) {
5986 try {
5987 mParent.childHasTransientStateChanged(this, hasTransientState);
5988 } catch (AbstractMethodError e) {
5989 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5990 " does not fully implement ViewParent", e);
5991 }
Adam Powell539ee872012-02-03 19:00:49 -08005992 }
5993 }
5994 }
5995
5996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 * If this view doesn't do any drawing on its own, set this flag to
5998 * allow further optimizations. By default, this flag is not set on
5999 * View, but could be set on some View subclasses such as ViewGroup.
6000 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006001 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
6002 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 *
6004 * @param willNotDraw whether or not this View draw on its own
6005 */
6006 public void setWillNotDraw(boolean willNotDraw) {
6007 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
6008 }
6009
6010 /**
6011 * Returns whether or not this View draws on its own.
6012 *
6013 * @return true if this view has nothing to draw, false otherwise
6014 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006015 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 public boolean willNotDraw() {
6017 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6018 }
6019
6020 /**
6021 * When a View's drawing cache is enabled, drawing is redirected to an
6022 * offscreen bitmap. Some views, like an ImageView, must be able to
6023 * bypass this mechanism if they already draw a single bitmap, to avoid
6024 * unnecessary usage of the memory.
6025 *
6026 * @param willNotCacheDrawing true if this view does not cache its
6027 * drawing, false otherwise
6028 */
6029 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6030 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6031 }
6032
6033 /**
6034 * Returns whether or not this View can cache its drawing or not.
6035 *
6036 * @return true if this view does not cache its drawing, false otherwise
6037 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006038 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 public boolean willNotCacheDrawing() {
6040 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6041 }
6042
6043 /**
6044 * Indicates whether this view reacts to click events or not.
6045 *
6046 * @return true if the view is clickable, false otherwise
6047 *
6048 * @see #setClickable(boolean)
6049 * @attr ref android.R.styleable#View_clickable
6050 */
6051 @ViewDebug.ExportedProperty
6052 public boolean isClickable() {
6053 return (mViewFlags & CLICKABLE) == CLICKABLE;
6054 }
6055
6056 /**
6057 * Enables or disables click events for this view. When a view
6058 * is clickable it will change its state to "pressed" on every click.
6059 * Subclasses should set the view clickable to visually react to
6060 * user's clicks.
6061 *
6062 * @param clickable true to make the view clickable, false otherwise
6063 *
6064 * @see #isClickable()
6065 * @attr ref android.R.styleable#View_clickable
6066 */
6067 public void setClickable(boolean clickable) {
6068 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6069 }
6070
6071 /**
6072 * Indicates whether this view reacts to long click events or not.
6073 *
6074 * @return true if the view is long clickable, false otherwise
6075 *
6076 * @see #setLongClickable(boolean)
6077 * @attr ref android.R.styleable#View_longClickable
6078 */
6079 public boolean isLongClickable() {
6080 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6081 }
6082
6083 /**
6084 * Enables or disables long click events for this view. When a view is long
6085 * clickable it reacts to the user holding down the button for a longer
6086 * duration than a tap. This event can either launch the listener or a
6087 * context menu.
6088 *
6089 * @param longClickable true to make the view long clickable, false otherwise
6090 * @see #isLongClickable()
6091 * @attr ref android.R.styleable#View_longClickable
6092 */
6093 public void setLongClickable(boolean longClickable) {
6094 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6095 }
6096
6097 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006098 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 *
6100 * @see #isClickable()
6101 * @see #setClickable(boolean)
6102 *
6103 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6104 * the View's internal state from a previously set "pressed" state.
6105 */
6106 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006107 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006110 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006112 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006114
6115 if (needsRefresh) {
6116 refreshDrawableState();
6117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 dispatchSetPressed(pressed);
6119 }
6120
6121 /**
6122 * Dispatch setPressed to all of this View's children.
6123 *
6124 * @see #setPressed(boolean)
6125 *
6126 * @param pressed The new pressed state
6127 */
6128 protected void dispatchSetPressed(boolean pressed) {
6129 }
6130
6131 /**
6132 * Indicates whether the view is currently in pressed state. Unless
6133 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6134 * the pressed state.
6135 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006136 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137 * @see #isClickable()
6138 * @see #setClickable(boolean)
6139 *
6140 * @return true if the view is currently pressed, false otherwise
6141 */
6142 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006143 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 }
6145
6146 /**
6147 * Indicates whether this view will save its state (that is,
6148 * whether its {@link #onSaveInstanceState} method will be called).
6149 *
6150 * @return Returns true if the view state saving is enabled, else false.
6151 *
6152 * @see #setSaveEnabled(boolean)
6153 * @attr ref android.R.styleable#View_saveEnabled
6154 */
6155 public boolean isSaveEnabled() {
6156 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6157 }
6158
6159 /**
6160 * Controls whether the saving of this view's state is
6161 * enabled (that is, whether its {@link #onSaveInstanceState} method
6162 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006163 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 * for its state to be saved. This flag can only disable the
6165 * saving of this view; any child views may still have their state saved.
6166 *
6167 * @param enabled Set to false to <em>disable</em> state saving, or true
6168 * (the default) to allow it.
6169 *
6170 * @see #isSaveEnabled()
6171 * @see #setId(int)
6172 * @see #onSaveInstanceState()
6173 * @attr ref android.R.styleable#View_saveEnabled
6174 */
6175 public void setSaveEnabled(boolean enabled) {
6176 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6177 }
6178
Jeff Brown85a31762010-09-01 17:01:00 -07006179 /**
6180 * Gets whether the framework should discard touches when the view's
6181 * window is obscured by another visible window.
6182 * Refer to the {@link View} security documentation for more details.
6183 *
6184 * @return True if touch filtering is enabled.
6185 *
6186 * @see #setFilterTouchesWhenObscured(boolean)
6187 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6188 */
6189 @ViewDebug.ExportedProperty
6190 public boolean getFilterTouchesWhenObscured() {
6191 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6192 }
6193
6194 /**
6195 * Sets whether the framework should discard touches when the view's
6196 * window is obscured by another visible window.
6197 * Refer to the {@link View} security documentation for more details.
6198 *
6199 * @param enabled True if touch filtering should be enabled.
6200 *
6201 * @see #getFilterTouchesWhenObscured
6202 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6203 */
6204 public void setFilterTouchesWhenObscured(boolean enabled) {
6205 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6206 FILTER_TOUCHES_WHEN_OBSCURED);
6207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208
6209 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006210 * Indicates whether the entire hierarchy under this view will save its
6211 * state when a state saving traversal occurs from its parent. The default
6212 * is true; if false, these views will not be saved unless
6213 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6214 *
6215 * @return Returns true if the view state saving from parent is enabled, else false.
6216 *
6217 * @see #setSaveFromParentEnabled(boolean)
6218 */
6219 public boolean isSaveFromParentEnabled() {
6220 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6221 }
6222
6223 /**
6224 * Controls whether the entire hierarchy under this view will save its
6225 * state when a state saving traversal occurs from its parent. The default
6226 * is true; if false, these views will not be saved unless
6227 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6228 *
6229 * @param enabled Set to false to <em>disable</em> state saving, or true
6230 * (the default) to allow it.
6231 *
6232 * @see #isSaveFromParentEnabled()
6233 * @see #setId(int)
6234 * @see #onSaveInstanceState()
6235 */
6236 public void setSaveFromParentEnabled(boolean enabled) {
6237 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6238 }
6239
6240
6241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 * Returns whether this View is able to take focus.
6243 *
6244 * @return True if this view can take focus, or false otherwise.
6245 * @attr ref android.R.styleable#View_focusable
6246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006247 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 public final boolean isFocusable() {
6249 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6250 }
6251
6252 /**
6253 * When a view is focusable, it may not want to take focus when in touch mode.
6254 * For example, a button would like focus when the user is navigating via a D-pad
6255 * so that the user can click on it, but once the user starts touching the screen,
6256 * the button shouldn't take focus
6257 * @return Whether the view is focusable in touch mode.
6258 * @attr ref android.R.styleable#View_focusableInTouchMode
6259 */
6260 @ViewDebug.ExportedProperty
6261 public final boolean isFocusableInTouchMode() {
6262 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6263 }
6264
6265 /**
6266 * Find the nearest view in the specified direction that can take focus.
6267 * This does not actually give focus to that view.
6268 *
6269 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6270 *
6271 * @return The nearest focusable in the specified direction, or null if none
6272 * can be found.
6273 */
6274 public View focusSearch(int direction) {
6275 if (mParent != null) {
6276 return mParent.focusSearch(this, direction);
6277 } else {
6278 return null;
6279 }
6280 }
6281
6282 /**
6283 * This method is the last chance for the focused view and its ancestors to
6284 * respond to an arrow key. This is called when the focused view did not
6285 * consume the key internally, nor could the view system find a new view in
6286 * the requested direction to give focus to.
6287 *
6288 * @param focused The currently focused view.
6289 * @param direction The direction focus wants to move. One of FOCUS_UP,
6290 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6291 * @return True if the this view consumed this unhandled move.
6292 */
6293 public boolean dispatchUnhandledMove(View focused, int direction) {
6294 return false;
6295 }
6296
6297 /**
6298 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006299 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006301 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6302 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 * @return The user specified next view, or null if there is none.
6304 */
6305 View findUserSetNextFocus(View root, int direction) {
6306 switch (direction) {
6307 case FOCUS_LEFT:
6308 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006309 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 case FOCUS_RIGHT:
6311 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006312 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 case FOCUS_UP:
6314 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006315 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 case FOCUS_DOWN:
6317 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006318 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006319 case FOCUS_FORWARD:
6320 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006321 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006322 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006323 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006324 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006325 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006326 @Override
6327 public boolean apply(View t) {
6328 return t.mNextFocusForwardId == id;
6329 }
6330 });
6331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 }
6333 return null;
6334 }
6335
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006336 private View findViewInsideOutShouldExist(View root, int id) {
6337 if (mMatchIdPredicate == null) {
6338 mMatchIdPredicate = new MatchIdPredicate();
6339 }
6340 mMatchIdPredicate.mId = id;
6341 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006343 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 }
6345 return result;
6346 }
6347
6348 /**
6349 * Find and return all focusable views that are descendants of this view,
6350 * possibly including this view if it is focusable itself.
6351 *
6352 * @param direction The direction of the focus
6353 * @return A list of focusable views
6354 */
6355 public ArrayList<View> getFocusables(int direction) {
6356 ArrayList<View> result = new ArrayList<View>(24);
6357 addFocusables(result, direction);
6358 return result;
6359 }
6360
6361 /**
6362 * Add any focusable views that are descendants of this view (possibly
6363 * including this view if it is focusable itself) to views. If we are in touch mode,
6364 * only add views that are also focusable in touch mode.
6365 *
6366 * @param views Focusable views found so far
6367 * @param direction The direction of the focus
6368 */
6369 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006370 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372
svetoslavganov75986cf2009-05-14 22:28:01 -07006373 /**
6374 * Adds any focusable views that are descendants of this view (possibly
6375 * including this view if it is focusable itself) to views. This method
6376 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006377 * only views focusable in touch mode if we are in touch mode or
6378 * only views that can take accessibility focus if accessibility is enabeld
6379 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006380 *
6381 * @param views Focusable views found so far or null if all we are interested is
6382 * the number of focusables.
6383 * @param direction The direction of the focus.
6384 * @param focusableMode The type of focusables to be added.
6385 *
6386 * @see #FOCUSABLES_ALL
6387 * @see #FOCUSABLES_TOUCH_MODE
6388 */
6389 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006390 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006391 return;
6392 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006393 if (!isFocusable()) {
6394 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006395 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006396 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6397 && isInTouchMode() && !isFocusableInTouchMode()) {
6398 return;
6399 }
6400 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 }
6402
6403 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006404 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006405 * The search is performed by either the text that the View renders or the content
6406 * description that describes the view for accessibility purposes and the view does
6407 * not render or both. Clients can specify how the search is to be performed via
6408 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6409 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006410 *
6411 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006412 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006413 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006414 * @see #FIND_VIEWS_WITH_TEXT
6415 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6416 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006417 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006418 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006419 if (getAccessibilityNodeProvider() != null) {
6420 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6421 outViews.add(this);
6422 }
6423 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006424 && (searched != null && searched.length() > 0)
6425 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006426 String searchedLowerCase = searched.toString().toLowerCase();
6427 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6428 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6429 outViews.add(this);
6430 }
6431 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006432 }
6433
6434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435 * Find and return all touchable views that are descendants of this view,
6436 * possibly including this view if it is touchable itself.
6437 *
6438 * @return A list of touchable views
6439 */
6440 public ArrayList<View> getTouchables() {
6441 ArrayList<View> result = new ArrayList<View>();
6442 addTouchables(result);
6443 return result;
6444 }
6445
6446 /**
6447 * Add any touchable views that are descendants of this view (possibly
6448 * including this view if it is touchable itself) to views.
6449 *
6450 * @param views Touchable views found so far
6451 */
6452 public void addTouchables(ArrayList<View> views) {
6453 final int viewFlags = mViewFlags;
6454
6455 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6456 && (viewFlags & ENABLED_MASK) == ENABLED) {
6457 views.add(this);
6458 }
6459 }
6460
6461 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006462 * Returns whether this View is accessibility focused.
6463 *
6464 * @return True if this View is accessibility focused.
6465 */
6466 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006467 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006468 }
6469
6470 /**
6471 * Call this to try to give accessibility focus to this view.
6472 *
6473 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6474 * returns false or the view is no visible or the view already has accessibility
6475 * focus.
6476 *
6477 * See also {@link #focusSearch(int)}, which is what you call to say that you
6478 * have focus, and you want your parent to look for the next one.
6479 *
6480 * @return Whether this view actually took accessibility focus.
6481 *
6482 * @hide
6483 */
6484 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006485 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6486 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006487 return false;
6488 }
6489 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6490 return false;
6491 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006492 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6493 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006494 ViewRootImpl viewRootImpl = getViewRootImpl();
6495 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006496 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006497 }
6498 invalidate();
6499 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6500 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006501 return true;
6502 }
6503 return false;
6504 }
6505
6506 /**
6507 * Call this to try to clear accessibility focus of this view.
6508 *
6509 * See also {@link #focusSearch(int)}, which is what you call to say that you
6510 * have focus, and you want your parent to look for the next one.
6511 *
6512 * @hide
6513 */
6514 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006515 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6516 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006517 invalidate();
6518 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6519 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006520 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006521 // Clear the global reference of accessibility focus if this
6522 // view or any of its descendants had accessibility focus.
6523 ViewRootImpl viewRootImpl = getViewRootImpl();
6524 if (viewRootImpl != null) {
6525 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6526 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006527 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006528 }
6529 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006530 }
6531
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006532 private void sendAccessibilityHoverEvent(int eventType) {
6533 // Since we are not delivering to a client accessibility events from not
6534 // important views (unless the clinet request that) we need to fire the
6535 // event from the deepest view exposed to the client. As a consequence if
6536 // the user crosses a not exposed view the client will see enter and exit
6537 // of the exposed predecessor followed by and enter and exit of that same
6538 // predecessor when entering and exiting the not exposed descendant. This
6539 // is fine since the client has a clear idea which view is hovered at the
6540 // price of a couple more events being sent. This is a simple and
6541 // working solution.
6542 View source = this;
6543 while (true) {
6544 if (source.includeForAccessibility()) {
6545 source.sendAccessibilityEvent(eventType);
6546 return;
6547 }
6548 ViewParent parent = source.getParent();
6549 if (parent instanceof View) {
6550 source = (View) parent;
6551 } else {
6552 return;
6553 }
6554 }
6555 }
6556
Svetoslav Ganov42138042012-03-20 11:51:39 -07006557 /**
6558 * Clears accessibility focus without calling any callback methods
6559 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6560 * is used for clearing accessibility focus when giving this focus to
6561 * another view.
6562 */
6563 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006564 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6565 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006566 invalidate();
6567 }
6568 }
6569
6570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006571 * Call this to try to give focus to a specific view or to one of its
6572 * descendants.
6573 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006574 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6575 * false), or if it is focusable and it is not focusable in touch mode
6576 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006578 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 * have focus, and you want your parent to look for the next one.
6580 *
6581 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6582 * {@link #FOCUS_DOWN} and <code>null</code>.
6583 *
6584 * @return Whether this view or one of its descendants actually took focus.
6585 */
6586 public final boolean requestFocus() {
6587 return requestFocus(View.FOCUS_DOWN);
6588 }
6589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 /**
6591 * Call this to try to give focus to a specific view or to one of its
6592 * descendants and give it a hint about what direction focus is heading.
6593 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006594 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6595 * false), or if it is focusable and it is not focusable in touch mode
6596 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006598 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 * have focus, and you want your parent to look for the next one.
6600 *
6601 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6602 * <code>null</code> set for the previously focused rectangle.
6603 *
6604 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6605 * @return Whether this view or one of its descendants actually took focus.
6606 */
6607 public final boolean requestFocus(int direction) {
6608 return requestFocus(direction, null);
6609 }
6610
6611 /**
6612 * Call this to try to give focus to a specific view or to one of its descendants
6613 * and give it hints about the direction and a specific rectangle that the focus
6614 * is coming from. The rectangle can help give larger views a finer grained hint
6615 * about where focus is coming from, and therefore, where to show selection, or
6616 * forward focus change internally.
6617 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006618 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6619 * false), or if it is focusable and it is not focusable in touch mode
6620 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 *
6622 * A View will not take focus if it is not visible.
6623 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006624 * A View will not take focus if one of its parents has
6625 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6626 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006628 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 * have focus, and you want your parent to look for the next one.
6630 *
6631 * You may wish to override this method if your custom {@link View} has an internal
6632 * {@link View} that it wishes to forward the request to.
6633 *
6634 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6635 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6636 * to give a finer grained hint about where focus is coming from. May be null
6637 * if there is no hint.
6638 * @return Whether this view or one of its descendants actually took focus.
6639 */
6640 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006641 return requestFocusNoSearch(direction, previouslyFocusedRect);
6642 }
6643
6644 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 // need to be focusable
6646 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6647 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6648 return false;
6649 }
6650
6651 // need to be focusable in touch mode if in touch mode
6652 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006653 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6654 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006655 }
6656
6657 // need to not have any parents blocking us
6658 if (hasAncestorThatBlocksDescendantFocus()) {
6659 return false;
6660 }
6661
6662 handleFocusGainInternal(direction, previouslyFocusedRect);
6663 return true;
6664 }
6665
6666 /**
6667 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6668 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6669 * touch mode to request focus when they are touched.
6670 *
6671 * @return Whether this view or one of its descendants actually took focus.
6672 *
6673 * @see #isInTouchMode()
6674 *
6675 */
6676 public final boolean requestFocusFromTouch() {
6677 // Leave touch mode if we need to
6678 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006679 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006680 if (viewRoot != null) {
6681 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 }
6683 }
6684 return requestFocus(View.FOCUS_DOWN);
6685 }
6686
6687 /**
6688 * @return Whether any ancestor of this view blocks descendant focus.
6689 */
6690 private boolean hasAncestorThatBlocksDescendantFocus() {
6691 ViewParent ancestor = mParent;
6692 while (ancestor instanceof ViewGroup) {
6693 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6694 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6695 return true;
6696 } else {
6697 ancestor = vgAncestor.getParent();
6698 }
6699 }
6700 return false;
6701 }
6702
6703 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006704 * Gets the mode for determining whether this View is important for accessibility
6705 * which is if it fires accessibility events and if it is reported to
6706 * accessibility services that query the screen.
6707 *
6708 * @return The mode for determining whether a View is important for accessibility.
6709 *
6710 * @attr ref android.R.styleable#View_importantForAccessibility
6711 *
6712 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6713 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6714 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6715 */
6716 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006717 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6718 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6719 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006720 })
6721 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006722 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6723 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006724 }
6725
6726 /**
6727 * Sets how to determine whether this view is important for accessibility
6728 * which is if it fires accessibility events and if it is reported to
6729 * accessibility services that query the screen.
6730 *
6731 * @param mode How to determine whether this view is important for accessibility.
6732 *
6733 * @attr ref android.R.styleable#View_importantForAccessibility
6734 *
6735 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6736 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6737 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6738 */
6739 public void setImportantForAccessibility(int mode) {
6740 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006741 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6742 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6743 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006744 notifyAccessibilityStateChanged();
6745 }
6746 }
6747
6748 /**
6749 * Gets whether this view should be exposed for accessibility.
6750 *
6751 * @return Whether the view is exposed for accessibility.
6752 *
6753 * @hide
6754 */
6755 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006756 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6757 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006758 switch (mode) {
6759 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6760 return true;
6761 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6762 return false;
6763 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006764 return isActionableForAccessibility() || hasListenersForAccessibility()
6765 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006766 default:
6767 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6768 + mode);
6769 }
6770 }
6771
6772 /**
6773 * Gets the parent for accessibility purposes. Note that the parent for
6774 * accessibility is not necessary the immediate parent. It is the first
6775 * predecessor that is important for accessibility.
6776 *
6777 * @return The parent for accessibility purposes.
6778 */
6779 public ViewParent getParentForAccessibility() {
6780 if (mParent instanceof View) {
6781 View parentView = (View) mParent;
6782 if (parentView.includeForAccessibility()) {
6783 return mParent;
6784 } else {
6785 return mParent.getParentForAccessibility();
6786 }
6787 }
6788 return null;
6789 }
6790
6791 /**
6792 * Adds the children of a given View for accessibility. Since some Views are
6793 * not important for accessibility the children for accessibility are not
6794 * necessarily direct children of the riew, rather they are the first level of
6795 * descendants important for accessibility.
6796 *
6797 * @param children The list of children for accessibility.
6798 */
6799 public void addChildrenForAccessibility(ArrayList<View> children) {
6800 if (includeForAccessibility()) {
6801 children.add(this);
6802 }
6803 }
6804
6805 /**
6806 * Whether to regard this view for accessibility. A view is regarded for
6807 * accessibility if it is important for accessibility or the querying
6808 * accessibility service has explicitly requested that view not
6809 * important for accessibility are regarded.
6810 *
6811 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006812 *
6813 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006814 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006815 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006816 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006817 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006818 }
6819 return false;
6820 }
6821
6822 /**
6823 * Returns whether the View is considered actionable from
6824 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006825 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006826 *
6827 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006828 *
6829 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006830 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006831 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006832 return (isClickable() || isLongClickable() || isFocusable());
6833 }
6834
6835 /**
6836 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006837 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006838 *
6839 * @return True if the view is actionable for accessibility.
6840 */
6841 private boolean hasListenersForAccessibility() {
6842 ListenerInfo info = getListenerInfo();
6843 return mTouchDelegate != null || info.mOnKeyListener != null
6844 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6845 || info.mOnHoverListener != null || info.mOnDragListener != null;
6846 }
6847
6848 /**
6849 * Notifies accessibility services that some view's important for
6850 * accessibility state has changed. Note that such notifications
6851 * are made at most once every
6852 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6853 * to avoid unnecessary load to the system. Also once a view has
6854 * made a notifucation this method is a NOP until the notification has
6855 * been sent to clients.
6856 *
6857 * @hide
6858 *
6859 * TODO: Makse sure this method is called for any view state change
6860 * that is interesting for accessilility purposes.
6861 */
6862 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006863 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6864 return;
6865 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006866 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6867 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006868 if (mParent != null) {
6869 mParent.childAccessibilityStateChanged(this);
6870 }
6871 }
6872 }
6873
6874 /**
6875 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006876 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006877 *
6878 * @hide
6879 */
6880 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006881 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006882 }
6883
6884 /**
6885 * Performs the specified accessibility action on the view. For
6886 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006887 * <p>
6888 * If an {@link AccessibilityDelegate} has been specified via calling
6889 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6890 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6891 * is responsible for handling this call.
6892 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006893 *
6894 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006895 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006896 * @return Whether the action was performed.
6897 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006898 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006899 if (mAccessibilityDelegate != null) {
6900 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6901 } else {
6902 return performAccessibilityActionInternal(action, arguments);
6903 }
6904 }
6905
6906 /**
6907 * @see #performAccessibilityAction(int, Bundle)
6908 *
6909 * Note: Called from the default {@link AccessibilityDelegate}.
6910 */
6911 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006912 switch (action) {
6913 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006914 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006915 performClick();
6916 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006917 }
6918 } break;
6919 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6920 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006921 performLongClick();
6922 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006923 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006924 } break;
6925 case AccessibilityNodeInfo.ACTION_FOCUS: {
6926 if (!hasFocus()) {
6927 // Get out of touch mode since accessibility
6928 // wants to move focus around.
6929 getViewRootImpl().ensureTouchMode(false);
6930 return requestFocus();
6931 }
6932 } break;
6933 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6934 if (hasFocus()) {
6935 clearFocus();
6936 return !isFocused();
6937 }
6938 } break;
6939 case AccessibilityNodeInfo.ACTION_SELECT: {
6940 if (!isSelected()) {
6941 setSelected(true);
6942 return isSelected();
6943 }
6944 } break;
6945 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6946 if (isSelected()) {
6947 setSelected(false);
6948 return !isSelected();
6949 }
6950 } break;
6951 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006952 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006953 return requestAccessibilityFocus();
6954 }
6955 } break;
6956 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6957 if (isAccessibilityFocused()) {
6958 clearAccessibilityFocus();
6959 return true;
6960 }
6961 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006962 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6963 if (arguments != null) {
6964 final int granularity = arguments.getInt(
6965 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6966 return nextAtGranularity(granularity);
6967 }
6968 } break;
6969 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6970 if (arguments != null) {
6971 final int granularity = arguments.getInt(
6972 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6973 return previousAtGranularity(granularity);
6974 }
6975 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006976 }
6977 return false;
6978 }
6979
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006980 private boolean nextAtGranularity(int granularity) {
6981 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006982 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006983 return false;
6984 }
6985 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6986 if (iterator == null) {
6987 return false;
6988 }
6989 final int current = getAccessibilityCursorPosition();
6990 final int[] range = iterator.following(current);
6991 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006992 return false;
6993 }
6994 final int start = range[0];
6995 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006996 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006997 sendViewTextTraversedAtGranularityEvent(
6998 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6999 granularity, start, end);
7000 return true;
7001 }
7002
7003 private boolean previousAtGranularity(int granularity) {
7004 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007005 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007006 return false;
7007 }
7008 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7009 if (iterator == null) {
7010 return false;
7011 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007012 int current = getAccessibilityCursorPosition();
7013 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7014 current = text.length();
7015 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7016 // When traversing by character we always put the cursor after the character
7017 // to ease edit and have to compensate before asking the for previous segment.
7018 current--;
7019 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007020 final int[] range = iterator.preceding(current);
7021 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007022 return false;
7023 }
7024 final int start = range[0];
7025 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007026 // Always put the cursor after the character to ease edit.
7027 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7028 setAccessibilityCursorPosition(end);
7029 } else {
7030 setAccessibilityCursorPosition(start);
7031 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007032 sendViewTextTraversedAtGranularityEvent(
7033 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7034 granularity, start, end);
7035 return true;
7036 }
7037
7038 /**
7039 * Gets the text reported for accessibility purposes.
7040 *
7041 * @return The accessibility text.
7042 *
7043 * @hide
7044 */
7045 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007046 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007047 }
7048
7049 /**
7050 * @hide
7051 */
7052 public int getAccessibilityCursorPosition() {
7053 return mAccessibilityCursorPosition;
7054 }
7055
7056 /**
7057 * @hide
7058 */
7059 public void setAccessibilityCursorPosition(int position) {
7060 mAccessibilityCursorPosition = position;
7061 }
7062
7063 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7064 int fromIndex, int toIndex) {
7065 if (mParent == null) {
7066 return;
7067 }
7068 AccessibilityEvent event = AccessibilityEvent.obtain(
7069 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7070 onInitializeAccessibilityEvent(event);
7071 onPopulateAccessibilityEvent(event);
7072 event.setFromIndex(fromIndex);
7073 event.setToIndex(toIndex);
7074 event.setAction(action);
7075 event.setMovementGranularity(granularity);
7076 mParent.requestSendAccessibilityEvent(this, event);
7077 }
7078
7079 /**
7080 * @hide
7081 */
7082 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7083 switch (granularity) {
7084 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7085 CharSequence text = getIterableTextForAccessibility();
7086 if (text != null && text.length() > 0) {
7087 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007088 CharacterTextSegmentIterator.getInstance(
7089 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007090 iterator.initialize(text.toString());
7091 return iterator;
7092 }
7093 } break;
7094 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7095 CharSequence text = getIterableTextForAccessibility();
7096 if (text != null && text.length() > 0) {
7097 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007098 WordTextSegmentIterator.getInstance(
7099 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007100 iterator.initialize(text.toString());
7101 return iterator;
7102 }
7103 } break;
7104 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7105 CharSequence text = getIterableTextForAccessibility();
7106 if (text != null && text.length() > 0) {
7107 ParagraphTextSegmentIterator iterator =
7108 ParagraphTextSegmentIterator.getInstance();
7109 iterator.initialize(text.toString());
7110 return iterator;
7111 }
7112 } break;
7113 }
7114 return null;
7115 }
7116
Svetoslav Ganov42138042012-03-20 11:51:39 -07007117 /**
Romain Guya440b002010-02-24 15:57:54 -08007118 * @hide
7119 */
7120 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007121 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007122 clearDisplayList();
7123
Romain Guya440b002010-02-24 15:57:54 -08007124 onStartTemporaryDetach();
7125 }
7126
7127 /**
7128 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7130 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007131 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 */
7133 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007134 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007135 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007136 }
7137
7138 /**
7139 * @hide
7140 */
7141 public void dispatchFinishTemporaryDetach() {
7142 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 }
Romain Guy8506ab42009-06-11 17:35:47 -07007144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 /**
7146 * Called after {@link #onStartTemporaryDetach} when the container is done
7147 * changing the view.
7148 */
7149 public void onFinishTemporaryDetach() {
7150 }
Romain Guy8506ab42009-06-11 17:35:47 -07007151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007153 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7154 * for this view's window. Returns null if the view is not currently attached
7155 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007156 * just use the standard high-level event callbacks like
7157 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007158 */
7159 public KeyEvent.DispatcherState getKeyDispatcherState() {
7160 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7161 }
Joe Malin32736f02011-01-19 16:14:20 -08007162
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 * Dispatch a key event before it is processed by any input method
7165 * associated with the view hierarchy. This can be used to intercept
7166 * key events in special situations before the IME consumes them; a
7167 * typical example would be handling the BACK key to update the application's
7168 * UI instead of allowing the IME to see it and close itself.
7169 *
7170 * @param event The key event to be dispatched.
7171 * @return True if the event was handled, false otherwise.
7172 */
7173 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7174 return onKeyPreIme(event.getKeyCode(), event);
7175 }
7176
7177 /**
7178 * Dispatch a key event to the next view on the focus path. This path runs
7179 * from the top of the view tree down to the currently focused view. If this
7180 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7181 * the next node down the focus path. This method also fires any key
7182 * listeners.
7183 *
7184 * @param event The key event to be dispatched.
7185 * @return True if the event was handled, false otherwise.
7186 */
7187 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007188 if (mInputEventConsistencyVerifier != null) {
7189 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191
Jeff Brown21bc5c92011-02-28 18:27:14 -08007192 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007193 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007194 ListenerInfo li = mListenerInfo;
7195 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7196 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 return true;
7198 }
7199
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007200 if (event.dispatch(this, mAttachInfo != null
7201 ? mAttachInfo.mKeyDispatchState : null, this)) {
7202 return true;
7203 }
7204
7205 if (mInputEventConsistencyVerifier != null) {
7206 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7207 }
7208 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 }
7210
7211 /**
7212 * Dispatches a key shortcut event.
7213 *
7214 * @param event The key event to be dispatched.
7215 * @return True if the event was handled by the view, false otherwise.
7216 */
7217 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7218 return onKeyShortcut(event.getKeyCode(), event);
7219 }
7220
7221 /**
7222 * Pass the touch screen motion event down to the target view, or this
7223 * view if it is the target.
7224 *
7225 * @param event The motion event to be dispatched.
7226 * @return True if the event was handled by the view, false otherwise.
7227 */
7228 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007229 if (mInputEventConsistencyVerifier != null) {
7230 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7231 }
7232
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007233 if (onFilterTouchEventForSecurity(event)) {
7234 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007235 ListenerInfo li = mListenerInfo;
7236 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7237 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007238 return true;
7239 }
7240
7241 if (onTouchEvent(event)) {
7242 return true;
7243 }
Jeff Brown85a31762010-09-01 17:01:00 -07007244 }
7245
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007246 if (mInputEventConsistencyVerifier != null) {
7247 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007249 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 }
7251
7252 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007253 * Filter the touch event to apply security policies.
7254 *
7255 * @param event The motion event to be filtered.
7256 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007257 *
Jeff Brown85a31762010-09-01 17:01:00 -07007258 * @see #getFilterTouchesWhenObscured
7259 */
7260 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007261 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007262 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7263 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7264 // Window is obscured, drop this touch.
7265 return false;
7266 }
7267 return true;
7268 }
7269
7270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 * Pass a trackball motion event down to the focused view.
7272 *
7273 * @param event The motion event to be dispatched.
7274 * @return True if the event was handled by the view, false otherwise.
7275 */
7276 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007277 if (mInputEventConsistencyVerifier != null) {
7278 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7279 }
7280
Romain Guy02ccac62011-06-24 13:20:23 -07007281 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 }
7283
7284 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007285 * Dispatch a generic motion event.
7286 * <p>
7287 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7288 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007289 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007290 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007291 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007292 *
7293 * @param event The motion event to be dispatched.
7294 * @return True if the event was handled by the view, false otherwise.
7295 */
7296 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007297 if (mInputEventConsistencyVerifier != null) {
7298 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7299 }
7300
Jeff Browna032cc02011-03-07 16:56:21 -08007301 final int source = event.getSource();
7302 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7303 final int action = event.getAction();
7304 if (action == MotionEvent.ACTION_HOVER_ENTER
7305 || action == MotionEvent.ACTION_HOVER_MOVE
7306 || action == MotionEvent.ACTION_HOVER_EXIT) {
7307 if (dispatchHoverEvent(event)) {
7308 return true;
7309 }
7310 } else if (dispatchGenericPointerEvent(event)) {
7311 return true;
7312 }
7313 } else if (dispatchGenericFocusedEvent(event)) {
7314 return true;
7315 }
7316
Jeff Brown10b62902011-06-20 16:40:37 -07007317 if (dispatchGenericMotionEventInternal(event)) {
7318 return true;
7319 }
7320
7321 if (mInputEventConsistencyVerifier != null) {
7322 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7323 }
7324 return false;
7325 }
7326
7327 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007328 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007329 ListenerInfo li = mListenerInfo;
7330 if (li != null && li.mOnGenericMotionListener != null
7331 && (mViewFlags & ENABLED_MASK) == ENABLED
7332 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007333 return true;
7334 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007335
7336 if (onGenericMotionEvent(event)) {
7337 return true;
7338 }
7339
7340 if (mInputEventConsistencyVerifier != null) {
7341 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7342 }
7343 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007344 }
7345
7346 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007347 * Dispatch a hover event.
7348 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007349 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007350 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007351 * </p>
7352 *
7353 * @param event The motion event to be dispatched.
7354 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007355 */
7356 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007357 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007358 ListenerInfo li = mListenerInfo;
7359 if (li != null && li.mOnHoverListener != null
7360 && (mViewFlags & ENABLED_MASK) == ENABLED
7361 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007362 return true;
7363 }
7364
Jeff Browna032cc02011-03-07 16:56:21 -08007365 return onHoverEvent(event);
7366 }
7367
7368 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007369 * Returns true if the view has a child to which it has recently sent
7370 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7371 * it does not have a hovered child, then it must be the innermost hovered view.
7372 * @hide
7373 */
7374 protected boolean hasHoveredChild() {
7375 return false;
7376 }
7377
7378 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007379 * Dispatch a generic motion event to the view under the first pointer.
7380 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007381 * Do not call this method directly.
7382 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007383 * </p>
7384 *
7385 * @param event The motion event to be dispatched.
7386 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007387 */
7388 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7389 return false;
7390 }
7391
7392 /**
7393 * Dispatch a generic motion event to the currently focused view.
7394 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007395 * Do not call this method directly.
7396 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007397 * </p>
7398 *
7399 * @param event The motion event to be dispatched.
7400 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007401 */
7402 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7403 return false;
7404 }
7405
7406 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007407 * Dispatch a pointer event.
7408 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007409 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7410 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7411 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007412 * and should not be expected to handle other pointing device features.
7413 * </p>
7414 *
7415 * @param event The motion event to be dispatched.
7416 * @return True if the event was handled by the view, false otherwise.
7417 * @hide
7418 */
7419 public final boolean dispatchPointerEvent(MotionEvent event) {
7420 if (event.isTouchEvent()) {
7421 return dispatchTouchEvent(event);
7422 } else {
7423 return dispatchGenericMotionEvent(event);
7424 }
7425 }
7426
7427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 * Called when the window containing this view gains or loses window focus.
7429 * ViewGroups should override to route to their children.
7430 *
7431 * @param hasFocus True if the window containing this view now has focus,
7432 * false otherwise.
7433 */
7434 public void dispatchWindowFocusChanged(boolean hasFocus) {
7435 onWindowFocusChanged(hasFocus);
7436 }
7437
7438 /**
7439 * Called when the window containing this view gains or loses focus. Note
7440 * that this is separate from view focus: to receive key events, both
7441 * your view and its window must have focus. If a window is displayed
7442 * on top of yours that takes input focus, then your own window will lose
7443 * focus but the view focus will remain unchanged.
7444 *
7445 * @param hasWindowFocus True if the window containing this view now has
7446 * focus, false otherwise.
7447 */
7448 public void onWindowFocusChanged(boolean hasWindowFocus) {
7449 InputMethodManager imm = InputMethodManager.peekInstance();
7450 if (!hasWindowFocus) {
7451 if (isPressed()) {
7452 setPressed(false);
7453 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007454 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 imm.focusOut(this);
7456 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007457 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007458 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007459 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007460 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007461 imm.focusIn(this);
7462 }
7463 refreshDrawableState();
7464 }
7465
7466 /**
7467 * Returns true if this view is in a window that currently has window focus.
7468 * Note that this is not the same as the view itself having focus.
7469 *
7470 * @return True if this view is in a window that currently has window focus.
7471 */
7472 public boolean hasWindowFocus() {
7473 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7474 }
7475
7476 /**
Adam Powell326d8082009-12-09 15:10:07 -08007477 * Dispatch a view visibility change down the view hierarchy.
7478 * ViewGroups should override to route to their children.
7479 * @param changedView The view whose visibility changed. Could be 'this' or
7480 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007481 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7482 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007483 */
7484 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7485 onVisibilityChanged(changedView, visibility);
7486 }
7487
7488 /**
7489 * Called when the visibility of the view or an ancestor of the view is changed.
7490 * @param changedView The view whose visibility changed. Could be 'this' or
7491 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007492 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7493 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007494 */
7495 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007496 if (visibility == VISIBLE) {
7497 if (mAttachInfo != null) {
7498 initialAwakenScrollBars();
7499 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007500 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007501 }
7502 }
Adam Powell326d8082009-12-09 15:10:07 -08007503 }
7504
7505 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007506 * Dispatch a hint about whether this view is displayed. For instance, when
7507 * a View moves out of the screen, it might receives a display hint indicating
7508 * the view is not displayed. Applications should not <em>rely</em> on this hint
7509 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007510 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007511 * @param hint A hint about whether or not this view is displayed:
7512 * {@link #VISIBLE} or {@link #INVISIBLE}.
7513 */
7514 public void dispatchDisplayHint(int hint) {
7515 onDisplayHint(hint);
7516 }
7517
7518 /**
7519 * Gives this view a hint about whether is displayed or not. For instance, when
7520 * a View moves out of the screen, it might receives a display hint indicating
7521 * the view is not displayed. Applications should not <em>rely</em> on this hint
7522 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007523 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007524 * @param hint A hint about whether or not this view is displayed:
7525 * {@link #VISIBLE} or {@link #INVISIBLE}.
7526 */
7527 protected void onDisplayHint(int hint) {
7528 }
7529
7530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 * Dispatch a window visibility change down the view hierarchy.
7532 * ViewGroups should override to route to their children.
7533 *
7534 * @param visibility The new visibility of the window.
7535 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007536 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 */
7538 public void dispatchWindowVisibilityChanged(int visibility) {
7539 onWindowVisibilityChanged(visibility);
7540 }
7541
7542 /**
7543 * Called when the window containing has change its visibility
7544 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7545 * that this tells you whether or not your window is being made visible
7546 * to the window manager; this does <em>not</em> tell you whether or not
7547 * your window is obscured by other windows on the screen, even if it
7548 * is itself visible.
7549 *
7550 * @param visibility The new visibility of the window.
7551 */
7552 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007553 if (visibility == VISIBLE) {
7554 initialAwakenScrollBars();
7555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 }
7557
7558 /**
7559 * Returns the current visibility of the window this view is attached to
7560 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7561 *
7562 * @return Returns the current visibility of the view's window.
7563 */
7564 public int getWindowVisibility() {
7565 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7566 }
7567
7568 /**
7569 * Retrieve the overall visible display size in which the window this view is
7570 * attached to has been positioned in. This takes into account screen
7571 * decorations above the window, for both cases where the window itself
7572 * is being position inside of them or the window is being placed under
7573 * then and covered insets are used for the window to position its content
7574 * inside. In effect, this tells you the available area where content can
7575 * be placed and remain visible to users.
7576 *
7577 * <p>This function requires an IPC back to the window manager to retrieve
7578 * the requested information, so should not be used in performance critical
7579 * code like drawing.
7580 *
7581 * @param outRect Filled in with the visible display frame. If the view
7582 * is not attached to a window, this is simply the raw display size.
7583 */
7584 public void getWindowVisibleDisplayFrame(Rect outRect) {
7585 if (mAttachInfo != null) {
7586 try {
7587 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7588 } catch (RemoteException e) {
7589 return;
7590 }
7591 // XXX This is really broken, and probably all needs to be done
7592 // in the window manager, and we need to know more about whether
7593 // we want the area behind or in front of the IME.
7594 final Rect insets = mAttachInfo.mVisibleInsets;
7595 outRect.left += insets.left;
7596 outRect.top += insets.top;
7597 outRect.right -= insets.right;
7598 outRect.bottom -= insets.bottom;
7599 return;
7600 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007601 // The view is not attached to a display so we don't have a context.
7602 // Make a best guess about the display size.
7603 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007604 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 }
7606
7607 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007608 * Dispatch a notification about a resource configuration change down
7609 * the view hierarchy.
7610 * ViewGroups should override to route to their children.
7611 *
7612 * @param newConfig The new resource configuration.
7613 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007614 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007615 */
7616 public void dispatchConfigurationChanged(Configuration newConfig) {
7617 onConfigurationChanged(newConfig);
7618 }
7619
7620 /**
7621 * Called when the current configuration of the resources being used
7622 * by the application have changed. You can use this to decide when
7623 * to reload resources that can changed based on orientation and other
7624 * configuration characterstics. You only need to use this if you are
7625 * not relying on the normal {@link android.app.Activity} mechanism of
7626 * recreating the activity instance upon a configuration change.
7627 *
7628 * @param newConfig The new resource configuration.
7629 */
7630 protected void onConfigurationChanged(Configuration newConfig) {
7631 }
7632
7633 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 * Private function to aggregate all per-view attributes in to the view
7635 * root.
7636 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007637 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7638 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 }
7640
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007641 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7642 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007643 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007644 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007645 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007646 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007647 ListenerInfo li = mListenerInfo;
7648 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007649 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 }
7652 }
7653
7654 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007655 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007656 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007657 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7658 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 ai.mRecomputeGlobalAttributes = true;
7660 }
7661 }
7662 }
7663
7664 /**
7665 * Returns whether the device is currently in touch mode. Touch mode is entered
7666 * once the user begins interacting with the device by touch, and affects various
7667 * things like whether focus is always visible to the user.
7668 *
7669 * @return Whether the device is in touch mode.
7670 */
7671 @ViewDebug.ExportedProperty
7672 public boolean isInTouchMode() {
7673 if (mAttachInfo != null) {
7674 return mAttachInfo.mInTouchMode;
7675 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007676 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 }
7678 }
7679
7680 /**
7681 * Returns the context the view is running in, through which it can
7682 * access the current theme, resources, etc.
7683 *
7684 * @return The view's Context.
7685 */
7686 @ViewDebug.CapturedViewProperty
7687 public final Context getContext() {
7688 return mContext;
7689 }
7690
7691 /**
7692 * Handle a key event before it is processed by any input method
7693 * associated with the view hierarchy. This can be used to intercept
7694 * key events in special situations before the IME consumes them; a
7695 * typical example would be handling the BACK key to update the application's
7696 * UI instead of allowing the IME to see it and close itself.
7697 *
7698 * @param keyCode The value in event.getKeyCode().
7699 * @param event Description of the key event.
7700 * @return If you handled the event, return true. If you want to allow the
7701 * event to be handled by the next receiver, return false.
7702 */
7703 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7704 return false;
7705 }
7706
7707 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007708 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7709 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7711 * is released, if the view is enabled and clickable.
7712 *
Jean Chalard405bc512012-05-29 19:12:34 +09007713 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7714 * although some may elect to do so in some situations. Do not rely on this to
7715 * catch software key presses.
7716 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007717 * @param keyCode A key code that represents the button pressed, from
7718 * {@link android.view.KeyEvent}.
7719 * @param event The KeyEvent object that defines the button action.
7720 */
7721 public boolean onKeyDown(int keyCode, KeyEvent event) {
7722 boolean result = false;
7723
7724 switch (keyCode) {
7725 case KeyEvent.KEYCODE_DPAD_CENTER:
7726 case KeyEvent.KEYCODE_ENTER: {
7727 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7728 return true;
7729 }
7730 // Long clickable items don't necessarily have to be clickable
7731 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7732 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7733 (event.getRepeatCount() == 0)) {
7734 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007735 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 return true;
7737 }
7738 break;
7739 }
7740 }
7741 return result;
7742 }
7743
7744 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007745 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7746 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7747 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007748 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7749 * although some may elect to do so in some situations. Do not rely on this to
7750 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007751 */
7752 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7753 return false;
7754 }
7755
7756 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007757 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7758 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7760 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007761 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7762 * although some may elect to do so in some situations. Do not rely on this to
7763 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007764 *
7765 * @param keyCode A key code that represents the button pressed, from
7766 * {@link android.view.KeyEvent}.
7767 * @param event The KeyEvent object that defines the button action.
7768 */
7769 public boolean onKeyUp(int keyCode, KeyEvent event) {
7770 boolean result = false;
7771
7772 switch (keyCode) {
7773 case KeyEvent.KEYCODE_DPAD_CENTER:
7774 case KeyEvent.KEYCODE_ENTER: {
7775 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7776 return true;
7777 }
7778 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7779 setPressed(false);
7780
7781 if (!mHasPerformedLongPress) {
7782 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007783 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784
7785 result = performClick();
7786 }
7787 }
7788 break;
7789 }
7790 }
7791 return result;
7792 }
7793
7794 /**
7795 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7796 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7797 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007798 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7799 * although some may elect to do so in some situations. Do not rely on this to
7800 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 *
7802 * @param keyCode A key code that represents the button pressed, from
7803 * {@link android.view.KeyEvent}.
7804 * @param repeatCount The number of times the action was made.
7805 * @param event The KeyEvent object that defines the button action.
7806 */
7807 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7808 return false;
7809 }
7810
7811 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007812 * Called on the focused view when a key shortcut event is not handled.
7813 * Override this method to implement local key shortcuts for the View.
7814 * Key shortcuts can also be implemented by setting the
7815 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 *
7817 * @param keyCode The value in event.getKeyCode().
7818 * @param event Description of the key event.
7819 * @return If you handled the event, return true. If you want to allow the
7820 * event to be handled by the next receiver, return false.
7821 */
7822 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7823 return false;
7824 }
7825
7826 /**
7827 * Check whether the called view is a text editor, in which case it
7828 * would make sense to automatically display a soft input window for
7829 * it. Subclasses should override this if they implement
7830 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007831 * a call on that method would return a non-null InputConnection, and
7832 * they are really a first-class editor that the user would normally
7833 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007834 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007835 * <p>The default implementation always returns false. This does
7836 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7837 * will not be called or the user can not otherwise perform edits on your
7838 * view; it is just a hint to the system that this is not the primary
7839 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007840 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007841 * @return Returns true if this view is a text editor, else false.
7842 */
7843 public boolean onCheckIsTextEditor() {
7844 return false;
7845 }
Romain Guy8506ab42009-06-11 17:35:47 -07007846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 /**
7848 * Create a new InputConnection for an InputMethod to interact
7849 * with the view. The default implementation returns null, since it doesn't
7850 * support input methods. You can override this to implement such support.
7851 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007852 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 * <p>When implementing this, you probably also want to implement
7854 * {@link #onCheckIsTextEditor()} to indicate you will return a
7855 * non-null InputConnection.
7856 *
7857 * @param outAttrs Fill in with attribute information about the connection.
7858 */
7859 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7860 return null;
7861 }
7862
7863 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007864 * Called by the {@link android.view.inputmethod.InputMethodManager}
7865 * when a view who is not the current
7866 * input connection target is trying to make a call on the manager. The
7867 * default implementation returns false; you can override this to return
7868 * true for certain views if you are performing InputConnection proxying
7869 * to them.
7870 * @param view The View that is making the InputMethodManager call.
7871 * @return Return true to allow the call, false to reject.
7872 */
7873 public boolean checkInputConnectionProxy(View view) {
7874 return false;
7875 }
Romain Guy8506ab42009-06-11 17:35:47 -07007876
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 * Show the context menu for this view. It is not safe to hold on to the
7879 * menu after returning from this method.
7880 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007881 * You should normally not overload this method. Overload
7882 * {@link #onCreateContextMenu(ContextMenu)} or define an
7883 * {@link OnCreateContextMenuListener} to add items to the context menu.
7884 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 * @param menu The context menu to populate
7886 */
7887 public void createContextMenu(ContextMenu menu) {
7888 ContextMenuInfo menuInfo = getContextMenuInfo();
7889
7890 // Sets the current menu info so all items added to menu will have
7891 // my extra info set.
7892 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7893
7894 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007895 ListenerInfo li = mListenerInfo;
7896 if (li != null && li.mOnCreateContextMenuListener != null) {
7897 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 }
7899
7900 // Clear the extra information so subsequent items that aren't mine don't
7901 // have my extra info.
7902 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7903
7904 if (mParent != null) {
7905 mParent.createContextMenu(menu);
7906 }
7907 }
7908
7909 /**
7910 * Views should implement this if they have extra information to associate
7911 * with the context menu. The return result is supplied as a parameter to
7912 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7913 * callback.
7914 *
7915 * @return Extra information about the item for which the context menu
7916 * should be shown. This information will vary across different
7917 * subclasses of View.
7918 */
7919 protected ContextMenuInfo getContextMenuInfo() {
7920 return null;
7921 }
7922
7923 /**
7924 * Views should implement this if the view itself is going to add items to
7925 * the context menu.
7926 *
7927 * @param menu the context menu to populate
7928 */
7929 protected void onCreateContextMenu(ContextMenu menu) {
7930 }
7931
7932 /**
7933 * Implement this method to handle trackball motion events. The
7934 * <em>relative</em> movement of the trackball since the last event
7935 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7936 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7937 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7938 * they will often be fractional values, representing the more fine-grained
7939 * movement information available from a trackball).
7940 *
7941 * @param event The motion event.
7942 * @return True if the event was handled, false otherwise.
7943 */
7944 public boolean onTrackballEvent(MotionEvent event) {
7945 return false;
7946 }
7947
7948 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007949 * Implement this method to handle generic motion events.
7950 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007951 * Generic motion events describe joystick movements, mouse hovers, track pad
7952 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007953 * {@link MotionEvent#getSource() source} of the motion event specifies
7954 * the class of input that was received. Implementations of this method
7955 * must examine the bits in the source before processing the event.
7956 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007957 * </p><p>
7958 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7959 * are delivered to the view under the pointer. All other generic motion events are
7960 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007961 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007962 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007963 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007964 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7965 * // process the joystick movement...
7966 * return true;
7967 * }
7968 * }
7969 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7970 * switch (event.getAction()) {
7971 * case MotionEvent.ACTION_HOVER_MOVE:
7972 * // process the mouse hover movement...
7973 * return true;
7974 * case MotionEvent.ACTION_SCROLL:
7975 * // process the scroll wheel movement...
7976 * return true;
7977 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007978 * }
7979 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007980 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007981 *
7982 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007983 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007984 */
7985 public boolean onGenericMotionEvent(MotionEvent event) {
7986 return false;
7987 }
7988
7989 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007990 * Implement this method to handle hover events.
7991 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007992 * This method is called whenever a pointer is hovering into, over, or out of the
7993 * bounds of a view and the view is not currently being touched.
7994 * Hover events are represented as pointer events with action
7995 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7996 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7997 * </p>
7998 * <ul>
7999 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8000 * when the pointer enters the bounds of the view.</li>
8001 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8002 * when the pointer has already entered the bounds of the view and has moved.</li>
8003 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8004 * when the pointer has exited the bounds of the view or when the pointer is
8005 * about to go down due to a button click, tap, or similar user action that
8006 * causes the view to be touched.</li>
8007 * </ul>
8008 * <p>
8009 * The view should implement this method to return true to indicate that it is
8010 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008011 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008012 * The default implementation calls {@link #setHovered} to update the hovered state
8013 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008014 * is enabled and is clickable. The default implementation also sends hover
8015 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008016 * </p>
8017 *
8018 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008019 * @return True if the view handled the hover event.
8020 *
8021 * @see #isHovered
8022 * @see #setHovered
8023 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008024 */
8025 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008026 // The root view may receive hover (or touch) events that are outside the bounds of
8027 // the window. This code ensures that we only send accessibility events for
8028 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008029 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008030 if (!mSendingHoverAccessibilityEvents) {
8031 if ((action == MotionEvent.ACTION_HOVER_ENTER
8032 || action == MotionEvent.ACTION_HOVER_MOVE)
8033 && !hasHoveredChild()
8034 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008035 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008036 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008037 }
8038 } else {
8039 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008040 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008041 && !pointInView(event.getX(), event.getY()))) {
8042 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008043 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008044 // If the window does not have input focus we take away accessibility
8045 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008046 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008047 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008048 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008049 }
Jeff Browna1b24182011-07-28 13:38:24 -07008050 }
8051
Jeff Brown87b7f802011-06-21 18:35:45 -07008052 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008053 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008054 case MotionEvent.ACTION_HOVER_ENTER:
8055 setHovered(true);
8056 break;
8057 case MotionEvent.ACTION_HOVER_EXIT:
8058 setHovered(false);
8059 break;
8060 }
Jeff Browna1b24182011-07-28 13:38:24 -07008061
8062 // Dispatch the event to onGenericMotionEvent before returning true.
8063 // This is to provide compatibility with existing applications that
8064 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8065 // break because of the new default handling for hoverable views
8066 // in onHoverEvent.
8067 // Note that onGenericMotionEvent will be called by default when
8068 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8069 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008070 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008071 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008072
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008073 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008074 }
8075
8076 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008077 * Returns true if the view should handle {@link #onHoverEvent}
8078 * by calling {@link #setHovered} to change its hovered state.
8079 *
8080 * @return True if the view is hoverable.
8081 */
8082 private boolean isHoverable() {
8083 final int viewFlags = mViewFlags;
8084 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8085 return false;
8086 }
8087
8088 return (viewFlags & CLICKABLE) == CLICKABLE
8089 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8090 }
8091
8092 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008093 * Returns true if the view is currently hovered.
8094 *
8095 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008096 *
8097 * @see #setHovered
8098 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008099 */
Jeff Brown10b62902011-06-20 16:40:37 -07008100 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008101 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008102 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008103 }
8104
8105 /**
8106 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008107 * <p>
8108 * Calling this method also changes the drawable state of the view. This
8109 * enables the view to react to hover by using different drawable resources
8110 * to change its appearance.
8111 * </p><p>
8112 * The {@link #onHoverChanged} method is called when the hovered state changes.
8113 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008114 *
8115 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008116 *
8117 * @see #isHovered
8118 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008119 */
8120 public void setHovered(boolean hovered) {
8121 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008122 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8123 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008124 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008125 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008126 }
8127 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008128 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8129 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008130 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008131 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008132 }
8133 }
8134 }
8135
8136 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008137 * Implement this method to handle hover state changes.
8138 * <p>
8139 * This method is called whenever the hover state changes as a result of a
8140 * call to {@link #setHovered}.
8141 * </p>
8142 *
8143 * @param hovered The current hover state, as returned by {@link #isHovered}.
8144 *
8145 * @see #isHovered
8146 * @see #setHovered
8147 */
8148 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008149 }
8150
8151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 * Implement this method to handle touch screen motion events.
8153 *
8154 * @param event The motion event.
8155 * @return True if the event was handled, false otherwise.
8156 */
8157 public boolean onTouchEvent(MotionEvent event) {
8158 final int viewFlags = mViewFlags;
8159
8160 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008161 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008162 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164 // A disabled view that is clickable still consumes the touch
8165 // events, it just doesn't respond to them.
8166 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8167 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8168 }
8169
8170 if (mTouchDelegate != null) {
8171 if (mTouchDelegate.onTouchEvent(event)) {
8172 return true;
8173 }
8174 }
8175
8176 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8177 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8178 switch (event.getAction()) {
8179 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008180 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8181 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 // take focus if we don't have it already and we should in
8183 // touch mode.
8184 boolean focusTaken = false;
8185 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8186 focusTaken = requestFocus();
8187 }
8188
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008189 if (prepressed) {
8190 // The button is being released before we actually
8191 // showed it as pressed. Make it show the pressed
8192 // state now (before scheduling the click) to ensure
8193 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008194 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008195 }
Joe Malin32736f02011-01-19 16:14:20 -08008196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 if (!mHasPerformedLongPress) {
8198 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008199 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200
8201 // Only perform take click actions if we were in the pressed state
8202 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008203 // Use a Runnable and post this rather than calling
8204 // performClick directly. This lets other visual state
8205 // of the view update before click actions start.
8206 if (mPerformClick == null) {
8207 mPerformClick = new PerformClick();
8208 }
8209 if (!post(mPerformClick)) {
8210 performClick();
8211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 }
8213 }
8214
8215 if (mUnsetPressedState == null) {
8216 mUnsetPressedState = new UnsetPressedState();
8217 }
8218
Adam Powelle14579b2009-12-16 18:39:52 -08008219 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008220 postDelayed(mUnsetPressedState,
8221 ViewConfiguration.getPressedStateDuration());
8222 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 // If the post failed, unpress right now
8224 mUnsetPressedState.run();
8225 }
Adam Powelle14579b2009-12-16 18:39:52 -08008226 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 }
8228 break;
8229
8230 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008231 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008232
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008233 if (performButtonActionOnTouchDown(event)) {
8234 break;
8235 }
8236
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008237 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008238 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008239
8240 // For views inside a scrolling container, delay the pressed feedback for
8241 // a short period in case this is a scroll.
8242 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008243 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008244 if (mPendingCheckForTap == null) {
8245 mPendingCheckForTap = new CheckForTap();
8246 }
8247 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8248 } else {
8249 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008250 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008251 checkForLongClick(0);
8252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 break;
8254
8255 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008256 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008257 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008258 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 break;
8260
8261 case MotionEvent.ACTION_MOVE:
8262 final int x = (int) event.getX();
8263 final int y = (int) event.getY();
8264
8265 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008266 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008268 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008269 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008270 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008271 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008272
Adam Powell4d6f0662012-02-21 15:11:11 -08008273 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 }
8276 break;
8277 }
8278 return true;
8279 }
8280
8281 return false;
8282 }
8283
8284 /**
Adam Powell10298662011-08-14 18:26:30 -07008285 * @hide
8286 */
8287 public boolean isInScrollingContainer() {
8288 ViewParent p = getParent();
8289 while (p != null && p instanceof ViewGroup) {
8290 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8291 return true;
8292 }
8293 p = p.getParent();
8294 }
8295 return false;
8296 }
8297
8298 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008299 * Remove the longpress detection timer.
8300 */
8301 private void removeLongPressCallback() {
8302 if (mPendingCheckForLongPress != null) {
8303 removeCallbacks(mPendingCheckForLongPress);
8304 }
8305 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008306
8307 /**
8308 * Remove the pending click action
8309 */
8310 private void removePerformClickCallback() {
8311 if (mPerformClick != null) {
8312 removeCallbacks(mPerformClick);
8313 }
8314 }
8315
Adam Powelle14579b2009-12-16 18:39:52 -08008316 /**
Romain Guya440b002010-02-24 15:57:54 -08008317 * Remove the prepress detection timer.
8318 */
8319 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008320 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008321 setPressed(false);
8322 removeCallbacks(mUnsetPressedState);
8323 }
8324 }
8325
8326 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008327 * Remove the tap detection timer.
8328 */
8329 private void removeTapCallback() {
8330 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008331 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008332 removeCallbacks(mPendingCheckForTap);
8333 }
8334 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008335
8336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 * Cancels a pending long press. Your subclass can use this if you
8338 * want the context menu to come up if the user presses and holds
8339 * at the same place, but you don't want it to come up if they press
8340 * and then move around enough to cause scrolling.
8341 */
8342 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008343 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008344
8345 /*
8346 * The prepressed state handled by the tap callback is a display
8347 * construct, but the tap callback will post a long press callback
8348 * less its own timeout. Remove it here.
8349 */
8350 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 }
8352
8353 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008354 * Remove the pending callback for sending a
8355 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8356 */
8357 private void removeSendViewScrolledAccessibilityEventCallback() {
8358 if (mSendViewScrolledAccessibilityEvent != null) {
8359 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008360 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008361 }
8362 }
8363
8364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 * Sets the TouchDelegate for this View.
8366 */
8367 public void setTouchDelegate(TouchDelegate delegate) {
8368 mTouchDelegate = delegate;
8369 }
8370
8371 /**
8372 * Gets the TouchDelegate for this View.
8373 */
8374 public TouchDelegate getTouchDelegate() {
8375 return mTouchDelegate;
8376 }
8377
8378 /**
8379 * Set flags controlling behavior of this view.
8380 *
8381 * @param flags Constant indicating the value which should be set
8382 * @param mask Constant indicating the bit range that should be changed
8383 */
8384 void setFlags(int flags, int mask) {
8385 int old = mViewFlags;
8386 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8387
8388 int changed = mViewFlags ^ old;
8389 if (changed == 0) {
8390 return;
8391 }
8392 int privateFlags = mPrivateFlags;
8393
8394 /* Check if the FOCUSABLE bit has changed */
8395 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008396 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008397 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008398 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 /* Give up focus if we are no longer focusable */
8400 clearFocus();
8401 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008402 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008403 /*
8404 * Tell the view system that we are now available to take focus
8405 * if no one else already has it.
8406 */
8407 if (mParent != null) mParent.focusableViewAvailable(this);
8408 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008409 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8410 notifyAccessibilityStateChanged();
8411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 }
8413
8414 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8415 if ((changed & VISIBILITY_MASK) != 0) {
8416 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008417 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008418 * it was not visible. Marking it drawn ensures that the invalidation will
8419 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008421 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008422 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423
8424 needGlobalAttributesUpdate(true);
8425
8426 // a view becoming visible is worth notifying the parent
8427 // about in case nothing has focus. even if this specific view
8428 // isn't focusable, it may contain something that is, so let
8429 // the root view try to give this focus if nothing else does.
8430 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8431 mParent.focusableViewAvailable(this);
8432 }
8433 }
8434 }
8435
8436 /* Check if the GONE bit has changed */
8437 if ((changed & GONE) != 0) {
8438 needGlobalAttributesUpdate(false);
8439 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440
Romain Guyecd80ee2009-12-03 17:13:02 -08008441 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8442 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008443 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008444 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008445 if (mParent instanceof View) {
8446 // GONE views noop invalidation, so invalidate the parent
8447 ((View) mParent).invalidate(true);
8448 }
8449 // Mark the view drawn to ensure that it gets invalidated properly the next
8450 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008451 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 }
8453 if (mAttachInfo != null) {
8454 mAttachInfo.mViewVisibilityChanged = true;
8455 }
8456 }
8457
8458 /* Check if the VISIBLE bit has changed */
8459 if ((changed & INVISIBLE) != 0) {
8460 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008461 /*
8462 * If this view is becoming invisible, set the DRAWN flag so that
8463 * the next invalidate() will not be skipped.
8464 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008465 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466
8467 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008468 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 if (getRootView() != this) {
8470 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008471 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 }
8473 }
8474 if (mAttachInfo != null) {
8475 mAttachInfo.mViewVisibilityChanged = true;
8476 }
8477 }
8478
Adam Powell326d8082009-12-09 15:10:07 -08008479 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008480 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008481 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8482 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008483 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008484 } else if (mParent != null) {
8485 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008486 }
Adam Powell326d8082009-12-09 15:10:07 -08008487 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8488 }
8489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8491 destroyDrawingCache();
8492 }
8493
8494 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8495 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008496 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008497 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 }
8499
8500 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8501 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008502 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 }
8504
8505 if ((changed & DRAW_MASK) != 0) {
8506 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008507 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008508 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8509 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008510 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008511 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 }
8513 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008514 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 }
8516 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008517 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 }
8519
8520 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008521 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008522 mParent.recomputeViewAttributes(this);
8523 }
8524 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008525
8526 if (AccessibilityManager.getInstance(mContext).isEnabled()
8527 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8528 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8529 notifyAccessibilityStateChanged();
8530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008531 }
8532
8533 /**
8534 * Change the view's z order in the tree, so it's on top of other sibling
8535 * views
8536 */
8537 public void bringToFront() {
8538 if (mParent != null) {
8539 mParent.bringChildToFront(this);
8540 }
8541 }
8542
8543 /**
8544 * This is called in response to an internal scroll in this view (i.e., the
8545 * view scrolled its own contents). This is typically as a result of
8546 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8547 * called.
8548 *
8549 * @param l Current horizontal scroll origin.
8550 * @param t Current vertical scroll origin.
8551 * @param oldl Previous horizontal scroll origin.
8552 * @param oldt Previous vertical scroll origin.
8553 */
8554 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008555 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8556 postSendViewScrolledAccessibilityEventCallback();
8557 }
8558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 mBackgroundSizeChanged = true;
8560
8561 final AttachInfo ai = mAttachInfo;
8562 if (ai != null) {
8563 ai.mViewScrollChanged = true;
8564 }
8565 }
8566
8567 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008568 * Interface definition for a callback to be invoked when the layout bounds of a view
8569 * changes due to layout processing.
8570 */
8571 public interface OnLayoutChangeListener {
8572 /**
8573 * Called when the focus state of a view has changed.
8574 *
8575 * @param v The view whose state has changed.
8576 * @param left The new value of the view's left property.
8577 * @param top The new value of the view's top property.
8578 * @param right The new value of the view's right property.
8579 * @param bottom The new value of the view's bottom property.
8580 * @param oldLeft The previous value of the view's left property.
8581 * @param oldTop The previous value of the view's top property.
8582 * @param oldRight The previous value of the view's right property.
8583 * @param oldBottom The previous value of the view's bottom property.
8584 */
8585 void onLayoutChange(View v, int left, int top, int right, int bottom,
8586 int oldLeft, int oldTop, int oldRight, int oldBottom);
8587 }
8588
8589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 * This is called during layout when the size of this view has changed. If
8591 * you were just added to the view hierarchy, you're called with the old
8592 * values of 0.
8593 *
8594 * @param w Current width of this view.
8595 * @param h Current height of this view.
8596 * @param oldw Old width of this view.
8597 * @param oldh Old height of this view.
8598 */
8599 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8600 }
8601
8602 /**
8603 * Called by draw to draw the child views. This may be overridden
8604 * by derived classes to gain control just before its children are drawn
8605 * (but after its own view has been drawn).
8606 * @param canvas the canvas on which to draw the view
8607 */
8608 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 }
8611
8612 /**
8613 * Gets the parent of this view. Note that the parent is a
8614 * ViewParent and not necessarily a View.
8615 *
8616 * @return Parent of this view.
8617 */
8618 public final ViewParent getParent() {
8619 return mParent;
8620 }
8621
8622 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008623 * Set the horizontal scrolled position of your view. This will cause a call to
8624 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8625 * invalidated.
8626 * @param value the x position to scroll to
8627 */
8628 public void setScrollX(int value) {
8629 scrollTo(value, mScrollY);
8630 }
8631
8632 /**
8633 * Set the vertical scrolled position of your view. This will cause a call to
8634 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8635 * invalidated.
8636 * @param value the y position to scroll to
8637 */
8638 public void setScrollY(int value) {
8639 scrollTo(mScrollX, value);
8640 }
8641
8642 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 * Return the scrolled left position of this view. This is the left edge of
8644 * the displayed part of your view. You do not need to draw any pixels
8645 * farther left, since those are outside of the frame of your view on
8646 * screen.
8647 *
8648 * @return The left edge of the displayed part of your view, in pixels.
8649 */
8650 public final int getScrollX() {
8651 return mScrollX;
8652 }
8653
8654 /**
8655 * Return the scrolled top position of this view. This is the top edge of
8656 * the displayed part of your view. You do not need to draw any pixels above
8657 * it, since those are outside of the frame of your view on screen.
8658 *
8659 * @return The top edge of the displayed part of your view, in pixels.
8660 */
8661 public final int getScrollY() {
8662 return mScrollY;
8663 }
8664
8665 /**
8666 * Return the width of the your view.
8667 *
8668 * @return The width of your view, in pixels.
8669 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008670 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 public final int getWidth() {
8672 return mRight - mLeft;
8673 }
8674
8675 /**
8676 * Return the height of your view.
8677 *
8678 * @return The height of your view, in pixels.
8679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008680 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 public final int getHeight() {
8682 return mBottom - mTop;
8683 }
8684
8685 /**
8686 * Return the visible drawing bounds of your view. Fills in the output
8687 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008688 * getWidth(), and getHeight(). These bounds do not account for any
8689 * transformation properties currently set on the view, such as
8690 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 *
8692 * @param outRect The (scrolled) drawing bounds of the view.
8693 */
8694 public void getDrawingRect(Rect outRect) {
8695 outRect.left = mScrollX;
8696 outRect.top = mScrollY;
8697 outRect.right = mScrollX + (mRight - mLeft);
8698 outRect.bottom = mScrollY + (mBottom - mTop);
8699 }
8700
8701 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008702 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8703 * raw width component (that is the result is masked by
8704 * {@link #MEASURED_SIZE_MASK}).
8705 *
8706 * @return The raw measured width of this view.
8707 */
8708 public final int getMeasuredWidth() {
8709 return mMeasuredWidth & MEASURED_SIZE_MASK;
8710 }
8711
8712 /**
8713 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008714 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008715 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008716 * This should be used during measurement and layout calculations only. Use
8717 * {@link #getWidth()} to see how wide a view is after layout.
8718 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008719 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008721 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 return mMeasuredWidth;
8723 }
8724
8725 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008726 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8727 * raw width component (that is the result is masked by
8728 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008729 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008730 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 */
8732 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008733 return mMeasuredHeight & MEASURED_SIZE_MASK;
8734 }
8735
8736 /**
8737 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008738 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008739 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8740 * This should be used during measurement and layout calculations only. Use
8741 * {@link #getHeight()} to see how wide a view is after layout.
8742 *
8743 * @return The measured width of this view as a bit mask.
8744 */
8745 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 return mMeasuredHeight;
8747 }
8748
8749 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008750 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8751 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8752 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8753 * and the height component is at the shifted bits
8754 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8755 */
8756 public final int getMeasuredState() {
8757 return (mMeasuredWidth&MEASURED_STATE_MASK)
8758 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8759 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8760 }
8761
8762 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008763 * The transform matrix of this view, which is calculated based on the current
8764 * roation, scale, and pivot properties.
8765 *
8766 * @see #getRotation()
8767 * @see #getScaleX()
8768 * @see #getScaleY()
8769 * @see #getPivotX()
8770 * @see #getPivotY()
8771 * @return The current transform matrix for the view
8772 */
8773 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008774 if (mTransformationInfo != null) {
8775 updateMatrix();
8776 return mTransformationInfo.mMatrix;
8777 }
8778 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008779 }
8780
8781 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008782 * Utility function to determine if the value is far enough away from zero to be
8783 * considered non-zero.
8784 * @param value A floating point value to check for zero-ness
8785 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8786 */
8787 private static boolean nonzero(float value) {
8788 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8789 }
8790
8791 /**
Jeff Brown86671742010-09-30 20:00:15 -07008792 * Returns true if the transform matrix is the identity matrix.
8793 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008794 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008795 * @return True if the transform matrix is the identity matrix, false otherwise.
8796 */
Jeff Brown86671742010-09-30 20:00:15 -07008797 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008798 if (mTransformationInfo != null) {
8799 updateMatrix();
8800 return mTransformationInfo.mMatrixIsIdentity;
8801 }
8802 return true;
8803 }
8804
8805 void ensureTransformationInfo() {
8806 if (mTransformationInfo == null) {
8807 mTransformationInfo = new TransformationInfo();
8808 }
Jeff Brown86671742010-09-30 20:00:15 -07008809 }
8810
8811 /**
8812 * Recomputes the transform matrix if necessary.
8813 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008814 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 final TransformationInfo info = mTransformationInfo;
8816 if (info == null) {
8817 return;
8818 }
8819 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008820 // transform-related properties have changed since the last time someone
8821 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008822
8823 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008824 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008825 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8826 info.mPrevWidth = mRight - mLeft;
8827 info.mPrevHeight = mBottom - mTop;
8828 info.mPivotX = info.mPrevWidth / 2f;
8829 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008830 }
8831 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008832 info.mMatrix.reset();
8833 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8834 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8835 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8836 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008837 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008838 if (info.mCamera == null) {
8839 info.mCamera = new Camera();
8840 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008841 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 info.mCamera.save();
8843 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8844 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8845 info.mCamera.getMatrix(info.matrix3D);
8846 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8847 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8848 info.mPivotY + info.mTranslationY);
8849 info.mMatrix.postConcat(info.matrix3D);
8850 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008851 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008852 info.mMatrixDirty = false;
8853 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8854 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008855 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008856 }
8857
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008858 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008859 * Utility method to retrieve the inverse of the current mMatrix property.
8860 * We cache the matrix to avoid recalculating it when transform properties
8861 * have not changed.
8862 *
8863 * @return The inverse of the current matrix of this view.
8864 */
Jeff Brown86671742010-09-30 20:00:15 -07008865 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008866 final TransformationInfo info = mTransformationInfo;
8867 if (info != null) {
8868 updateMatrix();
8869 if (info.mInverseMatrixDirty) {
8870 if (info.mInverseMatrix == null) {
8871 info.mInverseMatrix = new Matrix();
8872 }
8873 info.mMatrix.invert(info.mInverseMatrix);
8874 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008875 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008876 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008877 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008878 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008879 }
8880
8881 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008882 * Gets the distance along the Z axis from the camera to this view.
8883 *
8884 * @see #setCameraDistance(float)
8885 *
8886 * @return The distance along the Z axis.
8887 */
8888 public float getCameraDistance() {
8889 ensureTransformationInfo();
8890 final float dpi = mResources.getDisplayMetrics().densityDpi;
8891 final TransformationInfo info = mTransformationInfo;
8892 if (info.mCamera == null) {
8893 info.mCamera = new Camera();
8894 info.matrix3D = new Matrix();
8895 }
8896 return -(info.mCamera.getLocationZ() * dpi);
8897 }
8898
8899 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008900 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8901 * views are drawn) from the camera to this view. The camera's distance
8902 * affects 3D transformations, for instance rotations around the X and Y
8903 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008904 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008905 * use a camera distance that's greater than the height (X axis rotation) or
8906 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008907 *
Romain Guya5364ee2011-02-24 14:46:04 -08008908 * <p>The distance of the camera from the view plane can have an affect on the
8909 * perspective distortion of the view when it is rotated around the x or y axis.
8910 * For example, a large distance will result in a large viewing angle, and there
8911 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008912 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008913 * also result in some drawing artifacts if the rotated view ends up partially
8914 * behind the camera (which is why the recommendation is to use a distance at
8915 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008916 *
Romain Guya5364ee2011-02-24 14:46:04 -08008917 * <p>The distance is expressed in "depth pixels." The default distance depends
8918 * on the screen density. For instance, on a medium density display, the
8919 * default distance is 1280. On a high density display, the default distance
8920 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008921 *
Romain Guya5364ee2011-02-24 14:46:04 -08008922 * <p>If you want to specify a distance that leads to visually consistent
8923 * results across various densities, use the following formula:</p>
8924 * <pre>
8925 * float scale = context.getResources().getDisplayMetrics().density;
8926 * view.setCameraDistance(distance * scale);
8927 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008928 *
Romain Guya5364ee2011-02-24 14:46:04 -08008929 * <p>The density scale factor of a high density display is 1.5,
8930 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008931 *
Romain Guya5364ee2011-02-24 14:46:04 -08008932 * @param distance The distance in "depth pixels", if negative the opposite
8933 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008934 *
8935 * @see #setRotationX(float)
8936 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008937 */
8938 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008939 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008940
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008941 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008942 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008943 final TransformationInfo info = mTransformationInfo;
8944 if (info.mCamera == null) {
8945 info.mCamera = new Camera();
8946 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008947 }
8948
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008949 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8950 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008951
Chet Haase9d1992d2012-03-13 11:03:25 -07008952 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008953 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008954 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008955 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008956 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008957 // View was rejected last time it was drawn by its parent; this may have changed
8958 invalidateParentIfNeeded();
8959 }
Romain Guya5364ee2011-02-24 14:46:04 -08008960 }
8961
8962 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008963 * The degrees that the view is rotated around the pivot point.
8964 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008965 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 * @see #getPivotX()
8967 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008968 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008969 * @return The degrees of rotation.
8970 */
Chet Haasea5531132012-02-02 13:41:44 -08008971 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008972 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008973 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008974 }
8975
8976 /**
Chet Haase897247b2010-09-09 14:54:47 -07008977 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8978 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008979 *
8980 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008981 *
8982 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008983 * @see #getPivotX()
8984 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008985 * @see #setRotationX(float)
8986 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008987 *
8988 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008989 */
8990 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 ensureTransformationInfo();
8992 final TransformationInfo info = mTransformationInfo;
8993 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008994 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008995 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008996 info.mRotation = rotation;
8997 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008998 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008999 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009000 mDisplayList.setRotation(rotation);
9001 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009002 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009003 // View was rejected last time it was drawn by its parent; this may have changed
9004 invalidateParentIfNeeded();
9005 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009006 }
9007 }
9008
9009 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009010 * The degrees that the view is rotated around the vertical axis through the pivot point.
9011 *
9012 * @see #getPivotX()
9013 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009014 * @see #setRotationY(float)
9015 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009016 * @return The degrees of Y rotation.
9017 */
Chet Haasea5531132012-02-02 13:41:44 -08009018 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009019 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009020 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009021 }
9022
9023 /**
Chet Haase897247b2010-09-09 14:54:47 -07009024 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9025 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9026 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009027 *
Romain Guya5364ee2011-02-24 14:46:04 -08009028 * When rotating large views, it is recommended to adjust the camera distance
9029 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009030 *
9031 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009032 *
9033 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009034 * @see #getPivotX()
9035 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009036 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009037 * @see #setRotationX(float)
9038 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009039 *
9040 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009041 */
9042 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009043 ensureTransformationInfo();
9044 final TransformationInfo info = mTransformationInfo;
9045 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009046 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009047 info.mRotationY = rotationY;
9048 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009049 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009050 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009051 mDisplayList.setRotationY(rotationY);
9052 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009053 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009054 // View was rejected last time it was drawn by its parent; this may have changed
9055 invalidateParentIfNeeded();
9056 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009057 }
9058 }
9059
9060 /**
9061 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9062 *
9063 * @see #getPivotX()
9064 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009065 * @see #setRotationX(float)
9066 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009067 * @return The degrees of X rotation.
9068 */
Chet Haasea5531132012-02-02 13:41:44 -08009069 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009070 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009071 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009072 }
9073
9074 /**
Chet Haase897247b2010-09-09 14:54:47 -07009075 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9076 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9077 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009078 *
Romain Guya5364ee2011-02-24 14:46:04 -08009079 * When rotating large views, it is recommended to adjust the camera distance
9080 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009081 *
9082 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009083 *
9084 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009085 * @see #getPivotX()
9086 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009087 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009088 * @see #setRotationY(float)
9089 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009090 *
9091 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009092 */
9093 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009094 ensureTransformationInfo();
9095 final TransformationInfo info = mTransformationInfo;
9096 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009097 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009098 info.mRotationX = rotationX;
9099 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009100 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009101 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009102 mDisplayList.setRotationX(rotationX);
9103 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009104 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009105 // View was rejected last time it was drawn by its parent; this may have changed
9106 invalidateParentIfNeeded();
9107 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009108 }
9109 }
9110
9111 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009112 * The amount that the view is scaled in x around the pivot point, as a proportion of
9113 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9114 *
Joe Onorato93162322010-09-16 15:42:01 -04009115 * <p>By default, this is 1.0f.
9116 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009117 * @see #getPivotX()
9118 * @see #getPivotY()
9119 * @return The scaling factor.
9120 */
Chet Haasea5531132012-02-02 13:41:44 -08009121 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009122 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009123 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009124 }
9125
9126 /**
9127 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9128 * the view's unscaled width. A value of 1 means that no scaling is applied.
9129 *
9130 * @param scaleX The scaling factor.
9131 * @see #getPivotX()
9132 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009133 *
9134 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009135 */
9136 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009137 ensureTransformationInfo();
9138 final TransformationInfo info = mTransformationInfo;
9139 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009140 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009141 info.mScaleX = scaleX;
9142 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009143 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009144 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009145 mDisplayList.setScaleX(scaleX);
9146 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009147 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009148 // View was rejected last time it was drawn by its parent; this may have changed
9149 invalidateParentIfNeeded();
9150 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009151 }
9152 }
9153
9154 /**
9155 * The amount that the view is scaled in y around the pivot point, as a proportion of
9156 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9157 *
Joe Onorato93162322010-09-16 15:42:01 -04009158 * <p>By default, this is 1.0f.
9159 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009160 * @see #getPivotX()
9161 * @see #getPivotY()
9162 * @return The scaling factor.
9163 */
Chet Haasea5531132012-02-02 13:41:44 -08009164 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009166 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009167 }
9168
9169 /**
9170 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9171 * the view's unscaled width. A value of 1 means that no scaling is applied.
9172 *
9173 * @param scaleY The scaling factor.
9174 * @see #getPivotX()
9175 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009176 *
9177 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009178 */
9179 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009180 ensureTransformationInfo();
9181 final TransformationInfo info = mTransformationInfo;
9182 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009183 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009184 info.mScaleY = scaleY;
9185 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009186 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009187 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009188 mDisplayList.setScaleY(scaleY);
9189 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009190 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009191 // View was rejected last time it was drawn by its parent; this may have changed
9192 invalidateParentIfNeeded();
9193 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009194 }
9195 }
9196
9197 /**
9198 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9199 * and {@link #setScaleX(float) scaled}.
9200 *
9201 * @see #getRotation()
9202 * @see #getScaleX()
9203 * @see #getScaleY()
9204 * @see #getPivotY()
9205 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009206 *
9207 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009208 */
Chet Haasea5531132012-02-02 13:41:44 -08009209 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009210 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009211 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009212 }
9213
9214 /**
9215 * Sets the x location of the point around which the view is
9216 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009217 * By default, the pivot point is centered on the object.
9218 * Setting this property disables this behavior and causes the view to use only the
9219 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009220 *
9221 * @param pivotX The x location of the pivot point.
9222 * @see #getRotation()
9223 * @see #getScaleX()
9224 * @see #getScaleY()
9225 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009226 *
9227 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009228 */
9229 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009230 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009231 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009232 final TransformationInfo info = mTransformationInfo;
9233 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009234 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009235 info.mPivotX = pivotX;
9236 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009237 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009238 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009239 mDisplayList.setPivotX(pivotX);
9240 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009241 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009242 // View was rejected last time it was drawn by its parent; this may have changed
9243 invalidateParentIfNeeded();
9244 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009245 }
9246 }
9247
9248 /**
9249 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9250 * and {@link #setScaleY(float) scaled}.
9251 *
9252 * @see #getRotation()
9253 * @see #getScaleX()
9254 * @see #getScaleY()
9255 * @see #getPivotY()
9256 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009257 *
9258 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009259 */
Chet Haasea5531132012-02-02 13:41:44 -08009260 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009261 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009262 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009263 }
9264
9265 /**
9266 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009267 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9268 * Setting this property disables this behavior and causes the view to use only the
9269 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009270 *
9271 * @param pivotY The y location of the pivot point.
9272 * @see #getRotation()
9273 * @see #getScaleX()
9274 * @see #getScaleY()
9275 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009276 *
9277 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009278 */
9279 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009280 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009281 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009282 final TransformationInfo info = mTransformationInfo;
9283 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009284 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009285 info.mPivotY = pivotY;
9286 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009287 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009288 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009289 mDisplayList.setPivotY(pivotY);
9290 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009291 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009292 // View was rejected last time it was drawn by its parent; this may have changed
9293 invalidateParentIfNeeded();
9294 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009295 }
9296 }
9297
9298 /**
9299 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9300 * completely transparent and 1 means the view is completely opaque.
9301 *
Joe Onorato93162322010-09-16 15:42:01 -04009302 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009303 * @return The opacity of the view.
9304 */
Chet Haasea5531132012-02-02 13:41:44 -08009305 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009306 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009307 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009308 }
9309
9310 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009311 * Returns whether this View has content which overlaps. This function, intended to be
9312 * overridden by specific View types, is an optimization when alpha is set on a view. If
9313 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9314 * and then composited it into place, which can be expensive. If the view has no overlapping
9315 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9316 * An example of overlapping rendering is a TextView with a background image, such as a
9317 * Button. An example of non-overlapping rendering is a TextView with no background, or
9318 * an ImageView with only the foreground image. The default implementation returns true;
9319 * subclasses should override if they have cases which can be optimized.
9320 *
9321 * @return true if the content in this view might overlap, false otherwise.
9322 */
9323 public boolean hasOverlappingRendering() {
9324 return true;
9325 }
9326
9327 /**
Romain Guy171c5922011-01-06 10:04:23 -08009328 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9329 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009330 *
Romain Guy171c5922011-01-06 10:04:23 -08009331 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9332 * responsible for applying the opacity itself. Otherwise, calling this method is
9333 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009334 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009335 *
Chet Haasea5531132012-02-02 13:41:44 -08009336 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9337 * performance implications. It is generally best to use the alpha property sparingly and
9338 * transiently, as in the case of fading animations.</p>
9339 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009340 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009341 *
Joe Malin32736f02011-01-19 16:14:20 -08009342 * @see #setLayerType(int, android.graphics.Paint)
9343 *
Chet Haase73066682010-11-29 15:55:32 -08009344 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009345 */
9346 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009347 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009348 if (mTransformationInfo.mAlpha != alpha) {
9349 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009350 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009351 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009352 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009353 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009354 invalidate(true);
9355 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009356 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009357 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009358 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009359 mDisplayList.setAlpha(alpha);
9360 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009361 }
Chet Haaseed032702010-10-01 14:05:54 -07009362 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009363 }
9364
9365 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009366 * Faster version of setAlpha() which performs the same steps except there are
9367 * no calls to invalidate(). The caller of this function should perform proper invalidation
9368 * on the parent and this object. The return value indicates whether the subclass handles
9369 * alpha (the return value for onSetAlpha()).
9370 *
9371 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009372 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9373 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009374 */
9375 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009376 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009377 if (mTransformationInfo.mAlpha != alpha) {
9378 mTransformationInfo.mAlpha = alpha;
9379 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9380 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009381 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009382 return true;
9383 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009384 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009385 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009386 mDisplayList.setAlpha(alpha);
9387 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009388 }
Chet Haasea00f3862011-02-22 06:34:40 -08009389 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009390 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009391 }
9392
9393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 * Top position of this view relative to its parent.
9395 *
9396 * @return The top of this view, in pixels.
9397 */
9398 @ViewDebug.CapturedViewProperty
9399 public final int getTop() {
9400 return mTop;
9401 }
9402
9403 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009404 * Sets the top position of this view relative to its parent. This method is meant to be called
9405 * by the layout system and should not generally be called otherwise, because the property
9406 * may be changed at any time by the layout.
9407 *
9408 * @param top The top of this view, in pixels.
9409 */
9410 public final void setTop(int top) {
9411 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009412 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009413 final boolean matrixIsIdentity = mTransformationInfo == null
9414 || mTransformationInfo.mMatrixIsIdentity;
9415 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009416 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009417 int minTop;
9418 int yLoc;
9419 if (top < mTop) {
9420 minTop = top;
9421 yLoc = top - mTop;
9422 } else {
9423 minTop = mTop;
9424 yLoc = 0;
9425 }
Chet Haasee9140a72011-02-16 16:23:29 -08009426 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009427 }
9428 } else {
9429 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009430 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009431 }
9432
Chet Haaseed032702010-10-01 14:05:54 -07009433 int width = mRight - mLeft;
9434 int oldHeight = mBottom - mTop;
9435
Chet Haase21cd1382010-09-01 17:42:29 -07009436 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009437 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009438 mDisplayList.setTop(mTop);
9439 }
Chet Haase21cd1382010-09-01 17:42:29 -07009440
Chet Haaseed032702010-10-01 14:05:54 -07009441 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9442
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009443 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009444 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009445 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009446 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009447 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009448 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009449 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009450 }
Chet Haase55dbb652010-12-21 20:15:08 -08009451 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009452 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009453 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009454 // View was rejected last time it was drawn by its parent; this may have changed
9455 invalidateParentIfNeeded();
9456 }
Chet Haase21cd1382010-09-01 17:42:29 -07009457 }
9458 }
9459
9460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 * Bottom position of this view relative to its parent.
9462 *
9463 * @return The bottom of this view, in pixels.
9464 */
9465 @ViewDebug.CapturedViewProperty
9466 public final int getBottom() {
9467 return mBottom;
9468 }
9469
9470 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009471 * True if this view has changed since the last time being drawn.
9472 *
9473 * @return The dirty state of this view.
9474 */
9475 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009476 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009477 }
9478
9479 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009480 * Sets the bottom position of this view relative to its parent. This method is meant to be
9481 * called by the layout system and should not generally be called otherwise, because the
9482 * property may be changed at any time by the layout.
9483 *
9484 * @param bottom The bottom of this view, in pixels.
9485 */
9486 public final void setBottom(int bottom) {
9487 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009488 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009489 final boolean matrixIsIdentity = mTransformationInfo == null
9490 || mTransformationInfo.mMatrixIsIdentity;
9491 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009492 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009493 int maxBottom;
9494 if (bottom < mBottom) {
9495 maxBottom = mBottom;
9496 } else {
9497 maxBottom = bottom;
9498 }
Chet Haasee9140a72011-02-16 16:23:29 -08009499 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009500 }
9501 } else {
9502 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009503 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009504 }
9505
Chet Haaseed032702010-10-01 14:05:54 -07009506 int width = mRight - mLeft;
9507 int oldHeight = mBottom - mTop;
9508
Chet Haase21cd1382010-09-01 17:42:29 -07009509 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009510 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009511 mDisplayList.setBottom(mBottom);
9512 }
Chet Haase21cd1382010-09-01 17:42:29 -07009513
Chet Haaseed032702010-10-01 14:05:54 -07009514 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9515
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009516 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009517 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009518 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009519 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009520 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009521 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009522 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009523 }
Chet Haase55dbb652010-12-21 20:15:08 -08009524 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009525 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009526 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009527 // View was rejected last time it was drawn by its parent; this may have changed
9528 invalidateParentIfNeeded();
9529 }
Chet Haase21cd1382010-09-01 17:42:29 -07009530 }
9531 }
9532
9533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009534 * Left position of this view relative to its parent.
9535 *
9536 * @return The left edge of this view, in pixels.
9537 */
9538 @ViewDebug.CapturedViewProperty
9539 public final int getLeft() {
9540 return mLeft;
9541 }
9542
9543 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009544 * Sets the left position of this view relative to its parent. This method is meant to be called
9545 * by the layout system and should not generally be called otherwise, because the property
9546 * may be changed at any time by the layout.
9547 *
9548 * @param left The bottom of this view, in pixels.
9549 */
9550 public final void setLeft(int left) {
9551 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009552 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009553 final boolean matrixIsIdentity = mTransformationInfo == null
9554 || mTransformationInfo.mMatrixIsIdentity;
9555 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009556 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009557 int minLeft;
9558 int xLoc;
9559 if (left < mLeft) {
9560 minLeft = left;
9561 xLoc = left - mLeft;
9562 } else {
9563 minLeft = mLeft;
9564 xLoc = 0;
9565 }
Chet Haasee9140a72011-02-16 16:23:29 -08009566 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009567 }
9568 } else {
9569 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009570 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009571 }
9572
Chet Haaseed032702010-10-01 14:05:54 -07009573 int oldWidth = mRight - mLeft;
9574 int height = mBottom - mTop;
9575
Chet Haase21cd1382010-09-01 17:42:29 -07009576 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009577 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009578 mDisplayList.setLeft(left);
9579 }
Chet Haase21cd1382010-09-01 17:42:29 -07009580
Chet Haaseed032702010-10-01 14:05:54 -07009581 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9582
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009583 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009584 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009585 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009586 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009587 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009588 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009589 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009590 }
Chet Haase55dbb652010-12-21 20:15:08 -08009591 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009592 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009593 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009594 // View was rejected last time it was drawn by its parent; this may have changed
9595 invalidateParentIfNeeded();
9596 }
Chet Haase21cd1382010-09-01 17:42:29 -07009597 }
9598 }
9599
9600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601 * Right position of this view relative to its parent.
9602 *
9603 * @return The right edge of this view, in pixels.
9604 */
9605 @ViewDebug.CapturedViewProperty
9606 public final int getRight() {
9607 return mRight;
9608 }
9609
9610 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009611 * Sets the right position of this view relative to its parent. This method is meant to be called
9612 * by the layout system and should not generally be called otherwise, because the property
9613 * may be changed at any time by the layout.
9614 *
9615 * @param right The bottom of this view, in pixels.
9616 */
9617 public final void setRight(int right) {
9618 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009619 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009620 final boolean matrixIsIdentity = mTransformationInfo == null
9621 || mTransformationInfo.mMatrixIsIdentity;
9622 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009623 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009624 int maxRight;
9625 if (right < mRight) {
9626 maxRight = mRight;
9627 } else {
9628 maxRight = right;
9629 }
Chet Haasee9140a72011-02-16 16:23:29 -08009630 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009631 }
9632 } else {
9633 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009634 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009635 }
9636
Chet Haaseed032702010-10-01 14:05:54 -07009637 int oldWidth = mRight - mLeft;
9638 int height = mBottom - mTop;
9639
Chet Haase21cd1382010-09-01 17:42:29 -07009640 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009641 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009642 mDisplayList.setRight(mRight);
9643 }
Chet Haase21cd1382010-09-01 17:42:29 -07009644
Chet Haaseed032702010-10-01 14:05:54 -07009645 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9646
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009647 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009648 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009649 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009650 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009651 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009652 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009653 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009654 }
Chet Haase55dbb652010-12-21 20:15:08 -08009655 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009656 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009657 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009658 // View was rejected last time it was drawn by its parent; this may have changed
9659 invalidateParentIfNeeded();
9660 }
Chet Haase21cd1382010-09-01 17:42:29 -07009661 }
9662 }
9663
9664 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009665 * The visual x position of this view, in pixels. This is equivalent to the
9666 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009667 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009668 *
Chet Haasedf030d22010-07-30 17:22:38 -07009669 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009670 */
Chet Haasea5531132012-02-02 13:41:44 -08009671 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009672 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009673 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009674 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009675
Chet Haasedf030d22010-07-30 17:22:38 -07009676 /**
9677 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9678 * {@link #setTranslationX(float) translationX} property to be the difference between
9679 * the x value passed in and the current {@link #getLeft() left} property.
9680 *
9681 * @param x The visual x position of this view, in pixels.
9682 */
9683 public void setX(float x) {
9684 setTranslationX(x - mLeft);
9685 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009686
Chet Haasedf030d22010-07-30 17:22:38 -07009687 /**
9688 * The visual y position of this view, in pixels. This is equivalent to the
9689 * {@link #setTranslationY(float) translationY} property plus the current
9690 * {@link #getTop() top} property.
9691 *
9692 * @return The visual y position of this view, in pixels.
9693 */
Chet Haasea5531132012-02-02 13:41:44 -08009694 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009695 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009696 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009697 }
9698
9699 /**
9700 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9701 * {@link #setTranslationY(float) translationY} property to be the difference between
9702 * the y value passed in and the current {@link #getTop() top} property.
9703 *
9704 * @param y The visual y position of this view, in pixels.
9705 */
9706 public void setY(float y) {
9707 setTranslationY(y - mTop);
9708 }
9709
9710
9711 /**
9712 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9713 * This position is post-layout, in addition to wherever the object's
9714 * layout placed it.
9715 *
9716 * @return The horizontal position of this view relative to its left position, in pixels.
9717 */
Chet Haasea5531132012-02-02 13:41:44 -08009718 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009719 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009720 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009721 }
9722
9723 /**
9724 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9725 * This effectively positions the object post-layout, in addition to wherever the object's
9726 * layout placed it.
9727 *
9728 * @param translationX The horizontal position of this view relative to its left position,
9729 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009730 *
9731 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009732 */
9733 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009734 ensureTransformationInfo();
9735 final TransformationInfo info = mTransformationInfo;
9736 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009737 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009738 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009739 info.mTranslationX = translationX;
9740 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009741 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009742 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009743 mDisplayList.setTranslationX(translationX);
9744 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009745 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009746 // View was rejected last time it was drawn by its parent; this may have changed
9747 invalidateParentIfNeeded();
9748 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009749 }
9750 }
9751
9752 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009753 * The horizontal location of this view relative to its {@link #getTop() top} position.
9754 * This position is post-layout, in addition to wherever the object's
9755 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009756 *
Chet Haasedf030d22010-07-30 17:22:38 -07009757 * @return The vertical position of this view relative to its top position,
9758 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009759 */
Chet Haasea5531132012-02-02 13:41:44 -08009760 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009761 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009762 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009763 }
9764
9765 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009766 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9767 * This effectively positions the object post-layout, in addition to wherever the object's
9768 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009769 *
Chet Haasedf030d22010-07-30 17:22:38 -07009770 * @param translationY The vertical position of this view relative to its top position,
9771 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009772 *
9773 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009774 */
Chet Haasedf030d22010-07-30 17:22:38 -07009775 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009776 ensureTransformationInfo();
9777 final TransformationInfo info = mTransformationInfo;
9778 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009779 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009780 info.mTranslationY = translationY;
9781 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009782 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009783 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009784 mDisplayList.setTranslationY(translationY);
9785 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009786 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009787 // View was rejected last time it was drawn by its parent; this may have changed
9788 invalidateParentIfNeeded();
9789 }
Chet Haasedf030d22010-07-30 17:22:38 -07009790 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009791 }
9792
9793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 * Hit rectangle in parent's coordinates
9795 *
9796 * @param outRect The hit rectangle of the view.
9797 */
9798 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009799 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009800 final TransformationInfo info = mTransformationInfo;
9801 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009802 outRect.set(mLeft, mTop, mRight, mBottom);
9803 } else {
9804 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009805 tmpRect.set(-info.mPivotX, -info.mPivotY,
9806 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9807 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009808 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9809 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009810 }
9811 }
9812
9813 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009814 * Determines whether the given point, in local coordinates is inside the view.
9815 */
9816 /*package*/ final boolean pointInView(float localX, float localY) {
9817 return localX >= 0 && localX < (mRight - mLeft)
9818 && localY >= 0 && localY < (mBottom - mTop);
9819 }
9820
9821 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009822 * Utility method to determine whether the given point, in local coordinates,
9823 * is inside the view, where the area of the view is expanded by the slop factor.
9824 * This method is called while processing touch-move events to determine if the event
9825 * is still within the view.
9826 */
9827 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009828 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009829 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 }
9831
9832 /**
9833 * When a view has focus and the user navigates away from it, the next view is searched for
9834 * starting from the rectangle filled in by this method.
9835 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009836 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009837 * of the view. However, if your view maintains some idea of internal selection,
9838 * such as a cursor, or a selected row or column, you should override this method and
9839 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 *
9841 * @param r The rectangle to fill in, in this view's coordinates.
9842 */
9843 public void getFocusedRect(Rect r) {
9844 getDrawingRect(r);
9845 }
9846
9847 /**
9848 * If some part of this view is not clipped by any of its parents, then
9849 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009850 * coordinates (without taking possible View rotations into account), offset
9851 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9852 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 *
9854 * @param r If true is returned, r holds the global coordinates of the
9855 * visible portion of this view.
9856 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9857 * between this view and its root. globalOffet may be null.
9858 * @return true if r is non-empty (i.e. part of the view is visible at the
9859 * root level.
9860 */
9861 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9862 int width = mRight - mLeft;
9863 int height = mBottom - mTop;
9864 if (width > 0 && height > 0) {
9865 r.set(0, 0, width, height);
9866 if (globalOffset != null) {
9867 globalOffset.set(-mScrollX, -mScrollY);
9868 }
9869 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9870 }
9871 return false;
9872 }
9873
9874 public final boolean getGlobalVisibleRect(Rect r) {
9875 return getGlobalVisibleRect(r, null);
9876 }
9877
9878 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009879 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 if (getGlobalVisibleRect(r, offset)) {
9881 r.offset(-offset.x, -offset.y); // make r local
9882 return true;
9883 }
9884 return false;
9885 }
9886
9887 /**
9888 * Offset this view's vertical location by the specified number of pixels.
9889 *
9890 * @param offset the number of pixels to offset the view by
9891 */
9892 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009893 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009894 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009895 final boolean matrixIsIdentity = mTransformationInfo == null
9896 || mTransformationInfo.mMatrixIsIdentity;
9897 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009898 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009899 invalidateViewProperty(false, false);
9900 } else {
9901 final ViewParent p = mParent;
9902 if (p != null && mAttachInfo != null) {
9903 final Rect r = mAttachInfo.mTmpInvalRect;
9904 int minTop;
9905 int maxBottom;
9906 int yLoc;
9907 if (offset < 0) {
9908 minTop = mTop + offset;
9909 maxBottom = mBottom;
9910 yLoc = offset;
9911 } else {
9912 minTop = mTop;
9913 maxBottom = mBottom + offset;
9914 yLoc = 0;
9915 }
9916 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9917 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009918 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009919 }
9920 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009921 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009922 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009923
Chet Haasec3aa3612010-06-17 08:50:37 -07009924 mTop += offset;
9925 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009926 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009927 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009928 invalidateViewProperty(false, false);
9929 } else {
9930 if (!matrixIsIdentity) {
9931 invalidateViewProperty(false, true);
9932 }
9933 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009934 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 }
9937
9938 /**
9939 * Offset this view's horizontal location by the specified amount of pixels.
9940 *
9941 * @param offset the numer of pixels to offset the view by
9942 */
9943 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009944 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009945 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009946 final boolean matrixIsIdentity = mTransformationInfo == null
9947 || mTransformationInfo.mMatrixIsIdentity;
9948 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009949 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009950 invalidateViewProperty(false, false);
9951 } else {
9952 final ViewParent p = mParent;
9953 if (p != null && mAttachInfo != null) {
9954 final Rect r = mAttachInfo.mTmpInvalRect;
9955 int minLeft;
9956 int maxRight;
9957 if (offset < 0) {
9958 minLeft = mLeft + offset;
9959 maxRight = mRight;
9960 } else {
9961 minLeft = mLeft;
9962 maxRight = mRight + offset;
9963 }
9964 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9965 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009966 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009967 }
9968 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009969 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009970 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009971
Chet Haasec3aa3612010-06-17 08:50:37 -07009972 mLeft += offset;
9973 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009974 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009975 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009976 invalidateViewProperty(false, false);
9977 } else {
9978 if (!matrixIsIdentity) {
9979 invalidateViewProperty(false, true);
9980 }
9981 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009982 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 }
9985
9986 /**
9987 * Get the LayoutParams associated with this view. All views should have
9988 * layout parameters. These supply parameters to the <i>parent</i> of this
9989 * view specifying how it should be arranged. There are many subclasses of
9990 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9991 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009992 *
9993 * This method may return null if this View is not attached to a parent
9994 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9995 * was not invoked successfully. When a View is attached to a parent
9996 * ViewGroup, this method must not return null.
9997 *
9998 * @return The LayoutParams associated with this view, or null if no
9999 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010001 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 public ViewGroup.LayoutParams getLayoutParams() {
10003 return mLayoutParams;
10004 }
10005
10006 /**
10007 * Set the layout parameters associated with this view. These supply
10008 * parameters to the <i>parent</i> of this view specifying how it should be
10009 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10010 * correspond to the different subclasses of ViewGroup that are responsible
10011 * for arranging their children.
10012 *
Romain Guy01c174b2011-02-22 11:51:06 -080010013 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010014 */
10015 public void setLayoutParams(ViewGroup.LayoutParams params) {
10016 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010017 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010018 }
10019 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010020 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010021 if (mParent instanceof ViewGroup) {
10022 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 requestLayout();
10025 }
10026
10027 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010028 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010029 *
10030 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010031 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010032 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010033 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010034 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010035 }
10036 }
10037
10038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 * Set the scrolled position of your view. This will cause a call to
10040 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10041 * invalidated.
10042 * @param x the x position to scroll to
10043 * @param y the y position to scroll to
10044 */
10045 public void scrollTo(int x, int y) {
10046 if (mScrollX != x || mScrollY != y) {
10047 int oldX = mScrollX;
10048 int oldY = mScrollY;
10049 mScrollX = x;
10050 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010051 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010052 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010053 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010054 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010056 }
10057 }
10058
10059 /**
10060 * Move the scrolled position of your view. This will cause a call to
10061 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10062 * invalidated.
10063 * @param x the amount of pixels to scroll by horizontally
10064 * @param y the amount of pixels to scroll by vertically
10065 */
10066 public void scrollBy(int x, int y) {
10067 scrollTo(mScrollX + x, mScrollY + y);
10068 }
10069
10070 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010071 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10072 * animation to fade the scrollbars out after a default delay. If a subclass
10073 * provides animated scrolling, the start delay should equal the duration
10074 * of the scrolling animation.</p>
10075 *
10076 * <p>The animation starts only if at least one of the scrollbars is
10077 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10078 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10079 * this method returns true, and false otherwise. If the animation is
10080 * started, this method calls {@link #invalidate()}; in that case the
10081 * caller should not call {@link #invalidate()}.</p>
10082 *
10083 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010084 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010085 *
10086 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10087 * and {@link #scrollTo(int, int)}.</p>
10088 *
10089 * @return true if the animation is played, false otherwise
10090 *
10091 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010092 * @see #scrollBy(int, int)
10093 * @see #scrollTo(int, int)
10094 * @see #isHorizontalScrollBarEnabled()
10095 * @see #isVerticalScrollBarEnabled()
10096 * @see #setHorizontalScrollBarEnabled(boolean)
10097 * @see #setVerticalScrollBarEnabled(boolean)
10098 */
10099 protected boolean awakenScrollBars() {
10100 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010101 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010102 }
10103
10104 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010105 * Trigger the scrollbars to draw.
10106 * This method differs from awakenScrollBars() only in its default duration.
10107 * initialAwakenScrollBars() will show the scroll bars for longer than
10108 * usual to give the user more of a chance to notice them.
10109 *
10110 * @return true if the animation is played, false otherwise.
10111 */
10112 private boolean initialAwakenScrollBars() {
10113 return mScrollCache != null &&
10114 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10115 }
10116
10117 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010118 * <p>
10119 * Trigger the scrollbars to draw. When invoked this method starts an
10120 * animation to fade the scrollbars out after a fixed delay. If a subclass
10121 * provides animated scrolling, the start delay should equal the duration of
10122 * the scrolling animation.
10123 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010124 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010125 * <p>
10126 * The animation starts only if at least one of the scrollbars is enabled,
10127 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10128 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10129 * this method returns true, and false otherwise. If the animation is
10130 * started, this method calls {@link #invalidate()}; in that case the caller
10131 * should not call {@link #invalidate()}.
10132 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010133 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010134 * <p>
10135 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010136 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010137 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010138 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010139 * @param startDelay the delay, in milliseconds, after which the animation
10140 * should start; when the delay is 0, the animation starts
10141 * immediately
10142 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010143 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010144 * @see #scrollBy(int, int)
10145 * @see #scrollTo(int, int)
10146 * @see #isHorizontalScrollBarEnabled()
10147 * @see #isVerticalScrollBarEnabled()
10148 * @see #setHorizontalScrollBarEnabled(boolean)
10149 * @see #setVerticalScrollBarEnabled(boolean)
10150 */
10151 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010152 return awakenScrollBars(startDelay, true);
10153 }
Joe Malin32736f02011-01-19 16:14:20 -080010154
Mike Cleron290947b2009-09-29 18:34:32 -070010155 /**
10156 * <p>
10157 * Trigger the scrollbars to draw. When invoked this method starts an
10158 * animation to fade the scrollbars out after a fixed delay. If a subclass
10159 * provides animated scrolling, the start delay should equal the duration of
10160 * the scrolling animation.
10161 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010162 *
Mike Cleron290947b2009-09-29 18:34:32 -070010163 * <p>
10164 * The animation starts only if at least one of the scrollbars is enabled,
10165 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10166 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10167 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010168 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010169 * is set to true; in that case the caller
10170 * should not call {@link #invalidate()}.
10171 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010172 *
Mike Cleron290947b2009-09-29 18:34:32 -070010173 * <p>
10174 * This method should be invoked everytime a subclass directly updates the
10175 * scroll parameters.
10176 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010177 *
Mike Cleron290947b2009-09-29 18:34:32 -070010178 * @param startDelay the delay, in milliseconds, after which the animation
10179 * should start; when the delay is 0, the animation starts
10180 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010181 *
Mike Cleron290947b2009-09-29 18:34:32 -070010182 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010183 *
Mike Cleron290947b2009-09-29 18:34:32 -070010184 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010185 *
Mike Cleron290947b2009-09-29 18:34:32 -070010186 * @see #scrollBy(int, int)
10187 * @see #scrollTo(int, int)
10188 * @see #isHorizontalScrollBarEnabled()
10189 * @see #isVerticalScrollBarEnabled()
10190 * @see #setHorizontalScrollBarEnabled(boolean)
10191 * @see #setVerticalScrollBarEnabled(boolean)
10192 */
10193 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010194 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010195
Mike Cleronf116bf82009-09-27 19:14:12 -070010196 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10197 return false;
10198 }
10199
10200 if (scrollCache.scrollBar == null) {
10201 scrollCache.scrollBar = new ScrollBarDrawable();
10202 }
10203
10204 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10205
Mike Cleron290947b2009-09-29 18:34:32 -070010206 if (invalidate) {
10207 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010208 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010209 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010210
10211 if (scrollCache.state == ScrollabilityCache.OFF) {
10212 // FIXME: this is copied from WindowManagerService.
10213 // We should get this value from the system when it
10214 // is possible to do so.
10215 final int KEY_REPEAT_FIRST_DELAY = 750;
10216 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10217 }
10218
10219 // Tell mScrollCache when we should start fading. This may
10220 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010221 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010222 scrollCache.fadeStartTime = fadeStartTime;
10223 scrollCache.state = ScrollabilityCache.ON;
10224
10225 // Schedule our fader to run, unscheduling any old ones first
10226 if (mAttachInfo != null) {
10227 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10228 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10229 }
10230
10231 return true;
10232 }
10233
10234 return false;
10235 }
10236
10237 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010238 * Do not invalidate views which are not visible and which are not running an animation. They
10239 * will not get drawn and they should not set dirty flags as if they will be drawn
10240 */
10241 private boolean skipInvalidate() {
10242 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10243 (!(mParent instanceof ViewGroup) ||
10244 !((ViewGroup) mParent).isViewTransitioning(this));
10245 }
10246 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010247 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010248 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10249 * in the future. This must be called from a UI thread. To call from a non-UI
10250 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010251 *
10252 * WARNING: This method is destructive to dirty.
10253 * @param dirty the rectangle representing the bounds of the dirty region
10254 */
10255 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010256 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010257 return;
10258 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010259 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10260 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10261 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10262 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10263 mPrivateFlags |= PFLAG_INVALIDATED;
10264 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 final ViewParent p = mParent;
10266 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010267 //noinspection PointlessBooleanExpression,ConstantConditions
10268 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10269 if (p != null && ai != null && ai.mHardwareAccelerated) {
10270 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010271 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010272 p.invalidateChild(this, null);
10273 return;
10274 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 if (p != null && ai != null) {
10277 final int scrollX = mScrollX;
10278 final int scrollY = mScrollY;
10279 final Rect r = ai.mTmpInvalRect;
10280 r.set(dirty.left - scrollX, dirty.top - scrollY,
10281 dirty.right - scrollX, dirty.bottom - scrollY);
10282 mParent.invalidateChild(this, r);
10283 }
10284 }
10285 }
10286
10287 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010288 * 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 -080010289 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010290 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10291 * will be called at some point in the future. This must be called from
10292 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010293 * @param l the left position of the dirty region
10294 * @param t the top position of the dirty region
10295 * @param r the right position of the dirty region
10296 * @param b the bottom position of the dirty region
10297 */
10298 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010299 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010300 return;
10301 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010302 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10303 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10304 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10305 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10306 mPrivateFlags |= PFLAG_INVALIDATED;
10307 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010308 final ViewParent p = mParent;
10309 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010310 //noinspection PointlessBooleanExpression,ConstantConditions
10311 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10312 if (p != null && ai != null && ai.mHardwareAccelerated) {
10313 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010314 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010315 p.invalidateChild(this, null);
10316 return;
10317 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 if (p != null && ai != null && l < r && t < b) {
10320 final int scrollX = mScrollX;
10321 final int scrollY = mScrollY;
10322 final Rect tmpr = ai.mTmpInvalRect;
10323 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10324 p.invalidateChild(this, tmpr);
10325 }
10326 }
10327 }
10328
10329 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010330 * Invalidate the whole view. If the view is visible,
10331 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10332 * the future. This must be called from a UI thread. To call from a non-UI thread,
10333 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 */
10335 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010336 invalidate(true);
10337 }
Joe Malin32736f02011-01-19 16:14:20 -080010338
Chet Haaseed032702010-10-01 14:05:54 -070010339 /**
10340 * This is where the invalidate() work actually happens. A full invalidate()
10341 * causes the drawing cache to be invalidated, but this function can be called with
10342 * invalidateCache set to false to skip that invalidation step for cases that do not
10343 * need it (for example, a component that remains at the same dimensions with the same
10344 * content).
10345 *
10346 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10347 * well. This is usually true for a full invalidate, but may be set to false if the
10348 * View's contents or dimensions have not changed.
10349 */
Romain Guy849d0a32011-02-01 17:20:48 -080010350 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010351 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010352 return;
10353 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010354 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10355 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10356 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010357 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010358 mPrivateFlags &= ~PFLAG_DRAWN;
10359 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010360 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010361 mPrivateFlags |= PFLAG_INVALIDATED;
10362 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010364 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010365 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010366 //noinspection PointlessBooleanExpression,ConstantConditions
10367 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10368 if (p != null && ai != null && ai.mHardwareAccelerated) {
10369 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010370 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010371 p.invalidateChild(this, null);
10372 return;
10373 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010374 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 if (p != null && ai != null) {
10377 final Rect r = ai.mTmpInvalRect;
10378 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10379 // Don't call invalidate -- we don't want to internally scroll
10380 // our own bounds
10381 p.invalidateChild(this, r);
10382 }
10383 }
10384 }
10385
10386 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010387 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10388 * set any flags or handle all of the cases handled by the default invalidation methods.
10389 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10390 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10391 * walk up the hierarchy, transforming the dirty rect as necessary.
10392 *
10393 * The method also handles normal invalidation logic if display list properties are not
10394 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10395 * backup approach, to handle these cases used in the various property-setting methods.
10396 *
10397 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10398 * are not being used in this view
10399 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10400 * list properties are not being used in this view
10401 */
10402 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010403 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010404 if (invalidateParent) {
10405 invalidateParentCaches();
10406 }
10407 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010408 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010409 }
10410 invalidate(false);
10411 } else {
10412 final AttachInfo ai = mAttachInfo;
10413 final ViewParent p = mParent;
10414 if (p != null && ai != null) {
10415 final Rect r = ai.mTmpInvalRect;
10416 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10417 if (mParent instanceof ViewGroup) {
10418 ((ViewGroup) mParent).invalidateChildFast(this, r);
10419 } else {
10420 mParent.invalidateChild(this, r);
10421 }
10422 }
10423 }
10424 }
10425
10426 /**
10427 * Utility method to transform a given Rect by the current matrix of this view.
10428 */
10429 void transformRect(final Rect rect) {
10430 if (!getMatrix().isIdentity()) {
10431 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10432 boundingRect.set(rect);
10433 getMatrix().mapRect(boundingRect);
10434 rect.set((int) (boundingRect.left - 0.5f),
10435 (int) (boundingRect.top - 0.5f),
10436 (int) (boundingRect.right + 0.5f),
10437 (int) (boundingRect.bottom + 0.5f));
10438 }
10439 }
10440
10441 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010442 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010443 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10444 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010445 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10446 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010447 *
10448 * @hide
10449 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010450 protected void invalidateParentCaches() {
10451 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010452 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010453 }
10454 }
Joe Malin32736f02011-01-19 16:14:20 -080010455
Romain Guy0fd89bf2011-01-26 15:41:30 -080010456 /**
10457 * Used to indicate that the parent of this view should be invalidated. This functionality
10458 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10459 * which is necessary when various parent-managed properties of the view change, such as
10460 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10461 * an invalidation event to the parent.
10462 *
10463 * @hide
10464 */
10465 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010466 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010467 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010468 }
10469 }
10470
10471 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010472 * Indicates whether this View is opaque. An opaque View guarantees that it will
10473 * draw all the pixels overlapping its bounds using a fully opaque color.
10474 *
10475 * Subclasses of View should override this method whenever possible to indicate
10476 * whether an instance is opaque. Opaque Views are treated in a special way by
10477 * the View hierarchy, possibly allowing it to perform optimizations during
10478 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010479 *
Romain Guy24443ea2009-05-11 11:56:30 -070010480 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010481 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010482 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010483 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010484 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010485 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010486 }
10487
Adam Powell20232d02010-12-08 21:08:53 -080010488 /**
10489 * @hide
10490 */
10491 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010492 // Opaque if:
10493 // - Has a background
10494 // - Background is opaque
10495 // - Doesn't have scrollbars or scrollbars are inside overlay
10496
Philip Milne6c8ea062012-04-03 17:38:43 -070010497 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010498 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010499 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010500 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010501 }
10502
10503 final int flags = mViewFlags;
10504 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10505 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010506 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010507 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010508 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010509 }
10510 }
10511
10512 /**
10513 * @hide
10514 */
10515 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010516 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010517 }
10518
10519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 * @return A handler associated with the thread running the View. This
10521 * handler can be used to pump events in the UI events queue.
10522 */
10523 public Handler getHandler() {
10524 if (mAttachInfo != null) {
10525 return mAttachInfo.mHandler;
10526 }
10527 return null;
10528 }
10529
10530 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010531 * Gets the view root associated with the View.
10532 * @return The view root, or null if none.
10533 * @hide
10534 */
10535 public ViewRootImpl getViewRootImpl() {
10536 if (mAttachInfo != null) {
10537 return mAttachInfo.mViewRootImpl;
10538 }
10539 return null;
10540 }
10541
10542 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010543 * <p>Causes the Runnable to be added to the message queue.
10544 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 * @param action The Runnable that will be executed.
10547 *
10548 * @return Returns true if the Runnable was successfully placed in to the
10549 * message queue. Returns false on failure, usually because the
10550 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010551 *
10552 * @see #postDelayed
10553 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 */
10555 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010556 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010557 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010558 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010560 // Assume that post will succeed later
10561 ViewRootImpl.getRunQueue().post(action);
10562 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 }
10564
10565 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010566 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010568 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010569 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 * @param action The Runnable that will be executed.
10571 * @param delayMillis The delay (in milliseconds) until the Runnable
10572 * will be executed.
10573 *
10574 * @return true if the Runnable was successfully placed in to the
10575 * message queue. Returns false on failure, usually because the
10576 * looper processing the message queue is exiting. Note that a
10577 * result of true does not mean the Runnable will be processed --
10578 * if the looper is quit before the delivery time of the message
10579 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010580 *
10581 * @see #post
10582 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 */
10584 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010585 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010586 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010587 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010588 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010589 // Assume that post will succeed later
10590 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10591 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 }
10593
10594 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010595 * <p>Causes the Runnable to execute on the next animation time step.
10596 * The runnable will be run on the user interface thread.</p>
10597 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010598 * @param action The Runnable that will be executed.
10599 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010600 * @see #postOnAnimationDelayed
10601 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010602 */
10603 public void postOnAnimation(Runnable action) {
10604 final AttachInfo attachInfo = mAttachInfo;
10605 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010606 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10607 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010608 } else {
10609 // Assume that post will succeed later
10610 ViewRootImpl.getRunQueue().post(action);
10611 }
10612 }
10613
10614 /**
10615 * <p>Causes the Runnable to execute on the next animation time step,
10616 * after the specified amount of time elapses.
10617 * The runnable will be run on the user interface thread.</p>
10618 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010619 * @param action The Runnable that will be executed.
10620 * @param delayMillis The delay (in milliseconds) until the Runnable
10621 * will be executed.
10622 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010623 * @see #postOnAnimation
10624 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010625 */
10626 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10627 final AttachInfo attachInfo = mAttachInfo;
10628 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010629 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10630 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010631 } else {
10632 // Assume that post will succeed later
10633 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10634 }
10635 }
10636
10637 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010638 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 * @param action The Runnable to remove from the message handling queue
10641 *
10642 * @return true if this view could ask the Handler to remove the Runnable,
10643 * false otherwise. When the returned value is true, the Runnable
10644 * may or may not have been actually removed from the message queue
10645 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010646 *
10647 * @see #post
10648 * @see #postDelayed
10649 * @see #postOnAnimation
10650 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 */
10652 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010653 if (action != null) {
10654 final AttachInfo attachInfo = mAttachInfo;
10655 if (attachInfo != null) {
10656 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010657 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10658 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010659 } else {
10660 // Assume that post will succeed later
10661 ViewRootImpl.getRunQueue().removeCallbacks(action);
10662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 return true;
10665 }
10666
10667 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010668 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10669 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010670 *
Romain Guye63a4f32011-08-11 11:33:31 -070010671 * <p>This method can be invoked from outside of the UI thread
10672 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010675 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 */
10677 public void postInvalidate() {
10678 postInvalidateDelayed(0);
10679 }
10680
10681 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010682 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10683 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010684 *
Romain Guye63a4f32011-08-11 11:33:31 -070010685 * <p>This method can be invoked from outside of the UI thread
10686 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 *
10688 * @param left The left coordinate of the rectangle to invalidate.
10689 * @param top The top coordinate of the rectangle to invalidate.
10690 * @param right The right coordinate of the rectangle to invalidate.
10691 * @param bottom The bottom coordinate of the rectangle to invalidate.
10692 *
10693 * @see #invalidate(int, int, int, int)
10694 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010695 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 */
10697 public void postInvalidate(int left, int top, int right, int bottom) {
10698 postInvalidateDelayed(0, left, top, right, bottom);
10699 }
10700
10701 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010702 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10703 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010704 *
Romain Guye63a4f32011-08-11 11:33:31 -070010705 * <p>This method can be invoked from outside of the UI thread
10706 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 *
10708 * @param delayMilliseconds the duration in milliseconds to delay the
10709 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010710 *
10711 * @see #invalidate()
10712 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 */
10714 public void postInvalidateDelayed(long delayMilliseconds) {
10715 // We try only with the AttachInfo because there's no point in invalidating
10716 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010717 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010718 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010719 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 }
10721 }
10722
10723 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010724 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10725 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010726 *
Romain Guye63a4f32011-08-11 11:33:31 -070010727 * <p>This method can be invoked from outside of the UI thread
10728 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 *
10730 * @param delayMilliseconds the duration in milliseconds to delay the
10731 * invalidation by
10732 * @param left The left coordinate of the rectangle to invalidate.
10733 * @param top The top coordinate of the rectangle to invalidate.
10734 * @param right The right coordinate of the rectangle to invalidate.
10735 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010736 *
10737 * @see #invalidate(int, int, int, int)
10738 * @see #invalidate(Rect)
10739 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 */
10741 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10742 int right, int bottom) {
10743
10744 // We try only with the AttachInfo because there's no point in invalidating
10745 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010746 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010747 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010748 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 info.target = this;
10750 info.left = left;
10751 info.top = top;
10752 info.right = right;
10753 info.bottom = bottom;
10754
Jeff Browna175a5b2012-02-15 19:18:31 -080010755 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756 }
10757 }
10758
10759 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010760 * <p>Cause an invalidate to happen on the next animation time step, typically the
10761 * next display frame.</p>
10762 *
10763 * <p>This method can be invoked from outside of the UI thread
10764 * only when this View is attached to a window.</p>
10765 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010766 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010767 */
10768 public void postInvalidateOnAnimation() {
10769 // We try only with the AttachInfo because there's no point in invalidating
10770 // if we are not attached to our window
10771 final AttachInfo attachInfo = mAttachInfo;
10772 if (attachInfo != null) {
10773 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10774 }
10775 }
10776
10777 /**
10778 * <p>Cause an invalidate of the specified area to happen on the next animation
10779 * time step, typically the next display frame.</p>
10780 *
10781 * <p>This method can be invoked from outside of the UI thread
10782 * only when this View is attached to a window.</p>
10783 *
10784 * @param left The left coordinate of the rectangle to invalidate.
10785 * @param top The top coordinate of the rectangle to invalidate.
10786 * @param right The right coordinate of the rectangle to invalidate.
10787 * @param bottom The bottom coordinate of the rectangle to invalidate.
10788 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010789 * @see #invalidate(int, int, int, int)
10790 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010791 */
10792 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10793 // We try only with the AttachInfo because there's no point in invalidating
10794 // if we are not attached to our window
10795 final AttachInfo attachInfo = mAttachInfo;
10796 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010797 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010798 info.target = this;
10799 info.left = left;
10800 info.top = top;
10801 info.right = right;
10802 info.bottom = bottom;
10803
10804 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10805 }
10806 }
10807
10808 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010809 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10810 * This event is sent at most once every
10811 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10812 */
10813 private void postSendViewScrolledAccessibilityEventCallback() {
10814 if (mSendViewScrolledAccessibilityEvent == null) {
10815 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10816 }
10817 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10818 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10819 postDelayed(mSendViewScrolledAccessibilityEvent,
10820 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10821 }
10822 }
10823
10824 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 * Called by a parent to request that a child update its values for mScrollX
10826 * and mScrollY if necessary. This will typically be done if the child is
10827 * animating a scroll using a {@link android.widget.Scroller Scroller}
10828 * object.
10829 */
10830 public void computeScroll() {
10831 }
10832
10833 /**
10834 * <p>Indicate whether the horizontal edges are faded when the view is
10835 * scrolled horizontally.</p>
10836 *
10837 * @return true if the horizontal edges should are faded on scroll, false
10838 * otherwise
10839 *
10840 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010841 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010842 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 */
10844 public boolean isHorizontalFadingEdgeEnabled() {
10845 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10846 }
10847
10848 /**
10849 * <p>Define whether the horizontal edges should be faded when this view
10850 * is scrolled horizontally.</p>
10851 *
10852 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10853 * be faded when the view is scrolled
10854 * horizontally
10855 *
10856 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010857 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010858 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 */
10860 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10861 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10862 if (horizontalFadingEdgeEnabled) {
10863 initScrollCache();
10864 }
10865
10866 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10867 }
10868 }
10869
10870 /**
10871 * <p>Indicate whether the vertical edges are faded when the view is
10872 * scrolled horizontally.</p>
10873 *
10874 * @return true if the vertical edges should are faded on scroll, false
10875 * otherwise
10876 *
10877 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010878 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010879 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 */
10881 public boolean isVerticalFadingEdgeEnabled() {
10882 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10883 }
10884
10885 /**
10886 * <p>Define whether the vertical edges should be faded when this view
10887 * is scrolled vertically.</p>
10888 *
10889 * @param verticalFadingEdgeEnabled true if the vertical edges should
10890 * be faded when the view is scrolled
10891 * vertically
10892 *
10893 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010894 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010895 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010896 */
10897 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10898 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10899 if (verticalFadingEdgeEnabled) {
10900 initScrollCache();
10901 }
10902
10903 mViewFlags ^= FADING_EDGE_VERTICAL;
10904 }
10905 }
10906
10907 /**
10908 * Returns the strength, or intensity, of the top faded edge. The strength is
10909 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10910 * returns 0.0 or 1.0 but no value in between.
10911 *
10912 * Subclasses should override this method to provide a smoother fade transition
10913 * when scrolling occurs.
10914 *
10915 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10916 */
10917 protected float getTopFadingEdgeStrength() {
10918 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10919 }
10920
10921 /**
10922 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10923 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10924 * returns 0.0 or 1.0 but no value in between.
10925 *
10926 * Subclasses should override this method to provide a smoother fade transition
10927 * when scrolling occurs.
10928 *
10929 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10930 */
10931 protected float getBottomFadingEdgeStrength() {
10932 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10933 computeVerticalScrollRange() ? 1.0f : 0.0f;
10934 }
10935
10936 /**
10937 * Returns the strength, or intensity, of the left faded edge. The strength is
10938 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10939 * returns 0.0 or 1.0 but no value in between.
10940 *
10941 * Subclasses should override this method to provide a smoother fade transition
10942 * when scrolling occurs.
10943 *
10944 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10945 */
10946 protected float getLeftFadingEdgeStrength() {
10947 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10948 }
10949
10950 /**
10951 * Returns the strength, or intensity, of the right faded edge. The strength is
10952 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10953 * returns 0.0 or 1.0 but no value in between.
10954 *
10955 * Subclasses should override this method to provide a smoother fade transition
10956 * when scrolling occurs.
10957 *
10958 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10959 */
10960 protected float getRightFadingEdgeStrength() {
10961 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10962 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10963 }
10964
10965 /**
10966 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10967 * scrollbar is not drawn by default.</p>
10968 *
10969 * @return true if the horizontal scrollbar should be painted, false
10970 * otherwise
10971 *
10972 * @see #setHorizontalScrollBarEnabled(boolean)
10973 */
10974 public boolean isHorizontalScrollBarEnabled() {
10975 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10976 }
10977
10978 /**
10979 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10980 * scrollbar is not drawn by default.</p>
10981 *
10982 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10983 * be painted
10984 *
10985 * @see #isHorizontalScrollBarEnabled()
10986 */
10987 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10988 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10989 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010990 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010991 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010992 }
10993 }
10994
10995 /**
10996 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10997 * scrollbar is not drawn by default.</p>
10998 *
10999 * @return true if the vertical scrollbar should be painted, false
11000 * otherwise
11001 *
11002 * @see #setVerticalScrollBarEnabled(boolean)
11003 */
11004 public boolean isVerticalScrollBarEnabled() {
11005 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11006 }
11007
11008 /**
11009 * <p>Define whether the vertical scrollbar should be drawn or not. The
11010 * scrollbar is not drawn by default.</p>
11011 *
11012 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11013 * be painted
11014 *
11015 * @see #isVerticalScrollBarEnabled()
11016 */
11017 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11018 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11019 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011020 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011021 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 }
11023 }
11024
Adam Powell20232d02010-12-08 21:08:53 -080011025 /**
11026 * @hide
11027 */
11028 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011029 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 }
Joe Malin32736f02011-01-19 16:14:20 -080011031
Mike Cleronfe81d382009-09-28 14:22:16 -070011032 /**
11033 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011034 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011035 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011036 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011037 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011038 */
11039 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11040 initScrollCache();
11041 final ScrollabilityCache scrollabilityCache = mScrollCache;
11042 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011043 if (fadeScrollbars) {
11044 scrollabilityCache.state = ScrollabilityCache.OFF;
11045 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011046 scrollabilityCache.state = ScrollabilityCache.ON;
11047 }
11048 }
Joe Malin32736f02011-01-19 16:14:20 -080011049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011050 /**
Joe Malin32736f02011-01-19 16:14:20 -080011051 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011052 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011053 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011054 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011055 *
11056 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011057 */
11058 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011059 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011060 }
Joe Malin32736f02011-01-19 16:14:20 -080011061
Mike Cleron52f0a642009-09-28 18:21:37 -070011062 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011063 *
11064 * Returns the delay before scrollbars fade.
11065 *
11066 * @return the delay before scrollbars fade
11067 *
11068 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11069 */
11070 public int getScrollBarDefaultDelayBeforeFade() {
11071 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11072 mScrollCache.scrollBarDefaultDelayBeforeFade;
11073 }
11074
11075 /**
11076 * Define the delay before scrollbars fade.
11077 *
11078 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11079 *
11080 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11081 */
11082 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11083 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11084 }
11085
11086 /**
11087 *
11088 * Returns the scrollbar fade duration.
11089 *
11090 * @return the scrollbar fade duration
11091 *
11092 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11093 */
11094 public int getScrollBarFadeDuration() {
11095 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11096 mScrollCache.scrollBarFadeDuration;
11097 }
11098
11099 /**
11100 * Define the scrollbar fade duration.
11101 *
11102 * @param scrollBarFadeDuration - the scrollbar fade duration
11103 *
11104 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11105 */
11106 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11107 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11108 }
11109
11110 /**
11111 *
11112 * Returns the scrollbar size.
11113 *
11114 * @return the scrollbar size
11115 *
11116 * @attr ref android.R.styleable#View_scrollbarSize
11117 */
11118 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011119 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011120 mScrollCache.scrollBarSize;
11121 }
11122
11123 /**
11124 * Define the scrollbar size.
11125 *
11126 * @param scrollBarSize - the scrollbar size
11127 *
11128 * @attr ref android.R.styleable#View_scrollbarSize
11129 */
11130 public void setScrollBarSize(int scrollBarSize) {
11131 getScrollCache().scrollBarSize = scrollBarSize;
11132 }
11133
11134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011135 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11136 * inset. When inset, they add to the padding of the view. And the scrollbars
11137 * can be drawn inside the padding area or on the edge of the view. For example,
11138 * if a view has a background drawable and you want to draw the scrollbars
11139 * inside the padding specified by the drawable, you can use
11140 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11141 * appear at the edge of the view, ignoring the padding, then you can use
11142 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11143 * @param style the style of the scrollbars. Should be one of
11144 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11145 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11146 * @see #SCROLLBARS_INSIDE_OVERLAY
11147 * @see #SCROLLBARS_INSIDE_INSET
11148 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11149 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011150 *
11151 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 */
11153 public void setScrollBarStyle(int style) {
11154 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11155 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011156 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011157 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011158 }
11159 }
11160
11161 /**
11162 * <p>Returns the current scrollbar style.</p>
11163 * @return the current scrollbar style
11164 * @see #SCROLLBARS_INSIDE_OVERLAY
11165 * @see #SCROLLBARS_INSIDE_INSET
11166 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11167 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011168 *
11169 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011171 @ViewDebug.ExportedProperty(mapping = {
11172 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11173 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11174 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11175 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11176 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011177 public int getScrollBarStyle() {
11178 return mViewFlags & SCROLLBARS_STYLE_MASK;
11179 }
11180
11181 /**
11182 * <p>Compute the horizontal range that the horizontal scrollbar
11183 * represents.</p>
11184 *
11185 * <p>The range is expressed in arbitrary units that must be the same as the
11186 * units used by {@link #computeHorizontalScrollExtent()} and
11187 * {@link #computeHorizontalScrollOffset()}.</p>
11188 *
11189 * <p>The default range is the drawing width of this view.</p>
11190 *
11191 * @return the total horizontal range represented by the horizontal
11192 * scrollbar
11193 *
11194 * @see #computeHorizontalScrollExtent()
11195 * @see #computeHorizontalScrollOffset()
11196 * @see android.widget.ScrollBarDrawable
11197 */
11198 protected int computeHorizontalScrollRange() {
11199 return getWidth();
11200 }
11201
11202 /**
11203 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11204 * within the horizontal range. This value is used to compute the position
11205 * of the thumb within the scrollbar's track.</p>
11206 *
11207 * <p>The range is expressed in arbitrary units that must be the same as the
11208 * units used by {@link #computeHorizontalScrollRange()} and
11209 * {@link #computeHorizontalScrollExtent()}.</p>
11210 *
11211 * <p>The default offset is the scroll offset of this view.</p>
11212 *
11213 * @return the horizontal offset of the scrollbar's thumb
11214 *
11215 * @see #computeHorizontalScrollRange()
11216 * @see #computeHorizontalScrollExtent()
11217 * @see android.widget.ScrollBarDrawable
11218 */
11219 protected int computeHorizontalScrollOffset() {
11220 return mScrollX;
11221 }
11222
11223 /**
11224 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11225 * within the horizontal range. This value is used to compute the length
11226 * of the thumb within the scrollbar's track.</p>
11227 *
11228 * <p>The range is expressed in arbitrary units that must be the same as the
11229 * units used by {@link #computeHorizontalScrollRange()} and
11230 * {@link #computeHorizontalScrollOffset()}.</p>
11231 *
11232 * <p>The default extent is the drawing width of this view.</p>
11233 *
11234 * @return the horizontal extent of the scrollbar's thumb
11235 *
11236 * @see #computeHorizontalScrollRange()
11237 * @see #computeHorizontalScrollOffset()
11238 * @see android.widget.ScrollBarDrawable
11239 */
11240 protected int computeHorizontalScrollExtent() {
11241 return getWidth();
11242 }
11243
11244 /**
11245 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11246 *
11247 * <p>The range is expressed in arbitrary units that must be the same as the
11248 * units used by {@link #computeVerticalScrollExtent()} and
11249 * {@link #computeVerticalScrollOffset()}.</p>
11250 *
11251 * @return the total vertical range represented by the vertical scrollbar
11252 *
11253 * <p>The default range is the drawing height of this view.</p>
11254 *
11255 * @see #computeVerticalScrollExtent()
11256 * @see #computeVerticalScrollOffset()
11257 * @see android.widget.ScrollBarDrawable
11258 */
11259 protected int computeVerticalScrollRange() {
11260 return getHeight();
11261 }
11262
11263 /**
11264 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11265 * within the horizontal range. This value is used to compute the position
11266 * of the thumb within the scrollbar's track.</p>
11267 *
11268 * <p>The range is expressed in arbitrary units that must be the same as the
11269 * units used by {@link #computeVerticalScrollRange()} and
11270 * {@link #computeVerticalScrollExtent()}.</p>
11271 *
11272 * <p>The default offset is the scroll offset of this view.</p>
11273 *
11274 * @return the vertical offset of the scrollbar's thumb
11275 *
11276 * @see #computeVerticalScrollRange()
11277 * @see #computeVerticalScrollExtent()
11278 * @see android.widget.ScrollBarDrawable
11279 */
11280 protected int computeVerticalScrollOffset() {
11281 return mScrollY;
11282 }
11283
11284 /**
11285 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11286 * within the vertical range. This value is used to compute the length
11287 * of the thumb within the scrollbar's track.</p>
11288 *
11289 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011290 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011291 * {@link #computeVerticalScrollOffset()}.</p>
11292 *
11293 * <p>The default extent is the drawing height of this view.</p>
11294 *
11295 * @return the vertical extent of the scrollbar's thumb
11296 *
11297 * @see #computeVerticalScrollRange()
11298 * @see #computeVerticalScrollOffset()
11299 * @see android.widget.ScrollBarDrawable
11300 */
11301 protected int computeVerticalScrollExtent() {
11302 return getHeight();
11303 }
11304
11305 /**
Adam Powell69159442011-06-13 17:53:06 -070011306 * Check if this view can be scrolled horizontally in a certain direction.
11307 *
11308 * @param direction Negative to check scrolling left, positive to check scrolling right.
11309 * @return true if this view can be scrolled in the specified direction, false otherwise.
11310 */
11311 public boolean canScrollHorizontally(int direction) {
11312 final int offset = computeHorizontalScrollOffset();
11313 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11314 if (range == 0) return false;
11315 if (direction < 0) {
11316 return offset > 0;
11317 } else {
11318 return offset < range - 1;
11319 }
11320 }
11321
11322 /**
11323 * Check if this view can be scrolled vertically in a certain direction.
11324 *
11325 * @param direction Negative to check scrolling up, positive to check scrolling down.
11326 * @return true if this view can be scrolled in the specified direction, false otherwise.
11327 */
11328 public boolean canScrollVertically(int direction) {
11329 final int offset = computeVerticalScrollOffset();
11330 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11331 if (range == 0) return false;
11332 if (direction < 0) {
11333 return offset > 0;
11334 } else {
11335 return offset < range - 1;
11336 }
11337 }
11338
11339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11341 * scrollbars are painted only if they have been awakened first.</p>
11342 *
11343 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011344 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011345 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011346 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011347 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011348 // scrollbars are drawn only when the animation is running
11349 final ScrollabilityCache cache = mScrollCache;
11350 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011351
Mike Cleronf116bf82009-09-27 19:14:12 -070011352 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011353
Mike Cleronf116bf82009-09-27 19:14:12 -070011354 if (state == ScrollabilityCache.OFF) {
11355 return;
11356 }
Joe Malin32736f02011-01-19 16:14:20 -080011357
Mike Cleronf116bf82009-09-27 19:14:12 -070011358 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011359
Mike Cleronf116bf82009-09-27 19:14:12 -070011360 if (state == ScrollabilityCache.FADING) {
11361 // We're fading -- get our fade interpolation
11362 if (cache.interpolatorValues == null) {
11363 cache.interpolatorValues = new float[1];
11364 }
Joe Malin32736f02011-01-19 16:14:20 -080011365
Mike Cleronf116bf82009-09-27 19:14:12 -070011366 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011367
Mike Cleronf116bf82009-09-27 19:14:12 -070011368 // Stops the animation if we're done
11369 if (cache.scrollBarInterpolator.timeToValues(values) ==
11370 Interpolator.Result.FREEZE_END) {
11371 cache.state = ScrollabilityCache.OFF;
11372 } else {
11373 cache.scrollBar.setAlpha(Math.round(values[0]));
11374 }
Joe Malin32736f02011-01-19 16:14:20 -080011375
11376 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011377 // drawing. We only want this when we're fading so that
11378 // we prevent excessive redraws
11379 invalidate = true;
11380 } else {
11381 // We're just on -- but we may have been fading before so
11382 // reset alpha
11383 cache.scrollBar.setAlpha(255);
11384 }
11385
Joe Malin32736f02011-01-19 16:14:20 -080011386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 final int viewFlags = mViewFlags;
11388
11389 final boolean drawHorizontalScrollBar =
11390 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11391 final boolean drawVerticalScrollBar =
11392 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11393 && !isVerticalScrollBarHidden();
11394
11395 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11396 final int width = mRight - mLeft;
11397 final int height = mBottom - mTop;
11398
11399 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011400
Mike Reede8853fc2009-09-04 14:01:48 -040011401 final int scrollX = mScrollX;
11402 final int scrollY = mScrollY;
11403 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11404
Mike Cleronf116bf82009-09-27 19:14:12 -070011405 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011407 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011408 int size = scrollBar.getSize(false);
11409 if (size <= 0) {
11410 size = cache.scrollBarSize;
11411 }
11412
Mike Cleronf116bf82009-09-27 19:14:12 -070011413 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011414 computeHorizontalScrollOffset(),
11415 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011416 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011417 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011418 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011419 left = scrollX + (mPaddingLeft & inside);
11420 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11421 bottom = top + size;
11422 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11423 if (invalidate) {
11424 invalidate(left, top, right, bottom);
11425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426 }
11427
11428 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011429 int size = scrollBar.getSize(true);
11430 if (size <= 0) {
11431 size = cache.scrollBarSize;
11432 }
11433
Mike Reede8853fc2009-09-04 14:01:48 -040011434 scrollBar.setParameters(computeVerticalScrollRange(),
11435 computeVerticalScrollOffset(),
11436 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011437 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11438 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11439 verticalScrollbarPosition = isLayoutRtl() ?
11440 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11441 }
11442 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011443 default:
Adam Powell20232d02010-12-08 21:08:53 -080011444 case SCROLLBAR_POSITION_RIGHT:
11445 left = scrollX + width - size - (mUserPaddingRight & inside);
11446 break;
11447 case SCROLLBAR_POSITION_LEFT:
11448 left = scrollX + (mUserPaddingLeft & inside);
11449 break;
11450 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011451 top = scrollY + (mPaddingTop & inside);
11452 right = left + size;
11453 bottom = scrollY + height - (mUserPaddingBottom & inside);
11454 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11455 if (invalidate) {
11456 invalidate(left, top, right, bottom);
11457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011458 }
11459 }
11460 }
11461 }
Romain Guy8506ab42009-06-11 17:35:47 -070011462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011464 * 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 -080011465 * FastScroller is visible.
11466 * @return whether to temporarily hide the vertical scrollbar
11467 * @hide
11468 */
11469 protected boolean isVerticalScrollBarHidden() {
11470 return false;
11471 }
11472
11473 /**
11474 * <p>Draw the horizontal scrollbar if
11475 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011477 * @param canvas the canvas on which to draw the scrollbar
11478 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 *
11480 * @see #isHorizontalScrollBarEnabled()
11481 * @see #computeHorizontalScrollRange()
11482 * @see #computeHorizontalScrollExtent()
11483 * @see #computeHorizontalScrollOffset()
11484 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011485 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011486 */
Romain Guy8fb95422010-08-17 18:38:51 -070011487 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11488 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011489 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011490 scrollBar.draw(canvas);
11491 }
Mike Reede8853fc2009-09-04 14:01:48 -040011492
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011494 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11495 * returns true.</p>
11496 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011497 * @param canvas the canvas on which to draw the scrollbar
11498 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499 *
11500 * @see #isVerticalScrollBarEnabled()
11501 * @see #computeVerticalScrollRange()
11502 * @see #computeVerticalScrollExtent()
11503 * @see #computeVerticalScrollOffset()
11504 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011505 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011506 */
Romain Guy8fb95422010-08-17 18:38:51 -070011507 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11508 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011509 scrollBar.setBounds(l, t, r, b);
11510 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 }
11512
11513 /**
11514 * Implement this to do your drawing.
11515 *
11516 * @param canvas the canvas on which the background will be drawn
11517 */
11518 protected void onDraw(Canvas canvas) {
11519 }
11520
11521 /*
11522 * Caller is responsible for calling requestLayout if necessary.
11523 * (This allows addViewInLayout to not request a new layout.)
11524 */
11525 void assignParent(ViewParent parent) {
11526 if (mParent == null) {
11527 mParent = parent;
11528 } else if (parent == null) {
11529 mParent = null;
11530 } else {
11531 throw new RuntimeException("view " + this + " being added, but"
11532 + " it already has a parent");
11533 }
11534 }
11535
11536 /**
11537 * This is called when the view is attached to a window. At this point it
11538 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011539 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11540 * however it may be called any time before the first onDraw -- including
11541 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011542 *
11543 * @see #onDetachedFromWindow()
11544 */
11545 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011546 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 mParent.requestTransparentRegion(this);
11548 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011549
Dianne Hackborn4702a852012-08-17 15:18:29 -070011550 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011551 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011552 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011553 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011554
Chet Haasea9b61ac2010-12-20 07:40:25 -080011555 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011556
Svetoslav Ganov42138042012-03-20 11:51:39 -070011557 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011558 if (isFocused()) {
11559 InputMethodManager imm = InputMethodManager.peekInstance();
11560 imm.focusIn(this);
11561 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011562
11563 if (mAttachInfo != null && mDisplayList != null) {
11564 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11565 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011566 }
Cibu Johny86666632010-02-22 13:01:02 -080011567
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011568 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011569 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011570 *
11571 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011572 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011573 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011574 if (!needRtlPropertiesResolution()) return;
11575
11576 // Order is important here: LayoutDirection MUST be resolved first
11577 if (!isLayoutDirectionResolved()) {
11578 resolveLayoutDirection();
11579 resolveLayoutParams();
11580 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011581 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011582 if (!isTextDirectionResolved()) {
11583 resolveTextDirection();
11584 }
11585 if (!isTextAlignmentResolved()) {
11586 resolveTextAlignment();
11587 }
11588 if (!isPaddingResolved()) {
11589 resolvePadding();
11590 }
11591 if (!isDrawablesResolved()) {
11592 resolveDrawables();
11593 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011594 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011595 }
11596
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011597 /**
11598 * Reset resolution of all RTL related properties.
11599 *
11600 * @hide
11601 */
11602 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011603 resetResolvedLayoutDirection();
11604 resetResolvedTextDirection();
11605 resetResolvedTextAlignment();
11606 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011607 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011608 }
11609
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011610 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011611 * @see #onScreenStateChanged(int)
11612 */
11613 void dispatchScreenStateChanged(int screenState) {
11614 onScreenStateChanged(screenState);
11615 }
11616
11617 /**
11618 * This method is called whenever the state of the screen this view is
11619 * attached to changes. A state change will usually occurs when the screen
11620 * turns on or off (whether it happens automatically or the user does it
11621 * manually.)
11622 *
11623 * @param screenState The new state of the screen. Can be either
11624 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11625 */
11626 public void onScreenStateChanged(int screenState) {
11627 }
11628
11629 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011630 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11631 */
11632 private boolean hasRtlSupport() {
11633 return mContext.getApplicationInfo().hasRtlSupport();
11634 }
11635
11636 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011637 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11638 * RTL not supported)
11639 */
11640 private boolean isRtlCompatibilityMode() {
11641 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11642 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11643 }
11644
11645 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011646 * @return true if RTL properties need resolution.
11647 */
11648 private boolean needRtlPropertiesResolution() {
11649 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11650 }
11651
11652 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011653 * Called when any RTL property (layout direction or text direction or text alignment) has
11654 * been changed.
11655 *
11656 * Subclasses need to override this method to take care of cached information that depends on the
11657 * resolved layout direction, or to inform child views that inherit their layout direction.
11658 *
11659 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011660 *
11661 * @param layoutDirection the direction of the layout
11662 *
11663 * @see #LAYOUT_DIRECTION_LTR
11664 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011665 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011666 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011667 }
11668
11669 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011670 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11671 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011672 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011673 * @return true if resolution has been done, false otherwise.
11674 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011675 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011676 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011677 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011678 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011679 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011680
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011681 if (hasRtlSupport()) {
11682 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011683 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11684 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011685 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011686 // We cannot resolve yet. LTR is by default and let the resolution happen again
11687 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011688 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011689
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011690 View parent = ((View) mParent);
11691 // Parent has not yet resolved, LTR is still the default
11692 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011693
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011694 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011695 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011696 }
11697 break;
11698 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011699 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011700 break;
11701 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011702 if((LAYOUT_DIRECTION_RTL ==
11703 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011704 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011705 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011706 break;
11707 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011708 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011709 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011710 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011711
11712 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011713 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011714 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011715 }
11716
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011717 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011718 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011719 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011720 * @return true if layout direction resolution can be done otherwise return false.
11721 *
11722 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011723 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011724 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011725 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011726 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011727 return (mParent != null) && (mParent instanceof ViewGroup) &&
11728 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011729 default:
11730 return true;
11731 }
11732 }
11733
11734 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011735 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11736 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011737 *
11738 * @hide
11739 */
11740 public void resetResolvedLayoutDirection() {
11741 // Reset the current resolved bits
11742 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011743 }
11744
11745 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011746 * @return true if the layout direction is inherited.
11747 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011748 * @hide
11749 */
11750 public boolean isLayoutDirectionInherited() {
11751 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11752 }
11753
11754 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011755 * @return true if layout direction has been resolved.
11756 */
11757 private boolean isLayoutDirectionResolved() {
11758 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11759 }
11760
11761 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011762 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011763 *
11764 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011765 */
11766 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011767 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011768 }
11769
11770 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011771 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011772 *
11773 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011774 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011775 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011776 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011777 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11778 // If start / end padding are defined, they will be resolved (hence overriding) to
11779 // left / right or right / left depending on the resolved layout direction.
11780 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011781 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011782 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011783 mUserPaddingLeft = mUserPaddingLeftInitial;
11784 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011785 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011786 switch (resolvedLayoutDirection) {
11787 case LAYOUT_DIRECTION_RTL:
11788 if (mUserPaddingStart != UNDEFINED_PADDING) {
11789 mUserPaddingRight = mUserPaddingStart;
11790 }
11791 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11792 mUserPaddingLeft = mUserPaddingEnd;
11793 }
11794 break;
11795 case LAYOUT_DIRECTION_LTR:
11796 default:
11797 if (mUserPaddingStart != UNDEFINED_PADDING) {
11798 mUserPaddingLeft = mUserPaddingStart;
11799 }
11800 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11801 mUserPaddingRight = mUserPaddingEnd;
11802 }
11803 }
11804
11805 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11806
11807 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11808 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011809 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011810 }
11811
Dianne Hackborn4702a852012-08-17 15:18:29 -070011812 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011813 }
11814
11815 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011816 * Reset the resolved layout direction.
11817 *
11818 * @hide
11819 */
11820 public void resetResolvedPadding() {
11821 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11822 }
11823
11824 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011825 * This is called when the view is detached from a window. At this point it
11826 * no longer has a surface for drawing.
11827 *
11828 * @see #onAttachedToWindow()
11829 */
11830 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011831 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011832
Romain Guya440b002010-02-24 15:57:54 -080011833 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011834 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011835 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011836 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011838 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011839
Romain Guya998dff2012-03-23 18:58:36 -070011840 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011841
11842 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011843 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011844 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011845 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011846 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011847 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011848 // Should never happen
11849 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011850 }
11851
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011852 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011853
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011854 resetRtlProperties();
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011855 onRtlPropertiesChanged(LAYOUT_DIRECTION_DEFAULT);
Svetoslav Ganov42138042012-03-20 11:51:39 -070011856 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 }
11858
11859 /**
11860 * @return The number of times this view has been attached to a window
11861 */
11862 protected int getWindowAttachCount() {
11863 return mWindowAttachCount;
11864 }
11865
11866 /**
11867 * Retrieve a unique token identifying the window this view is attached to.
11868 * @return Return the window's token for use in
11869 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11870 */
11871 public IBinder getWindowToken() {
11872 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11873 }
11874
11875 /**
11876 * Retrieve a unique token identifying the top-level "real" window of
11877 * the window that this view is attached to. That is, this is like
11878 * {@link #getWindowToken}, except if the window this view in is a panel
11879 * window (attached to another containing window), then the token of
11880 * the containing window is returned instead.
11881 *
11882 * @return Returns the associated window token, either
11883 * {@link #getWindowToken()} or the containing window's token.
11884 */
11885 public IBinder getApplicationWindowToken() {
11886 AttachInfo ai = mAttachInfo;
11887 if (ai != null) {
11888 IBinder appWindowToken = ai.mPanelParentWindowToken;
11889 if (appWindowToken == null) {
11890 appWindowToken = ai.mWindowToken;
11891 }
11892 return appWindowToken;
11893 }
11894 return null;
11895 }
11896
11897 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011898 * Gets the logical display to which the view's window has been attached.
11899 *
11900 * @return The logical display, or null if the view is not currently attached to a window.
11901 */
11902 public Display getDisplay() {
11903 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11904 }
11905
11906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011907 * Retrieve private session object this view hierarchy is using to
11908 * communicate with the window manager.
11909 * @return the session object to communicate with the window manager
11910 */
11911 /*package*/ IWindowSession getWindowSession() {
11912 return mAttachInfo != null ? mAttachInfo.mSession : null;
11913 }
11914
11915 /**
11916 * @param info the {@link android.view.View.AttachInfo} to associated with
11917 * this view
11918 */
11919 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11920 //System.out.println("Attached! " + this);
11921 mAttachInfo = info;
11922 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011923 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011924 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 if (mFloatingTreeObserver != null) {
11926 info.mTreeObserver.merge(mFloatingTreeObserver);
11927 mFloatingTreeObserver = null;
11928 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011929 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011930 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011931 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011932 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011933 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011935
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011936 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011937 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011938 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011939 if (listeners != null && listeners.size() > 0) {
11940 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11941 // perform the dispatching. The iterator is a safe guard against listeners that
11942 // could mutate the list by calling the various add/remove methods. This prevents
11943 // the array from being modified while we iterate it.
11944 for (OnAttachStateChangeListener listener : listeners) {
11945 listener.onViewAttachedToWindow(this);
11946 }
11947 }
11948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011949 int vis = info.mWindowVisibility;
11950 if (vis != GONE) {
11951 onWindowVisibilityChanged(vis);
11952 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011953 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011954 // If nobody has evaluated the drawable state yet, then do it now.
11955 refreshDrawableState();
11956 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011957 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011958 }
11959
11960 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011961 AttachInfo info = mAttachInfo;
11962 if (info != null) {
11963 int vis = info.mWindowVisibility;
11964 if (vis != GONE) {
11965 onWindowVisibilityChanged(GONE);
11966 }
11967 }
11968
11969 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011970
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011971 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011972 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011973 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011974 if (listeners != null && listeners.size() > 0) {
11975 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11976 // perform the dispatching. The iterator is a safe guard against listeners that
11977 // could mutate the list by calling the various add/remove methods. This prevents
11978 // the array from being modified while we iterate it.
11979 for (OnAttachStateChangeListener listener : listeners) {
11980 listener.onViewDetachedFromWindow(this);
11981 }
11982 }
11983
Dianne Hackborn4702a852012-08-17 15:18:29 -070011984 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011985 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011986 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 mAttachInfo = null;
11990 }
11991
11992 /**
11993 * Store this view hierarchy's frozen state into the given container.
11994 *
11995 * @param container The SparseArray in which to save the view's state.
11996 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011997 * @see #restoreHierarchyState(android.util.SparseArray)
11998 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11999 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 */
12001 public void saveHierarchyState(SparseArray<Parcelable> container) {
12002 dispatchSaveInstanceState(container);
12003 }
12004
12005 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012006 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12007 * this view and its children. May be overridden to modify how freezing happens to a
12008 * 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 -080012009 *
12010 * @param container The SparseArray in which to save the view's state.
12011 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012012 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12013 * @see #saveHierarchyState(android.util.SparseArray)
12014 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012015 */
12016 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12017 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012018 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012019 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012020 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012021 throw new IllegalStateException(
12022 "Derived class did not call super.onSaveInstanceState()");
12023 }
12024 if (state != null) {
12025 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12026 // + ": " + state);
12027 container.put(mID, state);
12028 }
12029 }
12030 }
12031
12032 /**
12033 * Hook allowing a view to generate a representation of its internal state
12034 * that can later be used to create a new instance with that same state.
12035 * This state should only contain information that is not persistent or can
12036 * not be reconstructed later. For example, you will never store your
12037 * current position on screen because that will be computed again when a
12038 * new instance of the view is placed in its view hierarchy.
12039 * <p>
12040 * Some examples of things you may store here: the current cursor position
12041 * in a text view (but usually not the text itself since that is stored in a
12042 * content provider or other persistent storage), the currently selected
12043 * item in a list view.
12044 *
12045 * @return Returns a Parcelable object containing the view's current dynamic
12046 * state, or null if there is nothing interesting to save. The
12047 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012048 * @see #onRestoreInstanceState(android.os.Parcelable)
12049 * @see #saveHierarchyState(android.util.SparseArray)
12050 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012051 * @see #setSaveEnabled(boolean)
12052 */
12053 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012054 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012055 return BaseSavedState.EMPTY_STATE;
12056 }
12057
12058 /**
12059 * Restore this view hierarchy's frozen state from the given container.
12060 *
12061 * @param container The SparseArray which holds previously frozen states.
12062 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012063 * @see #saveHierarchyState(android.util.SparseArray)
12064 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12065 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 */
12067 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12068 dispatchRestoreInstanceState(container);
12069 }
12070
12071 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012072 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12073 * state for this view and its children. May be overridden to modify how restoring
12074 * happens to a view's children; for example, some views may want to not store state
12075 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076 *
12077 * @param container The SparseArray which holds previously saved state.
12078 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012079 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12080 * @see #restoreHierarchyState(android.util.SparseArray)
12081 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012082 */
12083 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12084 if (mID != NO_ID) {
12085 Parcelable state = container.get(mID);
12086 if (state != null) {
12087 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12088 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012089 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012091 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012092 throw new IllegalStateException(
12093 "Derived class did not call super.onRestoreInstanceState()");
12094 }
12095 }
12096 }
12097 }
12098
12099 /**
12100 * Hook allowing a view to re-apply a representation of its internal state that had previously
12101 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12102 * null state.
12103 *
12104 * @param state The frozen state that had previously been returned by
12105 * {@link #onSaveInstanceState}.
12106 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012107 * @see #onSaveInstanceState()
12108 * @see #restoreHierarchyState(android.util.SparseArray)
12109 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012110 */
12111 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012112 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012114 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12115 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012116 + "when two views of different type have the same id in the same hierarchy. "
12117 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012118 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012119 }
12120 }
12121
12122 /**
12123 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12124 *
12125 * @return the drawing start time in milliseconds
12126 */
12127 public long getDrawingTime() {
12128 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12129 }
12130
12131 /**
12132 * <p>Enables or disables the duplication of the parent's state into this view. When
12133 * duplication is enabled, this view gets its drawable state from its parent rather
12134 * than from its own internal properties.</p>
12135 *
12136 * <p>Note: in the current implementation, setting this property to true after the
12137 * view was added to a ViewGroup might have no effect at all. This property should
12138 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12139 *
12140 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12141 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012142 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012143 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12144 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012145 *
12146 * @param enabled True to enable duplication of the parent's drawable state, false
12147 * to disable it.
12148 *
12149 * @see #getDrawableState()
12150 * @see #isDuplicateParentStateEnabled()
12151 */
12152 public void setDuplicateParentStateEnabled(boolean enabled) {
12153 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12154 }
12155
12156 /**
12157 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12158 *
12159 * @return True if this view's drawable state is duplicated from the parent,
12160 * false otherwise
12161 *
12162 * @see #getDrawableState()
12163 * @see #setDuplicateParentStateEnabled(boolean)
12164 */
12165 public boolean isDuplicateParentStateEnabled() {
12166 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12167 }
12168
12169 /**
Romain Guy171c5922011-01-06 10:04:23 -080012170 * <p>Specifies the type of layer backing this view. The layer can be
12171 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12172 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012173 *
Romain Guy171c5922011-01-06 10:04:23 -080012174 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12175 * instance that controls how the layer is composed on screen. The following
12176 * properties of the paint are taken into account when composing the layer:</p>
12177 * <ul>
12178 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12179 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12180 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12181 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012182 *
Romain Guy171c5922011-01-06 10:04:23 -080012183 * <p>If this view has an alpha value set to < 1.0 by calling
12184 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12185 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12186 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012187 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012188 *
Romain Guy171c5922011-01-06 10:04:23 -080012189 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12190 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12191 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012192 *
Chet Haased15ebf22012-09-05 11:40:29 -070012193 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012194 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12195 * {@link #LAYER_TYPE_HARDWARE}
12196 * @param paint The paint used to compose the layer. This argument is optional
12197 * and can be null. It is ignored when the layer type is
12198 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012199 *
12200 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012201 * @see #LAYER_TYPE_NONE
12202 * @see #LAYER_TYPE_SOFTWARE
12203 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012204 * @see #setAlpha(float)
12205 *
Romain Guy171c5922011-01-06 10:04:23 -080012206 * @attr ref android.R.styleable#View_layerType
12207 */
12208 public void setLayerType(int layerType, Paint paint) {
12209 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012210 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012211 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12212 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012213
Romain Guyd6cd5722011-01-17 14:42:41 -080012214 if (layerType == mLayerType) {
12215 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12216 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012217 invalidateParentCaches();
12218 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012219 }
12220 return;
12221 }
Romain Guy171c5922011-01-06 10:04:23 -080012222
12223 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012224 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012225 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012226 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012227 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012228 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012229 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012230 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012231 default:
12232 break;
Romain Guy171c5922011-01-06 10:04:23 -080012233 }
12234
12235 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012236 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12237 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12238 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012239
Romain Guy0fd89bf2011-01-26 15:41:30 -080012240 invalidateParentCaches();
12241 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012242 }
12243
12244 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012245 * Updates the {@link Paint} object used with the current layer (used only if the current
12246 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12247 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12248 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12249 * ensure that the view gets redrawn immediately.
12250 *
12251 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12252 * instance that controls how the layer is composed on screen. The following
12253 * properties of the paint are taken into account when composing the layer:</p>
12254 * <ul>
12255 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12256 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12257 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12258 * </ul>
12259 *
12260 * <p>If this view has an alpha value set to < 1.0 by calling
12261 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12262 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12263 * equivalent to setting a hardware layer on this view and providing a paint with
12264 * the desired alpha value.</p>
12265 *
12266 * @param paint The paint used to compose the layer. This argument is optional
12267 * and can be null. It is ignored when the layer type is
12268 * {@link #LAYER_TYPE_NONE}
12269 *
12270 * @see #setLayerType(int, android.graphics.Paint)
12271 */
12272 public void setLayerPaint(Paint paint) {
12273 int layerType = getLayerType();
12274 if (layerType != LAYER_TYPE_NONE) {
12275 mLayerPaint = paint == null ? new Paint() : paint;
12276 if (layerType == LAYER_TYPE_HARDWARE) {
12277 HardwareLayer layer = getHardwareLayer();
12278 if (layer != null) {
12279 layer.setLayerPaint(paint);
12280 }
12281 invalidateViewProperty(false, false);
12282 } else {
12283 invalidate();
12284 }
12285 }
12286 }
12287
12288 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012289 * Indicates whether this view has a static layer. A view with layer type
12290 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12291 * dynamic.
12292 */
12293 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012294 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012295 }
12296
12297 /**
Romain Guy171c5922011-01-06 10:04:23 -080012298 * Indicates what type of layer is currently associated with this view. By default
12299 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12300 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12301 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012302 *
Romain Guy171c5922011-01-06 10:04:23 -080012303 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12304 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012305 *
12306 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012307 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012308 * @see #LAYER_TYPE_NONE
12309 * @see #LAYER_TYPE_SOFTWARE
12310 * @see #LAYER_TYPE_HARDWARE
12311 */
12312 public int getLayerType() {
12313 return mLayerType;
12314 }
Joe Malin32736f02011-01-19 16:14:20 -080012315
Romain Guy6c319ca2011-01-11 14:29:25 -080012316 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012317 * Forces this view's layer to be created and this view to be rendered
12318 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12319 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012320 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012321 * This method can for instance be used to render a view into its layer before
12322 * starting an animation. If this view is complex, rendering into the layer
12323 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012324 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012325 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012326 *
12327 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012328 */
12329 public void buildLayer() {
12330 if (mLayerType == LAYER_TYPE_NONE) return;
12331
12332 if (mAttachInfo == null) {
12333 throw new IllegalStateException("This view must be attached to a window first");
12334 }
12335
12336 switch (mLayerType) {
12337 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012338 if (mAttachInfo.mHardwareRenderer != null &&
12339 mAttachInfo.mHardwareRenderer.isEnabled() &&
12340 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012341 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012342 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012343 break;
12344 case LAYER_TYPE_SOFTWARE:
12345 buildDrawingCache(true);
12346 break;
12347 }
12348 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012349
Romain Guyf1ae1062011-03-02 18:16:04 -080012350 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012351 * <p>Returns a hardware layer that can be used to draw this view again
12352 * without executing its draw method.</p>
12353 *
12354 * @return A HardwareLayer ready to render, or null if an error occurred.
12355 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012356 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012357 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12358 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012359 return null;
12360 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012361
Romain Guy9c4b79a2011-11-10 19:23:58 -080012362 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012363
12364 final int width = mRight - mLeft;
12365 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012366
Romain Guy6c319ca2011-01-11 14:29:25 -080012367 if (width == 0 || height == 0) {
12368 return null;
12369 }
12370
Dianne Hackborn4702a852012-08-17 15:18:29 -070012371 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012372 if (mHardwareLayer == null) {
12373 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12374 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012375 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012376 } else {
12377 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012378 if (mHardwareLayer.resize(width, height)) {
12379 mLocalDirtyRect.set(0, 0, width, height);
12380 }
Romain Guy846a5332012-07-11 17:44:57 -070012381 }
12382
12383 // This should not be necessary but applications that change
12384 // the parameters of their background drawable without calling
12385 // this.setBackground(Drawable) can leave the view in a bad state
12386 // (for instance isOpaque() returns true, but the background is
12387 // not opaque.)
12388 computeOpaqueFlags();
12389
12390 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012391 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012392 mHardwareLayer.setOpaque(opaque);
12393 mLocalDirtyRect.set(0, 0, width, height);
12394 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012395 }
12396
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012397 // The layer is not valid if the underlying GPU resources cannot be allocated
12398 if (!mHardwareLayer.isValid()) {
12399 return null;
12400 }
12401
Romain Guy11cb6422012-09-21 00:39:43 -070012402 mHardwareLayer.setLayerPaint(mLayerPaint);
12403 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12404 ViewRootImpl viewRoot = getViewRootImpl();
12405 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12406
Michael Jurka7e52caf2012-03-06 15:57:06 -080012407 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012408 }
12409
12410 return mHardwareLayer;
12411 }
Romain Guy171c5922011-01-06 10:04:23 -080012412
Romain Guy589b0bb2011-10-10 13:57:47 -070012413 /**
12414 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012415 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012416 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012417 *
12418 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012419 * @see #LAYER_TYPE_HARDWARE
12420 */
Romain Guya998dff2012-03-23 18:58:36 -070012421 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012422 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012423 AttachInfo info = mAttachInfo;
12424 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012425 info.mHardwareRenderer.isEnabled() &&
12426 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012427 mHardwareLayer.destroy();
12428 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012429
Chet Haase6a2d17f2012-09-30 12:14:13 -070012430 if (mDisplayList != null) {
12431 mDisplayList.reset();
12432 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012433 invalidate(true);
12434 invalidateParentCaches();
12435 }
Romain Guy65b345f2011-07-27 18:51:50 -070012436 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012437 }
Romain Guy65b345f2011-07-27 18:51:50 -070012438 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012439 }
12440
Romain Guy171c5922011-01-06 10:04:23 -080012441 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012442 * Destroys all hardware rendering resources. This method is invoked
12443 * when the system needs to reclaim resources. Upon execution of this
12444 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012445 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012446 * Note: you <strong>must</strong> call
12447 * <code>super.destroyHardwareResources()</code> when overriding
12448 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012449 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012450 * @hide
12451 */
12452 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012453 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012454 }
12455
12456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012457 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12458 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12459 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12460 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12461 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12462 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012463 *
Romain Guy171c5922011-01-06 10:04:23 -080012464 * <p>Enabling the drawing cache is similar to
12465 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012466 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12467 * drawing cache has no effect on rendering because the system uses a different mechanism
12468 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12469 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12470 * for information on how to enable software and hardware layers.</p>
12471 *
12472 * <p>This API can be used to manually generate
12473 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12474 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475 *
12476 * @param enabled true to enable the drawing cache, false otherwise
12477 *
12478 * @see #isDrawingCacheEnabled()
12479 * @see #getDrawingCache()
12480 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012481 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012482 */
12483 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012484 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012485 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12486 }
12487
12488 /**
12489 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12490 *
12491 * @return true if the drawing cache is enabled
12492 *
12493 * @see #setDrawingCacheEnabled(boolean)
12494 * @see #getDrawingCache()
12495 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012496 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012497 public boolean isDrawingCacheEnabled() {
12498 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12499 }
12500
12501 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012502 * Debugging utility which recursively outputs the dirty state of a view and its
12503 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012504 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012505 * @hide
12506 */
Romain Guy676b1732011-02-14 14:45:33 -080012507 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012508 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012509 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12510 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12511 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12512 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012513 if (clear) {
12514 mPrivateFlags &= clearMask;
12515 }
12516 if (this instanceof ViewGroup) {
12517 ViewGroup parent = (ViewGroup) this;
12518 final int count = parent.getChildCount();
12519 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012520 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012521 child.outputDirtyFlags(indent + " ", clear, clearMask);
12522 }
12523 }
12524 }
12525
12526 /**
12527 * This method is used by ViewGroup to cause its children to restore or recreate their
12528 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12529 * to recreate its own display list, which would happen if it went through the normal
12530 * draw/dispatchDraw mechanisms.
12531 *
12532 * @hide
12533 */
12534 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012535
12536 /**
12537 * A view that is not attached or hardware accelerated cannot create a display list.
12538 * This method checks these conditions and returns the appropriate result.
12539 *
12540 * @return true if view has the ability to create a display list, false otherwise.
12541 *
12542 * @hide
12543 */
12544 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012545 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012546 }
Joe Malin32736f02011-01-19 16:14:20 -080012547
Chet Haasedaf98e92011-01-10 14:10:36 -080012548 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012549 * @return The HardwareRenderer associated with that view or null if hardware rendering
12550 * is not supported or this this has not been attached to a window.
12551 *
12552 * @hide
12553 */
12554 public HardwareRenderer getHardwareRenderer() {
12555 if (mAttachInfo != null) {
12556 return mAttachInfo.mHardwareRenderer;
12557 }
12558 return null;
12559 }
12560
12561 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012562 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12563 * Otherwise, the same display list will be returned (after having been rendered into
12564 * along the way, depending on the invalidation state of the view).
12565 *
12566 * @param displayList The previous version of this displayList, could be null.
12567 * @param isLayer Whether the requester of the display list is a layer. If so,
12568 * the view will avoid creating a layer inside the resulting display list.
12569 * @return A new or reused DisplayList object.
12570 */
12571 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12572 if (!canHaveDisplayList()) {
12573 return null;
12574 }
12575
Dianne Hackborn4702a852012-08-17 15:18:29 -070012576 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012577 displayList == null || !displayList.isValid() ||
12578 (!isLayer && mRecreateDisplayList))) {
12579 // Don't need to recreate the display list, just need to tell our
12580 // children to restore/recreate theirs
12581 if (displayList != null && displayList.isValid() &&
12582 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012583 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12584 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012585 dispatchGetDisplayList();
12586
12587 return displayList;
12588 }
12589
12590 if (!isLayer) {
12591 // If we got here, we're recreating it. Mark it as such to ensure that
12592 // we copy in child display lists into ours in drawChild()
12593 mRecreateDisplayList = true;
12594 }
12595 if (displayList == null) {
12596 final String name = getClass().getSimpleName();
12597 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12598 // If we're creating a new display list, make sure our parent gets invalidated
12599 // since they will need to recreate their display list to account for this
12600 // new child display list.
12601 invalidateParentCaches();
12602 }
12603
12604 boolean caching = false;
12605 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012606 int width = mRight - mLeft;
12607 int height = mBottom - mTop;
12608
12609 try {
12610 canvas.setViewport(width, height);
12611 // The dirty rect should always be null for a display list
12612 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012613 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012614 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012615 if (layerType == LAYER_TYPE_HARDWARE) {
12616 final HardwareLayer layer = getHardwareLayer();
12617 if (layer != null && layer.isValid()) {
12618 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12619 } else {
12620 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12621 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12622 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12623 }
12624 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012625 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012626 buildDrawingCache(true);
12627 Bitmap cache = getDrawingCache(true);
12628 if (cache != null) {
12629 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12630 caching = true;
12631 }
Chet Haasea1cff502012-02-21 13:43:44 -080012632 }
Chet Haasea1cff502012-02-21 13:43:44 -080012633 } else {
12634
12635 computeScroll();
12636
Chet Haasea1cff502012-02-21 13:43:44 -080012637 canvas.translate(-mScrollX, -mScrollY);
12638 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012639 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12640 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012641 }
12642
12643 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012644 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012645 dispatchDraw(canvas);
12646 } else {
12647 draw(canvas);
12648 }
12649 }
12650 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012651 canvas.onPostDraw();
12652
12653 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012654 displayList.setCaching(caching);
12655 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012656 displayList.setLeftTopRightBottom(0, 0, width, height);
12657 } else {
12658 setDisplayListProperties(displayList);
12659 }
12660 }
12661 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012662 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12663 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012664 }
12665
12666 return displayList;
12667 }
12668
12669 /**
12670 * Get the DisplayList for the HardwareLayer
12671 *
12672 * @param layer The HardwareLayer whose DisplayList we want
12673 * @return A DisplayList fopr the specified HardwareLayer
12674 */
12675 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12676 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12677 layer.setDisplayList(displayList);
12678 return displayList;
12679 }
12680
12681
12682 /**
Romain Guyb051e892010-09-28 19:09:36 -070012683 * <p>Returns a display list that can be used to draw this view again
12684 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012685 *
Romain Guyb051e892010-09-28 19:09:36 -070012686 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012687 *
12688 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012689 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012690 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012691 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012692 return mDisplayList;
12693 }
12694
Romain Guy38c2ece2012-05-24 14:20:56 -070012695 private void clearDisplayList() {
12696 if (mDisplayList != null) {
12697 mDisplayList.invalidate();
12698 mDisplayList.clear();
12699 }
12700 }
12701
Romain Guyb051e892010-09-28 19:09:36 -070012702 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012703 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012704 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012705 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012706 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012707 * @see #getDrawingCache(boolean)
12708 */
12709 public Bitmap getDrawingCache() {
12710 return getDrawingCache(false);
12711 }
12712
12713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012714 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12715 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12716 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12717 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12718 * request the drawing cache by calling this method and draw it on screen if the
12719 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012720 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012721 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12722 * this method will create a bitmap of the same size as this view. Because this bitmap
12723 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12724 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12725 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12726 * size than the view. This implies that your application must be able to handle this
12727 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012728 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012729 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12730 * the current density of the screen when the application is in compatibility
12731 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012732 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012733 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012734 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 * @see #setDrawingCacheEnabled(boolean)
12736 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012737 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012738 * @see #destroyDrawingCache()
12739 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012740 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12742 return null;
12743 }
12744 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012745 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012746 }
Romain Guy02890fd2010-08-06 17:58:44 -070012747 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 }
12749
12750 /**
12751 * <p>Frees the resources used by the drawing cache. If you call
12752 * {@link #buildDrawingCache()} manually without calling
12753 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12754 * should cleanup the cache with this method afterwards.</p>
12755 *
12756 * @see #setDrawingCacheEnabled(boolean)
12757 * @see #buildDrawingCache()
12758 * @see #getDrawingCache()
12759 */
12760 public void destroyDrawingCache() {
12761 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012762 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 mDrawingCache = null;
12764 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012765 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012766 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012767 mUnscaledDrawingCache = null;
12768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012769 }
12770
12771 /**
12772 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012773 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012774 * view will always be drawn on top of a solid color.
12775 *
12776 * @param color The background color to use for the drawing cache's bitmap
12777 *
12778 * @see #setDrawingCacheEnabled(boolean)
12779 * @see #buildDrawingCache()
12780 * @see #getDrawingCache()
12781 */
12782 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012783 if (color != mDrawingCacheBackgroundColor) {
12784 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012785 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012787 }
12788
12789 /**
12790 * @see #setDrawingCacheBackgroundColor(int)
12791 *
12792 * @return The background color to used for the drawing cache's bitmap
12793 */
12794 public int getDrawingCacheBackgroundColor() {
12795 return mDrawingCacheBackgroundColor;
12796 }
12797
12798 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012799 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012800 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012801 * @see #buildDrawingCache(boolean)
12802 */
12803 public void buildDrawingCache() {
12804 buildDrawingCache(false);
12805 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012806
Romain Guyfbd8f692009-06-26 14:51:58 -070012807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012808 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12809 *
12810 * <p>If you call {@link #buildDrawingCache()} manually without calling
12811 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12812 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012813 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012814 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12815 * this method will create a bitmap of the same size as this view. Because this bitmap
12816 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12817 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12818 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12819 * size than the view. This implies that your application must be able to handle this
12820 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012821 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012822 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12823 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012824 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012825 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012826 *
12827 * @see #getDrawingCache()
12828 * @see #destroyDrawingCache()
12829 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012830 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012831 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012832 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012833 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012834
Romain Guy8506ab42009-06-11 17:35:47 -070012835 int width = mRight - mLeft;
12836 int height = mBottom - mTop;
12837
12838 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012839 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012840
Romain Guye1123222009-06-29 14:24:56 -070012841 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012842 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12843 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012845
12846 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012847 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012848 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849
Romain Guyaf61cc42012-09-30 10:49:15 -070012850 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12851 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012852 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12853 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012854 if (width > 0 && height > 0) {
12855 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12856 + projectedBitmapSize + " bytes, only "
12857 + drawingCacheSize + " available");
12858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012860 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012861 return;
12862 }
12863
12864 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012865 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012866
12867 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012868 Bitmap.Config quality;
12869 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012870 // Never pick ARGB_4444 because it looks awful
12871 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12873 case DRAWING_CACHE_QUALITY_AUTO:
12874 quality = Bitmap.Config.ARGB_8888;
12875 break;
12876 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012877 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012878 break;
12879 case DRAWING_CACHE_QUALITY_HIGH:
12880 quality = Bitmap.Config.ARGB_8888;
12881 break;
12882 default:
12883 quality = Bitmap.Config.ARGB_8888;
12884 break;
12885 }
12886 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012887 // Optimization for translucent windows
12888 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012889 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012890 }
12891
12892 // Try to cleanup memory
12893 if (bitmap != null) bitmap.recycle();
12894
12895 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012896 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12897 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012898 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012899 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012900 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012901 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012902 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012903 }
Adam Powell26153a32010-11-08 15:22:27 -080012904 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012905 } catch (OutOfMemoryError e) {
12906 // If there is not enough memory to create the bitmap cache, just
12907 // ignore the issue as bitmap caches are not required to draw the
12908 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012909 if (autoScale) {
12910 mDrawingCache = null;
12911 } else {
12912 mUnscaledDrawingCache = null;
12913 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012914 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 return;
12916 }
12917
12918 clear = drawingCacheBackgroundColor != 0;
12919 }
12920
12921 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012922 if (attachInfo != null) {
12923 canvas = attachInfo.mCanvas;
12924 if (canvas == null) {
12925 canvas = new Canvas();
12926 }
12927 canvas.setBitmap(bitmap);
12928 // Temporarily clobber the cached Canvas in case one of our children
12929 // is also using a drawing cache. Without this, the children would
12930 // steal the canvas by attaching their own bitmap to it and bad, bad
12931 // thing would happen (invisible views, corrupted drawings, etc.)
12932 attachInfo.mCanvas = null;
12933 } else {
12934 // This case should hopefully never or seldom happen
12935 canvas = new Canvas(bitmap);
12936 }
12937
12938 if (clear) {
12939 bitmap.eraseColor(drawingCacheBackgroundColor);
12940 }
12941
12942 computeScroll();
12943 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012944
Romain Guye1123222009-06-29 14:24:56 -070012945 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012946 final float scale = attachInfo.mApplicationScale;
12947 canvas.scale(scale, scale);
12948 }
Joe Malin32736f02011-01-19 16:14:20 -080012949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012950 canvas.translate(-mScrollX, -mScrollY);
12951
Dianne Hackborn4702a852012-08-17 15:18:29 -070012952 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012953 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12954 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012955 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012957
12958 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012959 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12960 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012961 dispatchDraw(canvas);
12962 } else {
12963 draw(canvas);
12964 }
12965
12966 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012967 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012968
12969 if (attachInfo != null) {
12970 // Restore the cached Canvas for our siblings
12971 attachInfo.mCanvas = canvas;
12972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012973 }
12974 }
12975
12976 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012977 * Create a snapshot of the view into a bitmap. We should probably make
12978 * some form of this public, but should think about the API.
12979 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012980 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012981 int width = mRight - mLeft;
12982 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012983
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012984 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012985 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012986 width = (int) ((width * scale) + 0.5f);
12987 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012988
Dianne Hackborndde331c2012-08-03 14:01:57 -070012989 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12990 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012991 if (bitmap == null) {
12992 throw new OutOfMemoryError();
12993 }
12994
Romain Guyc529d8d2011-09-06 15:01:39 -070012995 Resources resources = getResources();
12996 if (resources != null) {
12997 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12998 }
Joe Malin32736f02011-01-19 16:14:20 -080012999
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013000 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013001 if (attachInfo != null) {
13002 canvas = attachInfo.mCanvas;
13003 if (canvas == null) {
13004 canvas = new Canvas();
13005 }
13006 canvas.setBitmap(bitmap);
13007 // Temporarily clobber the cached Canvas in case one of our children
13008 // is also using a drawing cache. Without this, the children would
13009 // steal the canvas by attaching their own bitmap to it and bad, bad
13010 // things would happen (invisible views, corrupted drawings, etc.)
13011 attachInfo.mCanvas = null;
13012 } else {
13013 // This case should hopefully never or seldom happen
13014 canvas = new Canvas(bitmap);
13015 }
13016
Romain Guy5bcdff42009-05-14 21:27:18 -070013017 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013018 bitmap.eraseColor(backgroundColor);
13019 }
13020
13021 computeScroll();
13022 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013023 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013024 canvas.translate(-mScrollX, -mScrollY);
13025
Romain Guy5bcdff42009-05-14 21:27:18 -070013026 // Temporarily remove the dirty mask
13027 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013028 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013029
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013030 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013031 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013032 dispatchDraw(canvas);
13033 } else {
13034 draw(canvas);
13035 }
13036
Romain Guy5bcdff42009-05-14 21:27:18 -070013037 mPrivateFlags = flags;
13038
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013039 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013040 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013041
13042 if (attachInfo != null) {
13043 // Restore the cached Canvas for our siblings
13044 attachInfo.mCanvas = canvas;
13045 }
Romain Guy8506ab42009-06-11 17:35:47 -070013046
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013047 return bitmap;
13048 }
13049
13050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013051 * Indicates whether this View is currently in edit mode. A View is usually
13052 * in edit mode when displayed within a developer tool. For instance, if
13053 * this View is being drawn by a visual user interface builder, this method
13054 * should return true.
13055 *
13056 * Subclasses should check the return value of this method to provide
13057 * different behaviors if their normal behavior might interfere with the
13058 * host environment. For instance: the class spawns a thread in its
13059 * constructor, the drawing code relies on device-specific features, etc.
13060 *
13061 * This method is usually checked in the drawing code of custom widgets.
13062 *
13063 * @return True if this View is in edit mode, false otherwise.
13064 */
13065 public boolean isInEditMode() {
13066 return false;
13067 }
13068
13069 /**
13070 * If the View draws content inside its padding and enables fading edges,
13071 * it needs to support padding offsets. Padding offsets are added to the
13072 * fading edges to extend the length of the fade so that it covers pixels
13073 * drawn inside the padding.
13074 *
13075 * Subclasses of this class should override this method if they need
13076 * to draw content inside the padding.
13077 *
13078 * @return True if padding offset must be applied, false otherwise.
13079 *
13080 * @see #getLeftPaddingOffset()
13081 * @see #getRightPaddingOffset()
13082 * @see #getTopPaddingOffset()
13083 * @see #getBottomPaddingOffset()
13084 *
13085 * @since CURRENT
13086 */
13087 protected boolean isPaddingOffsetRequired() {
13088 return false;
13089 }
13090
13091 /**
13092 * Amount by which to extend the left fading region. Called only when
13093 * {@link #isPaddingOffsetRequired()} returns true.
13094 *
13095 * @return The left padding offset in pixels.
13096 *
13097 * @see #isPaddingOffsetRequired()
13098 *
13099 * @since CURRENT
13100 */
13101 protected int getLeftPaddingOffset() {
13102 return 0;
13103 }
13104
13105 /**
13106 * Amount by which to extend the right fading region. Called only when
13107 * {@link #isPaddingOffsetRequired()} returns true.
13108 *
13109 * @return The right padding offset in pixels.
13110 *
13111 * @see #isPaddingOffsetRequired()
13112 *
13113 * @since CURRENT
13114 */
13115 protected int getRightPaddingOffset() {
13116 return 0;
13117 }
13118
13119 /**
13120 * Amount by which to extend the top fading region. Called only when
13121 * {@link #isPaddingOffsetRequired()} returns true.
13122 *
13123 * @return The top padding offset in pixels.
13124 *
13125 * @see #isPaddingOffsetRequired()
13126 *
13127 * @since CURRENT
13128 */
13129 protected int getTopPaddingOffset() {
13130 return 0;
13131 }
13132
13133 /**
13134 * Amount by which to extend the bottom fading region. Called only when
13135 * {@link #isPaddingOffsetRequired()} returns true.
13136 *
13137 * @return The bottom padding offset in pixels.
13138 *
13139 * @see #isPaddingOffsetRequired()
13140 *
13141 * @since CURRENT
13142 */
13143 protected int getBottomPaddingOffset() {
13144 return 0;
13145 }
13146
13147 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013148 * @hide
13149 * @param offsetRequired
13150 */
13151 protected int getFadeTop(boolean offsetRequired) {
13152 int top = mPaddingTop;
13153 if (offsetRequired) top += getTopPaddingOffset();
13154 return top;
13155 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013156
Romain Guyf2fc4602011-07-19 15:20:03 -070013157 /**
13158 * @hide
13159 * @param offsetRequired
13160 */
13161 protected int getFadeHeight(boolean offsetRequired) {
13162 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013163 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013164 return mBottom - mTop - mPaddingBottom - padding;
13165 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013166
Romain Guyf2fc4602011-07-19 15:20:03 -070013167 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013168 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013169 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013170 *
Romain Guy2bffd262010-09-12 17:40:02 -070013171 * <p>Even if this method returns true, it does not mean that every call
13172 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13173 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013174 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013175 * window is hardware accelerated,
13176 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13177 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013178 *
Romain Guy2bffd262010-09-12 17:40:02 -070013179 * @return True if the view is attached to a window and the window is
13180 * hardware accelerated; false in any other case.
13181 */
13182 public boolean isHardwareAccelerated() {
13183 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13184 }
Joe Malin32736f02011-01-19 16:14:20 -080013185
Romain Guy2bffd262010-09-12 17:40:02 -070013186 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013187 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13188 * case of an active Animation being run on the view.
13189 */
13190 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13191 Animation a, boolean scalingRequired) {
13192 Transformation invalidationTransform;
13193 final int flags = parent.mGroupFlags;
13194 final boolean initialized = a.isInitialized();
13195 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013196 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013197 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013198 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013199 onAnimationStart();
13200 }
13201
13202 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13203 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13204 if (parent.mInvalidationTransformation == null) {
13205 parent.mInvalidationTransformation = new Transformation();
13206 }
13207 invalidationTransform = parent.mInvalidationTransformation;
13208 a.getTransformation(drawingTime, invalidationTransform, 1f);
13209 } else {
13210 invalidationTransform = parent.mChildTransformation;
13211 }
Romain Guy393a52c2012-05-22 20:21:08 -070013212
Chet Haasebcca79a2012-02-14 08:45:14 -080013213 if (more) {
13214 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013215 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13216 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13217 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13218 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013219 // The child need to draw an animation, potentially offscreen, so
13220 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013221 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013222 parent.invalidate(mLeft, mTop, mRight, mBottom);
13223 }
13224 } else {
13225 if (parent.mInvalidateRegion == null) {
13226 parent.mInvalidateRegion = new RectF();
13227 }
13228 final RectF region = parent.mInvalidateRegion;
13229 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13230 invalidationTransform);
13231
13232 // The child need to draw an animation, potentially offscreen, so
13233 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013234 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013235
13236 final int left = mLeft + (int) region.left;
13237 final int top = mTop + (int) region.top;
13238 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13239 top + (int) (region.height() + .5f));
13240 }
13241 }
13242 return more;
13243 }
13244
Chet Haasea1cff502012-02-21 13:43:44 -080013245 /**
13246 * This method is called by getDisplayList() when a display list is created or re-rendered.
13247 * It sets or resets the current value of all properties on that display list (resetting is
13248 * necessary when a display list is being re-created, because we need to make sure that
13249 * previously-set transform values
13250 */
13251 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013252 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013253 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013254 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013255 if (mParent instanceof ViewGroup) {
13256 displayList.setClipChildren(
13257 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13258 }
Chet Haase9420abd2012-03-29 16:28:32 -070013259 float alpha = 1;
13260 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13261 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13262 ViewGroup parentVG = (ViewGroup) mParent;
13263 final boolean hasTransform =
13264 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13265 if (hasTransform) {
13266 Transformation transform = parentVG.mChildTransformation;
13267 final int transformType = parentVG.mChildTransformation.getTransformationType();
13268 if (transformType != Transformation.TYPE_IDENTITY) {
13269 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13270 alpha = transform.getAlpha();
13271 }
13272 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13273 displayList.setStaticMatrix(transform.getMatrix());
13274 }
13275 }
13276 }
Chet Haasea1cff502012-02-21 13:43:44 -080013277 }
13278 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013279 alpha *= mTransformationInfo.mAlpha;
13280 if (alpha < 1) {
13281 final int multipliedAlpha = (int) (255 * alpha);
13282 if (onSetAlpha(multipliedAlpha)) {
13283 alpha = 1;
13284 }
13285 }
13286 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013287 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13288 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13289 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13290 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013291 if (mTransformationInfo.mCamera == null) {
13292 mTransformationInfo.mCamera = new Camera();
13293 mTransformationInfo.matrix3D = new Matrix();
13294 }
13295 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013296 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013297 displayList.setPivotX(getPivotX());
13298 displayList.setPivotY(getPivotY());
13299 }
Chet Haase9420abd2012-03-29 16:28:32 -070013300 } else if (alpha < 1) {
13301 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013302 }
13303 }
13304 }
13305
Chet Haasebcca79a2012-02-14 08:45:14 -080013306 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013307 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13308 * This draw() method is an implementation detail and is not intended to be overridden or
13309 * to be called from anywhere else other than ViewGroup.drawChild().
13310 */
13311 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013312 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013313 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013314 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013315 final int flags = parent.mGroupFlags;
13316
Chet Haasea1cff502012-02-21 13:43:44 -080013317 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013318 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013319 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013320 }
13321
13322 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013323 boolean concatMatrix = false;
13324
13325 boolean scalingRequired = false;
13326 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013327 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013328
13329 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013330 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13331 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013332 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013333 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013334 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13335 } else {
13336 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13337 }
13338
Chet Haasebcca79a2012-02-14 08:45:14 -080013339 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013340 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013341 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013342 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013343 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013344 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013345 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013346 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013347 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013348 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013349 mDisplayList != null) {
13350 // No longer animating: clear out old animation matrix
13351 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013352 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013353 }
13354 if (!useDisplayListProperties &&
13355 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13356 final boolean hasTransform =
13357 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13358 if (hasTransform) {
13359 final int transformType = parent.mChildTransformation.getTransformationType();
13360 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13361 parent.mChildTransformation : null;
13362 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13363 }
Chet Haase64a48c12012-02-13 16:33:29 -080013364 }
13365 }
13366
13367 concatMatrix |= !childHasIdentityMatrix;
13368
13369 // Sets the flag as early as possible to allow draw() implementations
13370 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013371 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013372
Romain Guyfbb43212012-08-30 15:19:27 -070013373 if (!concatMatrix &&
13374 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13375 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013376 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013377 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13378 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013379 return more;
13380 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013381 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013382
13383 if (hardwareAccelerated) {
13384 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13385 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013386 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13387 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013388 }
13389
Chet Haase64a48c12012-02-13 16:33:29 -080013390 DisplayList displayList = null;
13391 Bitmap cache = null;
13392 boolean hasDisplayList = false;
13393 if (caching) {
13394 if (!hardwareAccelerated) {
13395 if (layerType != LAYER_TYPE_NONE) {
13396 layerType = LAYER_TYPE_SOFTWARE;
13397 buildDrawingCache(true);
13398 }
13399 cache = getDrawingCache(true);
13400 } else {
13401 switch (layerType) {
13402 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013403 if (useDisplayListProperties) {
13404 hasDisplayList = canHaveDisplayList();
13405 } else {
13406 buildDrawingCache(true);
13407 cache = getDrawingCache(true);
13408 }
Chet Haase64a48c12012-02-13 16:33:29 -080013409 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013410 case LAYER_TYPE_HARDWARE:
13411 if (useDisplayListProperties) {
13412 hasDisplayList = canHaveDisplayList();
13413 }
13414 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013415 case LAYER_TYPE_NONE:
13416 // Delay getting the display list until animation-driven alpha values are
13417 // set up and possibly passed on to the view
13418 hasDisplayList = canHaveDisplayList();
13419 break;
13420 }
13421 }
13422 }
Chet Haasea1cff502012-02-21 13:43:44 -080013423 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013424 if (useDisplayListProperties) {
13425 displayList = getDisplayList();
13426 if (!displayList.isValid()) {
13427 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13428 // to getDisplayList(), the display list will be marked invalid and we should not
13429 // try to use it again.
13430 displayList = null;
13431 hasDisplayList = false;
13432 useDisplayListProperties = false;
13433 }
13434 }
Chet Haase64a48c12012-02-13 16:33:29 -080013435
Chet Haase526057b2012-07-12 17:50:41 -070013436 int sx = 0;
13437 int sy = 0;
13438 if (!hasDisplayList) {
13439 computeScroll();
13440 sx = mScrollX;
13441 sy = mScrollY;
13442 }
13443
Chet Haase64a48c12012-02-13 16:33:29 -080013444 final boolean hasNoCache = cache == null || hasDisplayList;
13445 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13446 layerType != LAYER_TYPE_HARDWARE;
13447
Chet Haasea1cff502012-02-21 13:43:44 -080013448 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013449 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013450 restoreTo = canvas.save();
13451 }
Chet Haase64a48c12012-02-13 16:33:29 -080013452 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013453 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013454 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013455 if (!useDisplayListProperties) {
13456 canvas.translate(mLeft, mTop);
13457 }
Chet Haase64a48c12012-02-13 16:33:29 -080013458 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013459 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013460 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013461 restoreTo = canvas.save();
13462 }
Chet Haase64a48c12012-02-13 16:33:29 -080013463 // mAttachInfo cannot be null, otherwise scalingRequired == false
13464 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13465 canvas.scale(scale, scale);
13466 }
13467 }
13468
Chet Haasea1cff502012-02-21 13:43:44 -080013469 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013470 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013471 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013472 if (transformToApply != null || !childHasIdentityMatrix) {
13473 int transX = 0;
13474 int transY = 0;
13475
13476 if (offsetForScroll) {
13477 transX = -sx;
13478 transY = -sy;
13479 }
13480
13481 if (transformToApply != null) {
13482 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013483 if (useDisplayListProperties) {
13484 displayList.setAnimationMatrix(transformToApply.getMatrix());
13485 } else {
13486 // Undo the scroll translation, apply the transformation matrix,
13487 // then redo the scroll translate to get the correct result.
13488 canvas.translate(-transX, -transY);
13489 canvas.concat(transformToApply.getMatrix());
13490 canvas.translate(transX, transY);
13491 }
Chet Haasea1cff502012-02-21 13:43:44 -080013492 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013493 }
13494
13495 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013496 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013497 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013498 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013499 }
13500 }
13501
Chet Haasea1cff502012-02-21 13:43:44 -080013502 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013503 canvas.translate(-transX, -transY);
13504 canvas.concat(getMatrix());
13505 canvas.translate(transX, transY);
13506 }
13507 }
13508
Chet Haase21433372012-06-05 07:54:09 -070013509 // Deal with alpha if it is or used to be <1
13510 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013511 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013512 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013513 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013514 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013515 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013516 }
Chet Haasea1cff502012-02-21 13:43:44 -080013517 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013518 if (hasNoCache) {
13519 final int multipliedAlpha = (int) (255 * alpha);
13520 if (!onSetAlpha(multipliedAlpha)) {
13521 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013522 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013523 layerType != LAYER_TYPE_NONE) {
13524 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13525 }
Chet Haase9420abd2012-03-29 16:28:32 -070013526 if (useDisplayListProperties) {
13527 displayList.setAlpha(alpha * getAlpha());
13528 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013529 final int scrollX = hasDisplayList ? 0 : sx;
13530 final int scrollY = hasDisplayList ? 0 : sy;
13531 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13532 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013533 }
13534 } else {
13535 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013536 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013537 }
13538 }
13539 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013540 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013541 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013542 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013543 }
13544
Chet Haasea1cff502012-02-21 13:43:44 -080013545 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13546 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013547 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013548 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013549 } else {
13550 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013551 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013552 } else {
13553 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13554 }
13555 }
13556 }
13557
Chet Haase9420abd2012-03-29 16:28:32 -070013558 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013559 displayList = getDisplayList();
13560 if (!displayList.isValid()) {
13561 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13562 // to getDisplayList(), the display list will be marked invalid and we should not
13563 // try to use it again.
13564 displayList = null;
13565 hasDisplayList = false;
13566 }
13567 }
13568
13569 if (hasNoCache) {
13570 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013571 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013572 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013573 if (layer != null && layer.isValid()) {
13574 mLayerPaint.setAlpha((int) (alpha * 255));
13575 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13576 layerRendered = true;
13577 } else {
13578 final int scrollX = hasDisplayList ? 0 : sx;
13579 final int scrollY = hasDisplayList ? 0 : sy;
13580 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013581 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013582 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13583 }
13584 }
13585
13586 if (!layerRendered) {
13587 if (!hasDisplayList) {
13588 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013589 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13590 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013591 dispatchDraw(canvas);
13592 } else {
13593 draw(canvas);
13594 }
13595 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013596 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013597 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013598 }
13599 }
13600 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013601 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013602 Paint cachePaint;
13603
13604 if (layerType == LAYER_TYPE_NONE) {
13605 cachePaint = parent.mCachePaint;
13606 if (cachePaint == null) {
13607 cachePaint = new Paint();
13608 cachePaint.setDither(false);
13609 parent.mCachePaint = cachePaint;
13610 }
Chet Haase9420abd2012-03-29 16:28:32 -070013611 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013612 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013613 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13614 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013615 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013616 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013617 }
13618 } else {
13619 cachePaint = mLayerPaint;
13620 cachePaint.setAlpha((int) (alpha * 255));
13621 }
13622 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13623 }
13624
Chet Haasea1cff502012-02-21 13:43:44 -080013625 if (restoreTo >= 0) {
13626 canvas.restoreToCount(restoreTo);
13627 }
Chet Haase64a48c12012-02-13 16:33:29 -080013628
13629 if (a != null && !more) {
13630 if (!hardwareAccelerated && !a.getFillAfter()) {
13631 onSetAlpha(255);
13632 }
13633 parent.finishAnimatingView(this, a);
13634 }
13635
13636 if (more && hardwareAccelerated) {
13637 // invalidation is the trigger to recreate display lists, so if we're using
13638 // display lists to render, force an invalidate to allow the animation to
13639 // continue drawing another frame
13640 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013641 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013642 // alpha animations should cause the child to recreate its display list
13643 invalidate(true);
13644 }
13645 }
13646
13647 mRecreateDisplayList = false;
13648
13649 return more;
13650 }
13651
13652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013653 * Manually render this view (and all of its children) to the given Canvas.
13654 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013655 * called. When implementing a view, implement
13656 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13657 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013658 *
13659 * @param canvas The Canvas to which the View is rendered.
13660 */
13661 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013662 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013663 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013664 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013665 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013667 /*
13668 * Draw traversal performs several drawing steps which must be executed
13669 * in the appropriate order:
13670 *
13671 * 1. Draw the background
13672 * 2. If necessary, save the canvas' layers to prepare for fading
13673 * 3. Draw view's content
13674 * 4. Draw children
13675 * 5. If necessary, draw the fading edges and restore layers
13676 * 6. Draw decorations (scrollbars for instance)
13677 */
13678
13679 // Step 1, draw the background, if needed
13680 int saveCount;
13681
Romain Guy24443ea2009-05-11 11:56:30 -070013682 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013683 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013684 if (background != null) {
13685 final int scrollX = mScrollX;
13686 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013687
Romain Guy24443ea2009-05-11 11:56:30 -070013688 if (mBackgroundSizeChanged) {
13689 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13690 mBackgroundSizeChanged = false;
13691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013692
Romain Guy24443ea2009-05-11 11:56:30 -070013693 if ((scrollX | scrollY) == 0) {
13694 background.draw(canvas);
13695 } else {
13696 canvas.translate(scrollX, scrollY);
13697 background.draw(canvas);
13698 canvas.translate(-scrollX, -scrollY);
13699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013700 }
13701 }
13702
13703 // skip step 2 & 5 if possible (common case)
13704 final int viewFlags = mViewFlags;
13705 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13706 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13707 if (!verticalEdges && !horizontalEdges) {
13708 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013709 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013710
13711 // Step 4, draw the children
13712 dispatchDraw(canvas);
13713
13714 // Step 6, draw decorations (scrollbars)
13715 onDrawScrollBars(canvas);
13716
13717 // we're done...
13718 return;
13719 }
13720
13721 /*
13722 * Here we do the full fledged routine...
13723 * (this is an uncommon case where speed matters less,
13724 * this is why we repeat some of the tests that have been
13725 * done above)
13726 */
13727
13728 boolean drawTop = false;
13729 boolean drawBottom = false;
13730 boolean drawLeft = false;
13731 boolean drawRight = false;
13732
13733 float topFadeStrength = 0.0f;
13734 float bottomFadeStrength = 0.0f;
13735 float leftFadeStrength = 0.0f;
13736 float rightFadeStrength = 0.0f;
13737
13738 // Step 2, save the canvas' layers
13739 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740
13741 final boolean offsetRequired = isPaddingOffsetRequired();
13742 if (offsetRequired) {
13743 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013744 }
13745
13746 int left = mScrollX + paddingLeft;
13747 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013748 int top = mScrollY + getFadeTop(offsetRequired);
13749 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750
13751 if (offsetRequired) {
13752 right += getRightPaddingOffset();
13753 bottom += getBottomPaddingOffset();
13754 }
13755
13756 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013757 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013758 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013759
13760 // clip the fade length if top and bottom fades overlap
13761 // overlapping fades produce odd-looking artifacts
13762 if (verticalEdges && (top + length > bottom - length)) {
13763 length = (bottom - top) / 2;
13764 }
13765
13766 // also clip horizontal fades if necessary
13767 if (horizontalEdges && (left + length > right - length)) {
13768 length = (right - left) / 2;
13769 }
13770
13771 if (verticalEdges) {
13772 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013773 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013774 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013775 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013776 }
13777
13778 if (horizontalEdges) {
13779 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013780 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013781 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013782 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783 }
13784
13785 saveCount = canvas.getSaveCount();
13786
13787 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013788 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013789 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13790
13791 if (drawTop) {
13792 canvas.saveLayer(left, top, right, top + length, null, flags);
13793 }
13794
13795 if (drawBottom) {
13796 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13797 }
13798
13799 if (drawLeft) {
13800 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13801 }
13802
13803 if (drawRight) {
13804 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13805 }
13806 } else {
13807 scrollabilityCache.setFadeColor(solidColor);
13808 }
13809
13810 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013811 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013812
13813 // Step 4, draw the children
13814 dispatchDraw(canvas);
13815
13816 // Step 5, draw the fade effect and restore layers
13817 final Paint p = scrollabilityCache.paint;
13818 final Matrix matrix = scrollabilityCache.matrix;
13819 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820
13821 if (drawTop) {
13822 matrix.setScale(1, fadeHeight * topFadeStrength);
13823 matrix.postTranslate(left, top);
13824 fade.setLocalMatrix(matrix);
13825 canvas.drawRect(left, top, right, top + length, p);
13826 }
13827
13828 if (drawBottom) {
13829 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13830 matrix.postRotate(180);
13831 matrix.postTranslate(left, bottom);
13832 fade.setLocalMatrix(matrix);
13833 canvas.drawRect(left, bottom - length, right, bottom, p);
13834 }
13835
13836 if (drawLeft) {
13837 matrix.setScale(1, fadeHeight * leftFadeStrength);
13838 matrix.postRotate(-90);
13839 matrix.postTranslate(left, top);
13840 fade.setLocalMatrix(matrix);
13841 canvas.drawRect(left, top, left + length, bottom, p);
13842 }
13843
13844 if (drawRight) {
13845 matrix.setScale(1, fadeHeight * rightFadeStrength);
13846 matrix.postRotate(90);
13847 matrix.postTranslate(right, top);
13848 fade.setLocalMatrix(matrix);
13849 canvas.drawRect(right - length, top, right, bottom, p);
13850 }
13851
13852 canvas.restoreToCount(saveCount);
13853
13854 // Step 6, draw decorations (scrollbars)
13855 onDrawScrollBars(canvas);
13856 }
13857
13858 /**
13859 * Override this if your view is known to always be drawn on top of a solid color background,
13860 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13861 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13862 * should be set to 0xFF.
13863 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013864 * @see #setVerticalFadingEdgeEnabled(boolean)
13865 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013866 *
13867 * @return The known solid color background for this view, or 0 if the color may vary
13868 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013869 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013870 public int getSolidColor() {
13871 return 0;
13872 }
13873
13874 /**
13875 * Build a human readable string representation of the specified view flags.
13876 *
13877 * @param flags the view flags to convert to a string
13878 * @return a String representing the supplied flags
13879 */
13880 private static String printFlags(int flags) {
13881 String output = "";
13882 int numFlags = 0;
13883 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13884 output += "TAKES_FOCUS";
13885 numFlags++;
13886 }
13887
13888 switch (flags & VISIBILITY_MASK) {
13889 case INVISIBLE:
13890 if (numFlags > 0) {
13891 output += " ";
13892 }
13893 output += "INVISIBLE";
13894 // USELESS HERE numFlags++;
13895 break;
13896 case GONE:
13897 if (numFlags > 0) {
13898 output += " ";
13899 }
13900 output += "GONE";
13901 // USELESS HERE numFlags++;
13902 break;
13903 default:
13904 break;
13905 }
13906 return output;
13907 }
13908
13909 /**
13910 * Build a human readable string representation of the specified private
13911 * view flags.
13912 *
13913 * @param privateFlags the private view flags to convert to a string
13914 * @return a String representing the supplied flags
13915 */
13916 private static String printPrivateFlags(int privateFlags) {
13917 String output = "";
13918 int numFlags = 0;
13919
Dianne Hackborn4702a852012-08-17 15:18:29 -070013920 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 output += "WANTS_FOCUS";
13922 numFlags++;
13923 }
13924
Dianne Hackborn4702a852012-08-17 15:18:29 -070013925 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926 if (numFlags > 0) {
13927 output += " ";
13928 }
13929 output += "FOCUSED";
13930 numFlags++;
13931 }
13932
Dianne Hackborn4702a852012-08-17 15:18:29 -070013933 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 if (numFlags > 0) {
13935 output += " ";
13936 }
13937 output += "SELECTED";
13938 numFlags++;
13939 }
13940
Dianne Hackborn4702a852012-08-17 15:18:29 -070013941 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942 if (numFlags > 0) {
13943 output += " ";
13944 }
13945 output += "IS_ROOT_NAMESPACE";
13946 numFlags++;
13947 }
13948
Dianne Hackborn4702a852012-08-17 15:18:29 -070013949 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950 if (numFlags > 0) {
13951 output += " ";
13952 }
13953 output += "HAS_BOUNDS";
13954 numFlags++;
13955 }
13956
Dianne Hackborn4702a852012-08-17 15:18:29 -070013957 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013958 if (numFlags > 0) {
13959 output += " ";
13960 }
13961 output += "DRAWN";
13962 // USELESS HERE numFlags++;
13963 }
13964 return output;
13965 }
13966
13967 /**
13968 * <p>Indicates whether or not this view's layout will be requested during
13969 * the next hierarchy layout pass.</p>
13970 *
13971 * @return true if the layout will be forced during next layout pass
13972 */
13973 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013974 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 }
13976
13977 /**
Philip Milne7b757812012-09-19 18:13:44 -070013978 * Return true if o is a ViewGroup that is laying out using optical bounds.
13979 * @hide
13980 */
13981 public static boolean isLayoutModeOptical(Object o) {
13982 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
13983 }
13984
13985 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
13986 Insets parentInsets = mParent instanceof View ?
13987 ((View) mParent).getOpticalInsets() : Insets.NONE;
13988 Insets childInsets = getOpticalInsets();
13989 return setFrame(
13990 left + parentInsets.left - childInsets.left,
13991 top + parentInsets.top - childInsets.top,
13992 right + parentInsets.left + childInsets.right,
13993 bottom + parentInsets.top + childInsets.bottom);
13994 }
13995
13996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013997 * Assign a size and position to a view and all of its
13998 * descendants
13999 *
14000 * <p>This is the second phase of the layout mechanism.
14001 * (The first is measuring). In this phase, each parent calls
14002 * layout on all of its children to position them.
14003 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014004 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005 *
Chet Haase9c087442011-01-12 16:20:16 -080014006 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014007 * Derived classes with children should override
14008 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014009 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 *
14011 * @param l Left position, relative to parent
14012 * @param t Top position, relative to parent
14013 * @param r Right position, relative to parent
14014 * @param b Bottom position, relative to parent
14015 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014016 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014017 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014018 int oldL = mLeft;
14019 int oldT = mTop;
14020 int oldB = mBottom;
14021 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014022 boolean changed = isLayoutModeOptical(mParent) ?
14023 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014024 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014025 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014026 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014027
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014028 ListenerInfo li = mListenerInfo;
14029 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014030 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014031 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014032 int numListeners = listenersCopy.size();
14033 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014034 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014035 }
14036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014037 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014038 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039 }
14040
14041 /**
14042 * Called from layout when this view should
14043 * assign a size and position to each of its children.
14044 *
14045 * Derived classes with children should override
14046 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014047 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014048 * @param changed This is a new size or position for this view
14049 * @param left Left position, relative to parent
14050 * @param top Top position, relative to parent
14051 * @param right Right position, relative to parent
14052 * @param bottom Bottom position, relative to parent
14053 */
14054 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14055 }
14056
14057 /**
14058 * Assign a size and position to this view.
14059 *
14060 * This is called from layout.
14061 *
14062 * @param left Left position, relative to parent
14063 * @param top Top position, relative to parent
14064 * @param right Right position, relative to parent
14065 * @param bottom Bottom position, relative to parent
14066 * @return true if the new size and position are different than the
14067 * previous ones
14068 * {@hide}
14069 */
14070 protected boolean setFrame(int left, int top, int right, int bottom) {
14071 boolean changed = false;
14072
14073 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014074 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014075 + right + "," + bottom + ")");
14076 }
14077
14078 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14079 changed = true;
14080
14081 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014082 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014084 int oldWidth = mRight - mLeft;
14085 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014086 int newWidth = right - left;
14087 int newHeight = bottom - top;
14088 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14089
14090 // Invalidate our old position
14091 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092
14093 mLeft = left;
14094 mTop = top;
14095 mRight = right;
14096 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014097 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014098 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014100
Dianne Hackborn4702a852012-08-17 15:18:29 -070014101 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103
Chet Haase75755e22011-07-18 17:48:25 -070014104 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014105 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014106 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014107 if (mTransformationInfo != null) {
14108 mTransformationInfo.mMatrixDirty = true;
14109 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014111 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14112 }
14113
14114 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14115 // If we are visible, force the DRAWN bit to on so that
14116 // this invalidate will go through (at least to our parent).
14117 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014118 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014120 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014121 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014122 // parent display list may need to be recreated based on a change in the bounds
14123 // of any child
14124 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014125 }
14126
14127 // Reset drawn bit to original value (invalidate turns it off)
14128 mPrivateFlags |= drawn;
14129
14130 mBackgroundSizeChanged = true;
14131 }
14132 return changed;
14133 }
14134
14135 /**
14136 * Finalize inflating a view from XML. This is called as the last phase
14137 * of inflation, after all child views have been added.
14138 *
14139 * <p>Even if the subclass overrides onFinishInflate, they should always be
14140 * sure to call the super method, so that we get called.
14141 */
14142 protected void onFinishInflate() {
14143 }
14144
14145 /**
14146 * Returns the resources associated with this view.
14147 *
14148 * @return Resources object.
14149 */
14150 public Resources getResources() {
14151 return mResources;
14152 }
14153
14154 /**
14155 * Invalidates the specified Drawable.
14156 *
14157 * @param drawable the drawable to invalidate
14158 */
14159 public void invalidateDrawable(Drawable drawable) {
14160 if (verifyDrawable(drawable)) {
14161 final Rect dirty = drawable.getBounds();
14162 final int scrollX = mScrollX;
14163 final int scrollY = mScrollY;
14164
14165 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14166 dirty.right + scrollX, dirty.bottom + scrollY);
14167 }
14168 }
14169
14170 /**
14171 * Schedules an action on a drawable to occur at a specified time.
14172 *
14173 * @param who the recipient of the action
14174 * @param what the action to run on the drawable
14175 * @param when the time at which the action must occur. Uses the
14176 * {@link SystemClock#uptimeMillis} timebase.
14177 */
14178 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014179 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014180 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014181 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014182 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14183 Choreographer.CALLBACK_ANIMATION, what, who,
14184 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014185 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014186 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 }
14189 }
14190
14191 /**
14192 * Cancels a scheduled action on a drawable.
14193 *
14194 * @param who the recipient of the action
14195 * @param what the action to cancel
14196 */
14197 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014198 if (verifyDrawable(who) && what != null) {
14199 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014200 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14201 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014202 } else {
14203 ViewRootImpl.getRunQueue().removeCallbacks(what);
14204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014205 }
14206 }
14207
14208 /**
14209 * Unschedule any events associated with the given Drawable. This can be
14210 * used when selecting a new Drawable into a view, so that the previous
14211 * one is completely unscheduled.
14212 *
14213 * @param who The Drawable to unschedule.
14214 *
14215 * @see #drawableStateChanged
14216 */
14217 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014218 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014219 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14220 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014221 }
14222 }
14223
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014224 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014225 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14226 * that the View directionality can and will be resolved before its Drawables.
14227 *
14228 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014229 *
14230 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014231 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014232 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014233 if (canResolveLayoutDirection()) {
14234 if (mBackground != null) {
14235 mBackground.setLayoutDirection(getLayoutDirection());
14236 }
14237 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14238 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014239 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014240 }
14241
14242 /**
14243 * Called when layout direction has been resolved.
14244 *
14245 * The default implementation does nothing.
14246 *
14247 * @param layoutDirection The resolved layout direction.
14248 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014249 * @see #LAYOUT_DIRECTION_LTR
14250 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014251 *
14252 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014253 */
14254 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014255 }
14256
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014257 /**
14258 * @hide
14259 */
14260 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014261 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14262 }
14263
14264 private boolean isDrawablesResolved() {
14265 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14266 }
14267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 /**
14269 * If your view subclass is displaying its own Drawable objects, it should
14270 * override this function and return true for any Drawable it is
14271 * displaying. This allows animations for those drawables to be
14272 * scheduled.
14273 *
14274 * <p>Be sure to call through to the super class when overriding this
14275 * function.
14276 *
14277 * @param who The Drawable to verify. Return true if it is one you are
14278 * displaying, else return the result of calling through to the
14279 * super class.
14280 *
14281 * @return boolean If true than the Drawable is being displayed in the
14282 * view; else false and it is not allowed to animate.
14283 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014284 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14285 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014286 */
14287 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014288 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014289 }
14290
14291 /**
14292 * This function is called whenever the state of the view changes in such
14293 * a way that it impacts the state of drawables being shown.
14294 *
14295 * <p>Be sure to call through to the superclass when overriding this
14296 * function.
14297 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014298 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014299 */
14300 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014301 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 if (d != null && d.isStateful()) {
14303 d.setState(getDrawableState());
14304 }
14305 }
14306
14307 /**
14308 * Call this to force a view to update its drawable state. This will cause
14309 * drawableStateChanged to be called on this view. Views that are interested
14310 * in the new state should call getDrawableState.
14311 *
14312 * @see #drawableStateChanged
14313 * @see #getDrawableState
14314 */
14315 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014316 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014317 drawableStateChanged();
14318
14319 ViewParent parent = mParent;
14320 if (parent != null) {
14321 parent.childDrawableStateChanged(this);
14322 }
14323 }
14324
14325 /**
14326 * Return an array of resource IDs of the drawable states representing the
14327 * current state of the view.
14328 *
14329 * @return The current drawable state
14330 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014331 * @see Drawable#setState(int[])
14332 * @see #drawableStateChanged()
14333 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014334 */
14335 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014336 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014337 return mDrawableState;
14338 } else {
14339 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014340 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 return mDrawableState;
14342 }
14343 }
14344
14345 /**
14346 * Generate the new {@link android.graphics.drawable.Drawable} state for
14347 * this view. This is called by the view
14348 * system when the cached Drawable state is determined to be invalid. To
14349 * retrieve the current state, you should use {@link #getDrawableState}.
14350 *
14351 * @param extraSpace if non-zero, this is the number of extra entries you
14352 * would like in the returned array in which you can place your own
14353 * states.
14354 *
14355 * @return Returns an array holding the current {@link Drawable} state of
14356 * the view.
14357 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014358 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014359 */
14360 protected int[] onCreateDrawableState(int extraSpace) {
14361 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14362 mParent instanceof View) {
14363 return ((View) mParent).onCreateDrawableState(extraSpace);
14364 }
14365
14366 int[] drawableState;
14367
14368 int privateFlags = mPrivateFlags;
14369
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014370 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014371 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014372 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14373 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014374 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014375 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014376 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014377 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14378 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014379 // This is set if HW acceleration is requested, even if the current
14380 // process doesn't allow it. This is just to allow app preview
14381 // windows to better match their app.
14382 viewStateIndex |= VIEW_STATE_ACCELERATED;
14383 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014384 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014385
Christopher Tate3d4bf172011-03-28 16:16:46 -070014386 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014387 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14388 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014390 drawableState = VIEW_STATE_SETS[viewStateIndex];
14391
14392 //noinspection ConstantIfStatement
14393 if (false) {
14394 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14395 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014396 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014397 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14398 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014399 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014400 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014401 + ": " + Arrays.toString(drawableState));
14402 }
14403
14404 if (extraSpace == 0) {
14405 return drawableState;
14406 }
14407
14408 final int[] fullState;
14409 if (drawableState != null) {
14410 fullState = new int[drawableState.length + extraSpace];
14411 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14412 } else {
14413 fullState = new int[extraSpace];
14414 }
14415
14416 return fullState;
14417 }
14418
14419 /**
14420 * Merge your own state values in <var>additionalState</var> into the base
14421 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014422 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014423 *
14424 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014425 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 * own additional state values.
14427 *
14428 * @param additionalState The additional state values you would like
14429 * added to <var>baseState</var>; this array is not modified.
14430 *
14431 * @return As a convenience, the <var>baseState</var> array you originally
14432 * passed into the function is returned.
14433 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014434 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014435 */
14436 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14437 final int N = baseState.length;
14438 int i = N - 1;
14439 while (i >= 0 && baseState[i] == 0) {
14440 i--;
14441 }
14442 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14443 return baseState;
14444 }
14445
14446 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014447 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14448 * on all Drawable objects associated with this view.
14449 */
14450 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014451 if (mBackground != null) {
14452 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014453 }
14454 }
14455
14456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014457 * Sets the background color for this view.
14458 * @param color the color of the background
14459 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014460 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014461 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014462 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014463 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014464 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014465 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014466 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014467 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014469 }
14470
14471 /**
14472 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014473 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014474 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014476 * @attr ref android.R.styleable#View_background
14477 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014478 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 public void setBackgroundResource(int resid) {
14480 if (resid != 0 && resid == mBackgroundResource) {
14481 return;
14482 }
14483
14484 Drawable d= null;
14485 if (resid != 0) {
14486 d = mResources.getDrawable(resid);
14487 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014488 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489
14490 mBackgroundResource = resid;
14491 }
14492
14493 /**
14494 * Set the background to a given Drawable, or remove the background. If the
14495 * background has padding, this View's padding is set to the background's
14496 * padding. However, when a background is removed, this View's padding isn't
14497 * touched. If setting the padding is desired, please use
14498 * {@link #setPadding(int, int, int, int)}.
14499 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014500 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 * background
14502 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014503 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014504 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014505 setBackgroundDrawable(background);
14506 }
14507
14508 /**
14509 * @deprecated use {@link #setBackground(Drawable)} instead
14510 */
14511 @Deprecated
14512 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014513 computeOpaqueFlags();
14514
Philip Milne6c8ea062012-04-03 17:38:43 -070014515 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014516 return;
14517 }
14518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014519 boolean requestLayout = false;
14520
14521 mBackgroundResource = 0;
14522
14523 /*
14524 * Regardless of whether we're setting a new background or not, we want
14525 * to clear the previous drawable.
14526 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014527 if (mBackground != null) {
14528 mBackground.setCallback(null);
14529 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014530 }
14531
Philip Milne6c8ea062012-04-03 17:38:43 -070014532 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 Rect padding = sThreadLocal.get();
14534 if (padding == null) {
14535 padding = new Rect();
14536 sThreadLocal.set(padding);
14537 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014538 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014539 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014540 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014541 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014542 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014543 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014544 mUserPaddingLeftInitial = padding.right;
14545 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014546 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014547 break;
14548 case LAYOUT_DIRECTION_LTR:
14549 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014550 mUserPaddingLeftInitial = padding.left;
14551 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014552 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014554 }
14555
14556 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14557 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014558 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14559 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014560 requestLayout = true;
14561 }
14562
Philip Milne6c8ea062012-04-03 17:38:43 -070014563 background.setCallback(this);
14564 if (background.isStateful()) {
14565 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014566 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014567 background.setVisible(getVisibility() == VISIBLE, false);
14568 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569
Dianne Hackborn4702a852012-08-17 15:18:29 -070014570 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14571 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14572 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014573 requestLayout = true;
14574 }
14575 } else {
14576 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014577 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014578
Dianne Hackborn4702a852012-08-17 15:18:29 -070014579 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014580 /*
14581 * This view ONLY drew the background before and we're removing
14582 * the background, so now it won't draw anything
14583 * (hence we SKIP_DRAW)
14584 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014585 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14586 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014587 }
14588
14589 /*
14590 * When the background is set, we try to apply its padding to this
14591 * View. When the background is removed, we don't touch this View's
14592 * padding. This is noted in the Javadocs. Hence, we don't need to
14593 * requestLayout(), the invalidate() below is sufficient.
14594 */
14595
14596 // The old background's minimum size could have affected this
14597 // View's layout, so let's requestLayout
14598 requestLayout = true;
14599 }
14600
Romain Guy8f1344f52009-05-15 16:03:59 -070014601 computeOpaqueFlags();
14602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014603 if (requestLayout) {
14604 requestLayout();
14605 }
14606
14607 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014608 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014609 }
14610
14611 /**
14612 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014613 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014614 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014615 *
14616 * @see #setBackground(Drawable)
14617 *
14618 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014619 */
14620 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014621 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014622 }
14623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 /**
14625 * Sets the padding. The view may add on the space required to display
14626 * the scrollbars, depending on the style and visibility of the scrollbars.
14627 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14628 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14629 * from the values set in this call.
14630 *
14631 * @attr ref android.R.styleable#View_padding
14632 * @attr ref android.R.styleable#View_paddingBottom
14633 * @attr ref android.R.styleable#View_paddingLeft
14634 * @attr ref android.R.styleable#View_paddingRight
14635 * @attr ref android.R.styleable#View_paddingTop
14636 * @param left the left padding in pixels
14637 * @param top the top padding in pixels
14638 * @param right the right padding in pixels
14639 * @param bottom the bottom padding in pixels
14640 */
14641 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014642 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014643
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014644 mUserPaddingStart = UNDEFINED_PADDING;
14645 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014646
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014647 mUserPaddingLeftInitial = left;
14648 mUserPaddingRightInitial = right;
14649
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014650 internalSetPadding(left, top, right, bottom);
14651 }
14652
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014653 /**
14654 * @hide
14655 */
14656 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014657 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014658 mUserPaddingRight = right;
14659 mUserPaddingBottom = bottom;
14660
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014661 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014662 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014663
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014664 // Common case is there are no scroll bars.
14665 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014666 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014667 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014668 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014669 switch (mVerticalScrollbarPosition) {
14670 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014671 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014672 left += offset;
14673 } else {
14674 right += offset;
14675 }
14676 break;
Adam Powell20232d02010-12-08 21:08:53 -080014677 case SCROLLBAR_POSITION_RIGHT:
14678 right += offset;
14679 break;
14680 case SCROLLBAR_POSITION_LEFT:
14681 left += offset;
14682 break;
14683 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014684 }
Adam Powell20232d02010-12-08 21:08:53 -080014685 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014686 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14687 ? 0 : getHorizontalScrollbarHeight();
14688 }
14689 }
Romain Guy8506ab42009-06-11 17:35:47 -070014690
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014691 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014692 changed = true;
14693 mPaddingLeft = left;
14694 }
14695 if (mPaddingTop != top) {
14696 changed = true;
14697 mPaddingTop = top;
14698 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014699 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014700 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014701 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014702 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014703 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014704 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014705 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014706 }
14707
14708 if (changed) {
14709 requestLayout();
14710 }
14711 }
14712
14713 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014714 * Sets the relative padding. The view may add on the space required to display
14715 * the scrollbars, depending on the style and visibility of the scrollbars.
14716 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14717 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14718 * from the values set in this call.
14719 *
14720 * @attr ref android.R.styleable#View_padding
14721 * @attr ref android.R.styleable#View_paddingBottom
14722 * @attr ref android.R.styleable#View_paddingStart
14723 * @attr ref android.R.styleable#View_paddingEnd
14724 * @attr ref android.R.styleable#View_paddingTop
14725 * @param start the start padding in pixels
14726 * @param top the top padding in pixels
14727 * @param end the end padding in pixels
14728 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014729 */
14730 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014731 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014732
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014733 mUserPaddingStart = start;
14734 mUserPaddingEnd = end;
14735
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014736 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014737 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014738 mUserPaddingLeftInitial = end;
14739 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014740 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014741 break;
14742 case LAYOUT_DIRECTION_LTR:
14743 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014744 mUserPaddingLeftInitial = start;
14745 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014746 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014747 }
14748 }
14749
14750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014751 * Returns the top padding of this view.
14752 *
14753 * @return the top padding in pixels
14754 */
14755 public int getPaddingTop() {
14756 return mPaddingTop;
14757 }
14758
14759 /**
14760 * Returns the bottom padding of this view. If there are inset and enabled
14761 * scrollbars, this value may include the space required to display the
14762 * scrollbars as well.
14763 *
14764 * @return the bottom padding in pixels
14765 */
14766 public int getPaddingBottom() {
14767 return mPaddingBottom;
14768 }
14769
14770 /**
14771 * Returns the left padding of this view. If there are inset and enabled
14772 * scrollbars, this value may include the space required to display the
14773 * scrollbars as well.
14774 *
14775 * @return the left padding in pixels
14776 */
14777 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014778 if (!isPaddingResolved()) {
14779 resolvePadding();
14780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014781 return mPaddingLeft;
14782 }
14783
14784 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014785 * Returns the start padding of this view depending on its resolved layout direction.
14786 * If there are inset and enabled scrollbars, this value may include the space
14787 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014788 *
14789 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014790 */
14791 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014792 if (!isPaddingResolved()) {
14793 resolvePadding();
14794 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014795 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014796 mPaddingRight : mPaddingLeft;
14797 }
14798
14799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014800 * Returns the right padding of this view. If there are inset and enabled
14801 * scrollbars, this value may include the space required to display the
14802 * scrollbars as well.
14803 *
14804 * @return the right padding in pixels
14805 */
14806 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014807 if (!isPaddingResolved()) {
14808 resolvePadding();
14809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014810 return mPaddingRight;
14811 }
14812
14813 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014814 * Returns the end padding of this view depending on its resolved layout direction.
14815 * If there are inset and enabled scrollbars, this value may include the space
14816 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014817 *
14818 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014819 */
14820 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014821 if (!isPaddingResolved()) {
14822 resolvePadding();
14823 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014824 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014825 mPaddingLeft : mPaddingRight;
14826 }
14827
14828 /**
14829 * Return if the padding as been set thru relative values
14830 * {@link #setPaddingRelative(int, int, int, int)} or thru
14831 * @attr ref android.R.styleable#View_paddingStart or
14832 * @attr ref android.R.styleable#View_paddingEnd
14833 *
14834 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014835 */
14836 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014837 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014838 }
14839
Philip Milne7b757812012-09-19 18:13:44 -070014840 Insets computeOpticalInsets() {
14841 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014842 }
14843
14844 /**
14845 * @hide
14846 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014847 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014848 if (isRtlCompatibilityMode()) {
14849 mPaddingLeft = mUserPaddingLeftInitial;
14850 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014851 return;
14852 }
14853 if (isLayoutRtl()) {
14854 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14855 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014856 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014857 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14858 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014859 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014860 }
14861
14862 /**
14863 * @hide
14864 */
Philip Milne7b757812012-09-19 18:13:44 -070014865 public Insets getOpticalInsets() {
14866 if (mLayoutInsets == null) {
14867 mLayoutInsets = computeOpticalInsets();
14868 }
14869 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070014870 }
14871
14872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014873 * Changes the selection state of this view. A view can be selected or not.
14874 * Note that selection is not the same as focus. Views are typically
14875 * selected in the context of an AdapterView like ListView or GridView;
14876 * the selected view is the view that is highlighted.
14877 *
14878 * @param selected true if the view must be selected, false otherwise
14879 */
14880 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014881 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14882 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014883 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014884 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014885 refreshDrawableState();
14886 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014887 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14888 notifyAccessibilityStateChanged();
14889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014890 }
14891 }
14892
14893 /**
14894 * Dispatch setSelected to all of this View's children.
14895 *
14896 * @see #setSelected(boolean)
14897 *
14898 * @param selected The new selected state
14899 */
14900 protected void dispatchSetSelected(boolean selected) {
14901 }
14902
14903 /**
14904 * Indicates the selection state of this view.
14905 *
14906 * @return true if the view is selected, false otherwise
14907 */
14908 @ViewDebug.ExportedProperty
14909 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014910 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014911 }
14912
14913 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014914 * Changes the activated state of this view. A view can be activated or not.
14915 * Note that activation is not the same as selection. Selection is
14916 * a transient property, representing the view (hierarchy) the user is
14917 * currently interacting with. Activation is a longer-term state that the
14918 * user can move views in and out of. For example, in a list view with
14919 * single or multiple selection enabled, the views in the current selection
14920 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14921 * here.) The activated state is propagated down to children of the view it
14922 * is set on.
14923 *
14924 * @param activated true if the view must be activated, false otherwise
14925 */
14926 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014927 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14928 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014929 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014930 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014931 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014932 }
14933 }
14934
14935 /**
14936 * Dispatch setActivated to all of this View's children.
14937 *
14938 * @see #setActivated(boolean)
14939 *
14940 * @param activated The new activated state
14941 */
14942 protected void dispatchSetActivated(boolean activated) {
14943 }
14944
14945 /**
14946 * Indicates the activation state of this view.
14947 *
14948 * @return true if the view is activated, false otherwise
14949 */
14950 @ViewDebug.ExportedProperty
14951 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014952 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014953 }
14954
14955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014956 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14957 * observer can be used to get notifications when global events, like
14958 * layout, happen.
14959 *
14960 * The returned ViewTreeObserver observer is not guaranteed to remain
14961 * valid for the lifetime of this View. If the caller of this method keeps
14962 * a long-lived reference to ViewTreeObserver, it should always check for
14963 * the return value of {@link ViewTreeObserver#isAlive()}.
14964 *
14965 * @return The ViewTreeObserver for this view's hierarchy.
14966 */
14967 public ViewTreeObserver getViewTreeObserver() {
14968 if (mAttachInfo != null) {
14969 return mAttachInfo.mTreeObserver;
14970 }
14971 if (mFloatingTreeObserver == null) {
14972 mFloatingTreeObserver = new ViewTreeObserver();
14973 }
14974 return mFloatingTreeObserver;
14975 }
14976
14977 /**
14978 * <p>Finds the topmost view in the current view hierarchy.</p>
14979 *
14980 * @return the topmost view containing this view
14981 */
14982 public View getRootView() {
14983 if (mAttachInfo != null) {
14984 final View v = mAttachInfo.mRootView;
14985 if (v != null) {
14986 return v;
14987 }
14988 }
Romain Guy8506ab42009-06-11 17:35:47 -070014989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014990 View parent = this;
14991
14992 while (parent.mParent != null && parent.mParent instanceof View) {
14993 parent = (View) parent.mParent;
14994 }
14995
14996 return parent;
14997 }
14998
14999 /**
15000 * <p>Computes the coordinates of this view on the screen. The argument
15001 * must be an array of two integers. After the method returns, the array
15002 * contains the x and y location in that order.</p>
15003 *
15004 * @param location an array of two integers in which to hold the coordinates
15005 */
15006 public void getLocationOnScreen(int[] location) {
15007 getLocationInWindow(location);
15008
15009 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015010 if (info != null) {
15011 location[0] += info.mWindowLeft;
15012 location[1] += info.mWindowTop;
15013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015014 }
15015
15016 /**
15017 * <p>Computes the coordinates of this view in its window. The argument
15018 * must be an array of two integers. After the method returns, the array
15019 * contains the x and y location in that order.</p>
15020 *
15021 * @param location an array of two integers in which to hold the coordinates
15022 */
15023 public void getLocationInWindow(int[] location) {
15024 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015025 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015026 }
15027
Gilles Debunne6583ce52011-12-06 18:09:02 -080015028 if (mAttachInfo == null) {
15029 // When the view is not attached to a window, this method does not make sense
15030 location[0] = location[1] = 0;
15031 return;
15032 }
15033
Gilles Debunnecea45132011-11-24 02:19:27 +010015034 float[] position = mAttachInfo.mTmpTransformLocation;
15035 position[0] = position[1] = 0.0f;
15036
15037 if (!hasIdentityMatrix()) {
15038 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015040
Gilles Debunnecea45132011-11-24 02:19:27 +010015041 position[0] += mLeft;
15042 position[1] += mTop;
15043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015044 ViewParent viewParent = mParent;
15045 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015046 final View view = (View) viewParent;
15047
15048 position[0] -= view.mScrollX;
15049 position[1] -= view.mScrollY;
15050
15051 if (!view.hasIdentityMatrix()) {
15052 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015053 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015054
15055 position[0] += view.mLeft;
15056 position[1] += view.mTop;
15057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015058 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015059 }
Romain Guy8506ab42009-06-11 17:35:47 -070015060
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015061 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015062 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015063 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15064 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015066
15067 location[0] = (int) (position[0] + 0.5f);
15068 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015069 }
15070
15071 /**
15072 * {@hide}
15073 * @param id the id of the view to be found
15074 * @return the view of the specified id, null if cannot be found
15075 */
15076 protected View findViewTraversal(int id) {
15077 if (id == mID) {
15078 return this;
15079 }
15080 return null;
15081 }
15082
15083 /**
15084 * {@hide}
15085 * @param tag the tag of the view to be found
15086 * @return the view of specified tag, null if cannot be found
15087 */
15088 protected View findViewWithTagTraversal(Object tag) {
15089 if (tag != null && tag.equals(mTag)) {
15090 return this;
15091 }
15092 return null;
15093 }
15094
15095 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015096 * {@hide}
15097 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015098 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015099 * @return The first view that matches the predicate or null.
15100 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015101 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015102 if (predicate.apply(this)) {
15103 return this;
15104 }
15105 return null;
15106 }
15107
15108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015109 * Look for a child view with the given id. If this view has the given
15110 * id, return this view.
15111 *
15112 * @param id The id to search for.
15113 * @return The view that has the given id in the hierarchy or null
15114 */
15115 public final View findViewById(int id) {
15116 if (id < 0) {
15117 return null;
15118 }
15119 return findViewTraversal(id);
15120 }
15121
15122 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015123 * Finds a view by its unuque and stable accessibility id.
15124 *
15125 * @param accessibilityId The searched accessibility id.
15126 * @return The found view.
15127 */
15128 final View findViewByAccessibilityId(int accessibilityId) {
15129 if (accessibilityId < 0) {
15130 return null;
15131 }
15132 return findViewByAccessibilityIdTraversal(accessibilityId);
15133 }
15134
15135 /**
15136 * Performs the traversal to find a view by its unuque and stable accessibility id.
15137 *
15138 * <strong>Note:</strong>This method does not stop at the root namespace
15139 * boundary since the user can touch the screen at an arbitrary location
15140 * potentially crossing the root namespace bounday which will send an
15141 * accessibility event to accessibility services and they should be able
15142 * to obtain the event source. Also accessibility ids are guaranteed to be
15143 * unique in the window.
15144 *
15145 * @param accessibilityId The accessibility id.
15146 * @return The found view.
15147 */
15148 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15149 if (getAccessibilityViewId() == accessibilityId) {
15150 return this;
15151 }
15152 return null;
15153 }
15154
15155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015156 * Look for a child view with the given tag. If this view has the given
15157 * tag, return this view.
15158 *
15159 * @param tag The tag to search for, using "tag.equals(getTag())".
15160 * @return The View that has the given tag in the hierarchy or null
15161 */
15162 public final View findViewWithTag(Object tag) {
15163 if (tag == null) {
15164 return null;
15165 }
15166 return findViewWithTagTraversal(tag);
15167 }
15168
15169 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015170 * {@hide}
15171 * Look for a child view that matches the specified predicate.
15172 * If this view matches the predicate, return this view.
15173 *
15174 * @param predicate The predicate to evaluate.
15175 * @return The first view that matches the predicate or null.
15176 */
15177 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015178 return findViewByPredicateTraversal(predicate, null);
15179 }
15180
15181 /**
15182 * {@hide}
15183 * Look for a child view that matches the specified predicate,
15184 * starting with the specified view and its descendents and then
15185 * recusively searching the ancestors and siblings of that view
15186 * until this view is reached.
15187 *
15188 * This method is useful in cases where the predicate does not match
15189 * a single unique view (perhaps multiple views use the same id)
15190 * and we are trying to find the view that is "closest" in scope to the
15191 * starting view.
15192 *
15193 * @param start The view to start from.
15194 * @param predicate The predicate to evaluate.
15195 * @return The first view that matches the predicate or null.
15196 */
15197 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15198 View childToSkip = null;
15199 for (;;) {
15200 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15201 if (view != null || start == this) {
15202 return view;
15203 }
15204
15205 ViewParent parent = start.getParent();
15206 if (parent == null || !(parent instanceof View)) {
15207 return null;
15208 }
15209
15210 childToSkip = start;
15211 start = (View) parent;
15212 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015213 }
15214
15215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015216 * Sets the identifier for this view. The identifier does not have to be
15217 * unique in this view's hierarchy. The identifier should be a positive
15218 * number.
15219 *
15220 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015221 * @see #getId()
15222 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015223 *
15224 * @param id a number used to identify the view
15225 *
15226 * @attr ref android.R.styleable#View_id
15227 */
15228 public void setId(int id) {
15229 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015230 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15231 mID = generateViewId();
15232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015233 }
15234
15235 /**
15236 * {@hide}
15237 *
15238 * @param isRoot true if the view belongs to the root namespace, false
15239 * otherwise
15240 */
15241 public void setIsRootNamespace(boolean isRoot) {
15242 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015243 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015244 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015245 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015246 }
15247 }
15248
15249 /**
15250 * {@hide}
15251 *
15252 * @return true if the view belongs to the root namespace, false otherwise
15253 */
15254 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015255 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015256 }
15257
15258 /**
15259 * Returns this view's identifier.
15260 *
15261 * @return a positive integer used to identify the view or {@link #NO_ID}
15262 * if the view has no ID
15263 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015264 * @see #setId(int)
15265 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015266 * @attr ref android.R.styleable#View_id
15267 */
15268 @ViewDebug.CapturedViewProperty
15269 public int getId() {
15270 return mID;
15271 }
15272
15273 /**
15274 * Returns this view's tag.
15275 *
15276 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015277 *
15278 * @see #setTag(Object)
15279 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015280 */
15281 @ViewDebug.ExportedProperty
15282 public Object getTag() {
15283 return mTag;
15284 }
15285
15286 /**
15287 * Sets the tag associated with this view. A tag can be used to mark
15288 * a view in its hierarchy and does not have to be unique within the
15289 * hierarchy. Tags can also be used to store data within a view without
15290 * resorting to another data structure.
15291 *
15292 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015293 *
15294 * @see #getTag()
15295 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015296 */
15297 public void setTag(final Object tag) {
15298 mTag = tag;
15299 }
15300
15301 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015302 * Returns the tag associated with this view and the specified key.
15303 *
15304 * @param key The key identifying the tag
15305 *
15306 * @return the Object stored in this view as a tag
15307 *
15308 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015309 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015310 */
15311 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015312 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015313 return null;
15314 }
15315
15316 /**
15317 * Sets a tag associated with this view and a key. A tag can be used
15318 * to mark a view in its hierarchy and does not have to be unique within
15319 * the hierarchy. Tags can also be used to store data within a view
15320 * without resorting to another data structure.
15321 *
15322 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015323 * application to ensure it is unique (see the <a
15324 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15325 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015326 * the Android framework or not associated with any package will cause
15327 * an {@link IllegalArgumentException} to be thrown.
15328 *
15329 * @param key The key identifying the tag
15330 * @param tag An Object to tag the view with
15331 *
15332 * @throws IllegalArgumentException If they specified key is not valid
15333 *
15334 * @see #setTag(Object)
15335 * @see #getTag(int)
15336 */
15337 public void setTag(int key, final Object tag) {
15338 // If the package id is 0x00 or 0x01, it's either an undefined package
15339 // or a framework id
15340 if ((key >>> 24) < 2) {
15341 throw new IllegalArgumentException("The key must be an application-specific "
15342 + "resource id.");
15343 }
15344
Adam Powell2b2f6d62011-09-23 11:15:39 -070015345 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015346 }
15347
15348 /**
15349 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15350 * framework id.
15351 *
15352 * @hide
15353 */
15354 public void setTagInternal(int key, Object tag) {
15355 if ((key >>> 24) != 0x1) {
15356 throw new IllegalArgumentException("The key must be a framework-specific "
15357 + "resource id.");
15358 }
15359
Adam Powell2b2f6d62011-09-23 11:15:39 -070015360 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015361 }
15362
Adam Powell2b2f6d62011-09-23 11:15:39 -070015363 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015364 if (mKeyedTags == null) {
15365 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015366 }
15367
Adam Powell7db82ac2011-09-22 19:44:04 -070015368 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015369 }
15370
15371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015372 * Prints information about this view in the log output, with the tag
15373 * {@link #VIEW_LOG_TAG}.
15374 *
15375 * @hide
15376 */
15377 public void debug() {
15378 debug(0);
15379 }
15380
15381 /**
15382 * Prints information about this view in the log output, with the tag
15383 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15384 * indentation defined by the <code>depth</code>.
15385 *
15386 * @param depth the indentation level
15387 *
15388 * @hide
15389 */
15390 protected void debug(int depth) {
15391 String output = debugIndent(depth - 1);
15392
15393 output += "+ " + this;
15394 int id = getId();
15395 if (id != -1) {
15396 output += " (id=" + id + ")";
15397 }
15398 Object tag = getTag();
15399 if (tag != null) {
15400 output += " (tag=" + tag + ")";
15401 }
15402 Log.d(VIEW_LOG_TAG, output);
15403
Dianne Hackborn4702a852012-08-17 15:18:29 -070015404 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015405 output = debugIndent(depth) + " FOCUSED";
15406 Log.d(VIEW_LOG_TAG, output);
15407 }
15408
15409 output = debugIndent(depth);
15410 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15411 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15412 + "} ";
15413 Log.d(VIEW_LOG_TAG, output);
15414
15415 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15416 || mPaddingBottom != 0) {
15417 output = debugIndent(depth);
15418 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15419 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15420 Log.d(VIEW_LOG_TAG, output);
15421 }
15422
15423 output = debugIndent(depth);
15424 output += "mMeasureWidth=" + mMeasuredWidth +
15425 " mMeasureHeight=" + mMeasuredHeight;
15426 Log.d(VIEW_LOG_TAG, output);
15427
15428 output = debugIndent(depth);
15429 if (mLayoutParams == null) {
15430 output += "BAD! no layout params";
15431 } else {
15432 output = mLayoutParams.debug(output);
15433 }
15434 Log.d(VIEW_LOG_TAG, output);
15435
15436 output = debugIndent(depth);
15437 output += "flags={";
15438 output += View.printFlags(mViewFlags);
15439 output += "}";
15440 Log.d(VIEW_LOG_TAG, output);
15441
15442 output = debugIndent(depth);
15443 output += "privateFlags={";
15444 output += View.printPrivateFlags(mPrivateFlags);
15445 output += "}";
15446 Log.d(VIEW_LOG_TAG, output);
15447 }
15448
15449 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015450 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015451 *
15452 * @param depth the indentation level
15453 * @return a String containing (depth * 2 + 3) * 2 white spaces
15454 *
15455 * @hide
15456 */
15457 protected static String debugIndent(int depth) {
15458 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15459 for (int i = 0; i < (depth * 2) + 3; i++) {
15460 spaces.append(' ').append(' ');
15461 }
15462 return spaces.toString();
15463 }
15464
15465 /**
15466 * <p>Return the offset of the widget's text baseline from the widget's top
15467 * boundary. If this widget does not support baseline alignment, this
15468 * method returns -1. </p>
15469 *
15470 * @return the offset of the baseline within the widget's bounds or -1
15471 * if baseline alignment is not supported
15472 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015473 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015474 public int getBaseline() {
15475 return -1;
15476 }
15477
15478 /**
Chet Haase97140572012-09-13 14:56:47 -070015479 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15480 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15481 * a layout pass.
15482 *
15483 * @return whether the view hierarchy is currently undergoing a layout pass
15484 */
15485 public boolean isInLayout() {
15486 ViewRootImpl viewRoot = getViewRootImpl();
15487 return (viewRoot != null && viewRoot.isInLayout());
15488 }
15489
15490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015491 * Call this when something has changed which has invalidated the
15492 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015493 * tree. This should not be called while the view hierarchy is currently in a layout
15494 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15495 * end of the current layout pass (and then layout will run again) or after the current
15496 * frame is drawn and the next layout occurs.
15497 *
15498 * <p>Subclasses which override this method should call the superclass method to
15499 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015500 */
15501 public void requestLayout() {
Chet Haase97140572012-09-13 14:56:47 -070015502 ViewRootImpl viewRoot = getViewRootImpl();
15503 if (viewRoot != null && viewRoot.isInLayout()) {
15504 viewRoot.requestLayoutDuringLayout(this);
15505 return;
15506 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015507 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15508 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015509
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015510 if (mParent != null && !mParent.isLayoutRequested()) {
15511 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015512 }
15513 }
15514
15515 /**
15516 * Forces this view to be laid out during the next layout pass.
15517 * This method does not call requestLayout() or forceLayout()
15518 * on the parent.
15519 */
15520 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015521 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15522 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015523 }
15524
15525 /**
15526 * <p>
15527 * This is called to find out how big a view should be. The parent
15528 * supplies constraint information in the width and height parameters.
15529 * </p>
15530 *
15531 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015532 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015533 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015534 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015535 * </p>
15536 *
15537 *
15538 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15539 * parent
15540 * @param heightMeasureSpec Vertical space requirements as imposed by the
15541 * parent
15542 *
15543 * @see #onMeasure(int, int)
15544 */
15545 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015546 boolean optical = isLayoutModeOptical(this);
15547 if (optical != isLayoutModeOptical(mParent)) {
15548 Insets insets = getOpticalInsets();
15549 int oWidth = insets.left + insets.right;
15550 int oHeight = insets.top + insets.bottom;
15551 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15552 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15553 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015554 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015555 widthMeasureSpec != mOldWidthMeasureSpec ||
15556 heightMeasureSpec != mOldHeightMeasureSpec) {
15557
15558 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015559 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015560
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015561 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015563 // measure ourselves, this should set the measured dimension flag back
15564 onMeasure(widthMeasureSpec, heightMeasureSpec);
15565
15566 // flag not set, setMeasuredDimension() was not invoked, we raise
15567 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015568 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015569 throw new IllegalStateException("onMeasure() did not set the"
15570 + " measured dimension by calling"
15571 + " setMeasuredDimension()");
15572 }
15573
Dianne Hackborn4702a852012-08-17 15:18:29 -070015574 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015575 }
15576
15577 mOldWidthMeasureSpec = widthMeasureSpec;
15578 mOldHeightMeasureSpec = heightMeasureSpec;
15579 }
15580
15581 /**
15582 * <p>
15583 * Measure the view and its content to determine the measured width and the
15584 * measured height. This method is invoked by {@link #measure(int, int)} and
15585 * should be overriden by subclasses to provide accurate and efficient
15586 * measurement of their contents.
15587 * </p>
15588 *
15589 * <p>
15590 * <strong>CONTRACT:</strong> When overriding this method, you
15591 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15592 * measured width and height of this view. Failure to do so will trigger an
15593 * <code>IllegalStateException</code>, thrown by
15594 * {@link #measure(int, int)}. Calling the superclass'
15595 * {@link #onMeasure(int, int)} is a valid use.
15596 * </p>
15597 *
15598 * <p>
15599 * The base class implementation of measure defaults to the background size,
15600 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15601 * override {@link #onMeasure(int, int)} to provide better measurements of
15602 * their content.
15603 * </p>
15604 *
15605 * <p>
15606 * If this method is overridden, it is the subclass's responsibility to make
15607 * sure the measured height and width are at least the view's minimum height
15608 * and width ({@link #getSuggestedMinimumHeight()} and
15609 * {@link #getSuggestedMinimumWidth()}).
15610 * </p>
15611 *
15612 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15613 * The requirements are encoded with
15614 * {@link android.view.View.MeasureSpec}.
15615 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15616 * The requirements are encoded with
15617 * {@link android.view.View.MeasureSpec}.
15618 *
15619 * @see #getMeasuredWidth()
15620 * @see #getMeasuredHeight()
15621 * @see #setMeasuredDimension(int, int)
15622 * @see #getSuggestedMinimumHeight()
15623 * @see #getSuggestedMinimumWidth()
15624 * @see android.view.View.MeasureSpec#getMode(int)
15625 * @see android.view.View.MeasureSpec#getSize(int)
15626 */
15627 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15628 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15629 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15630 }
15631
15632 /**
15633 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15634 * measured width and measured height. Failing to do so will trigger an
15635 * exception at measurement time.</p>
15636 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015637 * @param measuredWidth The measured width of this view. May be a complex
15638 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15639 * {@link #MEASURED_STATE_TOO_SMALL}.
15640 * @param measuredHeight The measured height of this view. May be a complex
15641 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15642 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015643 */
15644 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015645 boolean optical = isLayoutModeOptical(this);
15646 if (optical != isLayoutModeOptical(mParent)) {
15647 Insets insets = getOpticalInsets();
15648 int opticalWidth = insets.left + insets.right;
15649 int opticalHeight = insets.top + insets.bottom;
15650
15651 measuredWidth += optical ? opticalWidth : -opticalWidth;
15652 measuredHeight += optical ? opticalHeight : -opticalHeight;
15653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015654 mMeasuredWidth = measuredWidth;
15655 mMeasuredHeight = measuredHeight;
15656
Dianne Hackborn4702a852012-08-17 15:18:29 -070015657 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015658 }
15659
15660 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015661 * Merge two states as returned by {@link #getMeasuredState()}.
15662 * @param curState The current state as returned from a view or the result
15663 * of combining multiple views.
15664 * @param newState The new view state to combine.
15665 * @return Returns a new integer reflecting the combination of the two
15666 * states.
15667 */
15668 public static int combineMeasuredStates(int curState, int newState) {
15669 return curState | newState;
15670 }
15671
15672 /**
15673 * Version of {@link #resolveSizeAndState(int, int, int)}
15674 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15675 */
15676 public static int resolveSize(int size, int measureSpec) {
15677 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15678 }
15679
15680 /**
15681 * Utility to reconcile a desired size and state, with constraints imposed
15682 * by a MeasureSpec. Will take the desired size, unless a different size
15683 * is imposed by the constraints. The returned value is a compound integer,
15684 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15685 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15686 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015687 *
15688 * @param size How big the view wants to be
15689 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015690 * @return Size information bit mask as defined by
15691 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015692 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015693 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015694 int result = size;
15695 int specMode = MeasureSpec.getMode(measureSpec);
15696 int specSize = MeasureSpec.getSize(measureSpec);
15697 switch (specMode) {
15698 case MeasureSpec.UNSPECIFIED:
15699 result = size;
15700 break;
15701 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015702 if (specSize < size) {
15703 result = specSize | MEASURED_STATE_TOO_SMALL;
15704 } else {
15705 result = size;
15706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015707 break;
15708 case MeasureSpec.EXACTLY:
15709 result = specSize;
15710 break;
15711 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015712 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015713 }
15714
15715 /**
15716 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015717 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015718 * by the MeasureSpec.
15719 *
15720 * @param size Default size for this view
15721 * @param measureSpec Constraints imposed by the parent
15722 * @return The size this view should be.
15723 */
15724 public static int getDefaultSize(int size, int measureSpec) {
15725 int result = size;
15726 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015727 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015728
15729 switch (specMode) {
15730 case MeasureSpec.UNSPECIFIED:
15731 result = size;
15732 break;
15733 case MeasureSpec.AT_MOST:
15734 case MeasureSpec.EXACTLY:
15735 result = specSize;
15736 break;
15737 }
15738 return result;
15739 }
15740
15741 /**
15742 * Returns the suggested minimum height that the view should use. This
15743 * returns the maximum of the view's minimum height
15744 * and the background's minimum height
15745 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15746 * <p>
15747 * When being used in {@link #onMeasure(int, int)}, the caller should still
15748 * ensure the returned height is within the requirements of the parent.
15749 *
15750 * @return The suggested minimum height of the view.
15751 */
15752 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015753 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015755 }
15756
15757 /**
15758 * Returns the suggested minimum width that the view should use. This
15759 * returns the maximum of the view's minimum width)
15760 * and the background's minimum width
15761 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15762 * <p>
15763 * When being used in {@link #onMeasure(int, int)}, the caller should still
15764 * ensure the returned width is within the requirements of the parent.
15765 *
15766 * @return The suggested minimum width of the view.
15767 */
15768 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015769 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015771
Philip Milne6c8ea062012-04-03 17:38:43 -070015772 /**
15773 * Returns the minimum height of the view.
15774 *
15775 * @return the minimum height the view will try to be.
15776 *
15777 * @see #setMinimumHeight(int)
15778 *
15779 * @attr ref android.R.styleable#View_minHeight
15780 */
15781 public int getMinimumHeight() {
15782 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015783 }
15784
15785 /**
15786 * Sets the minimum height of the view. It is not guaranteed the view will
15787 * be able to achieve this minimum height (for example, if its parent layout
15788 * constrains it with less available height).
15789 *
15790 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015791 *
15792 * @see #getMinimumHeight()
15793 *
15794 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015795 */
15796 public void setMinimumHeight(int minHeight) {
15797 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015798 requestLayout();
15799 }
15800
15801 /**
15802 * Returns the minimum width of the view.
15803 *
15804 * @return the minimum width the view will try to be.
15805 *
15806 * @see #setMinimumWidth(int)
15807 *
15808 * @attr ref android.R.styleable#View_minWidth
15809 */
15810 public int getMinimumWidth() {
15811 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015812 }
15813
15814 /**
15815 * Sets the minimum width of the view. It is not guaranteed the view will
15816 * be able to achieve this minimum width (for example, if its parent layout
15817 * constrains it with less available width).
15818 *
15819 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015820 *
15821 * @see #getMinimumWidth()
15822 *
15823 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015824 */
15825 public void setMinimumWidth(int minWidth) {
15826 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015827 requestLayout();
15828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015829 }
15830
15831 /**
15832 * Get the animation currently associated with this view.
15833 *
15834 * @return The animation that is currently playing or
15835 * scheduled to play for this view.
15836 */
15837 public Animation getAnimation() {
15838 return mCurrentAnimation;
15839 }
15840
15841 /**
15842 * Start the specified animation now.
15843 *
15844 * @param animation the animation to start now
15845 */
15846 public void startAnimation(Animation animation) {
15847 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15848 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015849 invalidateParentCaches();
15850 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015851 }
15852
15853 /**
15854 * Cancels any animations for this view.
15855 */
15856 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015857 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015858 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015860 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015861 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015862 }
15863
15864 /**
15865 * Sets the next animation to play for this view.
15866 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015867 * {@link #startAnimation(android.view.animation.Animation)} instead.
15868 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015869 * control over the start time and invalidation, but you
15870 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015871 * 2) the view's parent (which controls animations on its children)
15872 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015873 * start.
15874 *
15875 * @param animation The next animation, or null.
15876 */
15877 public void setAnimation(Animation animation) {
15878 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015880 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015881 // If the screen is off assume the animation start time is now instead of
15882 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15883 // would cause the animation to start when the screen turns back on
15884 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15885 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15886 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015888 animation.reset();
15889 }
15890 }
15891
15892 /**
15893 * Invoked by a parent ViewGroup to notify the start of the animation
15894 * currently associated with this view. If you override this method,
15895 * always call super.onAnimationStart();
15896 *
15897 * @see #setAnimation(android.view.animation.Animation)
15898 * @see #getAnimation()
15899 */
15900 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015901 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015902 }
15903
15904 /**
15905 * Invoked by a parent ViewGroup to notify the end of the animation
15906 * currently associated with this view. If you override this method,
15907 * always call super.onAnimationEnd();
15908 *
15909 * @see #setAnimation(android.view.animation.Animation)
15910 * @see #getAnimation()
15911 */
15912 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015913 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015914 }
15915
15916 /**
15917 * Invoked if there is a Transform that involves alpha. Subclass that can
15918 * draw themselves with the specified alpha should return true, and then
15919 * respect that alpha when their onDraw() is called. If this returns false
15920 * then the view may be redirected to draw into an offscreen buffer to
15921 * fulfill the request, which will look fine, but may be slower than if the
15922 * subclass handles it internally. The default implementation returns false.
15923 *
15924 * @param alpha The alpha (0..255) to apply to the view's drawing
15925 * @return true if the view can draw with the specified alpha.
15926 */
15927 protected boolean onSetAlpha(int alpha) {
15928 return false;
15929 }
15930
15931 /**
15932 * This is used by the RootView to perform an optimization when
15933 * the view hierarchy contains one or several SurfaceView.
15934 * SurfaceView is always considered transparent, but its children are not,
15935 * therefore all View objects remove themselves from the global transparent
15936 * region (passed as a parameter to this function).
15937 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015938 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015939 *
15940 * @return Returns true if the effective visibility of the view at this
15941 * point is opaque, regardless of the transparent region; returns false
15942 * if it is possible for underlying windows to be seen behind the view.
15943 *
15944 * {@hide}
15945 */
15946 public boolean gatherTransparentRegion(Region region) {
15947 final AttachInfo attachInfo = mAttachInfo;
15948 if (region != null && attachInfo != null) {
15949 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015950 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015951 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15952 // remove it from the transparent region.
15953 final int[] location = attachInfo.mTransparentLocation;
15954 getLocationInWindow(location);
15955 region.op(location[0], location[1], location[0] + mRight - mLeft,
15956 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015957 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015958 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15959 // exists, so we remove the background drawable's non-transparent
15960 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015961 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015962 }
15963 }
15964 return true;
15965 }
15966
15967 /**
15968 * Play a sound effect for this view.
15969 *
15970 * <p>The framework will play sound effects for some built in actions, such as
15971 * clicking, but you may wish to play these effects in your widget,
15972 * for instance, for internal navigation.
15973 *
15974 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15975 * {@link #isSoundEffectsEnabled()} is true.
15976 *
15977 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15978 */
15979 public void playSoundEffect(int soundConstant) {
15980 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15981 return;
15982 }
15983 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15984 }
15985
15986 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015987 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015988 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015989 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015990 *
15991 * <p>The framework will provide haptic feedback for some built in actions,
15992 * such as long presses, but you may wish to provide feedback for your
15993 * own widget.
15994 *
15995 * <p>The feedback will only be performed if
15996 * {@link #isHapticFeedbackEnabled()} is true.
15997 *
15998 * @param feedbackConstant One of the constants defined in
15999 * {@link HapticFeedbackConstants}
16000 */
16001 public boolean performHapticFeedback(int feedbackConstant) {
16002 return performHapticFeedback(feedbackConstant, 0);
16003 }
16004
16005 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016006 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016007 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016008 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016009 *
16010 * @param feedbackConstant One of the constants defined in
16011 * {@link HapticFeedbackConstants}
16012 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16013 */
16014 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16015 if (mAttachInfo == null) {
16016 return false;
16017 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016018 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016019 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016020 && !isHapticFeedbackEnabled()) {
16021 return false;
16022 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016023 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16024 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016025 }
16026
16027 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016028 * Request that the visibility of the status bar or other screen/window
16029 * decorations be changed.
16030 *
16031 * <p>This method is used to put the over device UI into temporary modes
16032 * where the user's attention is focused more on the application content,
16033 * by dimming or hiding surrounding system affordances. This is typically
16034 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16035 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16036 * to be placed behind the action bar (and with these flags other system
16037 * affordances) so that smooth transitions between hiding and showing them
16038 * can be done.
16039 *
16040 * <p>Two representative examples of the use of system UI visibility is
16041 * implementing a content browsing application (like a magazine reader)
16042 * and a video playing application.
16043 *
16044 * <p>The first code shows a typical implementation of a View in a content
16045 * browsing application. In this implementation, the application goes
16046 * into a content-oriented mode by hiding the status bar and action bar,
16047 * and putting the navigation elements into lights out mode. The user can
16048 * then interact with content while in this mode. Such an application should
16049 * provide an easy way for the user to toggle out of the mode (such as to
16050 * check information in the status bar or access notifications). In the
16051 * implementation here, this is done simply by tapping on the content.
16052 *
16053 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16054 * content}
16055 *
16056 * <p>This second code sample shows a typical implementation of a View
16057 * in a video playing application. In this situation, while the video is
16058 * playing the application would like to go into a complete full-screen mode,
16059 * to use as much of the display as possible for the video. When in this state
16060 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016061 * touching on the screen to pop the UI out of full screen mode. See
16062 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016063 *
16064 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16065 * content}
16066 *
16067 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16068 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16069 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16070 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016071 */
16072 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016073 if (visibility != mSystemUiVisibility) {
16074 mSystemUiVisibility = visibility;
16075 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16076 mParent.recomputeViewAttributes(this);
16077 }
Joe Onorato664644d2011-01-23 17:53:23 -080016078 }
16079 }
16080
16081 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016082 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16083 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16084 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16085 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16086 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016087 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016088 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016089 return mSystemUiVisibility;
16090 }
16091
Scott Mainec6331b2011-05-24 16:55:56 -070016092 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016093 * Returns the current system UI visibility that is currently set for
16094 * the entire window. This is the combination of the
16095 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16096 * views in the window.
16097 */
16098 public int getWindowSystemUiVisibility() {
16099 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16100 }
16101
16102 /**
16103 * Override to find out when the window's requested system UI visibility
16104 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16105 * This is different from the callbacks recieved through
16106 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16107 * in that this is only telling you about the local request of the window,
16108 * not the actual values applied by the system.
16109 */
16110 public void onWindowSystemUiVisibilityChanged(int visible) {
16111 }
16112
16113 /**
16114 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16115 * the view hierarchy.
16116 */
16117 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16118 onWindowSystemUiVisibilityChanged(visible);
16119 }
16120
16121 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016122 * Set a listener to receive callbacks when the visibility of the system bar changes.
16123 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16124 */
Joe Onorato664644d2011-01-23 17:53:23 -080016125 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016126 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016127 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16128 mParent.recomputeViewAttributes(this);
16129 }
16130 }
16131
16132 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016133 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16134 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016135 */
16136 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016137 ListenerInfo li = mListenerInfo;
16138 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16139 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016140 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016141 }
16142 }
16143
Dianne Hackborncf675782012-05-10 15:07:24 -070016144 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016145 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16146 if (val != mSystemUiVisibility) {
16147 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016148 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016149 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016150 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016151 }
16152
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016153 /** @hide */
16154 public void setDisabledSystemUiVisibility(int flags) {
16155 if (mAttachInfo != null) {
16156 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16157 mAttachInfo.mDisabledSystemUiVisibility = flags;
16158 if (mParent != null) {
16159 mParent.recomputeViewAttributes(this);
16160 }
16161 }
16162 }
16163 }
16164
Joe Onorato664644d2011-01-23 17:53:23 -080016165 /**
Joe Malin32736f02011-01-19 16:14:20 -080016166 * Creates an image that the system displays during the drag and drop
16167 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16168 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16169 * appearance as the given View. The default also positions the center of the drag shadow
16170 * directly under the touch point. If no View is provided (the constructor with no parameters
16171 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16172 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16173 * default is an invisible drag shadow.
16174 * <p>
16175 * You are not required to use the View you provide to the constructor as the basis of the
16176 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16177 * anything you want as the drag shadow.
16178 * </p>
16179 * <p>
16180 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16181 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16182 * size and position of the drag shadow. It uses this data to construct a
16183 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16184 * so that your application can draw the shadow image in the Canvas.
16185 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016186 *
16187 * <div class="special reference">
16188 * <h3>Developer Guides</h3>
16189 * <p>For a guide to implementing drag and drop features, read the
16190 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16191 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016192 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016193 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016194 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016195
16196 /**
Joe Malin32736f02011-01-19 16:14:20 -080016197 * Constructs a shadow image builder based on a View. By default, the resulting drag
16198 * shadow will have the same appearance and dimensions as the View, with the touch point
16199 * over the center of the View.
16200 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016201 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016202 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016203 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016204 }
16205
Christopher Tate17ed60c2011-01-18 12:50:26 -080016206 /**
16207 * Construct a shadow builder object with no associated View. This
16208 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16209 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16210 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016211 * reference to any View object. If they are not overridden, then the result is an
16212 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016213 */
16214 public DragShadowBuilder() {
16215 mView = new WeakReference<View>(null);
16216 }
16217
16218 /**
16219 * Returns the View object that had been passed to the
16220 * {@link #View.DragShadowBuilder(View)}
16221 * constructor. If that View parameter was {@code null} or if the
16222 * {@link #View.DragShadowBuilder()}
16223 * constructor was used to instantiate the builder object, this method will return
16224 * null.
16225 *
16226 * @return The View object associate with this builder object.
16227 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016228 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016229 final public View getView() {
16230 return mView.get();
16231 }
16232
Christopher Tate2c095f32010-10-04 14:13:40 -070016233 /**
Joe Malin32736f02011-01-19 16:14:20 -080016234 * Provides the metrics for the shadow image. These include the dimensions of
16235 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016236 * be centered under the touch location while dragging.
16237 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016238 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016239 * same as the dimensions of the View itself and centers the shadow under
16240 * the touch point.
16241 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016242 *
Joe Malin32736f02011-01-19 16:14:20 -080016243 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16244 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16245 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16246 * image.
16247 *
16248 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16249 * shadow image that should be underneath the touch point during the drag and drop
16250 * operation. Your application must set {@link android.graphics.Point#x} to the
16251 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016252 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016253 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016254 final View view = mView.get();
16255 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016256 shadowSize.set(view.getWidth(), view.getHeight());
16257 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016258 } else {
16259 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16260 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016261 }
16262
16263 /**
Joe Malin32736f02011-01-19 16:14:20 -080016264 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16265 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016266 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016267 *
Joe Malin32736f02011-01-19 16:14:20 -080016268 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016269 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016270 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016271 final View view = mView.get();
16272 if (view != null) {
16273 view.draw(canvas);
16274 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016275 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016276 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016277 }
16278 }
16279
16280 /**
Joe Malin32736f02011-01-19 16:14:20 -080016281 * Starts a drag and drop operation. When your application calls this method, it passes a
16282 * {@link android.view.View.DragShadowBuilder} object to the system. The
16283 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16284 * to get metrics for the drag shadow, and then calls the object's
16285 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16286 * <p>
16287 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16288 * drag events to all the View objects in your application that are currently visible. It does
16289 * this either by calling the View object's drag listener (an implementation of
16290 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16291 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16292 * Both are passed a {@link android.view.DragEvent} object that has a
16293 * {@link android.view.DragEvent#getAction()} value of
16294 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16295 * </p>
16296 * <p>
16297 * Your application can invoke startDrag() on any attached View object. The View object does not
16298 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16299 * be related to the View the user selected for dragging.
16300 * </p>
16301 * @param data A {@link android.content.ClipData} object pointing to the data to be
16302 * transferred by the drag and drop operation.
16303 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16304 * drag shadow.
16305 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16306 * drop operation. This Object is put into every DragEvent object sent by the system during the
16307 * current drag.
16308 * <p>
16309 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16310 * to the target Views. For example, it can contain flags that differentiate between a
16311 * a copy operation and a move operation.
16312 * </p>
16313 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16314 * so the parameter should be set to 0.
16315 * @return {@code true} if the method completes successfully, or
16316 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16317 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016318 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016319 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016320 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016321 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016322 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016323 }
16324 boolean okay = false;
16325
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016326 Point shadowSize = new Point();
16327 Point shadowTouchPoint = new Point();
16328 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016329
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016330 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16331 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16332 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016333 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016334
Chris Tatea32dcf72010-10-14 12:13:50 -070016335 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016336 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16337 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016338 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016339 Surface surface = new Surface();
16340 try {
16341 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016342 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016343 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016344 + " surface=" + surface);
16345 if (token != null) {
16346 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016347 try {
Chris Tate6b391282010-10-14 15:48:59 -070016348 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016349 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016350 } finally {
16351 surface.unlockCanvasAndPost(canvas);
16352 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016353
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016354 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016355
16356 // Cache the local state object for delivery with DragEvents
16357 root.setLocalDragState(myLocalState);
16358
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016359 // repurpose 'shadowSize' for the last touch point
16360 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016361
Christopher Tatea53146c2010-09-07 11:57:52 -070016362 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016363 shadowSize.x, shadowSize.y,
16364 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016365 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016366
16367 // Off and running! Release our local surface instance; the drag
16368 // shadow surface is now managed by the system process.
16369 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016370 }
16371 } catch (Exception e) {
16372 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16373 surface.destroy();
16374 }
16375
16376 return okay;
16377 }
16378
Christopher Tatea53146c2010-09-07 11:57:52 -070016379 /**
Joe Malin32736f02011-01-19 16:14:20 -080016380 * Handles drag events sent by the system following a call to
16381 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16382 *<p>
16383 * When the system calls this method, it passes a
16384 * {@link android.view.DragEvent} object. A call to
16385 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16386 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16387 * operation.
16388 * @param event The {@link android.view.DragEvent} sent by the system.
16389 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16390 * in DragEvent, indicating the type of drag event represented by this object.
16391 * @return {@code true} if the method was successful, otherwise {@code false}.
16392 * <p>
16393 * The method should return {@code true} in response to an action type of
16394 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16395 * operation.
16396 * </p>
16397 * <p>
16398 * The method should also return {@code true} in response to an action type of
16399 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16400 * {@code false} if it didn't.
16401 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016402 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016403 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016404 return false;
16405 }
16406
16407 /**
Joe Malin32736f02011-01-19 16:14:20 -080016408 * Detects if this View is enabled and has a drag event listener.
16409 * If both are true, then it calls the drag event listener with the
16410 * {@link android.view.DragEvent} it received. If the drag event listener returns
16411 * {@code true}, then dispatchDragEvent() returns {@code true}.
16412 * <p>
16413 * For all other cases, the method calls the
16414 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16415 * method and returns its result.
16416 * </p>
16417 * <p>
16418 * This ensures that a drag event is always consumed, even if the View does not have a drag
16419 * event listener. However, if the View has a listener and the listener returns true, then
16420 * onDragEvent() is not called.
16421 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016422 */
16423 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016424 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016425 ListenerInfo li = mListenerInfo;
16426 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16427 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016428 return true;
16429 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016430 return onDragEvent(event);
16431 }
16432
Christopher Tate3d4bf172011-03-28 16:16:46 -070016433 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016434 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016435 }
16436
Christopher Tatea53146c2010-09-07 11:57:52 -070016437 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016438 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16439 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016440 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016441 */
16442 public void onCloseSystemDialogs(String reason) {
16443 }
Joe Malin32736f02011-01-19 16:14:20 -080016444
Dianne Hackbornffa42482009-09-23 22:20:11 -070016445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016446 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016447 * update a Region being computed for
16448 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016449 * that any non-transparent parts of the Drawable are removed from the
16450 * given transparent region.
16451 *
16452 * @param dr The Drawable whose transparency is to be applied to the region.
16453 * @param region A Region holding the current transparency information,
16454 * where any parts of the region that are set are considered to be
16455 * transparent. On return, this region will be modified to have the
16456 * transparency information reduced by the corresponding parts of the
16457 * Drawable that are not transparent.
16458 * {@hide}
16459 */
16460 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16461 if (DBG) {
16462 Log.i("View", "Getting transparent region for: " + this);
16463 }
16464 final Region r = dr.getTransparentRegion();
16465 final Rect db = dr.getBounds();
16466 final AttachInfo attachInfo = mAttachInfo;
16467 if (r != null && attachInfo != null) {
16468 final int w = getRight()-getLeft();
16469 final int h = getBottom()-getTop();
16470 if (db.left > 0) {
16471 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16472 r.op(0, 0, db.left, h, Region.Op.UNION);
16473 }
16474 if (db.right < w) {
16475 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16476 r.op(db.right, 0, w, h, Region.Op.UNION);
16477 }
16478 if (db.top > 0) {
16479 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16480 r.op(0, 0, w, db.top, Region.Op.UNION);
16481 }
16482 if (db.bottom < h) {
16483 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16484 r.op(0, db.bottom, w, h, Region.Op.UNION);
16485 }
16486 final int[] location = attachInfo.mTransparentLocation;
16487 getLocationInWindow(location);
16488 r.translate(location[0], location[1]);
16489 region.op(r, Region.Op.INTERSECT);
16490 } else {
16491 region.op(db, Region.Op.DIFFERENCE);
16492 }
16493 }
16494
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016495 private void checkForLongClick(int delayOffset) {
16496 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16497 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016498
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016499 if (mPendingCheckForLongPress == null) {
16500 mPendingCheckForLongPress = new CheckForLongPress();
16501 }
16502 mPendingCheckForLongPress.rememberWindowAttachCount();
16503 postDelayed(mPendingCheckForLongPress,
16504 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016506 }
16507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016508 /**
16509 * Inflate a view from an XML resource. This convenience method wraps the {@link
16510 * LayoutInflater} class, which provides a full range of options for view inflation.
16511 *
16512 * @param context The Context object for your activity or application.
16513 * @param resource The resource ID to inflate
16514 * @param root A view group that will be the parent. Used to properly inflate the
16515 * layout_* parameters.
16516 * @see LayoutInflater
16517 */
16518 public static View inflate(Context context, int resource, ViewGroup root) {
16519 LayoutInflater factory = LayoutInflater.from(context);
16520 return factory.inflate(resource, root);
16521 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016523 /**
Adam Powell637d3372010-08-25 14:37:03 -070016524 * Scroll the view with standard behavior for scrolling beyond the normal
16525 * content boundaries. Views that call this method should override
16526 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16527 * results of an over-scroll operation.
16528 *
16529 * Views can use this method to handle any touch or fling-based scrolling.
16530 *
16531 * @param deltaX Change in X in pixels
16532 * @param deltaY Change in Y in pixels
16533 * @param scrollX Current X scroll value in pixels before applying deltaX
16534 * @param scrollY Current Y scroll value in pixels before applying deltaY
16535 * @param scrollRangeX Maximum content scroll range along the X axis
16536 * @param scrollRangeY Maximum content scroll range along the Y axis
16537 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16538 * along the X axis.
16539 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16540 * along the Y axis.
16541 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16542 * @return true if scrolling was clamped to an over-scroll boundary along either
16543 * axis, false otherwise.
16544 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016545 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016546 protected boolean overScrollBy(int deltaX, int deltaY,
16547 int scrollX, int scrollY,
16548 int scrollRangeX, int scrollRangeY,
16549 int maxOverScrollX, int maxOverScrollY,
16550 boolean isTouchEvent) {
16551 final int overScrollMode = mOverScrollMode;
16552 final boolean canScrollHorizontal =
16553 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16554 final boolean canScrollVertical =
16555 computeVerticalScrollRange() > computeVerticalScrollExtent();
16556 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16557 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16558 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16559 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16560
16561 int newScrollX = scrollX + deltaX;
16562 if (!overScrollHorizontal) {
16563 maxOverScrollX = 0;
16564 }
16565
16566 int newScrollY = scrollY + deltaY;
16567 if (!overScrollVertical) {
16568 maxOverScrollY = 0;
16569 }
16570
16571 // Clamp values if at the limits and record
16572 final int left = -maxOverScrollX;
16573 final int right = maxOverScrollX + scrollRangeX;
16574 final int top = -maxOverScrollY;
16575 final int bottom = maxOverScrollY + scrollRangeY;
16576
16577 boolean clampedX = false;
16578 if (newScrollX > right) {
16579 newScrollX = right;
16580 clampedX = true;
16581 } else if (newScrollX < left) {
16582 newScrollX = left;
16583 clampedX = true;
16584 }
16585
16586 boolean clampedY = false;
16587 if (newScrollY > bottom) {
16588 newScrollY = bottom;
16589 clampedY = true;
16590 } else if (newScrollY < top) {
16591 newScrollY = top;
16592 clampedY = true;
16593 }
16594
16595 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16596
16597 return clampedX || clampedY;
16598 }
16599
16600 /**
16601 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16602 * respond to the results of an over-scroll operation.
16603 *
16604 * @param scrollX New X scroll value in pixels
16605 * @param scrollY New Y scroll value in pixels
16606 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16607 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16608 */
16609 protected void onOverScrolled(int scrollX, int scrollY,
16610 boolean clampedX, boolean clampedY) {
16611 // Intentionally empty.
16612 }
16613
16614 /**
16615 * Returns the over-scroll mode for this view. The result will be
16616 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16617 * (allow over-scrolling only if the view content is larger than the container),
16618 * or {@link #OVER_SCROLL_NEVER}.
16619 *
16620 * @return This view's over-scroll mode.
16621 */
16622 public int getOverScrollMode() {
16623 return mOverScrollMode;
16624 }
16625
16626 /**
16627 * Set the over-scroll mode for this view. Valid over-scroll modes are
16628 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16629 * (allow over-scrolling only if the view content is larger than the container),
16630 * or {@link #OVER_SCROLL_NEVER}.
16631 *
16632 * Setting the over-scroll mode of a view will have an effect only if the
16633 * view is capable of scrolling.
16634 *
16635 * @param overScrollMode The new over-scroll mode for this view.
16636 */
16637 public void setOverScrollMode(int overScrollMode) {
16638 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16639 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16640 overScrollMode != OVER_SCROLL_NEVER) {
16641 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16642 }
16643 mOverScrollMode = overScrollMode;
16644 }
16645
16646 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016647 * Gets a scale factor that determines the distance the view should scroll
16648 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16649 * @return The vertical scroll scale factor.
16650 * @hide
16651 */
16652 protected float getVerticalScrollFactor() {
16653 if (mVerticalScrollFactor == 0) {
16654 TypedValue outValue = new TypedValue();
16655 if (!mContext.getTheme().resolveAttribute(
16656 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16657 throw new IllegalStateException(
16658 "Expected theme to define listPreferredItemHeight.");
16659 }
16660 mVerticalScrollFactor = outValue.getDimension(
16661 mContext.getResources().getDisplayMetrics());
16662 }
16663 return mVerticalScrollFactor;
16664 }
16665
16666 /**
16667 * Gets a scale factor that determines the distance the view should scroll
16668 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16669 * @return The horizontal scroll scale factor.
16670 * @hide
16671 */
16672 protected float getHorizontalScrollFactor() {
16673 // TODO: Should use something else.
16674 return getVerticalScrollFactor();
16675 }
16676
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016677 /**
16678 * Return the value specifying the text direction or policy that was set with
16679 * {@link #setTextDirection(int)}.
16680 *
16681 * @return the defined text direction. It can be one of:
16682 *
16683 * {@link #TEXT_DIRECTION_INHERIT},
16684 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16685 * {@link #TEXT_DIRECTION_ANY_RTL},
16686 * {@link #TEXT_DIRECTION_LTR},
16687 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016688 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016689 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016690 * @attr ref android.R.styleable#View_textDirection
16691 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016692 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016693 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016694 @ViewDebug.ExportedProperty(category = "text", mapping = {
16695 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16696 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16697 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16698 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16699 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16700 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16701 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016702 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016703 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016704 }
16705
16706 /**
16707 * Set the text direction.
16708 *
16709 * @param textDirection the direction to set. Should be one of:
16710 *
16711 * {@link #TEXT_DIRECTION_INHERIT},
16712 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16713 * {@link #TEXT_DIRECTION_ANY_RTL},
16714 * {@link #TEXT_DIRECTION_LTR},
16715 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016716 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016717 *
16718 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16719 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16720 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016721 *
16722 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016723 */
16724 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016725 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016726 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016727 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016728 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016729 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016730 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016731 // Do resolution
16732 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016733 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016734 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016735 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016736 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016737 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016738 }
16739 }
16740
16741 /**
16742 * Return the resolved text direction.
16743 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016744 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016745 *
Doug Feltcb3791202011-07-07 11:57:48 -070016746 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16747 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016748 * {@link #TEXT_DIRECTION_LTR},
16749 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016750 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016751 *
16752 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016753 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016754 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016755 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016756 }
16757
16758 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016759 * Resolve the text direction.
16760 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016761 * @return true if resolution has been done, false otherwise.
16762 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016763 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016764 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016765 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016766 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016767 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016768
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016769 if (hasRtlSupport()) {
16770 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016771 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016772 switch(textDirection) {
16773 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016774 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016775 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016776 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016777 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016778 return false;
16779 }
16780
16781 View parent = ((View) mParent);
16782 // Parent has not yet resolved, so we still return the default
16783 if (!parent.isTextDirectionResolved()) {
16784 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16785 // Resolution will need to happen again later
16786 return false;
16787 }
16788
16789 // Set current resolved direction to the same value as the parent's one
16790 final int parentResolvedDirection = parent.getTextDirection();
16791 switch (parentResolvedDirection) {
16792 case TEXT_DIRECTION_FIRST_STRONG:
16793 case TEXT_DIRECTION_ANY_RTL:
16794 case TEXT_DIRECTION_LTR:
16795 case TEXT_DIRECTION_RTL:
16796 case TEXT_DIRECTION_LOCALE:
16797 mPrivateFlags2 |=
16798 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16799 break;
16800 default:
16801 // Default resolved direction is "first strong" heuristic
16802 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016803 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016804 break;
16805 case TEXT_DIRECTION_FIRST_STRONG:
16806 case TEXT_DIRECTION_ANY_RTL:
16807 case TEXT_DIRECTION_LTR:
16808 case TEXT_DIRECTION_RTL:
16809 case TEXT_DIRECTION_LOCALE:
16810 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016811 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016812 break;
16813 default:
16814 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016815 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016816 }
16817 } else {
16818 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016819 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016820 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016821
16822 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016823 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016824 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016825 }
16826
16827 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016828 * Check if text direction resolution can be done.
16829 *
16830 * @return true if text direction resolution can be done otherwise return false.
16831 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016832 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016833 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016834 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016835 return (mParent != null) && (mParent instanceof View) &&
16836 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016837 default:
16838 return true;
16839 }
16840 }
16841
16842 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016843 * Reset resolved text direction. Text direction will be resolved during a call to
16844 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016845 *
16846 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016847 */
16848 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016849 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016850 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016851 // Set to default value
16852 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016853 }
16854
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016855 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016856 * @return true if text direction is inherited.
16857 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016858 * @hide
16859 */
16860 public boolean isTextDirectionInherited() {
16861 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16862 }
16863
16864 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016865 * @return true if text direction is resolved.
16866 */
16867 private boolean isTextDirectionResolved() {
16868 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16869 }
16870
16871 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016872 * Return the value specifying the text alignment or policy that was set with
16873 * {@link #setTextAlignment(int)}.
16874 *
16875 * @return the defined text alignment. It can be one of:
16876 *
16877 * {@link #TEXT_ALIGNMENT_INHERIT},
16878 * {@link #TEXT_ALIGNMENT_GRAVITY},
16879 * {@link #TEXT_ALIGNMENT_CENTER},
16880 * {@link #TEXT_ALIGNMENT_TEXT_START},
16881 * {@link #TEXT_ALIGNMENT_TEXT_END},
16882 * {@link #TEXT_ALIGNMENT_VIEW_START},
16883 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016884 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016885 * @attr ref android.R.styleable#View_textAlignment
16886 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016887 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016888 */
16889 @ViewDebug.ExportedProperty(category = "text", mapping = {
16890 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16891 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16892 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16893 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16894 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16895 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16896 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16897 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016898 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016899 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016900 }
16901
16902 /**
16903 * Set the text alignment.
16904 *
16905 * @param textAlignment The text alignment to set. Should be one of
16906 *
16907 * {@link #TEXT_ALIGNMENT_INHERIT},
16908 * {@link #TEXT_ALIGNMENT_GRAVITY},
16909 * {@link #TEXT_ALIGNMENT_CENTER},
16910 * {@link #TEXT_ALIGNMENT_TEXT_START},
16911 * {@link #TEXT_ALIGNMENT_TEXT_END},
16912 * {@link #TEXT_ALIGNMENT_VIEW_START},
16913 * {@link #TEXT_ALIGNMENT_VIEW_END}
16914 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016915 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16916 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16917 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16918 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016919 * @attr ref android.R.styleable#View_textAlignment
16920 */
16921 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016922 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016923 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016924 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016925 resetResolvedTextAlignment();
16926 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016927 mPrivateFlags2 |=
16928 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16929 // Do resolution
16930 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016931 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016932 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016933 // Refresh
16934 requestLayout();
16935 invalidate(true);
16936 }
16937 }
16938
16939 /**
16940 * Return the resolved text alignment.
16941 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016942 * @return the resolved text alignment. Returns one of:
16943 *
16944 * {@link #TEXT_ALIGNMENT_GRAVITY},
16945 * {@link #TEXT_ALIGNMENT_CENTER},
16946 * {@link #TEXT_ALIGNMENT_TEXT_START},
16947 * {@link #TEXT_ALIGNMENT_TEXT_END},
16948 * {@link #TEXT_ALIGNMENT_VIEW_START},
16949 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016950 *
16951 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016952 */
16953 @ViewDebug.ExportedProperty(category = "text", mapping = {
16954 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16955 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16956 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16957 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16958 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16959 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16960 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16961 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016962 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016963 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16964 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016965 }
16966
16967 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016968 * Resolve the text alignment.
16969 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016970 * @return true if resolution has been done, false otherwise.
16971 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016972 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016973 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016974 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016975 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016976 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016977
16978 if (hasRtlSupport()) {
16979 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016980 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016981 switch (textAlignment) {
16982 case TEXT_ALIGNMENT_INHERIT:
16983 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016984 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016985 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016986 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016987 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016988 return false;
16989 }
16990 View parent = (View) mParent;
16991
16992 // Parent has not yet resolved, so we still return the default
16993 if (!parent.isTextAlignmentResolved()) {
16994 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16995 // Resolution will need to happen again later
16996 return false;
16997 }
16998
16999 final int parentResolvedTextAlignment = parent.getTextAlignment();
17000 switch (parentResolvedTextAlignment) {
17001 case TEXT_ALIGNMENT_GRAVITY:
17002 case TEXT_ALIGNMENT_TEXT_START:
17003 case TEXT_ALIGNMENT_TEXT_END:
17004 case TEXT_ALIGNMENT_CENTER:
17005 case TEXT_ALIGNMENT_VIEW_START:
17006 case TEXT_ALIGNMENT_VIEW_END:
17007 // Resolved text alignment is the same as the parent resolved
17008 // text alignment
17009 mPrivateFlags2 |=
17010 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17011 break;
17012 default:
17013 // Use default resolved text alignment
17014 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017015 }
17016 break;
17017 case TEXT_ALIGNMENT_GRAVITY:
17018 case TEXT_ALIGNMENT_TEXT_START:
17019 case TEXT_ALIGNMENT_TEXT_END:
17020 case TEXT_ALIGNMENT_CENTER:
17021 case TEXT_ALIGNMENT_VIEW_START:
17022 case TEXT_ALIGNMENT_VIEW_END:
17023 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017024 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017025 break;
17026 default:
17027 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017028 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017029 }
17030 } else {
17031 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017032 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017033 }
17034
17035 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017036 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017037 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017038 }
17039
17040 /**
17041 * Check if text alignment resolution can be done.
17042 *
17043 * @return true if text alignment resolution can be done otherwise return false.
17044 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017045 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017046 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017047 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017048 return (mParent != null) && (mParent instanceof View) &&
17049 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017050 default:
17051 return true;
17052 }
17053 }
17054
17055 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017056 * Reset resolved text alignment. Text alignment will be resolved during a call to
17057 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017058 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017059 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017060 */
17061 public void resetResolvedTextAlignment() {
17062 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017063 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017064 // Set to default
17065 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017066 }
17067
Adam Powella9108a22012-07-18 11:18:09 -070017068 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017069 * @return true if text alignment is inherited.
17070 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017071 * @hide
17072 */
17073 public boolean isTextAlignmentInherited() {
17074 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17075 }
17076
17077 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017078 * @return true if text alignment is resolved.
17079 */
17080 private boolean isTextAlignmentResolved() {
17081 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17082 }
17083
17084 /**
Adam Powella9108a22012-07-18 11:18:09 -070017085 * Generate a value suitable for use in {@link #setId(int)}.
17086 * This value will not collide with ID values generated at build time by aapt for R.id.
17087 *
17088 * @return a generated ID value
17089 */
17090 public static int generateViewId() {
17091 for (;;) {
17092 final int result = sNextGeneratedId.get();
17093 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17094 int newValue = result + 1;
17095 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17096 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17097 return result;
17098 }
17099 }
17100 }
17101
Chet Haaseb39f0512011-05-24 14:36:40 -070017102 //
17103 // Properties
17104 //
17105 /**
17106 * A Property wrapper around the <code>alpha</code> functionality handled by the
17107 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17108 */
Chet Haased47f1532011-12-16 11:18:52 -080017109 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017110 @Override
17111 public void setValue(View object, float value) {
17112 object.setAlpha(value);
17113 }
17114
17115 @Override
17116 public Float get(View object) {
17117 return object.getAlpha();
17118 }
17119 };
17120
17121 /**
17122 * A Property wrapper around the <code>translationX</code> functionality handled by the
17123 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17124 */
Chet Haased47f1532011-12-16 11:18:52 -080017125 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017126 @Override
17127 public void setValue(View object, float value) {
17128 object.setTranslationX(value);
17129 }
17130
17131 @Override
17132 public Float get(View object) {
17133 return object.getTranslationX();
17134 }
17135 };
17136
17137 /**
17138 * A Property wrapper around the <code>translationY</code> functionality handled by the
17139 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17140 */
Chet Haased47f1532011-12-16 11:18:52 -080017141 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017142 @Override
17143 public void setValue(View object, float value) {
17144 object.setTranslationY(value);
17145 }
17146
17147 @Override
17148 public Float get(View object) {
17149 return object.getTranslationY();
17150 }
17151 };
17152
17153 /**
17154 * A Property wrapper around the <code>x</code> functionality handled by the
17155 * {@link View#setX(float)} and {@link View#getX()} methods.
17156 */
Chet Haased47f1532011-12-16 11:18:52 -080017157 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017158 @Override
17159 public void setValue(View object, float value) {
17160 object.setX(value);
17161 }
17162
17163 @Override
17164 public Float get(View object) {
17165 return object.getX();
17166 }
17167 };
17168
17169 /**
17170 * A Property wrapper around the <code>y</code> functionality handled by the
17171 * {@link View#setY(float)} and {@link View#getY()} methods.
17172 */
Chet Haased47f1532011-12-16 11:18:52 -080017173 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017174 @Override
17175 public void setValue(View object, float value) {
17176 object.setY(value);
17177 }
17178
17179 @Override
17180 public Float get(View object) {
17181 return object.getY();
17182 }
17183 };
17184
17185 /**
17186 * A Property wrapper around the <code>rotation</code> functionality handled by the
17187 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17188 */
Chet Haased47f1532011-12-16 11:18:52 -080017189 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017190 @Override
17191 public void setValue(View object, float value) {
17192 object.setRotation(value);
17193 }
17194
17195 @Override
17196 public Float get(View object) {
17197 return object.getRotation();
17198 }
17199 };
17200
17201 /**
17202 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17203 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17204 */
Chet Haased47f1532011-12-16 11:18:52 -080017205 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017206 @Override
17207 public void setValue(View object, float value) {
17208 object.setRotationX(value);
17209 }
17210
17211 @Override
17212 public Float get(View object) {
17213 return object.getRotationX();
17214 }
17215 };
17216
17217 /**
17218 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17219 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17220 */
Chet Haased47f1532011-12-16 11:18:52 -080017221 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017222 @Override
17223 public void setValue(View object, float value) {
17224 object.setRotationY(value);
17225 }
17226
17227 @Override
17228 public Float get(View object) {
17229 return object.getRotationY();
17230 }
17231 };
17232
17233 /**
17234 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17235 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17236 */
Chet Haased47f1532011-12-16 11:18:52 -080017237 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017238 @Override
17239 public void setValue(View object, float value) {
17240 object.setScaleX(value);
17241 }
17242
17243 @Override
17244 public Float get(View object) {
17245 return object.getScaleX();
17246 }
17247 };
17248
17249 /**
17250 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17251 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17252 */
Chet Haased47f1532011-12-16 11:18:52 -080017253 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017254 @Override
17255 public void setValue(View object, float value) {
17256 object.setScaleY(value);
17257 }
17258
17259 @Override
17260 public Float get(View object) {
17261 return object.getScaleY();
17262 }
17263 };
17264
Jeff Brown33bbfd22011-02-24 20:55:35 -080017265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017266 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17267 * Each MeasureSpec represents a requirement for either the width or the height.
17268 * A MeasureSpec is comprised of a size and a mode. There are three possible
17269 * modes:
17270 * <dl>
17271 * <dt>UNSPECIFIED</dt>
17272 * <dd>
17273 * The parent has not imposed any constraint on the child. It can be whatever size
17274 * it wants.
17275 * </dd>
17276 *
17277 * <dt>EXACTLY</dt>
17278 * <dd>
17279 * The parent has determined an exact size for the child. The child is going to be
17280 * given those bounds regardless of how big it wants to be.
17281 * </dd>
17282 *
17283 * <dt>AT_MOST</dt>
17284 * <dd>
17285 * The child can be as large as it wants up to the specified size.
17286 * </dd>
17287 * </dl>
17288 *
17289 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17290 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17291 */
17292 public static class MeasureSpec {
17293 private static final int MODE_SHIFT = 30;
17294 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17295
17296 /**
17297 * Measure specification mode: The parent has not imposed any constraint
17298 * on the child. It can be whatever size it wants.
17299 */
17300 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17301
17302 /**
17303 * Measure specification mode: The parent has determined an exact size
17304 * for the child. The child is going to be given those bounds regardless
17305 * of how big it wants to be.
17306 */
17307 public static final int EXACTLY = 1 << MODE_SHIFT;
17308
17309 /**
17310 * Measure specification mode: The child can be as large as it wants up
17311 * to the specified size.
17312 */
17313 public static final int AT_MOST = 2 << MODE_SHIFT;
17314
17315 /**
17316 * Creates a measure specification based on the supplied size and mode.
17317 *
17318 * The mode must always be one of the following:
17319 * <ul>
17320 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17321 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17322 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17323 * </ul>
17324 *
17325 * @param size the size of the measure specification
17326 * @param mode the mode of the measure specification
17327 * @return the measure specification based on size and mode
17328 */
17329 public static int makeMeasureSpec(int size, int mode) {
Adam Powelld5edc772012-09-26 15:21:39 -070017330 return (size & ~MODE_MASK) | (mode & MODE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017331 }
17332
17333 /**
17334 * Extracts the mode from the supplied measure specification.
17335 *
17336 * @param measureSpec the measure specification to extract the mode from
17337 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17338 * {@link android.view.View.MeasureSpec#AT_MOST} or
17339 * {@link android.view.View.MeasureSpec#EXACTLY}
17340 */
17341 public static int getMode(int measureSpec) {
17342 return (measureSpec & MODE_MASK);
17343 }
17344
17345 /**
17346 * Extracts the size from the supplied measure specification.
17347 *
17348 * @param measureSpec the measure specification to extract the size from
17349 * @return the size in pixels defined in the supplied measure specification
17350 */
17351 public static int getSize(int measureSpec) {
17352 return (measureSpec & ~MODE_MASK);
17353 }
17354
Philip Milne7b757812012-09-19 18:13:44 -070017355 static int adjust(int measureSpec, int delta) {
17356 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17357 }
17358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017359 /**
17360 * Returns a String representation of the specified measure
17361 * specification.
17362 *
17363 * @param measureSpec the measure specification to convert to a String
17364 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17365 */
17366 public static String toString(int measureSpec) {
17367 int mode = getMode(measureSpec);
17368 int size = getSize(measureSpec);
17369
17370 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17371
17372 if (mode == UNSPECIFIED)
17373 sb.append("UNSPECIFIED ");
17374 else if (mode == EXACTLY)
17375 sb.append("EXACTLY ");
17376 else if (mode == AT_MOST)
17377 sb.append("AT_MOST ");
17378 else
17379 sb.append(mode).append(" ");
17380
17381 sb.append(size);
17382 return sb.toString();
17383 }
17384 }
17385
17386 class CheckForLongPress implements Runnable {
17387
17388 private int mOriginalWindowAttachCount;
17389
17390 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017391 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017392 && mOriginalWindowAttachCount == mWindowAttachCount) {
17393 if (performLongClick()) {
17394 mHasPerformedLongPress = true;
17395 }
17396 }
17397 }
17398
17399 public void rememberWindowAttachCount() {
17400 mOriginalWindowAttachCount = mWindowAttachCount;
17401 }
17402 }
Joe Malin32736f02011-01-19 16:14:20 -080017403
Adam Powelle14579b2009-12-16 18:39:52 -080017404 private final class CheckForTap implements Runnable {
17405 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017406 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017407 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017408 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017409 }
17410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017411
Adam Powella35d7682010-03-12 14:48:13 -080017412 private final class PerformClick implements Runnable {
17413 public void run() {
17414 performClick();
17415 }
17416 }
17417
Dianne Hackborn63042d62011-01-26 18:56:29 -080017418 /** @hide */
17419 public void hackTurnOffWindowResizeAnim(boolean off) {
17420 mAttachInfo.mTurnOffWindowResizeAnim = off;
17421 }
Joe Malin32736f02011-01-19 16:14:20 -080017422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017424 * This method returns a ViewPropertyAnimator object, which can be used to animate
17425 * specific properties on this View.
17426 *
17427 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17428 */
17429 public ViewPropertyAnimator animate() {
17430 if (mAnimator == null) {
17431 mAnimator = new ViewPropertyAnimator(this);
17432 }
17433 return mAnimator;
17434 }
17435
17436 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017437 * Interface definition for a callback to be invoked when a hardware key event is
17438 * dispatched to this view. The callback will be invoked before the key event is
17439 * given to the view. This is only useful for hardware keyboards; a software input
17440 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017441 */
17442 public interface OnKeyListener {
17443 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017444 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017445 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017446 * <p>Key presses in software keyboards will generally NOT trigger this method,
17447 * although some may elect to do so in some situations. Do not assume a
17448 * software input method has to be key-based; even if it is, it may use key presses
17449 * in a different way than you expect, so there is no way to reliably catch soft
17450 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017451 *
17452 * @param v The view the key has been dispatched to.
17453 * @param keyCode The code for the physical key that was pressed
17454 * @param event The KeyEvent object containing full information about
17455 * the event.
17456 * @return True if the listener has consumed the event, false otherwise.
17457 */
17458 boolean onKey(View v, int keyCode, KeyEvent event);
17459 }
17460
17461 /**
17462 * Interface definition for a callback to be invoked when a touch event is
17463 * dispatched to this view. The callback will be invoked before the touch
17464 * event is given to the view.
17465 */
17466 public interface OnTouchListener {
17467 /**
17468 * Called when a touch event is dispatched to a view. This allows listeners to
17469 * get a chance to respond before the target view.
17470 *
17471 * @param v The view the touch event has been dispatched to.
17472 * @param event The MotionEvent object containing full information about
17473 * the event.
17474 * @return True if the listener has consumed the event, false otherwise.
17475 */
17476 boolean onTouch(View v, MotionEvent event);
17477 }
17478
17479 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017480 * Interface definition for a callback to be invoked when a hover event is
17481 * dispatched to this view. The callback will be invoked before the hover
17482 * event is given to the view.
17483 */
17484 public interface OnHoverListener {
17485 /**
17486 * Called when a hover event is dispatched to a view. This allows listeners to
17487 * get a chance to respond before the target view.
17488 *
17489 * @param v The view the hover event has been dispatched to.
17490 * @param event The MotionEvent object containing full information about
17491 * the event.
17492 * @return True if the listener has consumed the event, false otherwise.
17493 */
17494 boolean onHover(View v, MotionEvent event);
17495 }
17496
17497 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017498 * Interface definition for a callback to be invoked when a generic motion event is
17499 * dispatched to this view. The callback will be invoked before the generic motion
17500 * event is given to the view.
17501 */
17502 public interface OnGenericMotionListener {
17503 /**
17504 * Called when a generic motion event is dispatched to a view. This allows listeners to
17505 * get a chance to respond before the target view.
17506 *
17507 * @param v The view the generic motion event has been dispatched to.
17508 * @param event The MotionEvent object containing full information about
17509 * the event.
17510 * @return True if the listener has consumed the event, false otherwise.
17511 */
17512 boolean onGenericMotion(View v, MotionEvent event);
17513 }
17514
17515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017516 * Interface definition for a callback to be invoked when a view has been clicked and held.
17517 */
17518 public interface OnLongClickListener {
17519 /**
17520 * Called when a view has been clicked and held.
17521 *
17522 * @param v The view that was clicked and held.
17523 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017524 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017525 */
17526 boolean onLongClick(View v);
17527 }
17528
17529 /**
Chris Tate32affef2010-10-18 15:29:21 -070017530 * Interface definition for a callback to be invoked when a drag is being dispatched
17531 * to this view. The callback will be invoked before the hosting view's own
17532 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17533 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017534 *
17535 * <div class="special reference">
17536 * <h3>Developer Guides</h3>
17537 * <p>For a guide to implementing drag and drop features, read the
17538 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17539 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017540 */
17541 public interface OnDragListener {
17542 /**
17543 * Called when a drag event is dispatched to a view. This allows listeners
17544 * to get a chance to override base View behavior.
17545 *
Joe Malin32736f02011-01-19 16:14:20 -080017546 * @param v The View that received the drag event.
17547 * @param event The {@link android.view.DragEvent} object for the drag event.
17548 * @return {@code true} if the drag event was handled successfully, or {@code false}
17549 * if the drag event was not handled. Note that {@code false} will trigger the View
17550 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017551 */
17552 boolean onDrag(View v, DragEvent event);
17553 }
17554
17555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017556 * Interface definition for a callback to be invoked when the focus state of
17557 * a view changed.
17558 */
17559 public interface OnFocusChangeListener {
17560 /**
17561 * Called when the focus state of a view has changed.
17562 *
17563 * @param v The view whose state has changed.
17564 * @param hasFocus The new focus state of v.
17565 */
17566 void onFocusChange(View v, boolean hasFocus);
17567 }
17568
17569 /**
17570 * Interface definition for a callback to be invoked when a view is clicked.
17571 */
17572 public interface OnClickListener {
17573 /**
17574 * Called when a view has been clicked.
17575 *
17576 * @param v The view that was clicked.
17577 */
17578 void onClick(View v);
17579 }
17580
17581 /**
17582 * Interface definition for a callback to be invoked when the context menu
17583 * for this view is being built.
17584 */
17585 public interface OnCreateContextMenuListener {
17586 /**
17587 * Called when the context menu for this view is being built. It is not
17588 * safe to hold onto the menu after this method returns.
17589 *
17590 * @param menu The context menu that is being built
17591 * @param v The view for which the context menu is being built
17592 * @param menuInfo Extra information about the item for which the
17593 * context menu should be shown. This information will vary
17594 * depending on the class of v.
17595 */
17596 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17597 }
17598
Joe Onorato664644d2011-01-23 17:53:23 -080017599 /**
17600 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017601 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017602 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017603 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017604 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017605 */
17606 public interface OnSystemUiVisibilityChangeListener {
17607 /**
17608 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017609 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017610 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017611 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17612 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17613 * This tells you the <strong>global</strong> state of these UI visibility
17614 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017615 */
17616 public void onSystemUiVisibilityChange(int visibility);
17617 }
17618
Adam Powell4afd62b2011-02-18 15:02:18 -080017619 /**
17620 * Interface definition for a callback to be invoked when this view is attached
17621 * or detached from its window.
17622 */
17623 public interface OnAttachStateChangeListener {
17624 /**
17625 * Called when the view is attached to a window.
17626 * @param v The view that was attached
17627 */
17628 public void onViewAttachedToWindow(View v);
17629 /**
17630 * Called when the view is detached from a window.
17631 * @param v The view that was detached
17632 */
17633 public void onViewDetachedFromWindow(View v);
17634 }
17635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017636 private final class UnsetPressedState implements Runnable {
17637 public void run() {
17638 setPressed(false);
17639 }
17640 }
17641
17642 /**
17643 * Base class for derived classes that want to save and restore their own
17644 * state in {@link android.view.View#onSaveInstanceState()}.
17645 */
17646 public static class BaseSavedState extends AbsSavedState {
17647 /**
17648 * Constructor used when reading from a parcel. Reads the state of the superclass.
17649 *
17650 * @param source
17651 */
17652 public BaseSavedState(Parcel source) {
17653 super(source);
17654 }
17655
17656 /**
17657 * Constructor called by derived classes when creating their SavedState objects
17658 *
17659 * @param superState The state of the superclass of this view
17660 */
17661 public BaseSavedState(Parcelable superState) {
17662 super(superState);
17663 }
17664
17665 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17666 new Parcelable.Creator<BaseSavedState>() {
17667 public BaseSavedState createFromParcel(Parcel in) {
17668 return new BaseSavedState(in);
17669 }
17670
17671 public BaseSavedState[] newArray(int size) {
17672 return new BaseSavedState[size];
17673 }
17674 };
17675 }
17676
17677 /**
17678 * A set of information given to a view when it is attached to its parent
17679 * window.
17680 */
17681 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017682 interface Callbacks {
17683 void playSoundEffect(int effectId);
17684 boolean performHapticFeedback(int effectId, boolean always);
17685 }
17686
17687 /**
17688 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17689 * to a Handler. This class contains the target (View) to invalidate and
17690 * the coordinates of the dirty rectangle.
17691 *
17692 * For performance purposes, this class also implements a pool of up to
17693 * POOL_LIMIT objects that get reused. This reduces memory allocations
17694 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017695 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017696 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017697 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017698
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017699 private static final SynchronizedPool<InvalidateInfo> sPool =
17700 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017701
17702 View target;
17703
17704 int left;
17705 int top;
17706 int right;
17707 int bottom;
17708
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017709 public static InvalidateInfo obtain() {
17710 InvalidateInfo instance = sPool.acquire();
17711 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070017712 }
17713
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017714 public void recycle() {
17715 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017716 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017717 }
17718 }
17719
17720 final IWindowSession mSession;
17721
17722 final IWindow mWindow;
17723
17724 final IBinder mWindowToken;
17725
Jeff Brown98365d72012-08-19 20:30:52 -070017726 final Display mDisplay;
17727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017728 final Callbacks mRootCallbacks;
17729
Romain Guy59a12ca2011-06-09 17:48:21 -070017730 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017732 /**
17733 * The top view of the hierarchy.
17734 */
17735 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017737 IBinder mPanelParentWindowToken;
17738 Surface mSurface;
17739
Romain Guyb051e892010-09-28 19:09:36 -070017740 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017741 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017742 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017743
Romain Guy7e4e5612012-03-05 14:37:29 -080017744 boolean mScreenOn;
17745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017746 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017747 * Scale factor used by the compatibility mode
17748 */
17749 float mApplicationScale;
17750
17751 /**
17752 * Indicates whether the application is in compatibility mode
17753 */
17754 boolean mScalingRequired;
17755
17756 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017757 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017758 */
17759 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017760
Dianne Hackborn63042d62011-01-26 18:56:29 -080017761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017762 * Left position of this view's window
17763 */
17764 int mWindowLeft;
17765
17766 /**
17767 * Top position of this view's window
17768 */
17769 int mWindowTop;
17770
17771 /**
Adam Powell26153a32010-11-08 15:22:27 -080017772 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017773 */
Adam Powell26153a32010-11-08 15:22:27 -080017774 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017775
17776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017777 * For windows that are full-screen but using insets to layout inside
17778 * of the screen decorations, these are the current insets for the
17779 * content of the window.
17780 */
17781 final Rect mContentInsets = new Rect();
17782
17783 /**
17784 * For windows that are full-screen but using insets to layout inside
17785 * of the screen decorations, these are the current insets for the
17786 * actual visible parts of the window.
17787 */
17788 final Rect mVisibleInsets = new Rect();
17789
17790 /**
17791 * The internal insets given by this window. This value is
17792 * supplied by the client (through
17793 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17794 * be given to the window manager when changed to be used in laying
17795 * out windows behind it.
17796 */
17797 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17798 = new ViewTreeObserver.InternalInsetsInfo();
17799
17800 /**
17801 * All views in the window's hierarchy that serve as scroll containers,
17802 * used to determine if the window can be resized or must be panned
17803 * to adjust for a soft input area.
17804 */
17805 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17806
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017807 final KeyEvent.DispatcherState mKeyDispatchState
17808 = new KeyEvent.DispatcherState();
17809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017810 /**
17811 * Indicates whether the view's window currently has the focus.
17812 */
17813 boolean mHasWindowFocus;
17814
17815 /**
17816 * The current visibility of the window.
17817 */
17818 int mWindowVisibility;
17819
17820 /**
17821 * Indicates the time at which drawing started to occur.
17822 */
17823 long mDrawingTime;
17824
17825 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017826 * Indicates whether or not ignoring the DIRTY_MASK flags.
17827 */
17828 boolean mIgnoreDirtyState;
17829
17830 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017831 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17832 * to avoid clearing that flag prematurely.
17833 */
17834 boolean mSetIgnoreDirtyState = false;
17835
17836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017837 * Indicates whether the view's window is currently in touch mode.
17838 */
17839 boolean mInTouchMode;
17840
17841 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017842 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017843 * the next time it performs a traversal
17844 */
17845 boolean mRecomputeGlobalAttributes;
17846
17847 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017848 * Always report new attributes at next traversal.
17849 */
17850 boolean mForceReportNewAttributes;
17851
17852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017853 * Set during a traveral if any views want to keep the screen on.
17854 */
17855 boolean mKeepScreenOn;
17856
17857 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017858 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17859 */
17860 int mSystemUiVisibility;
17861
17862 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017863 * Hack to force certain system UI visibility flags to be cleared.
17864 */
17865 int mDisabledSystemUiVisibility;
17866
17867 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017868 * Last global system UI visibility reported by the window manager.
17869 */
17870 int mGlobalSystemUiVisibility;
17871
17872 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017873 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17874 * attached.
17875 */
17876 boolean mHasSystemUiListeners;
17877
17878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017879 * Set if the visibility of any views has changed.
17880 */
17881 boolean mViewVisibilityChanged;
17882
17883 /**
17884 * Set to true if a view has been scrolled.
17885 */
17886 boolean mViewScrollChanged;
17887
17888 /**
17889 * Global to the view hierarchy used as a temporary for dealing with
17890 * x/y points in the transparent region computations.
17891 */
17892 final int[] mTransparentLocation = new int[2];
17893
17894 /**
17895 * Global to the view hierarchy used as a temporary for dealing with
17896 * x/y points in the ViewGroup.invalidateChild implementation.
17897 */
17898 final int[] mInvalidateChildLocation = new int[2];
17899
Chet Haasec3aa3612010-06-17 08:50:37 -070017900
17901 /**
17902 * Global to the view hierarchy used as a temporary for dealing with
17903 * x/y location when view is transformed.
17904 */
17905 final float[] mTmpTransformLocation = new float[2];
17906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017907 /**
17908 * The view tree observer used to dispatch global events like
17909 * layout, pre-draw, touch mode change, etc.
17910 */
17911 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17912
17913 /**
17914 * A Canvas used by the view hierarchy to perform bitmap caching.
17915 */
17916 Canvas mCanvas;
17917
17918 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017919 * The view root impl.
17920 */
17921 final ViewRootImpl mViewRootImpl;
17922
17923 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017924 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017925 * handler can be used to pump events in the UI events queue.
17926 */
17927 final Handler mHandler;
17928
17929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017930 * Temporary for use in computing invalidate rectangles while
17931 * calling up the hierarchy.
17932 */
17933 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017934
17935 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017936 * Temporary for use in computing hit areas with transformed views
17937 */
17938 final RectF mTmpTransformRect = new RectF();
17939
17940 /**
Chet Haase599913d2012-07-23 16:22:05 -070017941 * Temporary for use in transforming invalidation rect
17942 */
17943 final Matrix mTmpMatrix = new Matrix();
17944
17945 /**
17946 * Temporary for use in transforming invalidation rect
17947 */
17948 final Transformation mTmpTransformation = new Transformation();
17949
17950 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017951 * Temporary list for use in collecting focusable descendents of a view.
17952 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017953 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017955 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017956 * The id of the window for accessibility purposes.
17957 */
17958 int mAccessibilityWindowId = View.NO_ID;
17959
17960 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017961 * Whether to ingore not exposed for accessibility Views when
17962 * reporting the view tree to accessibility services.
17963 */
17964 boolean mIncludeNotImportantViews;
17965
17966 /**
17967 * The drawable for highlighting accessibility focus.
17968 */
17969 Drawable mAccessibilityFocusDrawable;
17970
17971 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017972 * Show where the margins, bounds and layout bounds are for each view.
17973 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017974 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017975
17976 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017977 * Point used to compute visible regions.
17978 */
17979 final Point mPoint = new Point();
17980
17981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017982 * Creates a new set of attachment information with the specified
17983 * events handler and thread.
17984 *
17985 * @param handler the events handler the view must use
17986 */
Jeff Brown98365d72012-08-19 20:30:52 -070017987 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017988 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017989 mSession = session;
17990 mWindow = window;
17991 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017992 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017993 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017994 mHandler = handler;
17995 mRootCallbacks = effectPlayer;
17996 }
17997 }
17998
17999 /**
18000 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18001 * is supported. This avoids keeping too many unused fields in most
18002 * instances of View.</p>
18003 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018004 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018005
Mike Cleronf116bf82009-09-27 19:14:12 -070018006 /**
18007 * Scrollbars are not visible
18008 */
18009 public static final int OFF = 0;
18010
18011 /**
18012 * Scrollbars are visible
18013 */
18014 public static final int ON = 1;
18015
18016 /**
18017 * Scrollbars are fading away
18018 */
18019 public static final int FADING = 2;
18020
18021 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018023 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018024 public int scrollBarDefaultDelayBeforeFade;
18025 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018026
18027 public int scrollBarSize;
18028 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018029 public float[] interpolatorValues;
18030 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018031
18032 public final Paint paint;
18033 public final Matrix matrix;
18034 public Shader shader;
18035
Mike Cleronf116bf82009-09-27 19:14:12 -070018036 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18037
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018038 private static final float[] OPAQUE = { 255 };
18039 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018040
Mike Cleronf116bf82009-09-27 19:14:12 -070018041 /**
18042 * When fading should start. This time moves into the future every time
18043 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18044 */
18045 public long fadeStartTime;
18046
18047
18048 /**
18049 * The current state of the scrollbars: ON, OFF, or FADING
18050 */
18051 public int state = OFF;
18052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018053 private int mLastColor;
18054
Mike Cleronf116bf82009-09-27 19:14:12 -070018055 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018056 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18057 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018058 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18059 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018060
18061 paint = new Paint();
18062 matrix = new Matrix();
18063 // use use a height of 1, and then wack the matrix each time we
18064 // actually use it.
18065 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018066 paint.setShader(shader);
18067 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018068
Mike Cleronf116bf82009-09-27 19:14:12 -070018069 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018070 }
Romain Guy8506ab42009-06-11 17:35:47 -070018071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018072 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018073 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018074 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018075
Romain Guyd679b572012-08-29 21:49:00 -070018076 if (color != 0) {
18077 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18078 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18079 paint.setShader(shader);
18080 // Restore the default transfer mode (src_over)
18081 paint.setXfermode(null);
18082 } else {
18083 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18084 paint.setShader(shader);
18085 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018087 }
18088 }
Joe Malin32736f02011-01-19 16:14:20 -080018089
Mike Cleronf116bf82009-09-27 19:14:12 -070018090 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018091 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018092 if (now >= fadeStartTime) {
18093
18094 // the animation fades the scrollbars out by changing
18095 // the opacity (alpha) from fully opaque to fully
18096 // transparent
18097 int nextFrame = (int) now;
18098 int framesCount = 0;
18099
18100 Interpolator interpolator = scrollBarInterpolator;
18101
18102 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018103 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018104
18105 // End transparent
18106 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018107 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018108
18109 state = FADING;
18110
18111 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018112 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018113 }
18114 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018115 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018116
Svetoslav Ganova0156172011-06-26 17:55:44 -070018117 /**
18118 * Resuable callback for sending
18119 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18120 */
18121 private class SendViewScrolledAccessibilityEvent implements Runnable {
18122 public volatile boolean mIsPending;
18123
18124 public void run() {
18125 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18126 mIsPending = false;
18127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018128 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018129
18130 /**
18131 * <p>
18132 * This class represents a delegate that can be registered in a {@link View}
18133 * to enhance accessibility support via composition rather via inheritance.
18134 * It is specifically targeted to widget developers that extend basic View
18135 * classes i.e. classes in package android.view, that would like their
18136 * applications to be backwards compatible.
18137 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018138 * <div class="special reference">
18139 * <h3>Developer Guides</h3>
18140 * <p>For more information about making applications accessible, read the
18141 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18142 * developer guide.</p>
18143 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018144 * <p>
18145 * A scenario in which a developer would like to use an accessibility delegate
18146 * is overriding a method introduced in a later API version then the minimal API
18147 * version supported by the application. For example, the method
18148 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18149 * in API version 4 when the accessibility APIs were first introduced. If a
18150 * developer would like his application to run on API version 4 devices (assuming
18151 * all other APIs used by the application are version 4 or lower) and take advantage
18152 * of this method, instead of overriding the method which would break the application's
18153 * backwards compatibility, he can override the corresponding method in this
18154 * delegate and register the delegate in the target View if the API version of
18155 * the system is high enough i.e. the API version is same or higher to the API
18156 * version that introduced
18157 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18158 * </p>
18159 * <p>
18160 * Here is an example implementation:
18161 * </p>
18162 * <code><pre><p>
18163 * if (Build.VERSION.SDK_INT >= 14) {
18164 * // If the API version is equal of higher than the version in
18165 * // which onInitializeAccessibilityNodeInfo was introduced we
18166 * // register a delegate with a customized implementation.
18167 * View view = findViewById(R.id.view_id);
18168 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18169 * public void onInitializeAccessibilityNodeInfo(View host,
18170 * AccessibilityNodeInfo info) {
18171 * // Let the default implementation populate the info.
18172 * super.onInitializeAccessibilityNodeInfo(host, info);
18173 * // Set some other information.
18174 * info.setEnabled(host.isEnabled());
18175 * }
18176 * });
18177 * }
18178 * </code></pre></p>
18179 * <p>
18180 * This delegate contains methods that correspond to the accessibility methods
18181 * in View. If a delegate has been specified the implementation in View hands
18182 * off handling to the corresponding method in this delegate. The default
18183 * implementation the delegate methods behaves exactly as the corresponding
18184 * method in View for the case of no accessibility delegate been set. Hence,
18185 * to customize the behavior of a View method, clients can override only the
18186 * corresponding delegate method without altering the behavior of the rest
18187 * accessibility related methods of the host view.
18188 * </p>
18189 */
18190 public static class AccessibilityDelegate {
18191
18192 /**
18193 * Sends an accessibility event of the given type. If accessibility is not
18194 * enabled this method has no effect.
18195 * <p>
18196 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18197 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18198 * been set.
18199 * </p>
18200 *
18201 * @param host The View hosting the delegate.
18202 * @param eventType The type of the event to send.
18203 *
18204 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18205 */
18206 public void sendAccessibilityEvent(View host, int eventType) {
18207 host.sendAccessibilityEventInternal(eventType);
18208 }
18209
18210 /**
alanv8eeefef2012-05-07 16:57:53 -070018211 * Performs the specified accessibility action on the view. For
18212 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18213 * <p>
18214 * The default implementation behaves as
18215 * {@link View#performAccessibilityAction(int, Bundle)
18216 * View#performAccessibilityAction(int, Bundle)} for the case of
18217 * no accessibility delegate been set.
18218 * </p>
18219 *
18220 * @param action The action to perform.
18221 * @return Whether the action was performed.
18222 *
18223 * @see View#performAccessibilityAction(int, Bundle)
18224 * View#performAccessibilityAction(int, Bundle)
18225 */
18226 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18227 return host.performAccessibilityActionInternal(action, args);
18228 }
18229
18230 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018231 * Sends an accessibility event. This method behaves exactly as
18232 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18233 * empty {@link AccessibilityEvent} and does not perform a check whether
18234 * accessibility is enabled.
18235 * <p>
18236 * The default implementation behaves as
18237 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18238 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18239 * the case of no accessibility delegate been set.
18240 * </p>
18241 *
18242 * @param host The View hosting the delegate.
18243 * @param event The event to send.
18244 *
18245 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18246 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18247 */
18248 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18249 host.sendAccessibilityEventUncheckedInternal(event);
18250 }
18251
18252 /**
18253 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18254 * to its children for adding their text content to the event.
18255 * <p>
18256 * The default implementation behaves as
18257 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18258 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18259 * the case of no accessibility delegate been set.
18260 * </p>
18261 *
18262 * @param host The View hosting the delegate.
18263 * @param event The event.
18264 * @return True if the event population was completed.
18265 *
18266 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18267 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18268 */
18269 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18270 return host.dispatchPopulateAccessibilityEventInternal(event);
18271 }
18272
18273 /**
18274 * Gives a chance to the host View to populate the accessibility event with its
18275 * text content.
18276 * <p>
18277 * The default implementation behaves as
18278 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18279 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18280 * the case of no accessibility delegate been set.
18281 * </p>
18282 *
18283 * @param host The View hosting the delegate.
18284 * @param event The accessibility event which to populate.
18285 *
18286 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18287 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18288 */
18289 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18290 host.onPopulateAccessibilityEventInternal(event);
18291 }
18292
18293 /**
18294 * Initializes an {@link AccessibilityEvent} with information about the
18295 * the host View which is the event source.
18296 * <p>
18297 * The default implementation behaves as
18298 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18299 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18300 * the case of no accessibility delegate been set.
18301 * </p>
18302 *
18303 * @param host The View hosting the delegate.
18304 * @param event The event to initialize.
18305 *
18306 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18307 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18308 */
18309 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18310 host.onInitializeAccessibilityEventInternal(event);
18311 }
18312
18313 /**
18314 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18315 * <p>
18316 * The default implementation behaves as
18317 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18318 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18319 * the case of no accessibility delegate been set.
18320 * </p>
18321 *
18322 * @param host The View hosting the delegate.
18323 * @param info The instance to initialize.
18324 *
18325 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18326 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18327 */
18328 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18329 host.onInitializeAccessibilityNodeInfoInternal(info);
18330 }
18331
18332 /**
18333 * Called when a child of the host View has requested sending an
18334 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18335 * to augment the event.
18336 * <p>
18337 * The default implementation behaves as
18338 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18339 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18340 * the case of no accessibility delegate been set.
18341 * </p>
18342 *
18343 * @param host The View hosting the delegate.
18344 * @param child The child which requests sending the event.
18345 * @param event The event to be sent.
18346 * @return True if the event should be sent
18347 *
18348 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18349 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18350 */
18351 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18352 AccessibilityEvent event) {
18353 return host.onRequestSendAccessibilityEventInternal(child, event);
18354 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018355
18356 /**
18357 * Gets the provider for managing a virtual view hierarchy rooted at this View
18358 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18359 * that explore the window content.
18360 * <p>
18361 * The default implementation behaves as
18362 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18363 * the case of no accessibility delegate been set.
18364 * </p>
18365 *
18366 * @return The provider.
18367 *
18368 * @see AccessibilityNodeProvider
18369 */
18370 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18371 return null;
18372 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018373 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018374
18375 private class MatchIdPredicate implements Predicate<View> {
18376 public int mId;
18377
18378 @Override
18379 public boolean apply(View view) {
18380 return (view.mID == mId);
18381 }
18382 }
18383
18384 private class MatchLabelForPredicate implements Predicate<View> {
18385 private int mLabeledId;
18386
18387 @Override
18388 public boolean apply(View view) {
18389 return (view.mLabelForId == mLabeledId);
18390 }
18391 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018392
18393 /**
18394 * Dump all private flags in readable format, useful for documentation and
18395 * sanity checking.
18396 */
18397 private static void dumpFlags() {
18398 final HashMap<String, String> found = Maps.newHashMap();
18399 try {
18400 for (Field field : View.class.getDeclaredFields()) {
18401 final int modifiers = field.getModifiers();
18402 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18403 if (field.getType().equals(int.class)) {
18404 final int value = field.getInt(null);
18405 dumpFlag(found, field.getName(), value);
18406 } else if (field.getType().equals(int[].class)) {
18407 final int[] values = (int[]) field.get(null);
18408 for (int i = 0; i < values.length; i++) {
18409 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18410 }
18411 }
18412 }
18413 }
18414 } catch (IllegalAccessException e) {
18415 throw new RuntimeException(e);
18416 }
18417
18418 final ArrayList<String> keys = Lists.newArrayList();
18419 keys.addAll(found.keySet());
18420 Collections.sort(keys);
18421 for (String key : keys) {
18422 Log.d(VIEW_LOG_TAG, found.get(key));
18423 }
18424 }
18425
18426 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18427 // Sort flags by prefix, then by bits, always keeping unique keys
18428 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18429 final int prefix = name.indexOf('_');
18430 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18431 final String output = bits + " " + name;
18432 found.put(key, output);
18433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018434}