blob: 50638aaf449173aec893256a6022f89a79ca6f0f [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;
Adam Powell7da4b732012-12-07 15:28:33 -080043import android.os.Build;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070044import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Handler;
46import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Parcel;
48import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070051import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070052import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070054import android.util.FloatProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.Log;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080056import android.util.Pools.SynchronizedPool;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070061import android.view.AccessibilityIterators.TextSegmentIterator;
62import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
63import android.view.AccessibilityIterators.WordTextSegmentIterator;
64import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070065import android.view.accessibility.AccessibilityEvent;
66import android.view.accessibility.AccessibilityEventSource;
67import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070068import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070069import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070071import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080072import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputConnection;
75import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.widget.ScrollBarDrawable;
77
Romain Guy1ef3fdb2011-09-09 15:30:30 -070078import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070079import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070080
Doug Feltcb3791202011-07-07 11:57:48 -070081import com.android.internal.R;
82import com.android.internal.util.Predicate;
83import com.android.internal.view.menu.MenuBuilder;
Jeff Sharkey36901b62012-09-19 19:06:22 -070084import com.google.android.collect.Lists;
85import com.google.android.collect.Maps;
Doug Feltcb3791202011-07-07 11:57:48 -070086
Christopher Tatea0374192010-10-05 13:06:41 -070087import java.lang.ref.WeakReference;
Jeff Sharkey36901b62012-09-19 19:06:22 -070088import java.lang.reflect.Field;
svetoslavganov75986cf2009-05-14 22:28:01 -070089import java.lang.reflect.InvocationTargetException;
90import java.lang.reflect.Method;
Jeff Sharkey36901b62012-09-19 19:06:22 -070091import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.util.ArrayList;
93import java.util.Arrays;
Jeff Sharkey36901b62012-09-19 19:06:22 -070094import java.util.Collections;
95import java.util.HashMap;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070096import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080097import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070098import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
100/**
101 * <p>
102 * This class represents the basic building block for user interface components. A View
103 * occupies a rectangular area on the screen and is responsible for drawing and
104 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700105 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
107 * are invisible containers that hold other Views (or other ViewGroups) and define
108 * their layout properties.
109 * </p>
110 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700111 * <div class="special reference">
112 * <h3>Developer Guides</h3>
113 * <p>For information about using this class to develop your application's user interface,
114 * 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 -0800115 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * <a name="Using"></a>
118 * <h3>Using Views</h3>
119 * <p>
120 * All of the views in a window are arranged in a single tree. You can add views
121 * either from code or by specifying a tree of views in one or more XML layout
122 * files. There are many specialized subclasses of views that act as controls or
123 * are capable of displaying text, images, or other content.
124 * </p>
125 * <p>
126 * Once you have created a tree of views, there are typically a few types of
127 * common operations you may wish to perform:
128 * <ul>
129 * <li><strong>Set properties:</strong> for example setting the text of a
130 * {@link android.widget.TextView}. The available properties and the methods
131 * that set them will vary among the different subclasses of views. Note that
132 * properties that are known at build time can be set in the XML layout
133 * files.</li>
134 * <li><strong>Set focus:</strong> The framework will handled moving focus in
135 * response to user input. To force focus to a specific view, call
136 * {@link #requestFocus}.</li>
137 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
138 * that will be notified when something interesting happens to the view. For
139 * example, all views will let you set a listener to be notified when the view
140 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700141 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700142 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700143 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700145 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * </ul>
147 * </p>
148 * <p><em>
149 * Note: The Android framework is responsible for measuring, laying out and
150 * drawing views. You should not call methods that perform these actions on
151 * views yourself unless you are actually implementing a
152 * {@link android.view.ViewGroup}.
153 * </em></p>
154 *
155 * <a name="Lifecycle"></a>
156 * <h3>Implementing a Custom View</h3>
157 *
158 * <p>
159 * To implement a custom view, you will usually begin by providing overrides for
160 * some of the standard methods that the framework calls on all views. You do
161 * not need to override all of these methods. In fact, you can start by just
162 * overriding {@link #onDraw(android.graphics.Canvas)}.
163 * <table border="2" width="85%" align="center" cellpadding="5">
164 * <thead>
165 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
166 * </thead>
167 *
168 * <tbody>
169 * <tr>
170 * <td rowspan="2">Creation</td>
171 * <td>Constructors</td>
172 * <td>There is a form of the constructor that are called when the view
173 * is created from code and a form that is called when the view is
174 * inflated from a layout file. The second form should parse and apply
175 * any attributes defined in the layout file.
176 * </td>
177 * </tr>
178 * <tr>
179 * <td><code>{@link #onFinishInflate()}</code></td>
180 * <td>Called after a view and all of its children has been inflated
181 * from XML.</td>
182 * </tr>
183 *
184 * <tr>
185 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called to determine the size requirements for this view and all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when this view should assign a size and position to all
194 * of its children.
195 * </td>
196 * </tr>
197 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700198 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * <td>Called when the size of this view has changed.
200 * </td>
201 * </tr>
202 *
203 * <tr>
204 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when the view should render its content.
207 * </td>
208 * </tr>
209 *
210 * <tr>
211 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900213 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900218 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * </td>
220 * </tr>
221 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when a trackball motion event occurs.
224 * </td>
225 * </tr>
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when a touch screen motion event occurs.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700240 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 * <td>Called when the window containing the view gains or loses focus.
242 * </td>
243 * </tr>
244 *
245 * <tr>
246 * <td rowspan="3">Attaching</td>
247 * <td><code>{@link #onAttachedToWindow()}</code></td>
248 * <td>Called when the view is attached to a window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
253 * <td><code>{@link #onDetachedFromWindow}</code></td>
254 * <td>Called when the view is detached from its window.
255 * </td>
256 * </tr>
257 *
258 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700259 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * <td>Called when the visibility of the window containing the view
261 * has changed.
262 * </td>
263 * </tr>
264 * </tbody>
265 *
266 * </table>
267 * </p>
268 *
269 * <a name="IDs"></a>
270 * <h3>IDs</h3>
271 * Views may have an integer id associated with them. These ids are typically
272 * assigned in the layout XML files, and are used to find specific views within
273 * the view tree. A common pattern is to:
274 * <ul>
275 * <li>Define a Button in the layout file and assign it a unique ID.
276 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700277 * &lt;Button
278 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * android:layout_width="wrap_content"
280 * android:layout_height="wrap_content"
281 * android:text="@string/my_button_text"/&gt;
282 * </pre></li>
283 * <li>From the onCreate method of an Activity, find the Button
284 * <pre class="prettyprint">
285 * Button myButton = (Button) findViewById(R.id.my_button);
286 * </pre></li>
287 * </ul>
288 * <p>
289 * View IDs need not be unique throughout the tree, but it is good practice to
290 * ensure that they are at least unique within the part of the tree you are
291 * searching.
292 * </p>
293 *
294 * <a name="Position"></a>
295 * <h3>Position</h3>
296 * <p>
297 * The geometry of a view is that of a rectangle. A view has a location,
298 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
299 * two dimensions, expressed as a width and a height. The unit for location
300 * and dimensions is the pixel.
301 * </p>
302 *
303 * <p>
304 * It is possible to retrieve the location of a view by invoking the methods
305 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
306 * coordinate of the rectangle representing the view. The latter returns the
307 * top, or Y, coordinate of the rectangle representing the view. These methods
308 * both return the location of the view relative to its parent. For instance,
309 * when getLeft() returns 20, that means the view is located 20 pixels to the
310 * right of the left edge of its direct parent.
311 * </p>
312 *
313 * <p>
314 * In addition, several convenience methods are offered to avoid unnecessary
315 * computations, namely {@link #getRight()} and {@link #getBottom()}.
316 * These methods return the coordinates of the right and bottom edges of the
317 * rectangle representing the view. For instance, calling {@link #getRight()}
318 * is similar to the following computation: <code>getLeft() + getWidth()</code>
319 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
320 * </p>
321 *
322 * <a name="SizePaddingMargins"></a>
323 * <h3>Size, padding and margins</h3>
324 * <p>
325 * The size of a view is expressed with a width and a height. A view actually
326 * possess two pairs of width and height values.
327 * </p>
328 *
329 * <p>
330 * The first pair is known as <em>measured width</em> and
331 * <em>measured height</em>. These dimensions define how big a view wants to be
332 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
333 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
334 * and {@link #getMeasuredHeight()}.
335 * </p>
336 *
337 * <p>
338 * The second pair is simply known as <em>width</em> and <em>height</em>, or
339 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
340 * dimensions define the actual size of the view on screen, at drawing time and
341 * after layout. These values may, but do not have to, be different from the
342 * measured width and height. The width and height can be obtained by calling
343 * {@link #getWidth()} and {@link #getHeight()}.
344 * </p>
345 *
346 * <p>
347 * To measure its dimensions, a view takes into account its padding. The padding
348 * is expressed in pixels for the left, top, right and bottom parts of the view.
349 * Padding can be used to offset the content of the view by a specific amount of
350 * pixels. For instance, a left padding of 2 will push the view's content by
351 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700352 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
353 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
354 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
355 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * </p>
357 *
358 * <p>
359 * Even though a view can define a padding, it does not provide any support for
360 * margins. However, view groups provide such a support. Refer to
361 * {@link android.view.ViewGroup} and
362 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
363 * </p>
364 *
365 * <a name="Layout"></a>
366 * <h3>Layout</h3>
367 * <p>
368 * Layout is a two pass process: a measure pass and a layout pass. The measuring
369 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
370 * of the view tree. Each view pushes dimension specifications down the tree
371 * during the recursion. At the end of the measure pass, every view has stored
372 * its measurements. The second pass happens in
373 * {@link #layout(int,int,int,int)} and is also top-down. During
374 * this pass each parent is responsible for positioning all of its children
375 * using the sizes computed in the measure pass.
376 * </p>
377 *
378 * <p>
379 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
380 * {@link #getMeasuredHeight()} values must be set, along with those for all of
381 * that view's descendants. A view's measured width and measured height values
382 * must respect the constraints imposed by the view's parents. This guarantees
383 * that at the end of the measure pass, all parents accept all of their
384 * children's measurements. A parent view may call measure() more than once on
385 * its children. For example, the parent may measure each child once with
386 * unspecified dimensions to find out how big they want to be, then call
387 * measure() on them again with actual numbers if the sum of all the children's
388 * unconstrained sizes is too big or too small.
389 * </p>
390 *
391 * <p>
392 * The measure pass uses two classes to communicate dimensions. The
393 * {@link MeasureSpec} class is used by views to tell their parents how they
394 * want to be measured and positioned. The base LayoutParams class just
395 * describes how big the view wants to be for both width and height. For each
396 * dimension, it can specify one of:
397 * <ul>
398 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800399 * <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 -0800400 * (minus padding)
401 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
402 * enclose its content (plus padding).
403 * </ul>
404 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
405 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
406 * an X and Y value.
407 * </p>
408 *
409 * <p>
410 * MeasureSpecs are used to push requirements down the tree from parent to
411 * child. A MeasureSpec can be in one of three modes:
412 * <ul>
413 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
414 * of a child view. For example, a LinearLayout may call measure() on its child
415 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
416 * tall the child view wants to be given a width of 240 pixels.
417 * <li>EXACTLY: This is used by the parent to impose an exact size on the
418 * child. The child must use this size, and guarantee that all of its
419 * descendants will fit within this size.
420 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
421 * child. The child must gurantee that it and all of its descendants will fit
422 * within this size.
423 * </ul>
424 * </p>
425 *
426 * <p>
427 * To intiate a layout, call {@link #requestLayout}. This method is typically
428 * called by a view on itself when it believes that is can no longer fit within
429 * its current bounds.
430 * </p>
431 *
432 * <a name="Drawing"></a>
433 * <h3>Drawing</h3>
434 * <p>
435 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700436 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * this means that parents will draw before (i.e., behind) their children, with
438 * siblings drawn in the order they appear in the tree.
439 * If you set a background drawable for a View, then the View will draw it for you
440 * before calling back to its <code>onDraw()</code> method.
441 * </p>
442 *
443 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700444 * 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 -0800445 * </p>
446 *
447 * <p>
448 * To force a view to draw, call {@link #invalidate()}.
449 * </p>
450 *
451 * <a name="EventHandlingThreading"></a>
452 * <h3>Event Handling and Threading</h3>
453 * <p>
454 * The basic cycle of a view is as follows:
455 * <ol>
456 * <li>An event comes in and is dispatched to the appropriate view. The view
457 * handles the event and notifies any listeners.</li>
458 * <li>If in the course of processing the event, the view's bounds may need
459 * to be changed, the view will call {@link #requestLayout()}.</li>
460 * <li>Similarly, if in the course of processing the event the view's appearance
461 * may need to be changed, the view will call {@link #invalidate()}.</li>
462 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
463 * the framework will take care of measuring, laying out, and drawing the tree
464 * as appropriate.</li>
465 * </ol>
466 * </p>
467 *
468 * <p><em>Note: The entire view tree is single threaded. You must always be on
469 * the UI thread when calling any method on any view.</em>
470 * If you are doing work on other threads and want to update the state of a view
471 * from that thread, you should use a {@link Handler}.
472 * </p>
473 *
474 * <a name="FocusHandling"></a>
475 * <h3>Focus Handling</h3>
476 * <p>
477 * The framework will handle routine focus movement in response to user input.
478 * This includes changing the focus as views are removed or hidden, or as new
479 * views become available. Views indicate their willingness to take focus
480 * through the {@link #isFocusable} method. To change whether a view can take
481 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
482 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
483 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
484 * </p>
485 * <p>
486 * Focus movement is based on an algorithm which finds the nearest neighbor in a
487 * given direction. In rare cases, the default algorithm may not match the
488 * intended behavior of the developer. In these situations, you can provide
489 * explicit overrides by using these XML attributes in the layout file:
490 * <pre>
491 * nextFocusDown
492 * nextFocusLeft
493 * nextFocusRight
494 * nextFocusUp
495 * </pre>
496 * </p>
497 *
498 *
499 * <p>
500 * To get a particular view to take focus, call {@link #requestFocus()}.
501 * </p>
502 *
503 * <a name="TouchMode"></a>
504 * <h3>Touch Mode</h3>
505 * <p>
506 * When a user is navigating a user interface via directional keys such as a D-pad, it is
507 * necessary to give focus to actionable items such as buttons so the user can see
508 * what will take input. If the device has touch capabilities, however, and the user
509 * begins interacting with the interface by touching it, it is no longer necessary to
510 * always highlight, or give focus to, a particular view. This motivates a mode
511 * for interaction named 'touch mode'.
512 * </p>
513 * <p>
514 * For a touch capable device, once the user touches the screen, the device
515 * will enter touch mode. From this point onward, only views for which
516 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
517 * Other views that are touchable, like buttons, will not take focus when touched; they will
518 * only fire the on click listeners.
519 * </p>
520 * <p>
521 * Any time a user hits a directional key, such as a D-pad direction, the view device will
522 * exit touch mode, and find a view to take focus, so that the user may resume interacting
523 * with the user interface without touching the screen again.
524 * </p>
525 * <p>
526 * The touch mode state is maintained across {@link android.app.Activity}s. Call
527 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
528 * </p>
529 *
530 * <a name="Scrolling"></a>
531 * <h3>Scrolling</h3>
532 * <p>
533 * The framework provides basic support for views that wish to internally
534 * scroll their content. This includes keeping track of the X and Y scroll
535 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800536 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700537 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 * </p>
539 *
540 * <a name="Tags"></a>
541 * <h3>Tags</h3>
542 * <p>
543 * Unlike IDs, tags are not used to identify views. Tags are essentially an
544 * extra piece of information that can be associated with a view. They are most
545 * often used as a convenience to store data related to views in the views
546 * themselves rather than by putting them in a separate structure.
547 * </p>
548 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700549 * <a name="Properties"></a>
550 * <h3>Properties</h3>
551 * <p>
552 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
553 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
554 * available both in the {@link Property} form as well as in similarly-named setter/getter
555 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
556 * be used to set persistent state associated with these rendering-related properties on the view.
557 * The properties and methods can also be used in conjunction with
558 * {@link android.animation.Animator Animator}-based animations, described more in the
559 * <a href="#Animation">Animation</a> section.
560 * </p>
561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * <a name="Animation"></a>
563 * <h3>Animation</h3>
564 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700565 * Starting with Android 3.0, the preferred way of animating views is to use the
566 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
567 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
568 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
569 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
570 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
571 * makes animating these View properties particularly easy and efficient.
572 * </p>
573 * <p>
574 * 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 -0800575 * You can attach an {@link Animation} object to a view using
576 * {@link #setAnimation(Animation)} or
577 * {@link #startAnimation(Animation)}. The animation can alter the scale,
578 * rotation, translation and alpha of a view over time. If the animation is
579 * attached to a view that has children, the animation will affect the entire
580 * subtree rooted by that node. When an animation is started, the framework will
581 * take care of redrawing the appropriate views until the animation completes.
582 * </p>
583 *
Jeff Brown85a31762010-09-01 17:01:00 -0700584 * <a name="Security"></a>
585 * <h3>Security</h3>
586 * <p>
587 * Sometimes it is essential that an application be able to verify that an action
588 * is being performed with the full knowledge and consent of the user, such as
589 * granting a permission request, making a purchase or clicking on an advertisement.
590 * Unfortunately, a malicious application could try to spoof the user into
591 * performing these actions, unaware, by concealing the intended purpose of the view.
592 * As a remedy, the framework offers a touch filtering mechanism that can be used to
593 * improve the security of views that provide access to sensitive functionality.
594 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800596 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * will discard touches that are received whenever the view's window is obscured by
598 * another visible window. As a result, the view will not receive touches whenever a
599 * toast, dialog or other window appears above the view's window.
600 * </p><p>
601 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700602 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
603 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700604 * </p>
605 *
Romain Guy171c5922011-01-06 10:04:23 -0800606 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700607 * @attr ref android.R.styleable#View_background
608 * @attr ref android.R.styleable#View_clickable
609 * @attr ref android.R.styleable#View_contentDescription
610 * @attr ref android.R.styleable#View_drawingCacheQuality
611 * @attr ref android.R.styleable#View_duplicateParentState
612 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700613 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700614 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700615 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700616 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_isScrollContainer
619 * @attr ref android.R.styleable#View_focusable
620 * @attr ref android.R.styleable#View_focusableInTouchMode
621 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
622 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800623 * @attr ref android.R.styleable#View_layerType
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800624 * @attr ref android.R.styleable#View_layoutDirection
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_longClickable
626 * @attr ref android.R.styleable#View_minHeight
627 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 * @attr ref android.R.styleable#View_nextFocusDown
629 * @attr ref android.R.styleable#View_nextFocusLeft
630 * @attr ref android.R.styleable#View_nextFocusRight
631 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700632 * @attr ref android.R.styleable#View_onClick
633 * @attr ref android.R.styleable#View_padding
634 * @attr ref android.R.styleable#View_paddingBottom
635 * @attr ref android.R.styleable#View_paddingLeft
636 * @attr ref android.R.styleable#View_paddingRight
637 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800638 * @attr ref android.R.styleable#View_paddingStart
639 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700640 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800641 * @attr ref android.R.styleable#View_rotation
642 * @attr ref android.R.styleable#View_rotationX
643 * @attr ref android.R.styleable#View_rotationY
644 * @attr ref android.R.styleable#View_scaleX
645 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 * @attr ref android.R.styleable#View_scrollX
647 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700648 * @attr ref android.R.styleable#View_scrollbarSize
649 * @attr ref android.R.styleable#View_scrollbarStyle
650 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700651 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
652 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
654 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 * @attr ref android.R.styleable#View_scrollbarThumbVertical
656 * @attr ref android.R.styleable#View_scrollbarTrackVertical
657 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
658 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700659 * @attr ref android.R.styleable#View_soundEffectsEnabled
660 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700661 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800662 * @attr ref android.R.styleable#View_textDirection
Chet Haase73066682010-11-29 15:55:32 -0800663 * @attr ref android.R.styleable#View_transformPivotX
664 * @attr ref android.R.styleable#View_transformPivotY
665 * @attr ref android.R.styleable#View_translationX
666 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700667 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 *
669 * @see android.view.ViewGroup
670 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700671public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700672 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 private static final boolean DBG = false;
674
675 /**
676 * The logging tag used by this class with android.util.Log.
677 */
678 protected static final String VIEW_LOG_TAG = "View";
679
680 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700681 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700682 *
683 * @hide
684 */
685 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
686
687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 * Used to mark a View that has no ID.
689 */
690 public static final int NO_ID = -1;
691
Adam Powell7da4b732012-12-07 15:28:33 -0800692 private static boolean sUseBrokenMakeMeasureSpec = false;
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 /**
695 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
696 * calling setFlags.
697 */
698 private static final int NOT_FOCUSABLE = 0x00000000;
699
700 /**
701 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
702 * setFlags.
703 */
704 private static final int FOCUSABLE = 0x00000001;
705
706 /**
707 * Mask for use with setFlags indicating bits used for focus.
708 */
709 private static final int FOCUSABLE_MASK = 0x00000001;
710
711 /**
712 * This view will adjust its padding to fit sytem windows (e.g. status bar)
713 */
714 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
715
716 /**
Scott Main812634c22011-07-27 13:22:35 -0700717 * This view is visible.
718 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int VISIBLE = 0x00000000;
722
723 /**
724 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700725 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
726 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 */
728 public static final int INVISIBLE = 0x00000004;
729
730 /**
731 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700732 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
733 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 */
735 public static final int GONE = 0x00000008;
736
737 /**
738 * Mask for use with setFlags indicating bits used for visibility.
739 * {@hide}
740 */
741 static final int VISIBILITY_MASK = 0x0000000C;
742
743 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
744
745 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700746 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 * Use with ENABLED_MASK when calling setFlags.
748 * {@hide}
749 */
750 static final int ENABLED = 0x00000000;
751
752 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700753 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 * Use with ENABLED_MASK when calling setFlags.
755 * {@hide}
756 */
757 static final int DISABLED = 0x00000020;
758
759 /**
760 * Mask for use with setFlags indicating bits used for indicating whether
761 * this view is enabled
762 * {@hide}
763 */
764 static final int ENABLED_MASK = 0x00000020;
765
766 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700767 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
768 * called and further optimizations will be performed. It is okay to have
769 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 * {@hide}
771 */
772 static final int WILL_NOT_DRAW = 0x00000080;
773
774 /**
775 * Mask for use with setFlags indicating bits used for indicating whether
776 * this view is will draw
777 * {@hide}
778 */
779 static final int DRAW_MASK = 0x00000080;
780
781 /**
782 * <p>This view doesn't show scrollbars.</p>
783 * {@hide}
784 */
785 static final int SCROLLBARS_NONE = 0x00000000;
786
787 /**
788 * <p>This view shows horizontal scrollbars.</p>
789 * {@hide}
790 */
791 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
792
793 /**
794 * <p>This view shows vertical scrollbars.</p>
795 * {@hide}
796 */
797 static final int SCROLLBARS_VERTICAL = 0x00000200;
798
799 /**
800 * <p>Mask for use with setFlags indicating bits used for indicating which
801 * scrollbars are enabled.</p>
802 * {@hide}
803 */
804 static final int SCROLLBARS_MASK = 0x00000300;
805
Jeff Brown85a31762010-09-01 17:01:00 -0700806 /**
807 * Indicates that the view should filter touches when its window is obscured.
808 * Refer to the class comments for more information about this security feature.
809 * {@hide}
810 */
811 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
812
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700813 /**
814 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
815 * that they are optional and should be skipped if the window has
816 * requested system UI flags that ignore those insets for layout.
817 */
818 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820 /**
821 * <p>This view doesn't show fading edges.</p>
822 * {@hide}
823 */
824 static final int FADING_EDGE_NONE = 0x00000000;
825
826 /**
827 * <p>This view shows horizontal fading edges.</p>
828 * {@hide}
829 */
830 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
831
832 /**
833 * <p>This view shows vertical fading edges.</p>
834 * {@hide}
835 */
836 static final int FADING_EDGE_VERTICAL = 0x00002000;
837
838 /**
839 * <p>Mask for use with setFlags indicating bits used for indicating which
840 * fading edges are enabled.</p>
841 * {@hide}
842 */
843 static final int FADING_EDGE_MASK = 0x00003000;
844
845 /**
846 * <p>Indicates this view can be clicked. When clickable, a View reacts
847 * to clicks by notifying the OnClickListener.<p>
848 * {@hide}
849 */
850 static final int CLICKABLE = 0x00004000;
851
852 /**
853 * <p>Indicates this view is caching its drawing into a bitmap.</p>
854 * {@hide}
855 */
856 static final int DRAWING_CACHE_ENABLED = 0x00008000;
857
858 /**
859 * <p>Indicates that no icicle should be saved for this view.<p>
860 * {@hide}
861 */
862 static final int SAVE_DISABLED = 0x000010000;
863
864 /**
865 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
866 * property.</p>
867 * {@hide}
868 */
869 static final int SAVE_DISABLED_MASK = 0x000010000;
870
871 /**
872 * <p>Indicates that no drawing cache should ever be created for this view.<p>
873 * {@hide}
874 */
875 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
876
877 /**
878 * <p>Indicates this view can take / keep focus when int touch mode.</p>
879 * {@hide}
880 */
881 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
882
883 /**
884 * <p>Enables low quality mode for the drawing cache.</p>
885 */
886 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
887
888 /**
889 * <p>Enables high quality mode for the drawing cache.</p>
890 */
891 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
892
893 /**
894 * <p>Enables automatic quality mode for the drawing cache.</p>
895 */
896 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
897
898 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
899 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
900 };
901
902 /**
903 * <p>Mask for use with setFlags indicating bits used for the cache
904 * quality property.</p>
905 * {@hide}
906 */
907 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
908
909 /**
910 * <p>
911 * Indicates this view can be long clicked. When long clickable, a View
912 * reacts to long clicks by notifying the OnLongClickListener or showing a
913 * context menu.
914 * </p>
915 * {@hide}
916 */
917 static final int LONG_CLICKABLE = 0x00200000;
918
919 /**
920 * <p>Indicates that this view gets its drawable states from its direct parent
921 * and ignores its original internal states.</p>
922 *
923 * @hide
924 */
925 static final int DUPLICATE_PARENT_STATE = 0x00400000;
926
927 /**
928 * The scrollbar style to display the scrollbars inside the content area,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency on the view's content.
931 */
932 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
933
934 /**
935 * The scrollbar style to display the scrollbars inside the padded area,
936 * increasing the padding of the view. The scrollbars will not overlap the
937 * content area of the view.
938 */
939 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
940
941 /**
942 * The scrollbar style to display the scrollbars at the edge of the view,
943 * without increasing the padding. The scrollbars will be overlaid with
944 * translucency.
945 */
946 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
947
948 /**
949 * The scrollbar style to display the scrollbars at the edge of the view,
950 * increasing the padding of the view. The scrollbars will only overlap the
951 * background, if any.
952 */
953 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
954
955 /**
956 * Mask to check if the scrollbar style is overlay or inset.
957 * {@hide}
958 */
959 static final int SCROLLBARS_INSET_MASK = 0x01000000;
960
961 /**
962 * Mask to check if the scrollbar style is inside or outside.
963 * {@hide}
964 */
965 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
966
967 /**
968 * Mask for scrollbar style.
969 * {@hide}
970 */
971 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
972
973 /**
974 * View flag indicating that the screen should remain on while the
975 * window containing this view is visible to the user. This effectively
976 * takes care of automatically setting the WindowManager's
977 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
978 */
979 public static final int KEEP_SCREEN_ON = 0x04000000;
980
981 /**
982 * View flag indicating whether this view should have sound effects enabled
983 * for events such as clicking and touching.
984 */
985 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
986
987 /**
988 * View flag indicating whether this view should have haptic feedback
989 * enabled for events such as long presses.
990 */
991 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
992
993 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700994 * <p>Indicates that the view hierarchy should stop saving state when
995 * it reaches this view. If state saving is initiated immediately at
996 * the view, it will be allowed.
997 * {@hide}
998 */
999 static final int PARENT_SAVE_DISABLED = 0x20000000;
1000
1001 /**
1002 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
1003 * {@hide}
1004 */
1005 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
1006
1007 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001008 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1009 * should add all focusable Views regardless if they are focusable in touch mode.
1010 */
1011 public static final int FOCUSABLES_ALL = 0x00000000;
1012
1013 /**
1014 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1015 * should add only Views focusable in touch mode.
1016 */
1017 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_BACKWARD = 0x00000001;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 * item.
1028 */
1029 public static final int FOCUS_FORWARD = 0x00000002;
1030
1031 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001032 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 */
1034 public static final int FOCUS_LEFT = 0x00000011;
1035
1036 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001037 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 */
1039 public static final int FOCUS_UP = 0x00000021;
1040
1041 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001042 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 */
1044 public static final int FOCUS_RIGHT = 0x00000042;
1045
1046 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001047 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 */
1049 public static final int FOCUS_DOWN = 0x00000082;
1050
Svetoslav Ganov42138042012-03-20 11:51:39 -07001051 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001052 * Bits of {@link #getMeasuredWidthAndState()} and
1053 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1054 */
1055 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1056
1057 /**
1058 * Bits of {@link #getMeasuredWidthAndState()} and
1059 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1060 */
1061 public static final int MEASURED_STATE_MASK = 0xff000000;
1062
1063 /**
1064 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1065 * for functions that combine both width and height into a single int,
1066 * such as {@link #getMeasuredState()} and the childState argument of
1067 * {@link #resolveSizeAndState(int, int, int)}.
1068 */
1069 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1070
1071 /**
1072 * Bit of {@link #getMeasuredWidthAndState()} and
1073 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1074 * is smaller that the space the view would like to have.
1075 */
1076 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1077
1078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 * Base View state sets
1080 */
1081 // Singles
1082 /**
1083 * Indicates the view has no states set. States are used with
1084 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1085 * view depending on its state.
1086 *
1087 * @see android.graphics.drawable.Drawable
1088 * @see #getDrawableState()
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled. States are used with
1093 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1094 * view depending on its state.
1095 *
1096 * @see android.graphics.drawable.Drawable
1097 * @see #getDrawableState()
1098 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001099 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /**
1101 * Indicates the view is focused. States are used with
1102 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1103 * view depending on its state.
1104 *
1105 * @see android.graphics.drawable.Drawable
1106 * @see #getDrawableState()
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view is selected. States are used with
1111 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1112 * view depending on its state.
1113 *
1114 * @see android.graphics.drawable.Drawable
1115 * @see #getDrawableState()
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 /**
1119 * Indicates the view is pressed. States are used with
1120 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1121 * view depending on its state.
1122 *
1123 * @see android.graphics.drawable.Drawable
1124 * @see #getDrawableState()
1125 * @hide
1126 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001127 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 /**
1129 * Indicates the view's window has focus. States are used with
1130 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1131 * view depending on its state.
1132 *
1133 * @see android.graphics.drawable.Drawable
1134 * @see #getDrawableState()
1135 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001136 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 // Doubles
1138 /**
1139 * Indicates the view is enabled and has the focus.
1140 *
1141 * @see #ENABLED_STATE_SET
1142 * @see #FOCUSED_STATE_SET
1143 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001144 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 /**
1146 * Indicates the view is enabled and selected.
1147 *
1148 * @see #ENABLED_STATE_SET
1149 * @see #SELECTED_STATE_SET
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 /**
1153 * Indicates the view is enabled and that its window has focus.
1154 *
1155 * @see #ENABLED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is focused and selected.
1161 *
1162 * @see #FOCUSED_STATE_SET
1163 * @see #SELECTED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view has the focus and that its window has the focus.
1168 *
1169 * @see #FOCUSED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 /**
1174 * Indicates the view is selected and that its window has the focus.
1175 *
1176 * @see #SELECTED_STATE_SET
1177 * @see #WINDOW_FOCUSED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 // Triples
1181 /**
1182 * Indicates the view is enabled, focused and selected.
1183 *
1184 * @see #ENABLED_STATE_SET
1185 * @see #FOCUSED_STATE_SET
1186 * @see #SELECTED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is enabled, focused and its window has the focus.
1191 *
1192 * @see #ENABLED_STATE_SET
1193 * @see #FOCUSED_STATE_SET
1194 * @see #WINDOW_FOCUSED_STATE_SET
1195 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001196 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * Indicates the view is enabled, selected and its window has the focus.
1199 *
1200 * @see #ENABLED_STATE_SET
1201 * @see #SELECTED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is focused, selected and its window has the focus.
1207 *
1208 * @see #FOCUSED_STATE_SET
1209 * @see #SELECTED_STATE_SET
1210 * @see #WINDOW_FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is enabled, focused, selected and its window
1215 * has the focus.
1216 *
1217 * @see #ENABLED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and its window has the focus.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #WINDOW_FOCUSED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed and selected.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #SELECTED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed, selected and its window has the focus.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed and focused.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #FOCUSED_STATE_SET
1250 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001251 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * Indicates the view is pressed, focused and its window has the focus.
1254 *
1255 * @see #PRESSED_STATE_SET
1256 * @see #FOCUSED_STATE_SET
1257 * @see #WINDOW_FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed, focused and selected.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #SELECTED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed, focused, selected and its window has the focus.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #FOCUSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and enabled.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, enabled and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, enabled and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #ENABLED_STATE_SET
1297 * @see #SELECTED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, enabled, selected and its window has the
1302 * focus.
1303 *
1304 * @see #PRESSED_STATE_SET
1305 * @see #ENABLED_STATE_SET
1306 * @see #SELECTED_STATE_SET
1307 * @see #WINDOW_FOCUSED_STATE_SET
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 /**
1311 * Indicates the view is pressed, enabled and focused.
1312 *
1313 * @see #PRESSED_STATE_SET
1314 * @see #ENABLED_STATE_SET
1315 * @see #FOCUSED_STATE_SET
1316 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 /**
1319 * Indicates the view is pressed, enabled, focused and its window has the
1320 * focus.
1321 *
1322 * @see #PRESSED_STATE_SET
1323 * @see #ENABLED_STATE_SET
1324 * @see #FOCUSED_STATE_SET
1325 * @see #WINDOW_FOCUSED_STATE_SET
1326 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 /**
1329 * Indicates the view is pressed, enabled, focused and selected.
1330 *
1331 * @see #PRESSED_STATE_SET
1332 * @see #ENABLED_STATE_SET
1333 * @see #SELECTED_STATE_SET
1334 * @see #FOCUSED_STATE_SET
1335 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 /**
1338 * Indicates the view is pressed, enabled, focused, selected and its window
1339 * has the focus.
1340 *
1341 * @see #PRESSED_STATE_SET
1342 * @see #ENABLED_STATE_SET
1343 * @see #SELECTED_STATE_SET
1344 * @see #FOCUSED_STATE_SET
1345 * @see #WINDOW_FOCUSED_STATE_SET
1346 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348
1349 /**
1350 * The order here is very important to {@link #getDrawableState()}
1351 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001352 private static final int[][] VIEW_STATE_SETS;
1353
Romain Guyb051e892010-09-28 19:09:36 -07001354 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1355 static final int VIEW_STATE_SELECTED = 1 << 1;
1356 static final int VIEW_STATE_FOCUSED = 1 << 2;
1357 static final int VIEW_STATE_ENABLED = 1 << 3;
1358 static final int VIEW_STATE_PRESSED = 1 << 4;
1359 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001360 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001361 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001362 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1363 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001364
1365 static final int[] VIEW_STATE_IDS = new int[] {
1366 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1367 R.attr.state_selected, VIEW_STATE_SELECTED,
1368 R.attr.state_focused, VIEW_STATE_FOCUSED,
1369 R.attr.state_enabled, VIEW_STATE_ENABLED,
1370 R.attr.state_pressed, VIEW_STATE_PRESSED,
1371 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001372 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001373 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001374 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001375 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 };
1377
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001379 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1380 throw new IllegalStateException(
1381 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1382 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001384 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001386 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001388 orderedIds[i * 2] = viewState;
1389 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001390 }
1391 }
1392 }
Romain Guyb051e892010-09-28 19:09:36 -07001393 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1394 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1395 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396 int numBits = Integer.bitCount(i);
1397 int[] set = new int[numBits];
1398 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001399 for (int j = 0; j < orderedIds.length; j += 2) {
1400 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001401 set[pos++] = orderedIds[j];
1402 }
1403 }
1404 VIEW_STATE_SETS[i] = set;
1405 }
1406
1407 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1408 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1409 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1410 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1412 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1413 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1415 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1417 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_FOCUSED];
1420 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1421 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1423 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1425 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1427 | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1432 | VIEW_STATE_ENABLED];
1433 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1435 | VIEW_STATE_ENABLED];
1436 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1438 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1439
1440 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1441 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1443 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1445 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1452 | VIEW_STATE_PRESSED];
1453 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1455 | VIEW_STATE_PRESSED];
1456 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1458 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1472 | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1475 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1476 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1477 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1478 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1479 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1480 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1481 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1482 | VIEW_STATE_PRESSED];
1483 }
1484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001486 * Accessibility event types that are dispatched for text population.
1487 */
1488 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1489 AccessibilityEvent.TYPE_VIEW_CLICKED
1490 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1491 | AccessibilityEvent.TYPE_VIEW_SELECTED
1492 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1493 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1494 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001495 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001496 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001497 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001498 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1499 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001500
1501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 * Temporary Rect currently for use in setBackground(). This will probably
1503 * be extended in the future to hold our own class with more than just
1504 * a Rect. :)
1505 */
1506 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001507
1508 /**
1509 * Map used to store views' tags.
1510 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001511 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001514 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001515 */
1516 private static int sNextAccessibilityViewId;
1517
1518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 * The animation currently associated with this view.
1520 * @hide
1521 */
1522 protected Animation mCurrentAnimation = null;
1523
1524 /**
1525 * Width as measured during measure pass.
1526 * {@hide}
1527 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001528 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001529 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
1531 /**
1532 * Height as measured during measure pass.
1533 * {@hide}
1534 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001535 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001536 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537
1538 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001539 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1540 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1541 * its display list. This flag, used only when hw accelerated, allows us to clear the
1542 * flag while retaining this information until it's needed (at getDisplayList() time and
1543 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1544 *
1545 * {@hide}
1546 */
1547 boolean mRecreateDisplayList = false;
1548
1549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 * The view's identifier.
1551 * {@hide}
1552 *
1553 * @see #setId(int)
1554 * @see #getId()
1555 */
1556 @ViewDebug.ExportedProperty(resolveId = true)
1557 int mID = NO_ID;
1558
1559 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001560 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001561 */
1562 int mAccessibilityViewId = NO_ID;
1563
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 /**
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08001870 * Default horizontal layout direction.
1871 * @hide
1872 */
1873 static final int LAYOUT_DIRECTION_RESOLVED_DEFAULT = LAYOUT_DIRECTION_LTR;
1874
1875 /**
Adam Powell539ee872012-02-03 19:00:49 -08001876 * Indicates that the view is tracking some sort of transient state
1877 * that the app should not need to be aware of, but that the framework
1878 * should take special care to preserve.
1879 *
1880 * @hide
1881 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001882 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001883
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001884 /**
1885 * Text direction is inherited thru {@link ViewGroup}
1886 */
1887 public static final int TEXT_DIRECTION_INHERIT = 0;
1888
1889 /**
1890 * Text direction is using "first strong algorithm". The first strong directional character
1891 * determines the paragraph direction. If there is no strong directional character, the
1892 * paragraph direction is the view's resolved layout direction.
1893 */
1894 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1895
1896 /**
1897 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1898 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1899 * If there are neither, the paragraph direction is the view's resolved layout direction.
1900 */
1901 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1902
1903 /**
1904 * Text direction is forced to LTR.
1905 */
1906 public static final int TEXT_DIRECTION_LTR = 3;
1907
1908 /**
1909 * Text direction is forced to RTL.
1910 */
1911 public static final int TEXT_DIRECTION_RTL = 4;
1912
1913 /**
1914 * Text direction is coming from the system Locale.
1915 */
1916 public static final int TEXT_DIRECTION_LOCALE = 5;
1917
1918 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001919 * Default text direction is inherited
1920 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07001921 private static final int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001922
1923 /**
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08001924 * Default resolved text direction
1925 * @hide
1926 */
1927 static final int TEXT_DIRECTION_RESOLVED_DEFAULT = TEXT_DIRECTION_FIRST_STRONG;
1928
1929 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001930 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1931 * @hide
1932 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001933 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001934
1935 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001936 * Mask for use with private flags indicating bits used for text direction.
1937 * @hide
1938 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001939 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1940 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001941
1942 /**
1943 * Array of text direction flags for mapping attribute "textDirection" to correct
1944 * flag value.
1945 * @hide
1946 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001947 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1948 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1949 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1950 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1951 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1952 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1953 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001954 };
1955
1956 /**
1957 * Indicates whether the view text direction has been resolved.
1958 * @hide
1959 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001960 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1961 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001962
1963 /**
1964 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1965 * @hide
1966 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001967 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001968
1969 /**
1970 * Mask for use with private flags indicating bits used for resolved text direction.
1971 * @hide
1972 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001973 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1974 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001975
1976 /**
1977 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1978 * @hide
1979 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001980 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08001981 TEXT_DIRECTION_RESOLVED_DEFAULT << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001982
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001983 /*
1984 * Default text alignment. The text alignment of this View is inherited from its parent.
1985 * Use with {@link #setTextAlignment(int)}
1986 */
1987 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1988
1989 /**
1990 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1991 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1992 *
1993 * Use with {@link #setTextAlignment(int)}
1994 */
1995 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1996
1997 /**
1998 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1999 *
2000 * Use with {@link #setTextAlignment(int)}
2001 */
2002 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
2003
2004 /**
2005 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
2006 *
2007 * Use with {@link #setTextAlignment(int)}
2008 */
2009 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2010
2011 /**
2012 * Center the paragraph, e.g. ALIGN_CENTER.
2013 *
2014 * Use with {@link #setTextAlignment(int)}
2015 */
2016 public static final int TEXT_ALIGNMENT_CENTER = 4;
2017
2018 /**
2019 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2020 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2021 *
2022 * Use with {@link #setTextAlignment(int)}
2023 */
2024 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2025
2026 /**
2027 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2028 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2029 *
2030 * Use with {@link #setTextAlignment(int)}
2031 */
2032 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2033
2034 /**
2035 * Default text alignment is inherited
2036 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07002037 private static final int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002038
2039 /**
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08002040 * Default resolved text alignment
2041 * @hide
2042 */
2043 static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2044
2045 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002046 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2047 * @hide
2048 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002049 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002050
2051 /**
2052 * Mask for use with private flags indicating bits used for text alignment.
2053 * @hide
2054 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002055 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002056
2057 /**
2058 * Array of text direction flags for mapping attribute "textAlignment" to correct
2059 * flag value.
2060 * @hide
2061 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002062 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2063 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2064 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2065 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2066 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2067 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2068 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2069 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002070 };
2071
2072 /**
2073 * Indicates whether the view text alignment has been resolved.
2074 * @hide
2075 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002076 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002077
2078 /**
2079 * Bit shift to get the resolved text alignment.
2080 * @hide
2081 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002082 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002083
2084 /**
2085 * Mask for use with private flags indicating bits used for text alignment.
2086 * @hide
2087 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002088 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2089 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002090
2091 /**
2092 * Indicates whether if the view text alignment has been resolved to gravity
2093 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002094 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08002095 TEXT_ALIGNMENT_RESOLVED_DEFAULT << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002096
Svetoslav Ganov42138042012-03-20 11:51:39 -07002097 // Accessiblity constants for mPrivateFlags2
2098
2099 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002100 * Shift for the bits in {@link #mPrivateFlags2} related to the
2101 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002102 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002103 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002104
2105 /**
2106 * Automatically determine whether a view is important for accessibility.
2107 */
2108 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2109
2110 /**
2111 * The view is important for accessibility.
2112 */
2113 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2114
2115 /**
2116 * The view is not important for accessibility.
2117 */
2118 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2119
2120 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002121 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002122 */
2123 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2124
2125 /**
2126 * Mask for obtainig the bits which specify how to determine
2127 * whether a view is important for accessibility.
2128 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002129 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002130 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002131 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002132
2133 /**
2134 * Flag indicating whether a view has accessibility focus.
2135 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002136 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002137
2138 /**
2139 * Flag indicating whether a view state for accessibility has changed.
2140 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002141 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2142 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002143
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002144 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002145 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2146 * is used to check whether later changes to the view's transform should invalidate the
2147 * view to force the quickReject test to run again.
2148 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002149 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002150
Adam Powell0090f202012-08-07 17:15:30 -07002151 /**
2152 * Flag indicating that start/end padding has been resolved into left/right padding
2153 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2154 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2155 * during measurement. In some special cases this is required such as when an adapter-based
2156 * view measures prospective children without attaching them to a window.
2157 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002158 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002159
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002160 /**
2161 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2162 */
2163 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2164
2165 /**
2166 * Group of bits indicating that RTL properties resolution is done.
2167 */
2168 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002169 PFLAG2_TEXT_DIRECTION_RESOLVED |
2170 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2171 PFLAG2_PADDING_RESOLVED |
2172 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002173
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002174 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002175
Christopher Tate3d4bf172011-03-28 16:16:46 -07002176 /* End of masks for mPrivateFlags2 */
2177
Chet Haase21433372012-06-05 07:54:09 -07002178 /* Masks for mPrivateFlags3 */
2179
2180 /**
2181 * Flag indicating that view has a transform animation set on it. This is used to track whether
2182 * an animation is cleared between successive frames, in order to tell the associated
2183 * DisplayList to clear its animation matrix.
2184 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002185 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002186
2187 /**
2188 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2189 * animation is cleared between successive frames, in order to tell the associated
2190 * DisplayList to restore its alpha value.
2191 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002192 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002193
2194
2195 /* End of masks for mPrivateFlags3 */
2196
Dianne Hackborn4702a852012-08-17 15:18:29 -07002197 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002198
Chet Haasedaf98e92011-01-10 14:10:36 -08002199 /**
Adam Powell637d3372010-08-25 14:37:03 -07002200 * Always allow a user to over-scroll this view, provided it is a
2201 * view that can scroll.
2202 *
2203 * @see #getOverScrollMode()
2204 * @see #setOverScrollMode(int)
2205 */
2206 public static final int OVER_SCROLL_ALWAYS = 0;
2207
2208 /**
2209 * Allow a user to over-scroll this view only if the content is large
2210 * enough to meaningfully scroll, provided it is a view that can scroll.
2211 *
2212 * @see #getOverScrollMode()
2213 * @see #setOverScrollMode(int)
2214 */
2215 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2216
2217 /**
2218 * Never allow a user to over-scroll this view.
2219 *
2220 * @see #getOverScrollMode()
2221 * @see #setOverScrollMode(int)
2222 */
2223 public static final int OVER_SCROLL_NEVER = 2;
2224
2225 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002226 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2227 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002228 *
Joe Malin32736f02011-01-19 16:14:20 -08002229 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002230 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002231 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002232
2233 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002234 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2235 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002236 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002237 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002238 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002239 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002240 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002241 *
Joe Malin32736f02011-01-19 16:14:20 -08002242 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002243 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002244 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2245
2246 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002247 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2248 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002249 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002250 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002251 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2252 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2253 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002254 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002255 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2256 * window flags) for displaying content using every last pixel on the display.
2257 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002258 * <p>There is a limitation: because navigation controls are so important, the least user
2259 * interaction will cause them to reappear immediately. When this happens, both
2260 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2261 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002262 *
2263 * @see #setSystemUiVisibility(int)
2264 */
2265 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2266
2267 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002268 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2269 * into the normal fullscreen mode so that its content can take over the screen
2270 * while still allowing the user to interact with the application.
2271 *
2272 * <p>This has the same visual effect as
2273 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2274 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2275 * meaning that non-critical screen decorations (such as the status bar) will be
2276 * hidden while the user is in the View's window, focusing the experience on
2277 * that content. Unlike the window flag, if you are using ActionBar in
2278 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2279 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2280 * hide the action bar.
2281 *
2282 * <p>This approach to going fullscreen is best used over the window flag when
2283 * it is a transient state -- that is, the application does this at certain
2284 * points in its user interaction where it wants to allow the user to focus
2285 * on content, but not as a continuous state. For situations where the application
2286 * would like to simply stay full screen the entire time (such as a game that
2287 * wants to take over the screen), the
2288 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2289 * is usually a better approach. The state set here will be removed by the system
2290 * in various situations (such as the user moving to another application) like
2291 * the other system UI states.
2292 *
2293 * <p>When using this flag, the application should provide some easy facility
2294 * for the user to go out of it. A common example would be in an e-book
2295 * reader, where tapping on the screen brings back whatever screen and UI
2296 * decorations that had been hidden while the user was immersed in reading
2297 * the book.
2298 *
2299 * @see #setSystemUiVisibility(int)
2300 */
2301 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2302
2303 /**
2304 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2305 * flags, we would like a stable view of the content insets given to
2306 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2307 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002308 * as a continuous state. In the stock Android UI this is the space for
2309 * the system bar, nav bar, and status bar, but not more transient elements
2310 * such as an input method.
2311 *
2312 * The stable layout your UI sees is based on the system UI modes you can
2313 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2314 * then you will get a stable layout for changes of the
2315 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2316 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2317 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2318 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2319 * with a stable layout. (Note that you should avoid using
2320 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2321 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002322 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002323 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2324 * then a hidden status bar will be considered a "stable" state for purposes
2325 * here. This allows your UI to continually hide the status bar, while still
2326 * using the system UI flags to hide the action bar while still retaining
2327 * a stable layout. Note that changing the window fullscreen flag will never
2328 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002329 *
2330 * <p>If you are using ActionBar in
2331 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2332 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2333 * insets it adds to those given to the application.
2334 */
2335 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2336
2337 /**
2338 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2339 * to be layed out as if it has requested
2340 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2341 * allows it to avoid artifacts when switching in and out of that mode, at
2342 * the expense that some of its user interface may be covered by screen
2343 * decorations when they are shown. You can perform layout of your inner
2344 * UI elements to account for the navagation system UI through the
2345 * {@link #fitSystemWindows(Rect)} method.
2346 */
2347 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2348
2349 /**
2350 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2351 * to be layed out as if it has requested
2352 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2353 * allows it to avoid artifacts when switching in and out of that mode, at
2354 * the expense that some of its user interface may be covered by screen
2355 * decorations when they are shown. You can perform layout of your inner
2356 * UI elements to account for non-fullscreen system UI through the
2357 * {@link #fitSystemWindows(Rect)} method.
2358 */
2359 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2360
2361 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002362 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2363 */
2364 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2365
2366 /**
2367 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2368 */
2369 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002370
2371 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002372 * @hide
2373 *
2374 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2375 * out of the public fields to keep the undefined bits out of the developer's way.
2376 *
2377 * Flag to make the status bar not expandable. Unless you also
2378 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2379 */
2380 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2381
2382 /**
2383 * @hide
2384 *
2385 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2386 * out of the public fields to keep the undefined bits out of the developer's way.
2387 *
2388 * Flag to hide notification icons and scrolling ticker text.
2389 */
2390 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2391
2392 /**
2393 * @hide
2394 *
2395 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2396 * out of the public fields to keep the undefined bits out of the developer's way.
2397 *
2398 * Flag to disable incoming notification alerts. This will not block
2399 * icons, but it will block sound, vibrating and other visual or aural notifications.
2400 */
2401 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2402
2403 /**
2404 * @hide
2405 *
2406 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2407 * out of the public fields to keep the undefined bits out of the developer's way.
2408 *
2409 * Flag to hide only the scrolling ticker. Note that
2410 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2411 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2412 */
2413 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2414
2415 /**
2416 * @hide
2417 *
2418 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2419 * out of the public fields to keep the undefined bits out of the developer's way.
2420 *
2421 * Flag to hide the center system info area.
2422 */
2423 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2424
2425 /**
2426 * @hide
2427 *
2428 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2429 * out of the public fields to keep the undefined bits out of the developer's way.
2430 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002431 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002432 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2433 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002434 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002435
2436 /**
2437 * @hide
2438 *
2439 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2440 * out of the public fields to keep the undefined bits out of the developer's way.
2441 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002442 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002443 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2444 */
2445 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2446
2447 /**
2448 * @hide
2449 *
2450 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2451 * out of the public fields to keep the undefined bits out of the developer's way.
2452 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002453 * Flag to hide only the clock. You might use this if your activity has
2454 * its own clock making the status bar's clock redundant.
2455 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002456 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2457
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002458 /**
2459 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002460 *
2461 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2462 * out of the public fields to keep the undefined bits out of the developer's way.
2463 *
2464 * Flag to hide only the recent apps button. Don't use this
2465 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2466 */
2467 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2468
2469 /**
2470 * @hide
Daniel Sandlerd5483c32012-10-19 16:44:15 -04002471 *
2472 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2473 * out of the public fields to keep the undefined bits out of the developer's way.
2474 *
2475 * Flag to disable the global search gesture. Don't use this
2476 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2477 */
2478 public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
2479
2480 /**
2481 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002482 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002483 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002484
2485 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002486 * These are the system UI flags that can be cleared by events outside
2487 * of an application. Currently this is just the ability to tap on the
2488 * screen while hiding the navigation bar to have it return.
2489 * @hide
2490 */
2491 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002492 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2493 | SYSTEM_UI_FLAG_FULLSCREEN;
2494
2495 /**
2496 * Flags that can impact the layout in relation to system UI.
2497 */
2498 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2499 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2500 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002501
2502 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002503 * Find views that render the specified text.
2504 *
2505 * @see #findViewsWithText(ArrayList, CharSequence, int)
2506 */
2507 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2508
2509 /**
2510 * Find find views that contain the specified content description.
2511 *
2512 * @see #findViewsWithText(ArrayList, CharSequence, int)
2513 */
2514 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2515
2516 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002517 * Find views that contain {@link AccessibilityNodeProvider}. Such
2518 * a View is a root of virtual view hierarchy and may contain the searched
2519 * text. If this flag is set Views with providers are automatically
2520 * added and it is a responsibility of the client to call the APIs of
2521 * the provider to determine whether the virtual tree rooted at this View
2522 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2523 * represeting the virtual views with this text.
2524 *
2525 * @see #findViewsWithText(ArrayList, CharSequence, int)
2526 *
2527 * @hide
2528 */
2529 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2530
2531 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002532 * The undefined cursor position.
Svetoslav7c512842013-01-30 23:02:08 -08002533 *
2534 * @hide
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002535 */
Svetoslav7c512842013-01-30 23:02:08 -08002536 public static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002537
2538 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002539 * Indicates that the screen has changed state and is now off.
2540 *
2541 * @see #onScreenStateChanged(int)
2542 */
2543 public static final int SCREEN_STATE_OFF = 0x0;
2544
2545 /**
2546 * Indicates that the screen has changed state and is now on.
2547 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002548 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002549 */
2550 public static final int SCREEN_STATE_ON = 0x1;
2551
2552 /**
Adam Powell637d3372010-08-25 14:37:03 -07002553 * Controls the over-scroll mode for this view.
2554 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2555 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2556 * and {@link #OVER_SCROLL_NEVER}.
2557 */
2558 private int mOverScrollMode;
2559
2560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 * The parent this view is attached to.
2562 * {@hide}
2563 *
2564 * @see #getParent()
2565 */
2566 protected ViewParent mParent;
2567
2568 /**
2569 * {@hide}
2570 */
2571 AttachInfo mAttachInfo;
2572
2573 /**
2574 * {@hide}
2575 */
Romain Guy809a7f62009-05-14 15:44:42 -07002576 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002577 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002578 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002579 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002580 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002581 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002582 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002583 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2584 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2585 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2586 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002587 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002589 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002590 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591
2592 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002593 * This view's request for the visibility of the status bar.
2594 * @hide
2595 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002596 @ViewDebug.ExportedProperty(flagMapping = {
2597 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2598 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2599 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2600 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2601 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2602 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2603 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2604 equals = SYSTEM_UI_FLAG_VISIBLE,
2605 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2606 })
Joe Onorato664644d2011-01-23 17:53:23 -08002607 int mSystemUiVisibility;
2608
2609 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002610 * Reference count for transient state.
2611 * @see #setHasTransientState(boolean)
2612 */
2613 int mTransientStateCount = 0;
2614
2615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 * Count of how many windows this view has been attached to.
2617 */
2618 int mWindowAttachCount;
2619
2620 /**
2621 * The layout parameters associated with this view and used by the parent
2622 * {@link android.view.ViewGroup} to determine how this view should be
2623 * laid out.
2624 * {@hide}
2625 */
2626 protected ViewGroup.LayoutParams mLayoutParams;
2627
2628 /**
2629 * The view flags hold various views states.
2630 * {@hide}
2631 */
2632 @ViewDebug.ExportedProperty
2633 int mViewFlags;
2634
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002635 static class TransformationInfo {
2636 /**
2637 * The transform matrix for the View. This transform is calculated internally
2638 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2639 * is used by default. Do *not* use this variable directly; instead call
2640 * getMatrix(), which will automatically recalculate the matrix if necessary
2641 * to get the correct matrix based on the latest rotation and scale properties.
2642 */
2643 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002644
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002645 /**
2646 * The transform matrix for the View. This transform is calculated internally
2647 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2648 * is used by default. Do *not* use this variable directly; instead call
2649 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2650 * to get the correct matrix based on the latest rotation and scale properties.
2651 */
2652 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002653
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002654 /**
2655 * An internal variable that tracks whether we need to recalculate the
2656 * transform matrix, based on whether the rotation or scaleX/Y properties
2657 * have changed since the matrix was last calculated.
2658 */
2659 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002660
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002661 /**
2662 * An internal variable that tracks whether we need to recalculate the
2663 * transform matrix, based on whether the rotation or scaleX/Y properties
2664 * have changed since the matrix was last calculated.
2665 */
2666 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002667
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002668 /**
2669 * A variable that tracks whether we need to recalculate the
2670 * transform matrix, based on whether the rotation or scaleX/Y properties
2671 * have changed since the matrix was last calculated. This variable
2672 * is only valid after a call to updateMatrix() or to a function that
2673 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2674 */
2675 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002676
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002677 /**
2678 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2679 */
2680 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002681
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002682 /**
2683 * This matrix is used when computing the matrix for 3D rotations.
2684 */
2685 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002686
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002687 /**
2688 * These prev values are used to recalculate a centered pivot point when necessary. The
2689 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2690 * set), so thes values are only used then as well.
2691 */
2692 private int mPrevWidth = -1;
2693 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002694
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002695 /**
2696 * The degrees rotation around the vertical axis through the pivot point.
2697 */
2698 @ViewDebug.ExportedProperty
2699 float mRotationY = 0f;
2700
2701 /**
2702 * The degrees rotation around the horizontal axis through the pivot point.
2703 */
2704 @ViewDebug.ExportedProperty
2705 float mRotationX = 0f;
2706
2707 /**
2708 * The degrees rotation around the pivot point.
2709 */
2710 @ViewDebug.ExportedProperty
2711 float mRotation = 0f;
2712
2713 /**
2714 * The amount of translation of the object away from its left property (post-layout).
2715 */
2716 @ViewDebug.ExportedProperty
2717 float mTranslationX = 0f;
2718
2719 /**
2720 * The amount of translation of the object away from its top property (post-layout).
2721 */
2722 @ViewDebug.ExportedProperty
2723 float mTranslationY = 0f;
2724
2725 /**
2726 * The amount of scale in the x direction around the pivot point. A
2727 * value of 1 means no scaling is applied.
2728 */
2729 @ViewDebug.ExportedProperty
2730 float mScaleX = 1f;
2731
2732 /**
2733 * The amount of scale in the y direction around the pivot point. A
2734 * value of 1 means no scaling is applied.
2735 */
2736 @ViewDebug.ExportedProperty
2737 float mScaleY = 1f;
2738
2739 /**
Chet Haasea33de552012-02-03 16:28:24 -08002740 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002741 */
2742 @ViewDebug.ExportedProperty
2743 float mPivotX = 0f;
2744
2745 /**
Chet Haasea33de552012-02-03 16:28:24 -08002746 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002747 */
2748 @ViewDebug.ExportedProperty
2749 float mPivotY = 0f;
2750
2751 /**
2752 * The opacity of the View. This is a value from 0 to 1, where 0 means
2753 * completely transparent and 1 means completely opaque.
2754 */
2755 @ViewDebug.ExportedProperty
2756 float mAlpha = 1f;
2757 }
2758
2759 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002760
Chet Haase430742f2013-04-12 11:18:36 -07002761 /**
2762 * Current clip bounds. to which all drawing of this view are constrained.
2763 */
2764 private Rect mClipBounds = null;
2765
Joe Malin32736f02011-01-19 16:14:20 -08002766 private boolean mLastIsOpaque;
2767
Chet Haasefd2b0022010-08-06 13:08:56 -07002768 /**
2769 * Convenience value to check for float values that are close enough to zero to be considered
2770 * zero.
2771 */
Romain Guy2542d192010-08-18 11:47:12 -07002772 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002773
2774 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 * The distance in pixels from the left edge of this view's parent
2776 * to the left edge of this view.
2777 * {@hide}
2778 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002779 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 protected int mLeft;
2781 /**
2782 * The distance in pixels from the left edge of this view's parent
2783 * to the right edge of this view.
2784 * {@hide}
2785 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002786 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 protected int mRight;
2788 /**
2789 * The distance in pixels from the top edge of this view's parent
2790 * to the top edge of this view.
2791 * {@hide}
2792 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002793 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 protected int mTop;
2795 /**
2796 * The distance in pixels from the top edge of this view's parent
2797 * to the bottom edge of this view.
2798 * {@hide}
2799 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002800 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 protected int mBottom;
2802
2803 /**
2804 * The offset, in pixels, by which the content of this view is scrolled
2805 * horizontally.
2806 * {@hide}
2807 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002808 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 protected int mScrollX;
2810 /**
2811 * The offset, in pixels, by which the content of this view is scrolled
2812 * vertically.
2813 * {@hide}
2814 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002815 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 protected int mScrollY;
2817
2818 /**
2819 * The left padding in pixels, that is the distance in pixels between the
2820 * left edge of this view and the left edge of its content.
2821 * {@hide}
2822 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002823 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002824 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 /**
2826 * The right padding in pixels, that is the distance in pixels between the
2827 * right edge of this view and the right edge of its content.
2828 * {@hide}
2829 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002830 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002831 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 /**
2833 * The top padding in pixels, that is the distance in pixels between the
2834 * top edge of this view and the top edge of its content.
2835 * {@hide}
2836 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002837 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 protected int mPaddingTop;
2839 /**
2840 * The bottom padding in pixels, that is the distance in pixels between the
2841 * bottom edge of this view and the bottom edge of its content.
2842 * {@hide}
2843 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002844 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 protected int mPaddingBottom;
2846
2847 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002848 * The layout insets in pixels, that is the distance in pixels between the
2849 * visible edges of this view its bounds.
2850 */
2851 private Insets mLayoutInsets;
2852
2853 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002854 * Briefly describes the view and is primarily used for accessibility support.
2855 */
2856 private CharSequence mContentDescription;
2857
2858 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002859 * Specifies the id of a view for which this view serves as a label for
2860 * accessibility purposes.
2861 */
2862 private int mLabelForId = View.NO_ID;
2863
2864 /**
2865 * Predicate for matching labeled view id with its label for
2866 * accessibility purposes.
2867 */
2868 private MatchLabelForPredicate mMatchLabelForPredicate;
2869
2870 /**
2871 * Predicate for matching a view by its id.
2872 */
2873 private MatchIdPredicate mMatchIdPredicate;
2874
2875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002877 *
2878 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002880 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002881 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882
2883 /**
2884 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002885 *
2886 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002888 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002889 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002891 /**
Adam Powell20232d02010-12-08 21:08:53 -08002892 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002893 *
2894 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002895 */
2896 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002897 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002898
2899 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002900 * Cache the paddingStart set by the user to append to the scrollbar's size.
2901 *
2902 */
2903 @ViewDebug.ExportedProperty(category = "padding")
2904 int mUserPaddingStart;
2905
2906 /**
2907 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2908 *
2909 */
2910 @ViewDebug.ExportedProperty(category = "padding")
2911 int mUserPaddingEnd;
2912
2913 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002914 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002915 *
2916 * @hide
2917 */
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -07002918 int mUserPaddingLeftInitial;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002919
2920 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002921 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002922 *
2923 * @hide
2924 */
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -07002925 int mUserPaddingRightInitial;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002926
2927 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002928 * Default undefined padding
2929 */
2930 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2931
2932 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002933 * @hide
2934 */
2935 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2936 /**
2937 * @hide
2938 */
2939 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940
Romain Guydfab3632012-10-03 14:53:25 -07002941 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002942 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943
2944 private int mBackgroundResource;
2945 private boolean mBackgroundSizeChanged;
2946
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002947 static class ListenerInfo {
2948 /**
2949 * Listener used to dispatch focus change events.
2950 * This field should be made private, so it is hidden from the SDK.
2951 * {@hide}
2952 */
2953 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002955 /**
2956 * Listeners for layout change events.
2957 */
2958 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002959
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002960 /**
2961 * Listeners for attach events.
2962 */
2963 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002964
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002965 /**
2966 * Listener used to dispatch click events.
2967 * This field should be made private, so it is hidden from the SDK.
2968 * {@hide}
2969 */
2970 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002972 /**
2973 * Listener used to dispatch long click events.
2974 * This field should be made private, so it is hidden from the SDK.
2975 * {@hide}
2976 */
2977 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002979 /**
2980 * Listener used to build the context menu.
2981 * This field should be made private, so it is hidden from the SDK.
2982 * {@hide}
2983 */
2984 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002986 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002988 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002990 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002991
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002992 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002993
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002994 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002995
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002996 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2997 }
2998
2999 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08003000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 /**
3002 * The application environment this view lives in.
3003 * This field should be made private, so it is hidden from the SDK.
3004 * {@hide}
3005 */
3006 protected Context mContext;
3007
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003008 private final Resources mResources;
3009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 private ScrollabilityCache mScrollCache;
3011
3012 private int[] mDrawableState = null;
3013
Romain Guy0211a0a2011-02-14 16:34:59 -08003014 /**
3015 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07003016 *
Romain Guy0211a0a2011-02-14 16:34:59 -08003017 * @hide
3018 */
3019 public boolean mCachingFailed;
3020
Romain Guy02890fd2010-08-06 17:58:44 -07003021 private Bitmap mDrawingCache;
3022 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08003023 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003024 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025
3026 /**
3027 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3028 * the user may specify which view to go to next.
3029 */
3030 private int mNextFocusLeftId = View.NO_ID;
3031
3032 /**
3033 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3034 * the user may specify which view to go to next.
3035 */
3036 private int mNextFocusRightId = View.NO_ID;
3037
3038 /**
3039 * When this view has focus and the next focus is {@link #FOCUS_UP},
3040 * the user may specify which view to go to next.
3041 */
3042 private int mNextFocusUpId = View.NO_ID;
3043
3044 /**
3045 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3046 * the user may specify which view to go to next.
3047 */
3048 private int mNextFocusDownId = View.NO_ID;
3049
Jeff Brown4e6319b2010-12-13 10:36:51 -08003050 /**
3051 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3052 * the user may specify which view to go to next.
3053 */
3054 int mNextFocusForwardId = View.NO_ID;
3055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003057 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003058 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003059 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 private UnsetPressedState mUnsetPressedState;
3062
3063 /**
3064 * Whether the long press's action has been invoked. The tap's action is invoked on the
3065 * up event while a long press is invoked as soon as the long press duration is reached, so
3066 * a long press could be performed before the tap is checked, in which case the tap's action
3067 * should not be invoked.
3068 */
3069 private boolean mHasPerformedLongPress;
3070
3071 /**
3072 * The minimum height of the view. We'll try our best to have the height
3073 * of this view to at least this amount.
3074 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003075 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 private int mMinHeight;
3077
3078 /**
3079 * The minimum width of the view. We'll try our best to have the width
3080 * of this view to at least this amount.
3081 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003082 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 private int mMinWidth;
3084
3085 /**
3086 * The delegate to handle touch events that are physically in this view
3087 * but should be handled by another view.
3088 */
3089 private TouchDelegate mTouchDelegate = null;
3090
3091 /**
3092 * Solid color to use as a background when creating the drawing cache. Enables
3093 * the cache to use 16 bit bitmaps instead of 32 bit.
3094 */
3095 private int mDrawingCacheBackgroundColor = 0;
3096
3097 /**
3098 * Special tree observer used when mAttachInfo is null.
3099 */
3100 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003101
Adam Powelle14579b2009-12-16 18:39:52 -08003102 /**
3103 * Cache the touch slop from the context that created the view.
3104 */
3105 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003108 * Object that handles automatic animation of view properties.
3109 */
3110 private ViewPropertyAnimator mAnimator = null;
3111
3112 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003113 * Flag indicating that a drag can cross window boundaries. When
3114 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3115 * with this flag set, all visible applications will be able to participate
3116 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003117 *
3118 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003119 */
3120 public static final int DRAG_FLAG_GLOBAL = 1;
3121
3122 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003123 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3124 */
3125 private float mVerticalScrollFactor;
3126
3127 /**
Adam Powell20232d02010-12-08 21:08:53 -08003128 * Position of the vertical scroll bar.
3129 */
3130 private int mVerticalScrollbarPosition;
3131
3132 /**
3133 * Position the scroll bar at the default position as determined by the system.
3134 */
3135 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3136
3137 /**
3138 * Position the scroll bar along the left edge.
3139 */
3140 public static final int SCROLLBAR_POSITION_LEFT = 1;
3141
3142 /**
3143 * Position the scroll bar along the right edge.
3144 */
3145 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3146
3147 /**
Romain Guy171c5922011-01-06 10:04:23 -08003148 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003149 *
3150 * @see #getLayerType()
3151 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003152 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003153 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003154 */
3155 public static final int LAYER_TYPE_NONE = 0;
3156
3157 /**
3158 * <p>Indicates that the view has a software layer. A software layer is backed
3159 * by a bitmap and causes the view to be rendered using Android's software
3160 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003161 *
Romain Guy171c5922011-01-06 10:04:23 -08003162 * <p>Software layers have various usages:</p>
3163 * <p>When the application is not using hardware acceleration, a software layer
3164 * is useful to apply a specific color filter and/or blending mode and/or
3165 * translucency to a view and all its children.</p>
3166 * <p>When the application is using hardware acceleration, a software layer
3167 * is useful to render drawing primitives not supported by the hardware
3168 * accelerated pipeline. It can also be used to cache a complex view tree
3169 * into a texture and reduce the complexity of drawing operations. For instance,
3170 * when animating a complex view tree with a translation, a software layer can
3171 * be used to render the view tree only once.</p>
3172 * <p>Software layers should be avoided when the affected view tree updates
3173 * often. Every update will require to re-render the software layer, which can
3174 * potentially be slow (particularly when hardware acceleration is turned on
3175 * since the layer will have to be uploaded into a hardware texture after every
3176 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003177 *
3178 * @see #getLayerType()
3179 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003180 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003181 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003182 */
3183 public static final int LAYER_TYPE_SOFTWARE = 1;
3184
3185 /**
3186 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3187 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3188 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3189 * rendering pipeline, but only if hardware acceleration is turned on for the
3190 * view hierarchy. When hardware acceleration is turned off, hardware layers
3191 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003192 *
Romain Guy171c5922011-01-06 10:04:23 -08003193 * <p>A hardware layer is useful to apply a specific color filter and/or
3194 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003195 * <p>A hardware layer can be used to cache a complex view tree into a
3196 * texture and reduce the complexity of drawing operations. For instance,
3197 * when animating a complex view tree with a translation, a hardware layer can
3198 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003199 * <p>A hardware layer can also be used to increase the rendering quality when
3200 * rotation transformations are applied on a view. It can also be used to
3201 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003202 *
3203 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003204 * @see #setLayerType(int, android.graphics.Paint)
3205 * @see #LAYER_TYPE_NONE
3206 * @see #LAYER_TYPE_SOFTWARE
3207 */
3208 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003209
Romain Guy3aaff3a2011-01-12 14:18:47 -08003210 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3211 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3212 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3213 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3214 })
Romain Guy171c5922011-01-06 10:04:23 -08003215 int mLayerType = LAYER_TYPE_NONE;
3216 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003217 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003218
3219 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003220 * Set to true when the view is sending hover accessibility events because it
3221 * is the innermost hovered view.
3222 */
3223 private boolean mSendingHoverAccessibilityEvents;
3224
Dianne Hackborn4702a852012-08-17 15:18:29 -07003225 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003226 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003227 */
3228 AccessibilityDelegate mAccessibilityDelegate;
3229
3230 /**
Chet Haase91cedf12013-03-11 07:56:30 -07003231 * The view's overlay layer. Developers get a reference to the overlay via getOverlay()
3232 * and add/remove objects to/from the overlay directly through the Overlay methods.
3233 */
3234 ViewOverlay mOverlay;
3235
3236 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07003237 * Consistency verifier for debugging purposes.
3238 * @hide
3239 */
3240 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3241 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3242 new InputEventConsistencyVerifier(this, 0) : null;
3243
Adam Powella9108a22012-07-18 11:18:09 -07003244 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3245
Jeff Brown87b7f802011-06-21 18:35:45 -07003246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 * Simple constructor to use when creating a view from code.
3248 *
3249 * @param context The Context the view is running in, through which it can
3250 * access the current theme, resources, etc.
3251 */
3252 public View(Context context) {
3253 mContext = context;
3254 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003255 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07003256 // Set some flags defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003257 mPrivateFlags2 =
3258 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003259 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003260 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003261 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003262 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003263 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003264 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003265 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003266 mUserPaddingStart = UNDEFINED_PADDING;
3267 mUserPaddingEnd = UNDEFINED_PADDING;
Adam Powell7da4b732012-12-07 15:28:33 -08003268
3269 if (!sUseBrokenMakeMeasureSpec && context.getApplicationInfo().targetSdkVersion <=
3270 Build.VERSION_CODES.JELLY_BEAN_MR1 ) {
3271 // Older apps may need this compatibility hack for measurement.
3272 sUseBrokenMakeMeasureSpec = true;
3273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 }
3275
3276 /**
3277 * Constructor that is called when inflating a view from XML. This is called
3278 * when a view is being constructed from an XML file, supplying attributes
3279 * that were specified in the XML file. This version uses a default style of
3280 * 0, so the only attribute values applied are those in the Context's Theme
3281 * and the given AttributeSet.
3282 *
3283 * <p>
3284 * The method onFinishInflate() will be called after all children have been
3285 * added.
3286 *
3287 * @param context The Context the view is running in, through which it can
3288 * access the current theme, resources, etc.
3289 * @param attrs The attributes of the XML tag that is inflating the view.
3290 * @see #View(Context, AttributeSet, int)
3291 */
3292 public View(Context context, AttributeSet attrs) {
3293 this(context, attrs, 0);
3294 }
3295
3296 /**
3297 * Perform inflation from XML and apply a class-specific base style. This
3298 * constructor of View allows subclasses to use their own base style when
3299 * they are inflating. For example, a Button class's constructor would call
3300 * this version of the super class constructor and supply
3301 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3302 * the theme's button style to modify all of the base view attributes (in
3303 * particular its background) as well as the Button class's attributes.
3304 *
3305 * @param context The Context the view is running in, through which it can
3306 * access the current theme, resources, etc.
3307 * @param attrs The attributes of the XML tag that is inflating the view.
3308 * @param defStyle The default style to apply to this view. If 0, no style
3309 * will be applied (beyond what is included in the theme). This may
3310 * either be an attribute resource, whose value will be retrieved
3311 * from the current theme, or an explicit style resource.
3312 * @see #View(Context, AttributeSet)
3313 */
3314 public View(Context context, AttributeSet attrs, int defStyle) {
3315 this(context);
3316
3317 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3318 defStyle, 0);
3319
3320 Drawable background = null;
3321
3322 int leftPadding = -1;
3323 int topPadding = -1;
3324 int rightPadding = -1;
3325 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003326 int startPadding = UNDEFINED_PADDING;
3327 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328
3329 int padding = -1;
3330
3331 int viewFlagValues = 0;
3332 int viewFlagMasks = 0;
3333
3334 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 int x = 0;
3337 int y = 0;
3338
Chet Haase73066682010-11-29 15:55:32 -08003339 float tx = 0;
3340 float ty = 0;
3341 float rotation = 0;
3342 float rotationX = 0;
3343 float rotationY = 0;
3344 float sx = 1f;
3345 float sy = 1f;
3346 boolean transformSet = false;
3347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003349 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003350 boolean initializeScrollbars = false;
3351
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003352 boolean leftPaddingDefined = false;
3353 boolean rightPaddingDefined = false;
3354 boolean startPaddingDefined = false;
3355 boolean endPaddingDefined = false;
3356
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003357 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 final int N = a.getIndexCount();
3360 for (int i = 0; i < N; i++) {
3361 int attr = a.getIndex(i);
3362 switch (attr) {
3363 case com.android.internal.R.styleable.View_background:
3364 background = a.getDrawable(attr);
3365 break;
3366 case com.android.internal.R.styleable.View_padding:
3367 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003368 mUserPaddingLeftInitial = padding;
3369 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003370 leftPaddingDefined = true;
3371 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 break;
3373 case com.android.internal.R.styleable.View_paddingLeft:
3374 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003375 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003376 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 break;
3378 case com.android.internal.R.styleable.View_paddingTop:
3379 topPadding = a.getDimensionPixelSize(attr, -1);
3380 break;
3381 case com.android.internal.R.styleable.View_paddingRight:
3382 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003383 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003384 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 break;
3386 case com.android.internal.R.styleable.View_paddingBottom:
3387 bottomPadding = a.getDimensionPixelSize(attr, -1);
3388 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003389 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003390 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -07003391 startPaddingDefined = (startPadding != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003392 break;
3393 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003394 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -07003395 endPaddingDefined = (endPadding != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003396 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 case com.android.internal.R.styleable.View_scrollX:
3398 x = a.getDimensionPixelOffset(attr, 0);
3399 break;
3400 case com.android.internal.R.styleable.View_scrollY:
3401 y = a.getDimensionPixelOffset(attr, 0);
3402 break;
Chet Haase73066682010-11-29 15:55:32 -08003403 case com.android.internal.R.styleable.View_alpha:
3404 setAlpha(a.getFloat(attr, 1f));
3405 break;
3406 case com.android.internal.R.styleable.View_transformPivotX:
3407 setPivotX(a.getDimensionPixelOffset(attr, 0));
3408 break;
3409 case com.android.internal.R.styleable.View_transformPivotY:
3410 setPivotY(a.getDimensionPixelOffset(attr, 0));
3411 break;
3412 case com.android.internal.R.styleable.View_translationX:
3413 tx = a.getDimensionPixelOffset(attr, 0);
3414 transformSet = true;
3415 break;
3416 case com.android.internal.R.styleable.View_translationY:
3417 ty = a.getDimensionPixelOffset(attr, 0);
3418 transformSet = true;
3419 break;
3420 case com.android.internal.R.styleable.View_rotation:
3421 rotation = a.getFloat(attr, 0);
3422 transformSet = true;
3423 break;
3424 case com.android.internal.R.styleable.View_rotationX:
3425 rotationX = a.getFloat(attr, 0);
3426 transformSet = true;
3427 break;
3428 case com.android.internal.R.styleable.View_rotationY:
3429 rotationY = a.getFloat(attr, 0);
3430 transformSet = true;
3431 break;
3432 case com.android.internal.R.styleable.View_scaleX:
3433 sx = a.getFloat(attr, 1f);
3434 transformSet = true;
3435 break;
3436 case com.android.internal.R.styleable.View_scaleY:
3437 sy = a.getFloat(attr, 1f);
3438 transformSet = true;
3439 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 case com.android.internal.R.styleable.View_id:
3441 mID = a.getResourceId(attr, NO_ID);
3442 break;
3443 case com.android.internal.R.styleable.View_tag:
3444 mTag = a.getText(attr);
3445 break;
3446 case com.android.internal.R.styleable.View_fitsSystemWindows:
3447 if (a.getBoolean(attr, false)) {
3448 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3449 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3450 }
3451 break;
3452 case com.android.internal.R.styleable.View_focusable:
3453 if (a.getBoolean(attr, false)) {
3454 viewFlagValues |= FOCUSABLE;
3455 viewFlagMasks |= FOCUSABLE_MASK;
3456 }
3457 break;
3458 case com.android.internal.R.styleable.View_focusableInTouchMode:
3459 if (a.getBoolean(attr, false)) {
3460 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3461 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3462 }
3463 break;
3464 case com.android.internal.R.styleable.View_clickable:
3465 if (a.getBoolean(attr, false)) {
3466 viewFlagValues |= CLICKABLE;
3467 viewFlagMasks |= CLICKABLE;
3468 }
3469 break;
3470 case com.android.internal.R.styleable.View_longClickable:
3471 if (a.getBoolean(attr, false)) {
3472 viewFlagValues |= LONG_CLICKABLE;
3473 viewFlagMasks |= LONG_CLICKABLE;
3474 }
3475 break;
3476 case com.android.internal.R.styleable.View_saveEnabled:
3477 if (!a.getBoolean(attr, true)) {
3478 viewFlagValues |= SAVE_DISABLED;
3479 viewFlagMasks |= SAVE_DISABLED_MASK;
3480 }
3481 break;
3482 case com.android.internal.R.styleable.View_duplicateParentState:
3483 if (a.getBoolean(attr, false)) {
3484 viewFlagValues |= DUPLICATE_PARENT_STATE;
3485 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3486 }
3487 break;
3488 case com.android.internal.R.styleable.View_visibility:
3489 final int visibility = a.getInt(attr, 0);
3490 if (visibility != 0) {
3491 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3492 viewFlagMasks |= VISIBILITY_MASK;
3493 }
3494 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003495 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003496 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003497 mPrivateFlags2 &=
3498 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003499 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003500 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003501 final int value = (layoutDirection != -1) ?
3502 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003503 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003504 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 case com.android.internal.R.styleable.View_drawingCacheQuality:
3506 final int cacheQuality = a.getInt(attr, 0);
3507 if (cacheQuality != 0) {
3508 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3509 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3510 }
3511 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003512 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003513 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003514 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003515 case com.android.internal.R.styleable.View_labelFor:
3516 setLabelFor(a.getResourceId(attr, NO_ID));
3517 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3519 if (!a.getBoolean(attr, true)) {
3520 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3521 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3522 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003523 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3525 if (!a.getBoolean(attr, true)) {
3526 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3527 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3528 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003529 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 case R.styleable.View_scrollbars:
3531 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3532 if (scrollbars != SCROLLBARS_NONE) {
3533 viewFlagValues |= scrollbars;
3534 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003535 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
3537 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003538 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003540 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003541 // Ignore the attribute starting with ICS
3542 break;
3543 }
3544 // With builds < ICS, fall through and apply fading edges
3545 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3547 if (fadingEdge != FADING_EDGE_NONE) {
3548 viewFlagValues |= fadingEdge;
3549 viewFlagMasks |= FADING_EDGE_MASK;
3550 initializeFadingEdge(a);
3551 }
3552 break;
3553 case R.styleable.View_scrollbarStyle:
3554 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3555 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3556 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3557 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3558 }
3559 break;
3560 case R.styleable.View_isScrollContainer:
3561 setScrollContainer = true;
3562 if (a.getBoolean(attr, false)) {
3563 setScrollContainer(true);
3564 }
3565 break;
3566 case com.android.internal.R.styleable.View_keepScreenOn:
3567 if (a.getBoolean(attr, false)) {
3568 viewFlagValues |= KEEP_SCREEN_ON;
3569 viewFlagMasks |= KEEP_SCREEN_ON;
3570 }
3571 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003572 case R.styleable.View_filterTouchesWhenObscured:
3573 if (a.getBoolean(attr, false)) {
3574 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3575 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3576 }
3577 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 case R.styleable.View_nextFocusLeft:
3579 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3580 break;
3581 case R.styleable.View_nextFocusRight:
3582 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3583 break;
3584 case R.styleable.View_nextFocusUp:
3585 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3586 break;
3587 case R.styleable.View_nextFocusDown:
3588 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3589 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003590 case R.styleable.View_nextFocusForward:
3591 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3592 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 case R.styleable.View_minWidth:
3594 mMinWidth = a.getDimensionPixelSize(attr, 0);
3595 break;
3596 case R.styleable.View_minHeight:
3597 mMinHeight = a.getDimensionPixelSize(attr, 0);
3598 break;
Romain Guy9a817362009-05-01 10:57:14 -07003599 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003600 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003601 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003602 + "be used within a restricted context");
3603 }
3604
Romain Guy9a817362009-05-01 10:57:14 -07003605 final String handlerName = a.getString(attr);
3606 if (handlerName != null) {
3607 setOnClickListener(new OnClickListener() {
3608 private Method mHandler;
3609
3610 public void onClick(View v) {
3611 if (mHandler == null) {
3612 try {
3613 mHandler = getContext().getClass().getMethod(handlerName,
3614 View.class);
3615 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003616 int id = getId();
3617 String idText = id == NO_ID ? "" : " with id '"
3618 + getContext().getResources().getResourceEntryName(
3619 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003620 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003621 handlerName + "(View) in the activity "
3622 + getContext().getClass() + " for onClick handler"
3623 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003624 }
3625 }
3626
3627 try {
3628 mHandler.invoke(getContext(), View.this);
3629 } catch (IllegalAccessException e) {
3630 throw new IllegalStateException("Could not execute non "
3631 + "public method of the activity", e);
3632 } catch (InvocationTargetException e) {
3633 throw new IllegalStateException("Could not execute "
3634 + "method of the activity", e);
3635 }
3636 }
3637 });
3638 }
3639 break;
Adam Powell637d3372010-08-25 14:37:03 -07003640 case R.styleable.View_overScrollMode:
3641 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3642 break;
Adam Powell20232d02010-12-08 21:08:53 -08003643 case R.styleable.View_verticalScrollbarPosition:
3644 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3645 break;
Romain Guy171c5922011-01-06 10:04:23 -08003646 case R.styleable.View_layerType:
3647 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3648 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003649 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003650 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003651 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003652 // Set the text direction flags depending on the value of the attribute
3653 final int textDirection = a.getInt(attr, -1);
3654 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003655 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003656 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003657 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003658 case R.styleable.View_textAlignment:
3659 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003660 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003661 // Set the text alignment flag depending on the value of the attribute
3662 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003663 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003664 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003665 case R.styleable.View_importantForAccessibility:
3666 setImportantForAccessibility(a.getInt(attr,
3667 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003668 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 }
3670 }
3671
Adam Powell637d3372010-08-25 14:37:03 -07003672 setOverScrollMode(overScrollMode);
3673
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003674 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3675 // the resolved layout direction). Those cached values will be used later during padding
3676 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003677 mUserPaddingStart = startPadding;
3678 mUserPaddingEnd = endPadding;
3679
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003680 if (background != null) {
3681 setBackground(background);
3682 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 if (padding >= 0) {
3685 leftPadding = padding;
3686 topPadding = padding;
3687 rightPadding = padding;
3688 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003689 mUserPaddingLeftInitial = padding;
3690 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 }
3692
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003693 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003694 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3695 // left / right padding are used if defined (meaning here nothing to do). If they are not
3696 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3697 // start / end and resolve them as left / right (layout direction is not taken into account).
3698 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3699 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3700 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003701 if (!leftPaddingDefined && startPaddingDefined) {
3702 leftPadding = startPadding;
3703 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003704 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003705 if (!rightPaddingDefined && endPaddingDefined) {
3706 rightPadding = endPadding;
3707 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003708 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003709 } else {
3710 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3711 // values defined. Otherwise, left /right values are used.
3712 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3713 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3714 // defined.
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -07003715 final boolean hasRelativePadding = startPaddingDefined || endPaddingDefined;
3716
3717 if (leftPaddingDefined && !hasRelativePadding) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003718 mUserPaddingLeftInitial = leftPadding;
3719 }
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -07003720 if (rightPaddingDefined && !hasRelativePadding) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003721 mUserPaddingRightInitial = rightPadding;
3722 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003723 }
3724
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003725 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003726 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003728 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3730
3731 if (viewFlagMasks != 0) {
3732 setFlags(viewFlagValues, viewFlagMasks);
3733 }
3734
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003735 if (initializeScrollbars) {
3736 initializeScrollbars(a);
3737 }
3738
3739 a.recycle();
3740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 // Needs to be called after mViewFlags is set
3742 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3743 recomputePadding();
3744 }
3745
3746 if (x != 0 || y != 0) {
3747 scrollTo(x, y);
3748 }
3749
Chet Haase73066682010-11-29 15:55:32 -08003750 if (transformSet) {
3751 setTranslationX(tx);
3752 setTranslationY(ty);
3753 setRotation(rotation);
3754 setRotationX(rotationX);
3755 setRotationY(rotationY);
3756 setScaleX(sx);
3757 setScaleY(sy);
3758 }
3759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3761 setScrollContainer(true);
3762 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003763
3764 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 }
3766
3767 /**
3768 * Non-public constructor for use in testing
3769 */
3770 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003771 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 }
3773
Dianne Hackborn4702a852012-08-17 15:18:29 -07003774 public String toString() {
3775 StringBuilder out = new StringBuilder(128);
3776 out.append(getClass().getName());
3777 out.append('{');
3778 out.append(Integer.toHexString(System.identityHashCode(this)));
3779 out.append(' ');
3780 switch (mViewFlags&VISIBILITY_MASK) {
3781 case VISIBLE: out.append('V'); break;
3782 case INVISIBLE: out.append('I'); break;
3783 case GONE: out.append('G'); break;
3784 default: out.append('.'); break;
3785 }
3786 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3787 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3788 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3789 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3790 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3791 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3792 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3793 out.append(' ');
3794 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3795 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3796 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3797 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3798 out.append('p');
3799 } else {
3800 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3801 }
3802 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3803 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3804 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3805 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3806 out.append(' ');
3807 out.append(mLeft);
3808 out.append(',');
3809 out.append(mTop);
3810 out.append('-');
3811 out.append(mRight);
3812 out.append(',');
3813 out.append(mBottom);
3814 final int id = getId();
3815 if (id != NO_ID) {
3816 out.append(" #");
3817 out.append(Integer.toHexString(id));
3818 final Resources r = mResources;
3819 if (id != 0 && r != null) {
3820 try {
3821 String pkgname;
3822 switch (id&0xff000000) {
3823 case 0x7f000000:
3824 pkgname="app";
3825 break;
3826 case 0x01000000:
3827 pkgname="android";
3828 break;
3829 default:
3830 pkgname = r.getResourcePackageName(id);
3831 break;
3832 }
3833 String typename = r.getResourceTypeName(id);
3834 String entryname = r.getResourceEntryName(id);
3835 out.append(" ");
3836 out.append(pkgname);
3837 out.append(":");
3838 out.append(typename);
3839 out.append("/");
3840 out.append(entryname);
3841 } catch (Resources.NotFoundException e) {
3842 }
3843 }
3844 }
3845 out.append("}");
3846 return out.toString();
3847 }
3848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 /**
3850 * <p>
3851 * Initializes the fading edges from a given set of styled attributes. This
3852 * method should be called by subclasses that need fading edges and when an
3853 * instance of these subclasses is created programmatically rather than
3854 * being inflated from XML. This method is automatically called when the XML
3855 * is inflated.
3856 * </p>
3857 *
3858 * @param a the styled attributes set to initialize the fading edges from
3859 */
3860 protected void initializeFadingEdge(TypedArray a) {
3861 initScrollCache();
3862
3863 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3864 R.styleable.View_fadingEdgeLength,
3865 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3866 }
3867
3868 /**
3869 * Returns the size of the vertical faded edges used to indicate that more
3870 * content in this view is visible.
3871 *
3872 * @return The size in pixels of the vertical faded edge or 0 if vertical
3873 * faded edges are not enabled for this view.
3874 * @attr ref android.R.styleable#View_fadingEdgeLength
3875 */
3876 public int getVerticalFadingEdgeLength() {
3877 if (isVerticalFadingEdgeEnabled()) {
3878 ScrollabilityCache cache = mScrollCache;
3879 if (cache != null) {
3880 return cache.fadingEdgeLength;
3881 }
3882 }
3883 return 0;
3884 }
3885
3886 /**
3887 * Set the size of the faded edge used to indicate that more content in this
3888 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003889 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3890 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3891 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 *
3893 * @param length The size in pixels of the faded edge used to indicate that more
3894 * content in this view is visible.
3895 */
3896 public void setFadingEdgeLength(int length) {
3897 initScrollCache();
3898 mScrollCache.fadingEdgeLength = length;
3899 }
3900
3901 /**
3902 * Returns the size of the horizontal faded edges used to indicate that more
3903 * content in this view is visible.
3904 *
3905 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3906 * faded edges are not enabled for this view.
3907 * @attr ref android.R.styleable#View_fadingEdgeLength
3908 */
3909 public int getHorizontalFadingEdgeLength() {
3910 if (isHorizontalFadingEdgeEnabled()) {
3911 ScrollabilityCache cache = mScrollCache;
3912 if (cache != null) {
3913 return cache.fadingEdgeLength;
3914 }
3915 }
3916 return 0;
3917 }
3918
3919 /**
3920 * Returns the width of the vertical scrollbar.
3921 *
3922 * @return The width in pixels of the vertical scrollbar or 0 if there
3923 * is no vertical scrollbar.
3924 */
3925 public int getVerticalScrollbarWidth() {
3926 ScrollabilityCache cache = mScrollCache;
3927 if (cache != null) {
3928 ScrollBarDrawable scrollBar = cache.scrollBar;
3929 if (scrollBar != null) {
3930 int size = scrollBar.getSize(true);
3931 if (size <= 0) {
3932 size = cache.scrollBarSize;
3933 }
3934 return size;
3935 }
3936 return 0;
3937 }
3938 return 0;
3939 }
3940
3941 /**
3942 * Returns the height of the horizontal scrollbar.
3943 *
3944 * @return The height in pixels of the horizontal scrollbar or 0 if
3945 * there is no horizontal scrollbar.
3946 */
3947 protected int getHorizontalScrollbarHeight() {
3948 ScrollabilityCache cache = mScrollCache;
3949 if (cache != null) {
3950 ScrollBarDrawable scrollBar = cache.scrollBar;
3951 if (scrollBar != null) {
3952 int size = scrollBar.getSize(false);
3953 if (size <= 0) {
3954 size = cache.scrollBarSize;
3955 }
3956 return size;
3957 }
3958 return 0;
3959 }
3960 return 0;
3961 }
3962
3963 /**
3964 * <p>
3965 * Initializes the scrollbars from a given set of styled attributes. This
3966 * method should be called by subclasses that need scrollbars and when an
3967 * instance of these subclasses is created programmatically rather than
3968 * being inflated from XML. This method is automatically called when the XML
3969 * is inflated.
3970 * </p>
3971 *
3972 * @param a the styled attributes set to initialize the scrollbars from
3973 */
3974 protected void initializeScrollbars(TypedArray a) {
3975 initScrollCache();
3976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003978
Mike Cleronf116bf82009-09-27 19:14:12 -07003979 if (scrollabilityCache.scrollBar == null) {
3980 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3981 }
Joe Malin32736f02011-01-19 16:14:20 -08003982
Romain Guy8bda2482010-03-02 11:42:11 -08003983 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984
Mike Cleronf116bf82009-09-27 19:14:12 -07003985 if (!fadeScrollbars) {
3986 scrollabilityCache.state = ScrollabilityCache.ON;
3987 }
3988 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003989
3990
Mike Cleronf116bf82009-09-27 19:14:12 -07003991 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3992 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3993 .getScrollBarFadeDuration());
3994 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3995 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3996 ViewConfiguration.getScrollDefaultDelay());
3997
Joe Malin32736f02011-01-19 16:14:20 -08003998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
4000 com.android.internal.R.styleable.View_scrollbarSize,
4001 ViewConfiguration.get(mContext).getScaledScrollBarSize());
4002
4003 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
4004 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
4005
4006 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
4007 if (thumb != null) {
4008 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
4009 }
4010
4011 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
4012 false);
4013 if (alwaysDraw) {
4014 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
4015 }
4016
4017 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
4018 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
4019
4020 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
4021 if (thumb != null) {
4022 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
4023 }
4024
4025 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
4026 false);
4027 if (alwaysDraw) {
4028 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
4029 }
4030
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07004031 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07004032 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07004033 if (track != null) {
4034 track.setLayoutDirection(layoutDirection);
4035 }
4036 if (thumb != null) {
4037 thumb.setLayoutDirection(layoutDirection);
4038 }
4039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004041 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 }
4043
4044 /**
4045 * <p>
4046 * Initalizes the scrollability cache if necessary.
4047 * </p>
4048 */
4049 private void initScrollCache() {
4050 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004051 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 }
4053 }
4054
Philip Milne6c8ea062012-04-03 17:38:43 -07004055 private ScrollabilityCache getScrollCache() {
4056 initScrollCache();
4057 return mScrollCache;
4058 }
4059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 /**
Adam Powell20232d02010-12-08 21:08:53 -08004061 * Set the position of the vertical scroll bar. Should be one of
4062 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4063 * {@link #SCROLLBAR_POSITION_RIGHT}.
4064 *
4065 * @param position Where the vertical scroll bar should be positioned.
4066 */
4067 public void setVerticalScrollbarPosition(int position) {
4068 if (mVerticalScrollbarPosition != position) {
4069 mVerticalScrollbarPosition = position;
4070 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004071 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004072 }
4073 }
4074
4075 /**
4076 * @return The position where the vertical scroll bar will show, if applicable.
4077 * @see #setVerticalScrollbarPosition(int)
4078 */
4079 public int getVerticalScrollbarPosition() {
4080 return mVerticalScrollbarPosition;
4081 }
4082
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004083 ListenerInfo getListenerInfo() {
4084 if (mListenerInfo != null) {
4085 return mListenerInfo;
4086 }
4087 mListenerInfo = new ListenerInfo();
4088 return mListenerInfo;
4089 }
4090
Adam Powell20232d02010-12-08 21:08:53 -08004091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 * Register a callback to be invoked when focus of this view changed.
4093 *
4094 * @param l The callback that will run.
4095 */
4096 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 }
4099
4100 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004101 * Add a listener that will be called when the bounds of the view change due to
4102 * layout processing.
4103 *
4104 * @param listener The listener that will be called when layout bounds change.
4105 */
4106 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004107 ListenerInfo li = getListenerInfo();
4108 if (li.mOnLayoutChangeListeners == null) {
4109 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004110 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004111 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4112 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004113 }
Chet Haase21cd1382010-09-01 17:42:29 -07004114 }
4115
4116 /**
4117 * Remove a listener for layout changes.
4118 *
4119 * @param listener The listener for layout bounds change.
4120 */
4121 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004122 ListenerInfo li = mListenerInfo;
4123 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004124 return;
4125 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004126 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004127 }
4128
4129 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004130 * Add a listener for attach state changes.
4131 *
4132 * This listener will be called whenever this view is attached or detached
4133 * from a window. Remove the listener using
4134 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4135 *
4136 * @param listener Listener to attach
4137 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4138 */
4139 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004140 ListenerInfo li = getListenerInfo();
4141 if (li.mOnAttachStateChangeListeners == null) {
4142 li.mOnAttachStateChangeListeners
4143 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004144 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004145 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004146 }
4147
4148 /**
4149 * Remove a listener for attach state changes. The listener will receive no further
4150 * notification of window attach/detach events.
4151 *
4152 * @param listener Listener to remove
4153 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4154 */
4155 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004156 ListenerInfo li = mListenerInfo;
4157 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004158 return;
4159 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004160 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004161 }
4162
4163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 * Returns the focus-change callback registered for this view.
4165 *
4166 * @return The callback, or null if one is not registered.
4167 */
4168 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004169 ListenerInfo li = mListenerInfo;
4170 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 }
4172
4173 /**
4174 * Register a callback to be invoked when this view is clicked. If this view is not
4175 * clickable, it becomes clickable.
4176 *
4177 * @param l The callback that will run
4178 *
4179 * @see #setClickable(boolean)
4180 */
4181 public void setOnClickListener(OnClickListener l) {
4182 if (!isClickable()) {
4183 setClickable(true);
4184 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004185 getListenerInfo().mOnClickListener = l;
4186 }
4187
4188 /**
4189 * Return whether this view has an attached OnClickListener. Returns
4190 * true if there is a listener, false if there is none.
4191 */
4192 public boolean hasOnClickListeners() {
4193 ListenerInfo li = mListenerInfo;
4194 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196
4197 /**
4198 * Register a callback to be invoked when this view is clicked and held. If this view is not
4199 * long clickable, it becomes long clickable.
4200 *
4201 * @param l The callback that will run
4202 *
4203 * @see #setLongClickable(boolean)
4204 */
4205 public void setOnLongClickListener(OnLongClickListener l) {
4206 if (!isLongClickable()) {
4207 setLongClickable(true);
4208 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004209 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
4211
4212 /**
4213 * Register a callback to be invoked when the context menu for this view is
4214 * being built. If this view is not long clickable, it becomes long clickable.
4215 *
4216 * @param l The callback that will run
4217 *
4218 */
4219 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4220 if (!isLongClickable()) {
4221 setLongClickable(true);
4222 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004223 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 }
4225
4226 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004227 * Call this view's OnClickListener, if it is defined. Performs all normal
4228 * actions associated with clicking: reporting accessibility event, playing
4229 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 *
4231 * @return True there was an assigned OnClickListener that was called, false
4232 * otherwise is returned.
4233 */
4234 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004235 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4236
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004237 ListenerInfo li = mListenerInfo;
4238 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004240 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 return true;
4242 }
4243
4244 return false;
4245 }
4246
4247 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004248 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4249 * this only calls the listener, and does not do any associated clicking
4250 * actions like reporting an accessibility event.
4251 *
4252 * @return True there was an assigned OnClickListener that was called, false
4253 * otherwise is returned.
4254 */
4255 public boolean callOnClick() {
4256 ListenerInfo li = mListenerInfo;
4257 if (li != null && li.mOnClickListener != null) {
4258 li.mOnClickListener.onClick(this);
4259 return true;
4260 }
4261 return false;
4262 }
4263
4264 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004265 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4266 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004268 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 */
4270 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004271 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004274 ListenerInfo li = mListenerInfo;
4275 if (li != null && li.mOnLongClickListener != null) {
4276 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 }
4278 if (!handled) {
4279 handled = showContextMenu();
4280 }
4281 if (handled) {
4282 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4283 }
4284 return handled;
4285 }
4286
4287 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004288 * Performs button-related actions during a touch down event.
4289 *
4290 * @param event The event.
4291 * @return True if the down was consumed.
4292 *
4293 * @hide
4294 */
4295 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4296 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4297 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4298 return true;
4299 }
4300 }
4301 return false;
4302 }
4303
4304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 * Bring up the context menu for this view.
4306 *
4307 * @return Whether a context menu was displayed.
4308 */
4309 public boolean showContextMenu() {
4310 return getParent().showContextMenuForChild(this);
4311 }
4312
4313 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004314 * Bring up the context menu for this view, referring to the item under the specified point.
4315 *
4316 * @param x The referenced x coordinate.
4317 * @param y The referenced y coordinate.
4318 * @param metaState The keyboard modifiers that were pressed.
4319 * @return Whether a context menu was displayed.
4320 *
4321 * @hide
4322 */
4323 public boolean showContextMenu(float x, float y, int metaState) {
4324 return showContextMenu();
4325 }
4326
4327 /**
Adam Powell6e346362010-07-23 10:18:23 -07004328 * Start an action mode.
4329 *
4330 * @param callback Callback that will control the lifecycle of the action mode
4331 * @return The new action mode if it is started, null otherwise
4332 *
4333 * @see ActionMode
4334 */
4335 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004336 ViewParent parent = getParent();
4337 if (parent == null) return null;
4338 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004339 }
4340
4341 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004342 * Register a callback to be invoked when a hardware key is pressed in this view.
4343 * Key presses in software input methods will generally not trigger the methods of
4344 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 * @param l the key listener to attach to this view
4346 */
4347 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004348 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 }
4350
4351 /**
4352 * Register a callback to be invoked when a touch event is sent to this view.
4353 * @param l the touch listener to attach to this view
4354 */
4355 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004356 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
4358
4359 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004360 * Register a callback to be invoked when a generic motion event is sent to this view.
4361 * @param l the generic motion listener to attach to this view
4362 */
4363 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004364 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004365 }
4366
4367 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004368 * Register a callback to be invoked when a hover event is sent to this view.
4369 * @param l the hover listener to attach to this view
4370 */
4371 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004372 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004373 }
4374
4375 /**
Joe Malin32736f02011-01-19 16:14:20 -08004376 * Register a drag event listener callback object for this View. The parameter is
4377 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4378 * View, the system calls the
4379 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4380 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004381 */
4382 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004383 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004384 }
4385
4386 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004387 * Give this view focus. This will cause
4388 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 *
4390 * Note: this does not check whether this {@link View} should get focus, it just
4391 * gives it focus no matter what. It should only be called internally by framework
4392 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4393 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004394 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4395 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 * focus moved when requestFocus() is called. It may not always
4397 * apply, in which case use the default View.FOCUS_DOWN.
4398 * @param previouslyFocusedRect The rectangle of the view that had focus
4399 * prior in this View's coordinate system.
4400 */
4401 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4402 if (DBG) {
4403 System.out.println(this + " requestFocus()");
4404 }
4405
Dianne Hackborn4702a852012-08-17 15:18:29 -07004406 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4407 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004409 View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null;
4410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 if (mParent != null) {
4412 mParent.requestChildFocus(this, this);
4413 }
4414
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004415 if (mAttachInfo != null) {
4416 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, this);
4417 }
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 onFocusChanged(true, direction, previouslyFocusedRect);
4420 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004421
4422 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4423 notifyAccessibilityStateChanged();
4424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 }
4426 }
4427
4428 /**
4429 * Request that a rectangle of this view be visible on the screen,
4430 * scrolling if necessary just enough.
4431 *
4432 * <p>A View should call this if it maintains some notion of which part
4433 * of its content is interesting. For example, a text editing view
4434 * should call this when its cursor moves.
4435 *
4436 * @param rectangle The rectangle.
4437 * @return Whether any parent scrolled.
4438 */
4439 public boolean requestRectangleOnScreen(Rect rectangle) {
4440 return requestRectangleOnScreen(rectangle, false);
4441 }
4442
4443 /**
4444 * Request that a rectangle of this view be visible on the screen,
4445 * scrolling if necessary just enough.
4446 *
4447 * <p>A View should call this if it maintains some notion of which part
4448 * of its content is interesting. For example, a text editing view
4449 * should call this when its cursor moves.
4450 *
4451 * <p>When <code>immediate</code> is set to true, scrolling will not be
4452 * animated.
4453 *
4454 * @param rectangle The rectangle.
4455 * @param immediate True to forbid animated scrolling, false otherwise
4456 * @return Whether any parent scrolled.
4457 */
4458 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004459 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004460 return false;
4461 }
4462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004464
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004465 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004466 position.set(rectangle);
4467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 ViewParent parent = mParent;
4469 boolean scrolled = false;
4470 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004471 rectangle.set((int) position.left, (int) position.top,
4472 (int) position.right, (int) position.bottom);
4473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 scrolled |= parent.requestChildRectangleOnScreen(child,
4475 rectangle, immediate);
4476
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004477 if (!child.hasIdentityMatrix()) {
4478 child.getMatrix().mapRect(position);
4479 }
4480
4481 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482
4483 if (!(parent instanceof View)) {
4484 break;
4485 }
Romain Guy8506ab42009-06-11 17:35:47 -07004486
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004487 View parentView = (View) parent;
4488
4489 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4490
4491 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 parent = child.getParent();
4493 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 return scrolled;
4496 }
4497
4498 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004499 * Called when this view wants to give up focus. If focus is cleared
4500 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4501 * <p>
4502 * <strong>Note:</strong> When a View clears focus the framework is trying
4503 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004504 * View is the first from the top that can take focus, then all callbacks
4505 * related to clearing focus will be invoked after wich the framework will
4506 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004507 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 */
4509 public void clearFocus() {
4510 if (DBG) {
4511 System.out.println(this + " clearFocus()");
4512 }
4513
Dianne Hackborn4702a852012-08-17 15:18:29 -07004514 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4515 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516
4517 if (mParent != null) {
4518 mParent.clearChildFocus(this);
4519 }
4520
4521 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004524
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004525 if (!rootViewRequestFocus()) {
4526 notifyGlobalFocusCleared(this);
4527 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004528
4529 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4530 notifyAccessibilityStateChanged();
4531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 }
4533 }
4534
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004535 void notifyGlobalFocusCleared(View oldFocus) {
4536 if (oldFocus != null && mAttachInfo != null) {
4537 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
4538 }
4539 }
4540
4541 boolean rootViewRequestFocus() {
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004542 View root = getRootView();
4543 if (root != null) {
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004544 return root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004546 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 }
4548
4549 /**
4550 * Called internally by the view system when a new view is getting focus.
4551 * This is what clears the old focus.
4552 */
4553 void unFocus() {
4554 if (DBG) {
4555 System.out.println(this + " unFocus()");
4556 }
4557
Dianne Hackborn4702a852012-08-17 15:18:29 -07004558 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4559 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560
4561 onFocusChanged(false, 0, null);
4562 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004563
4564 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4565 notifyAccessibilityStateChanged();
4566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 }
4568 }
4569
4570 /**
4571 * Returns true if this view has focus iteself, or is the ancestor of the
4572 * view that has focus.
4573 *
4574 * @return True if this view has or contains focus, false otherwise.
4575 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004576 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004578 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 }
4580
4581 /**
4582 * Returns true if this view is focusable or if it contains a reachable View
4583 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4584 * is a View whose parents do not block descendants focus.
4585 *
4586 * Only {@link #VISIBLE} views are considered focusable.
4587 *
4588 * @return True if the view is focusable or if the view contains a focusable
4589 * View, false otherwise.
4590 *
4591 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4592 */
4593 public boolean hasFocusable() {
4594 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4595 }
4596
4597 /**
4598 * Called by the view system when the focus state of this view changes.
4599 * When the focus change event is caused by directional navigation, direction
4600 * and previouslyFocusedRect provide insight into where the focus is coming from.
4601 * When overriding, be sure to call up through to the super class so that
4602 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004603 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 * @param gainFocus True if the View has focus; false otherwise.
4605 * @param direction The direction focus has moved when requestFocus()
4606 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004607 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4608 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4609 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4611 * system, of the previously focused view. If applicable, this will be
4612 * passed in as finer grained information about where the focus is coming
4613 * from (in addition to direction). Will be <code>null</code> otherwise.
4614 */
4615 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004616 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004617 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4618 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004619 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004620 }
4621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 InputMethodManager imm = InputMethodManager.peekInstance();
4623 if (!gainFocus) {
4624 if (isPressed()) {
4625 setPressed(false);
4626 }
4627 if (imm != null && mAttachInfo != null
4628 && mAttachInfo.mHasWindowFocus) {
4629 imm.focusOut(this);
4630 }
Romain Guya2431d02009-04-30 16:30:00 -07004631 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 } else if (imm != null && mAttachInfo != null
4633 && mAttachInfo.mHasWindowFocus) {
4634 imm.focusIn(this);
4635 }
Romain Guy8506ab42009-06-11 17:35:47 -07004636
Romain Guy0fd89bf2011-01-26 15:41:30 -08004637 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004638 ListenerInfo li = mListenerInfo;
4639 if (li != null && li.mOnFocusChangeListener != null) {
4640 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 }
Joe Malin32736f02011-01-19 16:14:20 -08004642
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004643 if (mAttachInfo != null) {
4644 mAttachInfo.mKeyDispatchState.reset(this);
4645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 }
4647
4648 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004649 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004650 * not enabled this method has no effect. The default implementation calls
4651 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4652 * to populate information about the event source (this View), then calls
4653 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4654 * populate the text content of the event source including its descendants,
4655 * and last calls
4656 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4657 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 * <p>
4659 * If an {@link AccessibilityDelegate} has been specified via calling
4660 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4661 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4662 * responsible for handling this call.
4663 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004664 *
Scott Mainb303d832011-10-12 16:45:18 -07004665 * @param eventType The type of the event to send, as defined by several types from
4666 * {@link android.view.accessibility.AccessibilityEvent}, such as
4667 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4668 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004669 *
4670 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4671 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4672 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004673 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004674 */
4675 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004676 if (mAccessibilityDelegate != null) {
4677 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4678 } else {
4679 sendAccessibilityEventInternal(eventType);
4680 }
4681 }
4682
4683 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004684 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4685 * {@link AccessibilityEvent} to make an announcement which is related to some
4686 * sort of a context change for which none of the events representing UI transitions
4687 * is a good fit. For example, announcing a new page in a book. If accessibility
4688 * is not enabled this method does nothing.
4689 *
4690 * @param text The announcement text.
4691 */
4692 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004693 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004694 AccessibilityEvent event = AccessibilityEvent.obtain(
4695 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004696 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004697 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004698 event.setContentDescription(null);
4699 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004700 }
4701 }
4702
4703 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004704 * @see #sendAccessibilityEvent(int)
4705 *
4706 * Note: Called from the default {@link AccessibilityDelegate}.
4707 */
4708 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004709 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4710 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4711 }
4712 }
4713
4714 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004715 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4716 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004717 * perform a check whether accessibility is enabled.
4718 * <p>
4719 * If an {@link AccessibilityDelegate} has been specified via calling
4720 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4721 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4722 * is responsible for handling this call.
4723 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004724 *
4725 * @param event The event to send.
4726 *
4727 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004728 */
4729 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004730 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004731 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004732 } else {
4733 sendAccessibilityEventUncheckedInternal(event);
4734 }
4735 }
4736
4737 /**
4738 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4739 *
4740 * Note: Called from the default {@link AccessibilityDelegate}.
4741 */
4742 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004743 if (!isShown()) {
4744 return;
4745 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004746 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004747 // Only a subset of accessibility events populates text content.
4748 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4749 dispatchPopulateAccessibilityEvent(event);
4750 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004751 // In the beginning we called #isShown(), so we know that getParent() is not null.
4752 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004753 }
4754
4755 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004756 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4757 * to its children for adding their text content to the event. Note that the
4758 * event text is populated in a separate dispatch path since we add to the
4759 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004760 * A typical implementation will call
4761 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4762 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4763 * on each child. Override this method if custom population of the event text
4764 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004765 * <p>
4766 * If an {@link AccessibilityDelegate} has been specified via calling
4767 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4768 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4769 * is responsible for handling this call.
4770 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004771 * <p>
4772 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4773 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4774 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004775 *
4776 * @param event The event.
4777 *
4778 * @return True if the event population was completed.
4779 */
4780 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004781 if (mAccessibilityDelegate != null) {
4782 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4783 } else {
4784 return dispatchPopulateAccessibilityEventInternal(event);
4785 }
4786 }
4787
4788 /**
4789 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4790 *
4791 * Note: Called from the default {@link AccessibilityDelegate}.
4792 */
4793 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004794 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004795 return false;
4796 }
4797
4798 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004799 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004800 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004801 * text content. While this method is free to modify event
4802 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004803 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4804 * <p>
4805 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004806 * to the text added by the super implementation:
4807 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004808 * super.onPopulateAccessibilityEvent(event);
4809 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4810 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4811 * mCurrentDate.getTimeInMillis(), flags);
4812 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004813 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004814 * <p>
4815 * If an {@link AccessibilityDelegate} has been specified via calling
4816 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4817 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4818 * is responsible for handling this call.
4819 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004820 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4821 * information to the event, in case the default implementation has basic information to add.
4822 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004823 *
4824 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004825 *
4826 * @see #sendAccessibilityEvent(int)
4827 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004828 */
4829 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004830 if (mAccessibilityDelegate != null) {
4831 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4832 } else {
4833 onPopulateAccessibilityEventInternal(event);
4834 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004835 }
4836
4837 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004838 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4839 *
4840 * Note: Called from the default {@link AccessibilityDelegate}.
4841 */
4842 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4843
4844 }
4845
4846 /**
4847 * Initializes an {@link AccessibilityEvent} with information about
4848 * this View which is the event source. In other words, the source of
4849 * an accessibility event is the view whose state change triggered firing
4850 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004851 * <p>
4852 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004853 * to properties set by the super implementation:
4854 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4855 * super.onInitializeAccessibilityEvent(event);
4856 * event.setPassword(true);
4857 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004858 * <p>
4859 * If an {@link AccessibilityDelegate} has been specified via calling
4860 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4861 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4862 * is responsible for handling this call.
4863 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004864 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4865 * information to the event, in case the default implementation has basic information to add.
4866 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004867 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004868 *
4869 * @see #sendAccessibilityEvent(int)
4870 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4871 */
4872 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004873 if (mAccessibilityDelegate != null) {
4874 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4875 } else {
4876 onInitializeAccessibilityEventInternal(event);
4877 }
4878 }
4879
4880 /**
4881 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4882 *
4883 * Note: Called from the default {@link AccessibilityDelegate}.
4884 */
4885 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004886 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004887 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004888 event.setPackageName(getContext().getPackageName());
4889 event.setEnabled(isEnabled());
4890 event.setContentDescription(mContentDescription);
4891
Svetoslavbcc46a02013-02-06 11:56:00 -08004892 switch (event.getEventType()) {
4893 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
4894 ArrayList<View> focusablesTempList = (mAttachInfo != null)
4895 ? mAttachInfo.mTempArrayList : new ArrayList<View>();
4896 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
4897 event.setItemCount(focusablesTempList.size());
4898 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4899 if (mAttachInfo != null) {
4900 focusablesTempList.clear();
4901 }
4902 } break;
4903 case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED: {
4904 CharSequence text = getIterableTextForAccessibility();
4905 if (text != null && text.length() > 0) {
4906 event.setFromIndex(getAccessibilitySelectionStart());
4907 event.setToIndex(getAccessibilitySelectionEnd());
4908 event.setItemCount(text.length());
4909 }
4910 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07004911 }
4912 }
4913
4914 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004915 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4916 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4917 * This method is responsible for obtaining an accessibility node info from a
4918 * pool of reusable instances and calling
4919 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4920 * initialize the former.
4921 * <p>
4922 * Note: The client is responsible for recycling the obtained instance by calling
4923 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4924 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004925 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004926 * @return A populated {@link AccessibilityNodeInfo}.
4927 *
4928 * @see AccessibilityNodeInfo
4929 */
4930 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov50776862013-05-17 18:06:31 -07004931 if (mAccessibilityDelegate != null) {
4932 return mAccessibilityDelegate.createAccessibilityNodeInfo(this);
4933 } else {
4934 return createAccessibilityNodeInfoInternal();
4935 }
4936 }
4937
4938 /**
4939 * @see #createAccessibilityNodeInfo()
4940 */
4941 AccessibilityNodeInfo createAccessibilityNodeInfoInternal() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004942 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4943 if (provider != null) {
4944 return provider.createAccessibilityNodeInfo(View.NO_ID);
4945 } else {
4946 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4947 onInitializeAccessibilityNodeInfo(info);
4948 return info;
4949 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004950 }
4951
4952 /**
4953 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4954 * The base implementation sets:
4955 * <ul>
4956 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004957 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4958 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004959 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4960 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4961 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4962 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4963 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4964 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4965 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4966 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4967 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4968 * </ul>
4969 * <p>
4970 * Subclasses should override this method, call the super implementation,
4971 * and set additional attributes.
4972 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004973 * <p>
4974 * If an {@link AccessibilityDelegate} has been specified via calling
4975 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4976 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4977 * is responsible for handling this call.
4978 * </p>
4979 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004980 * @param info The instance to initialize.
4981 */
4982 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004983 if (mAccessibilityDelegate != null) {
4984 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4985 } else {
4986 onInitializeAccessibilityNodeInfoInternal(info);
4987 }
4988 }
4989
4990 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004991 * Gets the location of this view in screen coordintates.
4992 *
4993 * @param outRect The output location
4994 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004995 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004996 if (mAttachInfo == null) {
4997 return;
4998 }
4999
5000 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07005001 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005002
5003 if (!hasIdentityMatrix()) {
5004 getMatrix().mapRect(position);
5005 }
5006
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07005007 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005008
5009 ViewParent parent = mParent;
5010 while (parent instanceof View) {
5011 View parentView = (View) parent;
5012
5013 position.offset(-parentView.mScrollX, -parentView.mScrollY);
5014
5015 if (!parentView.hasIdentityMatrix()) {
5016 parentView.getMatrix().mapRect(position);
5017 }
5018
5019 position.offset(parentView.mLeft, parentView.mTop);
5020
5021 parent = parentView.mParent;
5022 }
5023
5024 if (parent instanceof ViewRootImpl) {
5025 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
5026 position.offset(0, -viewRootImpl.mCurScrollY);
5027 }
5028
5029 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
5030
5031 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
5032 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
5033 }
5034
5035 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005036 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
5037 *
5038 * Note: Called from the default {@link AccessibilityDelegate}.
5039 */
5040 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005041 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07005042
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005043 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07005044 info.setBoundsInParent(bounds);
5045
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005046 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07005047 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005048
Svetoslav Ganovc406be92012-05-11 16:12:32 -07005049 ViewParent parent = getParentForAccessibility();
5050 if (parent instanceof View) {
5051 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005052 }
5053
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005054 if (mID != View.NO_ID) {
5055 View rootView = getRootView();
5056 if (rootView == null) {
5057 rootView = this;
5058 }
5059 View label = rootView.findLabelForView(this, mID);
5060 if (label != null) {
5061 info.setLabeledBy(label);
5062 }
Svetoslav Ganov80943d82013-01-02 10:25:37 -08005063
5064 if ((mAttachInfo.mAccessibilityFetchFlags
Jeff Sharkey47b50332013-03-15 14:46:46 -07005065 & AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS) != 0
5066 && Resources.resourceHasPackage(mID)) {
Svetoslav Ganov80943d82013-01-02 10:25:37 -08005067 try {
Svetoslava33243e2013-02-11 15:43:46 -08005068 String viewId = getResources().getResourceName(mID);
5069 info.setViewIdResourceName(viewId);
Svetoslav Ganov80943d82013-01-02 10:25:37 -08005070 } catch (Resources.NotFoundException nfe) {
5071 /* ignore */
5072 }
Svetoslav Ganov80943d82013-01-02 10:25:37 -08005073 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005074 }
5075
5076 if (mLabelForId != View.NO_ID) {
5077 View rootView = getRootView();
5078 if (rootView == null) {
5079 rootView = this;
5080 }
5081 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
5082 if (labeled != null) {
5083 info.setLabelFor(labeled);
5084 }
5085 }
5086
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005087 info.setVisibleToUser(isVisibleToUser());
5088
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005089 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005090 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005091 info.setContentDescription(getContentDescription());
5092
5093 info.setEnabled(isEnabled());
5094 info.setClickable(isClickable());
5095 info.setFocusable(isFocusable());
5096 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005097 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005098 info.setSelected(isSelected());
5099 info.setLongClickable(isLongClickable());
5100
5101 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005102 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005103 // we should report as selectable view in an AdapterView.
5104 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5105 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5106
5107 if (isFocusable()) {
5108 if (isFocused()) {
5109 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5110 } else {
5111 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5112 }
5113 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005114
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005115 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005116 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005117 } else {
5118 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5119 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005120
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005121 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005122 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5123 }
5124
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005125 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005126 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5127 }
5128
Svetoslavbcc46a02013-02-06 11:56:00 -08005129 CharSequence text = getIterableTextForAccessibility();
5130 if (text != null && text.length() > 0) {
5131 info.setTextSelection(getAccessibilitySelectionStart(), getAccessibilitySelectionEnd());
5132
Svetoslavd0c83cc2013-02-04 18:39:59 -08005133 info.addAction(AccessibilityNodeInfo.ACTION_SET_SELECTION);
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005134 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5135 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5136 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005137 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5138 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005139 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005140 }
5141
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005142 private View findLabelForView(View view, int labeledId) {
5143 if (mMatchLabelForPredicate == null) {
5144 mMatchLabelForPredicate = new MatchLabelForPredicate();
5145 }
5146 mMatchLabelForPredicate.mLabeledId = labeledId;
5147 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5148 }
5149
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005150 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005151 * Computes whether this view is visible to the user. Such a view is
5152 * attached, visible, all its predecessors are visible, it is not clipped
5153 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005154 *
5155 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005156 *
5157 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005158 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005159 protected boolean isVisibleToUser() {
5160 return isVisibleToUser(null);
5161 }
5162
5163 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005164 * Computes whether the given portion of this view is visible to the user.
5165 * Such a view is attached, visible, all its predecessors are visible,
5166 * has an alpha greater than zero, and the specified portion is not
5167 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005168 *
5169 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5170 * <code>null</code>, and the entire view will be tested in this case.
5171 * When <code>true</code> is returned by the function, the actual visible
5172 * region will be stored in this parameter; that is, if boundInView is fully
5173 * contained within the view, no modification will be made, otherwise regions
5174 * outside of the visible area of the view will be clipped.
5175 *
5176 * @return Whether the specified portion of the view is visible on the screen.
5177 *
5178 * @hide
5179 */
5180 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005181 if (mAttachInfo != null) {
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005182 // Attached to invisible window means this view is not visible.
5183 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
5184 return false;
5185 }
5186 // An invisible predecessor or one with alpha zero means
5187 // that this view is not visible to the user.
5188 Object current = this;
5189 while (current instanceof View) {
5190 View view = (View) current;
5191 // We have attach info so this view is attached and there is no
5192 // need to check whether we reach to ViewRootImpl on the way up.
5193 if (view.getAlpha() <= 0 || view.getVisibility() != VISIBLE) {
5194 return false;
5195 }
5196 current = view.mParent;
5197 }
5198 // Check if the view is entirely covered by its predecessors.
Romain Guyf0af1d52012-07-11 18:31:21 -07005199 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5200 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005201 if (!getGlobalVisibleRect(visibleRect, offset)) {
5202 return false;
Guang Zhu0d607fb2012-05-11 19:34:56 -07005203 }
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005204 // Check if the visible portion intersects the rectangle of interest.
5205 if (boundInView != null) {
5206 visibleRect.offset(-offset.x, -offset.y);
5207 return boundInView.intersect(visibleRect);
5208 }
5209 return true;
Romain Guyf0af1d52012-07-11 18:31:21 -07005210 }
Romain Guyf0af1d52012-07-11 18:31:21 -07005211 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005212 }
5213
5214 /**
alanvb72fe7a2012-08-27 16:44:25 -07005215 * Returns the delegate for implementing accessibility support via
5216 * composition. For more details see {@link AccessibilityDelegate}.
5217 *
5218 * @return The delegate, or null if none set.
5219 *
5220 * @hide
5221 */
5222 public AccessibilityDelegate getAccessibilityDelegate() {
5223 return mAccessibilityDelegate;
5224 }
5225
5226 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005227 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005228 * opposed to inheritance. The delegate's primary use is for implementing
5229 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5230 *
5231 * @param delegate The delegate instance.
5232 *
5233 * @see AccessibilityDelegate
5234 */
5235 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5236 mAccessibilityDelegate = delegate;
5237 }
5238
5239 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005240 * Gets the provider for managing a virtual view hierarchy rooted at this View
5241 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5242 * that explore the window content.
5243 * <p>
5244 * If this method returns an instance, this instance is responsible for managing
5245 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5246 * View including the one representing the View itself. Similarly the returned
5247 * instance is responsible for performing accessibility actions on any virtual
5248 * view or the root view itself.
5249 * </p>
5250 * <p>
5251 * If an {@link AccessibilityDelegate} has been specified via calling
5252 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5253 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5254 * is responsible for handling this call.
5255 * </p>
5256 *
5257 * @return The provider.
5258 *
5259 * @see AccessibilityNodeProvider
5260 */
5261 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5262 if (mAccessibilityDelegate != null) {
5263 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5264 } else {
5265 return null;
5266 }
5267 }
5268
5269 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005270 * Gets the unique identifier of this view on the screen for accessibility purposes.
5271 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5272 *
5273 * @return The view accessibility id.
5274 *
5275 * @hide
5276 */
5277 public int getAccessibilityViewId() {
5278 if (mAccessibilityViewId == NO_ID) {
5279 mAccessibilityViewId = sNextAccessibilityViewId++;
5280 }
5281 return mAccessibilityViewId;
5282 }
5283
5284 /**
5285 * Gets the unique identifier of the window in which this View reseides.
5286 *
5287 * @return The window accessibility id.
5288 *
5289 * @hide
5290 */
5291 public int getAccessibilityWindowId() {
5292 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5293 }
5294
5295 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005296 * Gets the {@link View} description. It briefly describes the view and is
5297 * primarily used for accessibility support. Set this property to enable
5298 * better accessibility support for your application. This is especially
5299 * true for views that do not have textual representation (For example,
5300 * ImageButton).
5301 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005302 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005303 *
5304 * @attr ref android.R.styleable#View_contentDescription
5305 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005306 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005307 public CharSequence getContentDescription() {
5308 return mContentDescription;
5309 }
5310
5311 /**
5312 * Sets the {@link View} description. It briefly describes the view and is
5313 * primarily used for accessibility support. Set this property to enable
5314 * better accessibility support for your application. This is especially
5315 * true for views that do not have textual representation (For example,
5316 * ImageButton).
5317 *
5318 * @param contentDescription The content description.
5319 *
5320 * @attr ref android.R.styleable#View_contentDescription
5321 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005322 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005323 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005324 if (mContentDescription == null) {
5325 if (contentDescription == null) {
5326 return;
5327 }
5328 } else if (mContentDescription.equals(contentDescription)) {
5329 return;
5330 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005331 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005332 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5333 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5334 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5335 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005336 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005337 }
5338
5339 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005340 * Gets the id of a view for which this view serves as a label for
5341 * accessibility purposes.
5342 *
5343 * @return The labeled view id.
5344 */
5345 @ViewDebug.ExportedProperty(category = "accessibility")
5346 public int getLabelFor() {
5347 return mLabelForId;
5348 }
5349
5350 /**
5351 * Sets the id of a view for which this view serves as a label for
5352 * accessibility purposes.
5353 *
5354 * @param id The labeled view id.
5355 */
5356 @RemotableViewMethod
5357 public void setLabelFor(int id) {
5358 mLabelForId = id;
5359 if (mLabelForId != View.NO_ID
5360 && mID == View.NO_ID) {
5361 mID = generateViewId();
5362 }
5363 }
5364
5365 /**
Romain Guya2431d02009-04-30 16:30:00 -07005366 * Invoked whenever this view loses focus, either by losing window focus or by losing
5367 * focus within its window. This method can be used to clear any state tied to the
5368 * focus. For instance, if a button is held pressed with the trackball and the window
5369 * loses focus, this method can be used to cancel the press.
5370 *
5371 * Subclasses of View overriding this method should always call super.onFocusLost().
5372 *
5373 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005374 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005375 *
5376 * @hide pending API council approval
5377 */
5378 protected void onFocusLost() {
5379 resetPressedState();
5380 }
5381
5382 private void resetPressedState() {
5383 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5384 return;
5385 }
5386
5387 if (isPressed()) {
5388 setPressed(false);
5389
5390 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005391 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005392 }
5393 }
5394 }
5395
5396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397 * Returns true if this view has focus
5398 *
5399 * @return True if this view has focus, false otherwise.
5400 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005401 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005403 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 }
5405
5406 /**
5407 * Find the view in the hierarchy rooted at this view that currently has
5408 * focus.
5409 *
5410 * @return The view that currently has focus, or null if no focused view can
5411 * be found.
5412 */
5413 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005414 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 }
5416
5417 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005418 * Indicates whether this view is one of the set of scrollable containers in
5419 * its window.
5420 *
5421 * @return whether this view is one of the set of scrollable containers in
5422 * its window
5423 *
5424 * @attr ref android.R.styleable#View_isScrollContainer
5425 */
5426 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005427 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005428 }
5429
5430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 * Change whether this view is one of the set of scrollable containers in
5432 * its window. This will be used to determine whether the window can
5433 * resize or must pan when a soft input area is open -- scrollable
5434 * containers allow the window to use resize mode since the container
5435 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005436 *
5437 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 */
5439 public void setScrollContainer(boolean isScrollContainer) {
5440 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005441 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005443 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005445 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005447 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 mAttachInfo.mScrollContainers.remove(this);
5449 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005450 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 }
5452 }
5453
5454 /**
5455 * Returns the quality of the drawing cache.
5456 *
5457 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5458 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5459 *
5460 * @see #setDrawingCacheQuality(int)
5461 * @see #setDrawingCacheEnabled(boolean)
5462 * @see #isDrawingCacheEnabled()
5463 *
5464 * @attr ref android.R.styleable#View_drawingCacheQuality
5465 */
5466 public int getDrawingCacheQuality() {
5467 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5468 }
5469
5470 /**
5471 * Set the drawing cache quality of this view. This value is used only when the
5472 * drawing cache is enabled
5473 *
5474 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5475 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5476 *
5477 * @see #getDrawingCacheQuality()
5478 * @see #setDrawingCacheEnabled(boolean)
5479 * @see #isDrawingCacheEnabled()
5480 *
5481 * @attr ref android.R.styleable#View_drawingCacheQuality
5482 */
5483 public void setDrawingCacheQuality(int quality) {
5484 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5485 }
5486
5487 /**
5488 * Returns whether the screen should remain on, corresponding to the current
5489 * value of {@link #KEEP_SCREEN_ON}.
5490 *
5491 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5492 *
5493 * @see #setKeepScreenOn(boolean)
5494 *
5495 * @attr ref android.R.styleable#View_keepScreenOn
5496 */
5497 public boolean getKeepScreenOn() {
5498 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5499 }
5500
5501 /**
5502 * Controls whether the screen should remain on, modifying the
5503 * value of {@link #KEEP_SCREEN_ON}.
5504 *
5505 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5506 *
5507 * @see #getKeepScreenOn()
5508 *
5509 * @attr ref android.R.styleable#View_keepScreenOn
5510 */
5511 public void setKeepScreenOn(boolean keepScreenOn) {
5512 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5513 }
5514
5515 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005516 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5517 * @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 -08005518 *
5519 * @attr ref android.R.styleable#View_nextFocusLeft
5520 */
5521 public int getNextFocusLeftId() {
5522 return mNextFocusLeftId;
5523 }
5524
5525 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005526 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5527 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5528 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 *
5530 * @attr ref android.R.styleable#View_nextFocusLeft
5531 */
5532 public void setNextFocusLeftId(int nextFocusLeftId) {
5533 mNextFocusLeftId = nextFocusLeftId;
5534 }
5535
5536 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005537 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5538 * @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 -08005539 *
5540 * @attr ref android.R.styleable#View_nextFocusRight
5541 */
5542 public int getNextFocusRightId() {
5543 return mNextFocusRightId;
5544 }
5545
5546 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005547 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5548 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5549 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 *
5551 * @attr ref android.R.styleable#View_nextFocusRight
5552 */
5553 public void setNextFocusRightId(int nextFocusRightId) {
5554 mNextFocusRightId = nextFocusRightId;
5555 }
5556
5557 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005558 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5559 * @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 -08005560 *
5561 * @attr ref android.R.styleable#View_nextFocusUp
5562 */
5563 public int getNextFocusUpId() {
5564 return mNextFocusUpId;
5565 }
5566
5567 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005568 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5569 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5570 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 *
5572 * @attr ref android.R.styleable#View_nextFocusUp
5573 */
5574 public void setNextFocusUpId(int nextFocusUpId) {
5575 mNextFocusUpId = nextFocusUpId;
5576 }
5577
5578 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005579 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5580 * @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 -08005581 *
5582 * @attr ref android.R.styleable#View_nextFocusDown
5583 */
5584 public int getNextFocusDownId() {
5585 return mNextFocusDownId;
5586 }
5587
5588 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005589 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5590 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5591 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 *
5593 * @attr ref android.R.styleable#View_nextFocusDown
5594 */
5595 public void setNextFocusDownId(int nextFocusDownId) {
5596 mNextFocusDownId = nextFocusDownId;
5597 }
5598
5599 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005600 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5601 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5602 *
5603 * @attr ref android.R.styleable#View_nextFocusForward
5604 */
5605 public int getNextFocusForwardId() {
5606 return mNextFocusForwardId;
5607 }
5608
5609 /**
5610 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5611 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5612 * decide automatically.
5613 *
5614 * @attr ref android.R.styleable#View_nextFocusForward
5615 */
5616 public void setNextFocusForwardId(int nextFocusForwardId) {
5617 mNextFocusForwardId = nextFocusForwardId;
5618 }
5619
5620 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 * Returns the visibility of this view and all of its ancestors
5622 *
5623 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5624 */
5625 public boolean isShown() {
5626 View current = this;
5627 //noinspection ConstantConditions
5628 do {
5629 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5630 return false;
5631 }
5632 ViewParent parent = current.mParent;
5633 if (parent == null) {
5634 return false; // We are not attached to the view root
5635 }
5636 if (!(parent instanceof View)) {
5637 return true;
5638 }
5639 current = (View) parent;
5640 } while (current != null);
5641
5642 return false;
5643 }
5644
5645 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005646 * Called by the view hierarchy when the content insets for a window have
5647 * changed, to allow it to adjust its content to fit within those windows.
5648 * The content insets tell you the space that the status bar, input method,
5649 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005651 * <p>You do not normally need to deal with this function, since the default
5652 * window decoration given to applications takes care of applying it to the
5653 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5654 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5655 * and your content can be placed under those system elements. You can then
5656 * use this method within your view hierarchy if you have parts of your UI
5657 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005659 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005660 * inset's to the view's padding, consuming that content (modifying the
5661 * insets to be 0), and returning true. This behavior is off by default, but can
5662 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5663 *
5664 * <p>This function's traversal down the hierarchy is depth-first. The same content
5665 * insets object is propagated down the hierarchy, so any changes made to it will
5666 * be seen by all following views (including potentially ones above in
5667 * the hierarchy since this is a depth-first traversal). The first view
5668 * that returns true will abort the entire traversal.
5669 *
5670 * <p>The default implementation works well for a situation where it is
5671 * used with a container that covers the entire window, allowing it to
5672 * apply the appropriate insets to its content on all edges. If you need
5673 * a more complicated layout (such as two different views fitting system
5674 * windows, one on the top of the window, and one on the bottom),
5675 * you can override the method and handle the insets however you would like.
5676 * Note that the insets provided by the framework are always relative to the
5677 * far edges of the window, not accounting for the location of the called view
5678 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005679 * where the layout will place the view, as it is done before layout happens.)
5680 *
5681 * <p>Note: unlike many View methods, there is no dispatch phase to this
5682 * call. If you are overriding it in a ViewGroup and want to allow the
5683 * call to continue to your children, you must be sure to call the super
5684 * implementation.
5685 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005686 * <p>Here is a sample layout that makes use of fitting system windows
5687 * to have controls for a video view placed inside of the window decorations
5688 * that it hides and shows. This can be used with code like the second
5689 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5690 *
5691 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5692 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005693 * @param insets Current content insets of the window. Prior to
5694 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5695 * the insets or else you and Android will be unhappy.
5696 *
5697 * @return Return true if this view applied the insets and it should not
5698 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005699 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005700 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005701 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 */
5703 protected boolean fitSystemWindows(Rect insets) {
5704 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005705 mUserPaddingStart = UNDEFINED_PADDING;
5706 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005707 Rect localInsets = sThreadLocal.get();
5708 if (localInsets == null) {
5709 localInsets = new Rect();
5710 sThreadLocal.set(localInsets);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005711 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005712 boolean res = computeFitSystemWindows(insets, localInsets);
5713 internalSetPadding(localInsets.left, localInsets.top,
5714 localInsets.right, localInsets.bottom);
5715 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 }
5717 return false;
5718 }
5719
5720 /**
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005721 * @hide Compute the insets that should be consumed by this view and the ones
5722 * that should propagate to those under it.
5723 */
5724 protected boolean computeFitSystemWindows(Rect inoutInsets, Rect outLocalInsets) {
5725 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5726 || mAttachInfo == null
5727 || ((mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0
5728 && !mAttachInfo.mOverscanRequested)) {
5729 outLocalInsets.set(inoutInsets);
5730 inoutInsets.set(0, 0, 0, 0);
5731 return true;
5732 } else {
5733 // The application wants to take care of fitting system window for
5734 // the content... however we still need to take care of any overscan here.
5735 final Rect overscan = mAttachInfo.mOverscanInsets;
5736 outLocalInsets.set(overscan);
5737 inoutInsets.left -= overscan.left;
5738 inoutInsets.top -= overscan.top;
5739 inoutInsets.right -= overscan.right;
5740 inoutInsets.bottom -= overscan.bottom;
5741 return false;
5742 }
5743 }
5744
5745 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005746 * Sets whether or not this view should account for system screen decorations
5747 * such as the status bar and inset its content; that is, controlling whether
5748 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5749 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005750 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005751 * <p>Note that if you are providing your own implementation of
5752 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5753 * flag to true -- your implementation will be overriding the default
5754 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005755 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005756 * @param fitSystemWindows If true, then the default implementation of
5757 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005758 *
5759 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005760 * @see #getFitsSystemWindows()
5761 * @see #fitSystemWindows(Rect)
5762 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005763 */
5764 public void setFitsSystemWindows(boolean fitSystemWindows) {
5765 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5766 }
5767
5768 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005769 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005770 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5771 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005772 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005773 * @return Returns true if the default implementation of
5774 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005775 *
5776 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005777 * @see #setFitsSystemWindows()
5778 * @see #fitSystemWindows(Rect)
5779 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005780 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005781 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005782 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5783 }
5784
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005785 /** @hide */
5786 public boolean fitsSystemWindows() {
5787 return getFitsSystemWindows();
5788 }
5789
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005790 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005791 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5792 */
5793 public void requestFitSystemWindows() {
5794 if (mParent != null) {
5795 mParent.requestFitSystemWindows();
5796 }
5797 }
5798
5799 /**
5800 * For use by PhoneWindow to make its own system window fitting optional.
5801 * @hide
5802 */
5803 public void makeOptionalFitsSystemWindows() {
5804 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5805 }
5806
5807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 * Returns the visibility status for this view.
5809 *
5810 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5811 * @attr ref android.R.styleable#View_visibility
5812 */
5813 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005814 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5815 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5816 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 })
5818 public int getVisibility() {
5819 return mViewFlags & VISIBILITY_MASK;
5820 }
5821
5822 /**
5823 * Set the enabled state of this view.
5824 *
5825 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5826 * @attr ref android.R.styleable#View_visibility
5827 */
5828 @RemotableViewMethod
5829 public void setVisibility(int visibility) {
5830 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005831 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 }
5833
5834 /**
5835 * Returns the enabled status for this view. The interpretation of the
5836 * enabled state varies by subclass.
5837 *
5838 * @return True if this view is enabled, false otherwise.
5839 */
5840 @ViewDebug.ExportedProperty
5841 public boolean isEnabled() {
5842 return (mViewFlags & ENABLED_MASK) == ENABLED;
5843 }
5844
5845 /**
5846 * Set the enabled state of this view. The interpretation of the enabled
5847 * state varies by subclass.
5848 *
5849 * @param enabled True if this view is enabled, false otherwise.
5850 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005851 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005853 if (enabled == isEnabled()) return;
5854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5856
5857 /*
5858 * The View most likely has to change its appearance, so refresh
5859 * the drawable state.
5860 */
5861 refreshDrawableState();
5862
5863 // Invalidate too, since the default behavior for views is to be
5864 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005865 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 }
5867
5868 /**
5869 * Set whether this view can receive the focus.
5870 *
5871 * Setting this to false will also ensure that this view is not focusable
5872 * in touch mode.
5873 *
5874 * @param focusable If true, this view can receive the focus.
5875 *
5876 * @see #setFocusableInTouchMode(boolean)
5877 * @attr ref android.R.styleable#View_focusable
5878 */
5879 public void setFocusable(boolean focusable) {
5880 if (!focusable) {
5881 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5882 }
5883 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5884 }
5885
5886 /**
5887 * Set whether this view can receive focus while in touch mode.
5888 *
5889 * Setting this to true will also ensure that this view is focusable.
5890 *
5891 * @param focusableInTouchMode If true, this view can receive the focus while
5892 * in touch mode.
5893 *
5894 * @see #setFocusable(boolean)
5895 * @attr ref android.R.styleable#View_focusableInTouchMode
5896 */
5897 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5898 // Focusable in touch mode should always be set before the focusable flag
5899 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5900 // which, in touch mode, will not successfully request focus on this view
5901 // because the focusable in touch mode flag is not set
5902 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5903 if (focusableInTouchMode) {
5904 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5905 }
5906 }
5907
5908 /**
5909 * Set whether this view should have sound effects enabled for events such as
5910 * clicking and touching.
5911 *
5912 * <p>You may wish to disable sound effects for a view if you already play sounds,
5913 * for instance, a dial key that plays dtmf tones.
5914 *
5915 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5916 * @see #isSoundEffectsEnabled()
5917 * @see #playSoundEffect(int)
5918 * @attr ref android.R.styleable#View_soundEffectsEnabled
5919 */
5920 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5921 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5922 }
5923
5924 /**
5925 * @return whether this view should have sound effects enabled for events such as
5926 * clicking and touching.
5927 *
5928 * @see #setSoundEffectsEnabled(boolean)
5929 * @see #playSoundEffect(int)
5930 * @attr ref android.R.styleable#View_soundEffectsEnabled
5931 */
5932 @ViewDebug.ExportedProperty
5933 public boolean isSoundEffectsEnabled() {
5934 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5935 }
5936
5937 /**
5938 * Set whether this view should have haptic feedback for events such as
5939 * long presses.
5940 *
5941 * <p>You may wish to disable haptic feedback if your view already controls
5942 * its own haptic feedback.
5943 *
5944 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5945 * @see #isHapticFeedbackEnabled()
5946 * @see #performHapticFeedback(int)
5947 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5948 */
5949 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5950 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5951 }
5952
5953 /**
5954 * @return whether this view should have haptic feedback enabled for events
5955 * long presses.
5956 *
5957 * @see #setHapticFeedbackEnabled(boolean)
5958 * @see #performHapticFeedback(int)
5959 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5960 */
5961 @ViewDebug.ExportedProperty
5962 public boolean isHapticFeedbackEnabled() {
5963 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5964 }
5965
5966 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005967 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005968 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005969 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5970 * {@link #LAYOUT_DIRECTION_RTL},
5971 * {@link #LAYOUT_DIRECTION_INHERIT} or
5972 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005973 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005974 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005975 *
5976 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005977 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005978 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005979 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5980 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5981 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5982 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005983 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005984 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005985 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005986 }
5987
5988 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005989 * Set the layout direction for this view. This will propagate a reset of layout direction
5990 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005991 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005992 * @param layoutDirection the layout direction to set. Should be one of:
5993 *
5994 * {@link #LAYOUT_DIRECTION_LTR},
5995 * {@link #LAYOUT_DIRECTION_RTL},
5996 * {@link #LAYOUT_DIRECTION_INHERIT},
5997 * {@link #LAYOUT_DIRECTION_LOCALE}.
5998 *
5999 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
6000 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
6001 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006002 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006003 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08006004 */
6005 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006006 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006007 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07006008 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07006009 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07006010 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07006011 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07006012 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07006013 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006014 // We need to resolve all RTL properties as they all depend on layout direction
6015 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07006016 requestLayout();
6017 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07006018 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006019 }
6020
6021 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006022 * Returns the resolved layout direction for this view.
6023 *
6024 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07006025 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006026 *
6027 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
6028 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08006029 *
6030 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006031 */
6032 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07006033 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
6034 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006035 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006036 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07006037 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
6038 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006039 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08006040 return LAYOUT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07006041 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07006042 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
6043 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006044 }
6045
6046 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07006047 * Indicates whether or not this view's layout is right-to-left. This is resolved from
6048 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006049 *
6050 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07006051 *
6052 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006053 */
6054 @ViewDebug.ExportedProperty(category = "layout")
6055 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07006056 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07006057 }
6058
6059 /**
Adam Powell539ee872012-02-03 19:00:49 -08006060 * Indicates whether the view is currently tracking transient state that the
6061 * app should not need to concern itself with saving and restoring, but that
6062 * the framework should take special note to preserve when possible.
6063 *
Adam Powell785c4472012-05-02 21:25:39 -07006064 * <p>A view with transient state cannot be trivially rebound from an external
6065 * data source, such as an adapter binding item views in a list. This may be
6066 * because the view is performing an animation, tracking user selection
6067 * of content, or similar.</p>
6068 *
Adam Powell539ee872012-02-03 19:00:49 -08006069 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08006070 */
6071 @ViewDebug.ExportedProperty(category = "layout")
6072 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006073 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08006074 }
6075
6076 /**
6077 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07006078 * framework should attempt to preserve when possible. This flag is reference counted,
6079 * so every call to setHasTransientState(true) should be paired with a later call
6080 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08006081 *
Adam Powell785c4472012-05-02 21:25:39 -07006082 * <p>A view with transient state cannot be trivially rebound from an external
6083 * data source, such as an adapter binding item views in a list. This may be
6084 * because the view is performing an animation, tracking user selection
6085 * of content, or similar.</p>
6086 *
Adam Powell539ee872012-02-03 19:00:49 -08006087 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08006088 */
6089 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07006090 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
6091 mTransientStateCount - 1;
6092 if (mTransientStateCount < 0) {
6093 mTransientStateCount = 0;
6094 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
6095 "unmatched pair of setHasTransientState calls");
Chet Haase72871322013-02-26 16:12:13 -07006096 } else if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07006097 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07006098 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07006099 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
6100 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07006101 if (mParent != null) {
6102 try {
6103 mParent.childHasTransientStateChanged(this, hasTransientState);
6104 } catch (AbstractMethodError e) {
6105 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
6106 " does not fully implement ViewParent", e);
6107 }
Adam Powell539ee872012-02-03 19:00:49 -08006108 }
6109 }
6110 }
6111
6112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 * If this view doesn't do any drawing on its own, set this flag to
6114 * allow further optimizations. By default, this flag is not set on
6115 * View, but could be set on some View subclasses such as ViewGroup.
6116 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006117 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
6118 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 *
6120 * @param willNotDraw whether or not this View draw on its own
6121 */
6122 public void setWillNotDraw(boolean willNotDraw) {
6123 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
6124 }
6125
6126 /**
6127 * Returns whether or not this View draws on its own.
6128 *
6129 * @return true if this view has nothing to draw, false otherwise
6130 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006131 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 public boolean willNotDraw() {
6133 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6134 }
6135
6136 /**
6137 * When a View's drawing cache is enabled, drawing is redirected to an
6138 * offscreen bitmap. Some views, like an ImageView, must be able to
6139 * bypass this mechanism if they already draw a single bitmap, to avoid
6140 * unnecessary usage of the memory.
6141 *
6142 * @param willNotCacheDrawing true if this view does not cache its
6143 * drawing, false otherwise
6144 */
6145 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6146 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6147 }
6148
6149 /**
6150 * Returns whether or not this View can cache its drawing or not.
6151 *
6152 * @return true if this view does not cache its drawing, false otherwise
6153 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006154 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 public boolean willNotCacheDrawing() {
6156 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6157 }
6158
6159 /**
6160 * Indicates whether this view reacts to click events or not.
6161 *
6162 * @return true if the view is clickable, false otherwise
6163 *
6164 * @see #setClickable(boolean)
6165 * @attr ref android.R.styleable#View_clickable
6166 */
6167 @ViewDebug.ExportedProperty
6168 public boolean isClickable() {
6169 return (mViewFlags & CLICKABLE) == CLICKABLE;
6170 }
6171
6172 /**
6173 * Enables or disables click events for this view. When a view
6174 * is clickable it will change its state to "pressed" on every click.
6175 * Subclasses should set the view clickable to visually react to
6176 * user's clicks.
6177 *
6178 * @param clickable true to make the view clickable, false otherwise
6179 *
6180 * @see #isClickable()
6181 * @attr ref android.R.styleable#View_clickable
6182 */
6183 public void setClickable(boolean clickable) {
6184 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6185 }
6186
6187 /**
6188 * Indicates whether this view reacts to long click events or not.
6189 *
6190 * @return true if the view is long clickable, false otherwise
6191 *
6192 * @see #setLongClickable(boolean)
6193 * @attr ref android.R.styleable#View_longClickable
6194 */
6195 public boolean isLongClickable() {
6196 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6197 }
6198
6199 /**
6200 * Enables or disables long click events for this view. When a view is long
6201 * clickable it reacts to the user holding down the button for a longer
6202 * duration than a tap. This event can either launch the listener or a
6203 * context menu.
6204 *
6205 * @param longClickable true to make the view long clickable, false otherwise
6206 * @see #isLongClickable()
6207 * @attr ref android.R.styleable#View_longClickable
6208 */
6209 public void setLongClickable(boolean longClickable) {
6210 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6211 }
6212
6213 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006214 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 *
6216 * @see #isClickable()
6217 * @see #setClickable(boolean)
6218 *
6219 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6220 * the View's internal state from a previously set "pressed" state.
6221 */
6222 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006223 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006226 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006228 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006230
6231 if (needsRefresh) {
6232 refreshDrawableState();
6233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 dispatchSetPressed(pressed);
6235 }
6236
6237 /**
6238 * Dispatch setPressed to all of this View's children.
6239 *
6240 * @see #setPressed(boolean)
6241 *
6242 * @param pressed The new pressed state
6243 */
6244 protected void dispatchSetPressed(boolean pressed) {
6245 }
6246
6247 /**
6248 * Indicates whether the view is currently in pressed state. Unless
6249 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6250 * the pressed state.
6251 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006252 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 * @see #isClickable()
6254 * @see #setClickable(boolean)
6255 *
6256 * @return true if the view is currently pressed, false otherwise
6257 */
6258 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006259 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 }
6261
6262 /**
6263 * Indicates whether this view will save its state (that is,
6264 * whether its {@link #onSaveInstanceState} method will be called).
6265 *
6266 * @return Returns true if the view state saving is enabled, else false.
6267 *
6268 * @see #setSaveEnabled(boolean)
6269 * @attr ref android.R.styleable#View_saveEnabled
6270 */
6271 public boolean isSaveEnabled() {
6272 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6273 }
6274
6275 /**
6276 * Controls whether the saving of this view's state is
6277 * enabled (that is, whether its {@link #onSaveInstanceState} method
6278 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006279 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 * for its state to be saved. This flag can only disable the
6281 * saving of this view; any child views may still have their state saved.
6282 *
6283 * @param enabled Set to false to <em>disable</em> state saving, or true
6284 * (the default) to allow it.
6285 *
6286 * @see #isSaveEnabled()
6287 * @see #setId(int)
6288 * @see #onSaveInstanceState()
6289 * @attr ref android.R.styleable#View_saveEnabled
6290 */
6291 public void setSaveEnabled(boolean enabled) {
6292 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6293 }
6294
Jeff Brown85a31762010-09-01 17:01:00 -07006295 /**
6296 * Gets whether the framework should discard touches when the view's
6297 * window is obscured by another visible window.
6298 * Refer to the {@link View} security documentation for more details.
6299 *
6300 * @return True if touch filtering is enabled.
6301 *
6302 * @see #setFilterTouchesWhenObscured(boolean)
6303 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6304 */
6305 @ViewDebug.ExportedProperty
6306 public boolean getFilterTouchesWhenObscured() {
6307 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6308 }
6309
6310 /**
6311 * Sets whether the framework should discard touches when the view's
6312 * window is obscured by another visible window.
6313 * Refer to the {@link View} security documentation for more details.
6314 *
6315 * @param enabled True if touch filtering should be enabled.
6316 *
6317 * @see #getFilterTouchesWhenObscured
6318 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6319 */
6320 public void setFilterTouchesWhenObscured(boolean enabled) {
6321 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6322 FILTER_TOUCHES_WHEN_OBSCURED);
6323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324
6325 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006326 * Indicates whether the entire hierarchy under this view will save its
6327 * state when a state saving traversal occurs from its parent. The default
6328 * is true; if false, these views will not be saved unless
6329 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6330 *
6331 * @return Returns true if the view state saving from parent is enabled, else false.
6332 *
6333 * @see #setSaveFromParentEnabled(boolean)
6334 */
6335 public boolean isSaveFromParentEnabled() {
6336 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6337 }
6338
6339 /**
6340 * Controls whether the entire hierarchy under this view will save its
6341 * state when a state saving traversal occurs from its parent. The default
6342 * is true; if false, these views will not be saved unless
6343 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6344 *
6345 * @param enabled Set to false to <em>disable</em> state saving, or true
6346 * (the default) to allow it.
6347 *
6348 * @see #isSaveFromParentEnabled()
6349 * @see #setId(int)
6350 * @see #onSaveInstanceState()
6351 */
6352 public void setSaveFromParentEnabled(boolean enabled) {
6353 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6354 }
6355
6356
6357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 * Returns whether this View is able to take focus.
6359 *
6360 * @return True if this view can take focus, or false otherwise.
6361 * @attr ref android.R.styleable#View_focusable
6362 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006363 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006364 public final boolean isFocusable() {
6365 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6366 }
6367
6368 /**
6369 * When a view is focusable, it may not want to take focus when in touch mode.
6370 * For example, a button would like focus when the user is navigating via a D-pad
6371 * so that the user can click on it, but once the user starts touching the screen,
6372 * the button shouldn't take focus
6373 * @return Whether the view is focusable in touch mode.
6374 * @attr ref android.R.styleable#View_focusableInTouchMode
6375 */
6376 @ViewDebug.ExportedProperty
6377 public final boolean isFocusableInTouchMode() {
6378 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6379 }
6380
6381 /**
6382 * Find the nearest view in the specified direction that can take focus.
6383 * This does not actually give focus to that view.
6384 *
6385 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6386 *
6387 * @return The nearest focusable in the specified direction, or null if none
6388 * can be found.
6389 */
6390 public View focusSearch(int direction) {
6391 if (mParent != null) {
6392 return mParent.focusSearch(this, direction);
6393 } else {
6394 return null;
6395 }
6396 }
6397
6398 /**
6399 * This method is the last chance for the focused view and its ancestors to
6400 * respond to an arrow key. This is called when the focused view did not
6401 * consume the key internally, nor could the view system find a new view in
6402 * the requested direction to give focus to.
6403 *
6404 * @param focused The currently focused view.
6405 * @param direction The direction focus wants to move. One of FOCUS_UP,
6406 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6407 * @return True if the this view consumed this unhandled move.
6408 */
6409 public boolean dispatchUnhandledMove(View focused, int direction) {
6410 return false;
6411 }
6412
6413 /**
6414 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006415 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006417 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6418 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 * @return The user specified next view, or null if there is none.
6420 */
6421 View findUserSetNextFocus(View root, int direction) {
6422 switch (direction) {
6423 case FOCUS_LEFT:
6424 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006425 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 case FOCUS_RIGHT:
6427 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006428 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 case FOCUS_UP:
6430 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006431 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 case FOCUS_DOWN:
6433 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006434 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006435 case FOCUS_FORWARD:
6436 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006437 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006438 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006439 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006440 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006441 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006442 @Override
6443 public boolean apply(View t) {
6444 return t.mNextFocusForwardId == id;
6445 }
6446 });
6447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 }
6449 return null;
6450 }
6451
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006452 private View findViewInsideOutShouldExist(View root, int id) {
6453 if (mMatchIdPredicate == null) {
6454 mMatchIdPredicate = new MatchIdPredicate();
6455 }
6456 mMatchIdPredicate.mId = id;
6457 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006459 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460 }
6461 return result;
6462 }
6463
6464 /**
6465 * Find and return all focusable views that are descendants of this view,
6466 * possibly including this view if it is focusable itself.
6467 *
6468 * @param direction The direction of the focus
6469 * @return A list of focusable views
6470 */
6471 public ArrayList<View> getFocusables(int direction) {
6472 ArrayList<View> result = new ArrayList<View>(24);
6473 addFocusables(result, direction);
6474 return result;
6475 }
6476
6477 /**
6478 * Add any focusable views that are descendants of this view (possibly
6479 * including this view if it is focusable itself) to views. If we are in touch mode,
6480 * only add views that are also focusable in touch mode.
6481 *
6482 * @param views Focusable views found so far
6483 * @param direction The direction of the focus
6484 */
6485 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006486 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488
svetoslavganov75986cf2009-05-14 22:28:01 -07006489 /**
6490 * Adds any focusable views that are descendants of this view (possibly
6491 * including this view if it is focusable itself) to views. This method
6492 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006493 * only views focusable in touch mode if we are in touch mode or
6494 * only views that can take accessibility focus if accessibility is enabeld
6495 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006496 *
6497 * @param views Focusable views found so far or null if all we are interested is
6498 * the number of focusables.
6499 * @param direction The direction of the focus.
6500 * @param focusableMode The type of focusables to be added.
6501 *
6502 * @see #FOCUSABLES_ALL
6503 * @see #FOCUSABLES_TOUCH_MODE
6504 */
6505 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006506 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006507 return;
6508 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006509 if (!isFocusable()) {
6510 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006511 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006512 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6513 && isInTouchMode() && !isFocusableInTouchMode()) {
6514 return;
6515 }
6516 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 }
6518
6519 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006520 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006521 * The search is performed by either the text that the View renders or the content
6522 * description that describes the view for accessibility purposes and the view does
6523 * not render or both. Clients can specify how the search is to be performed via
6524 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6525 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006526 *
6527 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006528 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006529 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006530 * @see #FIND_VIEWS_WITH_TEXT
6531 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6532 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006533 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006534 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006535 if (getAccessibilityNodeProvider() != null) {
6536 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6537 outViews.add(this);
6538 }
6539 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006540 && (searched != null && searched.length() > 0)
6541 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006542 String searchedLowerCase = searched.toString().toLowerCase();
6543 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6544 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6545 outViews.add(this);
6546 }
6547 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006548 }
6549
6550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 * Find and return all touchable views that are descendants of this view,
6552 * possibly including this view if it is touchable itself.
6553 *
6554 * @return A list of touchable views
6555 */
6556 public ArrayList<View> getTouchables() {
6557 ArrayList<View> result = new ArrayList<View>();
6558 addTouchables(result);
6559 return result;
6560 }
6561
6562 /**
6563 * Add any touchable views that are descendants of this view (possibly
6564 * including this view if it is touchable itself) to views.
6565 *
6566 * @param views Touchable views found so far
6567 */
6568 public void addTouchables(ArrayList<View> views) {
6569 final int viewFlags = mViewFlags;
6570
6571 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6572 && (viewFlags & ENABLED_MASK) == ENABLED) {
6573 views.add(this);
6574 }
6575 }
6576
6577 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006578 * Returns whether this View is accessibility focused.
6579 *
6580 * @return True if this View is accessibility focused.
6581 */
6582 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006583 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006584 }
6585
6586 /**
6587 * Call this to try to give accessibility focus to this view.
6588 *
6589 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6590 * returns false or the view is no visible or the view already has accessibility
6591 * focus.
6592 *
6593 * See also {@link #focusSearch(int)}, which is what you call to say that you
6594 * have focus, and you want your parent to look for the next one.
6595 *
6596 * @return Whether this view actually took accessibility focus.
6597 *
6598 * @hide
6599 */
6600 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006601 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6602 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006603 return false;
6604 }
6605 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6606 return false;
6607 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006608 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6609 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006610 ViewRootImpl viewRootImpl = getViewRootImpl();
6611 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006612 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006613 }
6614 invalidate();
6615 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6616 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006617 return true;
6618 }
6619 return false;
6620 }
6621
6622 /**
6623 * Call this to try to clear accessibility focus of this view.
6624 *
6625 * See also {@link #focusSearch(int)}, which is what you call to say that you
6626 * have focus, and you want your parent to look for the next one.
6627 *
6628 * @hide
6629 */
6630 public void clearAccessibilityFocus() {
Svetoslav4293e232013-04-25 13:25:06 -07006631 clearAccessibilityFocusNoCallbacks();
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006632 // Clear the global reference of accessibility focus if this
6633 // view or any of its descendants had accessibility focus.
6634 ViewRootImpl viewRootImpl = getViewRootImpl();
6635 if (viewRootImpl != null) {
6636 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6637 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006638 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006639 }
6640 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006641 }
6642
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006643 private void sendAccessibilityHoverEvent(int eventType) {
6644 // Since we are not delivering to a client accessibility events from not
6645 // important views (unless the clinet request that) we need to fire the
6646 // event from the deepest view exposed to the client. As a consequence if
6647 // the user crosses a not exposed view the client will see enter and exit
6648 // of the exposed predecessor followed by and enter and exit of that same
6649 // predecessor when entering and exiting the not exposed descendant. This
6650 // is fine since the client has a clear idea which view is hovered at the
6651 // price of a couple more events being sent. This is a simple and
6652 // working solution.
6653 View source = this;
6654 while (true) {
6655 if (source.includeForAccessibility()) {
6656 source.sendAccessibilityEvent(eventType);
6657 return;
6658 }
6659 ViewParent parent = source.getParent();
6660 if (parent instanceof View) {
6661 source = (View) parent;
6662 } else {
6663 return;
6664 }
6665 }
6666 }
6667
Svetoslav Ganov42138042012-03-20 11:51:39 -07006668 /**
6669 * Clears accessibility focus without calling any callback methods
6670 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6671 * is used for clearing accessibility focus when giving this focus to
6672 * another view.
6673 */
6674 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006675 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6676 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006677 invalidate();
Svetoslav4293e232013-04-25 13:25:06 -07006678 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6679 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006680 }
6681 }
6682
6683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 * Call this to try to give focus to a specific view or to one of its
6685 * descendants.
6686 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006687 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6688 * false), or if it is focusable and it is not focusable in touch mode
6689 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006691 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 * have focus, and you want your parent to look for the next one.
6693 *
6694 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6695 * {@link #FOCUS_DOWN} and <code>null</code>.
6696 *
6697 * @return Whether this view or one of its descendants actually took focus.
6698 */
6699 public final boolean requestFocus() {
6700 return requestFocus(View.FOCUS_DOWN);
6701 }
6702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 /**
6704 * Call this to try to give focus to a specific view or to one of its
6705 * descendants and give it a hint about what direction focus is heading.
6706 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006707 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6708 * false), or if it is focusable and it is not focusable in touch mode
6709 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006710 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006711 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 * have focus, and you want your parent to look for the next one.
6713 *
6714 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6715 * <code>null</code> set for the previously focused rectangle.
6716 *
6717 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6718 * @return Whether this view or one of its descendants actually took focus.
6719 */
6720 public final boolean requestFocus(int direction) {
6721 return requestFocus(direction, null);
6722 }
6723
6724 /**
6725 * Call this to try to give focus to a specific view or to one of its descendants
6726 * and give it hints about the direction and a specific rectangle that the focus
6727 * is coming from. The rectangle can help give larger views a finer grained hint
6728 * about where focus is coming from, and therefore, where to show selection, or
6729 * forward focus change internally.
6730 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006731 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6732 * false), or if it is focusable and it is not focusable in touch mode
6733 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 *
6735 * A View will not take focus if it is not visible.
6736 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006737 * A View will not take focus if one of its parents has
6738 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6739 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006740 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006741 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 * have focus, and you want your parent to look for the next one.
6743 *
6744 * You may wish to override this method if your custom {@link View} has an internal
6745 * {@link View} that it wishes to forward the request to.
6746 *
6747 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6748 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6749 * to give a finer grained hint about where focus is coming from. May be null
6750 * if there is no hint.
6751 * @return Whether this view or one of its descendants actually took focus.
6752 */
6753 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006754 return requestFocusNoSearch(direction, previouslyFocusedRect);
6755 }
6756
6757 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 // need to be focusable
6759 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6760 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6761 return false;
6762 }
6763
6764 // need to be focusable in touch mode if in touch mode
6765 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006766 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6767 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 }
6769
6770 // need to not have any parents blocking us
6771 if (hasAncestorThatBlocksDescendantFocus()) {
6772 return false;
6773 }
6774
6775 handleFocusGainInternal(direction, previouslyFocusedRect);
6776 return true;
6777 }
6778
6779 /**
6780 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6781 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6782 * touch mode to request focus when they are touched.
6783 *
6784 * @return Whether this view or one of its descendants actually took focus.
6785 *
6786 * @see #isInTouchMode()
6787 *
6788 */
6789 public final boolean requestFocusFromTouch() {
6790 // Leave touch mode if we need to
6791 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006792 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006793 if (viewRoot != null) {
6794 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 }
6796 }
6797 return requestFocus(View.FOCUS_DOWN);
6798 }
6799
6800 /**
6801 * @return Whether any ancestor of this view blocks descendant focus.
6802 */
6803 private boolean hasAncestorThatBlocksDescendantFocus() {
6804 ViewParent ancestor = mParent;
6805 while (ancestor instanceof ViewGroup) {
6806 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6807 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6808 return true;
6809 } else {
6810 ancestor = vgAncestor.getParent();
6811 }
6812 }
6813 return false;
6814 }
6815
6816 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006817 * Gets the mode for determining whether this View is important for accessibility
6818 * which is if it fires accessibility events and if it is reported to
6819 * accessibility services that query the screen.
6820 *
6821 * @return The mode for determining whether a View is important for accessibility.
6822 *
6823 * @attr ref android.R.styleable#View_importantForAccessibility
6824 *
6825 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6826 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6827 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6828 */
6829 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006830 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6831 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6832 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006833 })
6834 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006835 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6836 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006837 }
6838
6839 /**
6840 * Sets how to determine whether this view is important for accessibility
6841 * which is if it fires accessibility events and if it is reported to
6842 * accessibility services that query the screen.
6843 *
6844 * @param mode How to determine whether this view is important for accessibility.
6845 *
6846 * @attr ref android.R.styleable#View_importantForAccessibility
6847 *
6848 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6849 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6850 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6851 */
6852 public void setImportantForAccessibility(int mode) {
6853 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006854 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6855 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6856 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006857 notifyAccessibilityStateChanged();
6858 }
6859 }
6860
6861 /**
6862 * Gets whether this view should be exposed for accessibility.
6863 *
6864 * @return Whether the view is exposed for accessibility.
6865 *
6866 * @hide
6867 */
6868 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006869 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6870 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006871 switch (mode) {
6872 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6873 return true;
6874 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6875 return false;
6876 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006877 return isActionableForAccessibility() || hasListenersForAccessibility()
6878 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006879 default:
6880 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6881 + mode);
6882 }
6883 }
6884
6885 /**
6886 * Gets the parent for accessibility purposes. Note that the parent for
6887 * accessibility is not necessary the immediate parent. It is the first
6888 * predecessor that is important for accessibility.
6889 *
6890 * @return The parent for accessibility purposes.
6891 */
6892 public ViewParent getParentForAccessibility() {
6893 if (mParent instanceof View) {
6894 View parentView = (View) mParent;
6895 if (parentView.includeForAccessibility()) {
6896 return mParent;
6897 } else {
6898 return mParent.getParentForAccessibility();
6899 }
6900 }
6901 return null;
6902 }
6903
6904 /**
6905 * Adds the children of a given View for accessibility. Since some Views are
6906 * not important for accessibility the children for accessibility are not
Scott Kennedya8396682013-01-16 08:39:47 -08006907 * necessarily direct children of the view, rather they are the first level of
Svetoslav Ganov42138042012-03-20 11:51:39 -07006908 * descendants important for accessibility.
6909 *
6910 * @param children The list of children for accessibility.
6911 */
6912 public void addChildrenForAccessibility(ArrayList<View> children) {
6913 if (includeForAccessibility()) {
6914 children.add(this);
6915 }
6916 }
6917
6918 /**
6919 * Whether to regard this view for accessibility. A view is regarded for
6920 * accessibility if it is important for accessibility or the querying
6921 * accessibility service has explicitly requested that view not
6922 * important for accessibility are regarded.
6923 *
6924 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006925 *
6926 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006927 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006928 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006929 if (mAttachInfo != null) {
Svetoslav Ganov80943d82013-01-02 10:25:37 -08006930 return (mAttachInfo.mAccessibilityFetchFlags
6931 & AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0
6932 || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006933 }
6934 return false;
6935 }
6936
6937 /**
6938 * Returns whether the View is considered actionable from
6939 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006940 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006941 *
6942 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006943 *
6944 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006945 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006946 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006947 return (isClickable() || isLongClickable() || isFocusable());
6948 }
6949
6950 /**
6951 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006952 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006953 *
6954 * @return True if the view is actionable for accessibility.
6955 */
6956 private boolean hasListenersForAccessibility() {
6957 ListenerInfo info = getListenerInfo();
6958 return mTouchDelegate != null || info.mOnKeyListener != null
6959 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6960 || info.mOnHoverListener != null || info.mOnDragListener != null;
6961 }
6962
6963 /**
6964 * Notifies accessibility services that some view's important for
6965 * accessibility state has changed. Note that such notifications
6966 * are made at most once every
6967 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6968 * to avoid unnecessary load to the system. Also once a view has
6969 * made a notifucation this method is a NOP until the notification has
6970 * been sent to clients.
6971 *
6972 * @hide
6973 *
6974 * TODO: Makse sure this method is called for any view state change
6975 * that is interesting for accessilility purposes.
6976 */
6977 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006978 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6979 return;
6980 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006981 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6982 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006983 if (mParent != null) {
6984 mParent.childAccessibilityStateChanged(this);
6985 }
6986 }
6987 }
6988
6989 /**
6990 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006991 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006992 *
6993 * @hide
6994 */
6995 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006996 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006997 }
6998
6999 /**
7000 * Performs the specified accessibility action on the view. For
7001 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07007002 * <p>
7003 * If an {@link AccessibilityDelegate} has been specified via calling
7004 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
7005 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
7006 * is responsible for handling this call.
7007 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07007008 *
7009 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007010 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007011 * @return Whether the action was performed.
7012 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007013 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07007014 if (mAccessibilityDelegate != null) {
7015 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
7016 } else {
7017 return performAccessibilityActionInternal(action, arguments);
7018 }
7019 }
7020
7021 /**
7022 * @see #performAccessibilityAction(int, Bundle)
7023 *
7024 * Note: Called from the default {@link AccessibilityDelegate}.
7025 */
7026 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007027 switch (action) {
7028 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007029 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07007030 performClick();
7031 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007032 }
7033 } break;
7034 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
7035 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07007036 performLongClick();
7037 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007038 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007039 } break;
7040 case AccessibilityNodeInfo.ACTION_FOCUS: {
7041 if (!hasFocus()) {
7042 // Get out of touch mode since accessibility
7043 // wants to move focus around.
7044 getViewRootImpl().ensureTouchMode(false);
7045 return requestFocus();
7046 }
7047 } break;
7048 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
7049 if (hasFocus()) {
7050 clearFocus();
7051 return !isFocused();
7052 }
7053 } break;
7054 case AccessibilityNodeInfo.ACTION_SELECT: {
7055 if (!isSelected()) {
7056 setSelected(true);
7057 return isSelected();
7058 }
7059 } break;
7060 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
7061 if (isSelected()) {
7062 setSelected(false);
7063 return !isSelected();
7064 }
7065 } break;
7066 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07007067 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007068 return requestAccessibilityFocus();
7069 }
7070 } break;
7071 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
7072 if (isAccessibilityFocused()) {
7073 clearAccessibilityFocus();
7074 return true;
7075 }
7076 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007077 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
7078 if (arguments != null) {
7079 final int granularity = arguments.getInt(
7080 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
Svetoslav7c512842013-01-30 23:02:08 -08007081 final boolean extendSelection = arguments.getBoolean(
7082 AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN);
Svetoslavabad55d2013-05-07 18:49:51 -07007083 return traverseAtGranularity(granularity, true, extendSelection);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007084 }
7085 } break;
7086 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
7087 if (arguments != null) {
7088 final int granularity = arguments.getInt(
7089 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
Svetoslav7c512842013-01-30 23:02:08 -08007090 final boolean extendSelection = arguments.getBoolean(
7091 AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN);
Svetoslavabad55d2013-05-07 18:49:51 -07007092 return traverseAtGranularity(granularity, false, extendSelection);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007093 }
7094 } break;
Svetoslavd0c83cc2013-02-04 18:39:59 -08007095 case AccessibilityNodeInfo.ACTION_SET_SELECTION: {
7096 CharSequence text = getIterableTextForAccessibility();
7097 if (text == null) {
7098 return false;
7099 }
7100 final int start = (arguments != null) ? arguments.getInt(
7101 AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, -1) : -1;
7102 final int end = (arguments != null) ? arguments.getInt(
7103 AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, -1) : -1;
7104 // Only cursor position can be specified (selection length == 0)
7105 if ((getAccessibilitySelectionStart() != start
7106 || getAccessibilitySelectionEnd() != end)
7107 && (start == end)) {
7108 setAccessibilitySelection(start, end);
7109 notifyAccessibilityStateChanged();
7110 return true;
7111 }
7112 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007113 }
7114 return false;
7115 }
7116
Svetoslavabad55d2013-05-07 18:49:51 -07007117 private boolean traverseAtGranularity(int granularity, boolean forward,
7118 boolean extendSelection) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007119 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007120 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007121 return false;
7122 }
7123 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7124 if (iterator == null) {
7125 return false;
7126 }
Svetoslav7c512842013-01-30 23:02:08 -08007127 int current = getAccessibilitySelectionEnd();
7128 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
Svetoslavabad55d2013-05-07 18:49:51 -07007129 current = forward ? 0 : text.length();
Svetoslav7c512842013-01-30 23:02:08 -08007130 }
Svetoslavabad55d2013-05-07 18:49:51 -07007131 final int[] range = forward ? iterator.following(current) : iterator.preceding(current);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007132 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007133 return false;
7134 }
Svetoslavabad55d2013-05-07 18:49:51 -07007135 final int segmentStart = range[0];
7136 final int segmentEnd = range[1];
7137 int selectionStart;
7138 int selectionEnd;
Svetoslav7c512842013-01-30 23:02:08 -08007139 if (extendSelection && isAccessibilitySelectionExtendable()) {
Svetoslavabad55d2013-05-07 18:49:51 -07007140 selectionStart = getAccessibilitySelectionStart();
Svetoslav7c512842013-01-30 23:02:08 -08007141 if (selectionStart == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
Svetoslavabad55d2013-05-07 18:49:51 -07007142 selectionStart = forward ? segmentStart : segmentEnd;
Svetoslav7c512842013-01-30 23:02:08 -08007143 }
Svetoslavabad55d2013-05-07 18:49:51 -07007144 selectionEnd = forward ? segmentEnd : segmentStart;
Svetoslav7c512842013-01-30 23:02:08 -08007145 } else {
Svetoslavabad55d2013-05-07 18:49:51 -07007146 selectionStart = selectionEnd= forward ? segmentEnd : segmentStart;
Svetoslav7c512842013-01-30 23:02:08 -08007147 }
Svetoslavabad55d2013-05-07 18:49:51 -07007148 setAccessibilitySelection(selectionStart, selectionEnd);
7149 final int action = forward ? AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
7150 : AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY;
7151 sendViewTextTraversedAtGranularityEvent(action, granularity, segmentStart, segmentEnd);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007152 return true;
7153 }
7154
7155 /**
7156 * Gets the text reported for accessibility purposes.
7157 *
7158 * @return The accessibility text.
7159 *
7160 * @hide
7161 */
7162 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007163 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007164 }
7165
7166 /**
Svetoslav7c512842013-01-30 23:02:08 -08007167 * Gets whether accessibility selection can be extended.
7168 *
7169 * @return If selection is extensible.
7170 *
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007171 * @hide
7172 */
Svetoslav7c512842013-01-30 23:02:08 -08007173 public boolean isAccessibilitySelectionExtendable() {
7174 return false;
7175 }
7176
7177 /**
7178 * @hide
7179 */
7180 public int getAccessibilitySelectionStart() {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007181 return mAccessibilityCursorPosition;
7182 }
7183
7184 /**
7185 * @hide
7186 */
Svetoslav7c512842013-01-30 23:02:08 -08007187 public int getAccessibilitySelectionEnd() {
7188 return getAccessibilitySelectionStart();
7189 }
7190
7191 /**
7192 * @hide
7193 */
7194 public void setAccessibilitySelection(int start, int end) {
Svetoslavbcc46a02013-02-06 11:56:00 -08007195 if (start == end && end == mAccessibilityCursorPosition) {
7196 return;
7197 }
Svetoslav7c512842013-01-30 23:02:08 -08007198 if (start >= 0 && start == end && end <= getIterableTextForAccessibility().length()) {
7199 mAccessibilityCursorPosition = start;
7200 } else {
7201 mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
7202 }
Svetoslavbcc46a02013-02-06 11:56:00 -08007203 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007204 }
7205
7206 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7207 int fromIndex, int toIndex) {
7208 if (mParent == null) {
7209 return;
7210 }
7211 AccessibilityEvent event = AccessibilityEvent.obtain(
7212 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7213 onInitializeAccessibilityEvent(event);
7214 onPopulateAccessibilityEvent(event);
7215 event.setFromIndex(fromIndex);
7216 event.setToIndex(toIndex);
7217 event.setAction(action);
7218 event.setMovementGranularity(granularity);
7219 mParent.requestSendAccessibilityEvent(this, event);
7220 }
7221
7222 /**
7223 * @hide
7224 */
7225 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7226 switch (granularity) {
7227 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7228 CharSequence text = getIterableTextForAccessibility();
7229 if (text != null && text.length() > 0) {
7230 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007231 CharacterTextSegmentIterator.getInstance(
7232 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007233 iterator.initialize(text.toString());
7234 return iterator;
7235 }
7236 } break;
7237 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7238 CharSequence text = getIterableTextForAccessibility();
7239 if (text != null && text.length() > 0) {
7240 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007241 WordTextSegmentIterator.getInstance(
7242 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007243 iterator.initialize(text.toString());
7244 return iterator;
7245 }
7246 } break;
7247 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7248 CharSequence text = getIterableTextForAccessibility();
7249 if (text != null && text.length() > 0) {
7250 ParagraphTextSegmentIterator iterator =
7251 ParagraphTextSegmentIterator.getInstance();
7252 iterator.initialize(text.toString());
7253 return iterator;
7254 }
7255 } break;
7256 }
7257 return null;
7258 }
7259
Svetoslav Ganov42138042012-03-20 11:51:39 -07007260 /**
Romain Guya440b002010-02-24 15:57:54 -08007261 * @hide
7262 */
7263 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007264 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007265 clearDisplayList();
7266
Romain Guya440b002010-02-24 15:57:54 -08007267 onStartTemporaryDetach();
7268 }
7269
7270 /**
7271 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7273 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007274 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 */
7276 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007277 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007278 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007279 }
7280
7281 /**
7282 * @hide
7283 */
7284 public void dispatchFinishTemporaryDetach() {
7285 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 }
Romain Guy8506ab42009-06-11 17:35:47 -07007287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 /**
7289 * Called after {@link #onStartTemporaryDetach} when the container is done
7290 * changing the view.
7291 */
7292 public void onFinishTemporaryDetach() {
7293 }
Romain Guy8506ab42009-06-11 17:35:47 -07007294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007296 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7297 * for this view's window. Returns null if the view is not currently attached
7298 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007299 * just use the standard high-level event callbacks like
7300 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007301 */
7302 public KeyEvent.DispatcherState getKeyDispatcherState() {
7303 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7304 }
Joe Malin32736f02011-01-19 16:14:20 -08007305
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 * Dispatch a key event before it is processed by any input method
7308 * associated with the view hierarchy. This can be used to intercept
7309 * key events in special situations before the IME consumes them; a
7310 * typical example would be handling the BACK key to update the application's
7311 * UI instead of allowing the IME to see it and close itself.
7312 *
7313 * @param event The key event to be dispatched.
7314 * @return True if the event was handled, false otherwise.
7315 */
7316 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7317 return onKeyPreIme(event.getKeyCode(), event);
7318 }
7319
7320 /**
7321 * Dispatch a key event to the next view on the focus path. This path runs
7322 * from the top of the view tree down to the currently focused view. If this
7323 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7324 * the next node down the focus path. This method also fires any key
7325 * listeners.
7326 *
7327 * @param event The key event to be dispatched.
7328 * @return True if the event was handled, false otherwise.
7329 */
7330 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007331 if (mInputEventConsistencyVerifier != null) {
7332 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334
Jeff Brown21bc5c92011-02-28 18:27:14 -08007335 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007336 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007337 ListenerInfo li = mListenerInfo;
7338 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7339 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 return true;
7341 }
7342
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007343 if (event.dispatch(this, mAttachInfo != null
7344 ? mAttachInfo.mKeyDispatchState : null, this)) {
7345 return true;
7346 }
7347
7348 if (mInputEventConsistencyVerifier != null) {
7349 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7350 }
7351 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007352 }
7353
7354 /**
7355 * Dispatches a key shortcut event.
7356 *
7357 * @param event The key event to be dispatched.
7358 * @return True if the event was handled by the view, false otherwise.
7359 */
7360 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7361 return onKeyShortcut(event.getKeyCode(), event);
7362 }
7363
7364 /**
7365 * Pass the touch screen motion event down to the target view, or this
7366 * view if it is the target.
7367 *
7368 * @param event The motion event to be dispatched.
7369 * @return True if the event was handled by the view, false otherwise.
7370 */
7371 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007372 if (mInputEventConsistencyVerifier != null) {
7373 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7374 }
7375
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007376 if (onFilterTouchEventForSecurity(event)) {
7377 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007378 ListenerInfo li = mListenerInfo;
7379 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7380 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007381 return true;
7382 }
7383
7384 if (onTouchEvent(event)) {
7385 return true;
7386 }
Jeff Brown85a31762010-09-01 17:01:00 -07007387 }
7388
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007389 if (mInputEventConsistencyVerifier != null) {
7390 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007392 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 }
7394
7395 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007396 * Filter the touch event to apply security policies.
7397 *
7398 * @param event The motion event to be filtered.
7399 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007400 *
Jeff Brown85a31762010-09-01 17:01:00 -07007401 * @see #getFilterTouchesWhenObscured
7402 */
7403 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007404 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007405 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7406 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7407 // Window is obscured, drop this touch.
7408 return false;
7409 }
7410 return true;
7411 }
7412
7413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 * Pass a trackball motion event down to the focused view.
7415 *
7416 * @param event The motion event to be dispatched.
7417 * @return True if the event was handled by the view, false otherwise.
7418 */
7419 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007420 if (mInputEventConsistencyVerifier != null) {
7421 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7422 }
7423
Romain Guy02ccac62011-06-24 13:20:23 -07007424 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 }
7426
7427 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007428 * Dispatch a generic motion event.
7429 * <p>
7430 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7431 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007432 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007433 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007434 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007435 *
7436 * @param event The motion event to be dispatched.
7437 * @return True if the event was handled by the view, false otherwise.
7438 */
7439 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007440 if (mInputEventConsistencyVerifier != null) {
7441 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7442 }
7443
Jeff Browna032cc02011-03-07 16:56:21 -08007444 final int source = event.getSource();
7445 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7446 final int action = event.getAction();
7447 if (action == MotionEvent.ACTION_HOVER_ENTER
7448 || action == MotionEvent.ACTION_HOVER_MOVE
7449 || action == MotionEvent.ACTION_HOVER_EXIT) {
7450 if (dispatchHoverEvent(event)) {
7451 return true;
7452 }
7453 } else if (dispatchGenericPointerEvent(event)) {
7454 return true;
7455 }
7456 } else if (dispatchGenericFocusedEvent(event)) {
7457 return true;
7458 }
7459
Jeff Brown10b62902011-06-20 16:40:37 -07007460 if (dispatchGenericMotionEventInternal(event)) {
7461 return true;
7462 }
7463
7464 if (mInputEventConsistencyVerifier != null) {
7465 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7466 }
7467 return false;
7468 }
7469
7470 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007471 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007472 ListenerInfo li = mListenerInfo;
7473 if (li != null && li.mOnGenericMotionListener != null
7474 && (mViewFlags & ENABLED_MASK) == ENABLED
7475 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007476 return true;
7477 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007478
7479 if (onGenericMotionEvent(event)) {
7480 return true;
7481 }
7482
7483 if (mInputEventConsistencyVerifier != null) {
7484 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7485 }
7486 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007487 }
7488
7489 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007490 * Dispatch a hover event.
7491 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007492 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007493 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007494 * </p>
7495 *
7496 * @param event The motion event to be dispatched.
7497 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007498 */
7499 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007500 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007501 ListenerInfo li = mListenerInfo;
7502 if (li != null && li.mOnHoverListener != null
7503 && (mViewFlags & ENABLED_MASK) == ENABLED
7504 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007505 return true;
7506 }
7507
Jeff Browna032cc02011-03-07 16:56:21 -08007508 return onHoverEvent(event);
7509 }
7510
7511 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007512 * Returns true if the view has a child to which it has recently sent
7513 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7514 * it does not have a hovered child, then it must be the innermost hovered view.
7515 * @hide
7516 */
7517 protected boolean hasHoveredChild() {
7518 return false;
7519 }
7520
7521 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007522 * Dispatch a generic motion event to the view under the first pointer.
7523 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007524 * Do not call this method directly.
7525 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007526 * </p>
7527 *
7528 * @param event The motion event to be dispatched.
7529 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007530 */
7531 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7532 return false;
7533 }
7534
7535 /**
7536 * Dispatch a generic motion event to the currently focused view.
7537 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007538 * Do not call this method directly.
7539 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007540 * </p>
7541 *
7542 * @param event The motion event to be dispatched.
7543 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007544 */
7545 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7546 return false;
7547 }
7548
7549 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007550 * Dispatch a pointer event.
7551 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007552 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7553 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7554 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007555 * and should not be expected to handle other pointing device features.
7556 * </p>
7557 *
7558 * @param event The motion event to be dispatched.
7559 * @return True if the event was handled by the view, false otherwise.
7560 * @hide
7561 */
7562 public final boolean dispatchPointerEvent(MotionEvent event) {
7563 if (event.isTouchEvent()) {
7564 return dispatchTouchEvent(event);
7565 } else {
7566 return dispatchGenericMotionEvent(event);
7567 }
7568 }
7569
7570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 * Called when the window containing this view gains or loses window focus.
7572 * ViewGroups should override to route to their children.
7573 *
7574 * @param hasFocus True if the window containing this view now has focus,
7575 * false otherwise.
7576 */
7577 public void dispatchWindowFocusChanged(boolean hasFocus) {
7578 onWindowFocusChanged(hasFocus);
7579 }
7580
7581 /**
7582 * Called when the window containing this view gains or loses focus. Note
7583 * that this is separate from view focus: to receive key events, both
7584 * your view and its window must have focus. If a window is displayed
7585 * on top of yours that takes input focus, then your own window will lose
7586 * focus but the view focus will remain unchanged.
7587 *
7588 * @param hasWindowFocus True if the window containing this view now has
7589 * focus, false otherwise.
7590 */
7591 public void onWindowFocusChanged(boolean hasWindowFocus) {
7592 InputMethodManager imm = InputMethodManager.peekInstance();
7593 if (!hasWindowFocus) {
7594 if (isPressed()) {
7595 setPressed(false);
7596 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007597 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598 imm.focusOut(this);
7599 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007600 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007601 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007602 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007603 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 imm.focusIn(this);
7605 }
7606 refreshDrawableState();
7607 }
7608
7609 /**
7610 * Returns true if this view is in a window that currently has window focus.
7611 * Note that this is not the same as the view itself having focus.
7612 *
7613 * @return True if this view is in a window that currently has window focus.
7614 */
7615 public boolean hasWindowFocus() {
7616 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7617 }
7618
7619 /**
Adam Powell326d8082009-12-09 15:10:07 -08007620 * Dispatch a view visibility change down the view hierarchy.
7621 * ViewGroups should override to route to their children.
7622 * @param changedView The view whose visibility changed. Could be 'this' or
7623 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007624 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7625 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007626 */
7627 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7628 onVisibilityChanged(changedView, visibility);
7629 }
7630
7631 /**
7632 * Called when the visibility of the view or an ancestor of the view is changed.
7633 * @param changedView The view whose visibility changed. Could be 'this' or
7634 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007635 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7636 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007637 */
7638 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007639 if (visibility == VISIBLE) {
7640 if (mAttachInfo != null) {
7641 initialAwakenScrollBars();
7642 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007643 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007644 }
7645 }
Adam Powell326d8082009-12-09 15:10:07 -08007646 }
7647
7648 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007649 * Dispatch a hint about whether this view is displayed. For instance, when
7650 * a View moves out of the screen, it might receives a display hint indicating
7651 * the view is not displayed. Applications should not <em>rely</em> on this hint
7652 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007653 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007654 * @param hint A hint about whether or not this view is displayed:
7655 * {@link #VISIBLE} or {@link #INVISIBLE}.
7656 */
7657 public void dispatchDisplayHint(int hint) {
7658 onDisplayHint(hint);
7659 }
7660
7661 /**
7662 * Gives this view a hint about whether is displayed or not. For instance, when
7663 * a View moves out of the screen, it might receives a display hint indicating
7664 * the view is not displayed. Applications should not <em>rely</em> on this hint
7665 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007666 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007667 * @param hint A hint about whether or not this view is displayed:
7668 * {@link #VISIBLE} or {@link #INVISIBLE}.
7669 */
7670 protected void onDisplayHint(int hint) {
7671 }
7672
7673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 * Dispatch a window visibility change down the view hierarchy.
7675 * ViewGroups should override to route to their children.
7676 *
7677 * @param visibility The new visibility of the window.
7678 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007679 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 */
7681 public void dispatchWindowVisibilityChanged(int visibility) {
7682 onWindowVisibilityChanged(visibility);
7683 }
7684
7685 /**
7686 * Called when the window containing has change its visibility
7687 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7688 * that this tells you whether or not your window is being made visible
7689 * to the window manager; this does <em>not</em> tell you whether or not
7690 * your window is obscured by other windows on the screen, even if it
7691 * is itself visible.
7692 *
7693 * @param visibility The new visibility of the window.
7694 */
7695 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007696 if (visibility == VISIBLE) {
7697 initialAwakenScrollBars();
7698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
7700
7701 /**
7702 * Returns the current visibility of the window this view is attached to
7703 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7704 *
7705 * @return Returns the current visibility of the view's window.
7706 */
7707 public int getWindowVisibility() {
7708 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7709 }
7710
7711 /**
7712 * Retrieve the overall visible display size in which the window this view is
7713 * attached to has been positioned in. This takes into account screen
7714 * decorations above the window, for both cases where the window itself
7715 * is being position inside of them or the window is being placed under
7716 * then and covered insets are used for the window to position its content
7717 * inside. In effect, this tells you the available area where content can
7718 * be placed and remain visible to users.
7719 *
7720 * <p>This function requires an IPC back to the window manager to retrieve
7721 * the requested information, so should not be used in performance critical
7722 * code like drawing.
7723 *
7724 * @param outRect Filled in with the visible display frame. If the view
7725 * is not attached to a window, this is simply the raw display size.
7726 */
7727 public void getWindowVisibleDisplayFrame(Rect outRect) {
7728 if (mAttachInfo != null) {
7729 try {
7730 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7731 } catch (RemoteException e) {
7732 return;
7733 }
7734 // XXX This is really broken, and probably all needs to be done
7735 // in the window manager, and we need to know more about whether
7736 // we want the area behind or in front of the IME.
7737 final Rect insets = mAttachInfo.mVisibleInsets;
7738 outRect.left += insets.left;
7739 outRect.top += insets.top;
7740 outRect.right -= insets.right;
7741 outRect.bottom -= insets.bottom;
7742 return;
7743 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007744 // The view is not attached to a display so we don't have a context.
7745 // Make a best guess about the display size.
7746 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007747 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 }
7749
7750 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007751 * Dispatch a notification about a resource configuration change down
7752 * the view hierarchy.
7753 * ViewGroups should override to route to their children.
7754 *
7755 * @param newConfig The new resource configuration.
7756 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007757 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007758 */
7759 public void dispatchConfigurationChanged(Configuration newConfig) {
7760 onConfigurationChanged(newConfig);
7761 }
7762
7763 /**
7764 * Called when the current configuration of the resources being used
7765 * by the application have changed. You can use this to decide when
7766 * to reload resources that can changed based on orientation and other
7767 * configuration characterstics. You only need to use this if you are
7768 * not relying on the normal {@link android.app.Activity} mechanism of
7769 * recreating the activity instance upon a configuration change.
7770 *
7771 * @param newConfig The new resource configuration.
7772 */
7773 protected void onConfigurationChanged(Configuration newConfig) {
7774 }
7775
7776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 * Private function to aggregate all per-view attributes in to the view
7778 * root.
7779 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007780 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7781 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 }
7783
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007784 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7785 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007786 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007787 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007788 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007789 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007790 ListenerInfo li = mListenerInfo;
7791 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007792 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 }
7795 }
7796
7797 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007798 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007799 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007800 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7801 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 ai.mRecomputeGlobalAttributes = true;
7803 }
7804 }
7805 }
7806
7807 /**
7808 * Returns whether the device is currently in touch mode. Touch mode is entered
7809 * once the user begins interacting with the device by touch, and affects various
7810 * things like whether focus is always visible to the user.
7811 *
7812 * @return Whether the device is in touch mode.
7813 */
7814 @ViewDebug.ExportedProperty
7815 public boolean isInTouchMode() {
7816 if (mAttachInfo != null) {
7817 return mAttachInfo.mInTouchMode;
7818 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007819 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 }
7821 }
7822
7823 /**
7824 * Returns the context the view is running in, through which it can
7825 * access the current theme, resources, etc.
7826 *
7827 * @return The view's Context.
7828 */
7829 @ViewDebug.CapturedViewProperty
7830 public final Context getContext() {
7831 return mContext;
7832 }
7833
7834 /**
7835 * Handle a key event before it is processed by any input method
7836 * associated with the view hierarchy. This can be used to intercept
7837 * key events in special situations before the IME consumes them; a
7838 * typical example would be handling the BACK key to update the application's
7839 * UI instead of allowing the IME to see it and close itself.
7840 *
7841 * @param keyCode The value in event.getKeyCode().
7842 * @param event Description of the key event.
7843 * @return If you handled the event, return true. If you want to allow the
7844 * event to be handled by the next receiver, return false.
7845 */
7846 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7847 return false;
7848 }
7849
7850 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007851 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7852 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7854 * is released, if the view is enabled and clickable.
7855 *
Jean Chalard405bc512012-05-29 19:12:34 +09007856 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7857 * although some may elect to do so in some situations. Do not rely on this to
7858 * catch software key presses.
7859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 * @param keyCode A key code that represents the button pressed, from
7861 * {@link android.view.KeyEvent}.
7862 * @param event The KeyEvent object that defines the button action.
7863 */
7864 public boolean onKeyDown(int keyCode, KeyEvent event) {
7865 boolean result = false;
7866
7867 switch (keyCode) {
7868 case KeyEvent.KEYCODE_DPAD_CENTER:
7869 case KeyEvent.KEYCODE_ENTER: {
7870 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7871 return true;
7872 }
7873 // Long clickable items don't necessarily have to be clickable
7874 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7875 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7876 (event.getRepeatCount() == 0)) {
7877 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007878 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 return true;
7880 }
7881 break;
7882 }
7883 }
7884 return result;
7885 }
7886
7887 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007888 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7889 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7890 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007891 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7892 * although some may elect to do so in some situations. Do not rely on this to
7893 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007894 */
7895 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7896 return false;
7897 }
7898
7899 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007900 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7901 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7903 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007904 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7905 * although some may elect to do so in some situations. Do not rely on this to
7906 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 *
7908 * @param keyCode A key code that represents the button pressed, from
7909 * {@link android.view.KeyEvent}.
7910 * @param event The KeyEvent object that defines the button action.
7911 */
7912 public boolean onKeyUp(int keyCode, KeyEvent event) {
7913 boolean result = false;
7914
7915 switch (keyCode) {
7916 case KeyEvent.KEYCODE_DPAD_CENTER:
7917 case KeyEvent.KEYCODE_ENTER: {
7918 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7919 return true;
7920 }
7921 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7922 setPressed(false);
7923
7924 if (!mHasPerformedLongPress) {
7925 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007926 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007927
7928 result = performClick();
7929 }
7930 }
7931 break;
7932 }
7933 }
7934 return result;
7935 }
7936
7937 /**
7938 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7939 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7940 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007941 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7942 * although some may elect to do so in some situations. Do not rely on this to
7943 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 *
7945 * @param keyCode A key code that represents the button pressed, from
7946 * {@link android.view.KeyEvent}.
7947 * @param repeatCount The number of times the action was made.
7948 * @param event The KeyEvent object that defines the button action.
7949 */
7950 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7951 return false;
7952 }
7953
7954 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007955 * Called on the focused view when a key shortcut event is not handled.
7956 * Override this method to implement local key shortcuts for the View.
7957 * Key shortcuts can also be implemented by setting the
7958 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 *
7960 * @param keyCode The value in event.getKeyCode().
7961 * @param event Description of the key event.
7962 * @return If you handled the event, return true. If you want to allow the
7963 * event to be handled by the next receiver, return false.
7964 */
7965 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7966 return false;
7967 }
7968
7969 /**
7970 * Check whether the called view is a text editor, in which case it
7971 * would make sense to automatically display a soft input window for
7972 * it. Subclasses should override this if they implement
7973 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007974 * a call on that method would return a non-null InputConnection, and
7975 * they are really a first-class editor that the user would normally
7976 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007977 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007978 * <p>The default implementation always returns false. This does
7979 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7980 * will not be called or the user can not otherwise perform edits on your
7981 * view; it is just a hint to the system that this is not the primary
7982 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007983 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 * @return Returns true if this view is a text editor, else false.
7985 */
7986 public boolean onCheckIsTextEditor() {
7987 return false;
7988 }
Romain Guy8506ab42009-06-11 17:35:47 -07007989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 /**
7991 * Create a new InputConnection for an InputMethod to interact
7992 * with the view. The default implementation returns null, since it doesn't
7993 * support input methods. You can override this to implement such support.
7994 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007995 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 * <p>When implementing this, you probably also want to implement
7997 * {@link #onCheckIsTextEditor()} to indicate you will return a
7998 * non-null InputConnection.
7999 *
8000 * @param outAttrs Fill in with attribute information about the connection.
8001 */
8002 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
8003 return null;
8004 }
8005
8006 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008007 * Called by the {@link android.view.inputmethod.InputMethodManager}
8008 * when a view who is not the current
8009 * input connection target is trying to make a call on the manager. The
8010 * default implementation returns false; you can override this to return
8011 * true for certain views if you are performing InputConnection proxying
8012 * to them.
8013 * @param view The View that is making the InputMethodManager call.
8014 * @return Return true to allow the call, false to reject.
8015 */
8016 public boolean checkInputConnectionProxy(View view) {
8017 return false;
8018 }
Romain Guy8506ab42009-06-11 17:35:47 -07008019
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 * Show the context menu for this view. It is not safe to hold on to the
8022 * menu after returning from this method.
8023 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07008024 * You should normally not overload this method. Overload
8025 * {@link #onCreateContextMenu(ContextMenu)} or define an
8026 * {@link OnCreateContextMenuListener} to add items to the context menu.
8027 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 * @param menu The context menu to populate
8029 */
8030 public void createContextMenu(ContextMenu menu) {
8031 ContextMenuInfo menuInfo = getContextMenuInfo();
8032
8033 // Sets the current menu info so all items added to menu will have
8034 // my extra info set.
8035 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
8036
8037 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07008038 ListenerInfo li = mListenerInfo;
8039 if (li != null && li.mOnCreateContextMenuListener != null) {
8040 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 }
8042
8043 // Clear the extra information so subsequent items that aren't mine don't
8044 // have my extra info.
8045 ((MenuBuilder)menu).setCurrentMenuInfo(null);
8046
8047 if (mParent != null) {
8048 mParent.createContextMenu(menu);
8049 }
8050 }
8051
8052 /**
8053 * Views should implement this if they have extra information to associate
8054 * with the context menu. The return result is supplied as a parameter to
8055 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
8056 * callback.
8057 *
8058 * @return Extra information about the item for which the context menu
8059 * should be shown. This information will vary across different
8060 * subclasses of View.
8061 */
8062 protected ContextMenuInfo getContextMenuInfo() {
8063 return null;
8064 }
8065
8066 /**
8067 * Views should implement this if the view itself is going to add items to
8068 * the context menu.
8069 *
8070 * @param menu the context menu to populate
8071 */
8072 protected void onCreateContextMenu(ContextMenu menu) {
8073 }
8074
8075 /**
8076 * Implement this method to handle trackball motion events. The
8077 * <em>relative</em> movement of the trackball since the last event
8078 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
8079 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
8080 * that a movement of 1 corresponds to the user pressing one DPAD key (so
8081 * they will often be fractional values, representing the more fine-grained
8082 * movement information available from a trackball).
8083 *
8084 * @param event The motion event.
8085 * @return True if the event was handled, false otherwise.
8086 */
8087 public boolean onTrackballEvent(MotionEvent event) {
8088 return false;
8089 }
8090
8091 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08008092 * Implement this method to handle generic motion events.
8093 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08008094 * Generic motion events describe joystick movements, mouse hovers, track pad
8095 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08008096 * {@link MotionEvent#getSource() source} of the motion event specifies
8097 * the class of input that was received. Implementations of this method
8098 * must examine the bits in the source before processing the event.
8099 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08008100 * </p><p>
8101 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
8102 * are delivered to the view under the pointer. All other generic motion events are
8103 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08008104 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07008105 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Michael Wright74e41562013-03-08 14:58:14 -08008106 * if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08008107 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
8108 * // process the joystick movement...
8109 * return true;
8110 * }
8111 * }
Michael Wright74e41562013-03-08 14:58:14 -08008112 * if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08008113 * switch (event.getAction()) {
8114 * case MotionEvent.ACTION_HOVER_MOVE:
8115 * // process the mouse hover movement...
8116 * return true;
8117 * case MotionEvent.ACTION_SCROLL:
8118 * // process the scroll wheel movement...
8119 * return true;
8120 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08008121 * }
8122 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07008123 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08008124 *
8125 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08008126 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08008127 */
8128 public boolean onGenericMotionEvent(MotionEvent event) {
8129 return false;
8130 }
8131
8132 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008133 * Implement this method to handle hover events.
8134 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07008135 * This method is called whenever a pointer is hovering into, over, or out of the
8136 * bounds of a view and the view is not currently being touched.
8137 * Hover events are represented as pointer events with action
8138 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
8139 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
8140 * </p>
8141 * <ul>
8142 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8143 * when the pointer enters the bounds of the view.</li>
8144 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8145 * when the pointer has already entered the bounds of the view and has moved.</li>
8146 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8147 * when the pointer has exited the bounds of the view or when the pointer is
8148 * about to go down due to a button click, tap, or similar user action that
8149 * causes the view to be touched.</li>
8150 * </ul>
8151 * <p>
8152 * The view should implement this method to return true to indicate that it is
8153 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008154 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008155 * The default implementation calls {@link #setHovered} to update the hovered state
8156 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008157 * is enabled and is clickable. The default implementation also sends hover
8158 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008159 * </p>
8160 *
8161 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008162 * @return True if the view handled the hover event.
8163 *
8164 * @see #isHovered
8165 * @see #setHovered
8166 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008167 */
8168 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008169 // The root view may receive hover (or touch) events that are outside the bounds of
8170 // the window. This code ensures that we only send accessibility events for
8171 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008172 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008173 if (!mSendingHoverAccessibilityEvents) {
8174 if ((action == MotionEvent.ACTION_HOVER_ENTER
8175 || action == MotionEvent.ACTION_HOVER_MOVE)
8176 && !hasHoveredChild()
8177 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008178 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008179 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008180 }
8181 } else {
8182 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008183 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008184 && !pointInView(event.getX(), event.getY()))) {
8185 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008186 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008187 // If the window does not have input focus we take away accessibility
8188 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008189 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008190 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008191 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008192 }
Jeff Browna1b24182011-07-28 13:38:24 -07008193 }
8194
Jeff Brown87b7f802011-06-21 18:35:45 -07008195 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008196 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008197 case MotionEvent.ACTION_HOVER_ENTER:
8198 setHovered(true);
8199 break;
8200 case MotionEvent.ACTION_HOVER_EXIT:
8201 setHovered(false);
8202 break;
8203 }
Jeff Browna1b24182011-07-28 13:38:24 -07008204
8205 // Dispatch the event to onGenericMotionEvent before returning true.
8206 // This is to provide compatibility with existing applications that
8207 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8208 // break because of the new default handling for hoverable views
8209 // in onHoverEvent.
8210 // Note that onGenericMotionEvent will be called by default when
8211 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
Alan Viverettee7a4e632013-04-25 17:32:45 -07008212 dispatchGenericMotionEventInternal(event);
8213 // The event was already handled by calling setHovered(), so always
8214 // return true.
8215 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008216 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008217
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008218 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008219 }
8220
8221 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008222 * Returns true if the view should handle {@link #onHoverEvent}
8223 * by calling {@link #setHovered} to change its hovered state.
8224 *
8225 * @return True if the view is hoverable.
8226 */
8227 private boolean isHoverable() {
8228 final int viewFlags = mViewFlags;
8229 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8230 return false;
8231 }
8232
8233 return (viewFlags & CLICKABLE) == CLICKABLE
8234 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8235 }
8236
8237 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008238 * Returns true if the view is currently hovered.
8239 *
8240 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008241 *
8242 * @see #setHovered
8243 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008244 */
Jeff Brown10b62902011-06-20 16:40:37 -07008245 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008246 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008247 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008248 }
8249
8250 /**
8251 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008252 * <p>
8253 * Calling this method also changes the drawable state of the view. This
8254 * enables the view to react to hover by using different drawable resources
8255 * to change its appearance.
8256 * </p><p>
8257 * The {@link #onHoverChanged} method is called when the hovered state changes.
8258 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008259 *
8260 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008261 *
8262 * @see #isHovered
8263 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008264 */
8265 public void setHovered(boolean hovered) {
8266 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008267 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8268 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008269 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008270 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008271 }
8272 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008273 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8274 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008275 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008276 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008277 }
8278 }
8279 }
8280
8281 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008282 * Implement this method to handle hover state changes.
8283 * <p>
8284 * This method is called whenever the hover state changes as a result of a
8285 * call to {@link #setHovered}.
8286 * </p>
8287 *
8288 * @param hovered The current hover state, as returned by {@link #isHovered}.
8289 *
8290 * @see #isHovered
8291 * @see #setHovered
8292 */
8293 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008294 }
8295
8296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 * Implement this method to handle touch screen motion events.
8298 *
8299 * @param event The motion event.
8300 * @return True if the event was handled, false otherwise.
8301 */
8302 public boolean onTouchEvent(MotionEvent event) {
8303 final int viewFlags = mViewFlags;
8304
8305 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008306 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008307 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 // A disabled view that is clickable still consumes the touch
8310 // events, it just doesn't respond to them.
8311 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8312 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8313 }
8314
8315 if (mTouchDelegate != null) {
8316 if (mTouchDelegate.onTouchEvent(event)) {
8317 return true;
8318 }
8319 }
8320
8321 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8322 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8323 switch (event.getAction()) {
8324 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008325 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8326 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 // take focus if we don't have it already and we should in
8328 // touch mode.
8329 boolean focusTaken = false;
8330 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8331 focusTaken = requestFocus();
8332 }
8333
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008334 if (prepressed) {
8335 // The button is being released before we actually
8336 // showed it as pressed. Make it show the pressed
8337 // state now (before scheduling the click) to ensure
8338 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008339 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008340 }
Joe Malin32736f02011-01-19 16:14:20 -08008341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 if (!mHasPerformedLongPress) {
8343 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008344 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345
8346 // Only perform take click actions if we were in the pressed state
8347 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008348 // Use a Runnable and post this rather than calling
8349 // performClick directly. This lets other visual state
8350 // of the view update before click actions start.
8351 if (mPerformClick == null) {
8352 mPerformClick = new PerformClick();
8353 }
8354 if (!post(mPerformClick)) {
8355 performClick();
8356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 }
8358 }
8359
8360 if (mUnsetPressedState == null) {
8361 mUnsetPressedState = new UnsetPressedState();
8362 }
8363
Adam Powelle14579b2009-12-16 18:39:52 -08008364 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008365 postDelayed(mUnsetPressedState,
8366 ViewConfiguration.getPressedStateDuration());
8367 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 // If the post failed, unpress right now
8369 mUnsetPressedState.run();
8370 }
Adam Powelle14579b2009-12-16 18:39:52 -08008371 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008372 }
8373 break;
8374
8375 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008376 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008377
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008378 if (performButtonActionOnTouchDown(event)) {
8379 break;
8380 }
8381
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008382 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008383 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008384
8385 // For views inside a scrolling container, delay the pressed feedback for
8386 // a short period in case this is a scroll.
8387 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008388 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008389 if (mPendingCheckForTap == null) {
8390 mPendingCheckForTap = new CheckForTap();
8391 }
8392 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8393 } else {
8394 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008395 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008396 checkForLongClick(0);
8397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 break;
8399
8400 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008401 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008402 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008403 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404 break;
8405
8406 case MotionEvent.ACTION_MOVE:
8407 final int x = (int) event.getX();
8408 final int y = (int) event.getY();
8409
8410 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008411 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008413 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008414 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008415 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008416 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417
Adam Powell4d6f0662012-02-21 15:11:11 -08008418 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 }
8421 break;
8422 }
8423 return true;
8424 }
8425
8426 return false;
8427 }
8428
8429 /**
Adam Powell10298662011-08-14 18:26:30 -07008430 * @hide
8431 */
8432 public boolean isInScrollingContainer() {
8433 ViewParent p = getParent();
8434 while (p != null && p instanceof ViewGroup) {
8435 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8436 return true;
8437 }
8438 p = p.getParent();
8439 }
8440 return false;
8441 }
8442
8443 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008444 * Remove the longpress detection timer.
8445 */
8446 private void removeLongPressCallback() {
8447 if (mPendingCheckForLongPress != null) {
8448 removeCallbacks(mPendingCheckForLongPress);
8449 }
8450 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008451
8452 /**
8453 * Remove the pending click action
8454 */
8455 private void removePerformClickCallback() {
8456 if (mPerformClick != null) {
8457 removeCallbacks(mPerformClick);
8458 }
8459 }
8460
Adam Powelle14579b2009-12-16 18:39:52 -08008461 /**
Romain Guya440b002010-02-24 15:57:54 -08008462 * Remove the prepress detection timer.
8463 */
8464 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008465 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008466 setPressed(false);
8467 removeCallbacks(mUnsetPressedState);
8468 }
8469 }
8470
8471 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008472 * Remove the tap detection timer.
8473 */
8474 private void removeTapCallback() {
8475 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008476 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008477 removeCallbacks(mPendingCheckForTap);
8478 }
8479 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008480
8481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008482 * Cancels a pending long press. Your subclass can use this if you
8483 * want the context menu to come up if the user presses and holds
8484 * at the same place, but you don't want it to come up if they press
8485 * and then move around enough to cause scrolling.
8486 */
8487 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008488 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008489
8490 /*
8491 * The prepressed state handled by the tap callback is a display
8492 * construct, but the tap callback will post a long press callback
8493 * less its own timeout. Remove it here.
8494 */
8495 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 }
8497
8498 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008499 * Remove the pending callback for sending a
8500 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8501 */
8502 private void removeSendViewScrolledAccessibilityEventCallback() {
8503 if (mSendViewScrolledAccessibilityEvent != null) {
8504 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008505 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008506 }
8507 }
8508
8509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008510 * Sets the TouchDelegate for this View.
8511 */
8512 public void setTouchDelegate(TouchDelegate delegate) {
8513 mTouchDelegate = delegate;
8514 }
8515
8516 /**
8517 * Gets the TouchDelegate for this View.
8518 */
8519 public TouchDelegate getTouchDelegate() {
8520 return mTouchDelegate;
8521 }
8522
8523 /**
8524 * Set flags controlling behavior of this view.
8525 *
8526 * @param flags Constant indicating the value which should be set
8527 * @param mask Constant indicating the bit range that should be changed
8528 */
8529 void setFlags(int flags, int mask) {
8530 int old = mViewFlags;
8531 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8532
8533 int changed = mViewFlags ^ old;
8534 if (changed == 0) {
8535 return;
8536 }
8537 int privateFlags = mPrivateFlags;
8538
8539 /* Check if the FOCUSABLE bit has changed */
8540 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008541 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008543 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 /* Give up focus if we are no longer focusable */
8545 clearFocus();
8546 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008547 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 /*
8549 * Tell the view system that we are now available to take focus
8550 * if no one else already has it.
8551 */
8552 if (mParent != null) mParent.focusableViewAvailable(this);
8553 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008554 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8555 notifyAccessibilityStateChanged();
8556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 }
8558
8559 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8560 if ((changed & VISIBILITY_MASK) != 0) {
8561 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008562 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008563 * it was not visible. Marking it drawn ensures that the invalidation will
8564 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008566 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008567 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568
8569 needGlobalAttributesUpdate(true);
8570
8571 // a view becoming visible is worth notifying the parent
8572 // about in case nothing has focus. even if this specific view
8573 // isn't focusable, it may contain something that is, so let
8574 // the root view try to give this focus if nothing else does.
8575 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8576 mParent.focusableViewAvailable(this);
8577 }
8578 }
8579 }
8580
8581 /* Check if the GONE bit has changed */
8582 if ((changed & GONE) != 0) {
8583 needGlobalAttributesUpdate(false);
8584 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585
Romain Guyecd80ee2009-12-03 17:13:02 -08008586 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8587 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008588 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008589 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008590 if (mParent instanceof View) {
8591 // GONE views noop invalidation, so invalidate the parent
8592 ((View) mParent).invalidate(true);
8593 }
8594 // Mark the view drawn to ensure that it gets invalidated properly the next
8595 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008596 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 }
8598 if (mAttachInfo != null) {
8599 mAttachInfo.mViewVisibilityChanged = true;
8600 }
8601 }
8602
8603 /* Check if the VISIBLE bit has changed */
8604 if ((changed & INVISIBLE) != 0) {
8605 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008606 /*
8607 * If this view is becoming invisible, set the DRAWN flag so that
8608 * the next invalidate() will not be skipped.
8609 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008610 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611
8612 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008613 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 if (getRootView() != this) {
8615 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008616 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 }
8618 }
8619 if (mAttachInfo != null) {
8620 mAttachInfo.mViewVisibilityChanged = true;
8621 }
8622 }
8623
Adam Powell326d8082009-12-09 15:10:07 -08008624 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008625 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008626 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8627 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008628 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008629 } else if (mParent != null) {
8630 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008631 }
Adam Powell326d8082009-12-09 15:10:07 -08008632 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8633 }
8634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8636 destroyDrawingCache();
8637 }
8638
8639 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8640 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008641 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008642 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 }
8644
8645 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8646 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008647 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 }
8649
8650 if ((changed & DRAW_MASK) != 0) {
8651 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008652 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008653 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8654 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008656 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008657 }
8658 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008659 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 }
8661 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008662 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 }
8664
8665 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008666 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 mParent.recomputeViewAttributes(this);
8668 }
8669 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008670
8671 if (AccessibilityManager.getInstance(mContext).isEnabled()
8672 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8673 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8674 notifyAccessibilityStateChanged();
8675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 }
8677
8678 /**
8679 * Change the view's z order in the tree, so it's on top of other sibling
Chet Haase0187a5d2013-04-23 06:55:04 -07008680 * views. This ordering change may affect layout, if the parent container
8681 * uses an order-dependent layout scheme (e.g., LinearLayout). This
8682 * method should be followed by calls to {@link #requestLayout()} and
8683 * {@link View#invalidate()} on the parent.
8684 *
8685 * @see ViewGroup#bringChildToFront(View)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008686 */
8687 public void bringToFront() {
8688 if (mParent != null) {
8689 mParent.bringChildToFront(this);
8690 }
8691 }
8692
8693 /**
8694 * This is called in response to an internal scroll in this view (i.e., the
8695 * view scrolled its own contents). This is typically as a result of
8696 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8697 * called.
8698 *
8699 * @param l Current horizontal scroll origin.
8700 * @param t Current vertical scroll origin.
8701 * @param oldl Previous horizontal scroll origin.
8702 * @param oldt Previous vertical scroll origin.
8703 */
8704 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008705 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8706 postSendViewScrolledAccessibilityEventCallback();
8707 }
8708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008709 mBackgroundSizeChanged = true;
8710
8711 final AttachInfo ai = mAttachInfo;
8712 if (ai != null) {
8713 ai.mViewScrollChanged = true;
8714 }
8715 }
8716
8717 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008718 * Interface definition for a callback to be invoked when the layout bounds of a view
8719 * changes due to layout processing.
8720 */
8721 public interface OnLayoutChangeListener {
8722 /**
8723 * Called when the focus state of a view has changed.
8724 *
8725 * @param v The view whose state has changed.
8726 * @param left The new value of the view's left property.
8727 * @param top The new value of the view's top property.
8728 * @param right The new value of the view's right property.
8729 * @param bottom The new value of the view's bottom property.
8730 * @param oldLeft The previous value of the view's left property.
8731 * @param oldTop The previous value of the view's top property.
8732 * @param oldRight The previous value of the view's right property.
8733 * @param oldBottom The previous value of the view's bottom property.
8734 */
8735 void onLayoutChange(View v, int left, int top, int right, int bottom,
8736 int oldLeft, int oldTop, int oldRight, int oldBottom);
8737 }
8738
8739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 * This is called during layout when the size of this view has changed. If
8741 * you were just added to the view hierarchy, you're called with the old
8742 * values of 0.
8743 *
8744 * @param w Current width of this view.
8745 * @param h Current height of this view.
8746 * @param oldw Old width of this view.
8747 * @param oldh Old height of this view.
8748 */
8749 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8750 }
8751
8752 /**
8753 * Called by draw to draw the child views. This may be overridden
8754 * by derived classes to gain control just before its children are drawn
8755 * (but after its own view has been drawn).
8756 * @param canvas the canvas on which to draw the view
8757 */
8758 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 }
8761
8762 /**
8763 * Gets the parent of this view. Note that the parent is a
8764 * ViewParent and not necessarily a View.
8765 *
8766 * @return Parent of this view.
8767 */
8768 public final ViewParent getParent() {
8769 return mParent;
8770 }
8771
8772 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008773 * Set the horizontal scrolled position of your view. This will cause a call to
8774 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8775 * invalidated.
8776 * @param value the x position to scroll to
8777 */
8778 public void setScrollX(int value) {
8779 scrollTo(value, mScrollY);
8780 }
8781
8782 /**
8783 * Set the vertical scrolled position of your view. This will cause a call to
8784 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8785 * invalidated.
8786 * @param value the y position to scroll to
8787 */
8788 public void setScrollY(int value) {
8789 scrollTo(mScrollX, value);
8790 }
8791
8792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 * Return the scrolled left position of this view. This is the left edge of
8794 * the displayed part of your view. You do not need to draw any pixels
8795 * farther left, since those are outside of the frame of your view on
8796 * screen.
8797 *
8798 * @return The left edge of the displayed part of your view, in pixels.
8799 */
8800 public final int getScrollX() {
8801 return mScrollX;
8802 }
8803
8804 /**
8805 * Return the scrolled top position of this view. This is the top edge of
8806 * the displayed part of your view. You do not need to draw any pixels above
8807 * it, since those are outside of the frame of your view on screen.
8808 *
8809 * @return The top edge of the displayed part of your view, in pixels.
8810 */
8811 public final int getScrollY() {
8812 return mScrollY;
8813 }
8814
8815 /**
8816 * Return the width of the your view.
8817 *
8818 * @return The width of your view, in pixels.
8819 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008820 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 public final int getWidth() {
8822 return mRight - mLeft;
8823 }
8824
8825 /**
8826 * Return the height of your view.
8827 *
8828 * @return The height of your view, in pixels.
8829 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008830 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008831 public final int getHeight() {
8832 return mBottom - mTop;
8833 }
8834
8835 /**
8836 * Return the visible drawing bounds of your view. Fills in the output
8837 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008838 * getWidth(), and getHeight(). These bounds do not account for any
8839 * transformation properties currently set on the view, such as
8840 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 *
8842 * @param outRect The (scrolled) drawing bounds of the view.
8843 */
8844 public void getDrawingRect(Rect outRect) {
8845 outRect.left = mScrollX;
8846 outRect.top = mScrollY;
8847 outRect.right = mScrollX + (mRight - mLeft);
8848 outRect.bottom = mScrollY + (mBottom - mTop);
8849 }
8850
8851 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008852 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8853 * raw width component (that is the result is masked by
8854 * {@link #MEASURED_SIZE_MASK}).
8855 *
8856 * @return The raw measured width of this view.
8857 */
8858 public final int getMeasuredWidth() {
8859 return mMeasuredWidth & MEASURED_SIZE_MASK;
8860 }
8861
8862 /**
8863 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008864 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008865 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008866 * This should be used during measurement and layout calculations only. Use
8867 * {@link #getWidth()} to see how wide a view is after layout.
8868 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008869 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008870 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008871 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 return mMeasuredWidth;
8873 }
8874
8875 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008876 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8877 * raw width component (that is the result is masked by
8878 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008879 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008880 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 */
8882 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008883 return mMeasuredHeight & MEASURED_SIZE_MASK;
8884 }
8885
8886 /**
8887 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008888 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008889 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8890 * This should be used during measurement and layout calculations only. Use
8891 * {@link #getHeight()} to see how wide a view is after layout.
8892 *
8893 * @return The measured width of this view as a bit mask.
8894 */
8895 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 return mMeasuredHeight;
8897 }
8898
8899 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008900 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8901 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8902 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8903 * and the height component is at the shifted bits
8904 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8905 */
8906 public final int getMeasuredState() {
8907 return (mMeasuredWidth&MEASURED_STATE_MASK)
8908 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8909 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8910 }
8911
8912 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008913 * The transform matrix of this view, which is calculated based on the current
8914 * roation, scale, and pivot properties.
8915 *
8916 * @see #getRotation()
8917 * @see #getScaleX()
8918 * @see #getScaleY()
8919 * @see #getPivotX()
8920 * @see #getPivotY()
8921 * @return The current transform matrix for the view
8922 */
8923 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008924 if (mTransformationInfo != null) {
8925 updateMatrix();
8926 return mTransformationInfo.mMatrix;
8927 }
8928 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008929 }
8930
8931 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008932 * Utility function to determine if the value is far enough away from zero to be
8933 * considered non-zero.
8934 * @param value A floating point value to check for zero-ness
8935 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8936 */
8937 private static boolean nonzero(float value) {
8938 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8939 }
8940
8941 /**
Jeff Brown86671742010-09-30 20:00:15 -07008942 * Returns true if the transform matrix is the identity matrix.
8943 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008944 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008945 * @return True if the transform matrix is the identity matrix, false otherwise.
8946 */
Jeff Brown86671742010-09-30 20:00:15 -07008947 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008948 if (mTransformationInfo != null) {
8949 updateMatrix();
8950 return mTransformationInfo.mMatrixIsIdentity;
8951 }
8952 return true;
8953 }
8954
8955 void ensureTransformationInfo() {
8956 if (mTransformationInfo == null) {
8957 mTransformationInfo = new TransformationInfo();
8958 }
Jeff Brown86671742010-09-30 20:00:15 -07008959 }
8960
8961 /**
8962 * Recomputes the transform matrix if necessary.
8963 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008964 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008965 final TransformationInfo info = mTransformationInfo;
8966 if (info == null) {
8967 return;
8968 }
8969 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008970 // transform-related properties have changed since the last time someone
8971 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008972
8973 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008974 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008975 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8976 info.mPrevWidth = mRight - mLeft;
8977 info.mPrevHeight = mBottom - mTop;
8978 info.mPivotX = info.mPrevWidth / 2f;
8979 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008980 }
8981 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008982 info.mMatrix.reset();
8983 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8984 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8985 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8986 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008987 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008988 if (info.mCamera == null) {
8989 info.mCamera = new Camera();
8990 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008991 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008992 info.mCamera.save();
8993 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8994 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8995 info.mCamera.getMatrix(info.matrix3D);
8996 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8997 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8998 info.mPivotY + info.mTranslationY);
8999 info.mMatrix.postConcat(info.matrix3D);
9000 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07009001 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009002 info.mMatrixDirty = false;
9003 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
9004 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07009005 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009006 }
9007
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01009008 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009009 * Utility method to retrieve the inverse of the current mMatrix property.
9010 * We cache the matrix to avoid recalculating it when transform properties
9011 * have not changed.
9012 *
9013 * @return The inverse of the current matrix of this view.
9014 */
Jeff Brown86671742010-09-30 20:00:15 -07009015 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009016 final TransformationInfo info = mTransformationInfo;
9017 if (info != null) {
9018 updateMatrix();
9019 if (info.mInverseMatrixDirty) {
9020 if (info.mInverseMatrix == null) {
9021 info.mInverseMatrix = new Matrix();
9022 }
9023 info.mMatrix.invert(info.mInverseMatrix);
9024 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07009025 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009026 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07009027 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009028 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07009029 }
9030
9031 /**
Chet Haasea1cff502012-02-21 13:43:44 -08009032 * Gets the distance along the Z axis from the camera to this view.
9033 *
9034 * @see #setCameraDistance(float)
9035 *
9036 * @return The distance along the Z axis.
9037 */
9038 public float getCameraDistance() {
9039 ensureTransformationInfo();
9040 final float dpi = mResources.getDisplayMetrics().densityDpi;
9041 final TransformationInfo info = mTransformationInfo;
9042 if (info.mCamera == null) {
9043 info.mCamera = new Camera();
9044 info.matrix3D = new Matrix();
9045 }
9046 return -(info.mCamera.getLocationZ() * dpi);
9047 }
9048
9049 /**
Romain Guya5364ee2011-02-24 14:46:04 -08009050 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
9051 * views are drawn) from the camera to this view. The camera's distance
9052 * affects 3D transformations, for instance rotations around the X and Y
9053 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07009054 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08009055 * use a camera distance that's greater than the height (X axis rotation) or
9056 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009057 *
Romain Guya5364ee2011-02-24 14:46:04 -08009058 * <p>The distance of the camera from the view plane can have an affect on the
9059 * perspective distortion of the view when it is rotated around the x or y axis.
9060 * For example, a large distance will result in a large viewing angle, and there
9061 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07009062 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08009063 * also result in some drawing artifacts if the rotated view ends up partially
9064 * behind the camera (which is why the recommendation is to use a distance at
9065 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009066 *
Romain Guya5364ee2011-02-24 14:46:04 -08009067 * <p>The distance is expressed in "depth pixels." The default distance depends
9068 * on the screen density. For instance, on a medium density display, the
9069 * default distance is 1280. On a high density display, the default distance
9070 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009071 *
Romain Guya5364ee2011-02-24 14:46:04 -08009072 * <p>If you want to specify a distance that leads to visually consistent
9073 * results across various densities, use the following formula:</p>
9074 * <pre>
9075 * float scale = context.getResources().getDisplayMetrics().density;
9076 * view.setCameraDistance(distance * scale);
9077 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07009078 *
Romain Guya5364ee2011-02-24 14:46:04 -08009079 * <p>The density scale factor of a high density display is 1.5,
9080 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009081 *
Romain Guya5364ee2011-02-24 14:46:04 -08009082 * @param distance The distance in "depth pixels", if negative the opposite
9083 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07009084 *
9085 * @see #setRotationX(float)
9086 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08009087 */
9088 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009089 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08009090
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009091 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08009092 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009093 final TransformationInfo info = mTransformationInfo;
9094 if (info.mCamera == null) {
9095 info.mCamera = new Camera();
9096 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08009097 }
9098
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009099 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
9100 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08009101
Chet Haase9d1992d2012-03-13 11:03:25 -07009102 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009103 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07009104 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08009105 }
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 }
Romain Guya5364ee2011-02-24 14:46:04 -08009110 }
9111
9112 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009113 * The degrees that the view is rotated around the pivot point.
9114 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009115 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07009116 * @see #getPivotX()
9117 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009118 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009119 * @return The degrees of rotation.
9120 */
Chet Haasea5531132012-02-02 13:41:44 -08009121 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009122 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009123 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009124 }
9125
9126 /**
Chet Haase897247b2010-09-09 14:54:47 -07009127 * Sets the degrees that the view is rotated around the pivot point. Increasing values
9128 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07009129 *
9130 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009131 *
9132 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07009133 * @see #getPivotX()
9134 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009135 * @see #setRotationX(float)
9136 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08009137 *
9138 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07009139 */
9140 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009141 ensureTransformationInfo();
9142 final TransformationInfo info = mTransformationInfo;
9143 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009144 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009145 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009146 info.mRotation = rotation;
9147 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009148 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009149 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009150 mDisplayList.setRotation(rotation);
9151 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009152 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009153 // View was rejected last time it was drawn by its parent; this may have changed
9154 invalidateParentIfNeeded();
9155 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009156 }
9157 }
9158
9159 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009160 * The degrees that the view is rotated around the vertical axis through the pivot point.
9161 *
9162 * @see #getPivotX()
9163 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009164 * @see #setRotationY(float)
9165 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009166 * @return The degrees of Y rotation.
9167 */
Chet Haasea5531132012-02-02 13:41:44 -08009168 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009169 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009170 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009171 }
9172
9173 /**
Chet Haase897247b2010-09-09 14:54:47 -07009174 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9175 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9176 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009177 *
Romain Guya5364ee2011-02-24 14:46:04 -08009178 * When rotating large views, it is recommended to adjust the camera distance
9179 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009180 *
9181 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009182 *
9183 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009184 * @see #getPivotX()
9185 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009186 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009187 * @see #setRotationX(float)
9188 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009189 *
9190 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009191 */
9192 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009193 ensureTransformationInfo();
9194 final TransformationInfo info = mTransformationInfo;
9195 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009196 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009197 info.mRotationY = rotationY;
9198 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009199 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009200 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009201 mDisplayList.setRotationY(rotationY);
9202 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009203 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009204 // View was rejected last time it was drawn by its parent; this may have changed
9205 invalidateParentIfNeeded();
9206 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009207 }
9208 }
9209
9210 /**
9211 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9212 *
9213 * @see #getPivotX()
9214 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009215 * @see #setRotationX(float)
9216 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009217 * @return The degrees of X rotation.
9218 */
Chet Haasea5531132012-02-02 13:41:44 -08009219 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009220 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009221 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009222 }
9223
9224 /**
Chet Haase897247b2010-09-09 14:54:47 -07009225 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9226 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9227 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009228 *
Romain Guya5364ee2011-02-24 14:46:04 -08009229 * When rotating large views, it is recommended to adjust the camera distance
9230 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009231 *
9232 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009233 *
9234 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009235 * @see #getPivotX()
9236 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009237 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009238 * @see #setRotationY(float)
9239 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009240 *
9241 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009242 */
9243 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009244 ensureTransformationInfo();
9245 final TransformationInfo info = mTransformationInfo;
9246 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009247 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009248 info.mRotationX = rotationX;
9249 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009250 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009251 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009252 mDisplayList.setRotationX(rotationX);
9253 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009254 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009255 // View was rejected last time it was drawn by its parent; this may have changed
9256 invalidateParentIfNeeded();
9257 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009258 }
9259 }
9260
9261 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009262 * The amount that the view is scaled in x around the pivot point, as a proportion of
9263 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9264 *
Joe Onorato93162322010-09-16 15:42:01 -04009265 * <p>By default, this is 1.0f.
9266 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009267 * @see #getPivotX()
9268 * @see #getPivotY()
9269 * @return The scaling factor.
9270 */
Chet Haasea5531132012-02-02 13:41:44 -08009271 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009272 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009273 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009274 }
9275
9276 /**
9277 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9278 * the view's unscaled width. A value of 1 means that no scaling is applied.
9279 *
9280 * @param scaleX The scaling factor.
9281 * @see #getPivotX()
9282 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009283 *
9284 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009285 */
9286 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009287 ensureTransformationInfo();
9288 final TransformationInfo info = mTransformationInfo;
9289 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009290 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009291 info.mScaleX = scaleX;
9292 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009293 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009294 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009295 mDisplayList.setScaleX(scaleX);
9296 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009297 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009298 // View was rejected last time it was drawn by its parent; this may have changed
9299 invalidateParentIfNeeded();
9300 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009301 }
9302 }
9303
9304 /**
9305 * The amount that the view is scaled in y around the pivot point, as a proportion of
9306 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9307 *
Joe Onorato93162322010-09-16 15:42:01 -04009308 * <p>By default, this is 1.0f.
9309 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009310 * @see #getPivotX()
9311 * @see #getPivotY()
9312 * @return The scaling factor.
9313 */
Chet Haasea5531132012-02-02 13:41:44 -08009314 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009315 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009316 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009317 }
9318
9319 /**
9320 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9321 * the view's unscaled width. A value of 1 means that no scaling is applied.
9322 *
9323 * @param scaleY The scaling factor.
9324 * @see #getPivotX()
9325 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009326 *
9327 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009328 */
9329 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009330 ensureTransformationInfo();
9331 final TransformationInfo info = mTransformationInfo;
9332 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009333 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009334 info.mScaleY = scaleY;
9335 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009336 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009337 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009338 mDisplayList.setScaleY(scaleY);
9339 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009340 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009341 // View was rejected last time it was drawn by its parent; this may have changed
9342 invalidateParentIfNeeded();
9343 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009344 }
9345 }
9346
9347 /**
9348 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9349 * and {@link #setScaleX(float) scaled}.
9350 *
9351 * @see #getRotation()
9352 * @see #getScaleX()
9353 * @see #getScaleY()
9354 * @see #getPivotY()
9355 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009356 *
9357 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009358 */
Chet Haasea5531132012-02-02 13:41:44 -08009359 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009360 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009361 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009362 }
9363
9364 /**
9365 * Sets the x location of the point around which the view is
9366 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009367 * By default, the pivot point is centered on the object.
9368 * Setting this property disables this behavior and causes the view to use only the
9369 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009370 *
9371 * @param pivotX The x location of the pivot point.
9372 * @see #getRotation()
9373 * @see #getScaleX()
9374 * @see #getScaleY()
9375 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009376 *
9377 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009378 */
9379 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009380 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009381 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009382 final TransformationInfo info = mTransformationInfo;
9383 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009384 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009385 info.mPivotX = pivotX;
9386 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009387 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009388 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009389 mDisplayList.setPivotX(pivotX);
9390 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009391 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009392 // View was rejected last time it was drawn by its parent; this may have changed
9393 invalidateParentIfNeeded();
9394 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009395 }
9396 }
9397
9398 /**
9399 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9400 * and {@link #setScaleY(float) scaled}.
9401 *
9402 * @see #getRotation()
9403 * @see #getScaleX()
9404 * @see #getScaleY()
9405 * @see #getPivotY()
9406 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009407 *
9408 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009409 */
Chet Haasea5531132012-02-02 13:41:44 -08009410 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009411 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009412 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009413 }
9414
9415 /**
9416 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009417 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9418 * Setting this property disables this behavior and causes the view to use only the
9419 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009420 *
9421 * @param pivotY The y location of the pivot point.
9422 * @see #getRotation()
9423 * @see #getScaleX()
9424 * @see #getScaleY()
9425 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009426 *
9427 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009428 */
9429 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009430 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009431 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009432 final TransformationInfo info = mTransformationInfo;
9433 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009434 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009435 info.mPivotY = pivotY;
9436 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009437 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009438 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009439 mDisplayList.setPivotY(pivotY);
9440 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009441 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009442 // View was rejected last time it was drawn by its parent; this may have changed
9443 invalidateParentIfNeeded();
9444 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009445 }
9446 }
9447
9448 /**
9449 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9450 * completely transparent and 1 means the view is completely opaque.
9451 *
Joe Onorato93162322010-09-16 15:42:01 -04009452 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009453 * @return The opacity of the view.
9454 */
Chet Haasea5531132012-02-02 13:41:44 -08009455 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009456 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009457 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009458 }
9459
9460 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009461 * Returns whether this View has content which overlaps. This function, intended to be
9462 * overridden by specific View types, is an optimization when alpha is set on a view. If
9463 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9464 * and then composited it into place, which can be expensive. If the view has no overlapping
9465 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9466 * An example of overlapping rendering is a TextView with a background image, such as a
9467 * Button. An example of non-overlapping rendering is a TextView with no background, or
9468 * an ImageView with only the foreground image. The default implementation returns true;
9469 * subclasses should override if they have cases which can be optimized.
9470 *
9471 * @return true if the content in this view might overlap, false otherwise.
9472 */
9473 public boolean hasOverlappingRendering() {
9474 return true;
9475 }
9476
9477 /**
Romain Guy171c5922011-01-06 10:04:23 -08009478 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9479 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009480 *
Chris Craik16ecda52013-03-29 10:59:59 -07009481 * <p> Note that setting alpha to a translucent value (0 < alpha < 1) can have significant
9482 * performance implications, especially for large views. It is best to use the alpha property
9483 * sparingly and transiently, as in the case of fading animations.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009484 *
Chris Craik16ecda52013-03-29 10:59:59 -07009485 * <p>For a view with a frequently changing alpha, such as during a fading animation, it is
9486 * strongly recommended for performance reasons to either override
9487 * {@link #hasOverlappingRendering()} to return false if appropriate, or setting a
9488 * {@link #setLayerType(int, android.graphics.Paint) layer type} on the view.</p>
9489 *
9490 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9491 * responsible for applying the opacity itself.</p>
9492 *
9493 * <p>Note that if the view is backed by a
9494 * {@link #setLayerType(int, android.graphics.Paint) layer} and is associated with a
9495 * {@link #setLayerPaint(android.graphics.Paint) layer paint}, setting an alpha value less than
9496 * 1.0 will supercede the alpha of the layer paint.</p>
Chet Haasea5531132012-02-02 13:41:44 -08009497 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009498 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009499 *
Chris Craik16ecda52013-03-29 10:59:59 -07009500 * @see #hasOverlappingRendering()
Joe Malin32736f02011-01-19 16:14:20 -08009501 * @see #setLayerType(int, android.graphics.Paint)
9502 *
Chet Haase73066682010-11-29 15:55:32 -08009503 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009504 */
9505 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009506 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009507 if (mTransformationInfo.mAlpha != alpha) {
9508 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009509 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009510 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009511 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009512 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009513 invalidate(true);
9514 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009515 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009516 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009517 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009518 mDisplayList.setAlpha(alpha);
9519 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009520 }
Chet Haaseed032702010-10-01 14:05:54 -07009521 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009522 }
9523
9524 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009525 * Faster version of setAlpha() which performs the same steps except there are
9526 * no calls to invalidate(). The caller of this function should perform proper invalidation
9527 * on the parent and this object. The return value indicates whether the subclass handles
9528 * alpha (the return value for onSetAlpha()).
9529 *
9530 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009531 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9532 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009533 */
9534 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009535 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009536 if (mTransformationInfo.mAlpha != alpha) {
9537 mTransformationInfo.mAlpha = alpha;
9538 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9539 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009540 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009541 return true;
9542 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009543 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009544 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009545 mDisplayList.setAlpha(alpha);
9546 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009547 }
Chet Haasea00f3862011-02-22 06:34:40 -08009548 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009549 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009550 }
9551
9552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 * Top position of this view relative to its parent.
9554 *
9555 * @return The top of this view, in pixels.
9556 */
9557 @ViewDebug.CapturedViewProperty
9558 public final int getTop() {
9559 return mTop;
9560 }
9561
9562 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009563 * Sets the top position of this view relative to its parent. This method is meant to be called
9564 * by the layout system and should not generally be called otherwise, because the property
9565 * may be changed at any time by the layout.
9566 *
9567 * @param top The top of this view, in pixels.
9568 */
9569 public final void setTop(int top) {
9570 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009571 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009572 final boolean matrixIsIdentity = mTransformationInfo == null
9573 || mTransformationInfo.mMatrixIsIdentity;
9574 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009575 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009576 int minTop;
9577 int yLoc;
9578 if (top < mTop) {
9579 minTop = top;
9580 yLoc = top - mTop;
9581 } else {
9582 minTop = mTop;
9583 yLoc = 0;
9584 }
Chet Haasee9140a72011-02-16 16:23:29 -08009585 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009586 }
9587 } else {
9588 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009589 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009590 }
9591
Chet Haaseed032702010-10-01 14:05:54 -07009592 int width = mRight - mLeft;
9593 int oldHeight = mBottom - mTop;
9594
Chet Haase21cd1382010-09-01 17:42:29 -07009595 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009596 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009597 mDisplayList.setTop(mTop);
9598 }
Chet Haase21cd1382010-09-01 17:42:29 -07009599
Chet Haase91cedf12013-03-11 07:56:30 -07009600 sizeChange(width, mBottom - mTop, width, oldHeight);
Chet Haaseed032702010-10-01 14:05:54 -07009601
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009602 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009603 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009604 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009605 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009606 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009607 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009608 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009609 }
Chet Haase55dbb652010-12-21 20:15:08 -08009610 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009611 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009612 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009613 // View was rejected last time it was drawn by its parent; this may have changed
9614 invalidateParentIfNeeded();
9615 }
Chet Haase21cd1382010-09-01 17:42:29 -07009616 }
9617 }
9618
9619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 * Bottom position of this view relative to its parent.
9621 *
9622 * @return The bottom of this view, in pixels.
9623 */
9624 @ViewDebug.CapturedViewProperty
9625 public final int getBottom() {
9626 return mBottom;
9627 }
9628
9629 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009630 * True if this view has changed since the last time being drawn.
9631 *
9632 * @return The dirty state of this view.
9633 */
9634 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009635 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009636 }
9637
9638 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009639 * Sets the bottom position of this view relative to its parent. This method is meant to be
9640 * called by the layout system and should not generally be called otherwise, because the
9641 * property may be changed at any time by the layout.
9642 *
9643 * @param bottom The bottom of this view, in pixels.
9644 */
9645 public final void setBottom(int bottom) {
9646 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009647 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009648 final boolean matrixIsIdentity = mTransformationInfo == null
9649 || mTransformationInfo.mMatrixIsIdentity;
9650 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009651 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009652 int maxBottom;
9653 if (bottom < mBottom) {
9654 maxBottom = mBottom;
9655 } else {
9656 maxBottom = bottom;
9657 }
Chet Haasee9140a72011-02-16 16:23:29 -08009658 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009659 }
9660 } else {
9661 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009662 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009663 }
9664
Chet Haaseed032702010-10-01 14:05:54 -07009665 int width = mRight - mLeft;
9666 int oldHeight = mBottom - mTop;
9667
Chet Haase21cd1382010-09-01 17:42:29 -07009668 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009669 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009670 mDisplayList.setBottom(mBottom);
9671 }
Chet Haase21cd1382010-09-01 17:42:29 -07009672
Chet Haase91cedf12013-03-11 07:56:30 -07009673 sizeChange(width, mBottom - mTop, width, oldHeight);
Chet Haaseed032702010-10-01 14:05:54 -07009674
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009675 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009676 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009677 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009678 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009679 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009680 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009681 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009682 }
Chet Haase55dbb652010-12-21 20:15:08 -08009683 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009684 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009685 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009686 // View was rejected last time it was drawn by its parent; this may have changed
9687 invalidateParentIfNeeded();
9688 }
Chet Haase21cd1382010-09-01 17:42:29 -07009689 }
9690 }
9691
9692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 * Left position of this view relative to its parent.
9694 *
9695 * @return The left edge of this view, in pixels.
9696 */
9697 @ViewDebug.CapturedViewProperty
9698 public final int getLeft() {
9699 return mLeft;
9700 }
9701
9702 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009703 * Sets the left position of this view relative to its parent. This method is meant to be called
9704 * by the layout system and should not generally be called otherwise, because the property
9705 * may be changed at any time by the layout.
9706 *
9707 * @param left The bottom of this view, in pixels.
9708 */
9709 public final void setLeft(int left) {
9710 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009711 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009712 final boolean matrixIsIdentity = mTransformationInfo == null
9713 || mTransformationInfo.mMatrixIsIdentity;
9714 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009715 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009716 int minLeft;
9717 int xLoc;
9718 if (left < mLeft) {
9719 minLeft = left;
9720 xLoc = left - mLeft;
9721 } else {
9722 minLeft = mLeft;
9723 xLoc = 0;
9724 }
Chet Haasee9140a72011-02-16 16:23:29 -08009725 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009726 }
9727 } else {
9728 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009729 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009730 }
9731
Chet Haaseed032702010-10-01 14:05:54 -07009732 int oldWidth = mRight - mLeft;
9733 int height = mBottom - mTop;
9734
Chet Haase21cd1382010-09-01 17:42:29 -07009735 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009736 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009737 mDisplayList.setLeft(left);
9738 }
Chet Haase21cd1382010-09-01 17:42:29 -07009739
Chet Haase91cedf12013-03-11 07:56:30 -07009740 sizeChange(mRight - mLeft, height, oldWidth, height);
Chet Haaseed032702010-10-01 14:05:54 -07009741
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009742 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009743 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009744 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009745 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009746 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009747 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009748 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009749 }
Chet Haase55dbb652010-12-21 20:15:08 -08009750 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009751 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009752 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009753 // View was rejected last time it was drawn by its parent; this may have changed
9754 invalidateParentIfNeeded();
9755 }
Chet Haase21cd1382010-09-01 17:42:29 -07009756 }
9757 }
9758
9759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 * Right position of this view relative to its parent.
9761 *
9762 * @return The right edge of this view, in pixels.
9763 */
9764 @ViewDebug.CapturedViewProperty
9765 public final int getRight() {
9766 return mRight;
9767 }
9768
9769 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009770 * Sets the right position of this view relative to its parent. This method is meant to be called
9771 * by the layout system and should not generally be called otherwise, because the property
9772 * may be changed at any time by the layout.
9773 *
9774 * @param right The bottom of this view, in pixels.
9775 */
9776 public final void setRight(int right) {
9777 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009778 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009779 final boolean matrixIsIdentity = mTransformationInfo == null
9780 || mTransformationInfo.mMatrixIsIdentity;
9781 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009782 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009783 int maxRight;
9784 if (right < mRight) {
9785 maxRight = mRight;
9786 } else {
9787 maxRight = right;
9788 }
Chet Haasee9140a72011-02-16 16:23:29 -08009789 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009790 }
9791 } else {
9792 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009793 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009794 }
9795
Chet Haaseed032702010-10-01 14:05:54 -07009796 int oldWidth = mRight - mLeft;
9797 int height = mBottom - mTop;
9798
Chet Haase21cd1382010-09-01 17:42:29 -07009799 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009800 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009801 mDisplayList.setRight(mRight);
9802 }
Chet Haase21cd1382010-09-01 17:42:29 -07009803
Chet Haase91cedf12013-03-11 07:56:30 -07009804 sizeChange(mRight - mLeft, height, oldWidth, height);
Chet Haaseed032702010-10-01 14:05:54 -07009805
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009806 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009807 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009808 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009809 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009810 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009811 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009812 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009813 }
Chet Haase55dbb652010-12-21 20:15:08 -08009814 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009815 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009816 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009817 // View was rejected last time it was drawn by its parent; this may have changed
9818 invalidateParentIfNeeded();
9819 }
Chet Haase21cd1382010-09-01 17:42:29 -07009820 }
9821 }
9822
9823 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009824 * The visual x position of this view, in pixels. This is equivalent to the
9825 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009826 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009827 *
Chet Haasedf030d22010-07-30 17:22:38 -07009828 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009829 */
Chet Haasea5531132012-02-02 13:41:44 -08009830 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009831 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009832 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009833 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009834
Chet Haasedf030d22010-07-30 17:22:38 -07009835 /**
9836 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9837 * {@link #setTranslationX(float) translationX} property to be the difference between
9838 * the x value passed in and the current {@link #getLeft() left} property.
9839 *
9840 * @param x The visual x position of this view, in pixels.
9841 */
9842 public void setX(float x) {
9843 setTranslationX(x - mLeft);
9844 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009845
Chet Haasedf030d22010-07-30 17:22:38 -07009846 /**
9847 * The visual y position of this view, in pixels. This is equivalent to the
9848 * {@link #setTranslationY(float) translationY} property plus the current
9849 * {@link #getTop() top} property.
9850 *
9851 * @return The visual y position of this view, in pixels.
9852 */
Chet Haasea5531132012-02-02 13:41:44 -08009853 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009854 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009855 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009856 }
9857
9858 /**
9859 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9860 * {@link #setTranslationY(float) translationY} property to be the difference between
9861 * the y value passed in and the current {@link #getTop() top} property.
9862 *
9863 * @param y The visual y position of this view, in pixels.
9864 */
9865 public void setY(float y) {
9866 setTranslationY(y - mTop);
9867 }
9868
9869
9870 /**
9871 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9872 * This position is post-layout, in addition to wherever the object's
9873 * layout placed it.
9874 *
9875 * @return The horizontal position of this view relative to its left position, in pixels.
9876 */
Chet Haasea5531132012-02-02 13:41:44 -08009877 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009878 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009879 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009880 }
9881
9882 /**
9883 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9884 * This effectively positions the object post-layout, in addition to wherever the object's
9885 * layout placed it.
9886 *
9887 * @param translationX The horizontal position of this view relative to its left position,
9888 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009889 *
9890 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009891 */
9892 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009893 ensureTransformationInfo();
9894 final TransformationInfo info = mTransformationInfo;
9895 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009896 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009897 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009898 info.mTranslationX = translationX;
9899 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009900 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009901 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009902 mDisplayList.setTranslationX(translationX);
9903 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009904 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009905 // View was rejected last time it was drawn by its parent; this may have changed
9906 invalidateParentIfNeeded();
9907 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009908 }
9909 }
9910
9911 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009912 * The horizontal location of this view relative to its {@link #getTop() top} position.
9913 * This position is post-layout, in addition to wherever the object's
9914 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009915 *
Chet Haasedf030d22010-07-30 17:22:38 -07009916 * @return The vertical position of this view relative to its top position,
9917 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009918 */
Chet Haasea5531132012-02-02 13:41:44 -08009919 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009920 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009921 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009922 }
9923
9924 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009925 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9926 * This effectively positions the object post-layout, in addition to wherever the object's
9927 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009928 *
Chet Haasedf030d22010-07-30 17:22:38 -07009929 * @param translationY The vertical position of this view relative to its top position,
9930 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009931 *
9932 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009933 */
Chet Haasedf030d22010-07-30 17:22:38 -07009934 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009935 ensureTransformationInfo();
9936 final TransformationInfo info = mTransformationInfo;
9937 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009938 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009939 info.mTranslationY = translationY;
9940 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009941 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009942 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009943 mDisplayList.setTranslationY(translationY);
9944 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009945 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009946 // View was rejected last time it was drawn by its parent; this may have changed
9947 invalidateParentIfNeeded();
9948 }
Chet Haasedf030d22010-07-30 17:22:38 -07009949 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009950 }
9951
9952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 * Hit rectangle in parent's coordinates
9954 *
9955 * @param outRect The hit rectangle of the view.
9956 */
9957 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009958 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009959 final TransformationInfo info = mTransformationInfo;
9960 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009961 outRect.set(mLeft, mTop, mRight, mBottom);
9962 } else {
9963 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Chet Haase71c24052013-02-12 07:34:10 -08009964 tmpRect.set(0, 0, getWidth(), getHeight());
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009965 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009966 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9967 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009968 }
9969 }
9970
9971 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009972 * Determines whether the given point, in local coordinates is inside the view.
9973 */
9974 /*package*/ final boolean pointInView(float localX, float localY) {
9975 return localX >= 0 && localX < (mRight - mLeft)
9976 && localY >= 0 && localY < (mBottom - mTop);
9977 }
9978
9979 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009980 * Utility method to determine whether the given point, in local coordinates,
9981 * is inside the view, where the area of the view is expanded by the slop factor.
9982 * This method is called while processing touch-move events to determine if the event
9983 * is still within the view.
9984 */
9985 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009986 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009987 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 }
9989
9990 /**
9991 * When a view has focus and the user navigates away from it, the next view is searched for
9992 * starting from the rectangle filled in by this method.
9993 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009994 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009995 * of the view. However, if your view maintains some idea of internal selection,
9996 * such as a cursor, or a selected row or column, you should override this method and
9997 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009998 *
9999 * @param r The rectangle to fill in, in this view's coordinates.
10000 */
10001 public void getFocusedRect(Rect r) {
10002 getDrawingRect(r);
10003 }
10004
10005 /**
10006 * If some part of this view is not clipped by any of its parents, then
10007 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +010010008 * coordinates (without taking possible View rotations into account), offset
10009 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
10010 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 *
10012 * @param r If true is returned, r holds the global coordinates of the
10013 * visible portion of this view.
10014 * @param globalOffset If true is returned, globalOffset holds the dx,dy
10015 * between this view and its root. globalOffet may be null.
10016 * @return true if r is non-empty (i.e. part of the view is visible at the
10017 * root level.
10018 */
10019 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
10020 int width = mRight - mLeft;
10021 int height = mBottom - mTop;
10022 if (width > 0 && height > 0) {
10023 r.set(0, 0, width, height);
10024 if (globalOffset != null) {
10025 globalOffset.set(-mScrollX, -mScrollY);
10026 }
10027 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
10028 }
10029 return false;
10030 }
10031
10032 public final boolean getGlobalVisibleRect(Rect r) {
10033 return getGlobalVisibleRect(r, null);
10034 }
10035
10036 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -070010037 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 if (getGlobalVisibleRect(r, offset)) {
10039 r.offset(-offset.x, -offset.y); // make r local
10040 return true;
10041 }
10042 return false;
10043 }
10044
10045 /**
10046 * Offset this view's vertical location by the specified number of pixels.
10047 *
10048 * @param offset the number of pixels to offset the view by
10049 */
10050 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -070010051 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -070010052 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010053 final boolean matrixIsIdentity = mTransformationInfo == null
10054 || mTransformationInfo.mMatrixIsIdentity;
10055 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010056 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010057 invalidateViewProperty(false, false);
10058 } else {
10059 final ViewParent p = mParent;
10060 if (p != null && mAttachInfo != null) {
10061 final Rect r = mAttachInfo.mTmpInvalRect;
10062 int minTop;
10063 int maxBottom;
10064 int yLoc;
10065 if (offset < 0) {
10066 minTop = mTop + offset;
10067 maxBottom = mBottom;
10068 yLoc = offset;
10069 } else {
10070 minTop = mTop;
10071 maxBottom = mBottom + offset;
10072 yLoc = 0;
10073 }
10074 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
10075 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -070010076 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010077 }
10078 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -070010079 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -070010080 }
Romain Guy33e72ae2010-07-17 12:40:29 -070010081
Chet Haasec3aa3612010-06-17 08:50:37 -070010082 mTop += offset;
10083 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -070010084 if (mDisplayList != null) {
Romain Guy52036b12013-02-14 18:03:37 -080010085 mDisplayList.offsetTopAndBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -070010086 invalidateViewProperty(false, false);
10087 } else {
10088 if (!matrixIsIdentity) {
10089 invalidateViewProperty(false, true);
10090 }
10091 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -080010092 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 }
10095
10096 /**
10097 * Offset this view's horizontal location by the specified amount of pixels.
10098 *
Chet Haase430742f2013-04-12 11:18:36 -070010099 * @param offset the number of pixels to offset the view by
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 */
10101 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -070010102 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -070010103 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010104 final boolean matrixIsIdentity = mTransformationInfo == null
10105 || mTransformationInfo.mMatrixIsIdentity;
10106 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010107 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010108 invalidateViewProperty(false, false);
10109 } else {
10110 final ViewParent p = mParent;
10111 if (p != null && mAttachInfo != null) {
10112 final Rect r = mAttachInfo.mTmpInvalRect;
10113 int minLeft;
10114 int maxRight;
10115 if (offset < 0) {
10116 minLeft = mLeft + offset;
10117 maxRight = mRight;
10118 } else {
10119 minLeft = mLeft;
10120 maxRight = mRight + offset;
10121 }
10122 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
10123 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -070010124 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010125 }
10126 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -070010127 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -070010128 }
Romain Guy33e72ae2010-07-17 12:40:29 -070010129
Chet Haasec3aa3612010-06-17 08:50:37 -070010130 mLeft += offset;
10131 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -070010132 if (mDisplayList != null) {
Romain Guy52036b12013-02-14 18:03:37 -080010133 mDisplayList.offsetLeftAndRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -070010134 invalidateViewProperty(false, false);
10135 } else {
10136 if (!matrixIsIdentity) {
10137 invalidateViewProperty(false, true);
10138 }
10139 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -080010140 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 }
10143
10144 /**
10145 * Get the LayoutParams associated with this view. All views should have
10146 * layout parameters. These supply parameters to the <i>parent</i> of this
10147 * view specifying how it should be arranged. There are many subclasses of
10148 * ViewGroup.LayoutParams, and these correspond to the different subclasses
10149 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -080010150 *
10151 * This method may return null if this View is not attached to a parent
10152 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
10153 * was not invoked successfully. When a View is attached to a parent
10154 * ViewGroup, this method must not return null.
10155 *
10156 * @return The LayoutParams associated with this view, or null if no
10157 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010158 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010159 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010160 public ViewGroup.LayoutParams getLayoutParams() {
10161 return mLayoutParams;
10162 }
10163
10164 /**
10165 * Set the layout parameters associated with this view. These supply
10166 * parameters to the <i>parent</i> of this view specifying how it should be
10167 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10168 * correspond to the different subclasses of ViewGroup that are responsible
10169 * for arranging their children.
10170 *
Romain Guy01c174b2011-02-22 11:51:06 -080010171 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 */
10173 public void setLayoutParams(ViewGroup.LayoutParams params) {
10174 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010175 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 }
10177 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010178 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010179 if (mParent instanceof ViewGroup) {
10180 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 requestLayout();
10183 }
10184
10185 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010186 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010187 *
10188 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010189 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010190 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010191 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010192 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010193 }
10194 }
10195
10196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010197 * Set the scrolled position of your view. This will cause a call to
10198 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10199 * invalidated.
10200 * @param x the x position to scroll to
10201 * @param y the y position to scroll to
10202 */
10203 public void scrollTo(int x, int y) {
10204 if (mScrollX != x || mScrollY != y) {
10205 int oldX = mScrollX;
10206 int oldY = mScrollY;
10207 mScrollX = x;
10208 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010209 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010211 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010212 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 }
10215 }
10216
10217 /**
10218 * Move the scrolled position of your view. This will cause a call to
10219 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10220 * invalidated.
10221 * @param x the amount of pixels to scroll by horizontally
10222 * @param y the amount of pixels to scroll by vertically
10223 */
10224 public void scrollBy(int x, int y) {
10225 scrollTo(mScrollX + x, mScrollY + y);
10226 }
10227
10228 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010229 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10230 * animation to fade the scrollbars out after a default delay. If a subclass
10231 * provides animated scrolling, the start delay should equal the duration
10232 * of the scrolling animation.</p>
10233 *
10234 * <p>The animation starts only if at least one of the scrollbars is
10235 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10236 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10237 * this method returns true, and false otherwise. If the animation is
10238 * started, this method calls {@link #invalidate()}; in that case the
10239 * caller should not call {@link #invalidate()}.</p>
10240 *
10241 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010242 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010243 *
10244 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10245 * and {@link #scrollTo(int, int)}.</p>
10246 *
10247 * @return true if the animation is played, false otherwise
10248 *
10249 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010250 * @see #scrollBy(int, int)
10251 * @see #scrollTo(int, int)
10252 * @see #isHorizontalScrollBarEnabled()
10253 * @see #isVerticalScrollBarEnabled()
10254 * @see #setHorizontalScrollBarEnabled(boolean)
10255 * @see #setVerticalScrollBarEnabled(boolean)
10256 */
10257 protected boolean awakenScrollBars() {
10258 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010259 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010260 }
10261
10262 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010263 * Trigger the scrollbars to draw.
10264 * This method differs from awakenScrollBars() only in its default duration.
10265 * initialAwakenScrollBars() will show the scroll bars for longer than
10266 * usual to give the user more of a chance to notice them.
10267 *
10268 * @return true if the animation is played, false otherwise.
10269 */
10270 private boolean initialAwakenScrollBars() {
10271 return mScrollCache != null &&
10272 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10273 }
10274
10275 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010276 * <p>
10277 * Trigger the scrollbars to draw. When invoked this method starts an
10278 * animation to fade the scrollbars out after a fixed delay. If a subclass
10279 * provides animated scrolling, the start delay should equal the duration of
10280 * the scrolling animation.
10281 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010282 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010283 * <p>
10284 * The animation starts only if at least one of the scrollbars is enabled,
10285 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10286 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10287 * this method returns true, and false otherwise. If the animation is
10288 * started, this method calls {@link #invalidate()}; in that case the caller
10289 * should not call {@link #invalidate()}.
10290 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010291 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010292 * <p>
10293 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010294 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010295 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010296 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010297 * @param startDelay the delay, in milliseconds, after which the animation
10298 * should start; when the delay is 0, the animation starts
10299 * immediately
10300 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010301 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010302 * @see #scrollBy(int, int)
10303 * @see #scrollTo(int, int)
10304 * @see #isHorizontalScrollBarEnabled()
10305 * @see #isVerticalScrollBarEnabled()
10306 * @see #setHorizontalScrollBarEnabled(boolean)
10307 * @see #setVerticalScrollBarEnabled(boolean)
10308 */
10309 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010310 return awakenScrollBars(startDelay, true);
10311 }
Joe Malin32736f02011-01-19 16:14:20 -080010312
Mike Cleron290947b2009-09-29 18:34:32 -070010313 /**
10314 * <p>
10315 * Trigger the scrollbars to draw. When invoked this method starts an
10316 * animation to fade the scrollbars out after a fixed delay. If a subclass
10317 * provides animated scrolling, the start delay should equal the duration of
10318 * the scrolling animation.
10319 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010320 *
Mike Cleron290947b2009-09-29 18:34:32 -070010321 * <p>
10322 * The animation starts only if at least one of the scrollbars is enabled,
10323 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10324 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10325 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010326 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010327 * is set to true; in that case the caller
10328 * should not call {@link #invalidate()}.
10329 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010330 *
Mike Cleron290947b2009-09-29 18:34:32 -070010331 * <p>
10332 * This method should be invoked everytime a subclass directly updates the
10333 * scroll parameters.
10334 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010335 *
Mike Cleron290947b2009-09-29 18:34:32 -070010336 * @param startDelay the delay, in milliseconds, after which the animation
10337 * should start; when the delay is 0, the animation starts
10338 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010339 *
Mike Cleron290947b2009-09-29 18:34:32 -070010340 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010341 *
Mike Cleron290947b2009-09-29 18:34:32 -070010342 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010343 *
Mike Cleron290947b2009-09-29 18:34:32 -070010344 * @see #scrollBy(int, int)
10345 * @see #scrollTo(int, int)
10346 * @see #isHorizontalScrollBarEnabled()
10347 * @see #isVerticalScrollBarEnabled()
10348 * @see #setHorizontalScrollBarEnabled(boolean)
10349 * @see #setVerticalScrollBarEnabled(boolean)
10350 */
10351 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010352 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010353
Mike Cleronf116bf82009-09-27 19:14:12 -070010354 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10355 return false;
10356 }
10357
10358 if (scrollCache.scrollBar == null) {
10359 scrollCache.scrollBar = new ScrollBarDrawable();
10360 }
10361
10362 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10363
Mike Cleron290947b2009-09-29 18:34:32 -070010364 if (invalidate) {
10365 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010366 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010367 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010368
10369 if (scrollCache.state == ScrollabilityCache.OFF) {
10370 // FIXME: this is copied from WindowManagerService.
10371 // We should get this value from the system when it
10372 // is possible to do so.
10373 final int KEY_REPEAT_FIRST_DELAY = 750;
10374 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10375 }
10376
10377 // Tell mScrollCache when we should start fading. This may
10378 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010379 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010380 scrollCache.fadeStartTime = fadeStartTime;
10381 scrollCache.state = ScrollabilityCache.ON;
10382
10383 // Schedule our fader to run, unscheduling any old ones first
10384 if (mAttachInfo != null) {
10385 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10386 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10387 }
10388
10389 return true;
10390 }
10391
10392 return false;
10393 }
10394
10395 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010396 * Do not invalidate views which are not visible and which are not running an animation. They
10397 * will not get drawn and they should not set dirty flags as if they will be drawn
10398 */
10399 private boolean skipInvalidate() {
10400 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10401 (!(mParent instanceof ViewGroup) ||
10402 !((ViewGroup) mParent).isViewTransitioning(this));
10403 }
10404 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010405 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010406 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10407 * in the future. This must be called from a UI thread. To call from a non-UI
10408 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010409 *
10410 * WARNING: This method is destructive to dirty.
10411 * @param dirty the rectangle representing the bounds of the dirty region
10412 */
10413 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010414 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010415 return;
10416 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010417 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10418 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10419 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10420 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10421 mPrivateFlags |= PFLAG_INVALIDATED;
10422 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 final ViewParent p = mParent;
10424 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010425 //noinspection PointlessBooleanExpression,ConstantConditions
10426 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10427 if (p != null && ai != null && ai.mHardwareAccelerated) {
10428 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010429 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010430 p.invalidateChild(this, null);
10431 return;
10432 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 if (p != null && ai != null) {
10435 final int scrollX = mScrollX;
10436 final int scrollY = mScrollY;
10437 final Rect r = ai.mTmpInvalRect;
10438 r.set(dirty.left - scrollX, dirty.top - scrollY,
10439 dirty.right - scrollX, dirty.bottom - scrollY);
10440 mParent.invalidateChild(this, r);
10441 }
10442 }
10443 }
10444
10445 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010446 * 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 -080010447 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010448 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10449 * will be called at some point in the future. This must be called from
10450 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 * @param l the left position of the dirty region
10452 * @param t the top position of the dirty region
10453 * @param r the right position of the dirty region
10454 * @param b the bottom position of the dirty region
10455 */
10456 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010457 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010458 return;
10459 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010460 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10461 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10462 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10463 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10464 mPrivateFlags |= PFLAG_INVALIDATED;
10465 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 final ViewParent p = mParent;
10467 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010468 //noinspection PointlessBooleanExpression,ConstantConditions
10469 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10470 if (p != null && ai != null && ai.mHardwareAccelerated) {
10471 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010472 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010473 p.invalidateChild(this, null);
10474 return;
10475 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 if (p != null && ai != null && l < r && t < b) {
10478 final int scrollX = mScrollX;
10479 final int scrollY = mScrollY;
10480 final Rect tmpr = ai.mTmpInvalRect;
10481 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10482 p.invalidateChild(this, tmpr);
10483 }
10484 }
10485 }
10486
10487 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010488 * Invalidate the whole view. If the view is visible,
10489 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10490 * the future. This must be called from a UI thread. To call from a non-UI thread,
10491 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 */
10493 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010494 invalidate(true);
10495 }
Joe Malin32736f02011-01-19 16:14:20 -080010496
Chet Haaseed032702010-10-01 14:05:54 -070010497 /**
10498 * This is where the invalidate() work actually happens. A full invalidate()
10499 * causes the drawing cache to be invalidated, but this function can be called with
10500 * invalidateCache set to false to skip that invalidation step for cases that do not
10501 * need it (for example, a component that remains at the same dimensions with the same
10502 * content).
10503 *
10504 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10505 * well. This is usually true for a full invalidate, but may be set to false if the
10506 * View's contents or dimensions have not changed.
10507 */
Romain Guy849d0a32011-02-01 17:20:48 -080010508 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010509 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010510 return;
10511 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010512 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10513 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10514 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010515 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010516 mPrivateFlags &= ~PFLAG_DRAWN;
10517 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010518 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010519 mPrivateFlags |= PFLAG_INVALIDATED;
10520 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010523 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010524 //noinspection PointlessBooleanExpression,ConstantConditions
10525 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10526 if (p != null && ai != null && ai.mHardwareAccelerated) {
10527 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010528 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010529 p.invalidateChild(this, null);
10530 return;
10531 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010532 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 if (p != null && ai != null) {
10535 final Rect r = ai.mTmpInvalRect;
10536 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10537 // Don't call invalidate -- we don't want to internally scroll
10538 // our own bounds
10539 p.invalidateChild(this, r);
10540 }
10541 }
10542 }
10543
10544 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010545 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10546 * set any flags or handle all of the cases handled by the default invalidation methods.
10547 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10548 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10549 * walk up the hierarchy, transforming the dirty rect as necessary.
10550 *
10551 * The method also handles normal invalidation logic if display list properties are not
10552 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10553 * backup approach, to handle these cases used in the various property-setting methods.
10554 *
10555 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10556 * are not being used in this view
10557 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10558 * list properties are not being used in this view
10559 */
10560 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010561 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010562 if (invalidateParent) {
10563 invalidateParentCaches();
10564 }
10565 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010566 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010567 }
10568 invalidate(false);
10569 } else {
10570 final AttachInfo ai = mAttachInfo;
10571 final ViewParent p = mParent;
10572 if (p != null && ai != null) {
10573 final Rect r = ai.mTmpInvalRect;
10574 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10575 if (mParent instanceof ViewGroup) {
10576 ((ViewGroup) mParent).invalidateChildFast(this, r);
10577 } else {
10578 mParent.invalidateChild(this, r);
10579 }
10580 }
10581 }
10582 }
10583
10584 /**
10585 * Utility method to transform a given Rect by the current matrix of this view.
10586 */
10587 void transformRect(final Rect rect) {
10588 if (!getMatrix().isIdentity()) {
10589 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10590 boundingRect.set(rect);
10591 getMatrix().mapRect(boundingRect);
Chet Haase5495c722013-05-14 17:56:01 -070010592 rect.set((int) Math.floor(boundingRect.left),
10593 (int) Math.floor(boundingRect.top),
10594 (int) Math.ceil(boundingRect.right),
10595 (int) Math.ceil(boundingRect.bottom));
Chet Haase9d1992d2012-03-13 11:03:25 -070010596 }
10597 }
10598
10599 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010600 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010601 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10602 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010603 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10604 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010605 *
10606 * @hide
10607 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010608 protected void invalidateParentCaches() {
10609 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010610 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010611 }
10612 }
Joe Malin32736f02011-01-19 16:14:20 -080010613
Romain Guy0fd89bf2011-01-26 15:41:30 -080010614 /**
10615 * Used to indicate that the parent of this view should be invalidated. This functionality
10616 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10617 * which is necessary when various parent-managed properties of the view change, such as
10618 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10619 * an invalidation event to the parent.
10620 *
10621 * @hide
10622 */
10623 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010624 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010625 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010626 }
10627 }
10628
10629 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010630 * Indicates whether this View is opaque. An opaque View guarantees that it will
10631 * draw all the pixels overlapping its bounds using a fully opaque color.
10632 *
10633 * Subclasses of View should override this method whenever possible to indicate
10634 * whether an instance is opaque. Opaque Views are treated in a special way by
10635 * the View hierarchy, possibly allowing it to perform optimizations during
10636 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010637 *
Romain Guy24443ea2009-05-11 11:56:30 -070010638 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010639 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010640 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010641 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010642 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010643 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010644 }
10645
Adam Powell20232d02010-12-08 21:08:53 -080010646 /**
10647 * @hide
10648 */
10649 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010650 // Opaque if:
10651 // - Has a background
10652 // - Background is opaque
Adam Powellf158b522013-03-06 17:45:45 -080010653 // - Doesn't have scrollbars or scrollbars overlay
Romain Guy8f1344f52009-05-15 16:03:59 -070010654
Philip Milne6c8ea062012-04-03 17:38:43 -070010655 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010656 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010657 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010658 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010659 }
10660
10661 final int flags = mViewFlags;
10662 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
Adam Powellf158b522013-03-06 17:45:45 -080010663 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY ||
10664 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_OUTSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010665 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010666 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010667 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010668 }
10669 }
10670
10671 /**
10672 * @hide
10673 */
10674 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010675 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010676 }
10677
10678 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 * @return A handler associated with the thread running the View. This
10680 * handler can be used to pump events in the UI events queue.
10681 */
10682 public Handler getHandler() {
Koji Fukuic6031ab2013-04-01 14:37:01 +090010683 final AttachInfo attachInfo = mAttachInfo;
10684 if (attachInfo != null) {
10685 return attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 }
10687 return null;
10688 }
10689
10690 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010691 * Gets the view root associated with the View.
10692 * @return The view root, or null if none.
10693 * @hide
10694 */
10695 public ViewRootImpl getViewRootImpl() {
10696 if (mAttachInfo != null) {
10697 return mAttachInfo.mViewRootImpl;
10698 }
10699 return null;
10700 }
10701
10702 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010703 * <p>Causes the Runnable to be added to the message queue.
10704 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010705 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010706 * @param action The Runnable that will be executed.
10707 *
10708 * @return Returns true if the Runnable was successfully placed in to the
10709 * message queue. Returns false on failure, usually because the
10710 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010711 *
10712 * @see #postDelayed
10713 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010714 */
10715 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010716 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010717 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010718 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010720 // Assume that post will succeed later
10721 ViewRootImpl.getRunQueue().post(action);
10722 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 }
10724
10725 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010726 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010727 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010728 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010729 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 * @param action The Runnable that will be executed.
10731 * @param delayMillis The delay (in milliseconds) until the Runnable
10732 * will be executed.
10733 *
10734 * @return true if the Runnable was successfully placed in to the
10735 * message queue. Returns false on failure, usually because the
10736 * looper processing the message queue is exiting. Note that a
10737 * result of true does not mean the Runnable will be processed --
10738 * if the looper is quit before the delivery time of the message
10739 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010740 *
10741 * @see #post
10742 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743 */
10744 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010745 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010746 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010747 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010749 // Assume that post will succeed later
10750 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10751 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010752 }
10753
10754 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010755 * <p>Causes the Runnable to execute on the next animation time step.
10756 * The runnable will be run on the user interface thread.</p>
10757 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010758 * @param action The Runnable that will be executed.
10759 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010760 * @see #postOnAnimationDelayed
10761 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010762 */
10763 public void postOnAnimation(Runnable action) {
10764 final AttachInfo attachInfo = mAttachInfo;
10765 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010766 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10767 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010768 } else {
10769 // Assume that post will succeed later
10770 ViewRootImpl.getRunQueue().post(action);
10771 }
10772 }
10773
10774 /**
10775 * <p>Causes the Runnable to execute on the next animation time step,
10776 * after the specified amount of time elapses.
10777 * The runnable will be run on the user interface thread.</p>
10778 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010779 * @param action The Runnable that will be executed.
10780 * @param delayMillis The delay (in milliseconds) until the Runnable
10781 * will be executed.
10782 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010783 * @see #postOnAnimation
10784 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010785 */
10786 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10787 final AttachInfo attachInfo = mAttachInfo;
10788 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010789 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10790 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010791 } else {
10792 // Assume that post will succeed later
10793 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10794 }
10795 }
10796
10797 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010798 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 * @param action The Runnable to remove from the message handling queue
10801 *
10802 * @return true if this view could ask the Handler to remove the Runnable,
10803 * false otherwise. When the returned value is true, the Runnable
10804 * may or may not have been actually removed from the message queue
10805 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010806 *
10807 * @see #post
10808 * @see #postDelayed
10809 * @see #postOnAnimation
10810 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 */
10812 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010813 if (action != null) {
10814 final AttachInfo attachInfo = mAttachInfo;
10815 if (attachInfo != null) {
10816 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010817 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10818 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010819 } else {
10820 // Assume that post will succeed later
10821 ViewRootImpl.getRunQueue().removeCallbacks(action);
10822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010824 return true;
10825 }
10826
10827 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010828 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10829 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010830 *
Romain Guye63a4f32011-08-11 11:33:31 -070010831 * <p>This method can be invoked from outside of the UI thread
10832 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010833 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010835 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 */
10837 public void postInvalidate() {
10838 postInvalidateDelayed(0);
10839 }
10840
10841 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010842 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10843 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010844 *
Romain Guye63a4f32011-08-11 11:33:31 -070010845 * <p>This method can be invoked from outside of the UI thread
10846 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010847 *
10848 * @param left The left coordinate of the rectangle to invalidate.
10849 * @param top The top coordinate of the rectangle to invalidate.
10850 * @param right The right coordinate of the rectangle to invalidate.
10851 * @param bottom The bottom coordinate of the rectangle to invalidate.
10852 *
10853 * @see #invalidate(int, int, int, int)
10854 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010855 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 */
10857 public void postInvalidate(int left, int top, int right, int bottom) {
10858 postInvalidateDelayed(0, left, top, right, bottom);
10859 }
10860
10861 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010862 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10863 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010864 *
Romain Guye63a4f32011-08-11 11:33:31 -070010865 * <p>This method can be invoked from outside of the UI thread
10866 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 *
10868 * @param delayMilliseconds the duration in milliseconds to delay the
10869 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010870 *
10871 * @see #invalidate()
10872 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 */
10874 public void postInvalidateDelayed(long delayMilliseconds) {
10875 // We try only with the AttachInfo because there's no point in invalidating
10876 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010877 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010878 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010879 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 }
10881 }
10882
10883 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010884 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10885 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010886 *
Romain Guye63a4f32011-08-11 11:33:31 -070010887 * <p>This method can be invoked from outside of the UI thread
10888 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 *
10890 * @param delayMilliseconds the duration in milliseconds to delay the
10891 * invalidation by
10892 * @param left The left coordinate of the rectangle to invalidate.
10893 * @param top The top coordinate of the rectangle to invalidate.
10894 * @param right The right coordinate of the rectangle to invalidate.
10895 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010896 *
10897 * @see #invalidate(int, int, int, int)
10898 * @see #invalidate(Rect)
10899 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 */
10901 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10902 int right, int bottom) {
10903
10904 // We try only with the AttachInfo because there's no point in invalidating
10905 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010906 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010907 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010908 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 info.target = this;
10910 info.left = left;
10911 info.top = top;
10912 info.right = right;
10913 info.bottom = bottom;
10914
Jeff Browna175a5b2012-02-15 19:18:31 -080010915 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 }
10917 }
10918
10919 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010920 * <p>Cause an invalidate to happen on the next animation time step, typically the
10921 * next display frame.</p>
10922 *
10923 * <p>This method can be invoked from outside of the UI thread
10924 * only when this View is attached to a window.</p>
10925 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010926 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010927 */
10928 public void postInvalidateOnAnimation() {
10929 // We try only with the AttachInfo because there's no point in invalidating
10930 // if we are not attached to our window
10931 final AttachInfo attachInfo = mAttachInfo;
10932 if (attachInfo != null) {
10933 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10934 }
10935 }
10936
10937 /**
10938 * <p>Cause an invalidate of the specified area to happen on the next animation
10939 * time step, typically the next display frame.</p>
10940 *
10941 * <p>This method can be invoked from outside of the UI thread
10942 * only when this View is attached to a window.</p>
10943 *
10944 * @param left The left coordinate of the rectangle to invalidate.
10945 * @param top The top coordinate of the rectangle to invalidate.
10946 * @param right The right coordinate of the rectangle to invalidate.
10947 * @param bottom The bottom coordinate of the rectangle to invalidate.
10948 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010949 * @see #invalidate(int, int, int, int)
10950 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010951 */
10952 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10953 // We try only with the AttachInfo because there's no point in invalidating
10954 // if we are not attached to our window
10955 final AttachInfo attachInfo = mAttachInfo;
10956 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010957 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010958 info.target = this;
10959 info.left = left;
10960 info.top = top;
10961 info.right = right;
10962 info.bottom = bottom;
10963
10964 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10965 }
10966 }
10967
10968 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010969 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10970 * This event is sent at most once every
10971 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10972 */
10973 private void postSendViewScrolledAccessibilityEventCallback() {
10974 if (mSendViewScrolledAccessibilityEvent == null) {
10975 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10976 }
10977 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10978 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10979 postDelayed(mSendViewScrolledAccessibilityEvent,
10980 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10981 }
10982 }
10983
10984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985 * Called by a parent to request that a child update its values for mScrollX
10986 * and mScrollY if necessary. This will typically be done if the child is
10987 * animating a scroll using a {@link android.widget.Scroller Scroller}
10988 * object.
10989 */
10990 public void computeScroll() {
10991 }
10992
10993 /**
10994 * <p>Indicate whether the horizontal edges are faded when the view is
10995 * scrolled horizontally.</p>
10996 *
10997 * @return true if the horizontal edges should are faded on scroll, false
10998 * otherwise
10999 *
11000 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070011001 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070011002 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 */
11004 public boolean isHorizontalFadingEdgeEnabled() {
11005 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
11006 }
11007
11008 /**
11009 * <p>Define whether the horizontal edges should be faded when this view
11010 * is scrolled horizontally.</p>
11011 *
11012 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
11013 * be faded when the view is scrolled
11014 * horizontally
11015 *
11016 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070011017 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070011018 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011019 */
11020 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
11021 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
11022 if (horizontalFadingEdgeEnabled) {
11023 initScrollCache();
11024 }
11025
11026 mViewFlags ^= FADING_EDGE_HORIZONTAL;
11027 }
11028 }
11029
11030 /**
11031 * <p>Indicate whether the vertical edges are faded when the view is
11032 * scrolled horizontally.</p>
11033 *
11034 * @return true if the vertical edges should are faded on scroll, false
11035 * otherwise
11036 *
11037 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070011038 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070011039 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 */
11041 public boolean isVerticalFadingEdgeEnabled() {
11042 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
11043 }
11044
11045 /**
11046 * <p>Define whether the vertical edges should be faded when this view
11047 * is scrolled vertically.</p>
11048 *
11049 * @param verticalFadingEdgeEnabled true if the vertical edges should
11050 * be faded when the view is scrolled
11051 * vertically
11052 *
11053 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070011054 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070011055 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011056 */
11057 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
11058 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
11059 if (verticalFadingEdgeEnabled) {
11060 initScrollCache();
11061 }
11062
11063 mViewFlags ^= FADING_EDGE_VERTICAL;
11064 }
11065 }
11066
11067 /**
11068 * Returns the strength, or intensity, of the top faded edge. The strength is
11069 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11070 * returns 0.0 or 1.0 but no value in between.
11071 *
11072 * Subclasses should override this method to provide a smoother fade transition
11073 * when scrolling occurs.
11074 *
11075 * @return the intensity of the top fade as a float between 0.0f and 1.0f
11076 */
11077 protected float getTopFadingEdgeStrength() {
11078 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
11079 }
11080
11081 /**
11082 * Returns the strength, or intensity, of the bottom faded edge. The strength is
11083 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11084 * returns 0.0 or 1.0 but no value in between.
11085 *
11086 * Subclasses should override this method to provide a smoother fade transition
11087 * when scrolling occurs.
11088 *
11089 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
11090 */
11091 protected float getBottomFadingEdgeStrength() {
11092 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
11093 computeVerticalScrollRange() ? 1.0f : 0.0f;
11094 }
11095
11096 /**
11097 * Returns the strength, or intensity, of the left faded edge. The strength is
11098 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11099 * returns 0.0 or 1.0 but no value in between.
11100 *
11101 * Subclasses should override this method to provide a smoother fade transition
11102 * when scrolling occurs.
11103 *
11104 * @return the intensity of the left fade as a float between 0.0f and 1.0f
11105 */
11106 protected float getLeftFadingEdgeStrength() {
11107 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
11108 }
11109
11110 /**
11111 * Returns the strength, or intensity, of the right faded edge. The strength is
11112 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11113 * returns 0.0 or 1.0 but no value in between.
11114 *
11115 * Subclasses should override this method to provide a smoother fade transition
11116 * when scrolling occurs.
11117 *
11118 * @return the intensity of the right fade as a float between 0.0f and 1.0f
11119 */
11120 protected float getRightFadingEdgeStrength() {
11121 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
11122 computeHorizontalScrollRange() ? 1.0f : 0.0f;
11123 }
11124
11125 /**
11126 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
11127 * scrollbar is not drawn by default.</p>
11128 *
11129 * @return true if the horizontal scrollbar should be painted, false
11130 * otherwise
11131 *
11132 * @see #setHorizontalScrollBarEnabled(boolean)
11133 */
11134 public boolean isHorizontalScrollBarEnabled() {
11135 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11136 }
11137
11138 /**
11139 * <p>Define whether the horizontal scrollbar should be drawn or not. The
11140 * scrollbar is not drawn by default.</p>
11141 *
11142 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
11143 * be painted
11144 *
11145 * @see #isHorizontalScrollBarEnabled()
11146 */
11147 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
11148 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
11149 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011150 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011151 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 }
11153 }
11154
11155 /**
11156 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
11157 * scrollbar is not drawn by default.</p>
11158 *
11159 * @return true if the vertical scrollbar should be painted, false
11160 * otherwise
11161 *
11162 * @see #setVerticalScrollBarEnabled(boolean)
11163 */
11164 public boolean isVerticalScrollBarEnabled() {
11165 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11166 }
11167
11168 /**
11169 * <p>Define whether the vertical scrollbar should be drawn or not. The
11170 * scrollbar is not drawn by default.</p>
11171 *
11172 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11173 * be painted
11174 *
11175 * @see #isVerticalScrollBarEnabled()
11176 */
11177 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11178 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11179 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011180 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011181 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011182 }
11183 }
11184
Adam Powell20232d02010-12-08 21:08:53 -080011185 /**
11186 * @hide
11187 */
11188 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011189 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011190 }
Joe Malin32736f02011-01-19 16:14:20 -080011191
Mike Cleronfe81d382009-09-28 14:22:16 -070011192 /**
11193 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011194 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011195 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011196 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011197 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011198 */
11199 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11200 initScrollCache();
11201 final ScrollabilityCache scrollabilityCache = mScrollCache;
11202 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011203 if (fadeScrollbars) {
11204 scrollabilityCache.state = ScrollabilityCache.OFF;
11205 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011206 scrollabilityCache.state = ScrollabilityCache.ON;
11207 }
11208 }
Joe Malin32736f02011-01-19 16:14:20 -080011209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 /**
Joe Malin32736f02011-01-19 16:14:20 -080011211 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011212 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011213 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011214 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011215 *
11216 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011217 */
11218 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011219 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011220 }
Joe Malin32736f02011-01-19 16:14:20 -080011221
Mike Cleron52f0a642009-09-28 18:21:37 -070011222 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011223 *
11224 * Returns the delay before scrollbars fade.
11225 *
11226 * @return the delay before scrollbars fade
11227 *
11228 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11229 */
11230 public int getScrollBarDefaultDelayBeforeFade() {
11231 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11232 mScrollCache.scrollBarDefaultDelayBeforeFade;
11233 }
11234
11235 /**
11236 * Define the delay before scrollbars fade.
11237 *
11238 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11239 *
11240 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11241 */
11242 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11243 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11244 }
11245
11246 /**
11247 *
11248 * Returns the scrollbar fade duration.
11249 *
11250 * @return the scrollbar fade duration
11251 *
11252 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11253 */
11254 public int getScrollBarFadeDuration() {
11255 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11256 mScrollCache.scrollBarFadeDuration;
11257 }
11258
11259 /**
11260 * Define the scrollbar fade duration.
11261 *
11262 * @param scrollBarFadeDuration - the scrollbar fade duration
11263 *
11264 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11265 */
11266 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11267 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11268 }
11269
11270 /**
11271 *
11272 * Returns the scrollbar size.
11273 *
11274 * @return the scrollbar size
11275 *
11276 * @attr ref android.R.styleable#View_scrollbarSize
11277 */
11278 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011279 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011280 mScrollCache.scrollBarSize;
11281 }
11282
11283 /**
11284 * Define the scrollbar size.
11285 *
11286 * @param scrollBarSize - the scrollbar size
11287 *
11288 * @attr ref android.R.styleable#View_scrollbarSize
11289 */
11290 public void setScrollBarSize(int scrollBarSize) {
11291 getScrollCache().scrollBarSize = scrollBarSize;
11292 }
11293
11294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011295 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11296 * inset. When inset, they add to the padding of the view. And the scrollbars
11297 * can be drawn inside the padding area or on the edge of the view. For example,
11298 * if a view has a background drawable and you want to draw the scrollbars
11299 * inside the padding specified by the drawable, you can use
11300 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11301 * appear at the edge of the view, ignoring the padding, then you can use
11302 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11303 * @param style the style of the scrollbars. Should be one of
11304 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11305 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11306 * @see #SCROLLBARS_INSIDE_OVERLAY
11307 * @see #SCROLLBARS_INSIDE_INSET
11308 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11309 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011310 *
11311 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011312 */
11313 public void setScrollBarStyle(int style) {
11314 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11315 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011316 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011317 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011318 }
11319 }
11320
11321 /**
11322 * <p>Returns the current scrollbar style.</p>
11323 * @return the current scrollbar style
11324 * @see #SCROLLBARS_INSIDE_OVERLAY
11325 * @see #SCROLLBARS_INSIDE_INSET
11326 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11327 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011328 *
11329 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011330 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011331 @ViewDebug.ExportedProperty(mapping = {
11332 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11333 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11334 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11335 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11336 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 public int getScrollBarStyle() {
11338 return mViewFlags & SCROLLBARS_STYLE_MASK;
11339 }
11340
11341 /**
11342 * <p>Compute the horizontal range that the horizontal scrollbar
11343 * represents.</p>
11344 *
11345 * <p>The range is expressed in arbitrary units that must be the same as the
11346 * units used by {@link #computeHorizontalScrollExtent()} and
11347 * {@link #computeHorizontalScrollOffset()}.</p>
11348 *
11349 * <p>The default range is the drawing width of this view.</p>
11350 *
11351 * @return the total horizontal range represented by the horizontal
11352 * scrollbar
11353 *
11354 * @see #computeHorizontalScrollExtent()
11355 * @see #computeHorizontalScrollOffset()
11356 * @see android.widget.ScrollBarDrawable
11357 */
11358 protected int computeHorizontalScrollRange() {
11359 return getWidth();
11360 }
11361
11362 /**
11363 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11364 * within the horizontal range. This value is used to compute the position
11365 * of the thumb within the scrollbar's track.</p>
11366 *
11367 * <p>The range is expressed in arbitrary units that must be the same as the
11368 * units used by {@link #computeHorizontalScrollRange()} and
11369 * {@link #computeHorizontalScrollExtent()}.</p>
11370 *
11371 * <p>The default offset is the scroll offset of this view.</p>
11372 *
11373 * @return the horizontal offset of the scrollbar's thumb
11374 *
11375 * @see #computeHorizontalScrollRange()
11376 * @see #computeHorizontalScrollExtent()
11377 * @see android.widget.ScrollBarDrawable
11378 */
11379 protected int computeHorizontalScrollOffset() {
11380 return mScrollX;
11381 }
11382
11383 /**
11384 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11385 * within the horizontal range. This value is used to compute the length
11386 * of the thumb within the scrollbar's track.</p>
11387 *
11388 * <p>The range is expressed in arbitrary units that must be the same as the
11389 * units used by {@link #computeHorizontalScrollRange()} and
11390 * {@link #computeHorizontalScrollOffset()}.</p>
11391 *
11392 * <p>The default extent is the drawing width of this view.</p>
11393 *
11394 * @return the horizontal extent of the scrollbar's thumb
11395 *
11396 * @see #computeHorizontalScrollRange()
11397 * @see #computeHorizontalScrollOffset()
11398 * @see android.widget.ScrollBarDrawable
11399 */
11400 protected int computeHorizontalScrollExtent() {
11401 return getWidth();
11402 }
11403
11404 /**
11405 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11406 *
11407 * <p>The range is expressed in arbitrary units that must be the same as the
11408 * units used by {@link #computeVerticalScrollExtent()} and
11409 * {@link #computeVerticalScrollOffset()}.</p>
11410 *
11411 * @return the total vertical range represented by the vertical scrollbar
11412 *
11413 * <p>The default range is the drawing height of this view.</p>
11414 *
11415 * @see #computeVerticalScrollExtent()
11416 * @see #computeVerticalScrollOffset()
11417 * @see android.widget.ScrollBarDrawable
11418 */
11419 protected int computeVerticalScrollRange() {
11420 return getHeight();
11421 }
11422
11423 /**
11424 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11425 * within the horizontal range. This value is used to compute the position
11426 * of the thumb within the scrollbar's track.</p>
11427 *
11428 * <p>The range is expressed in arbitrary units that must be the same as the
11429 * units used by {@link #computeVerticalScrollRange()} and
11430 * {@link #computeVerticalScrollExtent()}.</p>
11431 *
11432 * <p>The default offset is the scroll offset of this view.</p>
11433 *
11434 * @return the vertical offset of the scrollbar's thumb
11435 *
11436 * @see #computeVerticalScrollRange()
11437 * @see #computeVerticalScrollExtent()
11438 * @see android.widget.ScrollBarDrawable
11439 */
11440 protected int computeVerticalScrollOffset() {
11441 return mScrollY;
11442 }
11443
11444 /**
11445 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11446 * within the vertical range. This value is used to compute the length
11447 * of the thumb within the scrollbar's track.</p>
11448 *
11449 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011450 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011451 * {@link #computeVerticalScrollOffset()}.</p>
11452 *
11453 * <p>The default extent is the drawing height of this view.</p>
11454 *
11455 * @return the vertical extent of the scrollbar's thumb
11456 *
11457 * @see #computeVerticalScrollRange()
11458 * @see #computeVerticalScrollOffset()
11459 * @see android.widget.ScrollBarDrawable
11460 */
11461 protected int computeVerticalScrollExtent() {
11462 return getHeight();
11463 }
11464
11465 /**
Adam Powell69159442011-06-13 17:53:06 -070011466 * Check if this view can be scrolled horizontally in a certain direction.
11467 *
11468 * @param direction Negative to check scrolling left, positive to check scrolling right.
11469 * @return true if this view can be scrolled in the specified direction, false otherwise.
11470 */
11471 public boolean canScrollHorizontally(int direction) {
11472 final int offset = computeHorizontalScrollOffset();
11473 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11474 if (range == 0) return false;
11475 if (direction < 0) {
11476 return offset > 0;
11477 } else {
11478 return offset < range - 1;
11479 }
11480 }
11481
11482 /**
11483 * Check if this view can be scrolled vertically in a certain direction.
11484 *
11485 * @param direction Negative to check scrolling up, positive to check scrolling down.
11486 * @return true if this view can be scrolled in the specified direction, false otherwise.
11487 */
11488 public boolean canScrollVertically(int direction) {
11489 final int offset = computeVerticalScrollOffset();
11490 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11491 if (range == 0) return false;
11492 if (direction < 0) {
11493 return offset > 0;
11494 } else {
11495 return offset < range - 1;
11496 }
11497 }
11498
11499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011500 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11501 * scrollbars are painted only if they have been awakened first.</p>
11502 *
11503 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011504 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011505 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011506 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011507 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508 // scrollbars are drawn only when the animation is running
11509 final ScrollabilityCache cache = mScrollCache;
11510 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011511
Mike Cleronf116bf82009-09-27 19:14:12 -070011512 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011513
Mike Cleronf116bf82009-09-27 19:14:12 -070011514 if (state == ScrollabilityCache.OFF) {
11515 return;
11516 }
Joe Malin32736f02011-01-19 16:14:20 -080011517
Mike Cleronf116bf82009-09-27 19:14:12 -070011518 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011519
Mike Cleronf116bf82009-09-27 19:14:12 -070011520 if (state == ScrollabilityCache.FADING) {
11521 // We're fading -- get our fade interpolation
11522 if (cache.interpolatorValues == null) {
11523 cache.interpolatorValues = new float[1];
11524 }
Joe Malin32736f02011-01-19 16:14:20 -080011525
Mike Cleronf116bf82009-09-27 19:14:12 -070011526 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011527
Mike Cleronf116bf82009-09-27 19:14:12 -070011528 // Stops the animation if we're done
11529 if (cache.scrollBarInterpolator.timeToValues(values) ==
11530 Interpolator.Result.FREEZE_END) {
11531 cache.state = ScrollabilityCache.OFF;
11532 } else {
11533 cache.scrollBar.setAlpha(Math.round(values[0]));
11534 }
Joe Malin32736f02011-01-19 16:14:20 -080011535
11536 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011537 // drawing. We only want this when we're fading so that
11538 // we prevent excessive redraws
11539 invalidate = true;
11540 } else {
11541 // We're just on -- but we may have been fading before so
11542 // reset alpha
11543 cache.scrollBar.setAlpha(255);
11544 }
11545
Joe Malin32736f02011-01-19 16:14:20 -080011546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 final int viewFlags = mViewFlags;
11548
11549 final boolean drawHorizontalScrollBar =
11550 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11551 final boolean drawVerticalScrollBar =
11552 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11553 && !isVerticalScrollBarHidden();
11554
11555 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11556 final int width = mRight - mLeft;
11557 final int height = mBottom - mTop;
11558
11559 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011560
Mike Reede8853fc2009-09-04 14:01:48 -040011561 final int scrollX = mScrollX;
11562 final int scrollY = mScrollY;
11563 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11564
Jeff Sharkey47b50332013-03-15 14:46:46 -070011565 int left;
11566 int top;
11567 int right;
11568 int bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011571 int size = scrollBar.getSize(false);
11572 if (size <= 0) {
11573 size = cache.scrollBarSize;
11574 }
11575
Mike Cleronf116bf82009-09-27 19:14:12 -070011576 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011577 computeHorizontalScrollOffset(),
11578 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011579 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011580 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011581 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011582 left = scrollX + (mPaddingLeft & inside);
11583 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11584 bottom = top + size;
11585 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11586 if (invalidate) {
11587 invalidate(left, top, right, bottom);
11588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589 }
11590
11591 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011592 int size = scrollBar.getSize(true);
11593 if (size <= 0) {
11594 size = cache.scrollBarSize;
11595 }
11596
Mike Reede8853fc2009-09-04 14:01:48 -040011597 scrollBar.setParameters(computeVerticalScrollRange(),
11598 computeVerticalScrollOffset(),
11599 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011600 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11601 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11602 verticalScrollbarPosition = isLayoutRtl() ?
11603 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11604 }
11605 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011606 default:
Adam Powell20232d02010-12-08 21:08:53 -080011607 case SCROLLBAR_POSITION_RIGHT:
11608 left = scrollX + width - size - (mUserPaddingRight & inside);
11609 break;
11610 case SCROLLBAR_POSITION_LEFT:
11611 left = scrollX + (mUserPaddingLeft & inside);
11612 break;
11613 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011614 top = scrollY + (mPaddingTop & inside);
11615 right = left + size;
11616 bottom = scrollY + height - (mUserPaddingBottom & inside);
11617 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11618 if (invalidate) {
11619 invalidate(left, top, right, bottom);
11620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011621 }
11622 }
11623 }
11624 }
Romain Guy8506ab42009-06-11 17:35:47 -070011625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011626 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011627 * 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 -080011628 * FastScroller is visible.
11629 * @return whether to temporarily hide the vertical scrollbar
11630 * @hide
11631 */
11632 protected boolean isVerticalScrollBarHidden() {
11633 return false;
11634 }
11635
11636 /**
11637 * <p>Draw the horizontal scrollbar if
11638 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011640 * @param canvas the canvas on which to draw the scrollbar
11641 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011642 *
11643 * @see #isHorizontalScrollBarEnabled()
11644 * @see #computeHorizontalScrollRange()
11645 * @see #computeHorizontalScrollExtent()
11646 * @see #computeHorizontalScrollOffset()
11647 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011648 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011649 */
Romain Guy8fb95422010-08-17 18:38:51 -070011650 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11651 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011652 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011653 scrollBar.draw(canvas);
11654 }
Mike Reede8853fc2009-09-04 14:01:48 -040011655
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011656 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011657 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11658 * returns true.</p>
11659 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011660 * @param canvas the canvas on which to draw the scrollbar
11661 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011662 *
11663 * @see #isVerticalScrollBarEnabled()
11664 * @see #computeVerticalScrollRange()
11665 * @see #computeVerticalScrollExtent()
11666 * @see #computeVerticalScrollOffset()
11667 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011668 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 */
Romain Guy8fb95422010-08-17 18:38:51 -070011670 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11671 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011672 scrollBar.setBounds(l, t, r, b);
11673 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011674 }
11675
11676 /**
11677 * Implement this to do your drawing.
11678 *
11679 * @param canvas the canvas on which the background will be drawn
11680 */
11681 protected void onDraw(Canvas canvas) {
11682 }
11683
11684 /*
11685 * Caller is responsible for calling requestLayout if necessary.
11686 * (This allows addViewInLayout to not request a new layout.)
11687 */
11688 void assignParent(ViewParent parent) {
11689 if (mParent == null) {
11690 mParent = parent;
11691 } else if (parent == null) {
11692 mParent = null;
11693 } else {
11694 throw new RuntimeException("view " + this + " being added, but"
11695 + " it already has a parent");
11696 }
11697 }
11698
11699 /**
11700 * This is called when the view is attached to a window. At this point it
11701 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011702 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11703 * however it may be called any time before the first onDraw -- including
11704 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011705 *
11706 * @see #onDetachedFromWindow()
11707 */
11708 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011709 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011710 mParent.requestTransparentRegion(this);
11711 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011712
Dianne Hackborn4702a852012-08-17 15:18:29 -070011713 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011714 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011715 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011716 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011717
Chet Haasea9b61ac2010-12-20 07:40:25 -080011718 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011719
Svetoslav Ganov42138042012-03-20 11:51:39 -070011720 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011721 if (isFocused()) {
11722 InputMethodManager imm = InputMethodManager.peekInstance();
11723 imm.focusIn(this);
11724 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011725
Romain Guyfbb93fa2012-12-03 18:50:35 -080011726 if (mDisplayList != null) {
Romain Guy52036b12013-02-14 18:03:37 -080011727 mDisplayList.clearDirty();
Romain Guy2a0f2282012-05-08 14:43:12 -070011728 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011729 }
Cibu Johny86666632010-02-22 13:01:02 -080011730
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011731 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011732 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011733 *
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -070011734 * @return true if resolution of RTL properties has been done
11735 *
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011736 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011737 */
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -070011738 public boolean resolveRtlPropertiesIfNeeded() {
11739 if (!needRtlPropertiesResolution()) return false;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011740
11741 // Order is important here: LayoutDirection MUST be resolved first
11742 if (!isLayoutDirectionResolved()) {
11743 resolveLayoutDirection();
11744 resolveLayoutParams();
11745 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011746 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011747 if (!isTextDirectionResolved()) {
11748 resolveTextDirection();
11749 }
11750 if (!isTextAlignmentResolved()) {
11751 resolveTextAlignment();
11752 }
11753 if (!isPaddingResolved()) {
11754 resolvePadding();
11755 }
11756 if (!isDrawablesResolved()) {
11757 resolveDrawables();
11758 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011759 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -070011760 return true;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011761 }
11762
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011763 /**
11764 * Reset resolution of all RTL related properties.
11765 *
11766 * @hide
11767 */
11768 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011769 resetResolvedLayoutDirection();
11770 resetResolvedTextDirection();
11771 resetResolvedTextAlignment();
11772 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011773 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011774 }
11775
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011776 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011777 * @see #onScreenStateChanged(int)
11778 */
11779 void dispatchScreenStateChanged(int screenState) {
11780 onScreenStateChanged(screenState);
11781 }
11782
11783 /**
11784 * This method is called whenever the state of the screen this view is
11785 * attached to changes. A state change will usually occurs when the screen
11786 * turns on or off (whether it happens automatically or the user does it
11787 * manually.)
11788 *
11789 * @param screenState The new state of the screen. Can be either
11790 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11791 */
11792 public void onScreenStateChanged(int screenState) {
11793 }
11794
11795 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011796 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11797 */
11798 private boolean hasRtlSupport() {
11799 return mContext.getApplicationInfo().hasRtlSupport();
11800 }
11801
11802 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011803 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11804 * RTL not supported)
11805 */
11806 private boolean isRtlCompatibilityMode() {
11807 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11808 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11809 }
11810
11811 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011812 * @return true if RTL properties need resolution.
Fabrice Di Meglio09ecb252013-05-03 16:51:55 -070011813 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011814 */
11815 private boolean needRtlPropertiesResolution() {
11816 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11817 }
11818
11819 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011820 * Called when any RTL property (layout direction or text direction or text alignment) has
11821 * been changed.
11822 *
11823 * Subclasses need to override this method to take care of cached information that depends on the
11824 * resolved layout direction, or to inform child views that inherit their layout direction.
11825 *
11826 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011827 *
11828 * @param layoutDirection the direction of the layout
11829 *
11830 * @see #LAYOUT_DIRECTION_LTR
11831 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011832 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011833 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011834 }
11835
11836 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011837 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11838 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011839 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011840 * @return true if resolution has been done, false otherwise.
11841 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011842 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011843 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011844 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011845 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011846 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011847
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011848 if (hasRtlSupport()) {
11849 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011850 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11851 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011852 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011853 // We cannot resolve yet. LTR is by default and let the resolution happen again
11854 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011855 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011856
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011857 // Parent has not yet resolved, LTR is still the default
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080011858 if (!mParent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011859
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080011860 if (mParent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011861 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011862 }
11863 break;
11864 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011865 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011866 break;
11867 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011868 if((LAYOUT_DIRECTION_RTL ==
11869 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011870 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011871 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011872 break;
11873 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011874 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011875 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011876 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011877
11878 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011879 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011880 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011881 }
11882
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011883 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011884 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011885 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011886 * @return true if layout direction resolution can be done otherwise return false.
11887 *
11888 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011889 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011890 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011891 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011892 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080011893 return (mParent != null) && mParent.canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011894 default:
11895 return true;
11896 }
11897 }
11898
11899 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011900 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11901 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011902 *
11903 * @hide
11904 */
11905 public void resetResolvedLayoutDirection() {
11906 // Reset the current resolved bits
11907 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011908 }
11909
11910 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011911 * @return true if the layout direction is inherited.
11912 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011913 * @hide
11914 */
11915 public boolean isLayoutDirectionInherited() {
11916 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11917 }
11918
11919 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011920 * @return true if layout direction has been resolved.
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080011921 * @hide
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011922 */
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080011923 public boolean isLayoutDirectionResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011924 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11925 }
11926
11927 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011928 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011929 *
11930 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011931 */
11932 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011933 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011934 }
11935
11936 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011937 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011938 *
11939 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011940 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011941 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011942 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011943 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11944 // If start / end padding are defined, they will be resolved (hence overriding) to
11945 // left / right or right / left depending on the resolved layout direction.
11946 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011947 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011948 switch (resolvedLayoutDirection) {
11949 case LAYOUT_DIRECTION_RTL:
11950 if (mUserPaddingStart != UNDEFINED_PADDING) {
11951 mUserPaddingRight = mUserPaddingStart;
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -070011952 } else {
11953 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011954 }
11955 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11956 mUserPaddingLeft = mUserPaddingEnd;
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -070011957 } else {
11958 mUserPaddingLeft = mUserPaddingLeftInitial;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011959 }
11960 break;
11961 case LAYOUT_DIRECTION_LTR:
11962 default:
11963 if (mUserPaddingStart != UNDEFINED_PADDING) {
11964 mUserPaddingLeft = mUserPaddingStart;
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -070011965 } else {
11966 mUserPaddingLeft = mUserPaddingLeftInitial;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011967 }
11968 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11969 mUserPaddingRight = mUserPaddingEnd;
Fabrice Di Meglio1ae3b6a2013-04-18 17:48:37 -070011970 } else {
11971 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011972 }
11973 }
11974
11975 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11976
11977 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11978 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011979 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011980 }
11981
Dianne Hackborn4702a852012-08-17 15:18:29 -070011982 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011983 }
11984
11985 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011986 * Reset the resolved layout direction.
11987 *
11988 * @hide
11989 */
11990 public void resetResolvedPadding() {
11991 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11992 }
11993
11994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 * This is called when the view is detached from a window. At this point it
11996 * no longer has a surface for drawing.
11997 *
11998 * @see #onAttachedToWindow()
11999 */
12000 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012001 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080012002
Romain Guya440b002010-02-24 15:57:54 -080012003 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050012004 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080012005 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070012006 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080012007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012008 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012009
Romain Guya998dff2012-03-23 18:58:36 -070012010 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080012011
12012 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070012013 if (mDisplayList != null) {
Romain Guy52036b12013-02-14 18:03:37 -080012014 mDisplayList.markDirty();
Romain Guy2a0f2282012-05-08 14:43:12 -070012015 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070012016 }
Jeff Browna175a5b2012-02-15 19:18:31 -080012017 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070012018 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070012019 // Should never happen
12020 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080012021 }
12022
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080012023 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070012024
Svetoslav Ganov42138042012-03-20 11:51:39 -070012025 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012026 }
12027
12028 /**
12029 * @return The number of times this view has been attached to a window
12030 */
12031 protected int getWindowAttachCount() {
12032 return mWindowAttachCount;
12033 }
12034
12035 /**
12036 * Retrieve a unique token identifying the window this view is attached to.
12037 * @return Return the window's token for use in
12038 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
12039 */
12040 public IBinder getWindowToken() {
12041 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
12042 }
12043
12044 /**
Dianne Hackborne3f23a32013-03-01 13:25:35 -080012045 * Retrieve the {@link WindowId} for the window this view is
12046 * currently attached to.
12047 */
12048 public WindowId getWindowId() {
12049 if (mAttachInfo == null) {
12050 return null;
12051 }
12052 if (mAttachInfo.mWindowId == null) {
12053 try {
12054 mAttachInfo.mIWindowId = mAttachInfo.mSession.getWindowId(
12055 mAttachInfo.mWindowToken);
12056 mAttachInfo.mWindowId = new WindowId(
12057 mAttachInfo.mIWindowId);
12058 } catch (RemoteException e) {
12059 }
12060 }
12061 return mAttachInfo.mWindowId;
12062 }
12063
12064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065 * Retrieve a unique token identifying the top-level "real" window of
12066 * the window that this view is attached to. That is, this is like
12067 * {@link #getWindowToken}, except if the window this view in is a panel
12068 * window (attached to another containing window), then the token of
12069 * the containing window is returned instead.
12070 *
12071 * @return Returns the associated window token, either
12072 * {@link #getWindowToken()} or the containing window's token.
12073 */
12074 public IBinder getApplicationWindowToken() {
12075 AttachInfo ai = mAttachInfo;
12076 if (ai != null) {
12077 IBinder appWindowToken = ai.mPanelParentWindowToken;
12078 if (appWindowToken == null) {
12079 appWindowToken = ai.mWindowToken;
12080 }
12081 return appWindowToken;
12082 }
12083 return null;
12084 }
12085
12086 /**
Jeff Brown98365d72012-08-19 20:30:52 -070012087 * Gets the logical display to which the view's window has been attached.
12088 *
12089 * @return The logical display, or null if the view is not currently attached to a window.
12090 */
12091 public Display getDisplay() {
12092 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
12093 }
12094
12095 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012096 * Retrieve private session object this view hierarchy is using to
12097 * communicate with the window manager.
12098 * @return the session object to communicate with the window manager
12099 */
12100 /*package*/ IWindowSession getWindowSession() {
12101 return mAttachInfo != null ? mAttachInfo.mSession : null;
12102 }
12103
12104 /**
12105 * @param info the {@link android.view.View.AttachInfo} to associated with
12106 * this view
12107 */
12108 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
12109 //System.out.println("Attached! " + this);
12110 mAttachInfo = info;
Chet Haase91cedf12013-03-11 07:56:30 -070012111 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070012112 mOverlay.getOverlayView().dispatchAttachedToWindow(info, visibility);
Chet Haase91cedf12013-03-11 07:56:30 -070012113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012114 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012115 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070012116 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012117 if (mFloatingTreeObserver != null) {
12118 info.mTreeObserver.merge(mFloatingTreeObserver);
12119 mFloatingTreeObserver = null;
12120 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070012121 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012123 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012124 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070012125 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012126 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080012127
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012128 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080012129 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012130 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080012131 if (listeners != null && listeners.size() > 0) {
12132 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
12133 // perform the dispatching. The iterator is a safe guard against listeners that
12134 // could mutate the list by calling the various add/remove methods. This prevents
12135 // the array from being modified while we iterate it.
12136 for (OnAttachStateChangeListener listener : listeners) {
12137 listener.onViewAttachedToWindow(this);
12138 }
12139 }
12140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012141 int vis = info.mWindowVisibility;
12142 if (vis != GONE) {
12143 onWindowVisibilityChanged(vis);
12144 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070012145 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012146 // If nobody has evaluated the drawable state yet, then do it now.
12147 refreshDrawableState();
12148 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070012149 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012150 }
12151
12152 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012153 AttachInfo info = mAttachInfo;
12154 if (info != null) {
12155 int vis = info.mWindowVisibility;
12156 if (vis != GONE) {
12157 onWindowVisibilityChanged(GONE);
12158 }
12159 }
12160
12161 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080012162
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012163 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080012164 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012165 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080012166 if (listeners != null && listeners.size() > 0) {
12167 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
12168 // perform the dispatching. The iterator is a safe guard against listeners that
12169 // could mutate the list by calling the various add/remove methods. This prevents
12170 // the array from being modified while we iterate it.
12171 for (OnAttachStateChangeListener listener : listeners) {
12172 listener.onViewDetachedFromWindow(this);
12173 }
12174 }
12175
Dianne Hackborn4702a852012-08-17 15:18:29 -070012176 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012178 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 }
Romain Guy01d5edc2011-01-28 11:28:53 -080012180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012181 mAttachInfo = null;
Chet Haase91cedf12013-03-11 07:56:30 -070012182 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070012183 mOverlay.getOverlayView().dispatchDetachedFromWindow();
Chet Haase91cedf12013-03-11 07:56:30 -070012184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012185 }
12186
12187 /**
12188 * Store this view hierarchy's frozen state into the given container.
12189 *
12190 * @param container The SparseArray in which to save the view's state.
12191 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012192 * @see #restoreHierarchyState(android.util.SparseArray)
12193 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12194 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012195 */
12196 public void saveHierarchyState(SparseArray<Parcelable> container) {
12197 dispatchSaveInstanceState(container);
12198 }
12199
12200 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012201 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12202 * this view and its children. May be overridden to modify how freezing happens to a
12203 * 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 -080012204 *
12205 * @param container The SparseArray in which to save the view's state.
12206 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012207 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12208 * @see #saveHierarchyState(android.util.SparseArray)
12209 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210 */
12211 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12212 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012213 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012214 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012215 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012216 throw new IllegalStateException(
12217 "Derived class did not call super.onSaveInstanceState()");
12218 }
12219 if (state != null) {
12220 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12221 // + ": " + state);
12222 container.put(mID, state);
12223 }
12224 }
12225 }
12226
12227 /**
12228 * Hook allowing a view to generate a representation of its internal state
12229 * that can later be used to create a new instance with that same state.
12230 * This state should only contain information that is not persistent or can
12231 * not be reconstructed later. For example, you will never store your
12232 * current position on screen because that will be computed again when a
12233 * new instance of the view is placed in its view hierarchy.
12234 * <p>
12235 * Some examples of things you may store here: the current cursor position
12236 * in a text view (but usually not the text itself since that is stored in a
12237 * content provider or other persistent storage), the currently selected
12238 * item in a list view.
12239 *
12240 * @return Returns a Parcelable object containing the view's current dynamic
12241 * state, or null if there is nothing interesting to save. The
12242 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012243 * @see #onRestoreInstanceState(android.os.Parcelable)
12244 * @see #saveHierarchyState(android.util.SparseArray)
12245 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012246 * @see #setSaveEnabled(boolean)
12247 */
12248 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012249 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012250 return BaseSavedState.EMPTY_STATE;
12251 }
12252
12253 /**
12254 * Restore this view hierarchy's frozen state from the given container.
12255 *
12256 * @param container The SparseArray which holds previously frozen states.
12257 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012258 * @see #saveHierarchyState(android.util.SparseArray)
12259 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12260 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012261 */
12262 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12263 dispatchRestoreInstanceState(container);
12264 }
12265
12266 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012267 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12268 * state for this view and its children. May be overridden to modify how restoring
12269 * happens to a view's children; for example, some views may want to not store state
12270 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271 *
12272 * @param container The SparseArray which holds previously saved state.
12273 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012274 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12275 * @see #restoreHierarchyState(android.util.SparseArray)
12276 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012277 */
12278 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12279 if (mID != NO_ID) {
12280 Parcelable state = container.get(mID);
12281 if (state != null) {
12282 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12283 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012284 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012285 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012286 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287 throw new IllegalStateException(
12288 "Derived class did not call super.onRestoreInstanceState()");
12289 }
12290 }
12291 }
12292 }
12293
12294 /**
12295 * Hook allowing a view to re-apply a representation of its internal state that had previously
12296 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12297 * null state.
12298 *
12299 * @param state The frozen state that had previously been returned by
12300 * {@link #onSaveInstanceState}.
12301 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012302 * @see #onSaveInstanceState()
12303 * @see #restoreHierarchyState(android.util.SparseArray)
12304 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012305 */
12306 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012307 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012308 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012309 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12310 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012311 + "when two views of different type have the same id in the same hierarchy. "
12312 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012313 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 }
12315 }
12316
12317 /**
12318 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12319 *
12320 * @return the drawing start time in milliseconds
12321 */
12322 public long getDrawingTime() {
12323 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12324 }
12325
12326 /**
12327 * <p>Enables or disables the duplication of the parent's state into this view. When
12328 * duplication is enabled, this view gets its drawable state from its parent rather
12329 * than from its own internal properties.</p>
12330 *
12331 * <p>Note: in the current implementation, setting this property to true after the
12332 * view was added to a ViewGroup might have no effect at all. This property should
12333 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12334 *
12335 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12336 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012337 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012338 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12339 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012340 *
12341 * @param enabled True to enable duplication of the parent's drawable state, false
12342 * to disable it.
12343 *
12344 * @see #getDrawableState()
12345 * @see #isDuplicateParentStateEnabled()
12346 */
12347 public void setDuplicateParentStateEnabled(boolean enabled) {
12348 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12349 }
12350
12351 /**
12352 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12353 *
12354 * @return True if this view's drawable state is duplicated from the parent,
12355 * false otherwise
12356 *
12357 * @see #getDrawableState()
12358 * @see #setDuplicateParentStateEnabled(boolean)
12359 */
12360 public boolean isDuplicateParentStateEnabled() {
12361 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12362 }
12363
12364 /**
Romain Guy171c5922011-01-06 10:04:23 -080012365 * <p>Specifies the type of layer backing this view. The layer can be
John Spurlock50ef7ab2013-05-29 17:43:16 -040012366 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12367 * {@link #LAYER_TYPE_HARDWARE}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012368 *
Romain Guy171c5922011-01-06 10:04:23 -080012369 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12370 * instance that controls how the layer is composed on screen. The following
12371 * properties of the paint are taken into account when composing the layer:</p>
12372 * <ul>
12373 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12374 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12375 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12376 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012377 *
Romain Guy171c5922011-01-06 10:04:23 -080012378 * <p>If this view has an alpha value set to < 1.0 by calling
Chris Craik16ecda52013-03-29 10:59:59 -070012379 * {@link #setAlpha(float)}, the alpha value of the layer's paint is superceded
12380 * by this view's alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012381 *
Chris Craik16ecda52013-03-29 10:59:59 -070012382 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE},
12383 * {@link #LAYER_TYPE_SOFTWARE} and {@link #LAYER_TYPE_HARDWARE}
Romain Guy171c5922011-01-06 10:04:23 -080012384 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012385 *
Chet Haased15ebf22012-09-05 11:40:29 -070012386 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012387 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12388 * {@link #LAYER_TYPE_HARDWARE}
12389 * @param paint The paint used to compose the layer. This argument is optional
12390 * and can be null. It is ignored when the layer type is
12391 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012392 *
12393 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012394 * @see #LAYER_TYPE_NONE
12395 * @see #LAYER_TYPE_SOFTWARE
12396 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012397 * @see #setAlpha(float)
12398 *
Romain Guy171c5922011-01-06 10:04:23 -080012399 * @attr ref android.R.styleable#View_layerType
12400 */
12401 public void setLayerType(int layerType, Paint paint) {
12402 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012403 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012404 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12405 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012406
Romain Guyd6cd5722011-01-17 14:42:41 -080012407 if (layerType == mLayerType) {
12408 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12409 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012410 invalidateParentCaches();
12411 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012412 }
12413 return;
12414 }
Romain Guy171c5922011-01-06 10:04:23 -080012415
12416 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012417 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012418 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012419 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012420 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012421 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012422 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012423 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012424 default:
12425 break;
Romain Guy171c5922011-01-06 10:04:23 -080012426 }
12427
12428 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012429 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12430 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12431 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012432
Romain Guy0fd89bf2011-01-26 15:41:30 -080012433 invalidateParentCaches();
12434 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012435 }
12436
12437 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012438 * Updates the {@link Paint} object used with the current layer (used only if the current
12439 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12440 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12441 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12442 * ensure that the view gets redrawn immediately.
12443 *
12444 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12445 * instance that controls how the layer is composed on screen. The following
12446 * properties of the paint are taken into account when composing the layer:</p>
12447 * <ul>
12448 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12449 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12450 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12451 * </ul>
12452 *
Chris Craik16ecda52013-03-29 10:59:59 -070012453 * <p>If this view has an alpha value set to < 1.0 by calling {@link #setAlpha(float)}, the
12454 * alpha value of the layer's paint is superceded by this view's alpha value.</p>
Chet Haased15ebf22012-09-05 11:40:29 -070012455 *
12456 * @param paint The paint used to compose the layer. This argument is optional
12457 * and can be null. It is ignored when the layer type is
12458 * {@link #LAYER_TYPE_NONE}
12459 *
12460 * @see #setLayerType(int, android.graphics.Paint)
12461 */
12462 public void setLayerPaint(Paint paint) {
12463 int layerType = getLayerType();
12464 if (layerType != LAYER_TYPE_NONE) {
12465 mLayerPaint = paint == null ? new Paint() : paint;
12466 if (layerType == LAYER_TYPE_HARDWARE) {
12467 HardwareLayer layer = getHardwareLayer();
12468 if (layer != null) {
12469 layer.setLayerPaint(paint);
12470 }
12471 invalidateViewProperty(false, false);
12472 } else {
12473 invalidate();
12474 }
12475 }
12476 }
12477
12478 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012479 * Indicates whether this view has a static layer. A view with layer type
12480 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12481 * dynamic.
12482 */
12483 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012484 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012485 }
12486
12487 /**
Romain Guy171c5922011-01-06 10:04:23 -080012488 * Indicates what type of layer is currently associated with this view. By default
12489 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12490 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12491 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012492 *
Romain Guy171c5922011-01-06 10:04:23 -080012493 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12494 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012495 *
12496 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012497 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012498 * @see #LAYER_TYPE_NONE
12499 * @see #LAYER_TYPE_SOFTWARE
12500 * @see #LAYER_TYPE_HARDWARE
12501 */
12502 public int getLayerType() {
12503 return mLayerType;
12504 }
Joe Malin32736f02011-01-19 16:14:20 -080012505
Romain Guy6c319ca2011-01-11 14:29:25 -080012506 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012507 * Forces this view's layer to be created and this view to be rendered
12508 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12509 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012510 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012511 * This method can for instance be used to render a view into its layer before
12512 * starting an animation. If this view is complex, rendering into the layer
12513 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012514 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012515 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012516 *
12517 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012518 */
12519 public void buildLayer() {
12520 if (mLayerType == LAYER_TYPE_NONE) return;
12521
12522 if (mAttachInfo == null) {
12523 throw new IllegalStateException("This view must be attached to a window first");
12524 }
12525
12526 switch (mLayerType) {
12527 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012528 if (mAttachInfo.mHardwareRenderer != null &&
12529 mAttachInfo.mHardwareRenderer.isEnabled() &&
12530 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012531 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012532 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012533 break;
12534 case LAYER_TYPE_SOFTWARE:
12535 buildDrawingCache(true);
12536 break;
12537 }
12538 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012539
Romain Guyf1ae1062011-03-02 18:16:04 -080012540 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012541 * <p>Returns a hardware layer that can be used to draw this view again
12542 * without executing its draw method.</p>
12543 *
12544 * @return A HardwareLayer ready to render, or null if an error occurred.
12545 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012546 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012547 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12548 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012549 return null;
12550 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012551
Romain Guy9c4b79a2011-11-10 19:23:58 -080012552 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012553
12554 final int width = mRight - mLeft;
12555 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012556
Romain Guy6c319ca2011-01-11 14:29:25 -080012557 if (width == 0 || height == 0) {
12558 return null;
12559 }
12560
Dianne Hackborn4702a852012-08-17 15:18:29 -070012561 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012562 if (mHardwareLayer == null) {
12563 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12564 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012565 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012566 } else {
12567 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012568 if (mHardwareLayer.resize(width, height)) {
12569 mLocalDirtyRect.set(0, 0, width, height);
12570 }
Romain Guy846a5332012-07-11 17:44:57 -070012571 }
12572
12573 // This should not be necessary but applications that change
12574 // the parameters of their background drawable without calling
12575 // this.setBackground(Drawable) can leave the view in a bad state
12576 // (for instance isOpaque() returns true, but the background is
12577 // not opaque.)
12578 computeOpaqueFlags();
12579
12580 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012581 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012582 mHardwareLayer.setOpaque(opaque);
12583 mLocalDirtyRect.set(0, 0, width, height);
12584 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012585 }
12586
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012587 // The layer is not valid if the underlying GPU resources cannot be allocated
12588 if (!mHardwareLayer.isValid()) {
12589 return null;
12590 }
12591
Romain Guy11cb6422012-09-21 00:39:43 -070012592 mHardwareLayer.setLayerPaint(mLayerPaint);
12593 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12594 ViewRootImpl viewRoot = getViewRootImpl();
12595 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12596
Michael Jurka7e52caf2012-03-06 15:57:06 -080012597 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012598 }
12599
12600 return mHardwareLayer;
12601 }
Romain Guy171c5922011-01-06 10:04:23 -080012602
Romain Guy589b0bb2011-10-10 13:57:47 -070012603 /**
12604 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012605 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012606 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012607 *
12608 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012609 * @see #LAYER_TYPE_HARDWARE
12610 */
Romain Guya998dff2012-03-23 18:58:36 -070012611 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012612 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012613 AttachInfo info = mAttachInfo;
12614 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012615 info.mHardwareRenderer.isEnabled() &&
12616 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012617 mHardwareLayer.destroy();
12618 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012619
Chet Haase6a2d17f2012-09-30 12:14:13 -070012620 if (mDisplayList != null) {
12621 mDisplayList.reset();
12622 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012623 invalidate(true);
12624 invalidateParentCaches();
12625 }
Romain Guy65b345f2011-07-27 18:51:50 -070012626 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012627 }
Romain Guy65b345f2011-07-27 18:51:50 -070012628 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012629 }
12630
Romain Guy171c5922011-01-06 10:04:23 -080012631 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012632 * Destroys all hardware rendering resources. This method is invoked
12633 * when the system needs to reclaim resources. Upon execution of this
12634 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012635 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012636 * Note: you <strong>must</strong> call
12637 * <code>super.destroyHardwareResources()</code> when overriding
12638 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012639 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012640 * @hide
12641 */
12642 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012643 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012644 }
12645
12646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012647 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12648 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12649 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12650 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12651 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12652 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012653 *
Romain Guy171c5922011-01-06 10:04:23 -080012654 * <p>Enabling the drawing cache is similar to
12655 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012656 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12657 * drawing cache has no effect on rendering because the system uses a different mechanism
12658 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12659 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12660 * for information on how to enable software and hardware layers.</p>
12661 *
12662 * <p>This API can be used to manually generate
12663 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12664 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012665 *
12666 * @param enabled true to enable the drawing cache, false otherwise
12667 *
12668 * @see #isDrawingCacheEnabled()
12669 * @see #getDrawingCache()
12670 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012671 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012672 */
12673 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012674 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12676 }
12677
12678 /**
12679 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12680 *
12681 * @return true if the drawing cache is enabled
12682 *
12683 * @see #setDrawingCacheEnabled(boolean)
12684 * @see #getDrawingCache()
12685 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012686 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687 public boolean isDrawingCacheEnabled() {
12688 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12689 }
12690
12691 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012692 * Debugging utility which recursively outputs the dirty state of a view and its
12693 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012694 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012695 * @hide
12696 */
Romain Guy676b1732011-02-14 14:45:33 -080012697 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012698 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012699 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12700 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12701 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12702 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012703 if (clear) {
12704 mPrivateFlags &= clearMask;
12705 }
12706 if (this instanceof ViewGroup) {
12707 ViewGroup parent = (ViewGroup) this;
12708 final int count = parent.getChildCount();
12709 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012710 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012711 child.outputDirtyFlags(indent + " ", clear, clearMask);
12712 }
12713 }
12714 }
12715
12716 /**
12717 * This method is used by ViewGroup to cause its children to restore or recreate their
12718 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12719 * to recreate its own display list, which would happen if it went through the normal
12720 * draw/dispatchDraw mechanisms.
12721 *
12722 * @hide
12723 */
12724 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012725
12726 /**
12727 * A view that is not attached or hardware accelerated cannot create a display list.
12728 * This method checks these conditions and returns the appropriate result.
12729 *
12730 * @return true if view has the ability to create a display list, false otherwise.
12731 *
12732 * @hide
12733 */
12734 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012735 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012736 }
Joe Malin32736f02011-01-19 16:14:20 -080012737
Chet Haasedaf98e92011-01-10 14:10:36 -080012738 /**
Romain Guy52036b12013-02-14 18:03:37 -080012739 * @return The {@link HardwareRenderer} associated with that view or null if
12740 * hardware rendering is not supported or this view is not attached
12741 * to a window.
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012742 *
12743 * @hide
12744 */
12745 public HardwareRenderer getHardwareRenderer() {
12746 if (mAttachInfo != null) {
12747 return mAttachInfo.mHardwareRenderer;
12748 }
12749 return null;
12750 }
12751
12752 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012753 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12754 * Otherwise, the same display list will be returned (after having been rendered into
12755 * along the way, depending on the invalidation state of the view).
12756 *
12757 * @param displayList The previous version of this displayList, could be null.
12758 * @param isLayer Whether the requester of the display list is a layer. If so,
12759 * the view will avoid creating a layer inside the resulting display list.
12760 * @return A new or reused DisplayList object.
12761 */
12762 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12763 if (!canHaveDisplayList()) {
12764 return null;
12765 }
12766
Dianne Hackborn4702a852012-08-17 15:18:29 -070012767 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012768 displayList == null || !displayList.isValid() ||
12769 (!isLayer && mRecreateDisplayList))) {
12770 // Don't need to recreate the display list, just need to tell our
12771 // children to restore/recreate theirs
12772 if (displayList != null && displayList.isValid() &&
12773 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012774 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12775 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012776 dispatchGetDisplayList();
12777
12778 return displayList;
12779 }
12780
12781 if (!isLayer) {
12782 // If we got here, we're recreating it. Mark it as such to ensure that
12783 // we copy in child display lists into ours in drawChild()
12784 mRecreateDisplayList = true;
12785 }
12786 if (displayList == null) {
12787 final String name = getClass().getSimpleName();
12788 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12789 // If we're creating a new display list, make sure our parent gets invalidated
12790 // since they will need to recreate their display list to account for this
12791 // new child display list.
12792 invalidateParentCaches();
12793 }
12794
12795 boolean caching = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012796 int width = mRight - mLeft;
12797 int height = mBottom - mTop;
Romain Guy63696bf2013-02-07 15:20:55 -080012798 int layerType = getLayerType();
Chet Haasea1cff502012-02-21 13:43:44 -080012799
Romain Guy52036b12013-02-14 18:03:37 -080012800 final HardwareCanvas canvas = displayList.start(width, height);
12801
Chet Haasea1cff502012-02-21 13:43:44 -080012802 try {
Chet Haase1271e2c2012-04-20 09:54:27 -070012803 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012804 if (layerType == LAYER_TYPE_HARDWARE) {
12805 final HardwareLayer layer = getHardwareLayer();
12806 if (layer != null && layer.isValid()) {
12807 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12808 } else {
12809 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12810 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12811 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12812 }
12813 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012814 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012815 buildDrawingCache(true);
12816 Bitmap cache = getDrawingCache(true);
12817 if (cache != null) {
12818 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12819 caching = true;
12820 }
Chet Haasea1cff502012-02-21 13:43:44 -080012821 }
Chet Haasea1cff502012-02-21 13:43:44 -080012822 } else {
12823
12824 computeScroll();
12825
Chet Haasea1cff502012-02-21 13:43:44 -080012826 canvas.translate(-mScrollX, -mScrollY);
12827 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012828 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12829 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012830 }
12831
12832 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012833 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012834 dispatchDraw(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070012835 if (mOverlay != null && !mOverlay.isEmpty()) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070012836 mOverlay.getOverlayView().draw(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070012837 }
Chet Haasea1cff502012-02-21 13:43:44 -080012838 } else {
12839 draw(canvas);
12840 }
12841 }
12842 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012843 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012844 displayList.setCaching(caching);
12845 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012846 displayList.setLeftTopRightBottom(0, 0, width, height);
12847 } else {
12848 setDisplayListProperties(displayList);
12849 }
12850 }
12851 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012852 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12853 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012854 }
12855
12856 return displayList;
12857 }
12858
12859 /**
12860 * Get the DisplayList for the HardwareLayer
12861 *
12862 * @param layer The HardwareLayer whose DisplayList we want
12863 * @return A DisplayList fopr the specified HardwareLayer
12864 */
12865 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12866 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12867 layer.setDisplayList(displayList);
12868 return displayList;
12869 }
12870
12871
12872 /**
Romain Guyb051e892010-09-28 19:09:36 -070012873 * <p>Returns a display list that can be used to draw this view again
12874 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012875 *
Romain Guyb051e892010-09-28 19:09:36 -070012876 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012877 *
12878 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012879 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012880 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012881 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012882 return mDisplayList;
12883 }
12884
Romain Guy38c2ece2012-05-24 14:20:56 -070012885 private void clearDisplayList() {
12886 if (mDisplayList != null) {
Romain Guy38c2ece2012-05-24 14:20:56 -070012887 mDisplayList.clear();
12888 }
12889 }
12890
Romain Guyb051e892010-09-28 19:09:36 -070012891 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012892 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012893 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012894 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012895 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012896 * @see #getDrawingCache(boolean)
12897 */
12898 public Bitmap getDrawingCache() {
12899 return getDrawingCache(false);
12900 }
12901
12902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12904 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12905 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12906 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12907 * request the drawing cache by calling this method and draw it on screen if the
12908 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012909 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012910 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12911 * this method will create a bitmap of the same size as this view. Because this bitmap
12912 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12913 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12914 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12915 * size than the view. This implies that your application must be able to handle this
12916 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012917 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012918 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12919 * the current density of the screen when the application is in compatibility
12920 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012921 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012922 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012923 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012924 * @see #setDrawingCacheEnabled(boolean)
12925 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012926 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012927 * @see #destroyDrawingCache()
12928 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012929 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012930 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12931 return null;
12932 }
12933 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012934 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012935 }
Romain Guy02890fd2010-08-06 17:58:44 -070012936 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 }
12938
12939 /**
12940 * <p>Frees the resources used by the drawing cache. If you call
12941 * {@link #buildDrawingCache()} manually without calling
12942 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12943 * should cleanup the cache with this method afterwards.</p>
12944 *
12945 * @see #setDrawingCacheEnabled(boolean)
12946 * @see #buildDrawingCache()
12947 * @see #getDrawingCache()
12948 */
12949 public void destroyDrawingCache() {
12950 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012951 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012952 mDrawingCache = null;
12953 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012954 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012955 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012956 mUnscaledDrawingCache = null;
12957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012958 }
12959
12960 /**
12961 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012962 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012963 * view will always be drawn on top of a solid color.
12964 *
12965 * @param color The background color to use for the drawing cache's bitmap
12966 *
12967 * @see #setDrawingCacheEnabled(boolean)
12968 * @see #buildDrawingCache()
12969 * @see #getDrawingCache()
12970 */
12971 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012972 if (color != mDrawingCacheBackgroundColor) {
12973 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012974 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012976 }
12977
12978 /**
12979 * @see #setDrawingCacheBackgroundColor(int)
12980 *
12981 * @return The background color to used for the drawing cache's bitmap
12982 */
12983 public int getDrawingCacheBackgroundColor() {
12984 return mDrawingCacheBackgroundColor;
12985 }
12986
12987 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012988 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012989 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012990 * @see #buildDrawingCache(boolean)
12991 */
12992 public void buildDrawingCache() {
12993 buildDrawingCache(false);
12994 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012995
Romain Guyfbd8f692009-06-26 14:51:58 -070012996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012997 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12998 *
12999 * <p>If you call {@link #buildDrawingCache()} manually without calling
13000 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
13001 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013002 *
Romain Guyfbd8f692009-06-26 14:51:58 -070013003 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
13004 * this method will create a bitmap of the same size as this view. Because this bitmap
13005 * will be drawn scaled by the parent ViewGroup, the result on screen might show
13006 * scaling artifacts. To avoid such artifacts, you should call this method by setting
13007 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
13008 * size than the view. This implies that your application must be able to handle this
13009 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013010 *
Romain Guy0d9275e2010-10-26 14:22:30 -070013011 * <p>You should avoid calling this method when hardware acceleration is enabled. If
13012 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080013013 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070013014 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013015 *
13016 * @see #getDrawingCache()
13017 * @see #destroyDrawingCache()
13018 */
Romain Guyfbd8f692009-06-26 14:51:58 -070013019 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013020 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070013021 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080013022 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013023
Romain Guy8506ab42009-06-11 17:35:47 -070013024 int width = mRight - mLeft;
13025 int height = mBottom - mTop;
13026
13027 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070013028 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070013029
Romain Guye1123222009-06-29 14:24:56 -070013030 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070013031 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
13032 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070013033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013034
13035 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070013036 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080013037 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013038
Romain Guyaf61cc42012-09-30 10:49:15 -070013039 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
13040 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070013041 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
13042 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070013043 if (width > 0 && height > 0) {
13044 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
13045 + projectedBitmapSize + " bytes, only "
13046 + drawingCacheSize + " available");
13047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013048 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080013049 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013050 return;
13051 }
13052
13053 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070013054 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013055
13056 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013057 Bitmap.Config quality;
13058 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080013059 // Never pick ARGB_4444 because it looks awful
13060 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
13062 case DRAWING_CACHE_QUALITY_AUTO:
13063 quality = Bitmap.Config.ARGB_8888;
13064 break;
13065 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080013066 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013067 break;
13068 case DRAWING_CACHE_QUALITY_HIGH:
13069 quality = Bitmap.Config.ARGB_8888;
13070 break;
13071 default:
13072 quality = Bitmap.Config.ARGB_8888;
13073 break;
13074 }
13075 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070013076 // Optimization for translucent windows
13077 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080013078 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013079 }
13080
13081 // Try to cleanup memory
13082 if (bitmap != null) bitmap.recycle();
13083
13084 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070013085 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
13086 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070013087 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070013088 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070013089 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070013090 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070013091 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070013092 }
Adam Powell26153a32010-11-08 15:22:27 -080013093 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013094 } catch (OutOfMemoryError e) {
13095 // If there is not enough memory to create the bitmap cache, just
13096 // ignore the issue as bitmap caches are not required to draw the
13097 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070013098 if (autoScale) {
13099 mDrawingCache = null;
13100 } else {
13101 mUnscaledDrawingCache = null;
13102 }
Romain Guy0211a0a2011-02-14 16:34:59 -080013103 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013104 return;
13105 }
13106
13107 clear = drawingCacheBackgroundColor != 0;
13108 }
13109
13110 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013111 if (attachInfo != null) {
13112 canvas = attachInfo.mCanvas;
13113 if (canvas == null) {
13114 canvas = new Canvas();
13115 }
13116 canvas.setBitmap(bitmap);
13117 // Temporarily clobber the cached Canvas in case one of our children
13118 // is also using a drawing cache. Without this, the children would
13119 // steal the canvas by attaching their own bitmap to it and bad, bad
13120 // thing would happen (invisible views, corrupted drawings, etc.)
13121 attachInfo.mCanvas = null;
13122 } else {
13123 // This case should hopefully never or seldom happen
13124 canvas = new Canvas(bitmap);
13125 }
13126
13127 if (clear) {
13128 bitmap.eraseColor(drawingCacheBackgroundColor);
13129 }
13130
13131 computeScroll();
13132 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080013133
Romain Guye1123222009-06-29 14:24:56 -070013134 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070013135 final float scale = attachInfo.mApplicationScale;
13136 canvas.scale(scale, scale);
13137 }
Joe Malin32736f02011-01-19 16:14:20 -080013138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013139 canvas.translate(-mScrollX, -mScrollY);
13140
Dianne Hackborn4702a852012-08-17 15:18:29 -070013141 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080013142 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
13143 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013144 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070013145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013146
13147 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013148 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13149 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013150 dispatchDraw(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070013151 if (mOverlay != null && !mOverlay.isEmpty()) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070013152 mOverlay.getOverlayView().draw(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070013153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013154 } else {
13155 draw(canvas);
13156 }
13157
13158 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013159 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013160
13161 if (attachInfo != null) {
13162 // Restore the cached Canvas for our siblings
13163 attachInfo.mCanvas = canvas;
13164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013165 }
13166 }
13167
13168 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013169 * Create a snapshot of the view into a bitmap. We should probably make
13170 * some form of this public, but should think about the API.
13171 */
Romain Guy223ff5c2010-03-02 17:07:47 -080013172 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013173 int width = mRight - mLeft;
13174 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013175
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013176 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070013177 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013178 width = (int) ((width * scale) + 0.5f);
13179 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080013180
Dianne Hackborndde331c2012-08-03 14:01:57 -070013181 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
13182 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013183 if (bitmap == null) {
13184 throw new OutOfMemoryError();
13185 }
13186
Romain Guyc529d8d2011-09-06 15:01:39 -070013187 Resources resources = getResources();
13188 if (resources != null) {
13189 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
13190 }
Joe Malin32736f02011-01-19 16:14:20 -080013191
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013192 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013193 if (attachInfo != null) {
13194 canvas = attachInfo.mCanvas;
13195 if (canvas == null) {
13196 canvas = new Canvas();
13197 }
13198 canvas.setBitmap(bitmap);
13199 // Temporarily clobber the cached Canvas in case one of our children
13200 // is also using a drawing cache. Without this, the children would
13201 // steal the canvas by attaching their own bitmap to it and bad, bad
13202 // things would happen (invisible views, corrupted drawings, etc.)
13203 attachInfo.mCanvas = null;
13204 } else {
13205 // This case should hopefully never or seldom happen
13206 canvas = new Canvas(bitmap);
13207 }
13208
Romain Guy5bcdff42009-05-14 21:27:18 -070013209 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013210 bitmap.eraseColor(backgroundColor);
13211 }
13212
13213 computeScroll();
13214 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013215 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013216 canvas.translate(-mScrollX, -mScrollY);
13217
Romain Guy5bcdff42009-05-14 21:27:18 -070013218 // Temporarily remove the dirty mask
13219 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013220 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013221
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013222 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013223 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013224 dispatchDraw(canvas);
13225 } else {
13226 draw(canvas);
13227 }
13228
Romain Guy5bcdff42009-05-14 21:27:18 -070013229 mPrivateFlags = flags;
13230
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013231 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013232 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013233
13234 if (attachInfo != null) {
13235 // Restore the cached Canvas for our siblings
13236 attachInfo.mCanvas = canvas;
13237 }
Romain Guy8506ab42009-06-11 17:35:47 -070013238
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013239 return bitmap;
13240 }
13241
13242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013243 * Indicates whether this View is currently in edit mode. A View is usually
13244 * in edit mode when displayed within a developer tool. For instance, if
13245 * this View is being drawn by a visual user interface builder, this method
13246 * should return true.
13247 *
13248 * Subclasses should check the return value of this method to provide
13249 * different behaviors if their normal behavior might interfere with the
13250 * host environment. For instance: the class spawns a thread in its
13251 * constructor, the drawing code relies on device-specific features, etc.
13252 *
13253 * This method is usually checked in the drawing code of custom widgets.
13254 *
13255 * @return True if this View is in edit mode, false otherwise.
13256 */
13257 public boolean isInEditMode() {
13258 return false;
13259 }
13260
13261 /**
13262 * If the View draws content inside its padding and enables fading edges,
13263 * it needs to support padding offsets. Padding offsets are added to the
13264 * fading edges to extend the length of the fade so that it covers pixels
13265 * drawn inside the padding.
13266 *
13267 * Subclasses of this class should override this method if they need
13268 * to draw content inside the padding.
13269 *
13270 * @return True if padding offset must be applied, false otherwise.
13271 *
13272 * @see #getLeftPaddingOffset()
13273 * @see #getRightPaddingOffset()
13274 * @see #getTopPaddingOffset()
13275 * @see #getBottomPaddingOffset()
13276 *
13277 * @since CURRENT
13278 */
13279 protected boolean isPaddingOffsetRequired() {
13280 return false;
13281 }
13282
13283 /**
13284 * Amount by which to extend the left fading region. Called only when
13285 * {@link #isPaddingOffsetRequired()} returns true.
13286 *
13287 * @return The left padding offset in pixels.
13288 *
13289 * @see #isPaddingOffsetRequired()
13290 *
13291 * @since CURRENT
13292 */
13293 protected int getLeftPaddingOffset() {
13294 return 0;
13295 }
13296
13297 /**
13298 * Amount by which to extend the right fading region. Called only when
13299 * {@link #isPaddingOffsetRequired()} returns true.
13300 *
13301 * @return The right padding offset in pixels.
13302 *
13303 * @see #isPaddingOffsetRequired()
13304 *
13305 * @since CURRENT
13306 */
13307 protected int getRightPaddingOffset() {
13308 return 0;
13309 }
13310
13311 /**
13312 * Amount by which to extend the top fading region. Called only when
13313 * {@link #isPaddingOffsetRequired()} returns true.
13314 *
13315 * @return The top padding offset in pixels.
13316 *
13317 * @see #isPaddingOffsetRequired()
13318 *
13319 * @since CURRENT
13320 */
13321 protected int getTopPaddingOffset() {
13322 return 0;
13323 }
13324
13325 /**
13326 * Amount by which to extend the bottom fading region. Called only when
13327 * {@link #isPaddingOffsetRequired()} returns true.
13328 *
13329 * @return The bottom padding offset in pixels.
13330 *
13331 * @see #isPaddingOffsetRequired()
13332 *
13333 * @since CURRENT
13334 */
13335 protected int getBottomPaddingOffset() {
13336 return 0;
13337 }
13338
13339 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013340 * @hide
13341 * @param offsetRequired
13342 */
13343 protected int getFadeTop(boolean offsetRequired) {
13344 int top = mPaddingTop;
13345 if (offsetRequired) top += getTopPaddingOffset();
13346 return top;
13347 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013348
Romain Guyf2fc4602011-07-19 15:20:03 -070013349 /**
13350 * @hide
13351 * @param offsetRequired
13352 */
13353 protected int getFadeHeight(boolean offsetRequired) {
13354 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013355 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013356 return mBottom - mTop - mPaddingBottom - padding;
13357 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013358
Romain Guyf2fc4602011-07-19 15:20:03 -070013359 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013360 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013361 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013362 *
Romain Guy2bffd262010-09-12 17:40:02 -070013363 * <p>Even if this method returns true, it does not mean that every call
13364 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13365 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013366 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013367 * window is hardware accelerated,
13368 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13369 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013370 *
Romain Guy2bffd262010-09-12 17:40:02 -070013371 * @return True if the view is attached to a window and the window is
13372 * hardware accelerated; false in any other case.
13373 */
13374 public boolean isHardwareAccelerated() {
13375 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13376 }
Joe Malin32736f02011-01-19 16:14:20 -080013377
Romain Guy2bffd262010-09-12 17:40:02 -070013378 /**
Chet Haase430742f2013-04-12 11:18:36 -070013379 * Sets a rectangular area on this view to which the view will be clipped
Chet Haase21f9a362013-04-16 17:21:41 -070013380 * when it is drawn. Setting the value to null will remove the clip bounds
Chet Haase430742f2013-04-12 11:18:36 -070013381 * and the view will draw normally, using its full bounds.
13382 *
13383 * @param clipBounds The rectangular area, in the local coordinates of
13384 * this view, to which future drawing operations will be clipped.
13385 */
13386 public void setClipBounds(Rect clipBounds) {
Chet Haase430742f2013-04-12 11:18:36 -070013387 if (clipBounds != null) {
Chet Haase21f9a362013-04-16 17:21:41 -070013388 if (clipBounds.equals(mClipBounds)) {
13389 return;
13390 }
13391 if (mClipBounds == null) {
13392 invalidate();
13393 mClipBounds = new Rect(clipBounds);
13394 } else {
13395 invalidate(Math.min(mClipBounds.left, clipBounds.left),
13396 Math.min(mClipBounds.top, clipBounds.top),
13397 Math.max(mClipBounds.right, clipBounds.right),
13398 Math.max(mClipBounds.bottom, clipBounds.bottom));
13399 mClipBounds.set(clipBounds);
13400 }
Chet Haase430742f2013-04-12 11:18:36 -070013401 } else {
Chet Haase21f9a362013-04-16 17:21:41 -070013402 if (mClipBounds != null) {
13403 invalidate();
13404 mClipBounds = null;
13405 }
Chet Haase430742f2013-04-12 11:18:36 -070013406 }
13407 }
13408
13409 /**
13410 * Returns a copy of the current {@link #setClipBounds(Rect) clipBounds}.
13411 *
13412 * @return A copy of the current clip bounds if clip bounds are set,
13413 * otherwise null.
13414 */
13415 public Rect getClipBounds() {
13416 return (mClipBounds != null) ? new Rect(mClipBounds) : null;
13417 }
13418
13419 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013420 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13421 * case of an active Animation being run on the view.
13422 */
13423 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13424 Animation a, boolean scalingRequired) {
13425 Transformation invalidationTransform;
13426 final int flags = parent.mGroupFlags;
13427 final boolean initialized = a.isInitialized();
13428 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013429 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013430 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013431 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013432 onAnimationStart();
13433 }
13434
13435 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13436 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13437 if (parent.mInvalidationTransformation == null) {
13438 parent.mInvalidationTransformation = new Transformation();
13439 }
13440 invalidationTransform = parent.mInvalidationTransformation;
13441 a.getTransformation(drawingTime, invalidationTransform, 1f);
13442 } else {
13443 invalidationTransform = parent.mChildTransformation;
13444 }
Romain Guy393a52c2012-05-22 20:21:08 -070013445
Chet Haasebcca79a2012-02-14 08:45:14 -080013446 if (more) {
13447 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013448 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13449 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13450 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13451 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013452 // The child need to draw an animation, potentially offscreen, so
13453 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013454 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013455 parent.invalidate(mLeft, mTop, mRight, mBottom);
13456 }
13457 } else {
13458 if (parent.mInvalidateRegion == null) {
13459 parent.mInvalidateRegion = new RectF();
13460 }
13461 final RectF region = parent.mInvalidateRegion;
13462 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13463 invalidationTransform);
13464
13465 // The child need to draw an animation, potentially offscreen, so
13466 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013467 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013468
13469 final int left = mLeft + (int) region.left;
13470 final int top = mTop + (int) region.top;
13471 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13472 top + (int) (region.height() + .5f));
13473 }
13474 }
13475 return more;
13476 }
13477
Chet Haasea1cff502012-02-21 13:43:44 -080013478 /**
13479 * This method is called by getDisplayList() when a display list is created or re-rendered.
13480 * It sets or resets the current value of all properties on that display list (resetting is
13481 * necessary when a display list is being re-created, because we need to make sure that
13482 * previously-set transform values
13483 */
13484 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013485 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013486 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013487 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013488 if (mParent instanceof ViewGroup) {
Chet Haasedd671592013-04-19 14:54:34 -070013489 displayList.setClipToBounds(
13490 (((ViewGroup) mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
Chet Haasea1cff502012-02-21 13:43:44 -080013491 }
Chet Haase9420abd2012-03-29 16:28:32 -070013492 float alpha = 1;
13493 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13494 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13495 ViewGroup parentVG = (ViewGroup) mParent;
13496 final boolean hasTransform =
13497 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13498 if (hasTransform) {
13499 Transformation transform = parentVG.mChildTransformation;
13500 final int transformType = parentVG.mChildTransformation.getTransformationType();
13501 if (transformType != Transformation.TYPE_IDENTITY) {
13502 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13503 alpha = transform.getAlpha();
13504 }
13505 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
Romain Guy52036b12013-02-14 18:03:37 -080013506 displayList.setMatrix(transform.getMatrix());
Chet Haase9420abd2012-03-29 16:28:32 -070013507 }
13508 }
13509 }
Chet Haasea1cff502012-02-21 13:43:44 -080013510 }
13511 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013512 alpha *= mTransformationInfo.mAlpha;
13513 if (alpha < 1) {
13514 final int multipliedAlpha = (int) (255 * alpha);
13515 if (onSetAlpha(multipliedAlpha)) {
13516 alpha = 1;
13517 }
13518 }
13519 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013520 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13521 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13522 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13523 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013524 if (mTransformationInfo.mCamera == null) {
13525 mTransformationInfo.mCamera = new Camera();
13526 mTransformationInfo.matrix3D = new Matrix();
13527 }
13528 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013529 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013530 displayList.setPivotX(getPivotX());
13531 displayList.setPivotY(getPivotY());
13532 }
Chet Haase9420abd2012-03-29 16:28:32 -070013533 } else if (alpha < 1) {
13534 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013535 }
13536 }
13537 }
13538
Chet Haasebcca79a2012-02-14 08:45:14 -080013539 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013540 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13541 * This draw() method is an implementation detail and is not intended to be overridden or
13542 * to be called from anywhere else other than ViewGroup.drawChild().
13543 */
13544 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013545 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013546 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013547 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013548 final int flags = parent.mGroupFlags;
13549
Chet Haasea1cff502012-02-21 13:43:44 -080013550 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013551 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013552 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013553 }
13554
13555 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013556 boolean concatMatrix = false;
13557
13558 boolean scalingRequired = false;
13559 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013560 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013561
13562 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013563 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13564 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013565 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013566 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013567 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13568 } else {
13569 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13570 }
13571
Chet Haasebcca79a2012-02-14 08:45:14 -080013572 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013573 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013574 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013575 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013576 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013577 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013578 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013579 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013580 } else {
Romain Guy63696bf2013-02-07 15:20:55 -080013581 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) ==
13582 PFLAG3_VIEW_IS_ANIMATING_TRANSFORM && mDisplayList != null) {
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013583 // No longer animating: clear out old animation matrix
13584 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013585 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013586 }
13587 if (!useDisplayListProperties &&
13588 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13589 final boolean hasTransform =
13590 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13591 if (hasTransform) {
13592 final int transformType = parent.mChildTransformation.getTransformationType();
13593 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13594 parent.mChildTransformation : null;
13595 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13596 }
Chet Haase64a48c12012-02-13 16:33:29 -080013597 }
13598 }
13599
13600 concatMatrix |= !childHasIdentityMatrix;
13601
13602 // Sets the flag as early as possible to allow draw() implementations
13603 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013604 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013605
Romain Guyfbb43212012-08-30 15:19:27 -070013606 if (!concatMatrix &&
13607 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13608 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013609 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013610 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13611 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013612 return more;
13613 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013614 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013615
13616 if (hardwareAccelerated) {
13617 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13618 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013619 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13620 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013621 }
13622
Chet Haase64a48c12012-02-13 16:33:29 -080013623 DisplayList displayList = null;
13624 Bitmap cache = null;
13625 boolean hasDisplayList = false;
13626 if (caching) {
13627 if (!hardwareAccelerated) {
13628 if (layerType != LAYER_TYPE_NONE) {
13629 layerType = LAYER_TYPE_SOFTWARE;
13630 buildDrawingCache(true);
13631 }
13632 cache = getDrawingCache(true);
13633 } else {
13634 switch (layerType) {
13635 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013636 if (useDisplayListProperties) {
13637 hasDisplayList = canHaveDisplayList();
13638 } else {
13639 buildDrawingCache(true);
13640 cache = getDrawingCache(true);
13641 }
Chet Haase64a48c12012-02-13 16:33:29 -080013642 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013643 case LAYER_TYPE_HARDWARE:
13644 if (useDisplayListProperties) {
13645 hasDisplayList = canHaveDisplayList();
13646 }
13647 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013648 case LAYER_TYPE_NONE:
13649 // Delay getting the display list until animation-driven alpha values are
13650 // set up and possibly passed on to the view
13651 hasDisplayList = canHaveDisplayList();
13652 break;
13653 }
13654 }
13655 }
Chet Haasea1cff502012-02-21 13:43:44 -080013656 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013657 if (useDisplayListProperties) {
13658 displayList = getDisplayList();
13659 if (!displayList.isValid()) {
13660 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13661 // to getDisplayList(), the display list will be marked invalid and we should not
13662 // try to use it again.
13663 displayList = null;
13664 hasDisplayList = false;
13665 useDisplayListProperties = false;
13666 }
13667 }
Chet Haase64a48c12012-02-13 16:33:29 -080013668
Chet Haase526057b2012-07-12 17:50:41 -070013669 int sx = 0;
13670 int sy = 0;
13671 if (!hasDisplayList) {
13672 computeScroll();
13673 sx = mScrollX;
13674 sy = mScrollY;
13675 }
13676
Chet Haase64a48c12012-02-13 16:33:29 -080013677 final boolean hasNoCache = cache == null || hasDisplayList;
13678 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13679 layerType != LAYER_TYPE_HARDWARE;
13680
Chet Haasea1cff502012-02-21 13:43:44 -080013681 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013682 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013683 restoreTo = canvas.save();
13684 }
Chet Haase64a48c12012-02-13 16:33:29 -080013685 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013686 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013687 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013688 if (!useDisplayListProperties) {
13689 canvas.translate(mLeft, mTop);
13690 }
Chet Haase64a48c12012-02-13 16:33:29 -080013691 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013692 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013693 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013694 restoreTo = canvas.save();
13695 }
Chet Haase64a48c12012-02-13 16:33:29 -080013696 // mAttachInfo cannot be null, otherwise scalingRequired == false
13697 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13698 canvas.scale(scale, scale);
13699 }
13700 }
13701
Chet Haasea1cff502012-02-21 13:43:44 -080013702 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013703 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013704 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013705 if (transformToApply != null || !childHasIdentityMatrix) {
13706 int transX = 0;
13707 int transY = 0;
13708
13709 if (offsetForScroll) {
13710 transX = -sx;
13711 transY = -sy;
13712 }
13713
13714 if (transformToApply != null) {
13715 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013716 if (useDisplayListProperties) {
13717 displayList.setAnimationMatrix(transformToApply.getMatrix());
13718 } else {
13719 // Undo the scroll translation, apply the transformation matrix,
13720 // then redo the scroll translate to get the correct result.
13721 canvas.translate(-transX, -transY);
13722 canvas.concat(transformToApply.getMatrix());
13723 canvas.translate(transX, transY);
13724 }
Chet Haasea1cff502012-02-21 13:43:44 -080013725 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013726 }
13727
13728 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013729 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013730 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013731 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013732 }
13733 }
13734
Chet Haasea1cff502012-02-21 13:43:44 -080013735 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013736 canvas.translate(-transX, -transY);
13737 canvas.concat(getMatrix());
13738 canvas.translate(transX, transY);
13739 }
13740 }
13741
Chet Haase21433372012-06-05 07:54:09 -070013742 // Deal with alpha if it is or used to be <1
13743 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013744 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013745 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013746 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013747 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013748 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013749 }
Chet Haasea1cff502012-02-21 13:43:44 -080013750 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013751 if (hasNoCache) {
13752 final int multipliedAlpha = (int) (255 * alpha);
13753 if (!onSetAlpha(multipliedAlpha)) {
13754 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013755 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013756 layerType != LAYER_TYPE_NONE) {
13757 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13758 }
Chet Haase9420abd2012-03-29 16:28:32 -070013759 if (useDisplayListProperties) {
13760 displayList.setAlpha(alpha * getAlpha());
13761 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013762 final int scrollX = hasDisplayList ? 0 : sx;
13763 final int scrollY = hasDisplayList ? 0 : sy;
13764 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13765 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013766 }
13767 } else {
13768 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013769 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013770 }
13771 }
13772 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013773 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013774 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013775 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013776 }
13777
Chet Haasea1cff502012-02-21 13:43:44 -080013778 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chris Craikeaddac62013-04-25 15:32:40 -070013779 !useDisplayListProperties && cache == null) {
Chet Haase64a48c12012-02-13 16:33:29 -080013780 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013781 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013782 } else {
13783 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013784 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013785 } else {
13786 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13787 }
13788 }
13789 }
13790
Chet Haase9420abd2012-03-29 16:28:32 -070013791 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013792 displayList = getDisplayList();
13793 if (!displayList.isValid()) {
13794 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13795 // to getDisplayList(), the display list will be marked invalid and we should not
13796 // try to use it again.
13797 displayList = null;
13798 hasDisplayList = false;
13799 }
13800 }
13801
13802 if (hasNoCache) {
13803 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013804 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013805 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013806 if (layer != null && layer.isValid()) {
13807 mLayerPaint.setAlpha((int) (alpha * 255));
13808 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13809 layerRendered = true;
13810 } else {
13811 final int scrollX = hasDisplayList ? 0 : sx;
13812 final int scrollY = hasDisplayList ? 0 : sy;
13813 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013814 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013815 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13816 }
13817 }
13818
13819 if (!layerRendered) {
13820 if (!hasDisplayList) {
13821 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013822 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13823 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013824 dispatchDraw(canvas);
13825 } else {
13826 draw(canvas);
13827 }
13828 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013829 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013830 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013831 }
13832 }
13833 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013834 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013835 Paint cachePaint;
13836
13837 if (layerType == LAYER_TYPE_NONE) {
13838 cachePaint = parent.mCachePaint;
13839 if (cachePaint == null) {
13840 cachePaint = new Paint();
13841 cachePaint.setDither(false);
13842 parent.mCachePaint = cachePaint;
13843 }
Chet Haase9420abd2012-03-29 16:28:32 -070013844 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013845 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013846 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13847 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013848 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013849 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013850 }
13851 } else {
13852 cachePaint = mLayerPaint;
13853 cachePaint.setAlpha((int) (alpha * 255));
13854 }
13855 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13856 }
13857
Chet Haasea1cff502012-02-21 13:43:44 -080013858 if (restoreTo >= 0) {
13859 canvas.restoreToCount(restoreTo);
13860 }
Chet Haase64a48c12012-02-13 16:33:29 -080013861
13862 if (a != null && !more) {
13863 if (!hardwareAccelerated && !a.getFillAfter()) {
13864 onSetAlpha(255);
13865 }
13866 parent.finishAnimatingView(this, a);
13867 }
13868
13869 if (more && hardwareAccelerated) {
13870 // invalidation is the trigger to recreate display lists, so if we're using
13871 // display lists to render, force an invalidate to allow the animation to
13872 // continue drawing another frame
13873 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013874 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013875 // alpha animations should cause the child to recreate its display list
13876 invalidate(true);
13877 }
13878 }
13879
13880 mRecreateDisplayList = false;
13881
13882 return more;
13883 }
13884
13885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013886 * Manually render this view (and all of its children) to the given Canvas.
13887 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013888 * called. When implementing a view, implement
13889 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13890 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013891 *
13892 * @param canvas The Canvas to which the View is rendered.
13893 */
13894 public void draw(Canvas canvas) {
Chet Haase430742f2013-04-12 11:18:36 -070013895 if (mClipBounds != null) {
13896 canvas.clipRect(mClipBounds);
13897 }
Romain Guy5bcdff42009-05-14 21:27:18 -070013898 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013899 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013900 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013901 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 /*
13904 * Draw traversal performs several drawing steps which must be executed
13905 * in the appropriate order:
13906 *
13907 * 1. Draw the background
13908 * 2. If necessary, save the canvas' layers to prepare for fading
13909 * 3. Draw view's content
13910 * 4. Draw children
13911 * 5. If necessary, draw the fading edges and restore layers
13912 * 6. Draw decorations (scrollbars for instance)
13913 */
13914
13915 // Step 1, draw the background, if needed
13916 int saveCount;
13917
Romain Guy24443ea2009-05-11 11:56:30 -070013918 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013919 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013920 if (background != null) {
13921 final int scrollX = mScrollX;
13922 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923
Romain Guy24443ea2009-05-11 11:56:30 -070013924 if (mBackgroundSizeChanged) {
13925 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13926 mBackgroundSizeChanged = false;
13927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013928
Romain Guy24443ea2009-05-11 11:56:30 -070013929 if ((scrollX | scrollY) == 0) {
13930 background.draw(canvas);
13931 } else {
13932 canvas.translate(scrollX, scrollY);
13933 background.draw(canvas);
13934 canvas.translate(-scrollX, -scrollY);
13935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013936 }
13937 }
13938
13939 // skip step 2 & 5 if possible (common case)
13940 final int viewFlags = mViewFlags;
13941 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13942 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13943 if (!verticalEdges && !horizontalEdges) {
13944 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013945 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013946
13947 // Step 4, draw the children
13948 dispatchDraw(canvas);
13949
13950 // Step 6, draw decorations (scrollbars)
13951 onDrawScrollBars(canvas);
13952
Chet Haase91cedf12013-03-11 07:56:30 -070013953 if (mOverlay != null && !mOverlay.isEmpty()) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070013954 mOverlay.getOverlayView().dispatchDraw(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070013955 }
13956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013957 // we're done...
13958 return;
13959 }
13960
13961 /*
13962 * Here we do the full fledged routine...
13963 * (this is an uncommon case where speed matters less,
13964 * this is why we repeat some of the tests that have been
13965 * done above)
13966 */
13967
13968 boolean drawTop = false;
13969 boolean drawBottom = false;
13970 boolean drawLeft = false;
13971 boolean drawRight = false;
13972
13973 float topFadeStrength = 0.0f;
13974 float bottomFadeStrength = 0.0f;
13975 float leftFadeStrength = 0.0f;
13976 float rightFadeStrength = 0.0f;
13977
13978 // Step 2, save the canvas' layers
13979 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013980
13981 final boolean offsetRequired = isPaddingOffsetRequired();
13982 if (offsetRequired) {
13983 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013984 }
13985
13986 int left = mScrollX + paddingLeft;
13987 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013988 int top = mScrollY + getFadeTop(offsetRequired);
13989 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013990
13991 if (offsetRequired) {
13992 right += getRightPaddingOffset();
13993 bottom += getBottomPaddingOffset();
13994 }
13995
13996 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013997 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013998 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999
14000 // clip the fade length if top and bottom fades overlap
14001 // overlapping fades produce odd-looking artifacts
14002 if (verticalEdges && (top + length > bottom - length)) {
14003 length = (bottom - top) / 2;
14004 }
14005
14006 // also clip horizontal fades if necessary
14007 if (horizontalEdges && (left + length > right - length)) {
14008 length = (right - left) / 2;
14009 }
14010
14011 if (verticalEdges) {
14012 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014013 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014015 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014016 }
14017
14018 if (horizontalEdges) {
14019 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014020 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014021 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014022 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014023 }
14024
14025 saveCount = canvas.getSaveCount();
14026
14027 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070014028 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014029 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
14030
14031 if (drawTop) {
14032 canvas.saveLayer(left, top, right, top + length, null, flags);
14033 }
14034
14035 if (drawBottom) {
14036 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
14037 }
14038
14039 if (drawLeft) {
14040 canvas.saveLayer(left, top, left + length, bottom, null, flags);
14041 }
14042
14043 if (drawRight) {
14044 canvas.saveLayer(right - length, top, right, bottom, null, flags);
14045 }
14046 } else {
14047 scrollabilityCache.setFadeColor(solidColor);
14048 }
14049
14050 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070014051 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052
14053 // Step 4, draw the children
14054 dispatchDraw(canvas);
14055
14056 // Step 5, draw the fade effect and restore layers
14057 final Paint p = scrollabilityCache.paint;
14058 final Matrix matrix = scrollabilityCache.matrix;
14059 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060
14061 if (drawTop) {
14062 matrix.setScale(1, fadeHeight * topFadeStrength);
14063 matrix.postTranslate(left, top);
14064 fade.setLocalMatrix(matrix);
14065 canvas.drawRect(left, top, right, top + length, p);
14066 }
14067
14068 if (drawBottom) {
14069 matrix.setScale(1, fadeHeight * bottomFadeStrength);
14070 matrix.postRotate(180);
14071 matrix.postTranslate(left, bottom);
14072 fade.setLocalMatrix(matrix);
14073 canvas.drawRect(left, bottom - length, right, bottom, p);
14074 }
14075
14076 if (drawLeft) {
14077 matrix.setScale(1, fadeHeight * leftFadeStrength);
14078 matrix.postRotate(-90);
14079 matrix.postTranslate(left, top);
14080 fade.setLocalMatrix(matrix);
14081 canvas.drawRect(left, top, left + length, bottom, p);
14082 }
14083
14084 if (drawRight) {
14085 matrix.setScale(1, fadeHeight * rightFadeStrength);
14086 matrix.postRotate(90);
14087 matrix.postTranslate(right, top);
14088 fade.setLocalMatrix(matrix);
14089 canvas.drawRect(right - length, top, right, bottom, p);
14090 }
14091
14092 canvas.restoreToCount(saveCount);
14093
14094 // Step 6, draw decorations (scrollbars)
14095 onDrawScrollBars(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070014096
14097 if (mOverlay != null && !mOverlay.isEmpty()) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070014098 mOverlay.getOverlayView().dispatchDraw(canvas);
Chet Haase91cedf12013-03-11 07:56:30 -070014099 }
14100 }
14101
14102 /**
Chet Haaseedf6f4b2013-03-26 07:55:30 -070014103 * Returns the overlay for this view, creating it if it does not yet exist.
14104 * Adding drawables to the overlay will cause them to be displayed whenever
14105 * the view itself is redrawn. Objects in the overlay should be actively
14106 * managed: remove them when they should not be displayed anymore. The
14107 * overlay will always have the same size as its host view.
14108 *
Chet Haase95399492013-04-08 14:30:31 -070014109 * <p>Note: Overlays do not currently work correctly with {@link
14110 * SurfaceView} or {@link TextureView}; contents in overlays for these
14111 * types of views may not display correctly.</p>
14112 *
Chet Haaseedf6f4b2013-03-26 07:55:30 -070014113 * @return The ViewOverlay object for this view.
14114 * @see ViewOverlay
Chet Haase91cedf12013-03-11 07:56:30 -070014115 */
Chet Haaseedf6f4b2013-03-26 07:55:30 -070014116 public ViewOverlay getOverlay() {
Chet Haase91cedf12013-03-11 07:56:30 -070014117 if (mOverlay == null) {
14118 mOverlay = new ViewOverlay(mContext, this);
Chet Haase91cedf12013-03-11 07:56:30 -070014119 }
Chet Haase91cedf12013-03-11 07:56:30 -070014120 return mOverlay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014121 }
14122
14123 /**
14124 * Override this if your view is known to always be drawn on top of a solid color background,
14125 * and needs to draw fading edges. Returning a non-zero color enables the view system to
14126 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
14127 * should be set to 0xFF.
14128 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014129 * @see #setVerticalFadingEdgeEnabled(boolean)
14130 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 *
14132 * @return The known solid color background for this view, or 0 if the color may vary
14133 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014134 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014135 public int getSolidColor() {
14136 return 0;
14137 }
14138
14139 /**
14140 * Build a human readable string representation of the specified view flags.
14141 *
14142 * @param flags the view flags to convert to a string
14143 * @return a String representing the supplied flags
14144 */
14145 private static String printFlags(int flags) {
14146 String output = "";
14147 int numFlags = 0;
14148 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
14149 output += "TAKES_FOCUS";
14150 numFlags++;
14151 }
14152
14153 switch (flags & VISIBILITY_MASK) {
14154 case INVISIBLE:
14155 if (numFlags > 0) {
14156 output += " ";
14157 }
14158 output += "INVISIBLE";
14159 // USELESS HERE numFlags++;
14160 break;
14161 case GONE:
14162 if (numFlags > 0) {
14163 output += " ";
14164 }
14165 output += "GONE";
14166 // USELESS HERE numFlags++;
14167 break;
14168 default:
14169 break;
14170 }
14171 return output;
14172 }
14173
14174 /**
14175 * Build a human readable string representation of the specified private
14176 * view flags.
14177 *
14178 * @param privateFlags the private view flags to convert to a string
14179 * @return a String representing the supplied flags
14180 */
14181 private static String printPrivateFlags(int privateFlags) {
14182 String output = "";
14183 int numFlags = 0;
14184
Dianne Hackborn4702a852012-08-17 15:18:29 -070014185 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014186 output += "WANTS_FOCUS";
14187 numFlags++;
14188 }
14189
Dianne Hackborn4702a852012-08-17 15:18:29 -070014190 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 if (numFlags > 0) {
14192 output += " ";
14193 }
14194 output += "FOCUSED";
14195 numFlags++;
14196 }
14197
Dianne Hackborn4702a852012-08-17 15:18:29 -070014198 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014199 if (numFlags > 0) {
14200 output += " ";
14201 }
14202 output += "SELECTED";
14203 numFlags++;
14204 }
14205
Dianne Hackborn4702a852012-08-17 15:18:29 -070014206 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014207 if (numFlags > 0) {
14208 output += " ";
14209 }
14210 output += "IS_ROOT_NAMESPACE";
14211 numFlags++;
14212 }
14213
Dianne Hackborn4702a852012-08-17 15:18:29 -070014214 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215 if (numFlags > 0) {
14216 output += " ";
14217 }
14218 output += "HAS_BOUNDS";
14219 numFlags++;
14220 }
14221
Dianne Hackborn4702a852012-08-17 15:18:29 -070014222 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014223 if (numFlags > 0) {
14224 output += " ";
14225 }
14226 output += "DRAWN";
14227 // USELESS HERE numFlags++;
14228 }
14229 return output;
14230 }
14231
14232 /**
14233 * <p>Indicates whether or not this view's layout will be requested during
14234 * the next hierarchy layout pass.</p>
14235 *
14236 * @return true if the layout will be forced during next layout pass
14237 */
14238 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014239 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 }
14241
14242 /**
Philip Milne7b757812012-09-19 18:13:44 -070014243 * Return true if o is a ViewGroup that is laying out using optical bounds.
14244 * @hide
14245 */
14246 public static boolean isLayoutModeOptical(Object o) {
14247 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
14248 }
14249
14250 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
14251 Insets parentInsets = mParent instanceof View ?
14252 ((View) mParent).getOpticalInsets() : Insets.NONE;
14253 Insets childInsets = getOpticalInsets();
14254 return setFrame(
14255 left + parentInsets.left - childInsets.left,
14256 top + parentInsets.top - childInsets.top,
14257 right + parentInsets.left + childInsets.right,
14258 bottom + parentInsets.top + childInsets.bottom);
14259 }
14260
14261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 * Assign a size and position to a view and all of its
14263 * descendants
14264 *
14265 * <p>This is the second phase of the layout mechanism.
14266 * (The first is measuring). In this phase, each parent calls
14267 * layout on all of its children to position them.
14268 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014269 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014270 *
Chet Haase9c087442011-01-12 16:20:16 -080014271 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 * Derived classes with children should override
14273 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014274 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014275 *
14276 * @param l Left position, relative to parent
14277 * @param t Top position, relative to parent
14278 * @param r Right position, relative to parent
14279 * @param b Bottom position, relative to parent
14280 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014281 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014282 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014283 int oldL = mLeft;
14284 int oldT = mTop;
14285 int oldB = mBottom;
14286 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014287 boolean changed = isLayoutModeOptical(mParent) ?
14288 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014289 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014290 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014291 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014292
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014293 ListenerInfo li = mListenerInfo;
14294 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014295 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014296 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014297 int numListeners = listenersCopy.size();
14298 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014299 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014300 }
14301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014303 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014304 }
14305
14306 /**
14307 * Called from layout when this view should
14308 * assign a size and position to each of its children.
14309 *
14310 * Derived classes with children should override
14311 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014312 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014313 * @param changed This is a new size or position for this view
14314 * @param left Left position, relative to parent
14315 * @param top Top position, relative to parent
14316 * @param right Right position, relative to parent
14317 * @param bottom Bottom position, relative to parent
14318 */
14319 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14320 }
14321
14322 /**
14323 * Assign a size and position to this view.
14324 *
14325 * This is called from layout.
14326 *
14327 * @param left Left position, relative to parent
14328 * @param top Top position, relative to parent
14329 * @param right Right position, relative to parent
14330 * @param bottom Bottom position, relative to parent
14331 * @return true if the new size and position are different than the
14332 * previous ones
14333 * {@hide}
14334 */
14335 protected boolean setFrame(int left, int top, int right, int bottom) {
14336 boolean changed = false;
14337
14338 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014339 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014340 + right + "," + bottom + ")");
14341 }
14342
14343 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14344 changed = true;
14345
14346 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014347 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014349 int oldWidth = mRight - mLeft;
14350 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014351 int newWidth = right - left;
14352 int newHeight = bottom - top;
14353 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14354
14355 // Invalidate our old position
14356 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014357
14358 mLeft = left;
14359 mTop = top;
14360 mRight = right;
14361 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014362 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014363 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014365
Dianne Hackborn4702a852012-08-17 15:18:29 -070014366 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014368
Chet Haase75755e22011-07-18 17:48:25 -070014369 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014370 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014371 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014372 if (mTransformationInfo != null) {
14373 mTransformationInfo.mMatrixDirty = true;
14374 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014375 }
Chet Haase91cedf12013-03-11 07:56:30 -070014376 sizeChange(newWidth, newHeight, oldWidth, oldHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014377 }
14378
14379 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14380 // If we are visible, force the DRAWN bit to on so that
14381 // this invalidate will go through (at least to our parent).
14382 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014383 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014384 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014385 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014386 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014387 // parent display list may need to be recreated based on a change in the bounds
14388 // of any child
14389 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014390 }
14391
14392 // Reset drawn bit to original value (invalidate turns it off)
14393 mPrivateFlags |= drawn;
14394
14395 mBackgroundSizeChanged = true;
14396 }
14397 return changed;
14398 }
14399
Chet Haase91cedf12013-03-11 07:56:30 -070014400 private void sizeChange(int newWidth, int newHeight, int oldWidth, int oldHeight) {
14401 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14402 if (mOverlay != null) {
Chet Haaseedf6f4b2013-03-26 07:55:30 -070014403 mOverlay.getOverlayView().setRight(newWidth);
14404 mOverlay.getOverlayView().setBottom(newHeight);
Chet Haase91cedf12013-03-11 07:56:30 -070014405 }
14406 }
14407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014408 /**
14409 * Finalize inflating a view from XML. This is called as the last phase
14410 * of inflation, after all child views have been added.
14411 *
14412 * <p>Even if the subclass overrides onFinishInflate, they should always be
14413 * sure to call the super method, so that we get called.
14414 */
14415 protected void onFinishInflate() {
14416 }
14417
14418 /**
14419 * Returns the resources associated with this view.
14420 *
14421 * @return Resources object.
14422 */
14423 public Resources getResources() {
14424 return mResources;
14425 }
14426
14427 /**
14428 * Invalidates the specified Drawable.
14429 *
14430 * @param drawable the drawable to invalidate
14431 */
14432 public void invalidateDrawable(Drawable drawable) {
14433 if (verifyDrawable(drawable)) {
14434 final Rect dirty = drawable.getBounds();
14435 final int scrollX = mScrollX;
14436 final int scrollY = mScrollY;
14437
14438 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14439 dirty.right + scrollX, dirty.bottom + scrollY);
14440 }
14441 }
14442
14443 /**
14444 * Schedules an action on a drawable to occur at a specified time.
14445 *
14446 * @param who the recipient of the action
14447 * @param what the action to run on the drawable
14448 * @param when the time at which the action must occur. Uses the
14449 * {@link SystemClock#uptimeMillis} timebase.
14450 */
14451 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014452 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014453 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014454 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014455 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14456 Choreographer.CALLBACK_ANIMATION, what, who,
14457 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014458 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014459 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014461 }
14462 }
14463
14464 /**
14465 * Cancels a scheduled action on a drawable.
14466 *
14467 * @param who the recipient of the action
14468 * @param what the action to cancel
14469 */
14470 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014471 if (verifyDrawable(who) && what != null) {
14472 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014473 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14474 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014475 } else {
14476 ViewRootImpl.getRunQueue().removeCallbacks(what);
14477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014478 }
14479 }
14480
14481 /**
14482 * Unschedule any events associated with the given Drawable. This can be
14483 * used when selecting a new Drawable into a view, so that the previous
14484 * one is completely unscheduled.
14485 *
14486 * @param who The Drawable to unschedule.
14487 *
14488 * @see #drawableStateChanged
14489 */
14490 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014491 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014492 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14493 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494 }
14495 }
14496
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014497 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014498 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14499 * that the View directionality can and will be resolved before its Drawables.
14500 *
14501 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014502 *
14503 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014504 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014505 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014506 if (canResolveLayoutDirection()) {
14507 if (mBackground != null) {
14508 mBackground.setLayoutDirection(getLayoutDirection());
14509 }
14510 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14511 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014512 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014513 }
14514
14515 /**
14516 * Called when layout direction has been resolved.
14517 *
14518 * The default implementation does nothing.
14519 *
14520 * @param layoutDirection The resolved layout direction.
14521 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014522 * @see #LAYOUT_DIRECTION_LTR
14523 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014524 *
14525 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014526 */
14527 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014528 }
14529
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014530 /**
14531 * @hide
14532 */
14533 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014534 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14535 }
14536
14537 private boolean isDrawablesResolved() {
14538 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14539 }
14540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014541 /**
14542 * If your view subclass is displaying its own Drawable objects, it should
14543 * override this function and return true for any Drawable it is
14544 * displaying. This allows animations for those drawables to be
14545 * scheduled.
14546 *
14547 * <p>Be sure to call through to the super class when overriding this
14548 * function.
14549 *
14550 * @param who The Drawable to verify. Return true if it is one you are
14551 * displaying, else return the result of calling through to the
14552 * super class.
14553 *
14554 * @return boolean If true than the Drawable is being displayed in the
14555 * view; else false and it is not allowed to animate.
14556 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014557 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14558 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014559 */
14560 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014561 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 }
14563
14564 /**
14565 * This function is called whenever the state of the view changes in such
14566 * a way that it impacts the state of drawables being shown.
14567 *
14568 * <p>Be sure to call through to the superclass when overriding this
14569 * function.
14570 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014571 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014572 */
14573 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014574 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 if (d != null && d.isStateful()) {
14576 d.setState(getDrawableState());
14577 }
14578 }
14579
14580 /**
14581 * Call this to force a view to update its drawable state. This will cause
14582 * drawableStateChanged to be called on this view. Views that are interested
14583 * in the new state should call getDrawableState.
14584 *
14585 * @see #drawableStateChanged
14586 * @see #getDrawableState
14587 */
14588 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014589 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 drawableStateChanged();
14591
14592 ViewParent parent = mParent;
14593 if (parent != null) {
14594 parent.childDrawableStateChanged(this);
14595 }
14596 }
14597
14598 /**
14599 * Return an array of resource IDs of the drawable states representing the
14600 * current state of the view.
14601 *
14602 * @return The current drawable state
14603 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014604 * @see Drawable#setState(int[])
14605 * @see #drawableStateChanged()
14606 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 */
14608 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014609 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014610 return mDrawableState;
14611 } else {
14612 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014613 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014614 return mDrawableState;
14615 }
14616 }
14617
14618 /**
14619 * Generate the new {@link android.graphics.drawable.Drawable} state for
14620 * this view. This is called by the view
14621 * system when the cached Drawable state is determined to be invalid. To
14622 * retrieve the current state, you should use {@link #getDrawableState}.
14623 *
14624 * @param extraSpace if non-zero, this is the number of extra entries you
14625 * would like in the returned array in which you can place your own
14626 * states.
14627 *
14628 * @return Returns an array holding the current {@link Drawable} state of
14629 * the view.
14630 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014631 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014632 */
14633 protected int[] onCreateDrawableState(int extraSpace) {
14634 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14635 mParent instanceof View) {
14636 return ((View) mParent).onCreateDrawableState(extraSpace);
14637 }
14638
14639 int[] drawableState;
14640
14641 int privateFlags = mPrivateFlags;
14642
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014643 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014644 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014645 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14646 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014647 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014648 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014649 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014650 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14651 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014652 // This is set if HW acceleration is requested, even if the current
14653 // process doesn't allow it. This is just to allow app preview
14654 // windows to better match their app.
14655 viewStateIndex |= VIEW_STATE_ACCELERATED;
14656 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014657 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014658
Christopher Tate3d4bf172011-03-28 16:16:46 -070014659 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014660 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14661 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 drawableState = VIEW_STATE_SETS[viewStateIndex];
14664
14665 //noinspection ConstantIfStatement
14666 if (false) {
14667 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14668 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014669 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014670 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14671 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014672 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014673 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014674 + ": " + Arrays.toString(drawableState));
14675 }
14676
14677 if (extraSpace == 0) {
14678 return drawableState;
14679 }
14680
14681 final int[] fullState;
14682 if (drawableState != null) {
14683 fullState = new int[drawableState.length + extraSpace];
14684 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14685 } else {
14686 fullState = new int[extraSpace];
14687 }
14688
14689 return fullState;
14690 }
14691
14692 /**
14693 * Merge your own state values in <var>additionalState</var> into the base
14694 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014695 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014696 *
14697 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014698 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014699 * own additional state values.
14700 *
14701 * @param additionalState The additional state values you would like
14702 * added to <var>baseState</var>; this array is not modified.
14703 *
14704 * @return As a convenience, the <var>baseState</var> array you originally
14705 * passed into the function is returned.
14706 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014707 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014708 */
14709 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14710 final int N = baseState.length;
14711 int i = N - 1;
14712 while (i >= 0 && baseState[i] == 0) {
14713 i--;
14714 }
14715 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14716 return baseState;
14717 }
14718
14719 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014720 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14721 * on all Drawable objects associated with this view.
14722 */
14723 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014724 if (mBackground != null) {
14725 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014726 }
14727 }
14728
14729 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014730 * Sets the background color for this view.
14731 * @param color the color of the background
14732 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014733 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014734 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014735 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014736 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014737 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014738 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014739 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014740 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014742 }
14743
14744 /**
14745 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014746 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014747 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014748 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014749 * @attr ref android.R.styleable#View_background
14750 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014751 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014752 public void setBackgroundResource(int resid) {
14753 if (resid != 0 && resid == mBackgroundResource) {
14754 return;
14755 }
14756
14757 Drawable d= null;
14758 if (resid != 0) {
14759 d = mResources.getDrawable(resid);
14760 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014761 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014762
14763 mBackgroundResource = resid;
14764 }
14765
14766 /**
14767 * Set the background to a given Drawable, or remove the background. If the
14768 * background has padding, this View's padding is set to the background's
14769 * padding. However, when a background is removed, this View's padding isn't
14770 * touched. If setting the padding is desired, please use
14771 * {@link #setPadding(int, int, int, int)}.
14772 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014773 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014774 * background
14775 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014776 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014777 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014778 setBackgroundDrawable(background);
14779 }
14780
14781 /**
14782 * @deprecated use {@link #setBackground(Drawable)} instead
14783 */
14784 @Deprecated
14785 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014786 computeOpaqueFlags();
14787
Philip Milne6c8ea062012-04-03 17:38:43 -070014788 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014789 return;
14790 }
14791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014792 boolean requestLayout = false;
14793
14794 mBackgroundResource = 0;
14795
14796 /*
14797 * Regardless of whether we're setting a new background or not, we want
14798 * to clear the previous drawable.
14799 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014800 if (mBackground != null) {
14801 mBackground.setCallback(null);
14802 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014803 }
14804
Philip Milne6c8ea062012-04-03 17:38:43 -070014805 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806 Rect padding = sThreadLocal.get();
14807 if (padding == null) {
14808 padding = new Rect();
14809 sThreadLocal.set(padding);
14810 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014811 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014812 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014813 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014814 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014815 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014816 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014817 mUserPaddingLeftInitial = padding.right;
14818 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014819 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014820 break;
14821 case LAYOUT_DIRECTION_LTR:
14822 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014823 mUserPaddingLeftInitial = padding.left;
14824 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014825 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014827 }
14828
14829 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14830 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014831 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14832 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014833 requestLayout = true;
14834 }
14835
Philip Milne6c8ea062012-04-03 17:38:43 -070014836 background.setCallback(this);
14837 if (background.isStateful()) {
14838 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014839 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014840 background.setVisible(getVisibility() == VISIBLE, false);
14841 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014842
Dianne Hackborn4702a852012-08-17 15:18:29 -070014843 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14844 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14845 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 requestLayout = true;
14847 }
14848 } else {
14849 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014850 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014851
Dianne Hackborn4702a852012-08-17 15:18:29 -070014852 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014853 /*
14854 * This view ONLY drew the background before and we're removing
14855 * the background, so now it won't draw anything
14856 * (hence we SKIP_DRAW)
14857 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014858 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14859 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 }
14861
14862 /*
14863 * When the background is set, we try to apply its padding to this
14864 * View. When the background is removed, we don't touch this View's
14865 * padding. This is noted in the Javadocs. Hence, we don't need to
14866 * requestLayout(), the invalidate() below is sufficient.
14867 */
14868
14869 // The old background's minimum size could have affected this
14870 // View's layout, so let's requestLayout
14871 requestLayout = true;
14872 }
14873
Romain Guy8f1344f52009-05-15 16:03:59 -070014874 computeOpaqueFlags();
14875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014876 if (requestLayout) {
14877 requestLayout();
14878 }
14879
14880 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014882 }
14883
14884 /**
14885 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014887 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014888 *
14889 * @see #setBackground(Drawable)
14890 *
14891 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014892 */
14893 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014894 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014895 }
14896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014897 /**
14898 * Sets the padding. The view may add on the space required to display
14899 * the scrollbars, depending on the style and visibility of the scrollbars.
14900 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14901 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14902 * from the values set in this call.
14903 *
14904 * @attr ref android.R.styleable#View_padding
14905 * @attr ref android.R.styleable#View_paddingBottom
14906 * @attr ref android.R.styleable#View_paddingLeft
14907 * @attr ref android.R.styleable#View_paddingRight
14908 * @attr ref android.R.styleable#View_paddingTop
14909 * @param left the left padding in pixels
14910 * @param top the top padding in pixels
14911 * @param right the right padding in pixels
14912 * @param bottom the bottom padding in pixels
14913 */
14914 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014915 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014916
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014917 mUserPaddingStart = UNDEFINED_PADDING;
14918 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014919
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014920 mUserPaddingLeftInitial = left;
14921 mUserPaddingRightInitial = right;
14922
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014923 internalSetPadding(left, top, right, bottom);
14924 }
14925
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014926 /**
14927 * @hide
14928 */
14929 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014930 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014931 mUserPaddingRight = right;
14932 mUserPaddingBottom = bottom;
14933
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014934 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014935 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014936
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014937 // Common case is there are no scroll bars.
14938 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014939 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014940 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014941 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014942 switch (mVerticalScrollbarPosition) {
14943 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014944 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014945 left += offset;
14946 } else {
14947 right += offset;
14948 }
14949 break;
Adam Powell20232d02010-12-08 21:08:53 -080014950 case SCROLLBAR_POSITION_RIGHT:
14951 right += offset;
14952 break;
14953 case SCROLLBAR_POSITION_LEFT:
14954 left += offset;
14955 break;
14956 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014957 }
Adam Powell20232d02010-12-08 21:08:53 -080014958 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014959 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14960 ? 0 : getHorizontalScrollbarHeight();
14961 }
14962 }
Romain Guy8506ab42009-06-11 17:35:47 -070014963
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014964 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014965 changed = true;
14966 mPaddingLeft = left;
14967 }
14968 if (mPaddingTop != top) {
14969 changed = true;
14970 mPaddingTop = top;
14971 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014972 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014973 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014974 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014975 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014976 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014977 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014978 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014979 }
14980
14981 if (changed) {
14982 requestLayout();
14983 }
14984 }
14985
14986 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014987 * Sets the relative padding. The view may add on the space required to display
14988 * the scrollbars, depending on the style and visibility of the scrollbars.
14989 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14990 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14991 * from the values set in this call.
14992 *
14993 * @attr ref android.R.styleable#View_padding
14994 * @attr ref android.R.styleable#View_paddingBottom
14995 * @attr ref android.R.styleable#View_paddingStart
14996 * @attr ref android.R.styleable#View_paddingEnd
14997 * @attr ref android.R.styleable#View_paddingTop
14998 * @param start the start padding in pixels
14999 * @param top the top padding in pixels
15000 * @param end the end padding in pixels
15001 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015002 */
15003 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070015004 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015005
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070015006 mUserPaddingStart = start;
15007 mUserPaddingEnd = end;
15008
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070015009 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015010 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070015011 mUserPaddingLeftInitial = end;
15012 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080015013 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015014 break;
15015 case LAYOUT_DIRECTION_LTR:
15016 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070015017 mUserPaddingLeftInitial = start;
15018 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080015019 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015020 }
15021 }
15022
15023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015024 * Returns the top padding of this view.
15025 *
15026 * @return the top padding in pixels
15027 */
15028 public int getPaddingTop() {
15029 return mPaddingTop;
15030 }
15031
15032 /**
15033 * Returns the bottom padding of this view. If there are inset and enabled
15034 * scrollbars, this value may include the space required to display the
15035 * scrollbars as well.
15036 *
15037 * @return the bottom padding in pixels
15038 */
15039 public int getPaddingBottom() {
15040 return mPaddingBottom;
15041 }
15042
15043 /**
15044 * Returns the left padding of this view. If there are inset and enabled
15045 * scrollbars, this value may include the space required to display the
15046 * scrollbars as well.
15047 *
15048 * @return the left padding in pixels
15049 */
15050 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015051 if (!isPaddingResolved()) {
15052 resolvePadding();
15053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015054 return mPaddingLeft;
15055 }
15056
15057 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070015058 * Returns the start padding of this view depending on its resolved layout direction.
15059 * If there are inset and enabled scrollbars, this value may include the space
15060 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015061 *
15062 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015063 */
15064 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015065 if (!isPaddingResolved()) {
15066 resolvePadding();
15067 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070015068 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015069 mPaddingRight : mPaddingLeft;
15070 }
15071
15072 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015073 * Returns the right padding of this view. If there are inset and enabled
15074 * scrollbars, this value may include the space required to display the
15075 * scrollbars as well.
15076 *
15077 * @return the right padding in pixels
15078 */
15079 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015080 if (!isPaddingResolved()) {
15081 resolvePadding();
15082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015083 return mPaddingRight;
15084 }
15085
15086 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070015087 * Returns the end padding of this view depending on its resolved layout direction.
15088 * If there are inset and enabled scrollbars, this value may include the space
15089 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015090 *
15091 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015092 */
15093 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015094 if (!isPaddingResolved()) {
15095 resolvePadding();
15096 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070015097 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015098 mPaddingLeft : mPaddingRight;
15099 }
15100
15101 /**
15102 * Return if the padding as been set thru relative values
15103 * {@link #setPaddingRelative(int, int, int, int)} or thru
15104 * @attr ref android.R.styleable#View_paddingStart or
15105 * @attr ref android.R.styleable#View_paddingEnd
15106 *
15107 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015108 */
15109 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070015110 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070015111 }
15112
Philip Milne7b757812012-09-19 18:13:44 -070015113 Insets computeOpticalInsets() {
15114 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070015115 }
15116
15117 /**
15118 * @hide
15119 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070015120 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070015121 if (isRtlCompatibilityMode()) {
15122 mPaddingLeft = mUserPaddingLeftInitial;
15123 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070015124 return;
15125 }
15126 if (isLayoutRtl()) {
15127 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
15128 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070015129 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070015130 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
15131 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070015132 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070015133 }
15134
15135 /**
15136 * @hide
15137 */
Philip Milne7b757812012-09-19 18:13:44 -070015138 public Insets getOpticalInsets() {
15139 if (mLayoutInsets == null) {
15140 mLayoutInsets = computeOpticalInsets();
15141 }
15142 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070015143 }
15144
15145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015146 * Changes the selection state of this view. A view can be selected or not.
15147 * Note that selection is not the same as focus. Views are typically
15148 * selected in the context of an AdapterView like ListView or GridView;
15149 * the selected view is the view that is highlighted.
15150 *
15151 * @param selected true if the view must be selected, false otherwise
15152 */
15153 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015154 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
15155 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070015156 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080015157 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015158 refreshDrawableState();
15159 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070015160 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
15161 notifyAccessibilityStateChanged();
15162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015163 }
15164 }
15165
15166 /**
15167 * Dispatch setSelected to all of this View's children.
15168 *
15169 * @see #setSelected(boolean)
15170 *
15171 * @param selected The new selected state
15172 */
15173 protected void dispatchSetSelected(boolean selected) {
15174 }
15175
15176 /**
15177 * Indicates the selection state of this view.
15178 *
15179 * @return true if the view is selected, false otherwise
15180 */
15181 @ViewDebug.ExportedProperty
15182 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015183 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015184 }
15185
15186 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015187 * Changes the activated state of this view. A view can be activated or not.
15188 * Note that activation is not the same as selection. Selection is
15189 * a transient property, representing the view (hierarchy) the user is
15190 * currently interacting with. Activation is a longer-term state that the
15191 * user can move views in and out of. For example, in a list view with
15192 * single or multiple selection enabled, the views in the current selection
15193 * set are activated. (Um, yeah, we are deeply sorry about the terminology
15194 * here.) The activated state is propagated down to children of the view it
15195 * is set on.
15196 *
15197 * @param activated true if the view must be activated, false otherwise
15198 */
15199 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015200 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
15201 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015202 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015203 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070015204 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015205 }
15206 }
15207
15208 /**
15209 * Dispatch setActivated to all of this View's children.
15210 *
15211 * @see #setActivated(boolean)
15212 *
15213 * @param activated The new activated state
15214 */
15215 protected void dispatchSetActivated(boolean activated) {
15216 }
15217
15218 /**
15219 * Indicates the activation state of this view.
15220 *
15221 * @return true if the view is activated, false otherwise
15222 */
15223 @ViewDebug.ExportedProperty
15224 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015225 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015226 }
15227
15228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015229 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
15230 * observer can be used to get notifications when global events, like
15231 * layout, happen.
15232 *
15233 * The returned ViewTreeObserver observer is not guaranteed to remain
15234 * valid for the lifetime of this View. If the caller of this method keeps
15235 * a long-lived reference to ViewTreeObserver, it should always check for
15236 * the return value of {@link ViewTreeObserver#isAlive()}.
15237 *
15238 * @return The ViewTreeObserver for this view's hierarchy.
15239 */
15240 public ViewTreeObserver getViewTreeObserver() {
15241 if (mAttachInfo != null) {
15242 return mAttachInfo.mTreeObserver;
15243 }
15244 if (mFloatingTreeObserver == null) {
15245 mFloatingTreeObserver = new ViewTreeObserver();
15246 }
15247 return mFloatingTreeObserver;
15248 }
15249
15250 /**
15251 * <p>Finds the topmost view in the current view hierarchy.</p>
15252 *
15253 * @return the topmost view containing this view
15254 */
15255 public View getRootView() {
15256 if (mAttachInfo != null) {
15257 final View v = mAttachInfo.mRootView;
15258 if (v != null) {
15259 return v;
15260 }
15261 }
Romain Guy8506ab42009-06-11 17:35:47 -070015262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015263 View parent = this;
15264
15265 while (parent.mParent != null && parent.mParent instanceof View) {
15266 parent = (View) parent.mParent;
15267 }
15268
15269 return parent;
15270 }
15271
15272 /**
15273 * <p>Computes the coordinates of this view on the screen. The argument
15274 * must be an array of two integers. After the method returns, the array
15275 * contains the x and y location in that order.</p>
15276 *
15277 * @param location an array of two integers in which to hold the coordinates
15278 */
15279 public void getLocationOnScreen(int[] location) {
15280 getLocationInWindow(location);
15281
15282 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015283 if (info != null) {
15284 location[0] += info.mWindowLeft;
15285 location[1] += info.mWindowTop;
15286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015287 }
15288
15289 /**
15290 * <p>Computes the coordinates of this view in its window. The argument
15291 * must be an array of two integers. After the method returns, the array
15292 * contains the x and y location in that order.</p>
15293 *
15294 * @param location an array of two integers in which to hold the coordinates
15295 */
15296 public void getLocationInWindow(int[] location) {
15297 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015298 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015299 }
15300
Gilles Debunne6583ce52011-12-06 18:09:02 -080015301 if (mAttachInfo == null) {
15302 // When the view is not attached to a window, this method does not make sense
15303 location[0] = location[1] = 0;
15304 return;
15305 }
15306
Gilles Debunnecea45132011-11-24 02:19:27 +010015307 float[] position = mAttachInfo.mTmpTransformLocation;
15308 position[0] = position[1] = 0.0f;
15309
15310 if (!hasIdentityMatrix()) {
15311 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015313
Gilles Debunnecea45132011-11-24 02:19:27 +010015314 position[0] += mLeft;
15315 position[1] += mTop;
15316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015317 ViewParent viewParent = mParent;
15318 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015319 final View view = (View) viewParent;
15320
15321 position[0] -= view.mScrollX;
15322 position[1] -= view.mScrollY;
15323
15324 if (!view.hasIdentityMatrix()) {
15325 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015326 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015327
15328 position[0] += view.mLeft;
15329 position[1] += view.mTop;
15330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015331 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015332 }
Romain Guy8506ab42009-06-11 17:35:47 -070015333
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015334 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015335 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015336 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15337 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015338 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015339
15340 location[0] = (int) (position[0] + 0.5f);
15341 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015342 }
15343
15344 /**
15345 * {@hide}
15346 * @param id the id of the view to be found
15347 * @return the view of the specified id, null if cannot be found
15348 */
15349 protected View findViewTraversal(int id) {
15350 if (id == mID) {
15351 return this;
15352 }
15353 return null;
15354 }
15355
15356 /**
15357 * {@hide}
15358 * @param tag the tag of the view to be found
15359 * @return the view of specified tag, null if cannot be found
15360 */
15361 protected View findViewWithTagTraversal(Object tag) {
15362 if (tag != null && tag.equals(mTag)) {
15363 return this;
15364 }
15365 return null;
15366 }
15367
15368 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015369 * {@hide}
15370 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015371 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015372 * @return The first view that matches the predicate or null.
15373 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015374 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015375 if (predicate.apply(this)) {
15376 return this;
15377 }
15378 return null;
15379 }
15380
15381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015382 * Look for a child view with the given id. If this view has the given
15383 * id, return this view.
15384 *
15385 * @param id The id to search for.
15386 * @return The view that has the given id in the hierarchy or null
15387 */
15388 public final View findViewById(int id) {
15389 if (id < 0) {
15390 return null;
15391 }
15392 return findViewTraversal(id);
15393 }
15394
15395 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015396 * Finds a view by its unuque and stable accessibility id.
15397 *
15398 * @param accessibilityId The searched accessibility id.
15399 * @return The found view.
15400 */
15401 final View findViewByAccessibilityId(int accessibilityId) {
15402 if (accessibilityId < 0) {
15403 return null;
15404 }
15405 return findViewByAccessibilityIdTraversal(accessibilityId);
15406 }
15407
15408 /**
15409 * Performs the traversal to find a view by its unuque and stable accessibility id.
15410 *
15411 * <strong>Note:</strong>This method does not stop at the root namespace
15412 * boundary since the user can touch the screen at an arbitrary location
15413 * potentially crossing the root namespace bounday which will send an
15414 * accessibility event to accessibility services and they should be able
15415 * to obtain the event source. Also accessibility ids are guaranteed to be
15416 * unique in the window.
15417 *
15418 * @param accessibilityId The accessibility id.
15419 * @return The found view.
Svetoslav5b578da2013-05-08 14:23:32 -070015420 *
15421 * @hide
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015422 */
Svetoslav5b578da2013-05-08 14:23:32 -070015423 public View findViewByAccessibilityIdTraversal(int accessibilityId) {
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015424 if (getAccessibilityViewId() == accessibilityId) {
15425 return this;
15426 }
15427 return null;
15428 }
15429
15430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015431 * Look for a child view with the given tag. If this view has the given
15432 * tag, return this view.
15433 *
15434 * @param tag The tag to search for, using "tag.equals(getTag())".
15435 * @return The View that has the given tag in the hierarchy or null
15436 */
15437 public final View findViewWithTag(Object tag) {
15438 if (tag == null) {
15439 return null;
15440 }
15441 return findViewWithTagTraversal(tag);
15442 }
15443
15444 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015445 * {@hide}
15446 * Look for a child view that matches the specified predicate.
15447 * If this view matches the predicate, return this view.
15448 *
15449 * @param predicate The predicate to evaluate.
15450 * @return The first view that matches the predicate or null.
15451 */
15452 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015453 return findViewByPredicateTraversal(predicate, null);
15454 }
15455
15456 /**
15457 * {@hide}
15458 * Look for a child view that matches the specified predicate,
15459 * starting with the specified view and its descendents and then
15460 * recusively searching the ancestors and siblings of that view
15461 * until this view is reached.
15462 *
15463 * This method is useful in cases where the predicate does not match
15464 * a single unique view (perhaps multiple views use the same id)
15465 * and we are trying to find the view that is "closest" in scope to the
15466 * starting view.
15467 *
15468 * @param start The view to start from.
15469 * @param predicate The predicate to evaluate.
15470 * @return The first view that matches the predicate or null.
15471 */
15472 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15473 View childToSkip = null;
15474 for (;;) {
15475 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15476 if (view != null || start == this) {
15477 return view;
15478 }
15479
15480 ViewParent parent = start.getParent();
15481 if (parent == null || !(parent instanceof View)) {
15482 return null;
15483 }
15484
15485 childToSkip = start;
15486 start = (View) parent;
15487 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015488 }
15489
15490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015491 * Sets the identifier for this view. The identifier does not have to be
15492 * unique in this view's hierarchy. The identifier should be a positive
15493 * number.
15494 *
15495 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015496 * @see #getId()
15497 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015498 *
15499 * @param id a number used to identify the view
15500 *
15501 * @attr ref android.R.styleable#View_id
15502 */
15503 public void setId(int id) {
15504 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015505 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15506 mID = generateViewId();
15507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015508 }
15509
15510 /**
15511 * {@hide}
15512 *
15513 * @param isRoot true if the view belongs to the root namespace, false
15514 * otherwise
15515 */
15516 public void setIsRootNamespace(boolean isRoot) {
15517 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015518 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015519 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015520 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015521 }
15522 }
15523
15524 /**
15525 * {@hide}
15526 *
15527 * @return true if the view belongs to the root namespace, false otherwise
15528 */
15529 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015530 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015531 }
15532
15533 /**
15534 * Returns this view's identifier.
15535 *
15536 * @return a positive integer used to identify the view or {@link #NO_ID}
15537 * if the view has no ID
15538 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015539 * @see #setId(int)
15540 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015541 * @attr ref android.R.styleable#View_id
15542 */
15543 @ViewDebug.CapturedViewProperty
15544 public int getId() {
15545 return mID;
15546 }
15547
15548 /**
15549 * Returns this view's tag.
15550 *
15551 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015552 *
15553 * @see #setTag(Object)
15554 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015555 */
15556 @ViewDebug.ExportedProperty
15557 public Object getTag() {
15558 return mTag;
15559 }
15560
15561 /**
15562 * Sets the tag associated with this view. A tag can be used to mark
15563 * a view in its hierarchy and does not have to be unique within the
15564 * hierarchy. Tags can also be used to store data within a view without
15565 * resorting to another data structure.
15566 *
15567 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015568 *
15569 * @see #getTag()
15570 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015571 */
15572 public void setTag(final Object tag) {
15573 mTag = tag;
15574 }
15575
15576 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015577 * Returns the tag associated with this view and the specified key.
15578 *
15579 * @param key The key identifying the tag
15580 *
15581 * @return the Object stored in this view as a tag
15582 *
15583 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015584 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015585 */
15586 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015587 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015588 return null;
15589 }
15590
15591 /**
15592 * Sets a tag associated with this view and a key. A tag can be used
15593 * to mark a view in its hierarchy and does not have to be unique within
15594 * the hierarchy. Tags can also be used to store data within a view
15595 * without resorting to another data structure.
15596 *
15597 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015598 * application to ensure it is unique (see the <a
15599 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15600 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015601 * the Android framework or not associated with any package will cause
15602 * an {@link IllegalArgumentException} to be thrown.
15603 *
15604 * @param key The key identifying the tag
15605 * @param tag An Object to tag the view with
15606 *
15607 * @throws IllegalArgumentException If they specified key is not valid
15608 *
15609 * @see #setTag(Object)
15610 * @see #getTag(int)
15611 */
15612 public void setTag(int key, final Object tag) {
15613 // If the package id is 0x00 or 0x01, it's either an undefined package
15614 // or a framework id
15615 if ((key >>> 24) < 2) {
15616 throw new IllegalArgumentException("The key must be an application-specific "
15617 + "resource id.");
15618 }
15619
Adam Powell2b2f6d62011-09-23 11:15:39 -070015620 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015621 }
15622
15623 /**
15624 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15625 * framework id.
15626 *
15627 * @hide
15628 */
15629 public void setTagInternal(int key, Object tag) {
15630 if ((key >>> 24) != 0x1) {
15631 throw new IllegalArgumentException("The key must be a framework-specific "
15632 + "resource id.");
15633 }
15634
Adam Powell2b2f6d62011-09-23 11:15:39 -070015635 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015636 }
15637
Adam Powell2b2f6d62011-09-23 11:15:39 -070015638 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015639 if (mKeyedTags == null) {
15640 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015641 }
15642
Adam Powell7db82ac2011-09-22 19:44:04 -070015643 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015644 }
15645
15646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015647 * Prints information about this view in the log output, with the tag
15648 * {@link #VIEW_LOG_TAG}.
15649 *
15650 * @hide
15651 */
15652 public void debug() {
15653 debug(0);
15654 }
15655
15656 /**
15657 * Prints information about this view in the log output, with the tag
15658 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15659 * indentation defined by the <code>depth</code>.
15660 *
15661 * @param depth the indentation level
15662 *
15663 * @hide
15664 */
15665 protected void debug(int depth) {
15666 String output = debugIndent(depth - 1);
15667
15668 output += "+ " + this;
15669 int id = getId();
15670 if (id != -1) {
15671 output += " (id=" + id + ")";
15672 }
15673 Object tag = getTag();
15674 if (tag != null) {
15675 output += " (tag=" + tag + ")";
15676 }
15677 Log.d(VIEW_LOG_TAG, output);
15678
Dianne Hackborn4702a852012-08-17 15:18:29 -070015679 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015680 output = debugIndent(depth) + " FOCUSED";
15681 Log.d(VIEW_LOG_TAG, output);
15682 }
15683
15684 output = debugIndent(depth);
15685 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15686 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15687 + "} ";
15688 Log.d(VIEW_LOG_TAG, output);
15689
15690 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15691 || mPaddingBottom != 0) {
15692 output = debugIndent(depth);
15693 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15694 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15695 Log.d(VIEW_LOG_TAG, output);
15696 }
15697
15698 output = debugIndent(depth);
15699 output += "mMeasureWidth=" + mMeasuredWidth +
15700 " mMeasureHeight=" + mMeasuredHeight;
15701 Log.d(VIEW_LOG_TAG, output);
15702
15703 output = debugIndent(depth);
15704 if (mLayoutParams == null) {
15705 output += "BAD! no layout params";
15706 } else {
15707 output = mLayoutParams.debug(output);
15708 }
15709 Log.d(VIEW_LOG_TAG, output);
15710
15711 output = debugIndent(depth);
15712 output += "flags={";
15713 output += View.printFlags(mViewFlags);
15714 output += "}";
15715 Log.d(VIEW_LOG_TAG, output);
15716
15717 output = debugIndent(depth);
15718 output += "privateFlags={";
15719 output += View.printPrivateFlags(mPrivateFlags);
15720 output += "}";
15721 Log.d(VIEW_LOG_TAG, output);
15722 }
15723
15724 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015725 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015726 *
15727 * @param depth the indentation level
15728 * @return a String containing (depth * 2 + 3) * 2 white spaces
15729 *
15730 * @hide
15731 */
15732 protected static String debugIndent(int depth) {
15733 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15734 for (int i = 0; i < (depth * 2) + 3; i++) {
15735 spaces.append(' ').append(' ');
15736 }
15737 return spaces.toString();
15738 }
15739
15740 /**
15741 * <p>Return the offset of the widget's text baseline from the widget's top
15742 * boundary. If this widget does not support baseline alignment, this
15743 * method returns -1. </p>
15744 *
15745 * @return the offset of the baseline within the widget's bounds or -1
15746 * if baseline alignment is not supported
15747 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015748 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015749 public int getBaseline() {
15750 return -1;
15751 }
15752
15753 /**
Chet Haase97140572012-09-13 14:56:47 -070015754 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15755 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15756 * a layout pass.
15757 *
15758 * @return whether the view hierarchy is currently undergoing a layout pass
15759 */
15760 public boolean isInLayout() {
15761 ViewRootImpl viewRoot = getViewRootImpl();
15762 return (viewRoot != null && viewRoot.isInLayout());
15763 }
15764
15765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015766 * Call this when something has changed which has invalidated the
15767 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015768 * tree. This should not be called while the view hierarchy is currently in a layout
15769 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15770 * end of the current layout pass (and then layout will run again) or after the current
15771 * frame is drawn and the next layout occurs.
15772 *
15773 * <p>Subclasses which override this method should call the superclass method to
15774 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015775 */
15776 public void requestLayout() {
Chet Haasecc699b42012-12-13 09:06:55 -080015777 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
15778 // Only trigger request-during-layout logic if this is the view requesting it,
15779 // not the views in its parent hierarchy
15780 ViewRootImpl viewRoot = getViewRootImpl();
15781 if (viewRoot != null && viewRoot.isInLayout()) {
15782 if (!viewRoot.requestLayoutDuringLayout(this)) {
15783 return;
15784 }
15785 }
15786 mAttachInfo.mViewRequestingLayout = this;
Chet Haase97140572012-09-13 14:56:47 -070015787 }
Chet Haasecc699b42012-12-13 09:06:55 -080015788
Dianne Hackborn4702a852012-08-17 15:18:29 -070015789 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15790 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015791
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015792 if (mParent != null && !mParent.isLayoutRequested()) {
15793 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015794 }
Chet Haasecc699b42012-12-13 09:06:55 -080015795 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
15796 mAttachInfo.mViewRequestingLayout = null;
15797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015798 }
15799
15800 /**
15801 * Forces this view to be laid out during the next layout pass.
15802 * This method does not call requestLayout() or forceLayout()
15803 * on the parent.
15804 */
15805 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015806 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15807 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015808 }
15809
15810 /**
15811 * <p>
15812 * This is called to find out how big a view should be. The parent
15813 * supplies constraint information in the width and height parameters.
15814 * </p>
15815 *
15816 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015817 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015818 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015819 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015820 * </p>
15821 *
15822 *
15823 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15824 * parent
15825 * @param heightMeasureSpec Vertical space requirements as imposed by the
15826 * parent
15827 *
15828 * @see #onMeasure(int, int)
15829 */
15830 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015831 boolean optical = isLayoutModeOptical(this);
15832 if (optical != isLayoutModeOptical(mParent)) {
15833 Insets insets = getOpticalInsets();
15834 int oWidth = insets.left + insets.right;
15835 int oHeight = insets.top + insets.bottom;
15836 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15837 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15838 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015839 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015840 widthMeasureSpec != mOldWidthMeasureSpec ||
15841 heightMeasureSpec != mOldHeightMeasureSpec) {
15842
15843 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015844 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015845
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015846 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015848 // measure ourselves, this should set the measured dimension flag back
15849 onMeasure(widthMeasureSpec, heightMeasureSpec);
15850
15851 // flag not set, setMeasuredDimension() was not invoked, we raise
15852 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015853 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015854 throw new IllegalStateException("onMeasure() did not set the"
15855 + " measured dimension by calling"
15856 + " setMeasuredDimension()");
15857 }
15858
Dianne Hackborn4702a852012-08-17 15:18:29 -070015859 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015860 }
15861
15862 mOldWidthMeasureSpec = widthMeasureSpec;
15863 mOldHeightMeasureSpec = heightMeasureSpec;
15864 }
15865
15866 /**
15867 * <p>
15868 * Measure the view and its content to determine the measured width and the
15869 * measured height. This method is invoked by {@link #measure(int, int)} and
15870 * should be overriden by subclasses to provide accurate and efficient
15871 * measurement of their contents.
15872 * </p>
15873 *
15874 * <p>
15875 * <strong>CONTRACT:</strong> When overriding this method, you
15876 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15877 * measured width and height of this view. Failure to do so will trigger an
15878 * <code>IllegalStateException</code>, thrown by
15879 * {@link #measure(int, int)}. Calling the superclass'
15880 * {@link #onMeasure(int, int)} is a valid use.
15881 * </p>
15882 *
15883 * <p>
15884 * The base class implementation of measure defaults to the background size,
15885 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15886 * override {@link #onMeasure(int, int)} to provide better measurements of
15887 * their content.
15888 * </p>
15889 *
15890 * <p>
15891 * If this method is overridden, it is the subclass's responsibility to make
15892 * sure the measured height and width are at least the view's minimum height
15893 * and width ({@link #getSuggestedMinimumHeight()} and
15894 * {@link #getSuggestedMinimumWidth()}).
15895 * </p>
15896 *
15897 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15898 * The requirements are encoded with
15899 * {@link android.view.View.MeasureSpec}.
15900 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15901 * The requirements are encoded with
15902 * {@link android.view.View.MeasureSpec}.
15903 *
15904 * @see #getMeasuredWidth()
15905 * @see #getMeasuredHeight()
15906 * @see #setMeasuredDimension(int, int)
15907 * @see #getSuggestedMinimumHeight()
15908 * @see #getSuggestedMinimumWidth()
15909 * @see android.view.View.MeasureSpec#getMode(int)
15910 * @see android.view.View.MeasureSpec#getSize(int)
15911 */
15912 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15913 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15914 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15915 }
15916
15917 /**
John Spurlock44b1edd2013-05-28 11:40:58 -040015918 * <p>This method must be called by {@link #onMeasure(int, int)} to store the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015919 * measured width and measured height. Failing to do so will trigger an
15920 * exception at measurement time.</p>
15921 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015922 * @param measuredWidth The measured width of this view. May be a complex
15923 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15924 * {@link #MEASURED_STATE_TOO_SMALL}.
15925 * @param measuredHeight The measured height of this view. May be a complex
15926 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15927 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015928 */
15929 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015930 boolean optical = isLayoutModeOptical(this);
15931 if (optical != isLayoutModeOptical(mParent)) {
15932 Insets insets = getOpticalInsets();
15933 int opticalWidth = insets.left + insets.right;
15934 int opticalHeight = insets.top + insets.bottom;
15935
15936 measuredWidth += optical ? opticalWidth : -opticalWidth;
15937 measuredHeight += optical ? opticalHeight : -opticalHeight;
15938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015939 mMeasuredWidth = measuredWidth;
15940 mMeasuredHeight = measuredHeight;
15941
Dianne Hackborn4702a852012-08-17 15:18:29 -070015942 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015943 }
15944
15945 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015946 * Merge two states as returned by {@link #getMeasuredState()}.
15947 * @param curState The current state as returned from a view or the result
15948 * of combining multiple views.
15949 * @param newState The new view state to combine.
15950 * @return Returns a new integer reflecting the combination of the two
15951 * states.
15952 */
15953 public static int combineMeasuredStates(int curState, int newState) {
15954 return curState | newState;
15955 }
15956
15957 /**
15958 * Version of {@link #resolveSizeAndState(int, int, int)}
15959 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15960 */
15961 public static int resolveSize(int size, int measureSpec) {
15962 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15963 }
15964
15965 /**
15966 * Utility to reconcile a desired size and state, with constraints imposed
15967 * by a MeasureSpec. Will take the desired size, unless a different size
15968 * is imposed by the constraints. The returned value is a compound integer,
15969 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15970 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15971 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015972 *
15973 * @param size How big the view wants to be
15974 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015975 * @return Size information bit mask as defined by
15976 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015977 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015978 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015979 int result = size;
15980 int specMode = MeasureSpec.getMode(measureSpec);
15981 int specSize = MeasureSpec.getSize(measureSpec);
15982 switch (specMode) {
15983 case MeasureSpec.UNSPECIFIED:
15984 result = size;
15985 break;
15986 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015987 if (specSize < size) {
15988 result = specSize | MEASURED_STATE_TOO_SMALL;
15989 } else {
15990 result = size;
15991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015992 break;
15993 case MeasureSpec.EXACTLY:
15994 result = specSize;
15995 break;
15996 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015997 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015998 }
15999
16000 /**
16001 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070016002 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016003 * by the MeasureSpec.
16004 *
16005 * @param size Default size for this view
16006 * @param measureSpec Constraints imposed by the parent
16007 * @return The size this view should be.
16008 */
16009 public static int getDefaultSize(int size, int measureSpec) {
16010 int result = size;
16011 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070016012 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016013
16014 switch (specMode) {
16015 case MeasureSpec.UNSPECIFIED:
16016 result = size;
16017 break;
16018 case MeasureSpec.AT_MOST:
16019 case MeasureSpec.EXACTLY:
16020 result = specSize;
16021 break;
16022 }
16023 return result;
16024 }
16025
16026 /**
16027 * Returns the suggested minimum height that the view should use. This
16028 * returns the maximum of the view's minimum height
16029 * and the background's minimum height
16030 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
16031 * <p>
16032 * When being used in {@link #onMeasure(int, int)}, the caller should still
16033 * ensure the returned height is within the requirements of the parent.
16034 *
16035 * @return The suggested minimum height of the view.
16036 */
16037 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070016038 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016040 }
16041
16042 /**
16043 * Returns the suggested minimum width that the view should use. This
16044 * returns the maximum of the view's minimum width)
16045 * and the background's minimum width
16046 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
16047 * <p>
16048 * When being used in {@link #onMeasure(int, int)}, the caller should still
16049 * ensure the returned width is within the requirements of the parent.
16050 *
16051 * @return The suggested minimum width of the view.
16052 */
16053 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070016054 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
16055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016056
Philip Milne6c8ea062012-04-03 17:38:43 -070016057 /**
16058 * Returns the minimum height of the view.
16059 *
16060 * @return the minimum height the view will try to be.
16061 *
16062 * @see #setMinimumHeight(int)
16063 *
16064 * @attr ref android.R.styleable#View_minHeight
16065 */
16066 public int getMinimumHeight() {
16067 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016068 }
16069
16070 /**
16071 * Sets the minimum height of the view. It is not guaranteed the view will
16072 * be able to achieve this minimum height (for example, if its parent layout
16073 * constrains it with less available height).
16074 *
16075 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070016076 *
16077 * @see #getMinimumHeight()
16078 *
16079 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016080 */
16081 public void setMinimumHeight(int minHeight) {
16082 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070016083 requestLayout();
16084 }
16085
16086 /**
16087 * Returns the minimum width of the view.
16088 *
16089 * @return the minimum width the view will try to be.
16090 *
16091 * @see #setMinimumWidth(int)
16092 *
16093 * @attr ref android.R.styleable#View_minWidth
16094 */
16095 public int getMinimumWidth() {
16096 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016097 }
16098
16099 /**
16100 * Sets the minimum width of the view. It is not guaranteed the view will
16101 * be able to achieve this minimum width (for example, if its parent layout
16102 * constrains it with less available width).
16103 *
16104 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070016105 *
16106 * @see #getMinimumWidth()
16107 *
16108 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016109 */
16110 public void setMinimumWidth(int minWidth) {
16111 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070016112 requestLayout();
16113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016114 }
16115
16116 /**
16117 * Get the animation currently associated with this view.
16118 *
16119 * @return The animation that is currently playing or
16120 * scheduled to play for this view.
16121 */
16122 public Animation getAnimation() {
16123 return mCurrentAnimation;
16124 }
16125
16126 /**
16127 * Start the specified animation now.
16128 *
16129 * @param animation the animation to start now
16130 */
16131 public void startAnimation(Animation animation) {
16132 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
16133 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080016134 invalidateParentCaches();
16135 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016136 }
16137
16138 /**
16139 * Cancels any animations for this view.
16140 */
16141 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080016142 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080016143 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080016144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016145 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080016146 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016147 }
16148
16149 /**
16150 * Sets the next animation to play for this view.
16151 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070016152 * {@link #startAnimation(android.view.animation.Animation)} instead.
16153 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016154 * control over the start time and invalidation, but you
16155 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070016156 * 2) the view's parent (which controls animations on its children)
16157 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016158 * start.
16159 *
16160 * @param animation The next animation, or null.
16161 */
16162 public void setAnimation(Animation animation) {
16163 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070016164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016165 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070016166 // If the screen is off assume the animation start time is now instead of
16167 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
16168 // would cause the animation to start when the screen turns back on
16169 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
16170 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
16171 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
16172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016173 animation.reset();
16174 }
16175 }
16176
16177 /**
16178 * Invoked by a parent ViewGroup to notify the start of the animation
16179 * currently associated with this view. If you override this method,
16180 * always call super.onAnimationStart();
16181 *
16182 * @see #setAnimation(android.view.animation.Animation)
16183 * @see #getAnimation()
16184 */
16185 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016186 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016187 }
16188
16189 /**
16190 * Invoked by a parent ViewGroup to notify the end of the animation
16191 * currently associated with this view. If you override this method,
16192 * always call super.onAnimationEnd();
16193 *
16194 * @see #setAnimation(android.view.animation.Animation)
16195 * @see #getAnimation()
16196 */
16197 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016198 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016199 }
16200
16201 /**
16202 * Invoked if there is a Transform that involves alpha. Subclass that can
16203 * draw themselves with the specified alpha should return true, and then
16204 * respect that alpha when their onDraw() is called. If this returns false
16205 * then the view may be redirected to draw into an offscreen buffer to
16206 * fulfill the request, which will look fine, but may be slower than if the
16207 * subclass handles it internally. The default implementation returns false.
16208 *
16209 * @param alpha The alpha (0..255) to apply to the view's drawing
16210 * @return true if the view can draw with the specified alpha.
16211 */
16212 protected boolean onSetAlpha(int alpha) {
16213 return false;
16214 }
16215
16216 /**
16217 * This is used by the RootView to perform an optimization when
16218 * the view hierarchy contains one or several SurfaceView.
16219 * SurfaceView is always considered transparent, but its children are not,
16220 * therefore all View objects remove themselves from the global transparent
16221 * region (passed as a parameter to this function).
16222 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016223 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016224 *
16225 * @return Returns true if the effective visibility of the view at this
16226 * point is opaque, regardless of the transparent region; returns false
16227 * if it is possible for underlying windows to be seen behind the view.
16228 *
16229 * {@hide}
16230 */
16231 public boolean gatherTransparentRegion(Region region) {
16232 final AttachInfo attachInfo = mAttachInfo;
16233 if (region != null && attachInfo != null) {
16234 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070016235 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016236 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
16237 // remove it from the transparent region.
16238 final int[] location = attachInfo.mTransparentLocation;
16239 getLocationInWindow(location);
16240 region.op(location[0], location[1], location[0] + mRight - mLeft,
16241 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070016242 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016243 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
16244 // exists, so we remove the background drawable's non-transparent
16245 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070016246 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016247 }
16248 }
16249 return true;
16250 }
16251
16252 /**
16253 * Play a sound effect for this view.
16254 *
16255 * <p>The framework will play sound effects for some built in actions, such as
16256 * clicking, but you may wish to play these effects in your widget,
16257 * for instance, for internal navigation.
16258 *
16259 * <p>The sound effect will only be played if sound effects are enabled by the user, and
16260 * {@link #isSoundEffectsEnabled()} is true.
16261 *
16262 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
16263 */
16264 public void playSoundEffect(int soundConstant) {
16265 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
16266 return;
16267 }
16268 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
16269 }
16270
16271 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016272 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016273 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016274 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016275 *
16276 * <p>The framework will provide haptic feedback for some built in actions,
16277 * such as long presses, but you may wish to provide feedback for your
16278 * own widget.
16279 *
16280 * <p>The feedback will only be performed if
16281 * {@link #isHapticFeedbackEnabled()} is true.
16282 *
16283 * @param feedbackConstant One of the constants defined in
16284 * {@link HapticFeedbackConstants}
16285 */
16286 public boolean performHapticFeedback(int feedbackConstant) {
16287 return performHapticFeedback(feedbackConstant, 0);
16288 }
16289
16290 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016291 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016292 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016293 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016294 *
16295 * @param feedbackConstant One of the constants defined in
16296 * {@link HapticFeedbackConstants}
16297 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16298 */
16299 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16300 if (mAttachInfo == null) {
16301 return false;
16302 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016303 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016304 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016305 && !isHapticFeedbackEnabled()) {
16306 return false;
16307 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016308 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16309 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016310 }
16311
16312 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016313 * Request that the visibility of the status bar or other screen/window
16314 * decorations be changed.
16315 *
16316 * <p>This method is used to put the over device UI into temporary modes
16317 * where the user's attention is focused more on the application content,
16318 * by dimming or hiding surrounding system affordances. This is typically
16319 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16320 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16321 * to be placed behind the action bar (and with these flags other system
16322 * affordances) so that smooth transitions between hiding and showing them
16323 * can be done.
16324 *
16325 * <p>Two representative examples of the use of system UI visibility is
16326 * implementing a content browsing application (like a magazine reader)
16327 * and a video playing application.
16328 *
16329 * <p>The first code shows a typical implementation of a View in a content
16330 * browsing application. In this implementation, the application goes
16331 * into a content-oriented mode by hiding the status bar and action bar,
16332 * and putting the navigation elements into lights out mode. The user can
16333 * then interact with content while in this mode. Such an application should
16334 * provide an easy way for the user to toggle out of the mode (such as to
16335 * check information in the status bar or access notifications). In the
16336 * implementation here, this is done simply by tapping on the content.
16337 *
16338 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16339 * content}
16340 *
16341 * <p>This second code sample shows a typical implementation of a View
16342 * in a video playing application. In this situation, while the video is
16343 * playing the application would like to go into a complete full-screen mode,
16344 * to use as much of the display as possible for the video. When in this state
16345 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016346 * touching on the screen to pop the UI out of full screen mode. See
16347 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016348 *
16349 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16350 * content}
16351 *
16352 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16353 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16354 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16355 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016356 */
16357 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016358 if (visibility != mSystemUiVisibility) {
16359 mSystemUiVisibility = visibility;
16360 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16361 mParent.recomputeViewAttributes(this);
16362 }
Joe Onorato664644d2011-01-23 17:53:23 -080016363 }
16364 }
16365
16366 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016367 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16368 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16369 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16370 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16371 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016372 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016373 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016374 return mSystemUiVisibility;
16375 }
16376
Scott Mainec6331b2011-05-24 16:55:56 -070016377 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016378 * Returns the current system UI visibility that is currently set for
16379 * the entire window. This is the combination of the
16380 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16381 * views in the window.
16382 */
16383 public int getWindowSystemUiVisibility() {
16384 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16385 }
16386
16387 /**
16388 * Override to find out when the window's requested system UI visibility
16389 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16390 * This is different from the callbacks recieved through
16391 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16392 * in that this is only telling you about the local request of the window,
16393 * not the actual values applied by the system.
16394 */
16395 public void onWindowSystemUiVisibilityChanged(int visible) {
16396 }
16397
16398 /**
16399 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16400 * the view hierarchy.
16401 */
16402 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16403 onWindowSystemUiVisibilityChanged(visible);
16404 }
16405
16406 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016407 * Set a listener to receive callbacks when the visibility of the system bar changes.
16408 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16409 */
Joe Onorato664644d2011-01-23 17:53:23 -080016410 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016411 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016412 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16413 mParent.recomputeViewAttributes(this);
16414 }
16415 }
16416
16417 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016418 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16419 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016420 */
16421 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016422 ListenerInfo li = mListenerInfo;
16423 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16424 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016425 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016426 }
16427 }
16428
Dianne Hackborncf675782012-05-10 15:07:24 -070016429 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016430 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16431 if (val != mSystemUiVisibility) {
16432 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016433 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016434 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016435 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016436 }
16437
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016438 /** @hide */
16439 public void setDisabledSystemUiVisibility(int flags) {
16440 if (mAttachInfo != null) {
16441 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16442 mAttachInfo.mDisabledSystemUiVisibility = flags;
16443 if (mParent != null) {
16444 mParent.recomputeViewAttributes(this);
16445 }
16446 }
16447 }
16448 }
16449
Joe Onorato664644d2011-01-23 17:53:23 -080016450 /**
Joe Malin32736f02011-01-19 16:14:20 -080016451 * Creates an image that the system displays during the drag and drop
16452 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16453 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16454 * appearance as the given View. The default also positions the center of the drag shadow
16455 * directly under the touch point. If no View is provided (the constructor with no parameters
16456 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16457 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16458 * default is an invisible drag shadow.
16459 * <p>
16460 * You are not required to use the View you provide to the constructor as the basis of the
16461 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16462 * anything you want as the drag shadow.
16463 * </p>
16464 * <p>
16465 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16466 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16467 * size and position of the drag shadow. It uses this data to construct a
16468 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16469 * so that your application can draw the shadow image in the Canvas.
16470 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016471 *
16472 * <div class="special reference">
16473 * <h3>Developer Guides</h3>
16474 * <p>For a guide to implementing drag and drop features, read the
16475 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16476 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016477 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016478 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016479 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016480
16481 /**
Joe Malin32736f02011-01-19 16:14:20 -080016482 * Constructs a shadow image builder based on a View. By default, the resulting drag
16483 * shadow will have the same appearance and dimensions as the View, with the touch point
16484 * over the center of the View.
16485 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016486 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016487 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016488 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016489 }
16490
Christopher Tate17ed60c2011-01-18 12:50:26 -080016491 /**
16492 * Construct a shadow builder object with no associated View. This
16493 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16494 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16495 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016496 * reference to any View object. If they are not overridden, then the result is an
16497 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016498 */
16499 public DragShadowBuilder() {
16500 mView = new WeakReference<View>(null);
16501 }
16502
16503 /**
16504 * Returns the View object that had been passed to the
16505 * {@link #View.DragShadowBuilder(View)}
16506 * constructor. If that View parameter was {@code null} or if the
16507 * {@link #View.DragShadowBuilder()}
16508 * constructor was used to instantiate the builder object, this method will return
16509 * null.
16510 *
16511 * @return The View object associate with this builder object.
16512 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016513 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016514 final public View getView() {
16515 return mView.get();
16516 }
16517
Christopher Tate2c095f32010-10-04 14:13:40 -070016518 /**
Joe Malin32736f02011-01-19 16:14:20 -080016519 * Provides the metrics for the shadow image. These include the dimensions of
16520 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016521 * be centered under the touch location while dragging.
16522 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016523 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016524 * same as the dimensions of the View itself and centers the shadow under
16525 * the touch point.
16526 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016527 *
Joe Malin32736f02011-01-19 16:14:20 -080016528 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16529 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16530 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16531 * image.
16532 *
16533 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16534 * shadow image that should be underneath the touch point during the drag and drop
16535 * operation. Your application must set {@link android.graphics.Point#x} to the
16536 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016537 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016538 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016539 final View view = mView.get();
16540 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016541 shadowSize.set(view.getWidth(), view.getHeight());
16542 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016543 } else {
16544 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16545 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016546 }
16547
16548 /**
Joe Malin32736f02011-01-19 16:14:20 -080016549 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16550 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016551 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016552 *
Joe Malin32736f02011-01-19 16:14:20 -080016553 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016554 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016555 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016556 final View view = mView.get();
16557 if (view != null) {
16558 view.draw(canvas);
16559 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016560 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016561 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016562 }
16563 }
16564
16565 /**
Joe Malin32736f02011-01-19 16:14:20 -080016566 * Starts a drag and drop operation. When your application calls this method, it passes a
16567 * {@link android.view.View.DragShadowBuilder} object to the system. The
16568 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16569 * to get metrics for the drag shadow, and then calls the object's
16570 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16571 * <p>
16572 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16573 * drag events to all the View objects in your application that are currently visible. It does
16574 * this either by calling the View object's drag listener (an implementation of
16575 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16576 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16577 * Both are passed a {@link android.view.DragEvent} object that has a
16578 * {@link android.view.DragEvent#getAction()} value of
16579 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16580 * </p>
16581 * <p>
16582 * Your application can invoke startDrag() on any attached View object. The View object does not
16583 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16584 * be related to the View the user selected for dragging.
16585 * </p>
16586 * @param data A {@link android.content.ClipData} object pointing to the data to be
16587 * transferred by the drag and drop operation.
16588 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16589 * drag shadow.
16590 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16591 * drop operation. This Object is put into every DragEvent object sent by the system during the
16592 * current drag.
16593 * <p>
16594 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16595 * to the target Views. For example, it can contain flags that differentiate between a
16596 * a copy operation and a move operation.
16597 * </p>
16598 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16599 * so the parameter should be set to 0.
16600 * @return {@code true} if the method completes successfully, or
16601 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16602 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016603 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016604 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016605 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016606 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016607 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016608 }
16609 boolean okay = false;
16610
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016611 Point shadowSize = new Point();
16612 Point shadowTouchPoint = new Point();
16613 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016614
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016615 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16616 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16617 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016618 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016619
Chris Tatea32dcf72010-10-14 12:13:50 -070016620 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016621 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16622 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016623 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016624 Surface surface = new Surface();
16625 try {
16626 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016627 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016628 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016629 + " surface=" + surface);
16630 if (token != null) {
16631 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016632 try {
Chris Tate6b391282010-10-14 15:48:59 -070016633 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016634 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016635 } finally {
16636 surface.unlockCanvasAndPost(canvas);
16637 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016638
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016639 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016640
16641 // Cache the local state object for delivery with DragEvents
16642 root.setLocalDragState(myLocalState);
16643
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016644 // repurpose 'shadowSize' for the last touch point
16645 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016646
Christopher Tatea53146c2010-09-07 11:57:52 -070016647 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016648 shadowSize.x, shadowSize.y,
16649 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016650 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016651
16652 // Off and running! Release our local surface instance; the drag
16653 // shadow surface is now managed by the system process.
16654 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016655 }
16656 } catch (Exception e) {
16657 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16658 surface.destroy();
16659 }
16660
16661 return okay;
16662 }
16663
Christopher Tatea53146c2010-09-07 11:57:52 -070016664 /**
Joe Malin32736f02011-01-19 16:14:20 -080016665 * Handles drag events sent by the system following a call to
16666 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16667 *<p>
16668 * When the system calls this method, it passes a
16669 * {@link android.view.DragEvent} object. A call to
16670 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16671 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16672 * operation.
16673 * @param event The {@link android.view.DragEvent} sent by the system.
16674 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16675 * in DragEvent, indicating the type of drag event represented by this object.
16676 * @return {@code true} if the method was successful, otherwise {@code false}.
16677 * <p>
16678 * The method should return {@code true} in response to an action type of
16679 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16680 * operation.
16681 * </p>
16682 * <p>
16683 * The method should also return {@code true} in response to an action type of
16684 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16685 * {@code false} if it didn't.
16686 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016687 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016688 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016689 return false;
16690 }
16691
16692 /**
Joe Malin32736f02011-01-19 16:14:20 -080016693 * Detects if this View is enabled and has a drag event listener.
16694 * If both are true, then it calls the drag event listener with the
16695 * {@link android.view.DragEvent} it received. If the drag event listener returns
16696 * {@code true}, then dispatchDragEvent() returns {@code true}.
16697 * <p>
16698 * For all other cases, the method calls the
16699 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16700 * method and returns its result.
16701 * </p>
16702 * <p>
16703 * This ensures that a drag event is always consumed, even if the View does not have a drag
16704 * event listener. However, if the View has a listener and the listener returns true, then
16705 * onDragEvent() is not called.
16706 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016707 */
16708 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016709 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016710 ListenerInfo li = mListenerInfo;
16711 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16712 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016713 return true;
16714 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016715 return onDragEvent(event);
16716 }
16717
Christopher Tate3d4bf172011-03-28 16:16:46 -070016718 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016719 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016720 }
16721
Christopher Tatea53146c2010-09-07 11:57:52 -070016722 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016723 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16724 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016725 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016726 */
16727 public void onCloseSystemDialogs(String reason) {
16728 }
Joe Malin32736f02011-01-19 16:14:20 -080016729
Dianne Hackbornffa42482009-09-23 22:20:11 -070016730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016731 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016732 * update a Region being computed for
16733 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016734 * that any non-transparent parts of the Drawable are removed from the
16735 * given transparent region.
16736 *
16737 * @param dr The Drawable whose transparency is to be applied to the region.
16738 * @param region A Region holding the current transparency information,
16739 * where any parts of the region that are set are considered to be
16740 * transparent. On return, this region will be modified to have the
16741 * transparency information reduced by the corresponding parts of the
16742 * Drawable that are not transparent.
16743 * {@hide}
16744 */
16745 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16746 if (DBG) {
16747 Log.i("View", "Getting transparent region for: " + this);
16748 }
16749 final Region r = dr.getTransparentRegion();
16750 final Rect db = dr.getBounds();
16751 final AttachInfo attachInfo = mAttachInfo;
16752 if (r != null && attachInfo != null) {
16753 final int w = getRight()-getLeft();
16754 final int h = getBottom()-getTop();
16755 if (db.left > 0) {
16756 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16757 r.op(0, 0, db.left, h, Region.Op.UNION);
16758 }
16759 if (db.right < w) {
16760 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16761 r.op(db.right, 0, w, h, Region.Op.UNION);
16762 }
16763 if (db.top > 0) {
16764 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16765 r.op(0, 0, w, db.top, Region.Op.UNION);
16766 }
16767 if (db.bottom < h) {
16768 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16769 r.op(0, db.bottom, w, h, Region.Op.UNION);
16770 }
16771 final int[] location = attachInfo.mTransparentLocation;
16772 getLocationInWindow(location);
16773 r.translate(location[0], location[1]);
16774 region.op(r, Region.Op.INTERSECT);
16775 } else {
16776 region.op(db, Region.Op.DIFFERENCE);
16777 }
16778 }
16779
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016780 private void checkForLongClick(int delayOffset) {
16781 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16782 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016783
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016784 if (mPendingCheckForLongPress == null) {
16785 mPendingCheckForLongPress = new CheckForLongPress();
16786 }
16787 mPendingCheckForLongPress.rememberWindowAttachCount();
16788 postDelayed(mPendingCheckForLongPress,
16789 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016791 }
16792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016793 /**
16794 * Inflate a view from an XML resource. This convenience method wraps the {@link
16795 * LayoutInflater} class, which provides a full range of options for view inflation.
16796 *
16797 * @param context The Context object for your activity or application.
16798 * @param resource The resource ID to inflate
16799 * @param root A view group that will be the parent. Used to properly inflate the
16800 * layout_* parameters.
16801 * @see LayoutInflater
16802 */
16803 public static View inflate(Context context, int resource, ViewGroup root) {
16804 LayoutInflater factory = LayoutInflater.from(context);
16805 return factory.inflate(resource, root);
16806 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016808 /**
Adam Powell637d3372010-08-25 14:37:03 -070016809 * Scroll the view with standard behavior for scrolling beyond the normal
16810 * content boundaries. Views that call this method should override
16811 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16812 * results of an over-scroll operation.
16813 *
16814 * Views can use this method to handle any touch or fling-based scrolling.
16815 *
16816 * @param deltaX Change in X in pixels
16817 * @param deltaY Change in Y in pixels
16818 * @param scrollX Current X scroll value in pixels before applying deltaX
16819 * @param scrollY Current Y scroll value in pixels before applying deltaY
16820 * @param scrollRangeX Maximum content scroll range along the X axis
16821 * @param scrollRangeY Maximum content scroll range along the Y axis
16822 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16823 * along the X axis.
16824 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16825 * along the Y axis.
16826 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16827 * @return true if scrolling was clamped to an over-scroll boundary along either
16828 * axis, false otherwise.
16829 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016830 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016831 protected boolean overScrollBy(int deltaX, int deltaY,
16832 int scrollX, int scrollY,
16833 int scrollRangeX, int scrollRangeY,
16834 int maxOverScrollX, int maxOverScrollY,
16835 boolean isTouchEvent) {
16836 final int overScrollMode = mOverScrollMode;
16837 final boolean canScrollHorizontal =
16838 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16839 final boolean canScrollVertical =
16840 computeVerticalScrollRange() > computeVerticalScrollExtent();
16841 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16842 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16843 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16844 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16845
16846 int newScrollX = scrollX + deltaX;
16847 if (!overScrollHorizontal) {
16848 maxOverScrollX = 0;
16849 }
16850
16851 int newScrollY = scrollY + deltaY;
16852 if (!overScrollVertical) {
16853 maxOverScrollY = 0;
16854 }
16855
16856 // Clamp values if at the limits and record
16857 final int left = -maxOverScrollX;
16858 final int right = maxOverScrollX + scrollRangeX;
16859 final int top = -maxOverScrollY;
16860 final int bottom = maxOverScrollY + scrollRangeY;
16861
16862 boolean clampedX = false;
16863 if (newScrollX > right) {
16864 newScrollX = right;
16865 clampedX = true;
16866 } else if (newScrollX < left) {
16867 newScrollX = left;
16868 clampedX = true;
16869 }
16870
16871 boolean clampedY = false;
16872 if (newScrollY > bottom) {
16873 newScrollY = bottom;
16874 clampedY = true;
16875 } else if (newScrollY < top) {
16876 newScrollY = top;
16877 clampedY = true;
16878 }
16879
16880 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16881
16882 return clampedX || clampedY;
16883 }
16884
16885 /**
16886 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16887 * respond to the results of an over-scroll operation.
16888 *
16889 * @param scrollX New X scroll value in pixels
16890 * @param scrollY New Y scroll value in pixels
16891 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16892 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16893 */
16894 protected void onOverScrolled(int scrollX, int scrollY,
16895 boolean clampedX, boolean clampedY) {
16896 // Intentionally empty.
16897 }
16898
16899 /**
16900 * Returns the over-scroll mode for this view. The result will be
16901 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16902 * (allow over-scrolling only if the view content is larger than the container),
16903 * or {@link #OVER_SCROLL_NEVER}.
16904 *
16905 * @return This view's over-scroll mode.
16906 */
16907 public int getOverScrollMode() {
16908 return mOverScrollMode;
16909 }
16910
16911 /**
16912 * Set the over-scroll mode for this view. Valid over-scroll modes are
16913 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16914 * (allow over-scrolling only if the view content is larger than the container),
16915 * or {@link #OVER_SCROLL_NEVER}.
16916 *
16917 * Setting the over-scroll mode of a view will have an effect only if the
16918 * view is capable of scrolling.
16919 *
16920 * @param overScrollMode The new over-scroll mode for this view.
16921 */
16922 public void setOverScrollMode(int overScrollMode) {
16923 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16924 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16925 overScrollMode != OVER_SCROLL_NEVER) {
16926 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16927 }
16928 mOverScrollMode = overScrollMode;
16929 }
16930
16931 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016932 * Gets a scale factor that determines the distance the view should scroll
16933 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16934 * @return The vertical scroll scale factor.
16935 * @hide
16936 */
16937 protected float getVerticalScrollFactor() {
16938 if (mVerticalScrollFactor == 0) {
16939 TypedValue outValue = new TypedValue();
16940 if (!mContext.getTheme().resolveAttribute(
16941 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16942 throw new IllegalStateException(
16943 "Expected theme to define listPreferredItemHeight.");
16944 }
16945 mVerticalScrollFactor = outValue.getDimension(
16946 mContext.getResources().getDisplayMetrics());
16947 }
16948 return mVerticalScrollFactor;
16949 }
16950
16951 /**
16952 * Gets a scale factor that determines the distance the view should scroll
16953 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16954 * @return The horizontal scroll scale factor.
16955 * @hide
16956 */
16957 protected float getHorizontalScrollFactor() {
16958 // TODO: Should use something else.
16959 return getVerticalScrollFactor();
16960 }
16961
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016962 /**
16963 * Return the value specifying the text direction or policy that was set with
16964 * {@link #setTextDirection(int)}.
16965 *
16966 * @return the defined text direction. It can be one of:
16967 *
16968 * {@link #TEXT_DIRECTION_INHERIT},
16969 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16970 * {@link #TEXT_DIRECTION_ANY_RTL},
16971 * {@link #TEXT_DIRECTION_LTR},
16972 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016973 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016974 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016975 * @attr ref android.R.styleable#View_textDirection
16976 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016977 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016978 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016979 @ViewDebug.ExportedProperty(category = "text", mapping = {
16980 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16981 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16982 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16983 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16984 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16985 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16986 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016987 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016988 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016989 }
16990
16991 /**
16992 * Set the text direction.
16993 *
16994 * @param textDirection the direction to set. Should be one of:
16995 *
16996 * {@link #TEXT_DIRECTION_INHERIT},
16997 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16998 * {@link #TEXT_DIRECTION_ANY_RTL},
16999 * {@link #TEXT_DIRECTION_LTR},
17000 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017001 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017002 *
17003 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
17004 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
17005 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080017006 *
17007 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017008 */
17009 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070017010 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070017011 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070017012 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070017013 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017014 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070017015 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017016 // Do resolution
17017 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017018 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070017019 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017020 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017021 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070017022 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017023 }
17024 }
17025
17026 /**
17027 * Return the resolved text direction.
17028 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017029 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017030 *
Doug Feltcb3791202011-07-07 11:57:48 -070017031 * {@link #TEXT_DIRECTION_FIRST_STRONG}
17032 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017033 * {@link #TEXT_DIRECTION_LTR},
17034 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017035 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080017036 *
17037 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017038 */
Fabrice Di Meglio83f15e62013-03-19 11:45:56 -070017039 @ViewDebug.ExportedProperty(category = "text", mapping = {
17040 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
17041 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
17042 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
17043 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
17044 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
17045 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
17046 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070017047 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017048 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017049 }
17050
17051 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017052 * Resolve the text direction.
17053 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017054 * @return true if resolution has been done, false otherwise.
17055 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017056 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017057 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017058 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017059 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017060 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017061
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070017062 if (hasRtlSupport()) {
17063 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070017064 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070017065 switch(textDirection) {
17066 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017067 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070017068 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070017069 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017070 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017071 return false;
17072 }
17073
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017074 // Parent has not yet resolved, so we still return the default
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017075 if (!mParent.isTextDirectionResolved()) {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017076 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
17077 // Resolution will need to happen again later
17078 return false;
17079 }
17080
17081 // Set current resolved direction to the same value as the parent's one
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017082 final int parentResolvedDirection = mParent.getTextDirection();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017083 switch (parentResolvedDirection) {
17084 case TEXT_DIRECTION_FIRST_STRONG:
17085 case TEXT_DIRECTION_ANY_RTL:
17086 case TEXT_DIRECTION_LTR:
17087 case TEXT_DIRECTION_RTL:
17088 case TEXT_DIRECTION_LOCALE:
17089 mPrivateFlags2 |=
17090 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
17091 break;
17092 default:
17093 // Default resolved direction is "first strong" heuristic
17094 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017095 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070017096 break;
17097 case TEXT_DIRECTION_FIRST_STRONG:
17098 case TEXT_DIRECTION_ANY_RTL:
17099 case TEXT_DIRECTION_LTR:
17100 case TEXT_DIRECTION_RTL:
17101 case TEXT_DIRECTION_LOCALE:
17102 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070017103 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070017104 break;
17105 default:
17106 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070017107 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070017108 }
17109 } else {
17110 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070017111 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080017112 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017113
17114 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017115 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017116 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080017117 }
17118
17119 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017120 * Check if text direction resolution can be done.
17121 *
17122 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017123 *
17124 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017125 */
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017126 public boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070017127 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017128 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017129 return (mParent != null) && mParent.canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070017130 default:
17131 return true;
17132 }
17133 }
17134
17135 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017136 * Reset resolved text direction. Text direction will be resolved during a call to
17137 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017138 *
17139 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080017140 */
17141 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017142 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017143 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017144 // Set to default value
17145 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070017146 }
17147
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017148 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017149 * @return true if text direction is inherited.
17150 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070017151 * @hide
17152 */
17153 public boolean isTextDirectionInherited() {
17154 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
17155 }
17156
17157 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017158 * @return true if text direction is resolved.
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017159 *
17160 * @hide
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017161 */
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017162 public boolean isTextDirectionResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017163 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
17164 }
17165
17166 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017167 * Return the value specifying the text alignment or policy that was set with
17168 * {@link #setTextAlignment(int)}.
17169 *
17170 * @return the defined text alignment. It can be one of:
17171 *
17172 * {@link #TEXT_ALIGNMENT_INHERIT},
17173 * {@link #TEXT_ALIGNMENT_GRAVITY},
17174 * {@link #TEXT_ALIGNMENT_CENTER},
17175 * {@link #TEXT_ALIGNMENT_TEXT_START},
17176 * {@link #TEXT_ALIGNMENT_TEXT_END},
17177 * {@link #TEXT_ALIGNMENT_VIEW_START},
17178 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017179 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080017180 * @attr ref android.R.styleable#View_textAlignment
17181 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017182 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017183 */
17184 @ViewDebug.ExportedProperty(category = "text", mapping = {
17185 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
17186 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
17187 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
17188 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
17189 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
17190 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
17191 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
17192 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017193 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017194 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017195 }
17196
17197 /**
17198 * Set the text alignment.
17199 *
17200 * @param textAlignment The text alignment to set. Should be one of
17201 *
17202 * {@link #TEXT_ALIGNMENT_INHERIT},
17203 * {@link #TEXT_ALIGNMENT_GRAVITY},
17204 * {@link #TEXT_ALIGNMENT_CENTER},
17205 * {@link #TEXT_ALIGNMENT_TEXT_START},
17206 * {@link #TEXT_ALIGNMENT_TEXT_END},
17207 * {@link #TEXT_ALIGNMENT_VIEW_START},
17208 * {@link #TEXT_ALIGNMENT_VIEW_END}
17209 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017210 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
17211 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
17212 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
17213 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017214 * @attr ref android.R.styleable#View_textAlignment
17215 */
17216 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017217 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017218 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017219 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017220 resetResolvedTextAlignment();
17221 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017222 mPrivateFlags2 |=
17223 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
17224 // Do resolution
17225 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017226 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070017227 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017228 // Refresh
17229 requestLayout();
17230 invalidate(true);
17231 }
17232 }
17233
17234 /**
17235 * Return the resolved text alignment.
17236 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017237 * @return the resolved text alignment. Returns one of:
17238 *
17239 * {@link #TEXT_ALIGNMENT_GRAVITY},
17240 * {@link #TEXT_ALIGNMENT_CENTER},
17241 * {@link #TEXT_ALIGNMENT_TEXT_START},
17242 * {@link #TEXT_ALIGNMENT_TEXT_END},
17243 * {@link #TEXT_ALIGNMENT_VIEW_START},
17244 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080017245 *
17246 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017247 */
17248 @ViewDebug.ExportedProperty(category = "text", mapping = {
17249 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
17250 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
17251 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
17252 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
17253 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
17254 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
17255 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
17256 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017257 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017258 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
17259 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017260 }
17261
17262 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017263 * Resolve the text alignment.
17264 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017265 * @return true if resolution has been done, false otherwise.
17266 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017267 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017268 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017269 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017270 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017271 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017272
17273 if (hasRtlSupport()) {
17274 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017275 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017276 switch (textAlignment) {
17277 case TEXT_ALIGNMENT_INHERIT:
17278 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017279 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017280 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070017281 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017282 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017283 return false;
17284 }
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017285
17286 // Parent has not yet resolved, so we still return the default
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017287 if (!mParent.isTextAlignmentResolved()) {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017288 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
17289 // Resolution will need to happen again later
17290 return false;
17291 }
17292
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017293 final int parentResolvedTextAlignment = mParent.getTextAlignment();
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017294 switch (parentResolvedTextAlignment) {
17295 case TEXT_ALIGNMENT_GRAVITY:
17296 case TEXT_ALIGNMENT_TEXT_START:
17297 case TEXT_ALIGNMENT_TEXT_END:
17298 case TEXT_ALIGNMENT_CENTER:
17299 case TEXT_ALIGNMENT_VIEW_START:
17300 case TEXT_ALIGNMENT_VIEW_END:
17301 // Resolved text alignment is the same as the parent resolved
17302 // text alignment
17303 mPrivateFlags2 |=
17304 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17305 break;
17306 default:
17307 // Use default resolved text alignment
17308 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017309 }
17310 break;
17311 case TEXT_ALIGNMENT_GRAVITY:
17312 case TEXT_ALIGNMENT_TEXT_START:
17313 case TEXT_ALIGNMENT_TEXT_END:
17314 case TEXT_ALIGNMENT_CENTER:
17315 case TEXT_ALIGNMENT_VIEW_START:
17316 case TEXT_ALIGNMENT_VIEW_END:
17317 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017318 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017319 break;
17320 default:
17321 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017322 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017323 }
17324 } else {
17325 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017326 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017327 }
17328
17329 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017330 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017331 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017332 }
17333
17334 /**
17335 * Check if text alignment resolution can be done.
17336 *
17337 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017338 *
17339 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017340 */
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017341 public boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017342 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017343 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017344 return (mParent != null) && mParent.canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017345 default:
17346 return true;
17347 }
17348 }
17349
17350 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017351 * Reset resolved text alignment. Text alignment will be resolved during a call to
17352 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017353 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017354 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017355 */
17356 public void resetResolvedTextAlignment() {
17357 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017358 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017359 // Set to default
17360 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017361 }
17362
Adam Powella9108a22012-07-18 11:18:09 -070017363 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017364 * @return true if text alignment is inherited.
17365 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017366 * @hide
17367 */
17368 public boolean isTextAlignmentInherited() {
17369 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17370 }
17371
17372 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017373 * @return true if text alignment is resolved.
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017374 *
17375 * @hide
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017376 */
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -080017377 public boolean isTextAlignmentResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017378 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17379 }
17380
17381 /**
Adam Powella9108a22012-07-18 11:18:09 -070017382 * Generate a value suitable for use in {@link #setId(int)}.
17383 * This value will not collide with ID values generated at build time by aapt for R.id.
17384 *
17385 * @return a generated ID value
17386 */
17387 public static int generateViewId() {
17388 for (;;) {
17389 final int result = sNextGeneratedId.get();
17390 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17391 int newValue = result + 1;
17392 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17393 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17394 return result;
17395 }
17396 }
17397 }
17398
Chet Haaseb39f0512011-05-24 14:36:40 -070017399 //
17400 // Properties
17401 //
17402 /**
17403 * A Property wrapper around the <code>alpha</code> functionality handled by the
17404 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17405 */
Chet Haased47f1532011-12-16 11:18:52 -080017406 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017407 @Override
17408 public void setValue(View object, float value) {
17409 object.setAlpha(value);
17410 }
17411
17412 @Override
17413 public Float get(View object) {
17414 return object.getAlpha();
17415 }
17416 };
17417
17418 /**
17419 * A Property wrapper around the <code>translationX</code> functionality handled by the
17420 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17421 */
Chet Haased47f1532011-12-16 11:18:52 -080017422 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017423 @Override
17424 public void setValue(View object, float value) {
17425 object.setTranslationX(value);
17426 }
17427
17428 @Override
17429 public Float get(View object) {
17430 return object.getTranslationX();
17431 }
17432 };
17433
17434 /**
17435 * A Property wrapper around the <code>translationY</code> functionality handled by the
17436 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17437 */
Chet Haased47f1532011-12-16 11:18:52 -080017438 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017439 @Override
17440 public void setValue(View object, float value) {
17441 object.setTranslationY(value);
17442 }
17443
17444 @Override
17445 public Float get(View object) {
17446 return object.getTranslationY();
17447 }
17448 };
17449
17450 /**
17451 * A Property wrapper around the <code>x</code> functionality handled by the
17452 * {@link View#setX(float)} and {@link View#getX()} methods.
17453 */
Chet Haased47f1532011-12-16 11:18:52 -080017454 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017455 @Override
17456 public void setValue(View object, float value) {
17457 object.setX(value);
17458 }
17459
17460 @Override
17461 public Float get(View object) {
17462 return object.getX();
17463 }
17464 };
17465
17466 /**
17467 * A Property wrapper around the <code>y</code> functionality handled by the
17468 * {@link View#setY(float)} and {@link View#getY()} methods.
17469 */
Chet Haased47f1532011-12-16 11:18:52 -080017470 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017471 @Override
17472 public void setValue(View object, float value) {
17473 object.setY(value);
17474 }
17475
17476 @Override
17477 public Float get(View object) {
17478 return object.getY();
17479 }
17480 };
17481
17482 /**
17483 * A Property wrapper around the <code>rotation</code> functionality handled by the
17484 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17485 */
Chet Haased47f1532011-12-16 11:18:52 -080017486 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017487 @Override
17488 public void setValue(View object, float value) {
17489 object.setRotation(value);
17490 }
17491
17492 @Override
17493 public Float get(View object) {
17494 return object.getRotation();
17495 }
17496 };
17497
17498 /**
17499 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17500 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17501 */
Chet Haased47f1532011-12-16 11:18:52 -080017502 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017503 @Override
17504 public void setValue(View object, float value) {
17505 object.setRotationX(value);
17506 }
17507
17508 @Override
17509 public Float get(View object) {
17510 return object.getRotationX();
17511 }
17512 };
17513
17514 /**
17515 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17516 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17517 */
Chet Haased47f1532011-12-16 11:18:52 -080017518 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017519 @Override
17520 public void setValue(View object, float value) {
17521 object.setRotationY(value);
17522 }
17523
17524 @Override
17525 public Float get(View object) {
17526 return object.getRotationY();
17527 }
17528 };
17529
17530 /**
17531 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17532 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17533 */
Chet Haased47f1532011-12-16 11:18:52 -080017534 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017535 @Override
17536 public void setValue(View object, float value) {
17537 object.setScaleX(value);
17538 }
17539
17540 @Override
17541 public Float get(View object) {
17542 return object.getScaleX();
17543 }
17544 };
17545
17546 /**
17547 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17548 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17549 */
Chet Haased47f1532011-12-16 11:18:52 -080017550 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017551 @Override
17552 public void setValue(View object, float value) {
17553 object.setScaleY(value);
17554 }
17555
17556 @Override
17557 public Float get(View object) {
17558 return object.getScaleY();
17559 }
17560 };
17561
Jeff Brown33bbfd22011-02-24 20:55:35 -080017562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017563 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17564 * Each MeasureSpec represents a requirement for either the width or the height.
17565 * A MeasureSpec is comprised of a size and a mode. There are three possible
17566 * modes:
17567 * <dl>
17568 * <dt>UNSPECIFIED</dt>
17569 * <dd>
17570 * The parent has not imposed any constraint on the child. It can be whatever size
17571 * it wants.
17572 * </dd>
17573 *
17574 * <dt>EXACTLY</dt>
17575 * <dd>
17576 * The parent has determined an exact size for the child. The child is going to be
17577 * given those bounds regardless of how big it wants to be.
17578 * </dd>
17579 *
17580 * <dt>AT_MOST</dt>
17581 * <dd>
17582 * The child can be as large as it wants up to the specified size.
17583 * </dd>
17584 * </dl>
17585 *
17586 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17587 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17588 */
17589 public static class MeasureSpec {
17590 private static final int MODE_SHIFT = 30;
17591 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17592
17593 /**
17594 * Measure specification mode: The parent has not imposed any constraint
17595 * on the child. It can be whatever size it wants.
17596 */
17597 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17598
17599 /**
17600 * Measure specification mode: The parent has determined an exact size
17601 * for the child. The child is going to be given those bounds regardless
17602 * of how big it wants to be.
17603 */
17604 public static final int EXACTLY = 1 << MODE_SHIFT;
17605
17606 /**
17607 * Measure specification mode: The child can be as large as it wants up
17608 * to the specified size.
17609 */
17610 public static final int AT_MOST = 2 << MODE_SHIFT;
17611
17612 /**
17613 * Creates a measure specification based on the supplied size and mode.
17614 *
17615 * The mode must always be one of the following:
17616 * <ul>
17617 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17618 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17619 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17620 * </ul>
17621 *
Adam Powell2c8cc972012-12-07 18:04:51 -080017622 * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
17623 * implementation was such that the order of arguments did not matter
17624 * and overflow in either value could impact the resulting MeasureSpec.
17625 * {@link android.widget.RelativeLayout} was affected by this bug.
17626 * Apps targeting API levels greater than 17 will get the fixed, more strict
17627 * behavior.</p>
17628 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017629 * @param size the size of the measure specification
17630 * @param mode the mode of the measure specification
17631 * @return the measure specification based on size and mode
17632 */
17633 public static int makeMeasureSpec(int size, int mode) {
Adam Powell7da4b732012-12-07 15:28:33 -080017634 if (sUseBrokenMakeMeasureSpec) {
17635 return size + mode;
17636 } else {
17637 return (size & ~MODE_MASK) | (mode & MODE_MASK);
17638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017639 }
17640
17641 /**
17642 * Extracts the mode from the supplied measure specification.
17643 *
17644 * @param measureSpec the measure specification to extract the mode from
17645 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17646 * {@link android.view.View.MeasureSpec#AT_MOST} or
17647 * {@link android.view.View.MeasureSpec#EXACTLY}
17648 */
17649 public static int getMode(int measureSpec) {
17650 return (measureSpec & MODE_MASK);
17651 }
17652
17653 /**
17654 * Extracts the size from the supplied measure specification.
17655 *
17656 * @param measureSpec the measure specification to extract the size from
17657 * @return the size in pixels defined in the supplied measure specification
17658 */
17659 public static int getSize(int measureSpec) {
17660 return (measureSpec & ~MODE_MASK);
17661 }
17662
Philip Milne7b757812012-09-19 18:13:44 -070017663 static int adjust(int measureSpec, int delta) {
17664 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17665 }
17666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017667 /**
17668 * Returns a String representation of the specified measure
17669 * specification.
17670 *
17671 * @param measureSpec the measure specification to convert to a String
17672 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17673 */
17674 public static String toString(int measureSpec) {
17675 int mode = getMode(measureSpec);
17676 int size = getSize(measureSpec);
17677
17678 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17679
17680 if (mode == UNSPECIFIED)
17681 sb.append("UNSPECIFIED ");
17682 else if (mode == EXACTLY)
17683 sb.append("EXACTLY ");
17684 else if (mode == AT_MOST)
17685 sb.append("AT_MOST ");
17686 else
17687 sb.append(mode).append(" ");
17688
17689 sb.append(size);
17690 return sb.toString();
17691 }
17692 }
17693
17694 class CheckForLongPress implements Runnable {
17695
17696 private int mOriginalWindowAttachCount;
17697
17698 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017699 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017700 && mOriginalWindowAttachCount == mWindowAttachCount) {
17701 if (performLongClick()) {
17702 mHasPerformedLongPress = true;
17703 }
17704 }
17705 }
17706
17707 public void rememberWindowAttachCount() {
17708 mOriginalWindowAttachCount = mWindowAttachCount;
17709 }
17710 }
Joe Malin32736f02011-01-19 16:14:20 -080017711
Adam Powelle14579b2009-12-16 18:39:52 -080017712 private final class CheckForTap implements Runnable {
17713 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017714 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017715 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017716 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017717 }
17718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017719
Adam Powella35d7682010-03-12 14:48:13 -080017720 private final class PerformClick implements Runnable {
17721 public void run() {
17722 performClick();
17723 }
17724 }
17725
Dianne Hackborn63042d62011-01-26 18:56:29 -080017726 /** @hide */
17727 public void hackTurnOffWindowResizeAnim(boolean off) {
17728 mAttachInfo.mTurnOffWindowResizeAnim = off;
17729 }
Joe Malin32736f02011-01-19 16:14:20 -080017730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017731 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017732 * This method returns a ViewPropertyAnimator object, which can be used to animate
17733 * specific properties on this View.
17734 *
17735 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17736 */
17737 public ViewPropertyAnimator animate() {
17738 if (mAnimator == null) {
17739 mAnimator = new ViewPropertyAnimator(this);
17740 }
17741 return mAnimator;
17742 }
17743
17744 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017745 * Interface definition for a callback to be invoked when a hardware key event is
17746 * dispatched to this view. The callback will be invoked before the key event is
17747 * given to the view. This is only useful for hardware keyboards; a software input
17748 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017749 */
17750 public interface OnKeyListener {
17751 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017752 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017753 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017754 * <p>Key presses in software keyboards will generally NOT trigger this method,
17755 * although some may elect to do so in some situations. Do not assume a
17756 * software input method has to be key-based; even if it is, it may use key presses
17757 * in a different way than you expect, so there is no way to reliably catch soft
17758 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017759 *
17760 * @param v The view the key has been dispatched to.
17761 * @param keyCode The code for the physical key that was pressed
17762 * @param event The KeyEvent object containing full information about
17763 * the event.
17764 * @return True if the listener has consumed the event, false otherwise.
17765 */
17766 boolean onKey(View v, int keyCode, KeyEvent event);
17767 }
17768
17769 /**
17770 * Interface definition for a callback to be invoked when a touch event is
17771 * dispatched to this view. The callback will be invoked before the touch
17772 * event is given to the view.
17773 */
17774 public interface OnTouchListener {
17775 /**
17776 * Called when a touch event is dispatched to a view. This allows listeners to
17777 * get a chance to respond before the target view.
17778 *
17779 * @param v The view the touch event has been dispatched to.
17780 * @param event The MotionEvent object containing full information about
17781 * the event.
17782 * @return True if the listener has consumed the event, false otherwise.
17783 */
17784 boolean onTouch(View v, MotionEvent event);
17785 }
17786
17787 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017788 * Interface definition for a callback to be invoked when a hover event is
17789 * dispatched to this view. The callback will be invoked before the hover
17790 * event is given to the view.
17791 */
17792 public interface OnHoverListener {
17793 /**
17794 * Called when a hover event is dispatched to a view. This allows listeners to
17795 * get a chance to respond before the target view.
17796 *
17797 * @param v The view the hover event has been dispatched to.
17798 * @param event The MotionEvent object containing full information about
17799 * the event.
17800 * @return True if the listener has consumed the event, false otherwise.
17801 */
17802 boolean onHover(View v, MotionEvent event);
17803 }
17804
17805 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017806 * Interface definition for a callback to be invoked when a generic motion event is
17807 * dispatched to this view. The callback will be invoked before the generic motion
17808 * event is given to the view.
17809 */
17810 public interface OnGenericMotionListener {
17811 /**
17812 * Called when a generic motion event is dispatched to a view. This allows listeners to
17813 * get a chance to respond before the target view.
17814 *
17815 * @param v The view the generic motion event has been dispatched to.
17816 * @param event The MotionEvent object containing full information about
17817 * the event.
17818 * @return True if the listener has consumed the event, false otherwise.
17819 */
17820 boolean onGenericMotion(View v, MotionEvent event);
17821 }
17822
17823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017824 * Interface definition for a callback to be invoked when a view has been clicked and held.
17825 */
17826 public interface OnLongClickListener {
17827 /**
17828 * Called when a view has been clicked and held.
17829 *
17830 * @param v The view that was clicked and held.
17831 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017832 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017833 */
17834 boolean onLongClick(View v);
17835 }
17836
17837 /**
Chris Tate32affef2010-10-18 15:29:21 -070017838 * Interface definition for a callback to be invoked when a drag is being dispatched
17839 * to this view. The callback will be invoked before the hosting view's own
17840 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17841 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017842 *
17843 * <div class="special reference">
17844 * <h3>Developer Guides</h3>
17845 * <p>For a guide to implementing drag and drop features, read the
17846 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17847 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017848 */
17849 public interface OnDragListener {
17850 /**
17851 * Called when a drag event is dispatched to a view. This allows listeners
17852 * to get a chance to override base View behavior.
17853 *
Joe Malin32736f02011-01-19 16:14:20 -080017854 * @param v The View that received the drag event.
17855 * @param event The {@link android.view.DragEvent} object for the drag event.
17856 * @return {@code true} if the drag event was handled successfully, or {@code false}
17857 * if the drag event was not handled. Note that {@code false} will trigger the View
17858 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017859 */
17860 boolean onDrag(View v, DragEvent event);
17861 }
17862
17863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017864 * Interface definition for a callback to be invoked when the focus state of
17865 * a view changed.
17866 */
17867 public interface OnFocusChangeListener {
17868 /**
17869 * Called when the focus state of a view has changed.
17870 *
17871 * @param v The view whose state has changed.
17872 * @param hasFocus The new focus state of v.
17873 */
17874 void onFocusChange(View v, boolean hasFocus);
17875 }
17876
17877 /**
17878 * Interface definition for a callback to be invoked when a view is clicked.
17879 */
17880 public interface OnClickListener {
17881 /**
17882 * Called when a view has been clicked.
17883 *
17884 * @param v The view that was clicked.
17885 */
17886 void onClick(View v);
17887 }
17888
17889 /**
17890 * Interface definition for a callback to be invoked when the context menu
17891 * for this view is being built.
17892 */
17893 public interface OnCreateContextMenuListener {
17894 /**
17895 * Called when the context menu for this view is being built. It is not
17896 * safe to hold onto the menu after this method returns.
17897 *
17898 * @param menu The context menu that is being built
17899 * @param v The view for which the context menu is being built
17900 * @param menuInfo Extra information about the item for which the
17901 * context menu should be shown. This information will vary
17902 * depending on the class of v.
17903 */
17904 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17905 }
17906
Joe Onorato664644d2011-01-23 17:53:23 -080017907 /**
17908 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017909 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017910 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017911 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017912 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017913 */
17914 public interface OnSystemUiVisibilityChangeListener {
17915 /**
17916 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017917 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017918 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017919 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17920 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17921 * This tells you the <strong>global</strong> state of these UI visibility
17922 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017923 */
17924 public void onSystemUiVisibilityChange(int visibility);
17925 }
17926
Adam Powell4afd62b2011-02-18 15:02:18 -080017927 /**
17928 * Interface definition for a callback to be invoked when this view is attached
17929 * or detached from its window.
17930 */
17931 public interface OnAttachStateChangeListener {
17932 /**
17933 * Called when the view is attached to a window.
17934 * @param v The view that was attached
17935 */
17936 public void onViewAttachedToWindow(View v);
17937 /**
17938 * Called when the view is detached from a window.
17939 * @param v The view that was detached
17940 */
17941 public void onViewDetachedFromWindow(View v);
17942 }
17943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017944 private final class UnsetPressedState implements Runnable {
17945 public void run() {
17946 setPressed(false);
17947 }
17948 }
17949
17950 /**
17951 * Base class for derived classes that want to save and restore their own
17952 * state in {@link android.view.View#onSaveInstanceState()}.
17953 */
17954 public static class BaseSavedState extends AbsSavedState {
17955 /**
17956 * Constructor used when reading from a parcel. Reads the state of the superclass.
17957 *
17958 * @param source
17959 */
17960 public BaseSavedState(Parcel source) {
17961 super(source);
17962 }
17963
17964 /**
17965 * Constructor called by derived classes when creating their SavedState objects
17966 *
17967 * @param superState The state of the superclass of this view
17968 */
17969 public BaseSavedState(Parcelable superState) {
17970 super(superState);
17971 }
17972
17973 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17974 new Parcelable.Creator<BaseSavedState>() {
17975 public BaseSavedState createFromParcel(Parcel in) {
17976 return new BaseSavedState(in);
17977 }
17978
17979 public BaseSavedState[] newArray(int size) {
17980 return new BaseSavedState[size];
17981 }
17982 };
17983 }
17984
17985 /**
17986 * A set of information given to a view when it is attached to its parent
17987 * window.
17988 */
17989 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017990 interface Callbacks {
17991 void playSoundEffect(int effectId);
17992 boolean performHapticFeedback(int effectId, boolean always);
17993 }
17994
17995 /**
17996 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17997 * to a Handler. This class contains the target (View) to invalidate and
17998 * the coordinates of the dirty rectangle.
17999 *
18000 * For performance purposes, this class also implements a pool of up to
18001 * POOL_LIMIT objects that get reused. This reduces memory allocations
18002 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018003 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080018004 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018005 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018006
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080018007 private static final SynchronizedPool<InvalidateInfo> sPool =
18008 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018009
18010 View target;
18011
18012 int left;
18013 int top;
18014 int right;
18015 int bottom;
18016
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080018017 public static InvalidateInfo obtain() {
18018 InvalidateInfo instance = sPool.acquire();
18019 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070018020 }
18021
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080018022 public void recycle() {
18023 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070018024 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018025 }
18026 }
18027
18028 final IWindowSession mSession;
18029
18030 final IWindow mWindow;
18031
18032 final IBinder mWindowToken;
18033
Jeff Brown98365d72012-08-19 20:30:52 -070018034 final Display mDisplay;
18035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018036 final Callbacks mRootCallbacks;
18037
Romain Guy59a12ca2011-06-09 17:48:21 -070018038 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080018039
Dianne Hackborne3f23a32013-03-01 13:25:35 -080018040 IWindowId mIWindowId;
18041 WindowId mWindowId;
18042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018043 /**
18044 * The top view of the hierarchy.
18045 */
18046 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070018047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018048 IBinder mPanelParentWindowToken;
18049 Surface mSurface;
18050
Romain Guyb051e892010-09-28 19:09:36 -070018051 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080018052 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070018053 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080018054
Romain Guy7e4e5612012-03-05 14:37:29 -080018055 boolean mScreenOn;
18056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018057 /**
Romain Guy8506ab42009-06-11 17:35:47 -070018058 * Scale factor used by the compatibility mode
18059 */
18060 float mApplicationScale;
18061
18062 /**
18063 * Indicates whether the application is in compatibility mode
18064 */
18065 boolean mScalingRequired;
18066
18067 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070018068 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080018069 */
18070 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080018071
Dianne Hackborn63042d62011-01-26 18:56:29 -080018072 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018073 * Left position of this view's window
18074 */
18075 int mWindowLeft;
18076
18077 /**
18078 * Top position of this view's window
18079 */
18080 int mWindowTop;
18081
18082 /**
Adam Powell26153a32010-11-08 15:22:27 -080018083 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070018084 */
Adam Powell26153a32010-11-08 15:22:27 -080018085 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070018086
18087 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018088 * For windows that are full-screen but using insets to layout inside
Dianne Hackbornc4aad012013-02-22 15:05:25 -080018089 * of the screen areas, these are the current insets to appear inside
18090 * the overscan area of the display.
18091 */
18092 final Rect mOverscanInsets = new Rect();
18093
18094 /**
18095 * For windows that are full-screen but using insets to layout inside
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018096 * of the screen decorations, these are the current insets for the
18097 * content of the window.
18098 */
18099 final Rect mContentInsets = new Rect();
18100
18101 /**
18102 * For windows that are full-screen but using insets to layout inside
18103 * of the screen decorations, these are the current insets for the
18104 * actual visible parts of the window.
18105 */
18106 final Rect mVisibleInsets = new Rect();
18107
18108 /**
18109 * The internal insets given by this window. This value is
18110 * supplied by the client (through
18111 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
18112 * be given to the window manager when changed to be used in laying
18113 * out windows behind it.
18114 */
18115 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
18116 = new ViewTreeObserver.InternalInsetsInfo();
18117
18118 /**
18119 * All views in the window's hierarchy that serve as scroll containers,
18120 * used to determine if the window can be resized or must be panned
18121 * to adjust for a soft input area.
18122 */
18123 final ArrayList<View> mScrollContainers = new ArrayList<View>();
18124
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070018125 final KeyEvent.DispatcherState mKeyDispatchState
18126 = new KeyEvent.DispatcherState();
18127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018128 /**
18129 * Indicates whether the view's window currently has the focus.
18130 */
18131 boolean mHasWindowFocus;
18132
18133 /**
18134 * The current visibility of the window.
18135 */
18136 int mWindowVisibility;
18137
18138 /**
18139 * Indicates the time at which drawing started to occur.
18140 */
18141 long mDrawingTime;
18142
18143 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070018144 * Indicates whether or not ignoring the DIRTY_MASK flags.
18145 */
18146 boolean mIgnoreDirtyState;
18147
18148 /**
Romain Guy02ccac62011-06-24 13:20:23 -070018149 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
18150 * to avoid clearing that flag prematurely.
18151 */
18152 boolean mSetIgnoreDirtyState = false;
18153
18154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018155 * Indicates whether the view's window is currently in touch mode.
18156 */
18157 boolean mInTouchMode;
18158
18159 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070018160 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018161 * the next time it performs a traversal
18162 */
18163 boolean mRecomputeGlobalAttributes;
18164
18165 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070018166 * Always report new attributes at next traversal.
18167 */
18168 boolean mForceReportNewAttributes;
18169
18170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018171 * Set during a traveral if any views want to keep the screen on.
18172 */
18173 boolean mKeepScreenOn;
18174
18175 /**
Joe Onorato664644d2011-01-23 17:53:23 -080018176 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
18177 */
18178 int mSystemUiVisibility;
18179
18180 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070018181 * Hack to force certain system UI visibility flags to be cleared.
18182 */
18183 int mDisabledSystemUiVisibility;
18184
18185 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070018186 * Last global system UI visibility reported by the window manager.
18187 */
18188 int mGlobalSystemUiVisibility;
18189
18190 /**
Joe Onorato664644d2011-01-23 17:53:23 -080018191 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
18192 * attached.
18193 */
18194 boolean mHasSystemUiListeners;
18195
18196 /**
Dianne Hackbornc4aad012013-02-22 15:05:25 -080018197 * Set if the window has requested to extend into the overscan region
18198 * via WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN.
18199 */
18200 boolean mOverscanRequested;
18201
18202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018203 * Set if the visibility of any views has changed.
18204 */
18205 boolean mViewVisibilityChanged;
18206
18207 /**
18208 * Set to true if a view has been scrolled.
18209 */
18210 boolean mViewScrollChanged;
18211
18212 /**
18213 * Global to the view hierarchy used as a temporary for dealing with
18214 * x/y points in the transparent region computations.
18215 */
18216 final int[] mTransparentLocation = new int[2];
18217
18218 /**
18219 * Global to the view hierarchy used as a temporary for dealing with
18220 * x/y points in the ViewGroup.invalidateChild implementation.
18221 */
18222 final int[] mInvalidateChildLocation = new int[2];
18223
Chet Haasec3aa3612010-06-17 08:50:37 -070018224
18225 /**
18226 * Global to the view hierarchy used as a temporary for dealing with
18227 * x/y location when view is transformed.
18228 */
18229 final float[] mTmpTransformLocation = new float[2];
18230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018231 /**
18232 * The view tree observer used to dispatch global events like
18233 * layout, pre-draw, touch mode change, etc.
18234 */
18235 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
18236
18237 /**
18238 * A Canvas used by the view hierarchy to perform bitmap caching.
18239 */
18240 Canvas mCanvas;
18241
18242 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080018243 * The view root impl.
18244 */
18245 final ViewRootImpl mViewRootImpl;
18246
18247 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070018248 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018249 * handler can be used to pump events in the UI events queue.
18250 */
18251 final Handler mHandler;
18252
18253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018254 * Temporary for use in computing invalidate rectangles while
18255 * calling up the hierarchy.
18256 */
18257 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070018258
18259 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070018260 * Temporary for use in computing hit areas with transformed views
18261 */
18262 final RectF mTmpTransformRect = new RectF();
18263
18264 /**
Chet Haase599913d2012-07-23 16:22:05 -070018265 * Temporary for use in transforming invalidation rect
18266 */
18267 final Matrix mTmpMatrix = new Matrix();
18268
18269 /**
18270 * Temporary for use in transforming invalidation rect
18271 */
18272 final Transformation mTmpTransformation = new Transformation();
18273
18274 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070018275 * Temporary list for use in collecting focusable descendents of a view.
18276 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070018277 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070018278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018279 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070018280 * The id of the window for accessibility purposes.
18281 */
18282 int mAccessibilityWindowId = View.NO_ID;
18283
18284 /**
Svetoslav Ganov80943d82013-01-02 10:25:37 -080018285 * Flags related to accessibility processing.
18286 *
18287 * @see AccessibilityNodeInfo#FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
18288 * @see AccessibilityNodeInfo#FLAG_REPORT_VIEW_IDS
Svetoslav Ganov42138042012-03-20 11:51:39 -070018289 */
Svetoslav Ganov80943d82013-01-02 10:25:37 -080018290 int mAccessibilityFetchFlags;
Svetoslav Ganov42138042012-03-20 11:51:39 -070018291
18292 /**
18293 * The drawable for highlighting accessibility focus.
18294 */
18295 Drawable mAccessibilityFocusDrawable;
18296
18297 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070018298 * Show where the margins, bounds and layout bounds are for each view.
18299 */
Dianne Hackborna53de062012-05-08 18:53:51 -070018300 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070018301
18302 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070018303 * Point used to compute visible regions.
18304 */
18305 final Point mPoint = new Point();
18306
18307 /**
Chet Haasecc699b42012-12-13 09:06:55 -080018308 * Used to track which View originated a requestLayout() call, used when
18309 * requestLayout() is called during layout.
18310 */
18311 View mViewRequestingLayout;
18312
18313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018314 * Creates a new set of attachment information with the specified
18315 * events handler and thread.
18316 *
18317 * @param handler the events handler the view must use
18318 */
Jeff Brown98365d72012-08-19 20:30:52 -070018319 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080018320 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018321 mSession = session;
18322 mWindow = window;
18323 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070018324 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080018325 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018326 mHandler = handler;
18327 mRootCallbacks = effectPlayer;
18328 }
18329 }
18330
18331 /**
18332 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18333 * is supported. This avoids keeping too many unused fields in most
18334 * instances of View.</p>
18335 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018336 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018337
Mike Cleronf116bf82009-09-27 19:14:12 -070018338 /**
18339 * Scrollbars are not visible
18340 */
18341 public static final int OFF = 0;
18342
18343 /**
18344 * Scrollbars are visible
18345 */
18346 public static final int ON = 1;
18347
18348 /**
18349 * Scrollbars are fading away
18350 */
18351 public static final int FADING = 2;
18352
18353 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018355 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018356 public int scrollBarDefaultDelayBeforeFade;
18357 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018358
18359 public int scrollBarSize;
18360 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018361 public float[] interpolatorValues;
18362 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018363
18364 public final Paint paint;
18365 public final Matrix matrix;
18366 public Shader shader;
18367
Mike Cleronf116bf82009-09-27 19:14:12 -070018368 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18369
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018370 private static final float[] OPAQUE = { 255 };
18371 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018372
Mike Cleronf116bf82009-09-27 19:14:12 -070018373 /**
18374 * When fading should start. This time moves into the future every time
18375 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18376 */
18377 public long fadeStartTime;
18378
18379
18380 /**
18381 * The current state of the scrollbars: ON, OFF, or FADING
18382 */
18383 public int state = OFF;
18384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018385 private int mLastColor;
18386
Mike Cleronf116bf82009-09-27 19:14:12 -070018387 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018388 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18389 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018390 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18391 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018392
18393 paint = new Paint();
18394 matrix = new Matrix();
18395 // use use a height of 1, and then wack the matrix each time we
18396 // actually use it.
18397 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018398 paint.setShader(shader);
18399 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018400
Mike Cleronf116bf82009-09-27 19:14:12 -070018401 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018402 }
Romain Guy8506ab42009-06-11 17:35:47 -070018403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018404 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018405 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018406 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018407
Romain Guyd679b572012-08-29 21:49:00 -070018408 if (color != 0) {
18409 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18410 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18411 paint.setShader(shader);
18412 // Restore the default transfer mode (src_over)
18413 paint.setXfermode(null);
18414 } else {
18415 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18416 paint.setShader(shader);
18417 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018419 }
18420 }
Joe Malin32736f02011-01-19 16:14:20 -080018421
Mike Cleronf116bf82009-09-27 19:14:12 -070018422 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018423 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018424 if (now >= fadeStartTime) {
18425
18426 // the animation fades the scrollbars out by changing
18427 // the opacity (alpha) from fully opaque to fully
18428 // transparent
18429 int nextFrame = (int) now;
18430 int framesCount = 0;
18431
18432 Interpolator interpolator = scrollBarInterpolator;
18433
18434 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018435 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018436
18437 // End transparent
18438 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018439 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018440
18441 state = FADING;
18442
18443 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018444 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018445 }
18446 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018447 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018448
Svetoslav Ganova0156172011-06-26 17:55:44 -070018449 /**
18450 * Resuable callback for sending
18451 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18452 */
18453 private class SendViewScrolledAccessibilityEvent implements Runnable {
18454 public volatile boolean mIsPending;
18455
18456 public void run() {
18457 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18458 mIsPending = false;
18459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018460 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018461
18462 /**
18463 * <p>
18464 * This class represents a delegate that can be registered in a {@link View}
18465 * to enhance accessibility support via composition rather via inheritance.
18466 * It is specifically targeted to widget developers that extend basic View
18467 * classes i.e. classes in package android.view, that would like their
18468 * applications to be backwards compatible.
18469 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018470 * <div class="special reference">
18471 * <h3>Developer Guides</h3>
18472 * <p>For more information about making applications accessible, read the
18473 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18474 * developer guide.</p>
18475 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018476 * <p>
18477 * A scenario in which a developer would like to use an accessibility delegate
18478 * is overriding a method introduced in a later API version then the minimal API
18479 * version supported by the application. For example, the method
18480 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18481 * in API version 4 when the accessibility APIs were first introduced. If a
18482 * developer would like his application to run on API version 4 devices (assuming
18483 * all other APIs used by the application are version 4 or lower) and take advantage
18484 * of this method, instead of overriding the method which would break the application's
18485 * backwards compatibility, he can override the corresponding method in this
18486 * delegate and register the delegate in the target View if the API version of
18487 * the system is high enough i.e. the API version is same or higher to the API
18488 * version that introduced
18489 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18490 * </p>
18491 * <p>
18492 * Here is an example implementation:
18493 * </p>
18494 * <code><pre><p>
18495 * if (Build.VERSION.SDK_INT >= 14) {
18496 * // If the API version is equal of higher than the version in
18497 * // which onInitializeAccessibilityNodeInfo was introduced we
18498 * // register a delegate with a customized implementation.
18499 * View view = findViewById(R.id.view_id);
18500 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18501 * public void onInitializeAccessibilityNodeInfo(View host,
18502 * AccessibilityNodeInfo info) {
18503 * // Let the default implementation populate the info.
18504 * super.onInitializeAccessibilityNodeInfo(host, info);
18505 * // Set some other information.
18506 * info.setEnabled(host.isEnabled());
18507 * }
18508 * });
18509 * }
18510 * </code></pre></p>
18511 * <p>
18512 * This delegate contains methods that correspond to the accessibility methods
18513 * in View. If a delegate has been specified the implementation in View hands
18514 * off handling to the corresponding method in this delegate. The default
18515 * implementation the delegate methods behaves exactly as the corresponding
18516 * method in View for the case of no accessibility delegate been set. Hence,
18517 * to customize the behavior of a View method, clients can override only the
18518 * corresponding delegate method without altering the behavior of the rest
18519 * accessibility related methods of the host view.
18520 * </p>
18521 */
18522 public static class AccessibilityDelegate {
18523
18524 /**
18525 * Sends an accessibility event of the given type. If accessibility is not
18526 * enabled this method has no effect.
18527 * <p>
18528 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18529 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18530 * been set.
18531 * </p>
18532 *
18533 * @param host The View hosting the delegate.
18534 * @param eventType The type of the event to send.
18535 *
18536 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18537 */
18538 public void sendAccessibilityEvent(View host, int eventType) {
18539 host.sendAccessibilityEventInternal(eventType);
18540 }
18541
18542 /**
alanv8eeefef2012-05-07 16:57:53 -070018543 * Performs the specified accessibility action on the view. For
18544 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18545 * <p>
18546 * The default implementation behaves as
18547 * {@link View#performAccessibilityAction(int, Bundle)
18548 * View#performAccessibilityAction(int, Bundle)} for the case of
18549 * no accessibility delegate been set.
18550 * </p>
18551 *
18552 * @param action The action to perform.
18553 * @return Whether the action was performed.
18554 *
18555 * @see View#performAccessibilityAction(int, Bundle)
18556 * View#performAccessibilityAction(int, Bundle)
18557 */
18558 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18559 return host.performAccessibilityActionInternal(action, args);
18560 }
18561
18562 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018563 * Sends an accessibility event. This method behaves exactly as
18564 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18565 * empty {@link AccessibilityEvent} and does not perform a check whether
18566 * accessibility is enabled.
18567 * <p>
18568 * The default implementation behaves as
18569 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18570 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18571 * the case of no accessibility delegate been set.
18572 * </p>
18573 *
18574 * @param host The View hosting the delegate.
18575 * @param event The event to send.
18576 *
18577 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18578 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18579 */
18580 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18581 host.sendAccessibilityEventUncheckedInternal(event);
18582 }
18583
18584 /**
18585 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18586 * to its children for adding their text content to the event.
18587 * <p>
18588 * The default implementation behaves as
18589 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18590 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18591 * the case of no accessibility delegate been set.
18592 * </p>
18593 *
18594 * @param host The View hosting the delegate.
18595 * @param event The event.
18596 * @return True if the event population was completed.
18597 *
18598 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18599 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18600 */
18601 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18602 return host.dispatchPopulateAccessibilityEventInternal(event);
18603 }
18604
18605 /**
18606 * Gives a chance to the host View to populate the accessibility event with its
18607 * text content.
18608 * <p>
18609 * The default implementation behaves as
18610 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18611 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18612 * the case of no accessibility delegate been set.
18613 * </p>
18614 *
18615 * @param host The View hosting the delegate.
18616 * @param event The accessibility event which to populate.
18617 *
18618 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18619 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18620 */
18621 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18622 host.onPopulateAccessibilityEventInternal(event);
18623 }
18624
18625 /**
18626 * Initializes an {@link AccessibilityEvent} with information about the
18627 * the host View which is the event source.
18628 * <p>
18629 * The default implementation behaves as
18630 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18631 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18632 * the case of no accessibility delegate been set.
18633 * </p>
18634 *
18635 * @param host The View hosting the delegate.
18636 * @param event The event to initialize.
18637 *
18638 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18639 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18640 */
18641 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18642 host.onInitializeAccessibilityEventInternal(event);
18643 }
18644
18645 /**
18646 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18647 * <p>
18648 * The default implementation behaves as
18649 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18650 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18651 * the case of no accessibility delegate been set.
18652 * </p>
18653 *
18654 * @param host The View hosting the delegate.
18655 * @param info The instance to initialize.
18656 *
18657 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18658 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18659 */
18660 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18661 host.onInitializeAccessibilityNodeInfoInternal(info);
18662 }
18663
18664 /**
18665 * Called when a child of the host View has requested sending an
18666 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18667 * to augment the event.
18668 * <p>
18669 * The default implementation behaves as
18670 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18671 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18672 * the case of no accessibility delegate been set.
18673 * </p>
18674 *
18675 * @param host The View hosting the delegate.
18676 * @param child The child which requests sending the event.
18677 * @param event The event to be sent.
18678 * @return True if the event should be sent
18679 *
18680 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18681 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18682 */
18683 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18684 AccessibilityEvent event) {
18685 return host.onRequestSendAccessibilityEventInternal(child, event);
18686 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018687
18688 /**
18689 * Gets the provider for managing a virtual view hierarchy rooted at this View
18690 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18691 * that explore the window content.
18692 * <p>
18693 * The default implementation behaves as
18694 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18695 * the case of no accessibility delegate been set.
18696 * </p>
18697 *
18698 * @return The provider.
18699 *
18700 * @see AccessibilityNodeProvider
18701 */
18702 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18703 return null;
18704 }
Svetoslav Ganov50776862013-05-17 18:06:31 -070018705
18706 /**
18707 * Returns an {@link AccessibilityNodeInfo} representing the host view from the
18708 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
18709 * This method is responsible for obtaining an accessibility node info from a
18710 * pool of reusable instances and calling
18711 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on the host
18712 * view to initialize the former.
18713 * <p>
18714 * <strong>Note:</strong> The client is responsible for recycling the obtained
18715 * instance by calling {@link AccessibilityNodeInfo#recycle()} to minimize object
18716 * creation.
18717 * </p>
18718 * <p>
18719 * The default implementation behaves as
18720 * {@link View#createAccessibilityNodeInfo() View#createAccessibilityNodeInfo()} for
18721 * the case of no accessibility delegate been set.
18722 * </p>
18723 * @return A populated {@link AccessibilityNodeInfo}.
18724 *
18725 * @see AccessibilityNodeInfo
18726 *
18727 * @hide
18728 */
18729 public AccessibilityNodeInfo createAccessibilityNodeInfo(View host) {
18730 return host.createAccessibilityNodeInfoInternal();
18731 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018732 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018733
18734 private class MatchIdPredicate implements Predicate<View> {
18735 public int mId;
18736
18737 @Override
18738 public boolean apply(View view) {
18739 return (view.mID == mId);
18740 }
18741 }
18742
18743 private class MatchLabelForPredicate implements Predicate<View> {
18744 private int mLabeledId;
18745
18746 @Override
18747 public boolean apply(View view) {
18748 return (view.mLabelForId == mLabeledId);
18749 }
18750 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018751
18752 /**
18753 * Dump all private flags in readable format, useful for documentation and
18754 * sanity checking.
18755 */
18756 private static void dumpFlags() {
18757 final HashMap<String, String> found = Maps.newHashMap();
18758 try {
18759 for (Field field : View.class.getDeclaredFields()) {
18760 final int modifiers = field.getModifiers();
18761 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18762 if (field.getType().equals(int.class)) {
18763 final int value = field.getInt(null);
18764 dumpFlag(found, field.getName(), value);
18765 } else if (field.getType().equals(int[].class)) {
18766 final int[] values = (int[]) field.get(null);
18767 for (int i = 0; i < values.length; i++) {
18768 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18769 }
18770 }
18771 }
18772 }
18773 } catch (IllegalAccessException e) {
18774 throw new RuntimeException(e);
18775 }
18776
18777 final ArrayList<String> keys = Lists.newArrayList();
18778 keys.addAll(found.keySet());
18779 Collections.sort(keys);
18780 for (String key : keys) {
18781 Log.d(VIEW_LOG_TAG, found.get(key));
18782 }
18783 }
18784
18785 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18786 // Sort flags by prefix, then by bits, always keeping unique keys
18787 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18788 final int prefix = name.indexOf('_');
18789 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18790 final String output = bits + " " + name;
18791 found.put(key, output);
18792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018793}