blob: 3c4ea44ddbc22b91b7936d637ffd3a529236d767 [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();
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007015 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007016 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7017 // When traversing by character we always put the cursor after the character
7018 // to ease edit and have to compensate before asking the for previous segment.
7019 current--;
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007020 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007021 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007022 final int[] range = iterator.preceding(current);
7023 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007024 return false;
7025 }
7026 final int start = range[0];
7027 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007028 // Always put the cursor after the character to ease edit.
7029 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7030 setAccessibilityCursorPosition(end);
7031 } else {
7032 setAccessibilityCursorPosition(start);
7033 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007034 sendViewTextTraversedAtGranularityEvent(
7035 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7036 granularity, start, end);
7037 return true;
7038 }
7039
7040 /**
7041 * Gets the text reported for accessibility purposes.
7042 *
7043 * @return The accessibility text.
7044 *
7045 * @hide
7046 */
7047 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007048 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007049 }
7050
7051 /**
7052 * @hide
7053 */
7054 public int getAccessibilityCursorPosition() {
7055 return mAccessibilityCursorPosition;
7056 }
7057
7058 /**
7059 * @hide
7060 */
7061 public void setAccessibilityCursorPosition(int position) {
7062 mAccessibilityCursorPosition = position;
7063 }
7064
7065 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7066 int fromIndex, int toIndex) {
7067 if (mParent == null) {
7068 return;
7069 }
7070 AccessibilityEvent event = AccessibilityEvent.obtain(
7071 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7072 onInitializeAccessibilityEvent(event);
7073 onPopulateAccessibilityEvent(event);
7074 event.setFromIndex(fromIndex);
7075 event.setToIndex(toIndex);
7076 event.setAction(action);
7077 event.setMovementGranularity(granularity);
7078 mParent.requestSendAccessibilityEvent(this, event);
7079 }
7080
7081 /**
7082 * @hide
7083 */
7084 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7085 switch (granularity) {
7086 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7087 CharSequence text = getIterableTextForAccessibility();
7088 if (text != null && text.length() > 0) {
7089 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007090 CharacterTextSegmentIterator.getInstance(
7091 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007092 iterator.initialize(text.toString());
7093 return iterator;
7094 }
7095 } break;
7096 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7097 CharSequence text = getIterableTextForAccessibility();
7098 if (text != null && text.length() > 0) {
7099 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007100 WordTextSegmentIterator.getInstance(
7101 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007102 iterator.initialize(text.toString());
7103 return iterator;
7104 }
7105 } break;
7106 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7107 CharSequence text = getIterableTextForAccessibility();
7108 if (text != null && text.length() > 0) {
7109 ParagraphTextSegmentIterator iterator =
7110 ParagraphTextSegmentIterator.getInstance();
7111 iterator.initialize(text.toString());
7112 return iterator;
7113 }
7114 } break;
7115 }
7116 return null;
7117 }
7118
Svetoslav Ganov42138042012-03-20 11:51:39 -07007119 /**
Romain Guya440b002010-02-24 15:57:54 -08007120 * @hide
7121 */
7122 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007123 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007124 clearDisplayList();
7125
Romain Guya440b002010-02-24 15:57:54 -08007126 onStartTemporaryDetach();
7127 }
7128
7129 /**
7130 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7132 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007133 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 */
7135 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007136 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007137 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007138 }
7139
7140 /**
7141 * @hide
7142 */
7143 public void dispatchFinishTemporaryDetach() {
7144 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 }
Romain Guy8506ab42009-06-11 17:35:47 -07007146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007147 /**
7148 * Called after {@link #onStartTemporaryDetach} when the container is done
7149 * changing the view.
7150 */
7151 public void onFinishTemporaryDetach() {
7152 }
Romain Guy8506ab42009-06-11 17:35:47 -07007153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007155 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7156 * for this view's window. Returns null if the view is not currently attached
7157 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007158 * just use the standard high-level event callbacks like
7159 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007160 */
7161 public KeyEvent.DispatcherState getKeyDispatcherState() {
7162 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7163 }
Joe Malin32736f02011-01-19 16:14:20 -08007164
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 * Dispatch a key event before it is processed by any input method
7167 * associated with the view hierarchy. This can be used to intercept
7168 * key events in special situations before the IME consumes them; a
7169 * typical example would be handling the BACK key to update the application's
7170 * UI instead of allowing the IME to see it and close itself.
7171 *
7172 * @param event The key event to be dispatched.
7173 * @return True if the event was handled, false otherwise.
7174 */
7175 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7176 return onKeyPreIme(event.getKeyCode(), event);
7177 }
7178
7179 /**
7180 * Dispatch a key event to the next view on the focus path. This path runs
7181 * from the top of the view tree down to the currently focused view. If this
7182 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7183 * the next node down the focus path. This method also fires any key
7184 * listeners.
7185 *
7186 * @param event The key event to be dispatched.
7187 * @return True if the event was handled, false otherwise.
7188 */
7189 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007190 if (mInputEventConsistencyVerifier != null) {
7191 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193
Jeff Brown21bc5c92011-02-28 18:27:14 -08007194 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007195 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007196 ListenerInfo li = mListenerInfo;
7197 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7198 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 return true;
7200 }
7201
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007202 if (event.dispatch(this, mAttachInfo != null
7203 ? mAttachInfo.mKeyDispatchState : null, this)) {
7204 return true;
7205 }
7206
7207 if (mInputEventConsistencyVerifier != null) {
7208 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7209 }
7210 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 }
7212
7213 /**
7214 * Dispatches a key shortcut event.
7215 *
7216 * @param event The key event to be dispatched.
7217 * @return True if the event was handled by the view, false otherwise.
7218 */
7219 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7220 return onKeyShortcut(event.getKeyCode(), event);
7221 }
7222
7223 /**
7224 * Pass the touch screen motion event down to the target view, or this
7225 * view if it is the target.
7226 *
7227 * @param event The motion event to be dispatched.
7228 * @return True if the event was handled by the view, false otherwise.
7229 */
7230 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007231 if (mInputEventConsistencyVerifier != null) {
7232 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7233 }
7234
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007235 if (onFilterTouchEventForSecurity(event)) {
7236 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007237 ListenerInfo li = mListenerInfo;
7238 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7239 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007240 return true;
7241 }
7242
7243 if (onTouchEvent(event)) {
7244 return true;
7245 }
Jeff Brown85a31762010-09-01 17:01:00 -07007246 }
7247
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007248 if (mInputEventConsistencyVerifier != null) {
7249 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007251 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 }
7253
7254 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007255 * Filter the touch event to apply security policies.
7256 *
7257 * @param event The motion event to be filtered.
7258 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007259 *
Jeff Brown85a31762010-09-01 17:01:00 -07007260 * @see #getFilterTouchesWhenObscured
7261 */
7262 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007263 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007264 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7265 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7266 // Window is obscured, drop this touch.
7267 return false;
7268 }
7269 return true;
7270 }
7271
7272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 * Pass a trackball motion event down to the focused view.
7274 *
7275 * @param event The motion event to be dispatched.
7276 * @return True if the event was handled by the view, false otherwise.
7277 */
7278 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007279 if (mInputEventConsistencyVerifier != null) {
7280 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7281 }
7282
Romain Guy02ccac62011-06-24 13:20:23 -07007283 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 }
7285
7286 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007287 * Dispatch a generic motion event.
7288 * <p>
7289 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7290 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007291 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007292 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007293 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007294 *
7295 * @param event The motion event to be dispatched.
7296 * @return True if the event was handled by the view, false otherwise.
7297 */
7298 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007299 if (mInputEventConsistencyVerifier != null) {
7300 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7301 }
7302
Jeff Browna032cc02011-03-07 16:56:21 -08007303 final int source = event.getSource();
7304 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7305 final int action = event.getAction();
7306 if (action == MotionEvent.ACTION_HOVER_ENTER
7307 || action == MotionEvent.ACTION_HOVER_MOVE
7308 || action == MotionEvent.ACTION_HOVER_EXIT) {
7309 if (dispatchHoverEvent(event)) {
7310 return true;
7311 }
7312 } else if (dispatchGenericPointerEvent(event)) {
7313 return true;
7314 }
7315 } else if (dispatchGenericFocusedEvent(event)) {
7316 return true;
7317 }
7318
Jeff Brown10b62902011-06-20 16:40:37 -07007319 if (dispatchGenericMotionEventInternal(event)) {
7320 return true;
7321 }
7322
7323 if (mInputEventConsistencyVerifier != null) {
7324 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7325 }
7326 return false;
7327 }
7328
7329 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007330 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007331 ListenerInfo li = mListenerInfo;
7332 if (li != null && li.mOnGenericMotionListener != null
7333 && (mViewFlags & ENABLED_MASK) == ENABLED
7334 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007335 return true;
7336 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007337
7338 if (onGenericMotionEvent(event)) {
7339 return true;
7340 }
7341
7342 if (mInputEventConsistencyVerifier != null) {
7343 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7344 }
7345 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007346 }
7347
7348 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007349 * Dispatch a hover event.
7350 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007351 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007352 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007353 * </p>
7354 *
7355 * @param event The motion event to be dispatched.
7356 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007357 */
7358 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007359 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007360 ListenerInfo li = mListenerInfo;
7361 if (li != null && li.mOnHoverListener != null
7362 && (mViewFlags & ENABLED_MASK) == ENABLED
7363 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007364 return true;
7365 }
7366
Jeff Browna032cc02011-03-07 16:56:21 -08007367 return onHoverEvent(event);
7368 }
7369
7370 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007371 * Returns true if the view has a child to which it has recently sent
7372 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7373 * it does not have a hovered child, then it must be the innermost hovered view.
7374 * @hide
7375 */
7376 protected boolean hasHoveredChild() {
7377 return false;
7378 }
7379
7380 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007381 * Dispatch a generic motion event to the view under the first pointer.
7382 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007383 * Do not call this method directly.
7384 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007385 * </p>
7386 *
7387 * @param event The motion event to be dispatched.
7388 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007389 */
7390 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7391 return false;
7392 }
7393
7394 /**
7395 * Dispatch a generic motion event to the currently focused view.
7396 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007397 * Do not call this method directly.
7398 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007399 * </p>
7400 *
7401 * @param event The motion event to be dispatched.
7402 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007403 */
7404 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7405 return false;
7406 }
7407
7408 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007409 * Dispatch a pointer event.
7410 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007411 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7412 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7413 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007414 * and should not be expected to handle other pointing device features.
7415 * </p>
7416 *
7417 * @param event The motion event to be dispatched.
7418 * @return True if the event was handled by the view, false otherwise.
7419 * @hide
7420 */
7421 public final boolean dispatchPointerEvent(MotionEvent event) {
7422 if (event.isTouchEvent()) {
7423 return dispatchTouchEvent(event);
7424 } else {
7425 return dispatchGenericMotionEvent(event);
7426 }
7427 }
7428
7429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 * Called when the window containing this view gains or loses window focus.
7431 * ViewGroups should override to route to their children.
7432 *
7433 * @param hasFocus True if the window containing this view now has focus,
7434 * false otherwise.
7435 */
7436 public void dispatchWindowFocusChanged(boolean hasFocus) {
7437 onWindowFocusChanged(hasFocus);
7438 }
7439
7440 /**
7441 * Called when the window containing this view gains or loses focus. Note
7442 * that this is separate from view focus: to receive key events, both
7443 * your view and its window must have focus. If a window is displayed
7444 * on top of yours that takes input focus, then your own window will lose
7445 * focus but the view focus will remain unchanged.
7446 *
7447 * @param hasWindowFocus True if the window containing this view now has
7448 * focus, false otherwise.
7449 */
7450 public void onWindowFocusChanged(boolean hasWindowFocus) {
7451 InputMethodManager imm = InputMethodManager.peekInstance();
7452 if (!hasWindowFocus) {
7453 if (isPressed()) {
7454 setPressed(false);
7455 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007456 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 imm.focusOut(this);
7458 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007459 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007460 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007461 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007462 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 imm.focusIn(this);
7464 }
7465 refreshDrawableState();
7466 }
7467
7468 /**
7469 * Returns true if this view is in a window that currently has window focus.
7470 * Note that this is not the same as the view itself having focus.
7471 *
7472 * @return True if this view is in a window that currently has window focus.
7473 */
7474 public boolean hasWindowFocus() {
7475 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7476 }
7477
7478 /**
Adam Powell326d8082009-12-09 15:10:07 -08007479 * Dispatch a view visibility change down the view hierarchy.
7480 * ViewGroups should override to route to their children.
7481 * @param changedView The view whose visibility changed. Could be 'this' or
7482 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007483 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7484 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007485 */
7486 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7487 onVisibilityChanged(changedView, visibility);
7488 }
7489
7490 /**
7491 * Called when the visibility of the view or an ancestor of the view is changed.
7492 * @param changedView The view whose visibility changed. Could be 'this' or
7493 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007494 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7495 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007496 */
7497 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007498 if (visibility == VISIBLE) {
7499 if (mAttachInfo != null) {
7500 initialAwakenScrollBars();
7501 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007502 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007503 }
7504 }
Adam Powell326d8082009-12-09 15:10:07 -08007505 }
7506
7507 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007508 * Dispatch a hint about whether this view is displayed. For instance, when
7509 * a View moves out of the screen, it might receives a display hint indicating
7510 * the view is not displayed. Applications should not <em>rely</em> on this hint
7511 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007512 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007513 * @param hint A hint about whether or not this view is displayed:
7514 * {@link #VISIBLE} or {@link #INVISIBLE}.
7515 */
7516 public void dispatchDisplayHint(int hint) {
7517 onDisplayHint(hint);
7518 }
7519
7520 /**
7521 * Gives this view a hint about whether is displayed or not. For instance, when
7522 * a View moves out of the screen, it might receives a display hint indicating
7523 * the view is not displayed. Applications should not <em>rely</em> on this hint
7524 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007525 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007526 * @param hint A hint about whether or not this view is displayed:
7527 * {@link #VISIBLE} or {@link #INVISIBLE}.
7528 */
7529 protected void onDisplayHint(int hint) {
7530 }
7531
7532 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 * Dispatch a window visibility change down the view hierarchy.
7534 * ViewGroups should override to route to their children.
7535 *
7536 * @param visibility The new visibility of the window.
7537 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007538 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 */
7540 public void dispatchWindowVisibilityChanged(int visibility) {
7541 onWindowVisibilityChanged(visibility);
7542 }
7543
7544 /**
7545 * Called when the window containing has change its visibility
7546 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7547 * that this tells you whether or not your window is being made visible
7548 * to the window manager; this does <em>not</em> tell you whether or not
7549 * your window is obscured by other windows on the screen, even if it
7550 * is itself visible.
7551 *
7552 * @param visibility The new visibility of the window.
7553 */
7554 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007555 if (visibility == VISIBLE) {
7556 initialAwakenScrollBars();
7557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 }
7559
7560 /**
7561 * Returns the current visibility of the window this view is attached to
7562 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7563 *
7564 * @return Returns the current visibility of the view's window.
7565 */
7566 public int getWindowVisibility() {
7567 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7568 }
7569
7570 /**
7571 * Retrieve the overall visible display size in which the window this view is
7572 * attached to has been positioned in. This takes into account screen
7573 * decorations above the window, for both cases where the window itself
7574 * is being position inside of them or the window is being placed under
7575 * then and covered insets are used for the window to position its content
7576 * inside. In effect, this tells you the available area where content can
7577 * be placed and remain visible to users.
7578 *
7579 * <p>This function requires an IPC back to the window manager to retrieve
7580 * the requested information, so should not be used in performance critical
7581 * code like drawing.
7582 *
7583 * @param outRect Filled in with the visible display frame. If the view
7584 * is not attached to a window, this is simply the raw display size.
7585 */
7586 public void getWindowVisibleDisplayFrame(Rect outRect) {
7587 if (mAttachInfo != null) {
7588 try {
7589 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7590 } catch (RemoteException e) {
7591 return;
7592 }
7593 // XXX This is really broken, and probably all needs to be done
7594 // in the window manager, and we need to know more about whether
7595 // we want the area behind or in front of the IME.
7596 final Rect insets = mAttachInfo.mVisibleInsets;
7597 outRect.left += insets.left;
7598 outRect.top += insets.top;
7599 outRect.right -= insets.right;
7600 outRect.bottom -= insets.bottom;
7601 return;
7602 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007603 // The view is not attached to a display so we don't have a context.
7604 // Make a best guess about the display size.
7605 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007606 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 }
7608
7609 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007610 * Dispatch a notification about a resource configuration change down
7611 * the view hierarchy.
7612 * ViewGroups should override to route to their children.
7613 *
7614 * @param newConfig The new resource configuration.
7615 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007616 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007617 */
7618 public void dispatchConfigurationChanged(Configuration newConfig) {
7619 onConfigurationChanged(newConfig);
7620 }
7621
7622 /**
7623 * Called when the current configuration of the resources being used
7624 * by the application have changed. You can use this to decide when
7625 * to reload resources that can changed based on orientation and other
7626 * configuration characterstics. You only need to use this if you are
7627 * not relying on the normal {@link android.app.Activity} mechanism of
7628 * recreating the activity instance upon a configuration change.
7629 *
7630 * @param newConfig The new resource configuration.
7631 */
7632 protected void onConfigurationChanged(Configuration newConfig) {
7633 }
7634
7635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 * Private function to aggregate all per-view attributes in to the view
7637 * root.
7638 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007639 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7640 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 }
7642
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007643 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7644 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007645 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007646 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007647 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007648 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007649 ListenerInfo li = mListenerInfo;
7650 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007651 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 }
7654 }
7655
7656 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007657 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007658 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007659 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7660 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 ai.mRecomputeGlobalAttributes = true;
7662 }
7663 }
7664 }
7665
7666 /**
7667 * Returns whether the device is currently in touch mode. Touch mode is entered
7668 * once the user begins interacting with the device by touch, and affects various
7669 * things like whether focus is always visible to the user.
7670 *
7671 * @return Whether the device is in touch mode.
7672 */
7673 @ViewDebug.ExportedProperty
7674 public boolean isInTouchMode() {
7675 if (mAttachInfo != null) {
7676 return mAttachInfo.mInTouchMode;
7677 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007678 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 }
7680 }
7681
7682 /**
7683 * Returns the context the view is running in, through which it can
7684 * access the current theme, resources, etc.
7685 *
7686 * @return The view's Context.
7687 */
7688 @ViewDebug.CapturedViewProperty
7689 public final Context getContext() {
7690 return mContext;
7691 }
7692
7693 /**
7694 * Handle a key event before it is processed by any input method
7695 * associated with the view hierarchy. This can be used to intercept
7696 * key events in special situations before the IME consumes them; a
7697 * typical example would be handling the BACK key to update the application's
7698 * UI instead of allowing the IME to see it and close itself.
7699 *
7700 * @param keyCode The value in event.getKeyCode().
7701 * @param event Description of the key event.
7702 * @return If you handled the event, return true. If you want to allow the
7703 * event to be handled by the next receiver, return false.
7704 */
7705 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7706 return false;
7707 }
7708
7709 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007710 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7711 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7713 * is released, if the view is enabled and clickable.
7714 *
Jean Chalard405bc512012-05-29 19:12:34 +09007715 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7716 * although some may elect to do so in some situations. Do not rely on this to
7717 * catch software key presses.
7718 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 * @param keyCode A key code that represents the button pressed, from
7720 * {@link android.view.KeyEvent}.
7721 * @param event The KeyEvent object that defines the button action.
7722 */
7723 public boolean onKeyDown(int keyCode, KeyEvent event) {
7724 boolean result = false;
7725
7726 switch (keyCode) {
7727 case KeyEvent.KEYCODE_DPAD_CENTER:
7728 case KeyEvent.KEYCODE_ENTER: {
7729 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7730 return true;
7731 }
7732 // Long clickable items don't necessarily have to be clickable
7733 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7734 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7735 (event.getRepeatCount() == 0)) {
7736 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007737 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 return true;
7739 }
7740 break;
7741 }
7742 }
7743 return result;
7744 }
7745
7746 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007747 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7748 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7749 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007750 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7751 * although some may elect to do so in some situations. Do not rely on this to
7752 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007753 */
7754 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7755 return false;
7756 }
7757
7758 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007759 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7760 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7762 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007763 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7764 * although some may elect to do so in some situations. Do not rely on this to
7765 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 *
7767 * @param keyCode A key code that represents the button pressed, from
7768 * {@link android.view.KeyEvent}.
7769 * @param event The KeyEvent object that defines the button action.
7770 */
7771 public boolean onKeyUp(int keyCode, KeyEvent event) {
7772 boolean result = false;
7773
7774 switch (keyCode) {
7775 case KeyEvent.KEYCODE_DPAD_CENTER:
7776 case KeyEvent.KEYCODE_ENTER: {
7777 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7778 return true;
7779 }
7780 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7781 setPressed(false);
7782
7783 if (!mHasPerformedLongPress) {
7784 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007785 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786
7787 result = performClick();
7788 }
7789 }
7790 break;
7791 }
7792 }
7793 return result;
7794 }
7795
7796 /**
7797 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7798 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7799 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007800 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7801 * although some may elect to do so in some situations. Do not rely on this to
7802 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 *
7804 * @param keyCode A key code that represents the button pressed, from
7805 * {@link android.view.KeyEvent}.
7806 * @param repeatCount The number of times the action was made.
7807 * @param event The KeyEvent object that defines the button action.
7808 */
7809 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7810 return false;
7811 }
7812
7813 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007814 * Called on the focused view when a key shortcut event is not handled.
7815 * Override this method to implement local key shortcuts for the View.
7816 * Key shortcuts can also be implemented by setting the
7817 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 *
7819 * @param keyCode The value in event.getKeyCode().
7820 * @param event Description of the key event.
7821 * @return If you handled the event, return true. If you want to allow the
7822 * event to be handled by the next receiver, return false.
7823 */
7824 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7825 return false;
7826 }
7827
7828 /**
7829 * Check whether the called view is a text editor, in which case it
7830 * would make sense to automatically display a soft input window for
7831 * it. Subclasses should override this if they implement
7832 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007833 * a call on that method would return a non-null InputConnection, and
7834 * they are really a first-class editor that the user would normally
7835 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007836 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007837 * <p>The default implementation always returns false. This does
7838 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7839 * will not be called or the user can not otherwise perform edits on your
7840 * view; it is just a hint to the system that this is not the primary
7841 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007842 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 * @return Returns true if this view is a text editor, else false.
7844 */
7845 public boolean onCheckIsTextEditor() {
7846 return false;
7847 }
Romain Guy8506ab42009-06-11 17:35:47 -07007848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 /**
7850 * Create a new InputConnection for an InputMethod to interact
7851 * with the view. The default implementation returns null, since it doesn't
7852 * support input methods. You can override this to implement such support.
7853 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007855 * <p>When implementing this, you probably also want to implement
7856 * {@link #onCheckIsTextEditor()} to indicate you will return a
7857 * non-null InputConnection.
7858 *
7859 * @param outAttrs Fill in with attribute information about the connection.
7860 */
7861 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7862 return null;
7863 }
7864
7865 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007866 * Called by the {@link android.view.inputmethod.InputMethodManager}
7867 * when a view who is not the current
7868 * input connection target is trying to make a call on the manager. The
7869 * default implementation returns false; you can override this to return
7870 * true for certain views if you are performing InputConnection proxying
7871 * to them.
7872 * @param view The View that is making the InputMethodManager call.
7873 * @return Return true to allow the call, false to reject.
7874 */
7875 public boolean checkInputConnectionProxy(View view) {
7876 return false;
7877 }
Romain Guy8506ab42009-06-11 17:35:47 -07007878
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 * Show the context menu for this view. It is not safe to hold on to the
7881 * menu after returning from this method.
7882 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007883 * You should normally not overload this method. Overload
7884 * {@link #onCreateContextMenu(ContextMenu)} or define an
7885 * {@link OnCreateContextMenuListener} to add items to the context menu.
7886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 * @param menu The context menu to populate
7888 */
7889 public void createContextMenu(ContextMenu menu) {
7890 ContextMenuInfo menuInfo = getContextMenuInfo();
7891
7892 // Sets the current menu info so all items added to menu will have
7893 // my extra info set.
7894 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7895
7896 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007897 ListenerInfo li = mListenerInfo;
7898 if (li != null && li.mOnCreateContextMenuListener != null) {
7899 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007900 }
7901
7902 // Clear the extra information so subsequent items that aren't mine don't
7903 // have my extra info.
7904 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7905
7906 if (mParent != null) {
7907 mParent.createContextMenu(menu);
7908 }
7909 }
7910
7911 /**
7912 * Views should implement this if they have extra information to associate
7913 * with the context menu. The return result is supplied as a parameter to
7914 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7915 * callback.
7916 *
7917 * @return Extra information about the item for which the context menu
7918 * should be shown. This information will vary across different
7919 * subclasses of View.
7920 */
7921 protected ContextMenuInfo getContextMenuInfo() {
7922 return null;
7923 }
7924
7925 /**
7926 * Views should implement this if the view itself is going to add items to
7927 * the context menu.
7928 *
7929 * @param menu the context menu to populate
7930 */
7931 protected void onCreateContextMenu(ContextMenu menu) {
7932 }
7933
7934 /**
7935 * Implement this method to handle trackball motion events. The
7936 * <em>relative</em> movement of the trackball since the last event
7937 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7938 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7939 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7940 * they will often be fractional values, representing the more fine-grained
7941 * movement information available from a trackball).
7942 *
7943 * @param event The motion event.
7944 * @return True if the event was handled, false otherwise.
7945 */
7946 public boolean onTrackballEvent(MotionEvent event) {
7947 return false;
7948 }
7949
7950 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007951 * Implement this method to handle generic motion events.
7952 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007953 * Generic motion events describe joystick movements, mouse hovers, track pad
7954 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007955 * {@link MotionEvent#getSource() source} of the motion event specifies
7956 * the class of input that was received. Implementations of this method
7957 * must examine the bits in the source before processing the event.
7958 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007959 * </p><p>
7960 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7961 * are delivered to the view under the pointer. All other generic motion events are
7962 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007963 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007964 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007965 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007966 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7967 * // process the joystick movement...
7968 * return true;
7969 * }
7970 * }
7971 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7972 * switch (event.getAction()) {
7973 * case MotionEvent.ACTION_HOVER_MOVE:
7974 * // process the mouse hover movement...
7975 * return true;
7976 * case MotionEvent.ACTION_SCROLL:
7977 * // process the scroll wheel movement...
7978 * return true;
7979 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007980 * }
7981 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007982 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007983 *
7984 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007985 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007986 */
7987 public boolean onGenericMotionEvent(MotionEvent event) {
7988 return false;
7989 }
7990
7991 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007992 * Implement this method to handle hover events.
7993 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007994 * This method is called whenever a pointer is hovering into, over, or out of the
7995 * bounds of a view and the view is not currently being touched.
7996 * Hover events are represented as pointer events with action
7997 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7998 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7999 * </p>
8000 * <ul>
8001 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8002 * when the pointer enters the bounds of the view.</li>
8003 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8004 * when the pointer has already entered the bounds of the view and has moved.</li>
8005 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8006 * when the pointer has exited the bounds of the view or when the pointer is
8007 * about to go down due to a button click, tap, or similar user action that
8008 * causes the view to be touched.</li>
8009 * </ul>
8010 * <p>
8011 * The view should implement this method to return true to indicate that it is
8012 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008013 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008014 * The default implementation calls {@link #setHovered} to update the hovered state
8015 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008016 * is enabled and is clickable. The default implementation also sends hover
8017 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008018 * </p>
8019 *
8020 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008021 * @return True if the view handled the hover event.
8022 *
8023 * @see #isHovered
8024 * @see #setHovered
8025 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008026 */
8027 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008028 // The root view may receive hover (or touch) events that are outside the bounds of
8029 // the window. This code ensures that we only send accessibility events for
8030 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008031 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008032 if (!mSendingHoverAccessibilityEvents) {
8033 if ((action == MotionEvent.ACTION_HOVER_ENTER
8034 || action == MotionEvent.ACTION_HOVER_MOVE)
8035 && !hasHoveredChild()
8036 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008037 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008038 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008039 }
8040 } else {
8041 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008042 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008043 && !pointInView(event.getX(), event.getY()))) {
8044 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008045 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008046 // If the window does not have input focus we take away accessibility
8047 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008048 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008049 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008050 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008051 }
Jeff Browna1b24182011-07-28 13:38:24 -07008052 }
8053
Jeff Brown87b7f802011-06-21 18:35:45 -07008054 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008055 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008056 case MotionEvent.ACTION_HOVER_ENTER:
8057 setHovered(true);
8058 break;
8059 case MotionEvent.ACTION_HOVER_EXIT:
8060 setHovered(false);
8061 break;
8062 }
Jeff Browna1b24182011-07-28 13:38:24 -07008063
8064 // Dispatch the event to onGenericMotionEvent before returning true.
8065 // This is to provide compatibility with existing applications that
8066 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8067 // break because of the new default handling for hoverable views
8068 // in onHoverEvent.
8069 // Note that onGenericMotionEvent will be called by default when
8070 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8071 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008072 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008073 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008074
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008075 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008076 }
8077
8078 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008079 * Returns true if the view should handle {@link #onHoverEvent}
8080 * by calling {@link #setHovered} to change its hovered state.
8081 *
8082 * @return True if the view is hoverable.
8083 */
8084 private boolean isHoverable() {
8085 final int viewFlags = mViewFlags;
8086 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8087 return false;
8088 }
8089
8090 return (viewFlags & CLICKABLE) == CLICKABLE
8091 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8092 }
8093
8094 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008095 * Returns true if the view is currently hovered.
8096 *
8097 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008098 *
8099 * @see #setHovered
8100 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008101 */
Jeff Brown10b62902011-06-20 16:40:37 -07008102 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008103 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008104 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008105 }
8106
8107 /**
8108 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008109 * <p>
8110 * Calling this method also changes the drawable state of the view. This
8111 * enables the view to react to hover by using different drawable resources
8112 * to change its appearance.
8113 * </p><p>
8114 * The {@link #onHoverChanged} method is called when the hovered state changes.
8115 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008116 *
8117 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008118 *
8119 * @see #isHovered
8120 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008121 */
8122 public void setHovered(boolean hovered) {
8123 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008124 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8125 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008126 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008127 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008128 }
8129 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008130 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8131 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008132 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008133 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008134 }
8135 }
8136 }
8137
8138 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008139 * Implement this method to handle hover state changes.
8140 * <p>
8141 * This method is called whenever the hover state changes as a result of a
8142 * call to {@link #setHovered}.
8143 * </p>
8144 *
8145 * @param hovered The current hover state, as returned by {@link #isHovered}.
8146 *
8147 * @see #isHovered
8148 * @see #setHovered
8149 */
8150 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008151 }
8152
8153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 * Implement this method to handle touch screen motion events.
8155 *
8156 * @param event The motion event.
8157 * @return True if the event was handled, false otherwise.
8158 */
8159 public boolean onTouchEvent(MotionEvent event) {
8160 final int viewFlags = mViewFlags;
8161
8162 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008163 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008164 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 // A disabled view that is clickable still consumes the touch
8167 // events, it just doesn't respond to them.
8168 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8169 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8170 }
8171
8172 if (mTouchDelegate != null) {
8173 if (mTouchDelegate.onTouchEvent(event)) {
8174 return true;
8175 }
8176 }
8177
8178 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8179 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8180 switch (event.getAction()) {
8181 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008182 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8183 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 // take focus if we don't have it already and we should in
8185 // touch mode.
8186 boolean focusTaken = false;
8187 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8188 focusTaken = requestFocus();
8189 }
8190
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008191 if (prepressed) {
8192 // The button is being released before we actually
8193 // showed it as pressed. Make it show the pressed
8194 // state now (before scheduling the click) to ensure
8195 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008196 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008197 }
Joe Malin32736f02011-01-19 16:14:20 -08008198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 if (!mHasPerformedLongPress) {
8200 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008201 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202
8203 // Only perform take click actions if we were in the pressed state
8204 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008205 // Use a Runnable and post this rather than calling
8206 // performClick directly. This lets other visual state
8207 // of the view update before click actions start.
8208 if (mPerformClick == null) {
8209 mPerformClick = new PerformClick();
8210 }
8211 if (!post(mPerformClick)) {
8212 performClick();
8213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 }
8215 }
8216
8217 if (mUnsetPressedState == null) {
8218 mUnsetPressedState = new UnsetPressedState();
8219 }
8220
Adam Powelle14579b2009-12-16 18:39:52 -08008221 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008222 postDelayed(mUnsetPressedState,
8223 ViewConfiguration.getPressedStateDuration());
8224 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 // If the post failed, unpress right now
8226 mUnsetPressedState.run();
8227 }
Adam Powelle14579b2009-12-16 18:39:52 -08008228 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008229 }
8230 break;
8231
8232 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008233 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008234
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008235 if (performButtonActionOnTouchDown(event)) {
8236 break;
8237 }
8238
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008239 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008240 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008241
8242 // For views inside a scrolling container, delay the pressed feedback for
8243 // a short period in case this is a scroll.
8244 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008245 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008246 if (mPendingCheckForTap == null) {
8247 mPendingCheckForTap = new CheckForTap();
8248 }
8249 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8250 } else {
8251 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008252 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008253 checkForLongClick(0);
8254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 break;
8256
8257 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008258 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008259 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008260 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 break;
8262
8263 case MotionEvent.ACTION_MOVE:
8264 final int x = (int) event.getX();
8265 final int y = (int) event.getY();
8266
8267 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008268 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008270 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008271 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008272 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008273 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274
Adam Powell4d6f0662012-02-21 15:11:11 -08008275 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 }
8278 break;
8279 }
8280 return true;
8281 }
8282
8283 return false;
8284 }
8285
8286 /**
Adam Powell10298662011-08-14 18:26:30 -07008287 * @hide
8288 */
8289 public boolean isInScrollingContainer() {
8290 ViewParent p = getParent();
8291 while (p != null && p instanceof ViewGroup) {
8292 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8293 return true;
8294 }
8295 p = p.getParent();
8296 }
8297 return false;
8298 }
8299
8300 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008301 * Remove the longpress detection timer.
8302 */
8303 private void removeLongPressCallback() {
8304 if (mPendingCheckForLongPress != null) {
8305 removeCallbacks(mPendingCheckForLongPress);
8306 }
8307 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008308
8309 /**
8310 * Remove the pending click action
8311 */
8312 private void removePerformClickCallback() {
8313 if (mPerformClick != null) {
8314 removeCallbacks(mPerformClick);
8315 }
8316 }
8317
Adam Powelle14579b2009-12-16 18:39:52 -08008318 /**
Romain Guya440b002010-02-24 15:57:54 -08008319 * Remove the prepress detection timer.
8320 */
8321 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008322 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008323 setPressed(false);
8324 removeCallbacks(mUnsetPressedState);
8325 }
8326 }
8327
8328 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008329 * Remove the tap detection timer.
8330 */
8331 private void removeTapCallback() {
8332 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008333 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008334 removeCallbacks(mPendingCheckForTap);
8335 }
8336 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008337
8338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 * Cancels a pending long press. Your subclass can use this if you
8340 * want the context menu to come up if the user presses and holds
8341 * at the same place, but you don't want it to come up if they press
8342 * and then move around enough to cause scrolling.
8343 */
8344 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008345 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008346
8347 /*
8348 * The prepressed state handled by the tap callback is a display
8349 * construct, but the tap callback will post a long press callback
8350 * less its own timeout. Remove it here.
8351 */
8352 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 }
8354
8355 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008356 * Remove the pending callback for sending a
8357 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8358 */
8359 private void removeSendViewScrolledAccessibilityEventCallback() {
8360 if (mSendViewScrolledAccessibilityEvent != null) {
8361 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008362 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008363 }
8364 }
8365
8366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 * Sets the TouchDelegate for this View.
8368 */
8369 public void setTouchDelegate(TouchDelegate delegate) {
8370 mTouchDelegate = delegate;
8371 }
8372
8373 /**
8374 * Gets the TouchDelegate for this View.
8375 */
8376 public TouchDelegate getTouchDelegate() {
8377 return mTouchDelegate;
8378 }
8379
8380 /**
8381 * Set flags controlling behavior of this view.
8382 *
8383 * @param flags Constant indicating the value which should be set
8384 * @param mask Constant indicating the bit range that should be changed
8385 */
8386 void setFlags(int flags, int mask) {
8387 int old = mViewFlags;
8388 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8389
8390 int changed = mViewFlags ^ old;
8391 if (changed == 0) {
8392 return;
8393 }
8394 int privateFlags = mPrivateFlags;
8395
8396 /* Check if the FOCUSABLE bit has changed */
8397 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008398 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008400 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 /* Give up focus if we are no longer focusable */
8402 clearFocus();
8403 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008404 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 /*
8406 * Tell the view system that we are now available to take focus
8407 * if no one else already has it.
8408 */
8409 if (mParent != null) mParent.focusableViewAvailable(this);
8410 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008411 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8412 notifyAccessibilityStateChanged();
8413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 }
8415
8416 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8417 if ((changed & VISIBILITY_MASK) != 0) {
8418 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008419 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008420 * it was not visible. Marking it drawn ensures that the invalidation will
8421 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008423 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008424 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425
8426 needGlobalAttributesUpdate(true);
8427
8428 // a view becoming visible is worth notifying the parent
8429 // about in case nothing has focus. even if this specific view
8430 // isn't focusable, it may contain something that is, so let
8431 // the root view try to give this focus if nothing else does.
8432 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8433 mParent.focusableViewAvailable(this);
8434 }
8435 }
8436 }
8437
8438 /* Check if the GONE bit has changed */
8439 if ((changed & GONE) != 0) {
8440 needGlobalAttributesUpdate(false);
8441 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442
Romain Guyecd80ee2009-12-03 17:13:02 -08008443 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8444 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008445 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008446 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008447 if (mParent instanceof View) {
8448 // GONE views noop invalidation, so invalidate the parent
8449 ((View) mParent).invalidate(true);
8450 }
8451 // Mark the view drawn to ensure that it gets invalidated properly the next
8452 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008453 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 }
8455 if (mAttachInfo != null) {
8456 mAttachInfo.mViewVisibilityChanged = true;
8457 }
8458 }
8459
8460 /* Check if the VISIBLE bit has changed */
8461 if ((changed & INVISIBLE) != 0) {
8462 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008463 /*
8464 * If this view is becoming invisible, set the DRAWN flag so that
8465 * the next invalidate() will not be skipped.
8466 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008467 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468
8469 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008470 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 if (getRootView() != this) {
8472 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008473 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 }
8475 }
8476 if (mAttachInfo != null) {
8477 mAttachInfo.mViewVisibilityChanged = true;
8478 }
8479 }
8480
Adam Powell326d8082009-12-09 15:10:07 -08008481 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008482 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008483 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8484 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008485 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008486 } else if (mParent != null) {
8487 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008488 }
Adam Powell326d8082009-12-09 15:10:07 -08008489 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8490 }
8491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8493 destroyDrawingCache();
8494 }
8495
8496 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8497 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008498 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008499 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 }
8501
8502 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8503 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008504 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 }
8506
8507 if ((changed & DRAW_MASK) != 0) {
8508 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008509 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008510 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8511 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008513 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 }
8515 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008516 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 }
8518 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008519 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 }
8521
8522 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008523 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008524 mParent.recomputeViewAttributes(this);
8525 }
8526 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008527
8528 if (AccessibilityManager.getInstance(mContext).isEnabled()
8529 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8530 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8531 notifyAccessibilityStateChanged();
8532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 }
8534
8535 /**
8536 * Change the view's z order in the tree, so it's on top of other sibling
8537 * views
8538 */
8539 public void bringToFront() {
8540 if (mParent != null) {
8541 mParent.bringChildToFront(this);
8542 }
8543 }
8544
8545 /**
8546 * This is called in response to an internal scroll in this view (i.e., the
8547 * view scrolled its own contents). This is typically as a result of
8548 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8549 * called.
8550 *
8551 * @param l Current horizontal scroll origin.
8552 * @param t Current vertical scroll origin.
8553 * @param oldl Previous horizontal scroll origin.
8554 * @param oldt Previous vertical scroll origin.
8555 */
8556 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008557 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8558 postSendViewScrolledAccessibilityEventCallback();
8559 }
8560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561 mBackgroundSizeChanged = true;
8562
8563 final AttachInfo ai = mAttachInfo;
8564 if (ai != null) {
8565 ai.mViewScrollChanged = true;
8566 }
8567 }
8568
8569 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008570 * Interface definition for a callback to be invoked when the layout bounds of a view
8571 * changes due to layout processing.
8572 */
8573 public interface OnLayoutChangeListener {
8574 /**
8575 * Called when the focus state of a view has changed.
8576 *
8577 * @param v The view whose state has changed.
8578 * @param left The new value of the view's left property.
8579 * @param top The new value of the view's top property.
8580 * @param right The new value of the view's right property.
8581 * @param bottom The new value of the view's bottom property.
8582 * @param oldLeft The previous value of the view's left property.
8583 * @param oldTop The previous value of the view's top property.
8584 * @param oldRight The previous value of the view's right property.
8585 * @param oldBottom The previous value of the view's bottom property.
8586 */
8587 void onLayoutChange(View v, int left, int top, int right, int bottom,
8588 int oldLeft, int oldTop, int oldRight, int oldBottom);
8589 }
8590
8591 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 * This is called during layout when the size of this view has changed. If
8593 * you were just added to the view hierarchy, you're called with the old
8594 * values of 0.
8595 *
8596 * @param w Current width of this view.
8597 * @param h Current height of this view.
8598 * @param oldw Old width of this view.
8599 * @param oldh Old height of this view.
8600 */
8601 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8602 }
8603
8604 /**
8605 * Called by draw to draw the child views. This may be overridden
8606 * by derived classes to gain control just before its children are drawn
8607 * (but after its own view has been drawn).
8608 * @param canvas the canvas on which to draw the view
8609 */
8610 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 }
8613
8614 /**
8615 * Gets the parent of this view. Note that the parent is a
8616 * ViewParent and not necessarily a View.
8617 *
8618 * @return Parent of this view.
8619 */
8620 public final ViewParent getParent() {
8621 return mParent;
8622 }
8623
8624 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008625 * Set the horizontal scrolled position of your view. This will cause a call to
8626 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8627 * invalidated.
8628 * @param value the x position to scroll to
8629 */
8630 public void setScrollX(int value) {
8631 scrollTo(value, mScrollY);
8632 }
8633
8634 /**
8635 * Set the vertical scrolled position of your view. This will cause a call to
8636 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8637 * invalidated.
8638 * @param value the y position to scroll to
8639 */
8640 public void setScrollY(int value) {
8641 scrollTo(mScrollX, value);
8642 }
8643
8644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645 * Return the scrolled left position of this view. This is the left edge of
8646 * the displayed part of your view. You do not need to draw any pixels
8647 * farther left, since those are outside of the frame of your view on
8648 * screen.
8649 *
8650 * @return The left edge of the displayed part of your view, in pixels.
8651 */
8652 public final int getScrollX() {
8653 return mScrollX;
8654 }
8655
8656 /**
8657 * Return the scrolled top position of this view. This is the top edge of
8658 * the displayed part of your view. You do not need to draw any pixels above
8659 * it, since those are outside of the frame of your view on screen.
8660 *
8661 * @return The top edge of the displayed part of your view, in pixels.
8662 */
8663 public final int getScrollY() {
8664 return mScrollY;
8665 }
8666
8667 /**
8668 * Return the width of the your view.
8669 *
8670 * @return The width of your view, in pixels.
8671 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008672 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 public final int getWidth() {
8674 return mRight - mLeft;
8675 }
8676
8677 /**
8678 * Return the height of your view.
8679 *
8680 * @return The height of your view, in pixels.
8681 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008682 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 public final int getHeight() {
8684 return mBottom - mTop;
8685 }
8686
8687 /**
8688 * Return the visible drawing bounds of your view. Fills in the output
8689 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008690 * getWidth(), and getHeight(). These bounds do not account for any
8691 * transformation properties currently set on the view, such as
8692 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 *
8694 * @param outRect The (scrolled) drawing bounds of the view.
8695 */
8696 public void getDrawingRect(Rect outRect) {
8697 outRect.left = mScrollX;
8698 outRect.top = mScrollY;
8699 outRect.right = mScrollX + (mRight - mLeft);
8700 outRect.bottom = mScrollY + (mBottom - mTop);
8701 }
8702
8703 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008704 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8705 * raw width component (that is the result is masked by
8706 * {@link #MEASURED_SIZE_MASK}).
8707 *
8708 * @return The raw measured width of this view.
8709 */
8710 public final int getMeasuredWidth() {
8711 return mMeasuredWidth & MEASURED_SIZE_MASK;
8712 }
8713
8714 /**
8715 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008716 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008717 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 * This should be used during measurement and layout calculations only. Use
8719 * {@link #getWidth()} to see how wide a view is after layout.
8720 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008721 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008723 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 return mMeasuredWidth;
8725 }
8726
8727 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008728 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8729 * raw width component (that is the result is masked by
8730 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008732 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 */
8734 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008735 return mMeasuredHeight & MEASURED_SIZE_MASK;
8736 }
8737
8738 /**
8739 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008740 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008741 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8742 * This should be used during measurement and layout calculations only. Use
8743 * {@link #getHeight()} to see how wide a view is after layout.
8744 *
8745 * @return The measured width of this view as a bit mask.
8746 */
8747 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 return mMeasuredHeight;
8749 }
8750
8751 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008752 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8753 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8754 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8755 * and the height component is at the shifted bits
8756 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8757 */
8758 public final int getMeasuredState() {
8759 return (mMeasuredWidth&MEASURED_STATE_MASK)
8760 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8761 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8762 }
8763
8764 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008765 * The transform matrix of this view, which is calculated based on the current
8766 * roation, scale, and pivot properties.
8767 *
8768 * @see #getRotation()
8769 * @see #getScaleX()
8770 * @see #getScaleY()
8771 * @see #getPivotX()
8772 * @see #getPivotY()
8773 * @return The current transform matrix for the view
8774 */
8775 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008776 if (mTransformationInfo != null) {
8777 updateMatrix();
8778 return mTransformationInfo.mMatrix;
8779 }
8780 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008781 }
8782
8783 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008784 * Utility function to determine if the value is far enough away from zero to be
8785 * considered non-zero.
8786 * @param value A floating point value to check for zero-ness
8787 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8788 */
8789 private static boolean nonzero(float value) {
8790 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8791 }
8792
8793 /**
Jeff Brown86671742010-09-30 20:00:15 -07008794 * Returns true if the transform matrix is the identity matrix.
8795 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008796 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008797 * @return True if the transform matrix is the identity matrix, false otherwise.
8798 */
Jeff Brown86671742010-09-30 20:00:15 -07008799 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008800 if (mTransformationInfo != null) {
8801 updateMatrix();
8802 return mTransformationInfo.mMatrixIsIdentity;
8803 }
8804 return true;
8805 }
8806
8807 void ensureTransformationInfo() {
8808 if (mTransformationInfo == null) {
8809 mTransformationInfo = new TransformationInfo();
8810 }
Jeff Brown86671742010-09-30 20:00:15 -07008811 }
8812
8813 /**
8814 * Recomputes the transform matrix if necessary.
8815 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008816 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008817 final TransformationInfo info = mTransformationInfo;
8818 if (info == null) {
8819 return;
8820 }
8821 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008822 // transform-related properties have changed since the last time someone
8823 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008824
8825 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008826 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008827 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8828 info.mPrevWidth = mRight - mLeft;
8829 info.mPrevHeight = mBottom - mTop;
8830 info.mPivotX = info.mPrevWidth / 2f;
8831 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008832 }
8833 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008834 info.mMatrix.reset();
8835 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8836 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8837 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8838 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008839 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008840 if (info.mCamera == null) {
8841 info.mCamera = new Camera();
8842 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008843 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008844 info.mCamera.save();
8845 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8846 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8847 info.mCamera.getMatrix(info.matrix3D);
8848 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8849 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8850 info.mPivotY + info.mTranslationY);
8851 info.mMatrix.postConcat(info.matrix3D);
8852 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008853 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008854 info.mMatrixDirty = false;
8855 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8856 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008857 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008858 }
8859
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008860 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008861 * Utility method to retrieve the inverse of the current mMatrix property.
8862 * We cache the matrix to avoid recalculating it when transform properties
8863 * have not changed.
8864 *
8865 * @return The inverse of the current matrix of this view.
8866 */
Jeff Brown86671742010-09-30 20:00:15 -07008867 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008868 final TransformationInfo info = mTransformationInfo;
8869 if (info != null) {
8870 updateMatrix();
8871 if (info.mInverseMatrixDirty) {
8872 if (info.mInverseMatrix == null) {
8873 info.mInverseMatrix = new Matrix();
8874 }
8875 info.mMatrix.invert(info.mInverseMatrix);
8876 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008877 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008878 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008879 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008880 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008881 }
8882
8883 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008884 * Gets the distance along the Z axis from the camera to this view.
8885 *
8886 * @see #setCameraDistance(float)
8887 *
8888 * @return The distance along the Z axis.
8889 */
8890 public float getCameraDistance() {
8891 ensureTransformationInfo();
8892 final float dpi = mResources.getDisplayMetrics().densityDpi;
8893 final TransformationInfo info = mTransformationInfo;
8894 if (info.mCamera == null) {
8895 info.mCamera = new Camera();
8896 info.matrix3D = new Matrix();
8897 }
8898 return -(info.mCamera.getLocationZ() * dpi);
8899 }
8900
8901 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008902 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8903 * views are drawn) from the camera to this view. The camera's distance
8904 * affects 3D transformations, for instance rotations around the X and Y
8905 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008906 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008907 * use a camera distance that's greater than the height (X axis rotation) or
8908 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008909 *
Romain Guya5364ee2011-02-24 14:46:04 -08008910 * <p>The distance of the camera from the view plane can have an affect on the
8911 * perspective distortion of the view when it is rotated around the x or y axis.
8912 * For example, a large distance will result in a large viewing angle, and there
8913 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008914 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008915 * also result in some drawing artifacts if the rotated view ends up partially
8916 * behind the camera (which is why the recommendation is to use a distance at
8917 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008918 *
Romain Guya5364ee2011-02-24 14:46:04 -08008919 * <p>The distance is expressed in "depth pixels." The default distance depends
8920 * on the screen density. For instance, on a medium density display, the
8921 * default distance is 1280. On a high density display, the default distance
8922 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008923 *
Romain Guya5364ee2011-02-24 14:46:04 -08008924 * <p>If you want to specify a distance that leads to visually consistent
8925 * results across various densities, use the following formula:</p>
8926 * <pre>
8927 * float scale = context.getResources().getDisplayMetrics().density;
8928 * view.setCameraDistance(distance * scale);
8929 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008930 *
Romain Guya5364ee2011-02-24 14:46:04 -08008931 * <p>The density scale factor of a high density display is 1.5,
8932 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008933 *
Romain Guya5364ee2011-02-24 14:46:04 -08008934 * @param distance The distance in "depth pixels", if negative the opposite
8935 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008936 *
8937 * @see #setRotationX(float)
8938 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008939 */
8940 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008941 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008942
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008943 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008944 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008945 final TransformationInfo info = mTransformationInfo;
8946 if (info.mCamera == null) {
8947 info.mCamera = new Camera();
8948 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008949 }
8950
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008951 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8952 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008953
Chet Haase9d1992d2012-03-13 11:03:25 -07008954 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008955 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008956 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008957 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008958 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008959 // View was rejected last time it was drawn by its parent; this may have changed
8960 invalidateParentIfNeeded();
8961 }
Romain Guya5364ee2011-02-24 14:46:04 -08008962 }
8963
8964 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008965 * The degrees that the view is rotated around the pivot point.
8966 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008967 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008968 * @see #getPivotX()
8969 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008970 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008971 * @return The degrees of rotation.
8972 */
Chet Haasea5531132012-02-02 13:41:44 -08008973 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008974 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008975 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008976 }
8977
8978 /**
Chet Haase897247b2010-09-09 14:54:47 -07008979 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8980 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008981 *
8982 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008983 *
8984 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008985 * @see #getPivotX()
8986 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008987 * @see #setRotationX(float)
8988 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008989 *
8990 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008991 */
8992 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008993 ensureTransformationInfo();
8994 final TransformationInfo info = mTransformationInfo;
8995 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008996 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008997 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008998 info.mRotation = rotation;
8999 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009000 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009001 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009002 mDisplayList.setRotation(rotation);
9003 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009004 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009005 // View was rejected last time it was drawn by its parent; this may have changed
9006 invalidateParentIfNeeded();
9007 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 }
9009 }
9010
9011 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009012 * The degrees that the view is rotated around the vertical axis through the pivot point.
9013 *
9014 * @see #getPivotX()
9015 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009016 * @see #setRotationY(float)
9017 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009018 * @return The degrees of Y rotation.
9019 */
Chet Haasea5531132012-02-02 13:41:44 -08009020 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009021 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009022 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009023 }
9024
9025 /**
Chet Haase897247b2010-09-09 14:54:47 -07009026 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9027 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9028 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009029 *
Romain Guya5364ee2011-02-24 14:46:04 -08009030 * When rotating large views, it is recommended to adjust the camera distance
9031 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009032 *
9033 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009034 *
9035 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009036 * @see #getPivotX()
9037 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009038 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009039 * @see #setRotationX(float)
9040 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009041 *
9042 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009043 */
9044 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009045 ensureTransformationInfo();
9046 final TransformationInfo info = mTransformationInfo;
9047 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009048 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009049 info.mRotationY = rotationY;
9050 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009051 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009052 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009053 mDisplayList.setRotationY(rotationY);
9054 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009055 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009056 // View was rejected last time it was drawn by its parent; this may have changed
9057 invalidateParentIfNeeded();
9058 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009059 }
9060 }
9061
9062 /**
9063 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9064 *
9065 * @see #getPivotX()
9066 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009067 * @see #setRotationX(float)
9068 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009069 * @return The degrees of X rotation.
9070 */
Chet Haasea5531132012-02-02 13:41:44 -08009071 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009072 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009073 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009074 }
9075
9076 /**
Chet Haase897247b2010-09-09 14:54:47 -07009077 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9078 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9079 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009080 *
Romain Guya5364ee2011-02-24 14:46:04 -08009081 * When rotating large views, it is recommended to adjust the camera distance
9082 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009083 *
9084 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009085 *
9086 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009087 * @see #getPivotX()
9088 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009089 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009090 * @see #setRotationY(float)
9091 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009092 *
9093 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009094 */
9095 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009096 ensureTransformationInfo();
9097 final TransformationInfo info = mTransformationInfo;
9098 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009099 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009100 info.mRotationX = rotationX;
9101 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009102 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009103 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009104 mDisplayList.setRotationX(rotationX);
9105 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009106 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009107 // View was rejected last time it was drawn by its parent; this may have changed
9108 invalidateParentIfNeeded();
9109 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009110 }
9111 }
9112
9113 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009114 * The amount that the view is scaled in x around the pivot point, as a proportion of
9115 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9116 *
Joe Onorato93162322010-09-16 15:42:01 -04009117 * <p>By default, this is 1.0f.
9118 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009119 * @see #getPivotX()
9120 * @see #getPivotY()
9121 * @return The scaling factor.
9122 */
Chet Haasea5531132012-02-02 13:41:44 -08009123 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009124 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009125 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009126 }
9127
9128 /**
9129 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9130 * the view's unscaled width. A value of 1 means that no scaling is applied.
9131 *
9132 * @param scaleX The scaling factor.
9133 * @see #getPivotX()
9134 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009135 *
9136 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009137 */
9138 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009139 ensureTransformationInfo();
9140 final TransformationInfo info = mTransformationInfo;
9141 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009142 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009143 info.mScaleX = scaleX;
9144 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009145 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009146 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009147 mDisplayList.setScaleX(scaleX);
9148 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009149 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009150 // View was rejected last time it was drawn by its parent; this may have changed
9151 invalidateParentIfNeeded();
9152 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009153 }
9154 }
9155
9156 /**
9157 * The amount that the view is scaled in y around the pivot point, as a proportion of
9158 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9159 *
Joe Onorato93162322010-09-16 15:42:01 -04009160 * <p>By default, this is 1.0f.
9161 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009162 * @see #getPivotX()
9163 * @see #getPivotY()
9164 * @return The scaling factor.
9165 */
Chet Haasea5531132012-02-02 13:41:44 -08009166 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009167 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009168 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009169 }
9170
9171 /**
9172 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9173 * the view's unscaled width. A value of 1 means that no scaling is applied.
9174 *
9175 * @param scaleY The scaling factor.
9176 * @see #getPivotX()
9177 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009178 *
9179 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009180 */
9181 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009182 ensureTransformationInfo();
9183 final TransformationInfo info = mTransformationInfo;
9184 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009185 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009186 info.mScaleY = scaleY;
9187 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009188 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009189 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009190 mDisplayList.setScaleY(scaleY);
9191 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009192 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009193 // View was rejected last time it was drawn by its parent; this may have changed
9194 invalidateParentIfNeeded();
9195 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009196 }
9197 }
9198
9199 /**
9200 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9201 * and {@link #setScaleX(float) scaled}.
9202 *
9203 * @see #getRotation()
9204 * @see #getScaleX()
9205 * @see #getScaleY()
9206 * @see #getPivotY()
9207 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009208 *
9209 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009210 */
Chet Haasea5531132012-02-02 13:41:44 -08009211 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009212 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009213 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009214 }
9215
9216 /**
9217 * Sets the x location of the point around which the view is
9218 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009219 * By default, the pivot point is centered on the object.
9220 * Setting this property disables this behavior and causes the view to use only the
9221 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009222 *
9223 * @param pivotX The x location of the pivot point.
9224 * @see #getRotation()
9225 * @see #getScaleX()
9226 * @see #getScaleY()
9227 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009228 *
9229 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009230 */
9231 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009232 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009233 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009234 final TransformationInfo info = mTransformationInfo;
9235 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009236 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009237 info.mPivotX = pivotX;
9238 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009239 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009240 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009241 mDisplayList.setPivotX(pivotX);
9242 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009243 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009244 // View was rejected last time it was drawn by its parent; this may have changed
9245 invalidateParentIfNeeded();
9246 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009247 }
9248 }
9249
9250 /**
9251 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9252 * and {@link #setScaleY(float) scaled}.
9253 *
9254 * @see #getRotation()
9255 * @see #getScaleX()
9256 * @see #getScaleY()
9257 * @see #getPivotY()
9258 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009259 *
9260 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009261 */
Chet Haasea5531132012-02-02 13:41:44 -08009262 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009263 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009264 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009265 }
9266
9267 /**
9268 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009269 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9270 * Setting this property disables this behavior and causes the view to use only the
9271 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009272 *
9273 * @param pivotY The y location of the pivot point.
9274 * @see #getRotation()
9275 * @see #getScaleX()
9276 * @see #getScaleY()
9277 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009278 *
9279 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009280 */
9281 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009282 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009283 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009284 final TransformationInfo info = mTransformationInfo;
9285 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009286 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009287 info.mPivotY = pivotY;
9288 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009289 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009290 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009291 mDisplayList.setPivotY(pivotY);
9292 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009293 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009294 // View was rejected last time it was drawn by its parent; this may have changed
9295 invalidateParentIfNeeded();
9296 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009297 }
9298 }
9299
9300 /**
9301 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9302 * completely transparent and 1 means the view is completely opaque.
9303 *
Joe Onorato93162322010-09-16 15:42:01 -04009304 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009305 * @return The opacity of the view.
9306 */
Chet Haasea5531132012-02-02 13:41:44 -08009307 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009308 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009309 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009310 }
9311
9312 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009313 * Returns whether this View has content which overlaps. This function, intended to be
9314 * overridden by specific View types, is an optimization when alpha is set on a view. If
9315 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9316 * and then composited it into place, which can be expensive. If the view has no overlapping
9317 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9318 * An example of overlapping rendering is a TextView with a background image, such as a
9319 * Button. An example of non-overlapping rendering is a TextView with no background, or
9320 * an ImageView with only the foreground image. The default implementation returns true;
9321 * subclasses should override if they have cases which can be optimized.
9322 *
9323 * @return true if the content in this view might overlap, false otherwise.
9324 */
9325 public boolean hasOverlappingRendering() {
9326 return true;
9327 }
9328
9329 /**
Romain Guy171c5922011-01-06 10:04:23 -08009330 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9331 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009332 *
Romain Guy171c5922011-01-06 10:04:23 -08009333 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9334 * responsible for applying the opacity itself. Otherwise, calling this method is
9335 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009336 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009337 *
Chet Haasea5531132012-02-02 13:41:44 -08009338 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9339 * performance implications. It is generally best to use the alpha property sparingly and
9340 * transiently, as in the case of fading animations.</p>
9341 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009342 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009343 *
Joe Malin32736f02011-01-19 16:14:20 -08009344 * @see #setLayerType(int, android.graphics.Paint)
9345 *
Chet Haase73066682010-11-29 15:55:32 -08009346 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009347 */
9348 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009349 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009350 if (mTransformationInfo.mAlpha != alpha) {
9351 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009352 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009353 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009354 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009355 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009356 invalidate(true);
9357 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009358 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009359 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009360 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009361 mDisplayList.setAlpha(alpha);
9362 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009363 }
Chet Haaseed032702010-10-01 14:05:54 -07009364 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009365 }
9366
9367 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009368 * Faster version of setAlpha() which performs the same steps except there are
9369 * no calls to invalidate(). The caller of this function should perform proper invalidation
9370 * on the parent and this object. The return value indicates whether the subclass handles
9371 * alpha (the return value for onSetAlpha()).
9372 *
9373 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009374 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9375 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009376 */
9377 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009378 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009379 if (mTransformationInfo.mAlpha != alpha) {
9380 mTransformationInfo.mAlpha = alpha;
9381 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9382 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009383 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009384 return true;
9385 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009386 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009387 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009388 mDisplayList.setAlpha(alpha);
9389 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009390 }
Chet Haasea00f3862011-02-22 06:34:40 -08009391 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009392 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009393 }
9394
9395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 * Top position of this view relative to its parent.
9397 *
9398 * @return The top of this view, in pixels.
9399 */
9400 @ViewDebug.CapturedViewProperty
9401 public final int getTop() {
9402 return mTop;
9403 }
9404
9405 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009406 * Sets the top position of this view relative to its parent. This method is meant to be called
9407 * by the layout system and should not generally be called otherwise, because the property
9408 * may be changed at any time by the layout.
9409 *
9410 * @param top The top of this view, in pixels.
9411 */
9412 public final void setTop(int top) {
9413 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009414 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009415 final boolean matrixIsIdentity = mTransformationInfo == null
9416 || mTransformationInfo.mMatrixIsIdentity;
9417 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009418 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009419 int minTop;
9420 int yLoc;
9421 if (top < mTop) {
9422 minTop = top;
9423 yLoc = top - mTop;
9424 } else {
9425 minTop = mTop;
9426 yLoc = 0;
9427 }
Chet Haasee9140a72011-02-16 16:23:29 -08009428 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009429 }
9430 } else {
9431 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009432 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009433 }
9434
Chet Haaseed032702010-10-01 14:05:54 -07009435 int width = mRight - mLeft;
9436 int oldHeight = mBottom - mTop;
9437
Chet Haase21cd1382010-09-01 17:42:29 -07009438 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009439 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009440 mDisplayList.setTop(mTop);
9441 }
Chet Haase21cd1382010-09-01 17:42:29 -07009442
Chet Haaseed032702010-10-01 14:05:54 -07009443 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9444
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009445 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009446 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009447 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009448 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009449 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009450 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009451 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009452 }
Chet Haase55dbb652010-12-21 20:15:08 -08009453 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009454 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009455 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009456 // View was rejected last time it was drawn by its parent; this may have changed
9457 invalidateParentIfNeeded();
9458 }
Chet Haase21cd1382010-09-01 17:42:29 -07009459 }
9460 }
9461
9462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 * Bottom position of this view relative to its parent.
9464 *
9465 * @return The bottom of this view, in pixels.
9466 */
9467 @ViewDebug.CapturedViewProperty
9468 public final int getBottom() {
9469 return mBottom;
9470 }
9471
9472 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009473 * True if this view has changed since the last time being drawn.
9474 *
9475 * @return The dirty state of this view.
9476 */
9477 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009478 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009479 }
9480
9481 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009482 * Sets the bottom position of this view relative to its parent. This method is meant to be
9483 * called by the layout system and should not generally be called otherwise, because the
9484 * property may be changed at any time by the layout.
9485 *
9486 * @param bottom The bottom of this view, in pixels.
9487 */
9488 public final void setBottom(int bottom) {
9489 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009490 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009491 final boolean matrixIsIdentity = mTransformationInfo == null
9492 || mTransformationInfo.mMatrixIsIdentity;
9493 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009494 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009495 int maxBottom;
9496 if (bottom < mBottom) {
9497 maxBottom = mBottom;
9498 } else {
9499 maxBottom = bottom;
9500 }
Chet Haasee9140a72011-02-16 16:23:29 -08009501 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009502 }
9503 } else {
9504 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009505 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009506 }
9507
Chet Haaseed032702010-10-01 14:05:54 -07009508 int width = mRight - mLeft;
9509 int oldHeight = mBottom - mTop;
9510
Chet Haase21cd1382010-09-01 17:42:29 -07009511 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009512 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009513 mDisplayList.setBottom(mBottom);
9514 }
Chet Haase21cd1382010-09-01 17:42:29 -07009515
Chet Haaseed032702010-10-01 14:05:54 -07009516 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9517
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009518 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009519 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009520 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009521 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009522 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009523 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009524 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009525 }
Chet Haase55dbb652010-12-21 20:15:08 -08009526 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009527 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009528 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009529 // View was rejected last time it was drawn by its parent; this may have changed
9530 invalidateParentIfNeeded();
9531 }
Chet Haase21cd1382010-09-01 17:42:29 -07009532 }
9533 }
9534
9535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009536 * Left position of this view relative to its parent.
9537 *
9538 * @return The left edge of this view, in pixels.
9539 */
9540 @ViewDebug.CapturedViewProperty
9541 public final int getLeft() {
9542 return mLeft;
9543 }
9544
9545 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009546 * Sets the left position of this view relative to its parent. This method is meant to be called
9547 * by the layout system and should not generally be called otherwise, because the property
9548 * may be changed at any time by the layout.
9549 *
9550 * @param left The bottom of this view, in pixels.
9551 */
9552 public final void setLeft(int left) {
9553 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009554 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009555 final boolean matrixIsIdentity = mTransformationInfo == null
9556 || mTransformationInfo.mMatrixIsIdentity;
9557 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009558 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009559 int minLeft;
9560 int xLoc;
9561 if (left < mLeft) {
9562 minLeft = left;
9563 xLoc = left - mLeft;
9564 } else {
9565 minLeft = mLeft;
9566 xLoc = 0;
9567 }
Chet Haasee9140a72011-02-16 16:23:29 -08009568 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009569 }
9570 } else {
9571 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009572 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009573 }
9574
Chet Haaseed032702010-10-01 14:05:54 -07009575 int oldWidth = mRight - mLeft;
9576 int height = mBottom - mTop;
9577
Chet Haase21cd1382010-09-01 17:42:29 -07009578 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009579 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009580 mDisplayList.setLeft(left);
9581 }
Chet Haase21cd1382010-09-01 17:42:29 -07009582
Chet Haaseed032702010-10-01 14:05:54 -07009583 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9584
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009585 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009586 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009587 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009588 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009589 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009590 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009591 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009592 }
Chet Haase55dbb652010-12-21 20:15:08 -08009593 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009594 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009595 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009596 // View was rejected last time it was drawn by its parent; this may have changed
9597 invalidateParentIfNeeded();
9598 }
Chet Haase21cd1382010-09-01 17:42:29 -07009599 }
9600 }
9601
9602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 * Right position of this view relative to its parent.
9604 *
9605 * @return The right edge of this view, in pixels.
9606 */
9607 @ViewDebug.CapturedViewProperty
9608 public final int getRight() {
9609 return mRight;
9610 }
9611
9612 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009613 * Sets the right position of this view relative to its parent. This method is meant to be called
9614 * by the layout system and should not generally be called otherwise, because the property
9615 * may be changed at any time by the layout.
9616 *
9617 * @param right The bottom of this view, in pixels.
9618 */
9619 public final void setRight(int right) {
9620 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009621 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009622 final boolean matrixIsIdentity = mTransformationInfo == null
9623 || mTransformationInfo.mMatrixIsIdentity;
9624 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009625 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009626 int maxRight;
9627 if (right < mRight) {
9628 maxRight = mRight;
9629 } else {
9630 maxRight = right;
9631 }
Chet Haasee9140a72011-02-16 16:23:29 -08009632 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009633 }
9634 } else {
9635 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009636 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009637 }
9638
Chet Haaseed032702010-10-01 14:05:54 -07009639 int oldWidth = mRight - mLeft;
9640 int height = mBottom - mTop;
9641
Chet Haase21cd1382010-09-01 17:42:29 -07009642 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009643 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009644 mDisplayList.setRight(mRight);
9645 }
Chet Haase21cd1382010-09-01 17:42:29 -07009646
Chet Haaseed032702010-10-01 14:05:54 -07009647 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9648
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009649 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009650 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009651 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009652 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009653 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009654 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009655 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009656 }
Chet Haase55dbb652010-12-21 20:15:08 -08009657 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009658 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009659 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009660 // View was rejected last time it was drawn by its parent; this may have changed
9661 invalidateParentIfNeeded();
9662 }
Chet Haase21cd1382010-09-01 17:42:29 -07009663 }
9664 }
9665
9666 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009667 * The visual x position of this view, in pixels. This is equivalent to the
9668 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009669 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009670 *
Chet Haasedf030d22010-07-30 17:22:38 -07009671 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009672 */
Chet Haasea5531132012-02-02 13:41:44 -08009673 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009674 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009675 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009676 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009677
Chet Haasedf030d22010-07-30 17:22:38 -07009678 /**
9679 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9680 * {@link #setTranslationX(float) translationX} property to be the difference between
9681 * the x value passed in and the current {@link #getLeft() left} property.
9682 *
9683 * @param x The visual x position of this view, in pixels.
9684 */
9685 public void setX(float x) {
9686 setTranslationX(x - mLeft);
9687 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009688
Chet Haasedf030d22010-07-30 17:22:38 -07009689 /**
9690 * The visual y position of this view, in pixels. This is equivalent to the
9691 * {@link #setTranslationY(float) translationY} property plus the current
9692 * {@link #getTop() top} property.
9693 *
9694 * @return The visual y position of this view, in pixels.
9695 */
Chet Haasea5531132012-02-02 13:41:44 -08009696 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009697 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009698 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009699 }
9700
9701 /**
9702 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9703 * {@link #setTranslationY(float) translationY} property to be the difference between
9704 * the y value passed in and the current {@link #getTop() top} property.
9705 *
9706 * @param y The visual y position of this view, in pixels.
9707 */
9708 public void setY(float y) {
9709 setTranslationY(y - mTop);
9710 }
9711
9712
9713 /**
9714 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9715 * This position is post-layout, in addition to wherever the object's
9716 * layout placed it.
9717 *
9718 * @return The horizontal position of this view relative to its left position, in pixels.
9719 */
Chet Haasea5531132012-02-02 13:41:44 -08009720 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009721 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009722 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009723 }
9724
9725 /**
9726 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9727 * This effectively positions the object post-layout, in addition to wherever the object's
9728 * layout placed it.
9729 *
9730 * @param translationX The horizontal position of this view relative to its left position,
9731 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009732 *
9733 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009734 */
9735 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009736 ensureTransformationInfo();
9737 final TransformationInfo info = mTransformationInfo;
9738 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009739 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009740 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009741 info.mTranslationX = translationX;
9742 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009743 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009744 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009745 mDisplayList.setTranslationX(translationX);
9746 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009747 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009748 // View was rejected last time it was drawn by its parent; this may have changed
9749 invalidateParentIfNeeded();
9750 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009751 }
9752 }
9753
9754 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009755 * The horizontal location of this view relative to its {@link #getTop() top} position.
9756 * This position is post-layout, in addition to wherever the object's
9757 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009758 *
Chet Haasedf030d22010-07-30 17:22:38 -07009759 * @return The vertical position of this view relative to its top position,
9760 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009761 */
Chet Haasea5531132012-02-02 13:41:44 -08009762 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009763 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009764 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009765 }
9766
9767 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009768 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9769 * This effectively positions the object post-layout, in addition to wherever the object's
9770 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009771 *
Chet Haasedf030d22010-07-30 17:22:38 -07009772 * @param translationY The vertical position of this view relative to its top position,
9773 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009774 *
9775 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009776 */
Chet Haasedf030d22010-07-30 17:22:38 -07009777 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009778 ensureTransformationInfo();
9779 final TransformationInfo info = mTransformationInfo;
9780 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009781 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009782 info.mTranslationY = translationY;
9783 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009784 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009785 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009786 mDisplayList.setTranslationY(translationY);
9787 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009788 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009789 // View was rejected last time it was drawn by its parent; this may have changed
9790 invalidateParentIfNeeded();
9791 }
Chet Haasedf030d22010-07-30 17:22:38 -07009792 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009793 }
9794
9795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 * Hit rectangle in parent's coordinates
9797 *
9798 * @param outRect The hit rectangle of the view.
9799 */
9800 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009801 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009802 final TransformationInfo info = mTransformationInfo;
9803 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009804 outRect.set(mLeft, mTop, mRight, mBottom);
9805 } else {
9806 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009807 tmpRect.set(-info.mPivotX, -info.mPivotY,
9808 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9809 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009810 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9811 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009812 }
9813 }
9814
9815 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009816 * Determines whether the given point, in local coordinates is inside the view.
9817 */
9818 /*package*/ final boolean pointInView(float localX, float localY) {
9819 return localX >= 0 && localX < (mRight - mLeft)
9820 && localY >= 0 && localY < (mBottom - mTop);
9821 }
9822
9823 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009824 * Utility method to determine whether the given point, in local coordinates,
9825 * is inside the view, where the area of the view is expanded by the slop factor.
9826 * This method is called while processing touch-move events to determine if the event
9827 * is still within the view.
9828 */
9829 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009830 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009831 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009832 }
9833
9834 /**
9835 * When a view has focus and the user navigates away from it, the next view is searched for
9836 * starting from the rectangle filled in by this method.
9837 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009838 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009839 * of the view. However, if your view maintains some idea of internal selection,
9840 * such as a cursor, or a selected row or column, you should override this method and
9841 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 *
9843 * @param r The rectangle to fill in, in this view's coordinates.
9844 */
9845 public void getFocusedRect(Rect r) {
9846 getDrawingRect(r);
9847 }
9848
9849 /**
9850 * If some part of this view is not clipped by any of its parents, then
9851 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009852 * coordinates (without taking possible View rotations into account), offset
9853 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9854 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 *
9856 * @param r If true is returned, r holds the global coordinates of the
9857 * visible portion of this view.
9858 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9859 * between this view and its root. globalOffet may be null.
9860 * @return true if r is non-empty (i.e. part of the view is visible at the
9861 * root level.
9862 */
9863 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9864 int width = mRight - mLeft;
9865 int height = mBottom - mTop;
9866 if (width > 0 && height > 0) {
9867 r.set(0, 0, width, height);
9868 if (globalOffset != null) {
9869 globalOffset.set(-mScrollX, -mScrollY);
9870 }
9871 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9872 }
9873 return false;
9874 }
9875
9876 public final boolean getGlobalVisibleRect(Rect r) {
9877 return getGlobalVisibleRect(r, null);
9878 }
9879
9880 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009881 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009882 if (getGlobalVisibleRect(r, offset)) {
9883 r.offset(-offset.x, -offset.y); // make r local
9884 return true;
9885 }
9886 return false;
9887 }
9888
9889 /**
9890 * Offset this view's vertical location by the specified number of pixels.
9891 *
9892 * @param offset the number of pixels to offset the view by
9893 */
9894 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009895 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009896 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009897 final boolean matrixIsIdentity = mTransformationInfo == null
9898 || mTransformationInfo.mMatrixIsIdentity;
9899 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009900 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009901 invalidateViewProperty(false, false);
9902 } else {
9903 final ViewParent p = mParent;
9904 if (p != null && mAttachInfo != null) {
9905 final Rect r = mAttachInfo.mTmpInvalRect;
9906 int minTop;
9907 int maxBottom;
9908 int yLoc;
9909 if (offset < 0) {
9910 minTop = mTop + offset;
9911 maxBottom = mBottom;
9912 yLoc = offset;
9913 } else {
9914 minTop = mTop;
9915 maxBottom = mBottom + offset;
9916 yLoc = 0;
9917 }
9918 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9919 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009920 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009921 }
9922 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009923 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009924 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009925
Chet Haasec3aa3612010-06-17 08:50:37 -07009926 mTop += offset;
9927 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009928 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009929 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009930 invalidateViewProperty(false, false);
9931 } else {
9932 if (!matrixIsIdentity) {
9933 invalidateViewProperty(false, true);
9934 }
9935 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009936 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 }
9939
9940 /**
9941 * Offset this view's horizontal location by the specified amount of pixels.
9942 *
9943 * @param offset the numer of pixels to offset the view by
9944 */
9945 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009946 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009947 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009948 final boolean matrixIsIdentity = mTransformationInfo == null
9949 || mTransformationInfo.mMatrixIsIdentity;
9950 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009951 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009952 invalidateViewProperty(false, false);
9953 } else {
9954 final ViewParent p = mParent;
9955 if (p != null && mAttachInfo != null) {
9956 final Rect r = mAttachInfo.mTmpInvalRect;
9957 int minLeft;
9958 int maxRight;
9959 if (offset < 0) {
9960 minLeft = mLeft + offset;
9961 maxRight = mRight;
9962 } else {
9963 minLeft = mLeft;
9964 maxRight = mRight + offset;
9965 }
9966 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9967 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009968 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009969 }
9970 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009971 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009972 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009973
Chet Haasec3aa3612010-06-17 08:50:37 -07009974 mLeft += offset;
9975 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009976 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009977 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009978 invalidateViewProperty(false, false);
9979 } else {
9980 if (!matrixIsIdentity) {
9981 invalidateViewProperty(false, true);
9982 }
9983 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009984 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 }
9987
9988 /**
9989 * Get the LayoutParams associated with this view. All views should have
9990 * layout parameters. These supply parameters to the <i>parent</i> of this
9991 * view specifying how it should be arranged. There are many subclasses of
9992 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9993 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009994 *
9995 * This method may return null if this View is not attached to a parent
9996 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9997 * was not invoked successfully. When a View is attached to a parent
9998 * ViewGroup, this method must not return null.
9999 *
10000 * @return The LayoutParams associated with this view, or null if no
10001 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010003 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010004 public ViewGroup.LayoutParams getLayoutParams() {
10005 return mLayoutParams;
10006 }
10007
10008 /**
10009 * Set the layout parameters associated with this view. These supply
10010 * parameters to the <i>parent</i> of this view specifying how it should be
10011 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10012 * correspond to the different subclasses of ViewGroup that are responsible
10013 * for arranging their children.
10014 *
Romain Guy01c174b2011-02-22 11:51:06 -080010015 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 */
10017 public void setLayoutParams(ViewGroup.LayoutParams params) {
10018 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010019 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 }
10021 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010022 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010023 if (mParent instanceof ViewGroup) {
10024 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 requestLayout();
10027 }
10028
10029 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010030 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010031 *
10032 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010033 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010034 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010035 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010036 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010037 }
10038 }
10039
10040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010041 * Set the scrolled position of your view. This will cause a call to
10042 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10043 * invalidated.
10044 * @param x the x position to scroll to
10045 * @param y the y position to scroll to
10046 */
10047 public void scrollTo(int x, int y) {
10048 if (mScrollX != x || mScrollY != y) {
10049 int oldX = mScrollX;
10050 int oldY = mScrollY;
10051 mScrollX = x;
10052 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010053 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010055 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010056 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 }
10059 }
10060
10061 /**
10062 * Move the scrolled position of your view. This will cause a call to
10063 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10064 * invalidated.
10065 * @param x the amount of pixels to scroll by horizontally
10066 * @param y the amount of pixels to scroll by vertically
10067 */
10068 public void scrollBy(int x, int y) {
10069 scrollTo(mScrollX + x, mScrollY + y);
10070 }
10071
10072 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010073 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10074 * animation to fade the scrollbars out after a default delay. If a subclass
10075 * provides animated scrolling, the start delay should equal the duration
10076 * of the scrolling animation.</p>
10077 *
10078 * <p>The animation starts only if at least one of the scrollbars is
10079 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10080 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10081 * this method returns true, and false otherwise. If the animation is
10082 * started, this method calls {@link #invalidate()}; in that case the
10083 * caller should not call {@link #invalidate()}.</p>
10084 *
10085 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010086 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010087 *
10088 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10089 * and {@link #scrollTo(int, int)}.</p>
10090 *
10091 * @return true if the animation is played, false otherwise
10092 *
10093 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010094 * @see #scrollBy(int, int)
10095 * @see #scrollTo(int, int)
10096 * @see #isHorizontalScrollBarEnabled()
10097 * @see #isVerticalScrollBarEnabled()
10098 * @see #setHorizontalScrollBarEnabled(boolean)
10099 * @see #setVerticalScrollBarEnabled(boolean)
10100 */
10101 protected boolean awakenScrollBars() {
10102 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010103 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010104 }
10105
10106 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010107 * Trigger the scrollbars to draw.
10108 * This method differs from awakenScrollBars() only in its default duration.
10109 * initialAwakenScrollBars() will show the scroll bars for longer than
10110 * usual to give the user more of a chance to notice them.
10111 *
10112 * @return true if the animation is played, false otherwise.
10113 */
10114 private boolean initialAwakenScrollBars() {
10115 return mScrollCache != null &&
10116 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10117 }
10118
10119 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010120 * <p>
10121 * Trigger the scrollbars to draw. When invoked this method starts an
10122 * animation to fade the scrollbars out after a fixed delay. If a subclass
10123 * provides animated scrolling, the start delay should equal the duration of
10124 * the scrolling animation.
10125 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010126 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010127 * <p>
10128 * The animation starts only if at least one of the scrollbars is enabled,
10129 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10130 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10131 * this method returns true, and false otherwise. If the animation is
10132 * started, this method calls {@link #invalidate()}; in that case the caller
10133 * should not call {@link #invalidate()}.
10134 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010135 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010136 * <p>
10137 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010138 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010139 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010140 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010141 * @param startDelay the delay, in milliseconds, after which the animation
10142 * should start; when the delay is 0, the animation starts
10143 * immediately
10144 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010145 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010146 * @see #scrollBy(int, int)
10147 * @see #scrollTo(int, int)
10148 * @see #isHorizontalScrollBarEnabled()
10149 * @see #isVerticalScrollBarEnabled()
10150 * @see #setHorizontalScrollBarEnabled(boolean)
10151 * @see #setVerticalScrollBarEnabled(boolean)
10152 */
10153 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010154 return awakenScrollBars(startDelay, true);
10155 }
Joe Malin32736f02011-01-19 16:14:20 -080010156
Mike Cleron290947b2009-09-29 18:34:32 -070010157 /**
10158 * <p>
10159 * Trigger the scrollbars to draw. When invoked this method starts an
10160 * animation to fade the scrollbars out after a fixed delay. If a subclass
10161 * provides animated scrolling, the start delay should equal the duration of
10162 * the scrolling animation.
10163 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010164 *
Mike Cleron290947b2009-09-29 18:34:32 -070010165 * <p>
10166 * The animation starts only if at least one of the scrollbars is enabled,
10167 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10168 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10169 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010170 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010171 * is set to true; in that case the caller
10172 * should not call {@link #invalidate()}.
10173 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010174 *
Mike Cleron290947b2009-09-29 18:34:32 -070010175 * <p>
10176 * This method should be invoked everytime a subclass directly updates the
10177 * scroll parameters.
10178 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010179 *
Mike Cleron290947b2009-09-29 18:34:32 -070010180 * @param startDelay the delay, in milliseconds, after which the animation
10181 * should start; when the delay is 0, the animation starts
10182 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010183 *
Mike Cleron290947b2009-09-29 18:34:32 -070010184 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010185 *
Mike Cleron290947b2009-09-29 18:34:32 -070010186 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010187 *
Mike Cleron290947b2009-09-29 18:34:32 -070010188 * @see #scrollBy(int, int)
10189 * @see #scrollTo(int, int)
10190 * @see #isHorizontalScrollBarEnabled()
10191 * @see #isVerticalScrollBarEnabled()
10192 * @see #setHorizontalScrollBarEnabled(boolean)
10193 * @see #setVerticalScrollBarEnabled(boolean)
10194 */
10195 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010196 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010197
Mike Cleronf116bf82009-09-27 19:14:12 -070010198 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10199 return false;
10200 }
10201
10202 if (scrollCache.scrollBar == null) {
10203 scrollCache.scrollBar = new ScrollBarDrawable();
10204 }
10205
10206 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10207
Mike Cleron290947b2009-09-29 18:34:32 -070010208 if (invalidate) {
10209 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010210 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010211 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010212
10213 if (scrollCache.state == ScrollabilityCache.OFF) {
10214 // FIXME: this is copied from WindowManagerService.
10215 // We should get this value from the system when it
10216 // is possible to do so.
10217 final int KEY_REPEAT_FIRST_DELAY = 750;
10218 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10219 }
10220
10221 // Tell mScrollCache when we should start fading. This may
10222 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010223 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010224 scrollCache.fadeStartTime = fadeStartTime;
10225 scrollCache.state = ScrollabilityCache.ON;
10226
10227 // Schedule our fader to run, unscheduling any old ones first
10228 if (mAttachInfo != null) {
10229 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10230 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10231 }
10232
10233 return true;
10234 }
10235
10236 return false;
10237 }
10238
10239 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010240 * Do not invalidate views which are not visible and which are not running an animation. They
10241 * will not get drawn and they should not set dirty flags as if they will be drawn
10242 */
10243 private boolean skipInvalidate() {
10244 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10245 (!(mParent instanceof ViewGroup) ||
10246 !((ViewGroup) mParent).isViewTransitioning(this));
10247 }
10248 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010249 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010250 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10251 * in the future. This must be called from a UI thread. To call from a non-UI
10252 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 *
10254 * WARNING: This method is destructive to dirty.
10255 * @param dirty the rectangle representing the bounds of the dirty region
10256 */
10257 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010258 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010259 return;
10260 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010261 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10262 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10263 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10264 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10265 mPrivateFlags |= PFLAG_INVALIDATED;
10266 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 final ViewParent p = mParent;
10268 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010269 //noinspection PointlessBooleanExpression,ConstantConditions
10270 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10271 if (p != null && ai != null && ai.mHardwareAccelerated) {
10272 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010273 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010274 p.invalidateChild(this, null);
10275 return;
10276 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 if (p != null && ai != null) {
10279 final int scrollX = mScrollX;
10280 final int scrollY = mScrollY;
10281 final Rect r = ai.mTmpInvalRect;
10282 r.set(dirty.left - scrollX, dirty.top - scrollY,
10283 dirty.right - scrollX, dirty.bottom - scrollY);
10284 mParent.invalidateChild(this, r);
10285 }
10286 }
10287 }
10288
10289 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010290 * 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 -080010291 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010292 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10293 * will be called at some point in the future. This must be called from
10294 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 * @param l the left position of the dirty region
10296 * @param t the top position of the dirty region
10297 * @param r the right position of the dirty region
10298 * @param b the bottom position of the dirty region
10299 */
10300 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010301 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010302 return;
10303 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010304 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10305 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10306 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10307 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10308 mPrivateFlags |= PFLAG_INVALIDATED;
10309 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 final ViewParent p = mParent;
10311 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010312 //noinspection PointlessBooleanExpression,ConstantConditions
10313 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10314 if (p != null && ai != null && ai.mHardwareAccelerated) {
10315 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010316 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010317 p.invalidateChild(this, null);
10318 return;
10319 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 if (p != null && ai != null && l < r && t < b) {
10322 final int scrollX = mScrollX;
10323 final int scrollY = mScrollY;
10324 final Rect tmpr = ai.mTmpInvalRect;
10325 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10326 p.invalidateChild(this, tmpr);
10327 }
10328 }
10329 }
10330
10331 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010332 * Invalidate the whole view. If the view is visible,
10333 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10334 * the future. This must be called from a UI thread. To call from a non-UI thread,
10335 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 */
10337 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010338 invalidate(true);
10339 }
Joe Malin32736f02011-01-19 16:14:20 -080010340
Chet Haaseed032702010-10-01 14:05:54 -070010341 /**
10342 * This is where the invalidate() work actually happens. A full invalidate()
10343 * causes the drawing cache to be invalidated, but this function can be called with
10344 * invalidateCache set to false to skip that invalidation step for cases that do not
10345 * need it (for example, a component that remains at the same dimensions with the same
10346 * content).
10347 *
10348 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10349 * well. This is usually true for a full invalidate, but may be set to false if the
10350 * View's contents or dimensions have not changed.
10351 */
Romain Guy849d0a32011-02-01 17:20:48 -080010352 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010353 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010354 return;
10355 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010356 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10357 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10358 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010359 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010360 mPrivateFlags &= ~PFLAG_DRAWN;
10361 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010362 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010363 mPrivateFlags |= PFLAG_INVALIDATED;
10364 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010367 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010368 //noinspection PointlessBooleanExpression,ConstantConditions
10369 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10370 if (p != null && ai != null && ai.mHardwareAccelerated) {
10371 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010372 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010373 p.invalidateChild(this, null);
10374 return;
10375 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010376 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 if (p != null && ai != null) {
10379 final Rect r = ai.mTmpInvalRect;
10380 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10381 // Don't call invalidate -- we don't want to internally scroll
10382 // our own bounds
10383 p.invalidateChild(this, r);
10384 }
10385 }
10386 }
10387
10388 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010389 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10390 * set any flags or handle all of the cases handled by the default invalidation methods.
10391 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10392 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10393 * walk up the hierarchy, transforming the dirty rect as necessary.
10394 *
10395 * The method also handles normal invalidation logic if display list properties are not
10396 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10397 * backup approach, to handle these cases used in the various property-setting methods.
10398 *
10399 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10400 * are not being used in this view
10401 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10402 * list properties are not being used in this view
10403 */
10404 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010405 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010406 if (invalidateParent) {
10407 invalidateParentCaches();
10408 }
10409 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010410 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010411 }
10412 invalidate(false);
10413 } else {
10414 final AttachInfo ai = mAttachInfo;
10415 final ViewParent p = mParent;
10416 if (p != null && ai != null) {
10417 final Rect r = ai.mTmpInvalRect;
10418 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10419 if (mParent instanceof ViewGroup) {
10420 ((ViewGroup) mParent).invalidateChildFast(this, r);
10421 } else {
10422 mParent.invalidateChild(this, r);
10423 }
10424 }
10425 }
10426 }
10427
10428 /**
10429 * Utility method to transform a given Rect by the current matrix of this view.
10430 */
10431 void transformRect(final Rect rect) {
10432 if (!getMatrix().isIdentity()) {
10433 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10434 boundingRect.set(rect);
10435 getMatrix().mapRect(boundingRect);
10436 rect.set((int) (boundingRect.left - 0.5f),
10437 (int) (boundingRect.top - 0.5f),
10438 (int) (boundingRect.right + 0.5f),
10439 (int) (boundingRect.bottom + 0.5f));
10440 }
10441 }
10442
10443 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010444 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010445 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10446 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010447 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10448 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010449 *
10450 * @hide
10451 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010452 protected void invalidateParentCaches() {
10453 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010454 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010455 }
10456 }
Joe Malin32736f02011-01-19 16:14:20 -080010457
Romain Guy0fd89bf2011-01-26 15:41:30 -080010458 /**
10459 * Used to indicate that the parent of this view should be invalidated. This functionality
10460 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10461 * which is necessary when various parent-managed properties of the view change, such as
10462 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10463 * an invalidation event to the parent.
10464 *
10465 * @hide
10466 */
10467 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010468 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010469 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010470 }
10471 }
10472
10473 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010474 * Indicates whether this View is opaque. An opaque View guarantees that it will
10475 * draw all the pixels overlapping its bounds using a fully opaque color.
10476 *
10477 * Subclasses of View should override this method whenever possible to indicate
10478 * whether an instance is opaque. Opaque Views are treated in a special way by
10479 * the View hierarchy, possibly allowing it to perform optimizations during
10480 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010481 *
Romain Guy24443ea2009-05-11 11:56:30 -070010482 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010483 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010484 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010485 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010486 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010487 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010488 }
10489
Adam Powell20232d02010-12-08 21:08:53 -080010490 /**
10491 * @hide
10492 */
10493 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010494 // Opaque if:
10495 // - Has a background
10496 // - Background is opaque
10497 // - Doesn't have scrollbars or scrollbars are inside overlay
10498
Philip Milne6c8ea062012-04-03 17:38:43 -070010499 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010500 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010501 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010502 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010503 }
10504
10505 final int flags = mViewFlags;
10506 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10507 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010508 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010509 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010510 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010511 }
10512 }
10513
10514 /**
10515 * @hide
10516 */
10517 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010518 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010519 }
10520
10521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 * @return A handler associated with the thread running the View. This
10523 * handler can be used to pump events in the UI events queue.
10524 */
10525 public Handler getHandler() {
10526 if (mAttachInfo != null) {
10527 return mAttachInfo.mHandler;
10528 }
10529 return null;
10530 }
10531
10532 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010533 * Gets the view root associated with the View.
10534 * @return The view root, or null if none.
10535 * @hide
10536 */
10537 public ViewRootImpl getViewRootImpl() {
10538 if (mAttachInfo != null) {
10539 return mAttachInfo.mViewRootImpl;
10540 }
10541 return null;
10542 }
10543
10544 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010545 * <p>Causes the Runnable to be added to the message queue.
10546 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010547 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 * @param action The Runnable that will be executed.
10549 *
10550 * @return Returns true if the Runnable was successfully placed in to the
10551 * message queue. Returns false on failure, usually because the
10552 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010553 *
10554 * @see #postDelayed
10555 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 */
10557 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010558 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010559 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010560 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010562 // Assume that post will succeed later
10563 ViewRootImpl.getRunQueue().post(action);
10564 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 }
10566
10567 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010568 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010570 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 * @param action The Runnable that will be executed.
10573 * @param delayMillis The delay (in milliseconds) until the Runnable
10574 * will be executed.
10575 *
10576 * @return true if the Runnable was successfully placed in to the
10577 * message queue. Returns false on failure, usually because the
10578 * looper processing the message queue is exiting. Note that a
10579 * result of true does not mean the Runnable will be processed --
10580 * if the looper is quit before the delivery time of the message
10581 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010582 *
10583 * @see #post
10584 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 */
10586 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010587 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010588 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010589 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010591 // Assume that post will succeed later
10592 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10593 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 }
10595
10596 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010597 * <p>Causes the Runnable to execute on the next animation time step.
10598 * The runnable will be run on the user interface thread.</p>
10599 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010600 * @param action The Runnable that will be executed.
10601 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010602 * @see #postOnAnimationDelayed
10603 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010604 */
10605 public void postOnAnimation(Runnable action) {
10606 final AttachInfo attachInfo = mAttachInfo;
10607 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010608 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10609 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010610 } else {
10611 // Assume that post will succeed later
10612 ViewRootImpl.getRunQueue().post(action);
10613 }
10614 }
10615
10616 /**
10617 * <p>Causes the Runnable to execute on the next animation time step,
10618 * after the specified amount of time elapses.
10619 * The runnable will be run on the user interface thread.</p>
10620 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010621 * @param action The Runnable that will be executed.
10622 * @param delayMillis The delay (in milliseconds) until the Runnable
10623 * will be executed.
10624 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010625 * @see #postOnAnimation
10626 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010627 */
10628 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10629 final AttachInfo attachInfo = mAttachInfo;
10630 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010631 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10632 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010633 } else {
10634 // Assume that post will succeed later
10635 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10636 }
10637 }
10638
10639 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010640 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010641 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 * @param action The Runnable to remove from the message handling queue
10643 *
10644 * @return true if this view could ask the Handler to remove the Runnable,
10645 * false otherwise. When the returned value is true, the Runnable
10646 * may or may not have been actually removed from the message queue
10647 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010648 *
10649 * @see #post
10650 * @see #postDelayed
10651 * @see #postOnAnimation
10652 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 */
10654 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010655 if (action != null) {
10656 final AttachInfo attachInfo = mAttachInfo;
10657 if (attachInfo != null) {
10658 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010659 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10660 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010661 } else {
10662 // Assume that post will succeed later
10663 ViewRootImpl.getRunQueue().removeCallbacks(action);
10664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 return true;
10667 }
10668
10669 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010670 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10671 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 *
Romain Guye63a4f32011-08-11 11:33:31 -070010673 * <p>This method can be invoked from outside of the UI thread
10674 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010677 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010678 */
10679 public void postInvalidate() {
10680 postInvalidateDelayed(0);
10681 }
10682
10683 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010684 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10685 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010686 *
Romain Guye63a4f32011-08-11 11:33:31 -070010687 * <p>This method can be invoked from outside of the UI thread
10688 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 *
10690 * @param left The left coordinate of the rectangle to invalidate.
10691 * @param top The top coordinate of the rectangle to invalidate.
10692 * @param right The right coordinate of the rectangle to invalidate.
10693 * @param bottom The bottom coordinate of the rectangle to invalidate.
10694 *
10695 * @see #invalidate(int, int, int, int)
10696 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010697 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 */
10699 public void postInvalidate(int left, int top, int right, int bottom) {
10700 postInvalidateDelayed(0, left, top, right, bottom);
10701 }
10702
10703 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010704 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10705 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010706 *
Romain Guye63a4f32011-08-11 11:33:31 -070010707 * <p>This method can be invoked from outside of the UI thread
10708 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010709 *
10710 * @param delayMilliseconds the duration in milliseconds to delay the
10711 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010712 *
10713 * @see #invalidate()
10714 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 */
10716 public void postInvalidateDelayed(long delayMilliseconds) {
10717 // We try only with the AttachInfo because there's no point in invalidating
10718 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010719 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010720 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010721 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 }
10723 }
10724
10725 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010726 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10727 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010728 *
Romain Guye63a4f32011-08-11 11:33:31 -070010729 * <p>This method can be invoked from outside of the UI thread
10730 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 *
10732 * @param delayMilliseconds the duration in milliseconds to delay the
10733 * invalidation by
10734 * @param left The left coordinate of the rectangle to invalidate.
10735 * @param top The top coordinate of the rectangle to invalidate.
10736 * @param right The right coordinate of the rectangle to invalidate.
10737 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010738 *
10739 * @see #invalidate(int, int, int, int)
10740 * @see #invalidate(Rect)
10741 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 */
10743 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10744 int right, int bottom) {
10745
10746 // We try only with the AttachInfo because there's no point in invalidating
10747 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010748 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010749 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010750 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 info.target = this;
10752 info.left = left;
10753 info.top = top;
10754 info.right = right;
10755 info.bottom = bottom;
10756
Jeff Browna175a5b2012-02-15 19:18:31 -080010757 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 }
10759 }
10760
10761 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010762 * <p>Cause an invalidate to happen on the next animation time step, typically the
10763 * next display frame.</p>
10764 *
10765 * <p>This method can be invoked from outside of the UI thread
10766 * only when this View is attached to a window.</p>
10767 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010768 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010769 */
10770 public void postInvalidateOnAnimation() {
10771 // We try only with the AttachInfo because there's no point in invalidating
10772 // if we are not attached to our window
10773 final AttachInfo attachInfo = mAttachInfo;
10774 if (attachInfo != null) {
10775 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10776 }
10777 }
10778
10779 /**
10780 * <p>Cause an invalidate of the specified area to happen on the next animation
10781 * time step, typically the next display frame.</p>
10782 *
10783 * <p>This method can be invoked from outside of the UI thread
10784 * only when this View is attached to a window.</p>
10785 *
10786 * @param left The left coordinate of the rectangle to invalidate.
10787 * @param top The top coordinate of the rectangle to invalidate.
10788 * @param right The right coordinate of the rectangle to invalidate.
10789 * @param bottom The bottom coordinate of the rectangle to invalidate.
10790 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010791 * @see #invalidate(int, int, int, int)
10792 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010793 */
10794 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10795 // We try only with the AttachInfo because there's no point in invalidating
10796 // if we are not attached to our window
10797 final AttachInfo attachInfo = mAttachInfo;
10798 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010799 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010800 info.target = this;
10801 info.left = left;
10802 info.top = top;
10803 info.right = right;
10804 info.bottom = bottom;
10805
10806 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10807 }
10808 }
10809
10810 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010811 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10812 * This event is sent at most once every
10813 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10814 */
10815 private void postSendViewScrolledAccessibilityEventCallback() {
10816 if (mSendViewScrolledAccessibilityEvent == null) {
10817 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10818 }
10819 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10820 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10821 postDelayed(mSendViewScrolledAccessibilityEvent,
10822 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10823 }
10824 }
10825
10826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010827 * Called by a parent to request that a child update its values for mScrollX
10828 * and mScrollY if necessary. This will typically be done if the child is
10829 * animating a scroll using a {@link android.widget.Scroller Scroller}
10830 * object.
10831 */
10832 public void computeScroll() {
10833 }
10834
10835 /**
10836 * <p>Indicate whether the horizontal edges are faded when the view is
10837 * scrolled horizontally.</p>
10838 *
10839 * @return true if the horizontal edges should are faded on scroll, false
10840 * otherwise
10841 *
10842 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010843 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010844 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 */
10846 public boolean isHorizontalFadingEdgeEnabled() {
10847 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10848 }
10849
10850 /**
10851 * <p>Define whether the horizontal edges should be faded when this view
10852 * is scrolled horizontally.</p>
10853 *
10854 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10855 * be faded when the view is scrolled
10856 * horizontally
10857 *
10858 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010859 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010860 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010861 */
10862 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10863 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10864 if (horizontalFadingEdgeEnabled) {
10865 initScrollCache();
10866 }
10867
10868 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10869 }
10870 }
10871
10872 /**
10873 * <p>Indicate whether the vertical edges are faded when the view is
10874 * scrolled horizontally.</p>
10875 *
10876 * @return true if the vertical edges should are faded on scroll, false
10877 * otherwise
10878 *
10879 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010880 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010881 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010882 */
10883 public boolean isVerticalFadingEdgeEnabled() {
10884 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10885 }
10886
10887 /**
10888 * <p>Define whether the vertical edges should be faded when this view
10889 * is scrolled vertically.</p>
10890 *
10891 * @param verticalFadingEdgeEnabled true if the vertical edges should
10892 * be faded when the view is scrolled
10893 * vertically
10894 *
10895 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010896 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010897 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010898 */
10899 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10900 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10901 if (verticalFadingEdgeEnabled) {
10902 initScrollCache();
10903 }
10904
10905 mViewFlags ^= FADING_EDGE_VERTICAL;
10906 }
10907 }
10908
10909 /**
10910 * Returns the strength, or intensity, of the top faded edge. The strength is
10911 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10912 * returns 0.0 or 1.0 but no value in between.
10913 *
10914 * Subclasses should override this method to provide a smoother fade transition
10915 * when scrolling occurs.
10916 *
10917 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10918 */
10919 protected float getTopFadingEdgeStrength() {
10920 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10921 }
10922
10923 /**
10924 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10925 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10926 * returns 0.0 or 1.0 but no value in between.
10927 *
10928 * Subclasses should override this method to provide a smoother fade transition
10929 * when scrolling occurs.
10930 *
10931 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10932 */
10933 protected float getBottomFadingEdgeStrength() {
10934 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10935 computeVerticalScrollRange() ? 1.0f : 0.0f;
10936 }
10937
10938 /**
10939 * Returns the strength, or intensity, of the left faded edge. The strength is
10940 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10941 * returns 0.0 or 1.0 but no value in between.
10942 *
10943 * Subclasses should override this method to provide a smoother fade transition
10944 * when scrolling occurs.
10945 *
10946 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10947 */
10948 protected float getLeftFadingEdgeStrength() {
10949 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10950 }
10951
10952 /**
10953 * Returns the strength, or intensity, of the right faded edge. The strength is
10954 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10955 * returns 0.0 or 1.0 but no value in between.
10956 *
10957 * Subclasses should override this method to provide a smoother fade transition
10958 * when scrolling occurs.
10959 *
10960 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10961 */
10962 protected float getRightFadingEdgeStrength() {
10963 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10964 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10965 }
10966
10967 /**
10968 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10969 * scrollbar is not drawn by default.</p>
10970 *
10971 * @return true if the horizontal scrollbar should be painted, false
10972 * otherwise
10973 *
10974 * @see #setHorizontalScrollBarEnabled(boolean)
10975 */
10976 public boolean isHorizontalScrollBarEnabled() {
10977 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10978 }
10979
10980 /**
10981 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10982 * scrollbar is not drawn by default.</p>
10983 *
10984 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10985 * be painted
10986 *
10987 * @see #isHorizontalScrollBarEnabled()
10988 */
10989 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10990 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10991 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010992 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010993 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 }
10995 }
10996
10997 /**
10998 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10999 * scrollbar is not drawn by default.</p>
11000 *
11001 * @return true if the vertical scrollbar should be painted, false
11002 * otherwise
11003 *
11004 * @see #setVerticalScrollBarEnabled(boolean)
11005 */
11006 public boolean isVerticalScrollBarEnabled() {
11007 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11008 }
11009
11010 /**
11011 * <p>Define whether the vertical scrollbar should be drawn or not. The
11012 * scrollbar is not drawn by default.</p>
11013 *
11014 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11015 * be painted
11016 *
11017 * @see #isVerticalScrollBarEnabled()
11018 */
11019 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11020 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11021 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011022 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011023 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 }
11025 }
11026
Adam Powell20232d02010-12-08 21:08:53 -080011027 /**
11028 * @hide
11029 */
11030 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011031 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011032 }
Joe Malin32736f02011-01-19 16:14:20 -080011033
Mike Cleronfe81d382009-09-28 14:22:16 -070011034 /**
11035 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011036 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011037 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011038 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011039 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011040 */
11041 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11042 initScrollCache();
11043 final ScrollabilityCache scrollabilityCache = mScrollCache;
11044 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011045 if (fadeScrollbars) {
11046 scrollabilityCache.state = ScrollabilityCache.OFF;
11047 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011048 scrollabilityCache.state = ScrollabilityCache.ON;
11049 }
11050 }
Joe Malin32736f02011-01-19 16:14:20 -080011051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052 /**
Joe Malin32736f02011-01-19 16:14:20 -080011053 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011054 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011055 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011056 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011057 *
11058 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011059 */
11060 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011061 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011062 }
Joe Malin32736f02011-01-19 16:14:20 -080011063
Mike Cleron52f0a642009-09-28 18:21:37 -070011064 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011065 *
11066 * Returns the delay before scrollbars fade.
11067 *
11068 * @return the delay before scrollbars fade
11069 *
11070 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11071 */
11072 public int getScrollBarDefaultDelayBeforeFade() {
11073 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11074 mScrollCache.scrollBarDefaultDelayBeforeFade;
11075 }
11076
11077 /**
11078 * Define the delay before scrollbars fade.
11079 *
11080 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11081 *
11082 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11083 */
11084 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11085 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11086 }
11087
11088 /**
11089 *
11090 * Returns the scrollbar fade duration.
11091 *
11092 * @return the scrollbar fade duration
11093 *
11094 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11095 */
11096 public int getScrollBarFadeDuration() {
11097 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11098 mScrollCache.scrollBarFadeDuration;
11099 }
11100
11101 /**
11102 * Define the scrollbar fade duration.
11103 *
11104 * @param scrollBarFadeDuration - the scrollbar fade duration
11105 *
11106 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11107 */
11108 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11109 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11110 }
11111
11112 /**
11113 *
11114 * Returns the scrollbar size.
11115 *
11116 * @return the scrollbar size
11117 *
11118 * @attr ref android.R.styleable#View_scrollbarSize
11119 */
11120 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011121 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011122 mScrollCache.scrollBarSize;
11123 }
11124
11125 /**
11126 * Define the scrollbar size.
11127 *
11128 * @param scrollBarSize - the scrollbar size
11129 *
11130 * @attr ref android.R.styleable#View_scrollbarSize
11131 */
11132 public void setScrollBarSize(int scrollBarSize) {
11133 getScrollCache().scrollBarSize = scrollBarSize;
11134 }
11135
11136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11138 * inset. When inset, they add to the padding of the view. And the scrollbars
11139 * can be drawn inside the padding area or on the edge of the view. For example,
11140 * if a view has a background drawable and you want to draw the scrollbars
11141 * inside the padding specified by the drawable, you can use
11142 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11143 * appear at the edge of the view, ignoring the padding, then you can use
11144 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11145 * @param style the style of the scrollbars. Should be one of
11146 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11147 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11148 * @see #SCROLLBARS_INSIDE_OVERLAY
11149 * @see #SCROLLBARS_INSIDE_INSET
11150 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11151 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011152 *
11153 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011154 */
11155 public void setScrollBarStyle(int style) {
11156 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11157 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011158 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011159 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 }
11161 }
11162
11163 /**
11164 * <p>Returns the current scrollbar style.</p>
11165 * @return the current scrollbar style
11166 * @see #SCROLLBARS_INSIDE_OVERLAY
11167 * @see #SCROLLBARS_INSIDE_INSET
11168 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11169 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011170 *
11171 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011173 @ViewDebug.ExportedProperty(mapping = {
11174 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11175 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11176 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11177 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11178 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011179 public int getScrollBarStyle() {
11180 return mViewFlags & SCROLLBARS_STYLE_MASK;
11181 }
11182
11183 /**
11184 * <p>Compute the horizontal range that the horizontal scrollbar
11185 * represents.</p>
11186 *
11187 * <p>The range is expressed in arbitrary units that must be the same as the
11188 * units used by {@link #computeHorizontalScrollExtent()} and
11189 * {@link #computeHorizontalScrollOffset()}.</p>
11190 *
11191 * <p>The default range is the drawing width of this view.</p>
11192 *
11193 * @return the total horizontal range represented by the horizontal
11194 * scrollbar
11195 *
11196 * @see #computeHorizontalScrollExtent()
11197 * @see #computeHorizontalScrollOffset()
11198 * @see android.widget.ScrollBarDrawable
11199 */
11200 protected int computeHorizontalScrollRange() {
11201 return getWidth();
11202 }
11203
11204 /**
11205 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11206 * within the horizontal range. This value is used to compute the position
11207 * of the thumb within the scrollbar's track.</p>
11208 *
11209 * <p>The range is expressed in arbitrary units that must be the same as the
11210 * units used by {@link #computeHorizontalScrollRange()} and
11211 * {@link #computeHorizontalScrollExtent()}.</p>
11212 *
11213 * <p>The default offset is the scroll offset of this view.</p>
11214 *
11215 * @return the horizontal offset of the scrollbar's thumb
11216 *
11217 * @see #computeHorizontalScrollRange()
11218 * @see #computeHorizontalScrollExtent()
11219 * @see android.widget.ScrollBarDrawable
11220 */
11221 protected int computeHorizontalScrollOffset() {
11222 return mScrollX;
11223 }
11224
11225 /**
11226 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11227 * within the horizontal range. This value is used to compute the length
11228 * of the thumb within the scrollbar's track.</p>
11229 *
11230 * <p>The range is expressed in arbitrary units that must be the same as the
11231 * units used by {@link #computeHorizontalScrollRange()} and
11232 * {@link #computeHorizontalScrollOffset()}.</p>
11233 *
11234 * <p>The default extent is the drawing width of this view.</p>
11235 *
11236 * @return the horizontal extent of the scrollbar's thumb
11237 *
11238 * @see #computeHorizontalScrollRange()
11239 * @see #computeHorizontalScrollOffset()
11240 * @see android.widget.ScrollBarDrawable
11241 */
11242 protected int computeHorizontalScrollExtent() {
11243 return getWidth();
11244 }
11245
11246 /**
11247 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11248 *
11249 * <p>The range is expressed in arbitrary units that must be the same as the
11250 * units used by {@link #computeVerticalScrollExtent()} and
11251 * {@link #computeVerticalScrollOffset()}.</p>
11252 *
11253 * @return the total vertical range represented by the vertical scrollbar
11254 *
11255 * <p>The default range is the drawing height of this view.</p>
11256 *
11257 * @see #computeVerticalScrollExtent()
11258 * @see #computeVerticalScrollOffset()
11259 * @see android.widget.ScrollBarDrawable
11260 */
11261 protected int computeVerticalScrollRange() {
11262 return getHeight();
11263 }
11264
11265 /**
11266 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11267 * within the horizontal range. This value is used to compute the position
11268 * of the thumb within the scrollbar's track.</p>
11269 *
11270 * <p>The range is expressed in arbitrary units that must be the same as the
11271 * units used by {@link #computeVerticalScrollRange()} and
11272 * {@link #computeVerticalScrollExtent()}.</p>
11273 *
11274 * <p>The default offset is the scroll offset of this view.</p>
11275 *
11276 * @return the vertical offset of the scrollbar's thumb
11277 *
11278 * @see #computeVerticalScrollRange()
11279 * @see #computeVerticalScrollExtent()
11280 * @see android.widget.ScrollBarDrawable
11281 */
11282 protected int computeVerticalScrollOffset() {
11283 return mScrollY;
11284 }
11285
11286 /**
11287 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11288 * within the vertical range. This value is used to compute the length
11289 * of the thumb within the scrollbar's track.</p>
11290 *
11291 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011292 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011293 * {@link #computeVerticalScrollOffset()}.</p>
11294 *
11295 * <p>The default extent is the drawing height of this view.</p>
11296 *
11297 * @return the vertical extent of the scrollbar's thumb
11298 *
11299 * @see #computeVerticalScrollRange()
11300 * @see #computeVerticalScrollOffset()
11301 * @see android.widget.ScrollBarDrawable
11302 */
11303 protected int computeVerticalScrollExtent() {
11304 return getHeight();
11305 }
11306
11307 /**
Adam Powell69159442011-06-13 17:53:06 -070011308 * Check if this view can be scrolled horizontally in a certain direction.
11309 *
11310 * @param direction Negative to check scrolling left, positive to check scrolling right.
11311 * @return true if this view can be scrolled in the specified direction, false otherwise.
11312 */
11313 public boolean canScrollHorizontally(int direction) {
11314 final int offset = computeHorizontalScrollOffset();
11315 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11316 if (range == 0) return false;
11317 if (direction < 0) {
11318 return offset > 0;
11319 } else {
11320 return offset < range - 1;
11321 }
11322 }
11323
11324 /**
11325 * Check if this view can be scrolled vertically in a certain direction.
11326 *
11327 * @param direction Negative to check scrolling up, positive to check scrolling down.
11328 * @return true if this view can be scrolled in the specified direction, false otherwise.
11329 */
11330 public boolean canScrollVertically(int direction) {
11331 final int offset = computeVerticalScrollOffset();
11332 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11333 if (range == 0) return false;
11334 if (direction < 0) {
11335 return offset > 0;
11336 } else {
11337 return offset < range - 1;
11338 }
11339 }
11340
11341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011342 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11343 * scrollbars are painted only if they have been awakened first.</p>
11344 *
11345 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011346 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011347 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011348 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011349 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011350 // scrollbars are drawn only when the animation is running
11351 final ScrollabilityCache cache = mScrollCache;
11352 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011353
Mike Cleronf116bf82009-09-27 19:14:12 -070011354 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011355
Mike Cleronf116bf82009-09-27 19:14:12 -070011356 if (state == ScrollabilityCache.OFF) {
11357 return;
11358 }
Joe Malin32736f02011-01-19 16:14:20 -080011359
Mike Cleronf116bf82009-09-27 19:14:12 -070011360 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011361
Mike Cleronf116bf82009-09-27 19:14:12 -070011362 if (state == ScrollabilityCache.FADING) {
11363 // We're fading -- get our fade interpolation
11364 if (cache.interpolatorValues == null) {
11365 cache.interpolatorValues = new float[1];
11366 }
Joe Malin32736f02011-01-19 16:14:20 -080011367
Mike Cleronf116bf82009-09-27 19:14:12 -070011368 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011369
Mike Cleronf116bf82009-09-27 19:14:12 -070011370 // Stops the animation if we're done
11371 if (cache.scrollBarInterpolator.timeToValues(values) ==
11372 Interpolator.Result.FREEZE_END) {
11373 cache.state = ScrollabilityCache.OFF;
11374 } else {
11375 cache.scrollBar.setAlpha(Math.round(values[0]));
11376 }
Joe Malin32736f02011-01-19 16:14:20 -080011377
11378 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011379 // drawing. We only want this when we're fading so that
11380 // we prevent excessive redraws
11381 invalidate = true;
11382 } else {
11383 // We're just on -- but we may have been fading before so
11384 // reset alpha
11385 cache.scrollBar.setAlpha(255);
11386 }
11387
Joe Malin32736f02011-01-19 16:14:20 -080011388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011389 final int viewFlags = mViewFlags;
11390
11391 final boolean drawHorizontalScrollBar =
11392 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11393 final boolean drawVerticalScrollBar =
11394 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11395 && !isVerticalScrollBarHidden();
11396
11397 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11398 final int width = mRight - mLeft;
11399 final int height = mBottom - mTop;
11400
11401 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011402
Mike Reede8853fc2009-09-04 14:01:48 -040011403 final int scrollX = mScrollX;
11404 final int scrollY = mScrollY;
11405 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11406
Mike Cleronf116bf82009-09-27 19:14:12 -070011407 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011410 int size = scrollBar.getSize(false);
11411 if (size <= 0) {
11412 size = cache.scrollBarSize;
11413 }
11414
Mike Cleronf116bf82009-09-27 19:14:12 -070011415 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011416 computeHorizontalScrollOffset(),
11417 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011418 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011419 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011420 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011421 left = scrollX + (mPaddingLeft & inside);
11422 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11423 bottom = top + size;
11424 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11425 if (invalidate) {
11426 invalidate(left, top, right, bottom);
11427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428 }
11429
11430 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011431 int size = scrollBar.getSize(true);
11432 if (size <= 0) {
11433 size = cache.scrollBarSize;
11434 }
11435
Mike Reede8853fc2009-09-04 14:01:48 -040011436 scrollBar.setParameters(computeVerticalScrollRange(),
11437 computeVerticalScrollOffset(),
11438 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011439 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11440 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11441 verticalScrollbarPosition = isLayoutRtl() ?
11442 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11443 }
11444 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011445 default:
Adam Powell20232d02010-12-08 21:08:53 -080011446 case SCROLLBAR_POSITION_RIGHT:
11447 left = scrollX + width - size - (mUserPaddingRight & inside);
11448 break;
11449 case SCROLLBAR_POSITION_LEFT:
11450 left = scrollX + (mUserPaddingLeft & inside);
11451 break;
11452 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011453 top = scrollY + (mPaddingTop & inside);
11454 right = left + size;
11455 bottom = scrollY + height - (mUserPaddingBottom & inside);
11456 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11457 if (invalidate) {
11458 invalidate(left, top, right, bottom);
11459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011460 }
11461 }
11462 }
11463 }
Romain Guy8506ab42009-06-11 17:35:47 -070011464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011465 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011466 * 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 -080011467 * FastScroller is visible.
11468 * @return whether to temporarily hide the vertical scrollbar
11469 * @hide
11470 */
11471 protected boolean isVerticalScrollBarHidden() {
11472 return false;
11473 }
11474
11475 /**
11476 * <p>Draw the horizontal scrollbar if
11477 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11478 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 * @param canvas the canvas on which to draw the scrollbar
11480 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011481 *
11482 * @see #isHorizontalScrollBarEnabled()
11483 * @see #computeHorizontalScrollRange()
11484 * @see #computeHorizontalScrollExtent()
11485 * @see #computeHorizontalScrollOffset()
11486 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011487 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011488 */
Romain Guy8fb95422010-08-17 18:38:51 -070011489 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11490 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011491 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011492 scrollBar.draw(canvas);
11493 }
Mike Reede8853fc2009-09-04 14:01:48 -040011494
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011496 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11497 * returns true.</p>
11498 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499 * @param canvas the canvas on which to draw the scrollbar
11500 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011501 *
11502 * @see #isVerticalScrollBarEnabled()
11503 * @see #computeVerticalScrollRange()
11504 * @see #computeVerticalScrollExtent()
11505 * @see #computeVerticalScrollOffset()
11506 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011507 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508 */
Romain Guy8fb95422010-08-17 18:38:51 -070011509 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11510 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011511 scrollBar.setBounds(l, t, r, b);
11512 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 }
11514
11515 /**
11516 * Implement this to do your drawing.
11517 *
11518 * @param canvas the canvas on which the background will be drawn
11519 */
11520 protected void onDraw(Canvas canvas) {
11521 }
11522
11523 /*
11524 * Caller is responsible for calling requestLayout if necessary.
11525 * (This allows addViewInLayout to not request a new layout.)
11526 */
11527 void assignParent(ViewParent parent) {
11528 if (mParent == null) {
11529 mParent = parent;
11530 } else if (parent == null) {
11531 mParent = null;
11532 } else {
11533 throw new RuntimeException("view " + this + " being added, but"
11534 + " it already has a parent");
11535 }
11536 }
11537
11538 /**
11539 * This is called when the view is attached to a window. At this point it
11540 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011541 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11542 * however it may be called any time before the first onDraw -- including
11543 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011544 *
11545 * @see #onDetachedFromWindow()
11546 */
11547 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011548 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549 mParent.requestTransparentRegion(this);
11550 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011551
Dianne Hackborn4702a852012-08-17 15:18:29 -070011552 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011553 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011554 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011555 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011556
Chet Haasea9b61ac2010-12-20 07:40:25 -080011557 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011558
Svetoslav Ganov42138042012-03-20 11:51:39 -070011559 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011560 if (isFocused()) {
11561 InputMethodManager imm = InputMethodManager.peekInstance();
11562 imm.focusIn(this);
11563 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011564
Romain Guyfbb93fa2012-12-03 18:50:35 -080011565 if (mDisplayList != null) {
11566 mDisplayList.setDirty(false);
Romain Guy2a0f2282012-05-08 14:43:12 -070011567 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011568 }
Cibu Johny86666632010-02-22 13:01:02 -080011569
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011570 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011571 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011572 *
11573 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011574 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011575 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011576 if (!needRtlPropertiesResolution()) return;
11577
11578 // Order is important here: LayoutDirection MUST be resolved first
11579 if (!isLayoutDirectionResolved()) {
11580 resolveLayoutDirection();
11581 resolveLayoutParams();
11582 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011583 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011584 if (!isTextDirectionResolved()) {
11585 resolveTextDirection();
11586 }
11587 if (!isTextAlignmentResolved()) {
11588 resolveTextAlignment();
11589 }
11590 if (!isPaddingResolved()) {
11591 resolvePadding();
11592 }
11593 if (!isDrawablesResolved()) {
11594 resolveDrawables();
11595 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011596 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011597 }
11598
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011599 /**
11600 * Reset resolution of all RTL related properties.
11601 *
11602 * @hide
11603 */
11604 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011605 resetResolvedLayoutDirection();
11606 resetResolvedTextDirection();
11607 resetResolvedTextAlignment();
11608 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011609 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011610 }
11611
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011612 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011613 * @see #onScreenStateChanged(int)
11614 */
11615 void dispatchScreenStateChanged(int screenState) {
11616 onScreenStateChanged(screenState);
11617 }
11618
11619 /**
11620 * This method is called whenever the state of the screen this view is
11621 * attached to changes. A state change will usually occurs when the screen
11622 * turns on or off (whether it happens automatically or the user does it
11623 * manually.)
11624 *
11625 * @param screenState The new state of the screen. Can be either
11626 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11627 */
11628 public void onScreenStateChanged(int screenState) {
11629 }
11630
11631 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011632 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11633 */
11634 private boolean hasRtlSupport() {
11635 return mContext.getApplicationInfo().hasRtlSupport();
11636 }
11637
11638 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011639 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11640 * RTL not supported)
11641 */
11642 private boolean isRtlCompatibilityMode() {
11643 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11644 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11645 }
11646
11647 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011648 * @return true if RTL properties need resolution.
11649 */
11650 private boolean needRtlPropertiesResolution() {
11651 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11652 }
11653
11654 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011655 * Called when any RTL property (layout direction or text direction or text alignment) has
11656 * been changed.
11657 *
11658 * Subclasses need to override this method to take care of cached information that depends on the
11659 * resolved layout direction, or to inform child views that inherit their layout direction.
11660 *
11661 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011662 *
11663 * @param layoutDirection the direction of the layout
11664 *
11665 * @see #LAYOUT_DIRECTION_LTR
11666 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011667 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011668 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011669 }
11670
11671 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011672 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11673 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011674 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011675 * @return true if resolution has been done, false otherwise.
11676 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011677 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011678 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011679 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011680 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011681 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011682
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011683 if (hasRtlSupport()) {
11684 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011685 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11686 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011687 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011688 // We cannot resolve yet. LTR is by default and let the resolution happen again
11689 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011690 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011691
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011692 View parent = ((View) mParent);
11693 // Parent has not yet resolved, LTR is still the default
11694 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011695
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011696 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011697 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011698 }
11699 break;
11700 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011701 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011702 break;
11703 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011704 if((LAYOUT_DIRECTION_RTL ==
11705 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011706 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011707 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011708 break;
11709 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011710 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011711 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011712 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011713
11714 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011715 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011716 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011717 }
11718
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011719 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011720 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011721 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011722 * @return true if layout direction resolution can be done otherwise return false.
11723 *
11724 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011725 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011726 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011727 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011728 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011729 return (mParent != null) && (mParent instanceof ViewGroup) &&
11730 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011731 default:
11732 return true;
11733 }
11734 }
11735
11736 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011737 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11738 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011739 *
11740 * @hide
11741 */
11742 public void resetResolvedLayoutDirection() {
11743 // Reset the current resolved bits
11744 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011745 }
11746
11747 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011748 * @return true if the layout direction is inherited.
11749 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011750 * @hide
11751 */
11752 public boolean isLayoutDirectionInherited() {
11753 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11754 }
11755
11756 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011757 * @return true if layout direction has been resolved.
11758 */
11759 private boolean isLayoutDirectionResolved() {
11760 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11761 }
11762
11763 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011764 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011765 *
11766 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011767 */
11768 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011769 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011770 }
11771
11772 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011773 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011774 *
11775 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011776 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011777 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011778 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011779 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11780 // If start / end padding are defined, they will be resolved (hence overriding) to
11781 // left / right or right / left depending on the resolved layout direction.
11782 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011783 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011784 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011785 mUserPaddingLeft = mUserPaddingLeftInitial;
11786 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011787 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011788 switch (resolvedLayoutDirection) {
11789 case LAYOUT_DIRECTION_RTL:
11790 if (mUserPaddingStart != UNDEFINED_PADDING) {
11791 mUserPaddingRight = mUserPaddingStart;
11792 }
11793 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11794 mUserPaddingLeft = mUserPaddingEnd;
11795 }
11796 break;
11797 case LAYOUT_DIRECTION_LTR:
11798 default:
11799 if (mUserPaddingStart != UNDEFINED_PADDING) {
11800 mUserPaddingLeft = mUserPaddingStart;
11801 }
11802 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11803 mUserPaddingRight = mUserPaddingEnd;
11804 }
11805 }
11806
11807 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11808
11809 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11810 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011811 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011812 }
11813
Dianne Hackborn4702a852012-08-17 15:18:29 -070011814 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011815 }
11816
11817 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011818 * Reset the resolved layout direction.
11819 *
11820 * @hide
11821 */
11822 public void resetResolvedPadding() {
11823 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11824 }
11825
11826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011827 * This is called when the view is detached from a window. At this point it
11828 * no longer has a surface for drawing.
11829 *
11830 * @see #onAttachedToWindow()
11831 */
11832 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011833 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011834
Romain Guya440b002010-02-24 15:57:54 -080011835 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011836 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011837 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011838 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011840 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011841
Romain Guya998dff2012-03-23 18:58:36 -070011842 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011843
11844 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011845 if (mDisplayList != null) {
Romain Guyfbb93fa2012-12-03 18:50:35 -080011846 mDisplayList.setDirty(true);
Romain Guy2a0f2282012-05-08 14:43:12 -070011847 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011848 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011849 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011850 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011851 // Should never happen
11852 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011853 }
11854
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011855 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011856
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011857 resetRtlProperties();
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011858 onRtlPropertiesChanged(LAYOUT_DIRECTION_DEFAULT);
Svetoslav Ganov42138042012-03-20 11:51:39 -070011859 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 }
11861
11862 /**
11863 * @return The number of times this view has been attached to a window
11864 */
11865 protected int getWindowAttachCount() {
11866 return mWindowAttachCount;
11867 }
11868
11869 /**
11870 * Retrieve a unique token identifying the window this view is attached to.
11871 * @return Return the window's token for use in
11872 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11873 */
11874 public IBinder getWindowToken() {
11875 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11876 }
11877
11878 /**
11879 * Retrieve a unique token identifying the top-level "real" window of
11880 * the window that this view is attached to. That is, this is like
11881 * {@link #getWindowToken}, except if the window this view in is a panel
11882 * window (attached to another containing window), then the token of
11883 * the containing window is returned instead.
11884 *
11885 * @return Returns the associated window token, either
11886 * {@link #getWindowToken()} or the containing window's token.
11887 */
11888 public IBinder getApplicationWindowToken() {
11889 AttachInfo ai = mAttachInfo;
11890 if (ai != null) {
11891 IBinder appWindowToken = ai.mPanelParentWindowToken;
11892 if (appWindowToken == null) {
11893 appWindowToken = ai.mWindowToken;
11894 }
11895 return appWindowToken;
11896 }
11897 return null;
11898 }
11899
11900 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011901 * Gets the logical display to which the view's window has been attached.
11902 *
11903 * @return The logical display, or null if the view is not currently attached to a window.
11904 */
11905 public Display getDisplay() {
11906 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11907 }
11908
11909 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 * Retrieve private session object this view hierarchy is using to
11911 * communicate with the window manager.
11912 * @return the session object to communicate with the window manager
11913 */
11914 /*package*/ IWindowSession getWindowSession() {
11915 return mAttachInfo != null ? mAttachInfo.mSession : null;
11916 }
11917
11918 /**
11919 * @param info the {@link android.view.View.AttachInfo} to associated with
11920 * this view
11921 */
11922 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11923 //System.out.println("Attached! " + this);
11924 mAttachInfo = info;
11925 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011926 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011927 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011928 if (mFloatingTreeObserver != null) {
11929 info.mTreeObserver.merge(mFloatingTreeObserver);
11930 mFloatingTreeObserver = null;
11931 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011932 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011933 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011934 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011936 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011938
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011939 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011940 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011941 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011942 if (listeners != null && listeners.size() > 0) {
11943 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11944 // perform the dispatching. The iterator is a safe guard against listeners that
11945 // could mutate the list by calling the various add/remove methods. This prevents
11946 // the array from being modified while we iterate it.
11947 for (OnAttachStateChangeListener listener : listeners) {
11948 listener.onViewAttachedToWindow(this);
11949 }
11950 }
11951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011952 int vis = info.mWindowVisibility;
11953 if (vis != GONE) {
11954 onWindowVisibilityChanged(vis);
11955 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011956 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011957 // If nobody has evaluated the drawable state yet, then do it now.
11958 refreshDrawableState();
11959 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011960 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011961 }
11962
11963 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011964 AttachInfo info = mAttachInfo;
11965 if (info != null) {
11966 int vis = info.mWindowVisibility;
11967 if (vis != GONE) {
11968 onWindowVisibilityChanged(GONE);
11969 }
11970 }
11971
11972 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011973
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011974 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011975 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011976 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011977 if (listeners != null && listeners.size() > 0) {
11978 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11979 // perform the dispatching. The iterator is a safe guard against listeners that
11980 // could mutate the list by calling the various add/remove methods. This prevents
11981 // the array from being modified while we iterate it.
11982 for (OnAttachStateChangeListener listener : listeners) {
11983 listener.onViewDetachedFromWindow(this);
11984 }
11985 }
11986
Dianne Hackborn4702a852012-08-17 15:18:29 -070011987 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011989 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011990 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992 mAttachInfo = null;
11993 }
11994
11995 /**
11996 * Store this view hierarchy's frozen state into the given container.
11997 *
11998 * @param container The SparseArray in which to save the view's state.
11999 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012000 * @see #restoreHierarchyState(android.util.SparseArray)
12001 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12002 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003 */
12004 public void saveHierarchyState(SparseArray<Parcelable> container) {
12005 dispatchSaveInstanceState(container);
12006 }
12007
12008 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012009 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12010 * this view and its children. May be overridden to modify how freezing happens to a
12011 * 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 -080012012 *
12013 * @param container The SparseArray in which to save the view's state.
12014 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012015 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12016 * @see #saveHierarchyState(android.util.SparseArray)
12017 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 */
12019 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12020 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012021 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012022 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012023 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 throw new IllegalStateException(
12025 "Derived class did not call super.onSaveInstanceState()");
12026 }
12027 if (state != null) {
12028 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12029 // + ": " + state);
12030 container.put(mID, state);
12031 }
12032 }
12033 }
12034
12035 /**
12036 * Hook allowing a view to generate a representation of its internal state
12037 * that can later be used to create a new instance with that same state.
12038 * This state should only contain information that is not persistent or can
12039 * not be reconstructed later. For example, you will never store your
12040 * current position on screen because that will be computed again when a
12041 * new instance of the view is placed in its view hierarchy.
12042 * <p>
12043 * Some examples of things you may store here: the current cursor position
12044 * in a text view (but usually not the text itself since that is stored in a
12045 * content provider or other persistent storage), the currently selected
12046 * item in a list view.
12047 *
12048 * @return Returns a Parcelable object containing the view's current dynamic
12049 * state, or null if there is nothing interesting to save. The
12050 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012051 * @see #onRestoreInstanceState(android.os.Parcelable)
12052 * @see #saveHierarchyState(android.util.SparseArray)
12053 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 * @see #setSaveEnabled(boolean)
12055 */
12056 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012057 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012058 return BaseSavedState.EMPTY_STATE;
12059 }
12060
12061 /**
12062 * Restore this view hierarchy's frozen state from the given container.
12063 *
12064 * @param container The SparseArray which holds previously frozen states.
12065 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012066 * @see #saveHierarchyState(android.util.SparseArray)
12067 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12068 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 */
12070 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12071 dispatchRestoreInstanceState(container);
12072 }
12073
12074 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012075 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12076 * state for this view and its children. May be overridden to modify how restoring
12077 * happens to a view's children; for example, some views may want to not store state
12078 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012079 *
12080 * @param container The SparseArray which holds previously saved state.
12081 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012082 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12083 * @see #restoreHierarchyState(android.util.SparseArray)
12084 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012085 */
12086 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12087 if (mID != NO_ID) {
12088 Parcelable state = container.get(mID);
12089 if (state != null) {
12090 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12091 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012092 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012093 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012094 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095 throw new IllegalStateException(
12096 "Derived class did not call super.onRestoreInstanceState()");
12097 }
12098 }
12099 }
12100 }
12101
12102 /**
12103 * Hook allowing a view to re-apply a representation of its internal state that had previously
12104 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12105 * null state.
12106 *
12107 * @param state The frozen state that had previously been returned by
12108 * {@link #onSaveInstanceState}.
12109 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012110 * @see #onSaveInstanceState()
12111 * @see #restoreHierarchyState(android.util.SparseArray)
12112 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 */
12114 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012115 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012116 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012117 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12118 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012119 + "when two views of different type have the same id in the same hierarchy. "
12120 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012121 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 }
12123 }
12124
12125 /**
12126 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12127 *
12128 * @return the drawing start time in milliseconds
12129 */
12130 public long getDrawingTime() {
12131 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12132 }
12133
12134 /**
12135 * <p>Enables or disables the duplication of the parent's state into this view. When
12136 * duplication is enabled, this view gets its drawable state from its parent rather
12137 * than from its own internal properties.</p>
12138 *
12139 * <p>Note: in the current implementation, setting this property to true after the
12140 * view was added to a ViewGroup might have no effect at all. This property should
12141 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12142 *
12143 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12144 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012145 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012146 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12147 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012148 *
12149 * @param enabled True to enable duplication of the parent's drawable state, false
12150 * to disable it.
12151 *
12152 * @see #getDrawableState()
12153 * @see #isDuplicateParentStateEnabled()
12154 */
12155 public void setDuplicateParentStateEnabled(boolean enabled) {
12156 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12157 }
12158
12159 /**
12160 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12161 *
12162 * @return True if this view's drawable state is duplicated from the parent,
12163 * false otherwise
12164 *
12165 * @see #getDrawableState()
12166 * @see #setDuplicateParentStateEnabled(boolean)
12167 */
12168 public boolean isDuplicateParentStateEnabled() {
12169 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12170 }
12171
12172 /**
Romain Guy171c5922011-01-06 10:04:23 -080012173 * <p>Specifies the type of layer backing this view. The layer can be
12174 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12175 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012176 *
Romain Guy171c5922011-01-06 10:04:23 -080012177 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12178 * instance that controls how the layer is composed on screen. The following
12179 * properties of the paint are taken into account when composing the layer:</p>
12180 * <ul>
12181 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12182 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12183 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12184 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012185 *
Romain Guy171c5922011-01-06 10:04:23 -080012186 * <p>If this view has an alpha value set to < 1.0 by calling
12187 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12188 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12189 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012190 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012191 *
Romain Guy171c5922011-01-06 10:04:23 -080012192 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12193 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12194 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012195 *
Chet Haased15ebf22012-09-05 11:40:29 -070012196 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012197 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12198 * {@link #LAYER_TYPE_HARDWARE}
12199 * @param paint The paint used to compose the layer. This argument is optional
12200 * and can be null. It is ignored when the layer type is
12201 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012202 *
12203 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012204 * @see #LAYER_TYPE_NONE
12205 * @see #LAYER_TYPE_SOFTWARE
12206 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012207 * @see #setAlpha(float)
12208 *
Romain Guy171c5922011-01-06 10:04:23 -080012209 * @attr ref android.R.styleable#View_layerType
12210 */
12211 public void setLayerType(int layerType, Paint paint) {
12212 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012213 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012214 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12215 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012216
Romain Guyd6cd5722011-01-17 14:42:41 -080012217 if (layerType == mLayerType) {
12218 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12219 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012220 invalidateParentCaches();
12221 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012222 }
12223 return;
12224 }
Romain Guy171c5922011-01-06 10:04:23 -080012225
12226 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012227 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012228 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012229 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012230 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012231 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012232 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012233 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012234 default:
12235 break;
Romain Guy171c5922011-01-06 10:04:23 -080012236 }
12237
12238 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012239 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12240 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12241 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012242
Romain Guy0fd89bf2011-01-26 15:41:30 -080012243 invalidateParentCaches();
12244 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012245 }
12246
12247 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012248 * Updates the {@link Paint} object used with the current layer (used only if the current
12249 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12250 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12251 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12252 * ensure that the view gets redrawn immediately.
12253 *
12254 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12255 * instance that controls how the layer is composed on screen. The following
12256 * properties of the paint are taken into account when composing the layer:</p>
12257 * <ul>
12258 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12259 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12260 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12261 * </ul>
12262 *
12263 * <p>If this view has an alpha value set to < 1.0 by calling
12264 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12265 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12266 * equivalent to setting a hardware layer on this view and providing a paint with
12267 * the desired alpha value.</p>
12268 *
12269 * @param paint The paint used to compose the layer. This argument is optional
12270 * and can be null. It is ignored when the layer type is
12271 * {@link #LAYER_TYPE_NONE}
12272 *
12273 * @see #setLayerType(int, android.graphics.Paint)
12274 */
12275 public void setLayerPaint(Paint paint) {
12276 int layerType = getLayerType();
12277 if (layerType != LAYER_TYPE_NONE) {
12278 mLayerPaint = paint == null ? new Paint() : paint;
12279 if (layerType == LAYER_TYPE_HARDWARE) {
12280 HardwareLayer layer = getHardwareLayer();
12281 if (layer != null) {
12282 layer.setLayerPaint(paint);
12283 }
12284 invalidateViewProperty(false, false);
12285 } else {
12286 invalidate();
12287 }
12288 }
12289 }
12290
12291 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012292 * Indicates whether this view has a static layer. A view with layer type
12293 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12294 * dynamic.
12295 */
12296 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012297 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012298 }
12299
12300 /**
Romain Guy171c5922011-01-06 10:04:23 -080012301 * Indicates what type of layer is currently associated with this view. By default
12302 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12303 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12304 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012305 *
Romain Guy171c5922011-01-06 10:04:23 -080012306 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12307 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012308 *
12309 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012310 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012311 * @see #LAYER_TYPE_NONE
12312 * @see #LAYER_TYPE_SOFTWARE
12313 * @see #LAYER_TYPE_HARDWARE
12314 */
12315 public int getLayerType() {
12316 return mLayerType;
12317 }
Joe Malin32736f02011-01-19 16:14:20 -080012318
Romain Guy6c319ca2011-01-11 14:29:25 -080012319 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012320 * Forces this view's layer to be created and this view to be rendered
12321 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12322 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012323 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012324 * This method can for instance be used to render a view into its layer before
12325 * starting an animation. If this view is complex, rendering into the layer
12326 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012327 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012328 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012329 *
12330 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012331 */
12332 public void buildLayer() {
12333 if (mLayerType == LAYER_TYPE_NONE) return;
12334
12335 if (mAttachInfo == null) {
12336 throw new IllegalStateException("This view must be attached to a window first");
12337 }
12338
12339 switch (mLayerType) {
12340 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012341 if (mAttachInfo.mHardwareRenderer != null &&
12342 mAttachInfo.mHardwareRenderer.isEnabled() &&
12343 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012344 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012345 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012346 break;
12347 case LAYER_TYPE_SOFTWARE:
12348 buildDrawingCache(true);
12349 break;
12350 }
12351 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012352
Romain Guyf1ae1062011-03-02 18:16:04 -080012353 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012354 * <p>Returns a hardware layer that can be used to draw this view again
12355 * without executing its draw method.</p>
12356 *
12357 * @return A HardwareLayer ready to render, or null if an error occurred.
12358 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012359 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012360 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12361 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012362 return null;
12363 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012364
Romain Guy9c4b79a2011-11-10 19:23:58 -080012365 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012366
12367 final int width = mRight - mLeft;
12368 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012369
Romain Guy6c319ca2011-01-11 14:29:25 -080012370 if (width == 0 || height == 0) {
12371 return null;
12372 }
12373
Dianne Hackborn4702a852012-08-17 15:18:29 -070012374 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012375 if (mHardwareLayer == null) {
12376 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12377 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012378 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012379 } else {
12380 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012381 if (mHardwareLayer.resize(width, height)) {
12382 mLocalDirtyRect.set(0, 0, width, height);
12383 }
Romain Guy846a5332012-07-11 17:44:57 -070012384 }
12385
12386 // This should not be necessary but applications that change
12387 // the parameters of their background drawable without calling
12388 // this.setBackground(Drawable) can leave the view in a bad state
12389 // (for instance isOpaque() returns true, but the background is
12390 // not opaque.)
12391 computeOpaqueFlags();
12392
12393 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012394 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012395 mHardwareLayer.setOpaque(opaque);
12396 mLocalDirtyRect.set(0, 0, width, height);
12397 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012398 }
12399
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012400 // The layer is not valid if the underlying GPU resources cannot be allocated
12401 if (!mHardwareLayer.isValid()) {
12402 return null;
12403 }
12404
Romain Guy11cb6422012-09-21 00:39:43 -070012405 mHardwareLayer.setLayerPaint(mLayerPaint);
12406 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12407 ViewRootImpl viewRoot = getViewRootImpl();
12408 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12409
Michael Jurka7e52caf2012-03-06 15:57:06 -080012410 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012411 }
12412
12413 return mHardwareLayer;
12414 }
Romain Guy171c5922011-01-06 10:04:23 -080012415
Romain Guy589b0bb2011-10-10 13:57:47 -070012416 /**
12417 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012418 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012419 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012420 *
12421 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012422 * @see #LAYER_TYPE_HARDWARE
12423 */
Romain Guya998dff2012-03-23 18:58:36 -070012424 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012425 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012426 AttachInfo info = mAttachInfo;
12427 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012428 info.mHardwareRenderer.isEnabled() &&
12429 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012430 mHardwareLayer.destroy();
12431 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012432
Chet Haase6a2d17f2012-09-30 12:14:13 -070012433 if (mDisplayList != null) {
12434 mDisplayList.reset();
12435 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012436 invalidate(true);
12437 invalidateParentCaches();
12438 }
Romain Guy65b345f2011-07-27 18:51:50 -070012439 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012440 }
Romain Guy65b345f2011-07-27 18:51:50 -070012441 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012442 }
12443
Romain Guy171c5922011-01-06 10:04:23 -080012444 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012445 * Destroys all hardware rendering resources. This method is invoked
12446 * when the system needs to reclaim resources. Upon execution of this
12447 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012448 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012449 * Note: you <strong>must</strong> call
12450 * <code>super.destroyHardwareResources()</code> when overriding
12451 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012452 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012453 * @hide
12454 */
12455 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012456 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012457 }
12458
12459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012460 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12461 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12462 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12463 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12464 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12465 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012466 *
Romain Guy171c5922011-01-06 10:04:23 -080012467 * <p>Enabling the drawing cache is similar to
12468 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012469 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12470 * drawing cache has no effect on rendering because the system uses a different mechanism
12471 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12472 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12473 * for information on how to enable software and hardware layers.</p>
12474 *
12475 * <p>This API can be used to manually generate
12476 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12477 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012478 *
12479 * @param enabled true to enable the drawing cache, false otherwise
12480 *
12481 * @see #isDrawingCacheEnabled()
12482 * @see #getDrawingCache()
12483 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012484 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012485 */
12486 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012487 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12489 }
12490
12491 /**
12492 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12493 *
12494 * @return true if the drawing cache is enabled
12495 *
12496 * @see #setDrawingCacheEnabled(boolean)
12497 * @see #getDrawingCache()
12498 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012499 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012500 public boolean isDrawingCacheEnabled() {
12501 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12502 }
12503
12504 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012505 * Debugging utility which recursively outputs the dirty state of a view and its
12506 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012507 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012508 * @hide
12509 */
Romain Guy676b1732011-02-14 14:45:33 -080012510 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012511 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012512 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12513 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12514 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12515 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012516 if (clear) {
12517 mPrivateFlags &= clearMask;
12518 }
12519 if (this instanceof ViewGroup) {
12520 ViewGroup parent = (ViewGroup) this;
12521 final int count = parent.getChildCount();
12522 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012523 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012524 child.outputDirtyFlags(indent + " ", clear, clearMask);
12525 }
12526 }
12527 }
12528
12529 /**
12530 * This method is used by ViewGroup to cause its children to restore or recreate their
12531 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12532 * to recreate its own display list, which would happen if it went through the normal
12533 * draw/dispatchDraw mechanisms.
12534 *
12535 * @hide
12536 */
12537 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012538
12539 /**
12540 * A view that is not attached or hardware accelerated cannot create a display list.
12541 * This method checks these conditions and returns the appropriate result.
12542 *
12543 * @return true if view has the ability to create a display list, false otherwise.
12544 *
12545 * @hide
12546 */
12547 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012548 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012549 }
Joe Malin32736f02011-01-19 16:14:20 -080012550
Chet Haasedaf98e92011-01-10 14:10:36 -080012551 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012552 * @return The HardwareRenderer associated with that view or null if hardware rendering
12553 * is not supported or this this has not been attached to a window.
12554 *
12555 * @hide
12556 */
12557 public HardwareRenderer getHardwareRenderer() {
12558 if (mAttachInfo != null) {
12559 return mAttachInfo.mHardwareRenderer;
12560 }
12561 return null;
12562 }
12563
12564 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012565 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12566 * Otherwise, the same display list will be returned (after having been rendered into
12567 * along the way, depending on the invalidation state of the view).
12568 *
12569 * @param displayList The previous version of this displayList, could be null.
12570 * @param isLayer Whether the requester of the display list is a layer. If so,
12571 * the view will avoid creating a layer inside the resulting display list.
12572 * @return A new or reused DisplayList object.
12573 */
12574 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12575 if (!canHaveDisplayList()) {
12576 return null;
12577 }
12578
Dianne Hackborn4702a852012-08-17 15:18:29 -070012579 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012580 displayList == null || !displayList.isValid() ||
12581 (!isLayer && mRecreateDisplayList))) {
12582 // Don't need to recreate the display list, just need to tell our
12583 // children to restore/recreate theirs
12584 if (displayList != null && displayList.isValid() &&
12585 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012586 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12587 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012588 dispatchGetDisplayList();
12589
12590 return displayList;
12591 }
12592
12593 if (!isLayer) {
12594 // If we got here, we're recreating it. Mark it as such to ensure that
12595 // we copy in child display lists into ours in drawChild()
12596 mRecreateDisplayList = true;
12597 }
12598 if (displayList == null) {
12599 final String name = getClass().getSimpleName();
12600 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12601 // If we're creating a new display list, make sure our parent gets invalidated
12602 // since they will need to recreate their display list to account for this
12603 // new child display list.
12604 invalidateParentCaches();
12605 }
12606
12607 boolean caching = false;
12608 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012609 int width = mRight - mLeft;
12610 int height = mBottom - mTop;
12611
12612 try {
12613 canvas.setViewport(width, height);
12614 // The dirty rect should always be null for a display list
12615 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012616 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012617 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012618 if (layerType == LAYER_TYPE_HARDWARE) {
12619 final HardwareLayer layer = getHardwareLayer();
12620 if (layer != null && layer.isValid()) {
12621 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12622 } else {
12623 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12624 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12625 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12626 }
12627 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012628 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012629 buildDrawingCache(true);
12630 Bitmap cache = getDrawingCache(true);
12631 if (cache != null) {
12632 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12633 caching = true;
12634 }
Chet Haasea1cff502012-02-21 13:43:44 -080012635 }
Chet Haasea1cff502012-02-21 13:43:44 -080012636 } else {
12637
12638 computeScroll();
12639
Chet Haasea1cff502012-02-21 13:43:44 -080012640 canvas.translate(-mScrollX, -mScrollY);
12641 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012642 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12643 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012644 }
12645
12646 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012647 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012648 dispatchDraw(canvas);
12649 } else {
12650 draw(canvas);
12651 }
12652 }
12653 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012654 canvas.onPostDraw();
12655
12656 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012657 displayList.setCaching(caching);
12658 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012659 displayList.setLeftTopRightBottom(0, 0, width, height);
12660 } else {
12661 setDisplayListProperties(displayList);
12662 }
12663 }
12664 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012665 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12666 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012667 }
12668
12669 return displayList;
12670 }
12671
12672 /**
12673 * Get the DisplayList for the HardwareLayer
12674 *
12675 * @param layer The HardwareLayer whose DisplayList we want
12676 * @return A DisplayList fopr the specified HardwareLayer
12677 */
12678 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12679 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12680 layer.setDisplayList(displayList);
12681 return displayList;
12682 }
12683
12684
12685 /**
Romain Guyb051e892010-09-28 19:09:36 -070012686 * <p>Returns a display list that can be used to draw this view again
12687 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012688 *
Romain Guyb051e892010-09-28 19:09:36 -070012689 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012690 *
12691 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012692 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012693 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012694 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012695 return mDisplayList;
12696 }
12697
Romain Guy38c2ece2012-05-24 14:20:56 -070012698 private void clearDisplayList() {
12699 if (mDisplayList != null) {
12700 mDisplayList.invalidate();
12701 mDisplayList.clear();
12702 }
12703 }
12704
Romain Guyb051e892010-09-28 19:09:36 -070012705 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012706 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012707 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012708 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012709 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012710 * @see #getDrawingCache(boolean)
12711 */
12712 public Bitmap getDrawingCache() {
12713 return getDrawingCache(false);
12714 }
12715
12716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12718 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12719 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12720 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12721 * request the drawing cache by calling this method and draw it on screen if the
12722 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012723 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012724 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12725 * this method will create a bitmap of the same size as this view. Because this bitmap
12726 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12727 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12728 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12729 * size than the view. This implies that your application must be able to handle this
12730 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012731 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012732 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12733 * the current density of the screen when the application is in compatibility
12734 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012736 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012737 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012738 * @see #setDrawingCacheEnabled(boolean)
12739 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012740 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 * @see #destroyDrawingCache()
12742 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012743 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012744 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12745 return null;
12746 }
12747 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012748 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012749 }
Romain Guy02890fd2010-08-06 17:58:44 -070012750 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 }
12752
12753 /**
12754 * <p>Frees the resources used by the drawing cache. If you call
12755 * {@link #buildDrawingCache()} manually without calling
12756 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12757 * should cleanup the cache with this method afterwards.</p>
12758 *
12759 * @see #setDrawingCacheEnabled(boolean)
12760 * @see #buildDrawingCache()
12761 * @see #getDrawingCache()
12762 */
12763 public void destroyDrawingCache() {
12764 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012765 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 mDrawingCache = null;
12767 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012768 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012769 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012770 mUnscaledDrawingCache = null;
12771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012772 }
12773
12774 /**
12775 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012776 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 * view will always be drawn on top of a solid color.
12778 *
12779 * @param color The background color to use for the drawing cache's bitmap
12780 *
12781 * @see #setDrawingCacheEnabled(boolean)
12782 * @see #buildDrawingCache()
12783 * @see #getDrawingCache()
12784 */
12785 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012786 if (color != mDrawingCacheBackgroundColor) {
12787 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012788 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 }
12791
12792 /**
12793 * @see #setDrawingCacheBackgroundColor(int)
12794 *
12795 * @return The background color to used for the drawing cache's bitmap
12796 */
12797 public int getDrawingCacheBackgroundColor() {
12798 return mDrawingCacheBackgroundColor;
12799 }
12800
12801 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012802 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012803 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012804 * @see #buildDrawingCache(boolean)
12805 */
12806 public void buildDrawingCache() {
12807 buildDrawingCache(false);
12808 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012809
Romain Guyfbd8f692009-06-26 14:51:58 -070012810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012811 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12812 *
12813 * <p>If you call {@link #buildDrawingCache()} manually without calling
12814 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12815 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012816 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012817 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12818 * this method will create a bitmap of the same size as this view. Because this bitmap
12819 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12820 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12821 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12822 * size than the view. This implies that your application must be able to handle this
12823 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012824 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012825 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12826 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012827 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012828 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012829 *
12830 * @see #getDrawingCache()
12831 * @see #destroyDrawingCache()
12832 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012833 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012834 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012835 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012836 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012837
Romain Guy8506ab42009-06-11 17:35:47 -070012838 int width = mRight - mLeft;
12839 int height = mBottom - mTop;
12840
12841 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012842 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012843
Romain Guye1123222009-06-29 14:24:56 -070012844 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012845 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12846 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012848
12849 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012850 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012851 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012852
Romain Guyaf61cc42012-09-30 10:49:15 -070012853 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12854 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012855 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12856 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012857 if (width > 0 && height > 0) {
12858 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12859 + projectedBitmapSize + " bytes, only "
12860 + drawingCacheSize + " available");
12861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012862 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012863 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012864 return;
12865 }
12866
12867 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012868 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012869
12870 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012871 Bitmap.Config quality;
12872 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012873 // Never pick ARGB_4444 because it looks awful
12874 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012875 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12876 case DRAWING_CACHE_QUALITY_AUTO:
12877 quality = Bitmap.Config.ARGB_8888;
12878 break;
12879 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012880 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881 break;
12882 case DRAWING_CACHE_QUALITY_HIGH:
12883 quality = Bitmap.Config.ARGB_8888;
12884 break;
12885 default:
12886 quality = Bitmap.Config.ARGB_8888;
12887 break;
12888 }
12889 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012890 // Optimization for translucent windows
12891 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012892 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012893 }
12894
12895 // Try to cleanup memory
12896 if (bitmap != null) bitmap.recycle();
12897
12898 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012899 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12900 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012901 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012902 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012903 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012904 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012905 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012906 }
Adam Powell26153a32010-11-08 15:22:27 -080012907 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012908 } catch (OutOfMemoryError e) {
12909 // If there is not enough memory to create the bitmap cache, just
12910 // ignore the issue as bitmap caches are not required to draw the
12911 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012912 if (autoScale) {
12913 mDrawingCache = null;
12914 } else {
12915 mUnscaledDrawingCache = null;
12916 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012917 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012918 return;
12919 }
12920
12921 clear = drawingCacheBackgroundColor != 0;
12922 }
12923
12924 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 if (attachInfo != null) {
12926 canvas = attachInfo.mCanvas;
12927 if (canvas == null) {
12928 canvas = new Canvas();
12929 }
12930 canvas.setBitmap(bitmap);
12931 // Temporarily clobber the cached Canvas in case one of our children
12932 // is also using a drawing cache. Without this, the children would
12933 // steal the canvas by attaching their own bitmap to it and bad, bad
12934 // thing would happen (invisible views, corrupted drawings, etc.)
12935 attachInfo.mCanvas = null;
12936 } else {
12937 // This case should hopefully never or seldom happen
12938 canvas = new Canvas(bitmap);
12939 }
12940
12941 if (clear) {
12942 bitmap.eraseColor(drawingCacheBackgroundColor);
12943 }
12944
12945 computeScroll();
12946 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012947
Romain Guye1123222009-06-29 14:24:56 -070012948 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012949 final float scale = attachInfo.mApplicationScale;
12950 canvas.scale(scale, scale);
12951 }
Joe Malin32736f02011-01-19 16:14:20 -080012952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012953 canvas.translate(-mScrollX, -mScrollY);
12954
Dianne Hackborn4702a852012-08-17 15:18:29 -070012955 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012956 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12957 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012958 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012960
12961 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012962 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12963 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012964 dispatchDraw(canvas);
12965 } else {
12966 draw(canvas);
12967 }
12968
12969 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012970 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012971
12972 if (attachInfo != null) {
12973 // Restore the cached Canvas for our siblings
12974 attachInfo.mCanvas = canvas;
12975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012976 }
12977 }
12978
12979 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012980 * Create a snapshot of the view into a bitmap. We should probably make
12981 * some form of this public, but should think about the API.
12982 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012983 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012984 int width = mRight - mLeft;
12985 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012986
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012987 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012988 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012989 width = (int) ((width * scale) + 0.5f);
12990 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012991
Dianne Hackborndde331c2012-08-03 14:01:57 -070012992 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12993 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012994 if (bitmap == null) {
12995 throw new OutOfMemoryError();
12996 }
12997
Romain Guyc529d8d2011-09-06 15:01:39 -070012998 Resources resources = getResources();
12999 if (resources != null) {
13000 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
13001 }
Joe Malin32736f02011-01-19 16:14:20 -080013002
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013003 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013004 if (attachInfo != null) {
13005 canvas = attachInfo.mCanvas;
13006 if (canvas == null) {
13007 canvas = new Canvas();
13008 }
13009 canvas.setBitmap(bitmap);
13010 // Temporarily clobber the cached Canvas in case one of our children
13011 // is also using a drawing cache. Without this, the children would
13012 // steal the canvas by attaching their own bitmap to it and bad, bad
13013 // things would happen (invisible views, corrupted drawings, etc.)
13014 attachInfo.mCanvas = null;
13015 } else {
13016 // This case should hopefully never or seldom happen
13017 canvas = new Canvas(bitmap);
13018 }
13019
Romain Guy5bcdff42009-05-14 21:27:18 -070013020 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013021 bitmap.eraseColor(backgroundColor);
13022 }
13023
13024 computeScroll();
13025 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013026 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013027 canvas.translate(-mScrollX, -mScrollY);
13028
Romain Guy5bcdff42009-05-14 21:27:18 -070013029 // Temporarily remove the dirty mask
13030 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013031 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013032
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013033 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013034 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013035 dispatchDraw(canvas);
13036 } else {
13037 draw(canvas);
13038 }
13039
Romain Guy5bcdff42009-05-14 21:27:18 -070013040 mPrivateFlags = flags;
13041
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013042 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013043 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013044
13045 if (attachInfo != null) {
13046 // Restore the cached Canvas for our siblings
13047 attachInfo.mCanvas = canvas;
13048 }
Romain Guy8506ab42009-06-11 17:35:47 -070013049
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013050 return bitmap;
13051 }
13052
13053 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013054 * Indicates whether this View is currently in edit mode. A View is usually
13055 * in edit mode when displayed within a developer tool. For instance, if
13056 * this View is being drawn by a visual user interface builder, this method
13057 * should return true.
13058 *
13059 * Subclasses should check the return value of this method to provide
13060 * different behaviors if their normal behavior might interfere with the
13061 * host environment. For instance: the class spawns a thread in its
13062 * constructor, the drawing code relies on device-specific features, etc.
13063 *
13064 * This method is usually checked in the drawing code of custom widgets.
13065 *
13066 * @return True if this View is in edit mode, false otherwise.
13067 */
13068 public boolean isInEditMode() {
13069 return false;
13070 }
13071
13072 /**
13073 * If the View draws content inside its padding and enables fading edges,
13074 * it needs to support padding offsets. Padding offsets are added to the
13075 * fading edges to extend the length of the fade so that it covers pixels
13076 * drawn inside the padding.
13077 *
13078 * Subclasses of this class should override this method if they need
13079 * to draw content inside the padding.
13080 *
13081 * @return True if padding offset must be applied, false otherwise.
13082 *
13083 * @see #getLeftPaddingOffset()
13084 * @see #getRightPaddingOffset()
13085 * @see #getTopPaddingOffset()
13086 * @see #getBottomPaddingOffset()
13087 *
13088 * @since CURRENT
13089 */
13090 protected boolean isPaddingOffsetRequired() {
13091 return false;
13092 }
13093
13094 /**
13095 * Amount by which to extend the left fading region. Called only when
13096 * {@link #isPaddingOffsetRequired()} returns true.
13097 *
13098 * @return The left padding offset in pixels.
13099 *
13100 * @see #isPaddingOffsetRequired()
13101 *
13102 * @since CURRENT
13103 */
13104 protected int getLeftPaddingOffset() {
13105 return 0;
13106 }
13107
13108 /**
13109 * Amount by which to extend the right fading region. Called only when
13110 * {@link #isPaddingOffsetRequired()} returns true.
13111 *
13112 * @return The right padding offset in pixels.
13113 *
13114 * @see #isPaddingOffsetRequired()
13115 *
13116 * @since CURRENT
13117 */
13118 protected int getRightPaddingOffset() {
13119 return 0;
13120 }
13121
13122 /**
13123 * Amount by which to extend the top fading region. Called only when
13124 * {@link #isPaddingOffsetRequired()} returns true.
13125 *
13126 * @return The top padding offset in pixels.
13127 *
13128 * @see #isPaddingOffsetRequired()
13129 *
13130 * @since CURRENT
13131 */
13132 protected int getTopPaddingOffset() {
13133 return 0;
13134 }
13135
13136 /**
13137 * Amount by which to extend the bottom fading region. Called only when
13138 * {@link #isPaddingOffsetRequired()} returns true.
13139 *
13140 * @return The bottom padding offset in pixels.
13141 *
13142 * @see #isPaddingOffsetRequired()
13143 *
13144 * @since CURRENT
13145 */
13146 protected int getBottomPaddingOffset() {
13147 return 0;
13148 }
13149
13150 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013151 * @hide
13152 * @param offsetRequired
13153 */
13154 protected int getFadeTop(boolean offsetRequired) {
13155 int top = mPaddingTop;
13156 if (offsetRequired) top += getTopPaddingOffset();
13157 return top;
13158 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013159
Romain Guyf2fc4602011-07-19 15:20:03 -070013160 /**
13161 * @hide
13162 * @param offsetRequired
13163 */
13164 protected int getFadeHeight(boolean offsetRequired) {
13165 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013166 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013167 return mBottom - mTop - mPaddingBottom - padding;
13168 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013169
Romain Guyf2fc4602011-07-19 15:20:03 -070013170 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013171 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013172 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013173 *
Romain Guy2bffd262010-09-12 17:40:02 -070013174 * <p>Even if this method returns true, it does not mean that every call
13175 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13176 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013177 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013178 * window is hardware accelerated,
13179 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13180 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013181 *
Romain Guy2bffd262010-09-12 17:40:02 -070013182 * @return True if the view is attached to a window and the window is
13183 * hardware accelerated; false in any other case.
13184 */
13185 public boolean isHardwareAccelerated() {
13186 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13187 }
Joe Malin32736f02011-01-19 16:14:20 -080013188
Romain Guy2bffd262010-09-12 17:40:02 -070013189 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013190 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13191 * case of an active Animation being run on the view.
13192 */
13193 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13194 Animation a, boolean scalingRequired) {
13195 Transformation invalidationTransform;
13196 final int flags = parent.mGroupFlags;
13197 final boolean initialized = a.isInitialized();
13198 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013199 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013200 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013201 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013202 onAnimationStart();
13203 }
13204
13205 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13206 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13207 if (parent.mInvalidationTransformation == null) {
13208 parent.mInvalidationTransformation = new Transformation();
13209 }
13210 invalidationTransform = parent.mInvalidationTransformation;
13211 a.getTransformation(drawingTime, invalidationTransform, 1f);
13212 } else {
13213 invalidationTransform = parent.mChildTransformation;
13214 }
Romain Guy393a52c2012-05-22 20:21:08 -070013215
Chet Haasebcca79a2012-02-14 08:45:14 -080013216 if (more) {
13217 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013218 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13219 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13220 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13221 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013222 // The child need to draw an animation, potentially offscreen, so
13223 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013224 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013225 parent.invalidate(mLeft, mTop, mRight, mBottom);
13226 }
13227 } else {
13228 if (parent.mInvalidateRegion == null) {
13229 parent.mInvalidateRegion = new RectF();
13230 }
13231 final RectF region = parent.mInvalidateRegion;
13232 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13233 invalidationTransform);
13234
13235 // The child need to draw an animation, potentially offscreen, so
13236 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013237 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013238
13239 final int left = mLeft + (int) region.left;
13240 final int top = mTop + (int) region.top;
13241 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13242 top + (int) (region.height() + .5f));
13243 }
13244 }
13245 return more;
13246 }
13247
Chet Haasea1cff502012-02-21 13:43:44 -080013248 /**
13249 * This method is called by getDisplayList() when a display list is created or re-rendered.
13250 * It sets or resets the current value of all properties on that display list (resetting is
13251 * necessary when a display list is being re-created, because we need to make sure that
13252 * previously-set transform values
13253 */
13254 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013255 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013256 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013257 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013258 if (mParent instanceof ViewGroup) {
13259 displayList.setClipChildren(
13260 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13261 }
Chet Haase9420abd2012-03-29 16:28:32 -070013262 float alpha = 1;
13263 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13264 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13265 ViewGroup parentVG = (ViewGroup) mParent;
13266 final boolean hasTransform =
13267 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13268 if (hasTransform) {
13269 Transformation transform = parentVG.mChildTransformation;
13270 final int transformType = parentVG.mChildTransformation.getTransformationType();
13271 if (transformType != Transformation.TYPE_IDENTITY) {
13272 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13273 alpha = transform.getAlpha();
13274 }
13275 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13276 displayList.setStaticMatrix(transform.getMatrix());
13277 }
13278 }
13279 }
Chet Haasea1cff502012-02-21 13:43:44 -080013280 }
13281 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013282 alpha *= mTransformationInfo.mAlpha;
13283 if (alpha < 1) {
13284 final int multipliedAlpha = (int) (255 * alpha);
13285 if (onSetAlpha(multipliedAlpha)) {
13286 alpha = 1;
13287 }
13288 }
13289 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013290 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13291 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13292 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13293 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013294 if (mTransformationInfo.mCamera == null) {
13295 mTransformationInfo.mCamera = new Camera();
13296 mTransformationInfo.matrix3D = new Matrix();
13297 }
13298 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013299 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013300 displayList.setPivotX(getPivotX());
13301 displayList.setPivotY(getPivotY());
13302 }
Chet Haase9420abd2012-03-29 16:28:32 -070013303 } else if (alpha < 1) {
13304 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013305 }
13306 }
13307 }
13308
Chet Haasebcca79a2012-02-14 08:45:14 -080013309 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013310 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13311 * This draw() method is an implementation detail and is not intended to be overridden or
13312 * to be called from anywhere else other than ViewGroup.drawChild().
13313 */
13314 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013315 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013316 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013317 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013318 final int flags = parent.mGroupFlags;
13319
Chet Haasea1cff502012-02-21 13:43:44 -080013320 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013321 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013322 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013323 }
13324
13325 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013326 boolean concatMatrix = false;
13327
13328 boolean scalingRequired = false;
13329 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013330 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013331
13332 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013333 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13334 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013335 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013336 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013337 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13338 } else {
13339 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13340 }
13341
Chet Haasebcca79a2012-02-14 08:45:14 -080013342 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013343 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013344 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013345 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013346 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013347 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013348 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013349 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013350 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013351 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013352 mDisplayList != null) {
13353 // No longer animating: clear out old animation matrix
13354 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013355 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013356 }
13357 if (!useDisplayListProperties &&
13358 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13359 final boolean hasTransform =
13360 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13361 if (hasTransform) {
13362 final int transformType = parent.mChildTransformation.getTransformationType();
13363 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13364 parent.mChildTransformation : null;
13365 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13366 }
Chet Haase64a48c12012-02-13 16:33:29 -080013367 }
13368 }
13369
13370 concatMatrix |= !childHasIdentityMatrix;
13371
13372 // Sets the flag as early as possible to allow draw() implementations
13373 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013374 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013375
Romain Guyfbb43212012-08-30 15:19:27 -070013376 if (!concatMatrix &&
13377 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13378 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013379 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013380 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13381 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013382 return more;
13383 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013384 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013385
13386 if (hardwareAccelerated) {
13387 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13388 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013389 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13390 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013391 }
13392
Chet Haase64a48c12012-02-13 16:33:29 -080013393 DisplayList displayList = null;
13394 Bitmap cache = null;
13395 boolean hasDisplayList = false;
13396 if (caching) {
13397 if (!hardwareAccelerated) {
13398 if (layerType != LAYER_TYPE_NONE) {
13399 layerType = LAYER_TYPE_SOFTWARE;
13400 buildDrawingCache(true);
13401 }
13402 cache = getDrawingCache(true);
13403 } else {
13404 switch (layerType) {
13405 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013406 if (useDisplayListProperties) {
13407 hasDisplayList = canHaveDisplayList();
13408 } else {
13409 buildDrawingCache(true);
13410 cache = getDrawingCache(true);
13411 }
Chet Haase64a48c12012-02-13 16:33:29 -080013412 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013413 case LAYER_TYPE_HARDWARE:
13414 if (useDisplayListProperties) {
13415 hasDisplayList = canHaveDisplayList();
13416 }
13417 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013418 case LAYER_TYPE_NONE:
13419 // Delay getting the display list until animation-driven alpha values are
13420 // set up and possibly passed on to the view
13421 hasDisplayList = canHaveDisplayList();
13422 break;
13423 }
13424 }
13425 }
Chet Haasea1cff502012-02-21 13:43:44 -080013426 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013427 if (useDisplayListProperties) {
13428 displayList = getDisplayList();
13429 if (!displayList.isValid()) {
13430 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13431 // to getDisplayList(), the display list will be marked invalid and we should not
13432 // try to use it again.
13433 displayList = null;
13434 hasDisplayList = false;
13435 useDisplayListProperties = false;
13436 }
13437 }
Chet Haase64a48c12012-02-13 16:33:29 -080013438
Chet Haase526057b2012-07-12 17:50:41 -070013439 int sx = 0;
13440 int sy = 0;
13441 if (!hasDisplayList) {
13442 computeScroll();
13443 sx = mScrollX;
13444 sy = mScrollY;
13445 }
13446
Chet Haase64a48c12012-02-13 16:33:29 -080013447 final boolean hasNoCache = cache == null || hasDisplayList;
13448 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13449 layerType != LAYER_TYPE_HARDWARE;
13450
Chet Haasea1cff502012-02-21 13:43:44 -080013451 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013452 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013453 restoreTo = canvas.save();
13454 }
Chet Haase64a48c12012-02-13 16:33:29 -080013455 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013456 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013457 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013458 if (!useDisplayListProperties) {
13459 canvas.translate(mLeft, mTop);
13460 }
Chet Haase64a48c12012-02-13 16:33:29 -080013461 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013462 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013463 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013464 restoreTo = canvas.save();
13465 }
Chet Haase64a48c12012-02-13 16:33:29 -080013466 // mAttachInfo cannot be null, otherwise scalingRequired == false
13467 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13468 canvas.scale(scale, scale);
13469 }
13470 }
13471
Chet Haasea1cff502012-02-21 13:43:44 -080013472 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013473 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013474 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013475 if (transformToApply != null || !childHasIdentityMatrix) {
13476 int transX = 0;
13477 int transY = 0;
13478
13479 if (offsetForScroll) {
13480 transX = -sx;
13481 transY = -sy;
13482 }
13483
13484 if (transformToApply != null) {
13485 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013486 if (useDisplayListProperties) {
13487 displayList.setAnimationMatrix(transformToApply.getMatrix());
13488 } else {
13489 // Undo the scroll translation, apply the transformation matrix,
13490 // then redo the scroll translate to get the correct result.
13491 canvas.translate(-transX, -transY);
13492 canvas.concat(transformToApply.getMatrix());
13493 canvas.translate(transX, transY);
13494 }
Chet Haasea1cff502012-02-21 13:43:44 -080013495 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013496 }
13497
13498 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013499 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013500 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013501 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013502 }
13503 }
13504
Chet Haasea1cff502012-02-21 13:43:44 -080013505 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013506 canvas.translate(-transX, -transY);
13507 canvas.concat(getMatrix());
13508 canvas.translate(transX, transY);
13509 }
13510 }
13511
Chet Haase21433372012-06-05 07:54:09 -070013512 // Deal with alpha if it is or used to be <1
13513 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013514 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013515 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013516 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013517 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013518 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013519 }
Chet Haasea1cff502012-02-21 13:43:44 -080013520 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013521 if (hasNoCache) {
13522 final int multipliedAlpha = (int) (255 * alpha);
13523 if (!onSetAlpha(multipliedAlpha)) {
13524 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013525 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013526 layerType != LAYER_TYPE_NONE) {
13527 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13528 }
Chet Haase9420abd2012-03-29 16:28:32 -070013529 if (useDisplayListProperties) {
13530 displayList.setAlpha(alpha * getAlpha());
13531 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013532 final int scrollX = hasDisplayList ? 0 : sx;
13533 final int scrollY = hasDisplayList ? 0 : sy;
13534 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13535 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013536 }
13537 } else {
13538 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013539 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013540 }
13541 }
13542 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013543 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013544 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013545 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013546 }
13547
Chet Haasea1cff502012-02-21 13:43:44 -080013548 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13549 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013550 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013551 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013552 } else {
13553 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013554 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013555 } else {
13556 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13557 }
13558 }
13559 }
13560
Chet Haase9420abd2012-03-29 16:28:32 -070013561 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013562 displayList = getDisplayList();
13563 if (!displayList.isValid()) {
13564 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13565 // to getDisplayList(), the display list will be marked invalid and we should not
13566 // try to use it again.
13567 displayList = null;
13568 hasDisplayList = false;
13569 }
13570 }
13571
13572 if (hasNoCache) {
13573 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013574 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013575 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013576 if (layer != null && layer.isValid()) {
13577 mLayerPaint.setAlpha((int) (alpha * 255));
13578 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13579 layerRendered = true;
13580 } else {
13581 final int scrollX = hasDisplayList ? 0 : sx;
13582 final int scrollY = hasDisplayList ? 0 : sy;
13583 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013584 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013585 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13586 }
13587 }
13588
13589 if (!layerRendered) {
13590 if (!hasDisplayList) {
13591 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013592 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13593 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013594 dispatchDraw(canvas);
13595 } else {
13596 draw(canvas);
13597 }
13598 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013599 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013600 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013601 }
13602 }
13603 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013604 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013605 Paint cachePaint;
13606
13607 if (layerType == LAYER_TYPE_NONE) {
13608 cachePaint = parent.mCachePaint;
13609 if (cachePaint == null) {
13610 cachePaint = new Paint();
13611 cachePaint.setDither(false);
13612 parent.mCachePaint = cachePaint;
13613 }
Chet Haase9420abd2012-03-29 16:28:32 -070013614 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013615 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013616 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13617 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013618 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013619 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013620 }
13621 } else {
13622 cachePaint = mLayerPaint;
13623 cachePaint.setAlpha((int) (alpha * 255));
13624 }
13625 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13626 }
13627
Chet Haasea1cff502012-02-21 13:43:44 -080013628 if (restoreTo >= 0) {
13629 canvas.restoreToCount(restoreTo);
13630 }
Chet Haase64a48c12012-02-13 16:33:29 -080013631
13632 if (a != null && !more) {
13633 if (!hardwareAccelerated && !a.getFillAfter()) {
13634 onSetAlpha(255);
13635 }
13636 parent.finishAnimatingView(this, a);
13637 }
13638
13639 if (more && hardwareAccelerated) {
13640 // invalidation is the trigger to recreate display lists, so if we're using
13641 // display lists to render, force an invalidate to allow the animation to
13642 // continue drawing another frame
13643 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013644 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013645 // alpha animations should cause the child to recreate its display list
13646 invalidate(true);
13647 }
13648 }
13649
13650 mRecreateDisplayList = false;
13651
13652 return more;
13653 }
13654
13655 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656 * Manually render this view (and all of its children) to the given Canvas.
13657 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013658 * called. When implementing a view, implement
13659 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13660 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013661 *
13662 * @param canvas The Canvas to which the View is rendered.
13663 */
13664 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013665 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013666 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013667 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013668 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013670 /*
13671 * Draw traversal performs several drawing steps which must be executed
13672 * in the appropriate order:
13673 *
13674 * 1. Draw the background
13675 * 2. If necessary, save the canvas' layers to prepare for fading
13676 * 3. Draw view's content
13677 * 4. Draw children
13678 * 5. If necessary, draw the fading edges and restore layers
13679 * 6. Draw decorations (scrollbars for instance)
13680 */
13681
13682 // Step 1, draw the background, if needed
13683 int saveCount;
13684
Romain Guy24443ea2009-05-11 11:56:30 -070013685 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013686 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013687 if (background != null) {
13688 final int scrollX = mScrollX;
13689 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013690
Romain Guy24443ea2009-05-11 11:56:30 -070013691 if (mBackgroundSizeChanged) {
13692 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13693 mBackgroundSizeChanged = false;
13694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013695
Romain Guy24443ea2009-05-11 11:56:30 -070013696 if ((scrollX | scrollY) == 0) {
13697 background.draw(canvas);
13698 } else {
13699 canvas.translate(scrollX, scrollY);
13700 background.draw(canvas);
13701 canvas.translate(-scrollX, -scrollY);
13702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013703 }
13704 }
13705
13706 // skip step 2 & 5 if possible (common case)
13707 final int viewFlags = mViewFlags;
13708 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13709 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13710 if (!verticalEdges && !horizontalEdges) {
13711 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013712 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013713
13714 // Step 4, draw the children
13715 dispatchDraw(canvas);
13716
13717 // Step 6, draw decorations (scrollbars)
13718 onDrawScrollBars(canvas);
13719
13720 // we're done...
13721 return;
13722 }
13723
13724 /*
13725 * Here we do the full fledged routine...
13726 * (this is an uncommon case where speed matters less,
13727 * this is why we repeat some of the tests that have been
13728 * done above)
13729 */
13730
13731 boolean drawTop = false;
13732 boolean drawBottom = false;
13733 boolean drawLeft = false;
13734 boolean drawRight = false;
13735
13736 float topFadeStrength = 0.0f;
13737 float bottomFadeStrength = 0.0f;
13738 float leftFadeStrength = 0.0f;
13739 float rightFadeStrength = 0.0f;
13740
13741 // Step 2, save the canvas' layers
13742 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013743
13744 final boolean offsetRequired = isPaddingOffsetRequired();
13745 if (offsetRequired) {
13746 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013747 }
13748
13749 int left = mScrollX + paddingLeft;
13750 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013751 int top = mScrollY + getFadeTop(offsetRequired);
13752 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753
13754 if (offsetRequired) {
13755 right += getRightPaddingOffset();
13756 bottom += getBottomPaddingOffset();
13757 }
13758
13759 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013760 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013761 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762
13763 // clip the fade length if top and bottom fades overlap
13764 // overlapping fades produce odd-looking artifacts
13765 if (verticalEdges && (top + length > bottom - length)) {
13766 length = (bottom - top) / 2;
13767 }
13768
13769 // also clip horizontal fades if necessary
13770 if (horizontalEdges && (left + length > right - length)) {
13771 length = (right - left) / 2;
13772 }
13773
13774 if (verticalEdges) {
13775 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013776 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013777 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013778 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013779 }
13780
13781 if (horizontalEdges) {
13782 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013783 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013785 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013786 }
13787
13788 saveCount = canvas.getSaveCount();
13789
13790 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013791 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013792 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13793
13794 if (drawTop) {
13795 canvas.saveLayer(left, top, right, top + length, null, flags);
13796 }
13797
13798 if (drawBottom) {
13799 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13800 }
13801
13802 if (drawLeft) {
13803 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13804 }
13805
13806 if (drawRight) {
13807 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13808 }
13809 } else {
13810 scrollabilityCache.setFadeColor(solidColor);
13811 }
13812
13813 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013814 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013815
13816 // Step 4, draw the children
13817 dispatchDraw(canvas);
13818
13819 // Step 5, draw the fade effect and restore layers
13820 final Paint p = scrollabilityCache.paint;
13821 final Matrix matrix = scrollabilityCache.matrix;
13822 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013823
13824 if (drawTop) {
13825 matrix.setScale(1, fadeHeight * topFadeStrength);
13826 matrix.postTranslate(left, top);
13827 fade.setLocalMatrix(matrix);
13828 canvas.drawRect(left, top, right, top + length, p);
13829 }
13830
13831 if (drawBottom) {
13832 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13833 matrix.postRotate(180);
13834 matrix.postTranslate(left, bottom);
13835 fade.setLocalMatrix(matrix);
13836 canvas.drawRect(left, bottom - length, right, bottom, p);
13837 }
13838
13839 if (drawLeft) {
13840 matrix.setScale(1, fadeHeight * leftFadeStrength);
13841 matrix.postRotate(-90);
13842 matrix.postTranslate(left, top);
13843 fade.setLocalMatrix(matrix);
13844 canvas.drawRect(left, top, left + length, bottom, p);
13845 }
13846
13847 if (drawRight) {
13848 matrix.setScale(1, fadeHeight * rightFadeStrength);
13849 matrix.postRotate(90);
13850 matrix.postTranslate(right, top);
13851 fade.setLocalMatrix(matrix);
13852 canvas.drawRect(right - length, top, right, bottom, p);
13853 }
13854
13855 canvas.restoreToCount(saveCount);
13856
13857 // Step 6, draw decorations (scrollbars)
13858 onDrawScrollBars(canvas);
13859 }
13860
13861 /**
13862 * Override this if your view is known to always be drawn on top of a solid color background,
13863 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13864 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13865 * should be set to 0xFF.
13866 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013867 * @see #setVerticalFadingEdgeEnabled(boolean)
13868 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013869 *
13870 * @return The known solid color background for this view, or 0 if the color may vary
13871 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013872 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013873 public int getSolidColor() {
13874 return 0;
13875 }
13876
13877 /**
13878 * Build a human readable string representation of the specified view flags.
13879 *
13880 * @param flags the view flags to convert to a string
13881 * @return a String representing the supplied flags
13882 */
13883 private static String printFlags(int flags) {
13884 String output = "";
13885 int numFlags = 0;
13886 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13887 output += "TAKES_FOCUS";
13888 numFlags++;
13889 }
13890
13891 switch (flags & VISIBILITY_MASK) {
13892 case INVISIBLE:
13893 if (numFlags > 0) {
13894 output += " ";
13895 }
13896 output += "INVISIBLE";
13897 // USELESS HERE numFlags++;
13898 break;
13899 case GONE:
13900 if (numFlags > 0) {
13901 output += " ";
13902 }
13903 output += "GONE";
13904 // USELESS HERE numFlags++;
13905 break;
13906 default:
13907 break;
13908 }
13909 return output;
13910 }
13911
13912 /**
13913 * Build a human readable string representation of the specified private
13914 * view flags.
13915 *
13916 * @param privateFlags the private view flags to convert to a string
13917 * @return a String representing the supplied flags
13918 */
13919 private static String printPrivateFlags(int privateFlags) {
13920 String output = "";
13921 int numFlags = 0;
13922
Dianne Hackborn4702a852012-08-17 15:18:29 -070013923 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013924 output += "WANTS_FOCUS";
13925 numFlags++;
13926 }
13927
Dianne Hackborn4702a852012-08-17 15:18:29 -070013928 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013929 if (numFlags > 0) {
13930 output += " ";
13931 }
13932 output += "FOCUSED";
13933 numFlags++;
13934 }
13935
Dianne Hackborn4702a852012-08-17 15:18:29 -070013936 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 if (numFlags > 0) {
13938 output += " ";
13939 }
13940 output += "SELECTED";
13941 numFlags++;
13942 }
13943
Dianne Hackborn4702a852012-08-17 15:18:29 -070013944 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 if (numFlags > 0) {
13946 output += " ";
13947 }
13948 output += "IS_ROOT_NAMESPACE";
13949 numFlags++;
13950 }
13951
Dianne Hackborn4702a852012-08-17 15:18:29 -070013952 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013953 if (numFlags > 0) {
13954 output += " ";
13955 }
13956 output += "HAS_BOUNDS";
13957 numFlags++;
13958 }
13959
Dianne Hackborn4702a852012-08-17 15:18:29 -070013960 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 if (numFlags > 0) {
13962 output += " ";
13963 }
13964 output += "DRAWN";
13965 // USELESS HERE numFlags++;
13966 }
13967 return output;
13968 }
13969
13970 /**
13971 * <p>Indicates whether or not this view's layout will be requested during
13972 * the next hierarchy layout pass.</p>
13973 *
13974 * @return true if the layout will be forced during next layout pass
13975 */
13976 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013977 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013978 }
13979
13980 /**
Philip Milne7b757812012-09-19 18:13:44 -070013981 * Return true if o is a ViewGroup that is laying out using optical bounds.
13982 * @hide
13983 */
13984 public static boolean isLayoutModeOptical(Object o) {
13985 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
13986 }
13987
13988 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
13989 Insets parentInsets = mParent instanceof View ?
13990 ((View) mParent).getOpticalInsets() : Insets.NONE;
13991 Insets childInsets = getOpticalInsets();
13992 return setFrame(
13993 left + parentInsets.left - childInsets.left,
13994 top + parentInsets.top - childInsets.top,
13995 right + parentInsets.left + childInsets.right,
13996 bottom + parentInsets.top + childInsets.bottom);
13997 }
13998
13999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 * Assign a size and position to a view and all of its
14001 * descendants
14002 *
14003 * <p>This is the second phase of the layout mechanism.
14004 * (The first is measuring). In this phase, each parent calls
14005 * layout on all of its children to position them.
14006 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014007 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014008 *
Chet Haase9c087442011-01-12 16:20:16 -080014009 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 * Derived classes with children should override
14011 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014012 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 *
14014 * @param l Left position, relative to parent
14015 * @param t Top position, relative to parent
14016 * @param r Right position, relative to parent
14017 * @param b Bottom position, relative to parent
14018 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014019 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014020 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014021 int oldL = mLeft;
14022 int oldT = mTop;
14023 int oldB = mBottom;
14024 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014025 boolean changed = isLayoutModeOptical(mParent) ?
14026 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014027 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014028 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014029 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014030
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014031 ListenerInfo li = mListenerInfo;
14032 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014033 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014034 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014035 int numListeners = listenersCopy.size();
14036 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014037 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014038 }
14039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014040 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014041 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042 }
14043
14044 /**
14045 * Called from layout when this view should
14046 * assign a size and position to each of its children.
14047 *
14048 * Derived classes with children should override
14049 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014050 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014051 * @param changed This is a new size or position for this view
14052 * @param left Left position, relative to parent
14053 * @param top Top position, relative to parent
14054 * @param right Right position, relative to parent
14055 * @param bottom Bottom position, relative to parent
14056 */
14057 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14058 }
14059
14060 /**
14061 * Assign a size and position to this view.
14062 *
14063 * This is called from layout.
14064 *
14065 * @param left Left position, relative to parent
14066 * @param top Top position, relative to parent
14067 * @param right Right position, relative to parent
14068 * @param bottom Bottom position, relative to parent
14069 * @return true if the new size and position are different than the
14070 * previous ones
14071 * {@hide}
14072 */
14073 protected boolean setFrame(int left, int top, int right, int bottom) {
14074 boolean changed = false;
14075
14076 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014077 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014078 + right + "," + bottom + ")");
14079 }
14080
14081 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14082 changed = true;
14083
14084 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014085 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 int oldWidth = mRight - mLeft;
14088 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014089 int newWidth = right - left;
14090 int newHeight = bottom - top;
14091 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14092
14093 // Invalidate our old position
14094 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014095
14096 mLeft = left;
14097 mTop = top;
14098 mRight = right;
14099 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014100 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014101 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103
Dianne Hackborn4702a852012-08-17 15:18:29 -070014104 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014106
Chet Haase75755e22011-07-18 17:48:25 -070014107 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014108 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014109 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014110 if (mTransformationInfo != null) {
14111 mTransformationInfo.mMatrixDirty = true;
14112 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14115 }
14116
14117 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14118 // If we are visible, force the DRAWN bit to on so that
14119 // this invalidate will go through (at least to our parent).
14120 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014121 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014122 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014123 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014124 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014125 // parent display list may need to be recreated based on a change in the bounds
14126 // of any child
14127 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 }
14129
14130 // Reset drawn bit to original value (invalidate turns it off)
14131 mPrivateFlags |= drawn;
14132
14133 mBackgroundSizeChanged = true;
14134 }
14135 return changed;
14136 }
14137
14138 /**
14139 * Finalize inflating a view from XML. This is called as the last phase
14140 * of inflation, after all child views have been added.
14141 *
14142 * <p>Even if the subclass overrides onFinishInflate, they should always be
14143 * sure to call the super method, so that we get called.
14144 */
14145 protected void onFinishInflate() {
14146 }
14147
14148 /**
14149 * Returns the resources associated with this view.
14150 *
14151 * @return Resources object.
14152 */
14153 public Resources getResources() {
14154 return mResources;
14155 }
14156
14157 /**
14158 * Invalidates the specified Drawable.
14159 *
14160 * @param drawable the drawable to invalidate
14161 */
14162 public void invalidateDrawable(Drawable drawable) {
14163 if (verifyDrawable(drawable)) {
14164 final Rect dirty = drawable.getBounds();
14165 final int scrollX = mScrollX;
14166 final int scrollY = mScrollY;
14167
14168 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14169 dirty.right + scrollX, dirty.bottom + scrollY);
14170 }
14171 }
14172
14173 /**
14174 * Schedules an action on a drawable to occur at a specified time.
14175 *
14176 * @param who the recipient of the action
14177 * @param what the action to run on the drawable
14178 * @param when the time at which the action must occur. Uses the
14179 * {@link SystemClock#uptimeMillis} timebase.
14180 */
14181 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014182 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014183 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014184 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014185 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14186 Choreographer.CALLBACK_ANIMATION, what, who,
14187 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014188 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014189 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 }
14192 }
14193
14194 /**
14195 * Cancels a scheduled action on a drawable.
14196 *
14197 * @param who the recipient of the action
14198 * @param what the action to cancel
14199 */
14200 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014201 if (verifyDrawable(who) && what != null) {
14202 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014203 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14204 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014205 } else {
14206 ViewRootImpl.getRunQueue().removeCallbacks(what);
14207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014208 }
14209 }
14210
14211 /**
14212 * Unschedule any events associated with the given Drawable. This can be
14213 * used when selecting a new Drawable into a view, so that the previous
14214 * one is completely unscheduled.
14215 *
14216 * @param who The Drawable to unschedule.
14217 *
14218 * @see #drawableStateChanged
14219 */
14220 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014221 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014222 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14223 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 }
14225 }
14226
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014227 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014228 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14229 * that the View directionality can and will be resolved before its Drawables.
14230 *
14231 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014232 *
14233 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014234 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014235 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014236 if (canResolveLayoutDirection()) {
14237 if (mBackground != null) {
14238 mBackground.setLayoutDirection(getLayoutDirection());
14239 }
14240 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14241 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014242 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014243 }
14244
14245 /**
14246 * Called when layout direction has been resolved.
14247 *
14248 * The default implementation does nothing.
14249 *
14250 * @param layoutDirection The resolved layout direction.
14251 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014252 * @see #LAYOUT_DIRECTION_LTR
14253 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014254 *
14255 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014256 */
14257 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014258 }
14259
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014260 /**
14261 * @hide
14262 */
14263 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014264 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14265 }
14266
14267 private boolean isDrawablesResolved() {
14268 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14269 }
14270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014271 /**
14272 * If your view subclass is displaying its own Drawable objects, it should
14273 * override this function and return true for any Drawable it is
14274 * displaying. This allows animations for those drawables to be
14275 * scheduled.
14276 *
14277 * <p>Be sure to call through to the super class when overriding this
14278 * function.
14279 *
14280 * @param who The Drawable to verify. Return true if it is one you are
14281 * displaying, else return the result of calling through to the
14282 * super class.
14283 *
14284 * @return boolean If true than the Drawable is being displayed in the
14285 * view; else false and it is not allowed to animate.
14286 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014287 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14288 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014289 */
14290 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014291 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014292 }
14293
14294 /**
14295 * This function is called whenever the state of the view changes in such
14296 * a way that it impacts the state of drawables being shown.
14297 *
14298 * <p>Be sure to call through to the superclass when overriding this
14299 * function.
14300 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014301 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 */
14303 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014304 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014305 if (d != null && d.isStateful()) {
14306 d.setState(getDrawableState());
14307 }
14308 }
14309
14310 /**
14311 * Call this to force a view to update its drawable state. This will cause
14312 * drawableStateChanged to be called on this view. Views that are interested
14313 * in the new state should call getDrawableState.
14314 *
14315 * @see #drawableStateChanged
14316 * @see #getDrawableState
14317 */
14318 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014319 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 drawableStateChanged();
14321
14322 ViewParent parent = mParent;
14323 if (parent != null) {
14324 parent.childDrawableStateChanged(this);
14325 }
14326 }
14327
14328 /**
14329 * Return an array of resource IDs of the drawable states representing the
14330 * current state of the view.
14331 *
14332 * @return The current drawable state
14333 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014334 * @see Drawable#setState(int[])
14335 * @see #drawableStateChanged()
14336 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014337 */
14338 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014339 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014340 return mDrawableState;
14341 } else {
14342 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014343 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014344 return mDrawableState;
14345 }
14346 }
14347
14348 /**
14349 * Generate the new {@link android.graphics.drawable.Drawable} state for
14350 * this view. This is called by the view
14351 * system when the cached Drawable state is determined to be invalid. To
14352 * retrieve the current state, you should use {@link #getDrawableState}.
14353 *
14354 * @param extraSpace if non-zero, this is the number of extra entries you
14355 * would like in the returned array in which you can place your own
14356 * states.
14357 *
14358 * @return Returns an array holding the current {@link Drawable} state of
14359 * the view.
14360 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014361 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362 */
14363 protected int[] onCreateDrawableState(int extraSpace) {
14364 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14365 mParent instanceof View) {
14366 return ((View) mParent).onCreateDrawableState(extraSpace);
14367 }
14368
14369 int[] drawableState;
14370
14371 int privateFlags = mPrivateFlags;
14372
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014373 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014374 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014375 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14376 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014377 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014378 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014379 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014380 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14381 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014382 // This is set if HW acceleration is requested, even if the current
14383 // process doesn't allow it. This is just to allow app preview
14384 // windows to better match their app.
14385 viewStateIndex |= VIEW_STATE_ACCELERATED;
14386 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014387 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014388
Christopher Tate3d4bf172011-03-28 16:16:46 -070014389 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014390 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14391 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014393 drawableState = VIEW_STATE_SETS[viewStateIndex];
14394
14395 //noinspection ConstantIfStatement
14396 if (false) {
14397 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14398 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014399 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014400 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14401 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014402 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014403 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014404 + ": " + Arrays.toString(drawableState));
14405 }
14406
14407 if (extraSpace == 0) {
14408 return drawableState;
14409 }
14410
14411 final int[] fullState;
14412 if (drawableState != null) {
14413 fullState = new int[drawableState.length + extraSpace];
14414 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14415 } else {
14416 fullState = new int[extraSpace];
14417 }
14418
14419 return fullState;
14420 }
14421
14422 /**
14423 * Merge your own state values in <var>additionalState</var> into the base
14424 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014425 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 *
14427 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014428 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014429 * own additional state values.
14430 *
14431 * @param additionalState The additional state values you would like
14432 * added to <var>baseState</var>; this array is not modified.
14433 *
14434 * @return As a convenience, the <var>baseState</var> array you originally
14435 * passed into the function is returned.
14436 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014437 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014438 */
14439 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14440 final int N = baseState.length;
14441 int i = N - 1;
14442 while (i >= 0 && baseState[i] == 0) {
14443 i--;
14444 }
14445 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14446 return baseState;
14447 }
14448
14449 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014450 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14451 * on all Drawable objects associated with this view.
14452 */
14453 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014454 if (mBackground != null) {
14455 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014456 }
14457 }
14458
14459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014460 * Sets the background color for this view.
14461 * @param color the color of the background
14462 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014463 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014464 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014465 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014466 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014467 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014468 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014469 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014470 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014472 }
14473
14474 /**
14475 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014476 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014478 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 * @attr ref android.R.styleable#View_background
14480 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014481 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014482 public void setBackgroundResource(int resid) {
14483 if (resid != 0 && resid == mBackgroundResource) {
14484 return;
14485 }
14486
14487 Drawable d= null;
14488 if (resid != 0) {
14489 d = mResources.getDrawable(resid);
14490 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014491 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014492
14493 mBackgroundResource = resid;
14494 }
14495
14496 /**
14497 * Set the background to a given Drawable, or remove the background. If the
14498 * background has padding, this View's padding is set to the background's
14499 * padding. However, when a background is removed, this View's padding isn't
14500 * touched. If setting the padding is desired, please use
14501 * {@link #setPadding(int, int, int, int)}.
14502 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014503 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 * background
14505 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014506 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014507 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014508 setBackgroundDrawable(background);
14509 }
14510
14511 /**
14512 * @deprecated use {@link #setBackground(Drawable)} instead
14513 */
14514 @Deprecated
14515 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014516 computeOpaqueFlags();
14517
Philip Milne6c8ea062012-04-03 17:38:43 -070014518 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014519 return;
14520 }
14521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014522 boolean requestLayout = false;
14523
14524 mBackgroundResource = 0;
14525
14526 /*
14527 * Regardless of whether we're setting a new background or not, we want
14528 * to clear the previous drawable.
14529 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014530 if (mBackground != null) {
14531 mBackground.setCallback(null);
14532 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 }
14534
Philip Milne6c8ea062012-04-03 17:38:43 -070014535 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014536 Rect padding = sThreadLocal.get();
14537 if (padding == null) {
14538 padding = new Rect();
14539 sThreadLocal.set(padding);
14540 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014541 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014542 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014543 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014544 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014545 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014546 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014547 mUserPaddingLeftInitial = padding.right;
14548 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014549 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014550 break;
14551 case LAYOUT_DIRECTION_LTR:
14552 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014553 mUserPaddingLeftInitial = padding.left;
14554 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014555 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014557 }
14558
14559 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14560 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014561 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14562 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014563 requestLayout = true;
14564 }
14565
Philip Milne6c8ea062012-04-03 17:38:43 -070014566 background.setCallback(this);
14567 if (background.isStateful()) {
14568 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014570 background.setVisible(getVisibility() == VISIBLE, false);
14571 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014572
Dianne Hackborn4702a852012-08-17 15:18:29 -070014573 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14574 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14575 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014576 requestLayout = true;
14577 }
14578 } else {
14579 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014580 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014581
Dianne Hackborn4702a852012-08-17 15:18:29 -070014582 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014583 /*
14584 * This view ONLY drew the background before and we're removing
14585 * the background, so now it won't draw anything
14586 * (hence we SKIP_DRAW)
14587 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014588 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14589 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 }
14591
14592 /*
14593 * When the background is set, we try to apply its padding to this
14594 * View. When the background is removed, we don't touch this View's
14595 * padding. This is noted in the Javadocs. Hence, we don't need to
14596 * requestLayout(), the invalidate() below is sufficient.
14597 */
14598
14599 // The old background's minimum size could have affected this
14600 // View's layout, so let's requestLayout
14601 requestLayout = true;
14602 }
14603
Romain Guy8f1344f52009-05-15 16:03:59 -070014604 computeOpaqueFlags();
14605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014606 if (requestLayout) {
14607 requestLayout();
14608 }
14609
14610 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014611 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014612 }
14613
14614 /**
14615 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014616 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014617 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014618 *
14619 * @see #setBackground(Drawable)
14620 *
14621 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014622 */
14623 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014624 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014625 }
14626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014627 /**
14628 * Sets the padding. The view may add on the space required to display
14629 * the scrollbars, depending on the style and visibility of the scrollbars.
14630 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14631 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14632 * from the values set in this call.
14633 *
14634 * @attr ref android.R.styleable#View_padding
14635 * @attr ref android.R.styleable#View_paddingBottom
14636 * @attr ref android.R.styleable#View_paddingLeft
14637 * @attr ref android.R.styleable#View_paddingRight
14638 * @attr ref android.R.styleable#View_paddingTop
14639 * @param left the left padding in pixels
14640 * @param top the top padding in pixels
14641 * @param right the right padding in pixels
14642 * @param bottom the bottom padding in pixels
14643 */
14644 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014645 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014646
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014647 mUserPaddingStart = UNDEFINED_PADDING;
14648 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014649
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014650 mUserPaddingLeftInitial = left;
14651 mUserPaddingRightInitial = right;
14652
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014653 internalSetPadding(left, top, right, bottom);
14654 }
14655
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014656 /**
14657 * @hide
14658 */
14659 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014660 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014661 mUserPaddingRight = right;
14662 mUserPaddingBottom = bottom;
14663
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014664 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014665 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014666
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014667 // Common case is there are no scroll bars.
14668 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014669 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014670 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014671 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014672 switch (mVerticalScrollbarPosition) {
14673 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014674 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014675 left += offset;
14676 } else {
14677 right += offset;
14678 }
14679 break;
Adam Powell20232d02010-12-08 21:08:53 -080014680 case SCROLLBAR_POSITION_RIGHT:
14681 right += offset;
14682 break;
14683 case SCROLLBAR_POSITION_LEFT:
14684 left += offset;
14685 break;
14686 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014687 }
Adam Powell20232d02010-12-08 21:08:53 -080014688 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014689 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14690 ? 0 : getHorizontalScrollbarHeight();
14691 }
14692 }
Romain Guy8506ab42009-06-11 17:35:47 -070014693
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014694 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014695 changed = true;
14696 mPaddingLeft = left;
14697 }
14698 if (mPaddingTop != top) {
14699 changed = true;
14700 mPaddingTop = top;
14701 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014702 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014703 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014704 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014705 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014706 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014707 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014708 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014709 }
14710
14711 if (changed) {
14712 requestLayout();
14713 }
14714 }
14715
14716 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014717 * Sets the relative padding. The view may add on the space required to display
14718 * the scrollbars, depending on the style and visibility of the scrollbars.
14719 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14720 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14721 * from the values set in this call.
14722 *
14723 * @attr ref android.R.styleable#View_padding
14724 * @attr ref android.R.styleable#View_paddingBottom
14725 * @attr ref android.R.styleable#View_paddingStart
14726 * @attr ref android.R.styleable#View_paddingEnd
14727 * @attr ref android.R.styleable#View_paddingTop
14728 * @param start the start padding in pixels
14729 * @param top the top padding in pixels
14730 * @param end the end padding in pixels
14731 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014732 */
14733 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014734 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014735
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014736 mUserPaddingStart = start;
14737 mUserPaddingEnd = end;
14738
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014739 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014740 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014741 mUserPaddingLeftInitial = end;
14742 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014743 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014744 break;
14745 case LAYOUT_DIRECTION_LTR:
14746 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014747 mUserPaddingLeftInitial = start;
14748 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014749 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014750 }
14751 }
14752
14753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014754 * Returns the top padding of this view.
14755 *
14756 * @return the top padding in pixels
14757 */
14758 public int getPaddingTop() {
14759 return mPaddingTop;
14760 }
14761
14762 /**
14763 * Returns the bottom padding of this view. If there are inset and enabled
14764 * scrollbars, this value may include the space required to display the
14765 * scrollbars as well.
14766 *
14767 * @return the bottom padding in pixels
14768 */
14769 public int getPaddingBottom() {
14770 return mPaddingBottom;
14771 }
14772
14773 /**
14774 * Returns the left padding of this view. If there are inset and enabled
14775 * scrollbars, this value may include the space required to display the
14776 * scrollbars as well.
14777 *
14778 * @return the left padding in pixels
14779 */
14780 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014781 if (!isPaddingResolved()) {
14782 resolvePadding();
14783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014784 return mPaddingLeft;
14785 }
14786
14787 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014788 * Returns the start padding of this view depending on its resolved layout direction.
14789 * If there are inset and enabled scrollbars, this value may include the space
14790 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014791 *
14792 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014793 */
14794 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014795 if (!isPaddingResolved()) {
14796 resolvePadding();
14797 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014798 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014799 mPaddingRight : mPaddingLeft;
14800 }
14801
14802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014803 * Returns the right padding of this view. If there are inset and enabled
14804 * scrollbars, this value may include the space required to display the
14805 * scrollbars as well.
14806 *
14807 * @return the right padding in pixels
14808 */
14809 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014810 if (!isPaddingResolved()) {
14811 resolvePadding();
14812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014813 return mPaddingRight;
14814 }
14815
14816 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014817 * Returns the end padding of this view depending on its resolved layout direction.
14818 * If there are inset and enabled scrollbars, this value may include the space
14819 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014820 *
14821 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014822 */
14823 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014824 if (!isPaddingResolved()) {
14825 resolvePadding();
14826 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014827 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014828 mPaddingLeft : mPaddingRight;
14829 }
14830
14831 /**
14832 * Return if the padding as been set thru relative values
14833 * {@link #setPaddingRelative(int, int, int, int)} or thru
14834 * @attr ref android.R.styleable#View_paddingStart or
14835 * @attr ref android.R.styleable#View_paddingEnd
14836 *
14837 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014838 */
14839 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014840 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014841 }
14842
Philip Milne7b757812012-09-19 18:13:44 -070014843 Insets computeOpticalInsets() {
14844 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014845 }
14846
14847 /**
14848 * @hide
14849 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014850 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014851 if (isRtlCompatibilityMode()) {
14852 mPaddingLeft = mUserPaddingLeftInitial;
14853 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014854 return;
14855 }
14856 if (isLayoutRtl()) {
14857 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14858 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014859 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014860 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14861 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014862 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014863 }
14864
14865 /**
14866 * @hide
14867 */
Philip Milne7b757812012-09-19 18:13:44 -070014868 public Insets getOpticalInsets() {
14869 if (mLayoutInsets == null) {
14870 mLayoutInsets = computeOpticalInsets();
14871 }
14872 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070014873 }
14874
14875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014876 * Changes the selection state of this view. A view can be selected or not.
14877 * Note that selection is not the same as focus. Views are typically
14878 * selected in the context of an AdapterView like ListView or GridView;
14879 * the selected view is the view that is highlighted.
14880 *
14881 * @param selected true if the view must be selected, false otherwise
14882 */
14883 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014884 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14885 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014886 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014887 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 refreshDrawableState();
14889 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014890 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14891 notifyAccessibilityStateChanged();
14892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014893 }
14894 }
14895
14896 /**
14897 * Dispatch setSelected to all of this View's children.
14898 *
14899 * @see #setSelected(boolean)
14900 *
14901 * @param selected The new selected state
14902 */
14903 protected void dispatchSetSelected(boolean selected) {
14904 }
14905
14906 /**
14907 * Indicates the selection state of this view.
14908 *
14909 * @return true if the view is selected, false otherwise
14910 */
14911 @ViewDebug.ExportedProperty
14912 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014913 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014914 }
14915
14916 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014917 * Changes the activated state of this view. A view can be activated or not.
14918 * Note that activation is not the same as selection. Selection is
14919 * a transient property, representing the view (hierarchy) the user is
14920 * currently interacting with. Activation is a longer-term state that the
14921 * user can move views in and out of. For example, in a list view with
14922 * single or multiple selection enabled, the views in the current selection
14923 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14924 * here.) The activated state is propagated down to children of the view it
14925 * is set on.
14926 *
14927 * @param activated true if the view must be activated, false otherwise
14928 */
14929 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014930 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14931 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014932 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014933 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014934 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014935 }
14936 }
14937
14938 /**
14939 * Dispatch setActivated to all of this View's children.
14940 *
14941 * @see #setActivated(boolean)
14942 *
14943 * @param activated The new activated state
14944 */
14945 protected void dispatchSetActivated(boolean activated) {
14946 }
14947
14948 /**
14949 * Indicates the activation state of this view.
14950 *
14951 * @return true if the view is activated, false otherwise
14952 */
14953 @ViewDebug.ExportedProperty
14954 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014955 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014956 }
14957
14958 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014959 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14960 * observer can be used to get notifications when global events, like
14961 * layout, happen.
14962 *
14963 * The returned ViewTreeObserver observer is not guaranteed to remain
14964 * valid for the lifetime of this View. If the caller of this method keeps
14965 * a long-lived reference to ViewTreeObserver, it should always check for
14966 * the return value of {@link ViewTreeObserver#isAlive()}.
14967 *
14968 * @return The ViewTreeObserver for this view's hierarchy.
14969 */
14970 public ViewTreeObserver getViewTreeObserver() {
14971 if (mAttachInfo != null) {
14972 return mAttachInfo.mTreeObserver;
14973 }
14974 if (mFloatingTreeObserver == null) {
14975 mFloatingTreeObserver = new ViewTreeObserver();
14976 }
14977 return mFloatingTreeObserver;
14978 }
14979
14980 /**
14981 * <p>Finds the topmost view in the current view hierarchy.</p>
14982 *
14983 * @return the topmost view containing this view
14984 */
14985 public View getRootView() {
14986 if (mAttachInfo != null) {
14987 final View v = mAttachInfo.mRootView;
14988 if (v != null) {
14989 return v;
14990 }
14991 }
Romain Guy8506ab42009-06-11 17:35:47 -070014992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014993 View parent = this;
14994
14995 while (parent.mParent != null && parent.mParent instanceof View) {
14996 parent = (View) parent.mParent;
14997 }
14998
14999 return parent;
15000 }
15001
15002 /**
15003 * <p>Computes the coordinates of this view on the screen. The argument
15004 * must be an array of two integers. After the method returns, the array
15005 * contains the x and y location in that order.</p>
15006 *
15007 * @param location an array of two integers in which to hold the coordinates
15008 */
15009 public void getLocationOnScreen(int[] location) {
15010 getLocationInWindow(location);
15011
15012 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015013 if (info != null) {
15014 location[0] += info.mWindowLeft;
15015 location[1] += info.mWindowTop;
15016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015017 }
15018
15019 /**
15020 * <p>Computes the coordinates of this view in its window. The argument
15021 * must be an array of two integers. After the method returns, the array
15022 * contains the x and y location in that order.</p>
15023 *
15024 * @param location an array of two integers in which to hold the coordinates
15025 */
15026 public void getLocationInWindow(int[] location) {
15027 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015028 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015029 }
15030
Gilles Debunne6583ce52011-12-06 18:09:02 -080015031 if (mAttachInfo == null) {
15032 // When the view is not attached to a window, this method does not make sense
15033 location[0] = location[1] = 0;
15034 return;
15035 }
15036
Gilles Debunnecea45132011-11-24 02:19:27 +010015037 float[] position = mAttachInfo.mTmpTransformLocation;
15038 position[0] = position[1] = 0.0f;
15039
15040 if (!hasIdentityMatrix()) {
15041 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015043
Gilles Debunnecea45132011-11-24 02:19:27 +010015044 position[0] += mLeft;
15045 position[1] += mTop;
15046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015047 ViewParent viewParent = mParent;
15048 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015049 final View view = (View) viewParent;
15050
15051 position[0] -= view.mScrollX;
15052 position[1] -= view.mScrollY;
15053
15054 if (!view.hasIdentityMatrix()) {
15055 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015056 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015057
15058 position[0] += view.mLeft;
15059 position[1] += view.mTop;
15060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015061 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015062 }
Romain Guy8506ab42009-06-11 17:35:47 -070015063
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015064 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015066 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15067 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015068 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015069
15070 location[0] = (int) (position[0] + 0.5f);
15071 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015072 }
15073
15074 /**
15075 * {@hide}
15076 * @param id the id of the view to be found
15077 * @return the view of the specified id, null if cannot be found
15078 */
15079 protected View findViewTraversal(int id) {
15080 if (id == mID) {
15081 return this;
15082 }
15083 return null;
15084 }
15085
15086 /**
15087 * {@hide}
15088 * @param tag the tag of the view to be found
15089 * @return the view of specified tag, null if cannot be found
15090 */
15091 protected View findViewWithTagTraversal(Object tag) {
15092 if (tag != null && tag.equals(mTag)) {
15093 return this;
15094 }
15095 return null;
15096 }
15097
15098 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015099 * {@hide}
15100 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015101 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015102 * @return The first view that matches the predicate or null.
15103 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015104 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015105 if (predicate.apply(this)) {
15106 return this;
15107 }
15108 return null;
15109 }
15110
15111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015112 * Look for a child view with the given id. If this view has the given
15113 * id, return this view.
15114 *
15115 * @param id The id to search for.
15116 * @return The view that has the given id in the hierarchy or null
15117 */
15118 public final View findViewById(int id) {
15119 if (id < 0) {
15120 return null;
15121 }
15122 return findViewTraversal(id);
15123 }
15124
15125 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015126 * Finds a view by its unuque and stable accessibility id.
15127 *
15128 * @param accessibilityId The searched accessibility id.
15129 * @return The found view.
15130 */
15131 final View findViewByAccessibilityId(int accessibilityId) {
15132 if (accessibilityId < 0) {
15133 return null;
15134 }
15135 return findViewByAccessibilityIdTraversal(accessibilityId);
15136 }
15137
15138 /**
15139 * Performs the traversal to find a view by its unuque and stable accessibility id.
15140 *
15141 * <strong>Note:</strong>This method does not stop at the root namespace
15142 * boundary since the user can touch the screen at an arbitrary location
15143 * potentially crossing the root namespace bounday which will send an
15144 * accessibility event to accessibility services and they should be able
15145 * to obtain the event source. Also accessibility ids are guaranteed to be
15146 * unique in the window.
15147 *
15148 * @param accessibilityId The accessibility id.
15149 * @return The found view.
15150 */
15151 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15152 if (getAccessibilityViewId() == accessibilityId) {
15153 return this;
15154 }
15155 return null;
15156 }
15157
15158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015159 * Look for a child view with the given tag. If this view has the given
15160 * tag, return this view.
15161 *
15162 * @param tag The tag to search for, using "tag.equals(getTag())".
15163 * @return The View that has the given tag in the hierarchy or null
15164 */
15165 public final View findViewWithTag(Object tag) {
15166 if (tag == null) {
15167 return null;
15168 }
15169 return findViewWithTagTraversal(tag);
15170 }
15171
15172 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015173 * {@hide}
15174 * Look for a child view that matches the specified predicate.
15175 * If this view matches the predicate, return this view.
15176 *
15177 * @param predicate The predicate to evaluate.
15178 * @return The first view that matches the predicate or null.
15179 */
15180 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015181 return findViewByPredicateTraversal(predicate, null);
15182 }
15183
15184 /**
15185 * {@hide}
15186 * Look for a child view that matches the specified predicate,
15187 * starting with the specified view and its descendents and then
15188 * recusively searching the ancestors and siblings of that view
15189 * until this view is reached.
15190 *
15191 * This method is useful in cases where the predicate does not match
15192 * a single unique view (perhaps multiple views use the same id)
15193 * and we are trying to find the view that is "closest" in scope to the
15194 * starting view.
15195 *
15196 * @param start The view to start from.
15197 * @param predicate The predicate to evaluate.
15198 * @return The first view that matches the predicate or null.
15199 */
15200 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15201 View childToSkip = null;
15202 for (;;) {
15203 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15204 if (view != null || start == this) {
15205 return view;
15206 }
15207
15208 ViewParent parent = start.getParent();
15209 if (parent == null || !(parent instanceof View)) {
15210 return null;
15211 }
15212
15213 childToSkip = start;
15214 start = (View) parent;
15215 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015216 }
15217
15218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015219 * Sets the identifier for this view. The identifier does not have to be
15220 * unique in this view's hierarchy. The identifier should be a positive
15221 * number.
15222 *
15223 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015224 * @see #getId()
15225 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015226 *
15227 * @param id a number used to identify the view
15228 *
15229 * @attr ref android.R.styleable#View_id
15230 */
15231 public void setId(int id) {
15232 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015233 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15234 mID = generateViewId();
15235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 }
15237
15238 /**
15239 * {@hide}
15240 *
15241 * @param isRoot true if the view belongs to the root namespace, false
15242 * otherwise
15243 */
15244 public void setIsRootNamespace(boolean isRoot) {
15245 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015246 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015247 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015248 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015249 }
15250 }
15251
15252 /**
15253 * {@hide}
15254 *
15255 * @return true if the view belongs to the root namespace, false otherwise
15256 */
15257 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015258 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015259 }
15260
15261 /**
15262 * Returns this view's identifier.
15263 *
15264 * @return a positive integer used to identify the view or {@link #NO_ID}
15265 * if the view has no ID
15266 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015267 * @see #setId(int)
15268 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015269 * @attr ref android.R.styleable#View_id
15270 */
15271 @ViewDebug.CapturedViewProperty
15272 public int getId() {
15273 return mID;
15274 }
15275
15276 /**
15277 * Returns this view's tag.
15278 *
15279 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015280 *
15281 * @see #setTag(Object)
15282 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015283 */
15284 @ViewDebug.ExportedProperty
15285 public Object getTag() {
15286 return mTag;
15287 }
15288
15289 /**
15290 * Sets the tag associated with this view. A tag can be used to mark
15291 * a view in its hierarchy and does not have to be unique within the
15292 * hierarchy. Tags can also be used to store data within a view without
15293 * resorting to another data structure.
15294 *
15295 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015296 *
15297 * @see #getTag()
15298 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015299 */
15300 public void setTag(final Object tag) {
15301 mTag = tag;
15302 }
15303
15304 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015305 * Returns the tag associated with this view and the specified key.
15306 *
15307 * @param key The key identifying the tag
15308 *
15309 * @return the Object stored in this view as a tag
15310 *
15311 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015312 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015313 */
15314 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015315 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015316 return null;
15317 }
15318
15319 /**
15320 * Sets a tag associated with this view and a key. A tag can be used
15321 * to mark a view in its hierarchy and does not have to be unique within
15322 * the hierarchy. Tags can also be used to store data within a view
15323 * without resorting to another data structure.
15324 *
15325 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015326 * application to ensure it is unique (see the <a
15327 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15328 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015329 * the Android framework or not associated with any package will cause
15330 * an {@link IllegalArgumentException} to be thrown.
15331 *
15332 * @param key The key identifying the tag
15333 * @param tag An Object to tag the view with
15334 *
15335 * @throws IllegalArgumentException If they specified key is not valid
15336 *
15337 * @see #setTag(Object)
15338 * @see #getTag(int)
15339 */
15340 public void setTag(int key, final Object tag) {
15341 // If the package id is 0x00 or 0x01, it's either an undefined package
15342 // or a framework id
15343 if ((key >>> 24) < 2) {
15344 throw new IllegalArgumentException("The key must be an application-specific "
15345 + "resource id.");
15346 }
15347
Adam Powell2b2f6d62011-09-23 11:15:39 -070015348 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015349 }
15350
15351 /**
15352 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15353 * framework id.
15354 *
15355 * @hide
15356 */
15357 public void setTagInternal(int key, Object tag) {
15358 if ((key >>> 24) != 0x1) {
15359 throw new IllegalArgumentException("The key must be a framework-specific "
15360 + "resource id.");
15361 }
15362
Adam Powell2b2f6d62011-09-23 11:15:39 -070015363 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015364 }
15365
Adam Powell2b2f6d62011-09-23 11:15:39 -070015366 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015367 if (mKeyedTags == null) {
15368 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015369 }
15370
Adam Powell7db82ac2011-09-22 19:44:04 -070015371 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015372 }
15373
15374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015375 * Prints information about this view in the log output, with the tag
15376 * {@link #VIEW_LOG_TAG}.
15377 *
15378 * @hide
15379 */
15380 public void debug() {
15381 debug(0);
15382 }
15383
15384 /**
15385 * Prints information about this view in the log output, with the tag
15386 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15387 * indentation defined by the <code>depth</code>.
15388 *
15389 * @param depth the indentation level
15390 *
15391 * @hide
15392 */
15393 protected void debug(int depth) {
15394 String output = debugIndent(depth - 1);
15395
15396 output += "+ " + this;
15397 int id = getId();
15398 if (id != -1) {
15399 output += " (id=" + id + ")";
15400 }
15401 Object tag = getTag();
15402 if (tag != null) {
15403 output += " (tag=" + tag + ")";
15404 }
15405 Log.d(VIEW_LOG_TAG, output);
15406
Dianne Hackborn4702a852012-08-17 15:18:29 -070015407 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015408 output = debugIndent(depth) + " FOCUSED";
15409 Log.d(VIEW_LOG_TAG, output);
15410 }
15411
15412 output = debugIndent(depth);
15413 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15414 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15415 + "} ";
15416 Log.d(VIEW_LOG_TAG, output);
15417
15418 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15419 || mPaddingBottom != 0) {
15420 output = debugIndent(depth);
15421 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15422 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15423 Log.d(VIEW_LOG_TAG, output);
15424 }
15425
15426 output = debugIndent(depth);
15427 output += "mMeasureWidth=" + mMeasuredWidth +
15428 " mMeasureHeight=" + mMeasuredHeight;
15429 Log.d(VIEW_LOG_TAG, output);
15430
15431 output = debugIndent(depth);
15432 if (mLayoutParams == null) {
15433 output += "BAD! no layout params";
15434 } else {
15435 output = mLayoutParams.debug(output);
15436 }
15437 Log.d(VIEW_LOG_TAG, output);
15438
15439 output = debugIndent(depth);
15440 output += "flags={";
15441 output += View.printFlags(mViewFlags);
15442 output += "}";
15443 Log.d(VIEW_LOG_TAG, output);
15444
15445 output = debugIndent(depth);
15446 output += "privateFlags={";
15447 output += View.printPrivateFlags(mPrivateFlags);
15448 output += "}";
15449 Log.d(VIEW_LOG_TAG, output);
15450 }
15451
15452 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015453 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015454 *
15455 * @param depth the indentation level
15456 * @return a String containing (depth * 2 + 3) * 2 white spaces
15457 *
15458 * @hide
15459 */
15460 protected static String debugIndent(int depth) {
15461 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15462 for (int i = 0; i < (depth * 2) + 3; i++) {
15463 spaces.append(' ').append(' ');
15464 }
15465 return spaces.toString();
15466 }
15467
15468 /**
15469 * <p>Return the offset of the widget's text baseline from the widget's top
15470 * boundary. If this widget does not support baseline alignment, this
15471 * method returns -1. </p>
15472 *
15473 * @return the offset of the baseline within the widget's bounds or -1
15474 * if baseline alignment is not supported
15475 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015476 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015477 public int getBaseline() {
15478 return -1;
15479 }
15480
15481 /**
Chet Haase97140572012-09-13 14:56:47 -070015482 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15483 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15484 * a layout pass.
15485 *
15486 * @return whether the view hierarchy is currently undergoing a layout pass
15487 */
15488 public boolean isInLayout() {
15489 ViewRootImpl viewRoot = getViewRootImpl();
15490 return (viewRoot != null && viewRoot.isInLayout());
15491 }
15492
15493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015494 * Call this when something has changed which has invalidated the
15495 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015496 * tree. This should not be called while the view hierarchy is currently in a layout
15497 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15498 * end of the current layout pass (and then layout will run again) or after the current
15499 * frame is drawn and the next layout occurs.
15500 *
15501 * <p>Subclasses which override this method should call the superclass method to
15502 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015503 */
15504 public void requestLayout() {
Chet Haase97140572012-09-13 14:56:47 -070015505 ViewRootImpl viewRoot = getViewRootImpl();
15506 if (viewRoot != null && viewRoot.isInLayout()) {
15507 viewRoot.requestLayoutDuringLayout(this);
15508 return;
15509 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015510 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15511 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015512
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015513 if (mParent != null && !mParent.isLayoutRequested()) {
15514 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 }
15516 }
15517
15518 /**
15519 * Forces this view to be laid out during the next layout pass.
15520 * This method does not call requestLayout() or forceLayout()
15521 * on the parent.
15522 */
15523 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015524 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15525 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526 }
15527
15528 /**
15529 * <p>
15530 * This is called to find out how big a view should be. The parent
15531 * supplies constraint information in the width and height parameters.
15532 * </p>
15533 *
15534 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015535 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015536 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015537 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015538 * </p>
15539 *
15540 *
15541 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15542 * parent
15543 * @param heightMeasureSpec Vertical space requirements as imposed by the
15544 * parent
15545 *
15546 * @see #onMeasure(int, int)
15547 */
15548 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015549 boolean optical = isLayoutModeOptical(this);
15550 if (optical != isLayoutModeOptical(mParent)) {
15551 Insets insets = getOpticalInsets();
15552 int oWidth = insets.left + insets.right;
15553 int oHeight = insets.top + insets.bottom;
15554 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15555 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15556 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015557 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015558 widthMeasureSpec != mOldWidthMeasureSpec ||
15559 heightMeasureSpec != mOldHeightMeasureSpec) {
15560
15561 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015562 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015563
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015564 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015566 // measure ourselves, this should set the measured dimension flag back
15567 onMeasure(widthMeasureSpec, heightMeasureSpec);
15568
15569 // flag not set, setMeasuredDimension() was not invoked, we raise
15570 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015571 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015572 throw new IllegalStateException("onMeasure() did not set the"
15573 + " measured dimension by calling"
15574 + " setMeasuredDimension()");
15575 }
15576
Dianne Hackborn4702a852012-08-17 15:18:29 -070015577 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015578 }
15579
15580 mOldWidthMeasureSpec = widthMeasureSpec;
15581 mOldHeightMeasureSpec = heightMeasureSpec;
15582 }
15583
15584 /**
15585 * <p>
15586 * Measure the view and its content to determine the measured width and the
15587 * measured height. This method is invoked by {@link #measure(int, int)} and
15588 * should be overriden by subclasses to provide accurate and efficient
15589 * measurement of their contents.
15590 * </p>
15591 *
15592 * <p>
15593 * <strong>CONTRACT:</strong> When overriding this method, you
15594 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15595 * measured width and height of this view. Failure to do so will trigger an
15596 * <code>IllegalStateException</code>, thrown by
15597 * {@link #measure(int, int)}. Calling the superclass'
15598 * {@link #onMeasure(int, int)} is a valid use.
15599 * </p>
15600 *
15601 * <p>
15602 * The base class implementation of measure defaults to the background size,
15603 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15604 * override {@link #onMeasure(int, int)} to provide better measurements of
15605 * their content.
15606 * </p>
15607 *
15608 * <p>
15609 * If this method is overridden, it is the subclass's responsibility to make
15610 * sure the measured height and width are at least the view's minimum height
15611 * and width ({@link #getSuggestedMinimumHeight()} and
15612 * {@link #getSuggestedMinimumWidth()}).
15613 * </p>
15614 *
15615 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15616 * The requirements are encoded with
15617 * {@link android.view.View.MeasureSpec}.
15618 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15619 * The requirements are encoded with
15620 * {@link android.view.View.MeasureSpec}.
15621 *
15622 * @see #getMeasuredWidth()
15623 * @see #getMeasuredHeight()
15624 * @see #setMeasuredDimension(int, int)
15625 * @see #getSuggestedMinimumHeight()
15626 * @see #getSuggestedMinimumWidth()
15627 * @see android.view.View.MeasureSpec#getMode(int)
15628 * @see android.view.View.MeasureSpec#getSize(int)
15629 */
15630 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15631 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15632 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15633 }
15634
15635 /**
15636 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15637 * measured width and measured height. Failing to do so will trigger an
15638 * exception at measurement time.</p>
15639 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015640 * @param measuredWidth The measured width of this view. May be a complex
15641 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15642 * {@link #MEASURED_STATE_TOO_SMALL}.
15643 * @param measuredHeight The measured height of this view. May be a complex
15644 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15645 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015646 */
15647 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015648 boolean optical = isLayoutModeOptical(this);
15649 if (optical != isLayoutModeOptical(mParent)) {
15650 Insets insets = getOpticalInsets();
15651 int opticalWidth = insets.left + insets.right;
15652 int opticalHeight = insets.top + insets.bottom;
15653
15654 measuredWidth += optical ? opticalWidth : -opticalWidth;
15655 measuredHeight += optical ? opticalHeight : -opticalHeight;
15656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015657 mMeasuredWidth = measuredWidth;
15658 mMeasuredHeight = measuredHeight;
15659
Dianne Hackborn4702a852012-08-17 15:18:29 -070015660 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015661 }
15662
15663 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015664 * Merge two states as returned by {@link #getMeasuredState()}.
15665 * @param curState The current state as returned from a view or the result
15666 * of combining multiple views.
15667 * @param newState The new view state to combine.
15668 * @return Returns a new integer reflecting the combination of the two
15669 * states.
15670 */
15671 public static int combineMeasuredStates(int curState, int newState) {
15672 return curState | newState;
15673 }
15674
15675 /**
15676 * Version of {@link #resolveSizeAndState(int, int, int)}
15677 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15678 */
15679 public static int resolveSize(int size, int measureSpec) {
15680 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15681 }
15682
15683 /**
15684 * Utility to reconcile a desired size and state, with constraints imposed
15685 * by a MeasureSpec. Will take the desired size, unless a different size
15686 * is imposed by the constraints. The returned value is a compound integer,
15687 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15688 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15689 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015690 *
15691 * @param size How big the view wants to be
15692 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015693 * @return Size information bit mask as defined by
15694 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015695 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015696 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015697 int result = size;
15698 int specMode = MeasureSpec.getMode(measureSpec);
15699 int specSize = MeasureSpec.getSize(measureSpec);
15700 switch (specMode) {
15701 case MeasureSpec.UNSPECIFIED:
15702 result = size;
15703 break;
15704 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015705 if (specSize < size) {
15706 result = specSize | MEASURED_STATE_TOO_SMALL;
15707 } else {
15708 result = size;
15709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015710 break;
15711 case MeasureSpec.EXACTLY:
15712 result = specSize;
15713 break;
15714 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015715 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015716 }
15717
15718 /**
15719 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015720 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015721 * by the MeasureSpec.
15722 *
15723 * @param size Default size for this view
15724 * @param measureSpec Constraints imposed by the parent
15725 * @return The size this view should be.
15726 */
15727 public static int getDefaultSize(int size, int measureSpec) {
15728 int result = size;
15729 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015730 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015731
15732 switch (specMode) {
15733 case MeasureSpec.UNSPECIFIED:
15734 result = size;
15735 break;
15736 case MeasureSpec.AT_MOST:
15737 case MeasureSpec.EXACTLY:
15738 result = specSize;
15739 break;
15740 }
15741 return result;
15742 }
15743
15744 /**
15745 * Returns the suggested minimum height that the view should use. This
15746 * returns the maximum of the view's minimum height
15747 * and the background's minimum height
15748 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15749 * <p>
15750 * When being used in {@link #onMeasure(int, int)}, the caller should still
15751 * ensure the returned height is within the requirements of the parent.
15752 *
15753 * @return The suggested minimum height of the view.
15754 */
15755 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015756 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015758 }
15759
15760 /**
15761 * Returns the suggested minimum width that the view should use. This
15762 * returns the maximum of the view's minimum width)
15763 * and the background's minimum width
15764 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15765 * <p>
15766 * When being used in {@link #onMeasure(int, int)}, the caller should still
15767 * ensure the returned width is within the requirements of the parent.
15768 *
15769 * @return The suggested minimum width of the view.
15770 */
15771 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015772 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015774
Philip Milne6c8ea062012-04-03 17:38:43 -070015775 /**
15776 * Returns the minimum height of the view.
15777 *
15778 * @return the minimum height the view will try to be.
15779 *
15780 * @see #setMinimumHeight(int)
15781 *
15782 * @attr ref android.R.styleable#View_minHeight
15783 */
15784 public int getMinimumHeight() {
15785 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015786 }
15787
15788 /**
15789 * Sets the minimum height of the view. It is not guaranteed the view will
15790 * be able to achieve this minimum height (for example, if its parent layout
15791 * constrains it with less available height).
15792 *
15793 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015794 *
15795 * @see #getMinimumHeight()
15796 *
15797 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015798 */
15799 public void setMinimumHeight(int minHeight) {
15800 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015801 requestLayout();
15802 }
15803
15804 /**
15805 * Returns the minimum width of the view.
15806 *
15807 * @return the minimum width the view will try to be.
15808 *
15809 * @see #setMinimumWidth(int)
15810 *
15811 * @attr ref android.R.styleable#View_minWidth
15812 */
15813 public int getMinimumWidth() {
15814 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015815 }
15816
15817 /**
15818 * Sets the minimum width of the view. It is not guaranteed the view will
15819 * be able to achieve this minimum width (for example, if its parent layout
15820 * constrains it with less available width).
15821 *
15822 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015823 *
15824 * @see #getMinimumWidth()
15825 *
15826 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015827 */
15828 public void setMinimumWidth(int minWidth) {
15829 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015830 requestLayout();
15831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015832 }
15833
15834 /**
15835 * Get the animation currently associated with this view.
15836 *
15837 * @return The animation that is currently playing or
15838 * scheduled to play for this view.
15839 */
15840 public Animation getAnimation() {
15841 return mCurrentAnimation;
15842 }
15843
15844 /**
15845 * Start the specified animation now.
15846 *
15847 * @param animation the animation to start now
15848 */
15849 public void startAnimation(Animation animation) {
15850 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15851 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015852 invalidateParentCaches();
15853 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015854 }
15855
15856 /**
15857 * Cancels any animations for this view.
15858 */
15859 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015860 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015861 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015863 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015864 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015865 }
15866
15867 /**
15868 * Sets the next animation to play for this view.
15869 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015870 * {@link #startAnimation(android.view.animation.Animation)} instead.
15871 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015872 * control over the start time and invalidation, but you
15873 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015874 * 2) the view's parent (which controls animations on its children)
15875 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015876 * start.
15877 *
15878 * @param animation The next animation, or null.
15879 */
15880 public void setAnimation(Animation animation) {
15881 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015883 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015884 // If the screen is off assume the animation start time is now instead of
15885 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15886 // would cause the animation to start when the screen turns back on
15887 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15888 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15889 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015891 animation.reset();
15892 }
15893 }
15894
15895 /**
15896 * Invoked by a parent ViewGroup to notify the start of the animation
15897 * currently associated with this view. If you override this method,
15898 * always call super.onAnimationStart();
15899 *
15900 * @see #setAnimation(android.view.animation.Animation)
15901 * @see #getAnimation()
15902 */
15903 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015904 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015905 }
15906
15907 /**
15908 * Invoked by a parent ViewGroup to notify the end of the animation
15909 * currently associated with this view. If you override this method,
15910 * always call super.onAnimationEnd();
15911 *
15912 * @see #setAnimation(android.view.animation.Animation)
15913 * @see #getAnimation()
15914 */
15915 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015916 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015917 }
15918
15919 /**
15920 * Invoked if there is a Transform that involves alpha. Subclass that can
15921 * draw themselves with the specified alpha should return true, and then
15922 * respect that alpha when their onDraw() is called. If this returns false
15923 * then the view may be redirected to draw into an offscreen buffer to
15924 * fulfill the request, which will look fine, but may be slower than if the
15925 * subclass handles it internally. The default implementation returns false.
15926 *
15927 * @param alpha The alpha (0..255) to apply to the view's drawing
15928 * @return true if the view can draw with the specified alpha.
15929 */
15930 protected boolean onSetAlpha(int alpha) {
15931 return false;
15932 }
15933
15934 /**
15935 * This is used by the RootView to perform an optimization when
15936 * the view hierarchy contains one or several SurfaceView.
15937 * SurfaceView is always considered transparent, but its children are not,
15938 * therefore all View objects remove themselves from the global transparent
15939 * region (passed as a parameter to this function).
15940 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015941 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015942 *
15943 * @return Returns true if the effective visibility of the view at this
15944 * point is opaque, regardless of the transparent region; returns false
15945 * if it is possible for underlying windows to be seen behind the view.
15946 *
15947 * {@hide}
15948 */
15949 public boolean gatherTransparentRegion(Region region) {
15950 final AttachInfo attachInfo = mAttachInfo;
15951 if (region != null && attachInfo != null) {
15952 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015953 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015954 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15955 // remove it from the transparent region.
15956 final int[] location = attachInfo.mTransparentLocation;
15957 getLocationInWindow(location);
15958 region.op(location[0], location[1], location[0] + mRight - mLeft,
15959 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015960 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015961 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15962 // exists, so we remove the background drawable's non-transparent
15963 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015964 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015965 }
15966 }
15967 return true;
15968 }
15969
15970 /**
15971 * Play a sound effect for this view.
15972 *
15973 * <p>The framework will play sound effects for some built in actions, such as
15974 * clicking, but you may wish to play these effects in your widget,
15975 * for instance, for internal navigation.
15976 *
15977 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15978 * {@link #isSoundEffectsEnabled()} is true.
15979 *
15980 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15981 */
15982 public void playSoundEffect(int soundConstant) {
15983 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15984 return;
15985 }
15986 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15987 }
15988
15989 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015990 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015991 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015992 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015993 *
15994 * <p>The framework will provide haptic feedback for some built in actions,
15995 * such as long presses, but you may wish to provide feedback for your
15996 * own widget.
15997 *
15998 * <p>The feedback will only be performed if
15999 * {@link #isHapticFeedbackEnabled()} is true.
16000 *
16001 * @param feedbackConstant One of the constants defined in
16002 * {@link HapticFeedbackConstants}
16003 */
16004 public boolean performHapticFeedback(int feedbackConstant) {
16005 return performHapticFeedback(feedbackConstant, 0);
16006 }
16007
16008 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016009 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016010 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016011 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016012 *
16013 * @param feedbackConstant One of the constants defined in
16014 * {@link HapticFeedbackConstants}
16015 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16016 */
16017 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16018 if (mAttachInfo == null) {
16019 return false;
16020 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016021 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016022 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016023 && !isHapticFeedbackEnabled()) {
16024 return false;
16025 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016026 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16027 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016028 }
16029
16030 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016031 * Request that the visibility of the status bar or other screen/window
16032 * decorations be changed.
16033 *
16034 * <p>This method is used to put the over device UI into temporary modes
16035 * where the user's attention is focused more on the application content,
16036 * by dimming or hiding surrounding system affordances. This is typically
16037 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16038 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16039 * to be placed behind the action bar (and with these flags other system
16040 * affordances) so that smooth transitions between hiding and showing them
16041 * can be done.
16042 *
16043 * <p>Two representative examples of the use of system UI visibility is
16044 * implementing a content browsing application (like a magazine reader)
16045 * and a video playing application.
16046 *
16047 * <p>The first code shows a typical implementation of a View in a content
16048 * browsing application. In this implementation, the application goes
16049 * into a content-oriented mode by hiding the status bar and action bar,
16050 * and putting the navigation elements into lights out mode. The user can
16051 * then interact with content while in this mode. Such an application should
16052 * provide an easy way for the user to toggle out of the mode (such as to
16053 * check information in the status bar or access notifications). In the
16054 * implementation here, this is done simply by tapping on the content.
16055 *
16056 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16057 * content}
16058 *
16059 * <p>This second code sample shows a typical implementation of a View
16060 * in a video playing application. In this situation, while the video is
16061 * playing the application would like to go into a complete full-screen mode,
16062 * to use as much of the display as possible for the video. When in this state
16063 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016064 * touching on the screen to pop the UI out of full screen mode. See
16065 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016066 *
16067 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16068 * content}
16069 *
16070 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16071 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16072 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16073 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016074 */
16075 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016076 if (visibility != mSystemUiVisibility) {
16077 mSystemUiVisibility = visibility;
16078 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16079 mParent.recomputeViewAttributes(this);
16080 }
Joe Onorato664644d2011-01-23 17:53:23 -080016081 }
16082 }
16083
16084 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016085 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16086 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16087 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16088 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16089 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016090 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016091 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016092 return mSystemUiVisibility;
16093 }
16094
Scott Mainec6331b2011-05-24 16:55:56 -070016095 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016096 * Returns the current system UI visibility that is currently set for
16097 * the entire window. This is the combination of the
16098 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16099 * views in the window.
16100 */
16101 public int getWindowSystemUiVisibility() {
16102 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16103 }
16104
16105 /**
16106 * Override to find out when the window's requested system UI visibility
16107 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16108 * This is different from the callbacks recieved through
16109 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16110 * in that this is only telling you about the local request of the window,
16111 * not the actual values applied by the system.
16112 */
16113 public void onWindowSystemUiVisibilityChanged(int visible) {
16114 }
16115
16116 /**
16117 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16118 * the view hierarchy.
16119 */
16120 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16121 onWindowSystemUiVisibilityChanged(visible);
16122 }
16123
16124 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016125 * Set a listener to receive callbacks when the visibility of the system bar changes.
16126 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16127 */
Joe Onorato664644d2011-01-23 17:53:23 -080016128 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016129 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016130 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16131 mParent.recomputeViewAttributes(this);
16132 }
16133 }
16134
16135 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016136 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16137 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016138 */
16139 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016140 ListenerInfo li = mListenerInfo;
16141 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16142 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016143 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016144 }
16145 }
16146
Dianne Hackborncf675782012-05-10 15:07:24 -070016147 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016148 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16149 if (val != mSystemUiVisibility) {
16150 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016151 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016152 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016153 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016154 }
16155
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016156 /** @hide */
16157 public void setDisabledSystemUiVisibility(int flags) {
16158 if (mAttachInfo != null) {
16159 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16160 mAttachInfo.mDisabledSystemUiVisibility = flags;
16161 if (mParent != null) {
16162 mParent.recomputeViewAttributes(this);
16163 }
16164 }
16165 }
16166 }
16167
Joe Onorato664644d2011-01-23 17:53:23 -080016168 /**
Joe Malin32736f02011-01-19 16:14:20 -080016169 * Creates an image that the system displays during the drag and drop
16170 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16171 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16172 * appearance as the given View. The default also positions the center of the drag shadow
16173 * directly under the touch point. If no View is provided (the constructor with no parameters
16174 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16175 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16176 * default is an invisible drag shadow.
16177 * <p>
16178 * You are not required to use the View you provide to the constructor as the basis of the
16179 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16180 * anything you want as the drag shadow.
16181 * </p>
16182 * <p>
16183 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16184 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16185 * size and position of the drag shadow. It uses this data to construct a
16186 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16187 * so that your application can draw the shadow image in the Canvas.
16188 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016189 *
16190 * <div class="special reference">
16191 * <h3>Developer Guides</h3>
16192 * <p>For a guide to implementing drag and drop features, read the
16193 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16194 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016195 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016196 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016197 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016198
16199 /**
Joe Malin32736f02011-01-19 16:14:20 -080016200 * Constructs a shadow image builder based on a View. By default, the resulting drag
16201 * shadow will have the same appearance and dimensions as the View, with the touch point
16202 * over the center of the View.
16203 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016204 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016205 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016206 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016207 }
16208
Christopher Tate17ed60c2011-01-18 12:50:26 -080016209 /**
16210 * Construct a shadow builder object with no associated View. This
16211 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16212 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16213 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016214 * reference to any View object. If they are not overridden, then the result is an
16215 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016216 */
16217 public DragShadowBuilder() {
16218 mView = new WeakReference<View>(null);
16219 }
16220
16221 /**
16222 * Returns the View object that had been passed to the
16223 * {@link #View.DragShadowBuilder(View)}
16224 * constructor. If that View parameter was {@code null} or if the
16225 * {@link #View.DragShadowBuilder()}
16226 * constructor was used to instantiate the builder object, this method will return
16227 * null.
16228 *
16229 * @return The View object associate with this builder object.
16230 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016231 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016232 final public View getView() {
16233 return mView.get();
16234 }
16235
Christopher Tate2c095f32010-10-04 14:13:40 -070016236 /**
Joe Malin32736f02011-01-19 16:14:20 -080016237 * Provides the metrics for the shadow image. These include the dimensions of
16238 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016239 * be centered under the touch location while dragging.
16240 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016241 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016242 * same as the dimensions of the View itself and centers the shadow under
16243 * the touch point.
16244 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016245 *
Joe Malin32736f02011-01-19 16:14:20 -080016246 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16247 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16248 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16249 * image.
16250 *
16251 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16252 * shadow image that should be underneath the touch point during the drag and drop
16253 * operation. Your application must set {@link android.graphics.Point#x} to the
16254 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016255 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016256 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016257 final View view = mView.get();
16258 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016259 shadowSize.set(view.getWidth(), view.getHeight());
16260 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016261 } else {
16262 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16263 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016264 }
16265
16266 /**
Joe Malin32736f02011-01-19 16:14:20 -080016267 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16268 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016269 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016270 *
Joe Malin32736f02011-01-19 16:14:20 -080016271 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016272 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016273 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016274 final View view = mView.get();
16275 if (view != null) {
16276 view.draw(canvas);
16277 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016278 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016279 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016280 }
16281 }
16282
16283 /**
Joe Malin32736f02011-01-19 16:14:20 -080016284 * Starts a drag and drop operation. When your application calls this method, it passes a
16285 * {@link android.view.View.DragShadowBuilder} object to the system. The
16286 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16287 * to get metrics for the drag shadow, and then calls the object's
16288 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16289 * <p>
16290 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16291 * drag events to all the View objects in your application that are currently visible. It does
16292 * this either by calling the View object's drag listener (an implementation of
16293 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16294 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16295 * Both are passed a {@link android.view.DragEvent} object that has a
16296 * {@link android.view.DragEvent#getAction()} value of
16297 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16298 * </p>
16299 * <p>
16300 * Your application can invoke startDrag() on any attached View object. The View object does not
16301 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16302 * be related to the View the user selected for dragging.
16303 * </p>
16304 * @param data A {@link android.content.ClipData} object pointing to the data to be
16305 * transferred by the drag and drop operation.
16306 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16307 * drag shadow.
16308 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16309 * drop operation. This Object is put into every DragEvent object sent by the system during the
16310 * current drag.
16311 * <p>
16312 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16313 * to the target Views. For example, it can contain flags that differentiate between a
16314 * a copy operation and a move operation.
16315 * </p>
16316 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16317 * so the parameter should be set to 0.
16318 * @return {@code true} if the method completes successfully, or
16319 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16320 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016321 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016322 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016323 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016324 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016325 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016326 }
16327 boolean okay = false;
16328
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016329 Point shadowSize = new Point();
16330 Point shadowTouchPoint = new Point();
16331 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016332
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016333 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16334 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16335 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016336 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016337
Chris Tatea32dcf72010-10-14 12:13:50 -070016338 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016339 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16340 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016341 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016342 Surface surface = new Surface();
16343 try {
16344 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016345 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016346 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016347 + " surface=" + surface);
16348 if (token != null) {
16349 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016350 try {
Chris Tate6b391282010-10-14 15:48:59 -070016351 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016352 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016353 } finally {
16354 surface.unlockCanvasAndPost(canvas);
16355 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016356
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016357 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016358
16359 // Cache the local state object for delivery with DragEvents
16360 root.setLocalDragState(myLocalState);
16361
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016362 // repurpose 'shadowSize' for the last touch point
16363 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016364
Christopher Tatea53146c2010-09-07 11:57:52 -070016365 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016366 shadowSize.x, shadowSize.y,
16367 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016368 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016369
16370 // Off and running! Release our local surface instance; the drag
16371 // shadow surface is now managed by the system process.
16372 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016373 }
16374 } catch (Exception e) {
16375 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16376 surface.destroy();
16377 }
16378
16379 return okay;
16380 }
16381
Christopher Tatea53146c2010-09-07 11:57:52 -070016382 /**
Joe Malin32736f02011-01-19 16:14:20 -080016383 * Handles drag events sent by the system following a call to
16384 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16385 *<p>
16386 * When the system calls this method, it passes a
16387 * {@link android.view.DragEvent} object. A call to
16388 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16389 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16390 * operation.
16391 * @param event The {@link android.view.DragEvent} sent by the system.
16392 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16393 * in DragEvent, indicating the type of drag event represented by this object.
16394 * @return {@code true} if the method was successful, otherwise {@code false}.
16395 * <p>
16396 * The method should return {@code true} in response to an action type of
16397 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16398 * operation.
16399 * </p>
16400 * <p>
16401 * The method should also return {@code true} in response to an action type of
16402 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16403 * {@code false} if it didn't.
16404 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016405 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016406 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016407 return false;
16408 }
16409
16410 /**
Joe Malin32736f02011-01-19 16:14:20 -080016411 * Detects if this View is enabled and has a drag event listener.
16412 * If both are true, then it calls the drag event listener with the
16413 * {@link android.view.DragEvent} it received. If the drag event listener returns
16414 * {@code true}, then dispatchDragEvent() returns {@code true}.
16415 * <p>
16416 * For all other cases, the method calls the
16417 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16418 * method and returns its result.
16419 * </p>
16420 * <p>
16421 * This ensures that a drag event is always consumed, even if the View does not have a drag
16422 * event listener. However, if the View has a listener and the listener returns true, then
16423 * onDragEvent() is not called.
16424 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016425 */
16426 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016427 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016428 ListenerInfo li = mListenerInfo;
16429 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16430 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016431 return true;
16432 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016433 return onDragEvent(event);
16434 }
16435
Christopher Tate3d4bf172011-03-28 16:16:46 -070016436 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016437 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016438 }
16439
Christopher Tatea53146c2010-09-07 11:57:52 -070016440 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016441 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16442 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016443 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016444 */
16445 public void onCloseSystemDialogs(String reason) {
16446 }
Joe Malin32736f02011-01-19 16:14:20 -080016447
Dianne Hackbornffa42482009-09-23 22:20:11 -070016448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016449 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016450 * update a Region being computed for
16451 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016452 * that any non-transparent parts of the Drawable are removed from the
16453 * given transparent region.
16454 *
16455 * @param dr The Drawable whose transparency is to be applied to the region.
16456 * @param region A Region holding the current transparency information,
16457 * where any parts of the region that are set are considered to be
16458 * transparent. On return, this region will be modified to have the
16459 * transparency information reduced by the corresponding parts of the
16460 * Drawable that are not transparent.
16461 * {@hide}
16462 */
16463 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16464 if (DBG) {
16465 Log.i("View", "Getting transparent region for: " + this);
16466 }
16467 final Region r = dr.getTransparentRegion();
16468 final Rect db = dr.getBounds();
16469 final AttachInfo attachInfo = mAttachInfo;
16470 if (r != null && attachInfo != null) {
16471 final int w = getRight()-getLeft();
16472 final int h = getBottom()-getTop();
16473 if (db.left > 0) {
16474 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16475 r.op(0, 0, db.left, h, Region.Op.UNION);
16476 }
16477 if (db.right < w) {
16478 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16479 r.op(db.right, 0, w, h, Region.Op.UNION);
16480 }
16481 if (db.top > 0) {
16482 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16483 r.op(0, 0, w, db.top, Region.Op.UNION);
16484 }
16485 if (db.bottom < h) {
16486 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16487 r.op(0, db.bottom, w, h, Region.Op.UNION);
16488 }
16489 final int[] location = attachInfo.mTransparentLocation;
16490 getLocationInWindow(location);
16491 r.translate(location[0], location[1]);
16492 region.op(r, Region.Op.INTERSECT);
16493 } else {
16494 region.op(db, Region.Op.DIFFERENCE);
16495 }
16496 }
16497
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016498 private void checkForLongClick(int delayOffset) {
16499 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16500 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016501
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016502 if (mPendingCheckForLongPress == null) {
16503 mPendingCheckForLongPress = new CheckForLongPress();
16504 }
16505 mPendingCheckForLongPress.rememberWindowAttachCount();
16506 postDelayed(mPendingCheckForLongPress,
16507 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016509 }
16510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016511 /**
16512 * Inflate a view from an XML resource. This convenience method wraps the {@link
16513 * LayoutInflater} class, which provides a full range of options for view inflation.
16514 *
16515 * @param context The Context object for your activity or application.
16516 * @param resource The resource ID to inflate
16517 * @param root A view group that will be the parent. Used to properly inflate the
16518 * layout_* parameters.
16519 * @see LayoutInflater
16520 */
16521 public static View inflate(Context context, int resource, ViewGroup root) {
16522 LayoutInflater factory = LayoutInflater.from(context);
16523 return factory.inflate(resource, root);
16524 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016526 /**
Adam Powell637d3372010-08-25 14:37:03 -070016527 * Scroll the view with standard behavior for scrolling beyond the normal
16528 * content boundaries. Views that call this method should override
16529 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16530 * results of an over-scroll operation.
16531 *
16532 * Views can use this method to handle any touch or fling-based scrolling.
16533 *
16534 * @param deltaX Change in X in pixels
16535 * @param deltaY Change in Y in pixels
16536 * @param scrollX Current X scroll value in pixels before applying deltaX
16537 * @param scrollY Current Y scroll value in pixels before applying deltaY
16538 * @param scrollRangeX Maximum content scroll range along the X axis
16539 * @param scrollRangeY Maximum content scroll range along the Y axis
16540 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16541 * along the X axis.
16542 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16543 * along the Y axis.
16544 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16545 * @return true if scrolling was clamped to an over-scroll boundary along either
16546 * axis, false otherwise.
16547 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016548 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016549 protected boolean overScrollBy(int deltaX, int deltaY,
16550 int scrollX, int scrollY,
16551 int scrollRangeX, int scrollRangeY,
16552 int maxOverScrollX, int maxOverScrollY,
16553 boolean isTouchEvent) {
16554 final int overScrollMode = mOverScrollMode;
16555 final boolean canScrollHorizontal =
16556 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16557 final boolean canScrollVertical =
16558 computeVerticalScrollRange() > computeVerticalScrollExtent();
16559 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16560 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16561 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16562 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16563
16564 int newScrollX = scrollX + deltaX;
16565 if (!overScrollHorizontal) {
16566 maxOverScrollX = 0;
16567 }
16568
16569 int newScrollY = scrollY + deltaY;
16570 if (!overScrollVertical) {
16571 maxOverScrollY = 0;
16572 }
16573
16574 // Clamp values if at the limits and record
16575 final int left = -maxOverScrollX;
16576 final int right = maxOverScrollX + scrollRangeX;
16577 final int top = -maxOverScrollY;
16578 final int bottom = maxOverScrollY + scrollRangeY;
16579
16580 boolean clampedX = false;
16581 if (newScrollX > right) {
16582 newScrollX = right;
16583 clampedX = true;
16584 } else if (newScrollX < left) {
16585 newScrollX = left;
16586 clampedX = true;
16587 }
16588
16589 boolean clampedY = false;
16590 if (newScrollY > bottom) {
16591 newScrollY = bottom;
16592 clampedY = true;
16593 } else if (newScrollY < top) {
16594 newScrollY = top;
16595 clampedY = true;
16596 }
16597
16598 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16599
16600 return clampedX || clampedY;
16601 }
16602
16603 /**
16604 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16605 * respond to the results of an over-scroll operation.
16606 *
16607 * @param scrollX New X scroll value in pixels
16608 * @param scrollY New Y scroll value in pixels
16609 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16610 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16611 */
16612 protected void onOverScrolled(int scrollX, int scrollY,
16613 boolean clampedX, boolean clampedY) {
16614 // Intentionally empty.
16615 }
16616
16617 /**
16618 * Returns the over-scroll mode for this view. The result will be
16619 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16620 * (allow over-scrolling only if the view content is larger than the container),
16621 * or {@link #OVER_SCROLL_NEVER}.
16622 *
16623 * @return This view's over-scroll mode.
16624 */
16625 public int getOverScrollMode() {
16626 return mOverScrollMode;
16627 }
16628
16629 /**
16630 * Set the over-scroll mode for this view. Valid over-scroll modes are
16631 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16632 * (allow over-scrolling only if the view content is larger than the container),
16633 * or {@link #OVER_SCROLL_NEVER}.
16634 *
16635 * Setting the over-scroll mode of a view will have an effect only if the
16636 * view is capable of scrolling.
16637 *
16638 * @param overScrollMode The new over-scroll mode for this view.
16639 */
16640 public void setOverScrollMode(int overScrollMode) {
16641 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16642 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16643 overScrollMode != OVER_SCROLL_NEVER) {
16644 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16645 }
16646 mOverScrollMode = overScrollMode;
16647 }
16648
16649 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016650 * Gets a scale factor that determines the distance the view should scroll
16651 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16652 * @return The vertical scroll scale factor.
16653 * @hide
16654 */
16655 protected float getVerticalScrollFactor() {
16656 if (mVerticalScrollFactor == 0) {
16657 TypedValue outValue = new TypedValue();
16658 if (!mContext.getTheme().resolveAttribute(
16659 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16660 throw new IllegalStateException(
16661 "Expected theme to define listPreferredItemHeight.");
16662 }
16663 mVerticalScrollFactor = outValue.getDimension(
16664 mContext.getResources().getDisplayMetrics());
16665 }
16666 return mVerticalScrollFactor;
16667 }
16668
16669 /**
16670 * Gets a scale factor that determines the distance the view should scroll
16671 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16672 * @return The horizontal scroll scale factor.
16673 * @hide
16674 */
16675 protected float getHorizontalScrollFactor() {
16676 // TODO: Should use something else.
16677 return getVerticalScrollFactor();
16678 }
16679
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016680 /**
16681 * Return the value specifying the text direction or policy that was set with
16682 * {@link #setTextDirection(int)}.
16683 *
16684 * @return the defined text direction. It can be one of:
16685 *
16686 * {@link #TEXT_DIRECTION_INHERIT},
16687 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16688 * {@link #TEXT_DIRECTION_ANY_RTL},
16689 * {@link #TEXT_DIRECTION_LTR},
16690 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016691 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016692 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016693 * @attr ref android.R.styleable#View_textDirection
16694 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016695 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016696 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016697 @ViewDebug.ExportedProperty(category = "text", mapping = {
16698 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16699 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16700 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16701 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16702 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16703 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16704 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016705 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016706 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016707 }
16708
16709 /**
16710 * Set the text direction.
16711 *
16712 * @param textDirection the direction to set. Should be one of:
16713 *
16714 * {@link #TEXT_DIRECTION_INHERIT},
16715 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16716 * {@link #TEXT_DIRECTION_ANY_RTL},
16717 * {@link #TEXT_DIRECTION_LTR},
16718 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016719 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016720 *
16721 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16722 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16723 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016724 *
16725 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016726 */
16727 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016728 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016729 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016730 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016731 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016732 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016733 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016734 // Do resolution
16735 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016736 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016737 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016738 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016739 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016740 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016741 }
16742 }
16743
16744 /**
16745 * Return the resolved text direction.
16746 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016747 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016748 *
Doug Feltcb3791202011-07-07 11:57:48 -070016749 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16750 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016751 * {@link #TEXT_DIRECTION_LTR},
16752 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016753 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016754 *
16755 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016756 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016757 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016758 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016759 }
16760
16761 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016762 * Resolve the text direction.
16763 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016764 * @return true if resolution has been done, false otherwise.
16765 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016766 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016767 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016768 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016769 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016770 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016771
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016772 if (hasRtlSupport()) {
16773 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016774 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016775 switch(textDirection) {
16776 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016777 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016778 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016779 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016780 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016781 return false;
16782 }
16783
16784 View parent = ((View) mParent);
16785 // Parent has not yet resolved, so we still return the default
16786 if (!parent.isTextDirectionResolved()) {
16787 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16788 // Resolution will need to happen again later
16789 return false;
16790 }
16791
16792 // Set current resolved direction to the same value as the parent's one
16793 final int parentResolvedDirection = parent.getTextDirection();
16794 switch (parentResolvedDirection) {
16795 case TEXT_DIRECTION_FIRST_STRONG:
16796 case TEXT_DIRECTION_ANY_RTL:
16797 case TEXT_DIRECTION_LTR:
16798 case TEXT_DIRECTION_RTL:
16799 case TEXT_DIRECTION_LOCALE:
16800 mPrivateFlags2 |=
16801 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16802 break;
16803 default:
16804 // Default resolved direction is "first strong" heuristic
16805 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016806 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016807 break;
16808 case TEXT_DIRECTION_FIRST_STRONG:
16809 case TEXT_DIRECTION_ANY_RTL:
16810 case TEXT_DIRECTION_LTR:
16811 case TEXT_DIRECTION_RTL:
16812 case TEXT_DIRECTION_LOCALE:
16813 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016814 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016815 break;
16816 default:
16817 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016818 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016819 }
16820 } else {
16821 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016822 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016823 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016824
16825 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016826 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016827 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016828 }
16829
16830 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016831 * Check if text direction resolution can be done.
16832 *
16833 * @return true if text direction resolution can be done otherwise return false.
16834 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016835 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016836 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016837 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016838 return (mParent != null) && (mParent instanceof View) &&
16839 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016840 default:
16841 return true;
16842 }
16843 }
16844
16845 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016846 * Reset resolved text direction. Text direction will be resolved during a call to
16847 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016848 *
16849 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016850 */
16851 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016852 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016853 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016854 // Set to default value
16855 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016856 }
16857
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016858 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016859 * @return true if text direction is inherited.
16860 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016861 * @hide
16862 */
16863 public boolean isTextDirectionInherited() {
16864 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16865 }
16866
16867 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016868 * @return true if text direction is resolved.
16869 */
16870 private boolean isTextDirectionResolved() {
16871 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16872 }
16873
16874 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016875 * Return the value specifying the text alignment or policy that was set with
16876 * {@link #setTextAlignment(int)}.
16877 *
16878 * @return the defined text alignment. It can be one of:
16879 *
16880 * {@link #TEXT_ALIGNMENT_INHERIT},
16881 * {@link #TEXT_ALIGNMENT_GRAVITY},
16882 * {@link #TEXT_ALIGNMENT_CENTER},
16883 * {@link #TEXT_ALIGNMENT_TEXT_START},
16884 * {@link #TEXT_ALIGNMENT_TEXT_END},
16885 * {@link #TEXT_ALIGNMENT_VIEW_START},
16886 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016887 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016888 * @attr ref android.R.styleable#View_textAlignment
16889 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016890 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016891 */
16892 @ViewDebug.ExportedProperty(category = "text", mapping = {
16893 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16894 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16895 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16896 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16897 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16898 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16899 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16900 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016901 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016902 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016903 }
16904
16905 /**
16906 * Set the text alignment.
16907 *
16908 * @param textAlignment The text alignment to set. Should be one of
16909 *
16910 * {@link #TEXT_ALIGNMENT_INHERIT},
16911 * {@link #TEXT_ALIGNMENT_GRAVITY},
16912 * {@link #TEXT_ALIGNMENT_CENTER},
16913 * {@link #TEXT_ALIGNMENT_TEXT_START},
16914 * {@link #TEXT_ALIGNMENT_TEXT_END},
16915 * {@link #TEXT_ALIGNMENT_VIEW_START},
16916 * {@link #TEXT_ALIGNMENT_VIEW_END}
16917 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016918 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16919 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16920 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16921 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016922 * @attr ref android.R.styleable#View_textAlignment
16923 */
16924 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016925 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016926 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016927 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016928 resetResolvedTextAlignment();
16929 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016930 mPrivateFlags2 |=
16931 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16932 // Do resolution
16933 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016934 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016935 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016936 // Refresh
16937 requestLayout();
16938 invalidate(true);
16939 }
16940 }
16941
16942 /**
16943 * Return the resolved text alignment.
16944 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016945 * @return the resolved text alignment. Returns one of:
16946 *
16947 * {@link #TEXT_ALIGNMENT_GRAVITY},
16948 * {@link #TEXT_ALIGNMENT_CENTER},
16949 * {@link #TEXT_ALIGNMENT_TEXT_START},
16950 * {@link #TEXT_ALIGNMENT_TEXT_END},
16951 * {@link #TEXT_ALIGNMENT_VIEW_START},
16952 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016953 *
16954 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016955 */
16956 @ViewDebug.ExportedProperty(category = "text", mapping = {
16957 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16958 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16959 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16960 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16961 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16962 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16963 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16964 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016965 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016966 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16967 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016968 }
16969
16970 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016971 * Resolve the text alignment.
16972 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016973 * @return true if resolution has been done, false otherwise.
16974 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016975 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016976 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016977 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016978 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016979 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016980
16981 if (hasRtlSupport()) {
16982 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016983 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016984 switch (textAlignment) {
16985 case TEXT_ALIGNMENT_INHERIT:
16986 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016987 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016988 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016989 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016990 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016991 return false;
16992 }
16993 View parent = (View) mParent;
16994
16995 // Parent has not yet resolved, so we still return the default
16996 if (!parent.isTextAlignmentResolved()) {
16997 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16998 // Resolution will need to happen again later
16999 return false;
17000 }
17001
17002 final int parentResolvedTextAlignment = parent.getTextAlignment();
17003 switch (parentResolvedTextAlignment) {
17004 case TEXT_ALIGNMENT_GRAVITY:
17005 case TEXT_ALIGNMENT_TEXT_START:
17006 case TEXT_ALIGNMENT_TEXT_END:
17007 case TEXT_ALIGNMENT_CENTER:
17008 case TEXT_ALIGNMENT_VIEW_START:
17009 case TEXT_ALIGNMENT_VIEW_END:
17010 // Resolved text alignment is the same as the parent resolved
17011 // text alignment
17012 mPrivateFlags2 |=
17013 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17014 break;
17015 default:
17016 // Use default resolved text alignment
17017 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017018 }
17019 break;
17020 case TEXT_ALIGNMENT_GRAVITY:
17021 case TEXT_ALIGNMENT_TEXT_START:
17022 case TEXT_ALIGNMENT_TEXT_END:
17023 case TEXT_ALIGNMENT_CENTER:
17024 case TEXT_ALIGNMENT_VIEW_START:
17025 case TEXT_ALIGNMENT_VIEW_END:
17026 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017027 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017028 break;
17029 default:
17030 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017031 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017032 }
17033 } else {
17034 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017035 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017036 }
17037
17038 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017039 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017040 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017041 }
17042
17043 /**
17044 * Check if text alignment resolution can be done.
17045 *
17046 * @return true if text alignment resolution can be done otherwise return false.
17047 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017048 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017049 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017050 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017051 return (mParent != null) && (mParent instanceof View) &&
17052 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017053 default:
17054 return true;
17055 }
17056 }
17057
17058 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017059 * Reset resolved text alignment. Text alignment will be resolved during a call to
17060 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017061 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017062 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017063 */
17064 public void resetResolvedTextAlignment() {
17065 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017066 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017067 // Set to default
17068 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017069 }
17070
Adam Powella9108a22012-07-18 11:18:09 -070017071 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017072 * @return true if text alignment is inherited.
17073 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017074 * @hide
17075 */
17076 public boolean isTextAlignmentInherited() {
17077 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17078 }
17079
17080 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017081 * @return true if text alignment is resolved.
17082 */
17083 private boolean isTextAlignmentResolved() {
17084 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17085 }
17086
17087 /**
Adam Powella9108a22012-07-18 11:18:09 -070017088 * Generate a value suitable for use in {@link #setId(int)}.
17089 * This value will not collide with ID values generated at build time by aapt for R.id.
17090 *
17091 * @return a generated ID value
17092 */
17093 public static int generateViewId() {
17094 for (;;) {
17095 final int result = sNextGeneratedId.get();
17096 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17097 int newValue = result + 1;
17098 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17099 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17100 return result;
17101 }
17102 }
17103 }
17104
Chet Haaseb39f0512011-05-24 14:36:40 -070017105 //
17106 // Properties
17107 //
17108 /**
17109 * A Property wrapper around the <code>alpha</code> functionality handled by the
17110 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17111 */
Chet Haased47f1532011-12-16 11:18:52 -080017112 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017113 @Override
17114 public void setValue(View object, float value) {
17115 object.setAlpha(value);
17116 }
17117
17118 @Override
17119 public Float get(View object) {
17120 return object.getAlpha();
17121 }
17122 };
17123
17124 /**
17125 * A Property wrapper around the <code>translationX</code> functionality handled by the
17126 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17127 */
Chet Haased47f1532011-12-16 11:18:52 -080017128 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017129 @Override
17130 public void setValue(View object, float value) {
17131 object.setTranslationX(value);
17132 }
17133
17134 @Override
17135 public Float get(View object) {
17136 return object.getTranslationX();
17137 }
17138 };
17139
17140 /**
17141 * A Property wrapper around the <code>translationY</code> functionality handled by the
17142 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17143 */
Chet Haased47f1532011-12-16 11:18:52 -080017144 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017145 @Override
17146 public void setValue(View object, float value) {
17147 object.setTranslationY(value);
17148 }
17149
17150 @Override
17151 public Float get(View object) {
17152 return object.getTranslationY();
17153 }
17154 };
17155
17156 /**
17157 * A Property wrapper around the <code>x</code> functionality handled by the
17158 * {@link View#setX(float)} and {@link View#getX()} methods.
17159 */
Chet Haased47f1532011-12-16 11:18:52 -080017160 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017161 @Override
17162 public void setValue(View object, float value) {
17163 object.setX(value);
17164 }
17165
17166 @Override
17167 public Float get(View object) {
17168 return object.getX();
17169 }
17170 };
17171
17172 /**
17173 * A Property wrapper around the <code>y</code> functionality handled by the
17174 * {@link View#setY(float)} and {@link View#getY()} methods.
17175 */
Chet Haased47f1532011-12-16 11:18:52 -080017176 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017177 @Override
17178 public void setValue(View object, float value) {
17179 object.setY(value);
17180 }
17181
17182 @Override
17183 public Float get(View object) {
17184 return object.getY();
17185 }
17186 };
17187
17188 /**
17189 * A Property wrapper around the <code>rotation</code> functionality handled by the
17190 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17191 */
Chet Haased47f1532011-12-16 11:18:52 -080017192 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017193 @Override
17194 public void setValue(View object, float value) {
17195 object.setRotation(value);
17196 }
17197
17198 @Override
17199 public Float get(View object) {
17200 return object.getRotation();
17201 }
17202 };
17203
17204 /**
17205 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17206 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17207 */
Chet Haased47f1532011-12-16 11:18:52 -080017208 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017209 @Override
17210 public void setValue(View object, float value) {
17211 object.setRotationX(value);
17212 }
17213
17214 @Override
17215 public Float get(View object) {
17216 return object.getRotationX();
17217 }
17218 };
17219
17220 /**
17221 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17222 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17223 */
Chet Haased47f1532011-12-16 11:18:52 -080017224 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017225 @Override
17226 public void setValue(View object, float value) {
17227 object.setRotationY(value);
17228 }
17229
17230 @Override
17231 public Float get(View object) {
17232 return object.getRotationY();
17233 }
17234 };
17235
17236 /**
17237 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17238 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17239 */
Chet Haased47f1532011-12-16 11:18:52 -080017240 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017241 @Override
17242 public void setValue(View object, float value) {
17243 object.setScaleX(value);
17244 }
17245
17246 @Override
17247 public Float get(View object) {
17248 return object.getScaleX();
17249 }
17250 };
17251
17252 /**
17253 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17254 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17255 */
Chet Haased47f1532011-12-16 11:18:52 -080017256 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017257 @Override
17258 public void setValue(View object, float value) {
17259 object.setScaleY(value);
17260 }
17261
17262 @Override
17263 public Float get(View object) {
17264 return object.getScaleY();
17265 }
17266 };
17267
Jeff Brown33bbfd22011-02-24 20:55:35 -080017268 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017269 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17270 * Each MeasureSpec represents a requirement for either the width or the height.
17271 * A MeasureSpec is comprised of a size and a mode. There are three possible
17272 * modes:
17273 * <dl>
17274 * <dt>UNSPECIFIED</dt>
17275 * <dd>
17276 * The parent has not imposed any constraint on the child. It can be whatever size
17277 * it wants.
17278 * </dd>
17279 *
17280 * <dt>EXACTLY</dt>
17281 * <dd>
17282 * The parent has determined an exact size for the child. The child is going to be
17283 * given those bounds regardless of how big it wants to be.
17284 * </dd>
17285 *
17286 * <dt>AT_MOST</dt>
17287 * <dd>
17288 * The child can be as large as it wants up to the specified size.
17289 * </dd>
17290 * </dl>
17291 *
17292 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17293 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17294 */
17295 public static class MeasureSpec {
17296 private static final int MODE_SHIFT = 30;
17297 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17298
17299 /**
17300 * Measure specification mode: The parent has not imposed any constraint
17301 * on the child. It can be whatever size it wants.
17302 */
17303 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17304
17305 /**
17306 * Measure specification mode: The parent has determined an exact size
17307 * for the child. The child is going to be given those bounds regardless
17308 * of how big it wants to be.
17309 */
17310 public static final int EXACTLY = 1 << MODE_SHIFT;
17311
17312 /**
17313 * Measure specification mode: The child can be as large as it wants up
17314 * to the specified size.
17315 */
17316 public static final int AT_MOST = 2 << MODE_SHIFT;
17317
17318 /**
17319 * Creates a measure specification based on the supplied size and mode.
17320 *
17321 * The mode must always be one of the following:
17322 * <ul>
17323 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17324 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17325 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17326 * </ul>
17327 *
17328 * @param size the size of the measure specification
17329 * @param mode the mode of the measure specification
17330 * @return the measure specification based on size and mode
17331 */
17332 public static int makeMeasureSpec(int size, int mode) {
Adam Powelld5edc772012-09-26 15:21:39 -070017333 return (size & ~MODE_MASK) | (mode & MODE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017334 }
17335
17336 /**
17337 * Extracts the mode from the supplied measure specification.
17338 *
17339 * @param measureSpec the measure specification to extract the mode from
17340 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17341 * {@link android.view.View.MeasureSpec#AT_MOST} or
17342 * {@link android.view.View.MeasureSpec#EXACTLY}
17343 */
17344 public static int getMode(int measureSpec) {
17345 return (measureSpec & MODE_MASK);
17346 }
17347
17348 /**
17349 * Extracts the size from the supplied measure specification.
17350 *
17351 * @param measureSpec the measure specification to extract the size from
17352 * @return the size in pixels defined in the supplied measure specification
17353 */
17354 public static int getSize(int measureSpec) {
17355 return (measureSpec & ~MODE_MASK);
17356 }
17357
Philip Milne7b757812012-09-19 18:13:44 -070017358 static int adjust(int measureSpec, int delta) {
17359 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17360 }
17361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017362 /**
17363 * Returns a String representation of the specified measure
17364 * specification.
17365 *
17366 * @param measureSpec the measure specification to convert to a String
17367 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17368 */
17369 public static String toString(int measureSpec) {
17370 int mode = getMode(measureSpec);
17371 int size = getSize(measureSpec);
17372
17373 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17374
17375 if (mode == UNSPECIFIED)
17376 sb.append("UNSPECIFIED ");
17377 else if (mode == EXACTLY)
17378 sb.append("EXACTLY ");
17379 else if (mode == AT_MOST)
17380 sb.append("AT_MOST ");
17381 else
17382 sb.append(mode).append(" ");
17383
17384 sb.append(size);
17385 return sb.toString();
17386 }
17387 }
17388
17389 class CheckForLongPress implements Runnable {
17390
17391 private int mOriginalWindowAttachCount;
17392
17393 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017394 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017395 && mOriginalWindowAttachCount == mWindowAttachCount) {
17396 if (performLongClick()) {
17397 mHasPerformedLongPress = true;
17398 }
17399 }
17400 }
17401
17402 public void rememberWindowAttachCount() {
17403 mOriginalWindowAttachCount = mWindowAttachCount;
17404 }
17405 }
Joe Malin32736f02011-01-19 16:14:20 -080017406
Adam Powelle14579b2009-12-16 18:39:52 -080017407 private final class CheckForTap implements Runnable {
17408 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017409 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017410 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017411 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017412 }
17413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017414
Adam Powella35d7682010-03-12 14:48:13 -080017415 private final class PerformClick implements Runnable {
17416 public void run() {
17417 performClick();
17418 }
17419 }
17420
Dianne Hackborn63042d62011-01-26 18:56:29 -080017421 /** @hide */
17422 public void hackTurnOffWindowResizeAnim(boolean off) {
17423 mAttachInfo.mTurnOffWindowResizeAnim = off;
17424 }
Joe Malin32736f02011-01-19 16:14:20 -080017425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017426 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017427 * This method returns a ViewPropertyAnimator object, which can be used to animate
17428 * specific properties on this View.
17429 *
17430 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17431 */
17432 public ViewPropertyAnimator animate() {
17433 if (mAnimator == null) {
17434 mAnimator = new ViewPropertyAnimator(this);
17435 }
17436 return mAnimator;
17437 }
17438
17439 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017440 * Interface definition for a callback to be invoked when a hardware key event is
17441 * dispatched to this view. The callback will be invoked before the key event is
17442 * given to the view. This is only useful for hardware keyboards; a software input
17443 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017444 */
17445 public interface OnKeyListener {
17446 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017447 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017448 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017449 * <p>Key presses in software keyboards will generally NOT trigger this method,
17450 * although some may elect to do so in some situations. Do not assume a
17451 * software input method has to be key-based; even if it is, it may use key presses
17452 * in a different way than you expect, so there is no way to reliably catch soft
17453 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017454 *
17455 * @param v The view the key has been dispatched to.
17456 * @param keyCode The code for the physical key that was pressed
17457 * @param event The KeyEvent object containing full information about
17458 * the event.
17459 * @return True if the listener has consumed the event, false otherwise.
17460 */
17461 boolean onKey(View v, int keyCode, KeyEvent event);
17462 }
17463
17464 /**
17465 * Interface definition for a callback to be invoked when a touch event is
17466 * dispatched to this view. The callback will be invoked before the touch
17467 * event is given to the view.
17468 */
17469 public interface OnTouchListener {
17470 /**
17471 * Called when a touch event is dispatched to a view. This allows listeners to
17472 * get a chance to respond before the target view.
17473 *
17474 * @param v The view the touch event has been dispatched to.
17475 * @param event The MotionEvent object containing full information about
17476 * the event.
17477 * @return True if the listener has consumed the event, false otherwise.
17478 */
17479 boolean onTouch(View v, MotionEvent event);
17480 }
17481
17482 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017483 * Interface definition for a callback to be invoked when a hover event is
17484 * dispatched to this view. The callback will be invoked before the hover
17485 * event is given to the view.
17486 */
17487 public interface OnHoverListener {
17488 /**
17489 * Called when a hover event is dispatched to a view. This allows listeners to
17490 * get a chance to respond before the target view.
17491 *
17492 * @param v The view the hover event has been dispatched to.
17493 * @param event The MotionEvent object containing full information about
17494 * the event.
17495 * @return True if the listener has consumed the event, false otherwise.
17496 */
17497 boolean onHover(View v, MotionEvent event);
17498 }
17499
17500 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017501 * Interface definition for a callback to be invoked when a generic motion event is
17502 * dispatched to this view. The callback will be invoked before the generic motion
17503 * event is given to the view.
17504 */
17505 public interface OnGenericMotionListener {
17506 /**
17507 * Called when a generic motion event is dispatched to a view. This allows listeners to
17508 * get a chance to respond before the target view.
17509 *
17510 * @param v The view the generic motion event has been dispatched to.
17511 * @param event The MotionEvent object containing full information about
17512 * the event.
17513 * @return True if the listener has consumed the event, false otherwise.
17514 */
17515 boolean onGenericMotion(View v, MotionEvent event);
17516 }
17517
17518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017519 * Interface definition for a callback to be invoked when a view has been clicked and held.
17520 */
17521 public interface OnLongClickListener {
17522 /**
17523 * Called when a view has been clicked and held.
17524 *
17525 * @param v The view that was clicked and held.
17526 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017527 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017528 */
17529 boolean onLongClick(View v);
17530 }
17531
17532 /**
Chris Tate32affef2010-10-18 15:29:21 -070017533 * Interface definition for a callback to be invoked when a drag is being dispatched
17534 * to this view. The callback will be invoked before the hosting view's own
17535 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17536 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017537 *
17538 * <div class="special reference">
17539 * <h3>Developer Guides</h3>
17540 * <p>For a guide to implementing drag and drop features, read the
17541 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17542 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017543 */
17544 public interface OnDragListener {
17545 /**
17546 * Called when a drag event is dispatched to a view. This allows listeners
17547 * to get a chance to override base View behavior.
17548 *
Joe Malin32736f02011-01-19 16:14:20 -080017549 * @param v The View that received the drag event.
17550 * @param event The {@link android.view.DragEvent} object for the drag event.
17551 * @return {@code true} if the drag event was handled successfully, or {@code false}
17552 * if the drag event was not handled. Note that {@code false} will trigger the View
17553 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017554 */
17555 boolean onDrag(View v, DragEvent event);
17556 }
17557
17558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017559 * Interface definition for a callback to be invoked when the focus state of
17560 * a view changed.
17561 */
17562 public interface OnFocusChangeListener {
17563 /**
17564 * Called when the focus state of a view has changed.
17565 *
17566 * @param v The view whose state has changed.
17567 * @param hasFocus The new focus state of v.
17568 */
17569 void onFocusChange(View v, boolean hasFocus);
17570 }
17571
17572 /**
17573 * Interface definition for a callback to be invoked when a view is clicked.
17574 */
17575 public interface OnClickListener {
17576 /**
17577 * Called when a view has been clicked.
17578 *
17579 * @param v The view that was clicked.
17580 */
17581 void onClick(View v);
17582 }
17583
17584 /**
17585 * Interface definition for a callback to be invoked when the context menu
17586 * for this view is being built.
17587 */
17588 public interface OnCreateContextMenuListener {
17589 /**
17590 * Called when the context menu for this view is being built. It is not
17591 * safe to hold onto the menu after this method returns.
17592 *
17593 * @param menu The context menu that is being built
17594 * @param v The view for which the context menu is being built
17595 * @param menuInfo Extra information about the item for which the
17596 * context menu should be shown. This information will vary
17597 * depending on the class of v.
17598 */
17599 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17600 }
17601
Joe Onorato664644d2011-01-23 17:53:23 -080017602 /**
17603 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017604 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017605 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017606 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017607 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017608 */
17609 public interface OnSystemUiVisibilityChangeListener {
17610 /**
17611 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017612 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017613 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017614 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17615 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17616 * This tells you the <strong>global</strong> state of these UI visibility
17617 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017618 */
17619 public void onSystemUiVisibilityChange(int visibility);
17620 }
17621
Adam Powell4afd62b2011-02-18 15:02:18 -080017622 /**
17623 * Interface definition for a callback to be invoked when this view is attached
17624 * or detached from its window.
17625 */
17626 public interface OnAttachStateChangeListener {
17627 /**
17628 * Called when the view is attached to a window.
17629 * @param v The view that was attached
17630 */
17631 public void onViewAttachedToWindow(View v);
17632 /**
17633 * Called when the view is detached from a window.
17634 * @param v The view that was detached
17635 */
17636 public void onViewDetachedFromWindow(View v);
17637 }
17638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017639 private final class UnsetPressedState implements Runnable {
17640 public void run() {
17641 setPressed(false);
17642 }
17643 }
17644
17645 /**
17646 * Base class for derived classes that want to save and restore their own
17647 * state in {@link android.view.View#onSaveInstanceState()}.
17648 */
17649 public static class BaseSavedState extends AbsSavedState {
17650 /**
17651 * Constructor used when reading from a parcel. Reads the state of the superclass.
17652 *
17653 * @param source
17654 */
17655 public BaseSavedState(Parcel source) {
17656 super(source);
17657 }
17658
17659 /**
17660 * Constructor called by derived classes when creating their SavedState objects
17661 *
17662 * @param superState The state of the superclass of this view
17663 */
17664 public BaseSavedState(Parcelable superState) {
17665 super(superState);
17666 }
17667
17668 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17669 new Parcelable.Creator<BaseSavedState>() {
17670 public BaseSavedState createFromParcel(Parcel in) {
17671 return new BaseSavedState(in);
17672 }
17673
17674 public BaseSavedState[] newArray(int size) {
17675 return new BaseSavedState[size];
17676 }
17677 };
17678 }
17679
17680 /**
17681 * A set of information given to a view when it is attached to its parent
17682 * window.
17683 */
17684 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017685 interface Callbacks {
17686 void playSoundEffect(int effectId);
17687 boolean performHapticFeedback(int effectId, boolean always);
17688 }
17689
17690 /**
17691 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17692 * to a Handler. This class contains the target (View) to invalidate and
17693 * the coordinates of the dirty rectangle.
17694 *
17695 * For performance purposes, this class also implements a pool of up to
17696 * POOL_LIMIT objects that get reused. This reduces memory allocations
17697 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017698 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017699 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017700 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017701
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017702 private static final SynchronizedPool<InvalidateInfo> sPool =
17703 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017704
17705 View target;
17706
17707 int left;
17708 int top;
17709 int right;
17710 int bottom;
17711
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017712 public static InvalidateInfo obtain() {
17713 InvalidateInfo instance = sPool.acquire();
17714 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070017715 }
17716
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017717 public void recycle() {
17718 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017719 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017720 }
17721 }
17722
17723 final IWindowSession mSession;
17724
17725 final IWindow mWindow;
17726
17727 final IBinder mWindowToken;
17728
Jeff Brown98365d72012-08-19 20:30:52 -070017729 final Display mDisplay;
17730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017731 final Callbacks mRootCallbacks;
17732
Romain Guy59a12ca2011-06-09 17:48:21 -070017733 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017735 /**
17736 * The top view of the hierarchy.
17737 */
17738 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017740 IBinder mPanelParentWindowToken;
17741 Surface mSurface;
17742
Romain Guyb051e892010-09-28 19:09:36 -070017743 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017744 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017745 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017746
Romain Guy7e4e5612012-03-05 14:37:29 -080017747 boolean mScreenOn;
17748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017749 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017750 * Scale factor used by the compatibility mode
17751 */
17752 float mApplicationScale;
17753
17754 /**
17755 * Indicates whether the application is in compatibility mode
17756 */
17757 boolean mScalingRequired;
17758
17759 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017760 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017761 */
17762 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017763
Dianne Hackborn63042d62011-01-26 18:56:29 -080017764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017765 * Left position of this view's window
17766 */
17767 int mWindowLeft;
17768
17769 /**
17770 * Top position of this view's window
17771 */
17772 int mWindowTop;
17773
17774 /**
Adam Powell26153a32010-11-08 15:22:27 -080017775 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017776 */
Adam Powell26153a32010-11-08 15:22:27 -080017777 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017778
17779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017780 * For windows that are full-screen but using insets to layout inside
17781 * of the screen decorations, these are the current insets for the
17782 * content of the window.
17783 */
17784 final Rect mContentInsets = new Rect();
17785
17786 /**
17787 * For windows that are full-screen but using insets to layout inside
17788 * of the screen decorations, these are the current insets for the
17789 * actual visible parts of the window.
17790 */
17791 final Rect mVisibleInsets = new Rect();
17792
17793 /**
17794 * The internal insets given by this window. This value is
17795 * supplied by the client (through
17796 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17797 * be given to the window manager when changed to be used in laying
17798 * out windows behind it.
17799 */
17800 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17801 = new ViewTreeObserver.InternalInsetsInfo();
17802
17803 /**
17804 * All views in the window's hierarchy that serve as scroll containers,
17805 * used to determine if the window can be resized or must be panned
17806 * to adjust for a soft input area.
17807 */
17808 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17809
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017810 final KeyEvent.DispatcherState mKeyDispatchState
17811 = new KeyEvent.DispatcherState();
17812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017813 /**
17814 * Indicates whether the view's window currently has the focus.
17815 */
17816 boolean mHasWindowFocus;
17817
17818 /**
17819 * The current visibility of the window.
17820 */
17821 int mWindowVisibility;
17822
17823 /**
17824 * Indicates the time at which drawing started to occur.
17825 */
17826 long mDrawingTime;
17827
17828 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017829 * Indicates whether or not ignoring the DIRTY_MASK flags.
17830 */
17831 boolean mIgnoreDirtyState;
17832
17833 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017834 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17835 * to avoid clearing that flag prematurely.
17836 */
17837 boolean mSetIgnoreDirtyState = false;
17838
17839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017840 * Indicates whether the view's window is currently in touch mode.
17841 */
17842 boolean mInTouchMode;
17843
17844 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017845 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017846 * the next time it performs a traversal
17847 */
17848 boolean mRecomputeGlobalAttributes;
17849
17850 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017851 * Always report new attributes at next traversal.
17852 */
17853 boolean mForceReportNewAttributes;
17854
17855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017856 * Set during a traveral if any views want to keep the screen on.
17857 */
17858 boolean mKeepScreenOn;
17859
17860 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017861 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17862 */
17863 int mSystemUiVisibility;
17864
17865 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017866 * Hack to force certain system UI visibility flags to be cleared.
17867 */
17868 int mDisabledSystemUiVisibility;
17869
17870 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017871 * Last global system UI visibility reported by the window manager.
17872 */
17873 int mGlobalSystemUiVisibility;
17874
17875 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017876 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17877 * attached.
17878 */
17879 boolean mHasSystemUiListeners;
17880
17881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017882 * Set if the visibility of any views has changed.
17883 */
17884 boolean mViewVisibilityChanged;
17885
17886 /**
17887 * Set to true if a view has been scrolled.
17888 */
17889 boolean mViewScrollChanged;
17890
17891 /**
17892 * Global to the view hierarchy used as a temporary for dealing with
17893 * x/y points in the transparent region computations.
17894 */
17895 final int[] mTransparentLocation = new int[2];
17896
17897 /**
17898 * Global to the view hierarchy used as a temporary for dealing with
17899 * x/y points in the ViewGroup.invalidateChild implementation.
17900 */
17901 final int[] mInvalidateChildLocation = new int[2];
17902
Chet Haasec3aa3612010-06-17 08:50:37 -070017903
17904 /**
17905 * Global to the view hierarchy used as a temporary for dealing with
17906 * x/y location when view is transformed.
17907 */
17908 final float[] mTmpTransformLocation = new float[2];
17909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017910 /**
17911 * The view tree observer used to dispatch global events like
17912 * layout, pre-draw, touch mode change, etc.
17913 */
17914 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17915
17916 /**
17917 * A Canvas used by the view hierarchy to perform bitmap caching.
17918 */
17919 Canvas mCanvas;
17920
17921 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017922 * The view root impl.
17923 */
17924 final ViewRootImpl mViewRootImpl;
17925
17926 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017927 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017928 * handler can be used to pump events in the UI events queue.
17929 */
17930 final Handler mHandler;
17931
17932 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017933 * Temporary for use in computing invalidate rectangles while
17934 * calling up the hierarchy.
17935 */
17936 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017937
17938 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017939 * Temporary for use in computing hit areas with transformed views
17940 */
17941 final RectF mTmpTransformRect = new RectF();
17942
17943 /**
Chet Haase599913d2012-07-23 16:22:05 -070017944 * Temporary for use in transforming invalidation rect
17945 */
17946 final Matrix mTmpMatrix = new Matrix();
17947
17948 /**
17949 * Temporary for use in transforming invalidation rect
17950 */
17951 final Transformation mTmpTransformation = new Transformation();
17952
17953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017954 * Temporary list for use in collecting focusable descendents of a view.
17955 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017956 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017958 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017959 * The id of the window for accessibility purposes.
17960 */
17961 int mAccessibilityWindowId = View.NO_ID;
17962
17963 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017964 * Whether to ingore not exposed for accessibility Views when
17965 * reporting the view tree to accessibility services.
17966 */
17967 boolean mIncludeNotImportantViews;
17968
17969 /**
17970 * The drawable for highlighting accessibility focus.
17971 */
17972 Drawable mAccessibilityFocusDrawable;
17973
17974 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017975 * Show where the margins, bounds and layout bounds are for each view.
17976 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017977 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017978
17979 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017980 * Point used to compute visible regions.
17981 */
17982 final Point mPoint = new Point();
17983
17984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017985 * Creates a new set of attachment information with the specified
17986 * events handler and thread.
17987 *
17988 * @param handler the events handler the view must use
17989 */
Jeff Brown98365d72012-08-19 20:30:52 -070017990 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017991 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017992 mSession = session;
17993 mWindow = window;
17994 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017995 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017996 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017997 mHandler = handler;
17998 mRootCallbacks = effectPlayer;
17999 }
18000 }
18001
18002 /**
18003 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18004 * is supported. This avoids keeping too many unused fields in most
18005 * instances of View.</p>
18006 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018007 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018008
Mike Cleronf116bf82009-09-27 19:14:12 -070018009 /**
18010 * Scrollbars are not visible
18011 */
18012 public static final int OFF = 0;
18013
18014 /**
18015 * Scrollbars are visible
18016 */
18017 public static final int ON = 1;
18018
18019 /**
18020 * Scrollbars are fading away
18021 */
18022 public static final int FADING = 2;
18023
18024 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018026 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018027 public int scrollBarDefaultDelayBeforeFade;
18028 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018029
18030 public int scrollBarSize;
18031 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018032 public float[] interpolatorValues;
18033 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018034
18035 public final Paint paint;
18036 public final Matrix matrix;
18037 public Shader shader;
18038
Mike Cleronf116bf82009-09-27 19:14:12 -070018039 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18040
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018041 private static final float[] OPAQUE = { 255 };
18042 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018043
Mike Cleronf116bf82009-09-27 19:14:12 -070018044 /**
18045 * When fading should start. This time moves into the future every time
18046 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18047 */
18048 public long fadeStartTime;
18049
18050
18051 /**
18052 * The current state of the scrollbars: ON, OFF, or FADING
18053 */
18054 public int state = OFF;
18055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018056 private int mLastColor;
18057
Mike Cleronf116bf82009-09-27 19:14:12 -070018058 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018059 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18060 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018061 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18062 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018063
18064 paint = new Paint();
18065 matrix = new Matrix();
18066 // use use a height of 1, and then wack the matrix each time we
18067 // actually use it.
18068 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018069 paint.setShader(shader);
18070 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018071
Mike Cleronf116bf82009-09-27 19:14:12 -070018072 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018073 }
Romain Guy8506ab42009-06-11 17:35:47 -070018074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018075 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018076 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018077 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018078
Romain Guyd679b572012-08-29 21:49:00 -070018079 if (color != 0) {
18080 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18081 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18082 paint.setShader(shader);
18083 // Restore the default transfer mode (src_over)
18084 paint.setXfermode(null);
18085 } else {
18086 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18087 paint.setShader(shader);
18088 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018090 }
18091 }
Joe Malin32736f02011-01-19 16:14:20 -080018092
Mike Cleronf116bf82009-09-27 19:14:12 -070018093 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018094 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018095 if (now >= fadeStartTime) {
18096
18097 // the animation fades the scrollbars out by changing
18098 // the opacity (alpha) from fully opaque to fully
18099 // transparent
18100 int nextFrame = (int) now;
18101 int framesCount = 0;
18102
18103 Interpolator interpolator = scrollBarInterpolator;
18104
18105 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018106 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018107
18108 // End transparent
18109 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018110 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018111
18112 state = FADING;
18113
18114 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018115 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018116 }
18117 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018118 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018119
Svetoslav Ganova0156172011-06-26 17:55:44 -070018120 /**
18121 * Resuable callback for sending
18122 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18123 */
18124 private class SendViewScrolledAccessibilityEvent implements Runnable {
18125 public volatile boolean mIsPending;
18126
18127 public void run() {
18128 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18129 mIsPending = false;
18130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018131 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018132
18133 /**
18134 * <p>
18135 * This class represents a delegate that can be registered in a {@link View}
18136 * to enhance accessibility support via composition rather via inheritance.
18137 * It is specifically targeted to widget developers that extend basic View
18138 * classes i.e. classes in package android.view, that would like their
18139 * applications to be backwards compatible.
18140 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018141 * <div class="special reference">
18142 * <h3>Developer Guides</h3>
18143 * <p>For more information about making applications accessible, read the
18144 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18145 * developer guide.</p>
18146 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018147 * <p>
18148 * A scenario in which a developer would like to use an accessibility delegate
18149 * is overriding a method introduced in a later API version then the minimal API
18150 * version supported by the application. For example, the method
18151 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18152 * in API version 4 when the accessibility APIs were first introduced. If a
18153 * developer would like his application to run on API version 4 devices (assuming
18154 * all other APIs used by the application are version 4 or lower) and take advantage
18155 * of this method, instead of overriding the method which would break the application's
18156 * backwards compatibility, he can override the corresponding method in this
18157 * delegate and register the delegate in the target View if the API version of
18158 * the system is high enough i.e. the API version is same or higher to the API
18159 * version that introduced
18160 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18161 * </p>
18162 * <p>
18163 * Here is an example implementation:
18164 * </p>
18165 * <code><pre><p>
18166 * if (Build.VERSION.SDK_INT >= 14) {
18167 * // If the API version is equal of higher than the version in
18168 * // which onInitializeAccessibilityNodeInfo was introduced we
18169 * // register a delegate with a customized implementation.
18170 * View view = findViewById(R.id.view_id);
18171 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18172 * public void onInitializeAccessibilityNodeInfo(View host,
18173 * AccessibilityNodeInfo info) {
18174 * // Let the default implementation populate the info.
18175 * super.onInitializeAccessibilityNodeInfo(host, info);
18176 * // Set some other information.
18177 * info.setEnabled(host.isEnabled());
18178 * }
18179 * });
18180 * }
18181 * </code></pre></p>
18182 * <p>
18183 * This delegate contains methods that correspond to the accessibility methods
18184 * in View. If a delegate has been specified the implementation in View hands
18185 * off handling to the corresponding method in this delegate. The default
18186 * implementation the delegate methods behaves exactly as the corresponding
18187 * method in View for the case of no accessibility delegate been set. Hence,
18188 * to customize the behavior of a View method, clients can override only the
18189 * corresponding delegate method without altering the behavior of the rest
18190 * accessibility related methods of the host view.
18191 * </p>
18192 */
18193 public static class AccessibilityDelegate {
18194
18195 /**
18196 * Sends an accessibility event of the given type. If accessibility is not
18197 * enabled this method has no effect.
18198 * <p>
18199 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18200 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18201 * been set.
18202 * </p>
18203 *
18204 * @param host The View hosting the delegate.
18205 * @param eventType The type of the event to send.
18206 *
18207 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18208 */
18209 public void sendAccessibilityEvent(View host, int eventType) {
18210 host.sendAccessibilityEventInternal(eventType);
18211 }
18212
18213 /**
alanv8eeefef2012-05-07 16:57:53 -070018214 * Performs the specified accessibility action on the view. For
18215 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18216 * <p>
18217 * The default implementation behaves as
18218 * {@link View#performAccessibilityAction(int, Bundle)
18219 * View#performAccessibilityAction(int, Bundle)} for the case of
18220 * no accessibility delegate been set.
18221 * </p>
18222 *
18223 * @param action The action to perform.
18224 * @return Whether the action was performed.
18225 *
18226 * @see View#performAccessibilityAction(int, Bundle)
18227 * View#performAccessibilityAction(int, Bundle)
18228 */
18229 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18230 return host.performAccessibilityActionInternal(action, args);
18231 }
18232
18233 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018234 * Sends an accessibility event. This method behaves exactly as
18235 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18236 * empty {@link AccessibilityEvent} and does not perform a check whether
18237 * accessibility is enabled.
18238 * <p>
18239 * The default implementation behaves as
18240 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18241 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18242 * the case of no accessibility delegate been set.
18243 * </p>
18244 *
18245 * @param host The View hosting the delegate.
18246 * @param event The event to send.
18247 *
18248 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18249 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18250 */
18251 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18252 host.sendAccessibilityEventUncheckedInternal(event);
18253 }
18254
18255 /**
18256 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18257 * to its children for adding their text content to the event.
18258 * <p>
18259 * The default implementation behaves as
18260 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18261 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18262 * the case of no accessibility delegate been set.
18263 * </p>
18264 *
18265 * @param host The View hosting the delegate.
18266 * @param event The event.
18267 * @return True if the event population was completed.
18268 *
18269 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18270 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18271 */
18272 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18273 return host.dispatchPopulateAccessibilityEventInternal(event);
18274 }
18275
18276 /**
18277 * Gives a chance to the host View to populate the accessibility event with its
18278 * text content.
18279 * <p>
18280 * The default implementation behaves as
18281 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18282 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18283 * the case of no accessibility delegate been set.
18284 * </p>
18285 *
18286 * @param host The View hosting the delegate.
18287 * @param event The accessibility event which to populate.
18288 *
18289 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18290 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18291 */
18292 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18293 host.onPopulateAccessibilityEventInternal(event);
18294 }
18295
18296 /**
18297 * Initializes an {@link AccessibilityEvent} with information about the
18298 * the host View which is the event source.
18299 * <p>
18300 * The default implementation behaves as
18301 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18302 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18303 * the case of no accessibility delegate been set.
18304 * </p>
18305 *
18306 * @param host The View hosting the delegate.
18307 * @param event The event to initialize.
18308 *
18309 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18310 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18311 */
18312 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18313 host.onInitializeAccessibilityEventInternal(event);
18314 }
18315
18316 /**
18317 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18318 * <p>
18319 * The default implementation behaves as
18320 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18321 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18322 * the case of no accessibility delegate been set.
18323 * </p>
18324 *
18325 * @param host The View hosting the delegate.
18326 * @param info The instance to initialize.
18327 *
18328 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18329 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18330 */
18331 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18332 host.onInitializeAccessibilityNodeInfoInternal(info);
18333 }
18334
18335 /**
18336 * Called when a child of the host View has requested sending an
18337 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18338 * to augment the event.
18339 * <p>
18340 * The default implementation behaves as
18341 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18342 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18343 * the case of no accessibility delegate been set.
18344 * </p>
18345 *
18346 * @param host The View hosting the delegate.
18347 * @param child The child which requests sending the event.
18348 * @param event The event to be sent.
18349 * @return True if the event should be sent
18350 *
18351 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18352 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18353 */
18354 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18355 AccessibilityEvent event) {
18356 return host.onRequestSendAccessibilityEventInternal(child, event);
18357 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018358
18359 /**
18360 * Gets the provider for managing a virtual view hierarchy rooted at this View
18361 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18362 * that explore the window content.
18363 * <p>
18364 * The default implementation behaves as
18365 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18366 * the case of no accessibility delegate been set.
18367 * </p>
18368 *
18369 * @return The provider.
18370 *
18371 * @see AccessibilityNodeProvider
18372 */
18373 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18374 return null;
18375 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018376 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018377
18378 private class MatchIdPredicate implements Predicate<View> {
18379 public int mId;
18380
18381 @Override
18382 public boolean apply(View view) {
18383 return (view.mID == mId);
18384 }
18385 }
18386
18387 private class MatchLabelForPredicate implements Predicate<View> {
18388 private int mLabeledId;
18389
18390 @Override
18391 public boolean apply(View view) {
18392 return (view.mLabelForId == mLabeledId);
18393 }
18394 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018395
18396 /**
18397 * Dump all private flags in readable format, useful for documentation and
18398 * sanity checking.
18399 */
18400 private static void dumpFlags() {
18401 final HashMap<String, String> found = Maps.newHashMap();
18402 try {
18403 for (Field field : View.class.getDeclaredFields()) {
18404 final int modifiers = field.getModifiers();
18405 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18406 if (field.getType().equals(int.class)) {
18407 final int value = field.getInt(null);
18408 dumpFlag(found, field.getName(), value);
18409 } else if (field.getType().equals(int[].class)) {
18410 final int[] values = (int[]) field.get(null);
18411 for (int i = 0; i < values.length; i++) {
18412 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18413 }
18414 }
18415 }
18416 }
18417 } catch (IllegalAccessException e) {
18418 throw new RuntimeException(e);
18419 }
18420
18421 final ArrayList<String> keys = Lists.newArrayList();
18422 keys.addAll(found.keySet());
18423 Collections.sort(keys);
18424 for (String key : keys) {
18425 Log.d(VIEW_LOG_TAG, found.get(key));
18426 }
18427 }
18428
18429 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18430 // Sort flags by prefix, then by bits, always keeping unique keys
18431 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18432 final int prefix = name.indexOf('_');
18433 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18434 final String output = bits + " " + name;
18435 found.put(key, output);
18436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018437}