blob: 97287c3b2762078b7db032c0f2b3a5eee3aa84ce [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
1564 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001565 * @hide
1566 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001567 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001568
1569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 * The view's tag.
1571 * {@hide}
1572 *
1573 * @see #setTag(Object)
1574 * @see #getTag()
1575 */
1576 protected Object mTag;
1577
1578 // for mPrivateFlags:
1579 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001580 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001582 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001584 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001586 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001588 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001590 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 /**
1592 * When this flag is set, this view is running an animation on behalf of its
1593 * children and should therefore not cancel invalidate requests, even if they
1594 * lie outside of this view's bounds.
1595 *
1596 * {@hide}
1597 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001598 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001600 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001602 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001604 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001606 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001608 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001610 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001611 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001612 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
Dianne Hackborn4702a852012-08-17 15:18:29 -07001614 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615
1616 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001617 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 /**
1619 * Flag used to indicate that this view should be drawn once more (and only once
1620 * more) after its animation has completed.
1621 * {@hide}
1622 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001623 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
Dianne Hackborn4702a852012-08-17 15:18:29 -07001625 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626
1627 /**
1628 * Indicates that the View returned true when onSetAlpha() was called and that
1629 * the alpha must be restored.
1630 * {@hide}
1631 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001632 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633
1634 /**
1635 * Set by {@link #setScrollContainer(boolean)}.
1636 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001637 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638
1639 /**
1640 * Set by {@link #setScrollContainer(boolean)}.
1641 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001642 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643
1644 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001645 * View flag indicating whether this view was invalidated (fully or partially.)
1646 *
1647 * @hide
1648 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001649 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001650
1651 /**
1652 * View flag indicating whether this view was invalidated by an opaque
1653 * invalidate request.
1654 *
1655 * @hide
1656 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001657 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001658
1659 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001660 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001661 *
1662 * @hide
1663 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001664 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001665
1666 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001667 * Indicates whether the background is opaque.
1668 *
1669 * @hide
1670 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001671 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001672
1673 /**
1674 * Indicates whether the scrollbars are opaque.
1675 *
1676 * @hide
1677 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001678 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001679
1680 /**
1681 * Indicates whether the view is opaque.
1682 *
1683 * @hide
1684 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001685 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powelle14579b2009-12-16 18:39:52 -08001687 /**
1688 * Indicates a prepressed state;
1689 * the short time between ACTION_DOWN and recognizing
1690 * a 'real' press. Prepressed is used to recognize quick taps
1691 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powelle14579b2009-12-16 18:39:52 -08001693 * @hide
1694 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001695 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001696
Adam Powellc9fbaab2010-02-16 17:16:19 -08001697 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001698 * Indicates whether the view is temporarily detached.
1699 *
1700 * @hide
1701 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001702 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001703
Adam Powell8568c3a2010-04-19 14:26:11 -07001704 /**
1705 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001706 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001707 * @hide
1708 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001709 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001710
1711 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001712 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1713 * @hide
1714 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001715 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001716
1717 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001718 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1719 * for transform operations
1720 *
1721 * @hide
1722 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001723 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001724
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001725 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001726 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001727
Chet Haasefd2b0022010-08-06 13:08:56 -07001728 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001729 * Indicates that this view was specifically invalidated, not just dirtied because some
1730 * child view was invalidated. The flag is used to determine when we need to recreate
1731 * a view's display list (as opposed to just returning a reference to its existing
1732 * display list).
1733 *
1734 * @hide
1735 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001736 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001737
Jeff Sharkey36901b62012-09-19 19:06:22 -07001738 /**
1739 * Masks for mPrivateFlags2, as generated by dumpFlags():
1740 *
1741 * -------|-------|-------|-------|
1742 * PFLAG2_TEXT_ALIGNMENT_FLAGS[0]
1743 * PFLAG2_TEXT_DIRECTION_FLAGS[0]
1744 * 1 PFLAG2_DRAG_CAN_ACCEPT
1745 * 1 PFLAG2_DRAG_HOVERED
1746 * 1 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
1747 * 11 PFLAG2_TEXT_DIRECTION_MASK_SHIFT
1748 * 1 1 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT
1749 * 11 PFLAG2_LAYOUT_DIRECTION_MASK
1750 * 11 1 PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
1751 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
1752 * 1 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT
1753 * 1 1 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT
1754 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED
1755 * 11 PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
1756 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[1]
1757 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[2]
1758 * 11 PFLAG2_TEXT_DIRECTION_FLAGS[3]
1759 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[4]
1760 * 1 1 PFLAG2_TEXT_DIRECTION_FLAGS[5]
1761 * 111 PFLAG2_TEXT_DIRECTION_MASK
1762 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED
1763 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT
1764 * 111 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK
1765 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[1]
1766 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[2]
1767 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[3]
1768 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[4]
1769 * 1 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[5]
1770 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[6]
1771 * 111 PFLAG2_TEXT_ALIGNMENT_MASK
1772 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED
1773 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT
1774 * 111 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK
1775 * 11 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK
1776 * 1 PFLAG2_HAS_TRANSIENT_STATE
1777 * 1 PFLAG2_ACCESSIBILITY_FOCUSED
1778 * 1 PFLAG2_ACCESSIBILITY_STATE_CHANGED
1779 * 1 PFLAG2_VIEW_QUICK_REJECTED
1780 * 1 PFLAG2_PADDING_RESOLVED
1781 * -------|-------|-------|-------|
1782 */
Christopher Tate3d4bf172011-03-28 16:16:46 -07001783
1784 /**
1785 * Indicates that this view has reported that it can accept the current drag's content.
1786 * Cleared when the drag operation concludes.
1787 * @hide
1788 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001789 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001790
1791 /**
1792 * Indicates that this view is currently directly under the drag location in a
1793 * drag-and-drop operation involving content that it can accept. Cleared when
1794 * the drag exits the view, or when the drag operation concludes.
1795 * @hide
1796 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001797 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001798
Cibu Johny86666632010-02-22 13:01:02 -08001799 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800 * Horizontal layout direction of this view is from Left to Right.
1801 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001802 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001803 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001804
1805 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001806 * Horizontal layout direction of this view is from Right to Left.
1807 * Use with {@link #setLayoutDirection}.
1808 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001809 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001810
1811 /**
1812 * Horizontal layout direction of this view is inherited from its parent.
1813 * Use with {@link #setLayoutDirection}.
1814 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001815 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001816
1817 /**
1818 * Horizontal layout direction of this view is from deduced from the default language
1819 * script for the locale. Use with {@link #setLayoutDirection}.
1820 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001821 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001822
1823 /**
1824 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001825 * @hide
1826 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001827 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001828
1829 /**
1830 * Mask for use with private flags indicating bits used for horizontal layout direction.
1831 * @hide
1832 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001833 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001834
1835 /**
1836 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1837 * right-to-left direction.
1838 * @hide
1839 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001840 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001841
1842 /**
1843 * Indicates whether the view horizontal layout direction has been resolved.
1844 * @hide
1845 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001846 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001847
1848 /**
1849 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1850 * @hide
1851 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001852 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1853 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001854
1855 /*
1856 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1857 * flag value.
1858 * @hide
1859 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001860 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1861 LAYOUT_DIRECTION_LTR,
1862 LAYOUT_DIRECTION_RTL,
1863 LAYOUT_DIRECTION_INHERIT,
1864 LAYOUT_DIRECTION_LOCALE
1865 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001866
1867 /**
1868 * Default horizontal layout direction.
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001869 */
1870 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001871
Adam Powell539ee872012-02-03 19:00:49 -08001872 /**
1873 * Indicates that the view is tracking some sort of transient state
1874 * that the app should not need to be aware of, but that the framework
1875 * should take special care to preserve.
1876 *
1877 * @hide
1878 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001879 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001880
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001881 /**
1882 * Text direction is inherited thru {@link ViewGroup}
1883 */
1884 public static final int TEXT_DIRECTION_INHERIT = 0;
1885
1886 /**
1887 * Text direction is using "first strong algorithm". The first strong directional character
1888 * determines the paragraph direction. If there is no strong directional character, the
1889 * paragraph direction is the view's resolved layout direction.
1890 */
1891 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1892
1893 /**
1894 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1895 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1896 * If there are neither, the paragraph direction is the view's resolved layout direction.
1897 */
1898 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1899
1900 /**
1901 * Text direction is forced to LTR.
1902 */
1903 public static final int TEXT_DIRECTION_LTR = 3;
1904
1905 /**
1906 * Text direction is forced to RTL.
1907 */
1908 public static final int TEXT_DIRECTION_RTL = 4;
1909
1910 /**
1911 * Text direction is coming from the system Locale.
1912 */
1913 public static final int TEXT_DIRECTION_LOCALE = 5;
1914
1915 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001916 * Default text direction is inherited
1917 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07001918 private static final int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001924 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001930 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1931 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001932
1933 /**
1934 * Array of text direction flags for mapping attribute "textDirection" to correct
1935 * flag value.
1936 * @hide
1937 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001938 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1939 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1944 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001945 };
1946
1947 /**
1948 * Indicates whether the view text direction has been resolved.
1949 * @hide
1950 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001951 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1952 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001953
1954 /**
1955 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1956 * @hide
1957 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001958 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001959
1960 /**
1961 * Mask for use with private flags indicating bits used for resolved text direction.
1962 * @hide
1963 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001964 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1965 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001966
1967 /**
1968 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1969 * @hide
1970 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001971 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1972 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001973
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001974 /*
1975 * Default text alignment. The text alignment of this View is inherited from its parent.
1976 * Use with {@link #setTextAlignment(int)}
1977 */
1978 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1979
1980 /**
1981 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1982 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1983 *
1984 * Use with {@link #setTextAlignment(int)}
1985 */
1986 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1987
1988 /**
1989 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1990 *
1991 * Use with {@link #setTextAlignment(int)}
1992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
1999 */
2000 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2001
2002 /**
2003 * Center the paragraph, e.g. ALIGN_CENTER.
2004 *
2005 * Use with {@link #setTextAlignment(int)}
2006 */
2007 public static final int TEXT_ALIGNMENT_CENTER = 4;
2008
2009 /**
2010 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2011 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2012 *
2013 * Use with {@link #setTextAlignment(int)}
2014 */
2015 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2016
2017 /**
2018 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2019 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2020 *
2021 * Use with {@link #setTextAlignment(int)}
2022 */
2023 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2024
2025 /**
2026 * Default text alignment is inherited
2027 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07002028 private static final int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002029
2030 /**
2031 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2032 * @hide
2033 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002034 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002035
2036 /**
2037 * Mask for use with private flags indicating bits used for text alignment.
2038 * @hide
2039 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002040 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002041
2042 /**
2043 * Array of text direction flags for mapping attribute "textAlignment" to correct
2044 * flag value.
2045 * @hide
2046 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002047 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2048 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2052 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2053 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002055 };
2056
2057 /**
2058 * Indicates whether the view text alignment has been resolved.
2059 * @hide
2060 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002061 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002062
2063 /**
2064 * Bit shift to get the resolved text alignment.
2065 * @hide
2066 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002067 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002068
2069 /**
2070 * Mask for use with private flags indicating bits used for text alignment.
2071 * @hide
2072 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002073 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2074 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002075
2076 /**
2077 * Indicates whether if the view text alignment has been resolved to gravity
2078 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002079 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2080 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002081
Svetoslav Ganov42138042012-03-20 11:51:39 -07002082 // Accessiblity constants for mPrivateFlags2
2083
2084 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002085 * Shift for the bits in {@link #mPrivateFlags2} related to the
2086 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002087 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002088 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002089
2090 /**
2091 * Automatically determine whether a view is important for accessibility.
2092 */
2093 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2094
2095 /**
2096 * The view is important for accessibility.
2097 */
2098 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2099
2100 /**
2101 * The view is not important for accessibility.
2102 */
2103 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2104
2105 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002106 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002107 */
2108 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2109
2110 /**
2111 * Mask for obtainig the bits which specify how to determine
2112 * whether a view is important for accessibility.
2113 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002114 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002115 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002116 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002117
2118 /**
2119 * Flag indicating whether a view has accessibility focus.
2120 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002121 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002122
2123 /**
2124 * Flag indicating whether a view state for accessibility has changed.
2125 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002126 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2127 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002128
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002129 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002130 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2131 * is used to check whether later changes to the view's transform should invalidate the
2132 * view to force the quickReject test to run again.
2133 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002134 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002135
Adam Powell0090f202012-08-07 17:15:30 -07002136 /**
2137 * Flag indicating that start/end padding has been resolved into left/right padding
2138 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2139 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2140 * during measurement. In some special cases this is required such as when an adapter-based
2141 * view measures prospective children without attaching them to a window.
2142 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002143 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002144
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002145 /**
2146 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2147 */
2148 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2149
2150 /**
2151 * Group of bits indicating that RTL properties resolution is done.
2152 */
2153 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002154 PFLAG2_TEXT_DIRECTION_RESOLVED |
2155 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2156 PFLAG2_PADDING_RESOLVED |
2157 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002158
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002159 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002160
Christopher Tate3d4bf172011-03-28 16:16:46 -07002161 /* End of masks for mPrivateFlags2 */
2162
Chet Haase21433372012-06-05 07:54:09 -07002163 /* Masks for mPrivateFlags3 */
2164
2165 /**
2166 * Flag indicating that view has a transform animation set on it. This is used to track whether
2167 * an animation is cleared between successive frames, in order to tell the associated
2168 * DisplayList to clear its animation matrix.
2169 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002170 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002171
2172 /**
2173 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2174 * animation is cleared between successive frames, in order to tell the associated
2175 * DisplayList to restore its alpha value.
2176 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002177 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002178
2179
2180 /* End of masks for mPrivateFlags3 */
2181
Dianne Hackborn4702a852012-08-17 15:18:29 -07002182 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002183
Chet Haasedaf98e92011-01-10 14:10:36 -08002184 /**
Adam Powell637d3372010-08-25 14:37:03 -07002185 * Always allow a user to over-scroll this view, provided it is a
2186 * view that can scroll.
2187 *
2188 * @see #getOverScrollMode()
2189 * @see #setOverScrollMode(int)
2190 */
2191 public static final int OVER_SCROLL_ALWAYS = 0;
2192
2193 /**
2194 * Allow a user to over-scroll this view only if the content is large
2195 * enough to meaningfully scroll, provided it is a view that can scroll.
2196 *
2197 * @see #getOverScrollMode()
2198 * @see #setOverScrollMode(int)
2199 */
2200 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2201
2202 /**
2203 * Never allow a user to over-scroll this view.
2204 *
2205 * @see #getOverScrollMode()
2206 * @see #setOverScrollMode(int)
2207 */
2208 public static final int OVER_SCROLL_NEVER = 2;
2209
2210 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002211 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2212 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002213 *
Joe Malin32736f02011-01-19 16:14:20 -08002214 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002215 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002216 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002217
2218 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002219 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2220 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002221 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002222 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002223 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002224 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002225 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002226 *
Joe Malin32736f02011-01-19 16:14:20 -08002227 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002228 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002229 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2230
2231 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002232 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2233 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002234 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002235 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002236 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2237 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2238 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002239 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002240 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2241 * window flags) for displaying content using every last pixel on the display.
2242 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002243 * <p>There is a limitation: because navigation controls are so important, the least user
2244 * interaction will cause them to reappear immediately. When this happens, both
2245 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2246 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002247 *
2248 * @see #setSystemUiVisibility(int)
2249 */
2250 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2251
2252 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002253 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2254 * into the normal fullscreen mode so that its content can take over the screen
2255 * while still allowing the user to interact with the application.
2256 *
2257 * <p>This has the same visual effect as
2258 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2259 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2260 * meaning that non-critical screen decorations (such as the status bar) will be
2261 * hidden while the user is in the View's window, focusing the experience on
2262 * that content. Unlike the window flag, if you are using ActionBar in
2263 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2264 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2265 * hide the action bar.
2266 *
2267 * <p>This approach to going fullscreen is best used over the window flag when
2268 * it is a transient state -- that is, the application does this at certain
2269 * points in its user interaction where it wants to allow the user to focus
2270 * on content, but not as a continuous state. For situations where the application
2271 * would like to simply stay full screen the entire time (such as a game that
2272 * wants to take over the screen), the
2273 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2274 * is usually a better approach. The state set here will be removed by the system
2275 * in various situations (such as the user moving to another application) like
2276 * the other system UI states.
2277 *
2278 * <p>When using this flag, the application should provide some easy facility
2279 * for the user to go out of it. A common example would be in an e-book
2280 * reader, where tapping on the screen brings back whatever screen and UI
2281 * decorations that had been hidden while the user was immersed in reading
2282 * the book.
2283 *
2284 * @see #setSystemUiVisibility(int)
2285 */
2286 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2287
2288 /**
2289 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2290 * flags, we would like a stable view of the content insets given to
2291 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2292 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002293 * as a continuous state. In the stock Android UI this is the space for
2294 * the system bar, nav bar, and status bar, but not more transient elements
2295 * such as an input method.
2296 *
2297 * The stable layout your UI sees is based on the system UI modes you can
2298 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2299 * then you will get a stable layout for changes of the
2300 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2301 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2302 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2303 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2304 * with a stable layout. (Note that you should avoid using
2305 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2306 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002307 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002308 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2309 * then a hidden status bar will be considered a "stable" state for purposes
2310 * here. This allows your UI to continually hide the status bar, while still
2311 * using the system UI flags to hide the action bar while still retaining
2312 * a stable layout. Note that changing the window fullscreen flag will never
2313 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002314 *
2315 * <p>If you are using ActionBar in
2316 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2317 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2318 * insets it adds to those given to the application.
2319 */
2320 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2321
2322 /**
2323 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2324 * to be layed out as if it has requested
2325 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2326 * allows it to avoid artifacts when switching in and out of that mode, at
2327 * the expense that some of its user interface may be covered by screen
2328 * decorations when they are shown. You can perform layout of your inner
2329 * UI elements to account for the navagation system UI through the
2330 * {@link #fitSystemWindows(Rect)} method.
2331 */
2332 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2333
2334 /**
2335 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2336 * to be layed out as if it has requested
2337 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2338 * allows it to avoid artifacts when switching in and out of that mode, at
2339 * the expense that some of its user interface may be covered by screen
2340 * decorations when they are shown. You can perform layout of your inner
2341 * UI elements to account for non-fullscreen system UI through the
2342 * {@link #fitSystemWindows(Rect)} method.
2343 */
2344 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2345
2346 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002347 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2348 */
2349 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2350
2351 /**
2352 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2353 */
2354 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002355
2356 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002357 * @hide
2358 *
2359 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2360 * out of the public fields to keep the undefined bits out of the developer's way.
2361 *
2362 * Flag to make the status bar not expandable. Unless you also
2363 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2364 */
2365 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2366
2367 /**
2368 * @hide
2369 *
2370 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2371 * out of the public fields to keep the undefined bits out of the developer's way.
2372 *
2373 * Flag to hide notification icons and scrolling ticker text.
2374 */
2375 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2376
2377 /**
2378 * @hide
2379 *
2380 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2381 * out of the public fields to keep the undefined bits out of the developer's way.
2382 *
2383 * Flag to disable incoming notification alerts. This will not block
2384 * icons, but it will block sound, vibrating and other visual or aural notifications.
2385 */
2386 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2387
2388 /**
2389 * @hide
2390 *
2391 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2392 * out of the public fields to keep the undefined bits out of the developer's way.
2393 *
2394 * Flag to hide only the scrolling ticker. Note that
2395 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2396 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2397 */
2398 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2399
2400 /**
2401 * @hide
2402 *
2403 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2404 * out of the public fields to keep the undefined bits out of the developer's way.
2405 *
2406 * Flag to hide the center system info area.
2407 */
2408 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2409
2410 /**
2411 * @hide
2412 *
2413 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2414 * out of the public fields to keep the undefined bits out of the developer's way.
2415 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002416 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002417 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2418 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002419 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002420
2421 /**
2422 * @hide
2423 *
2424 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2425 * out of the public fields to keep the undefined bits out of the developer's way.
2426 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002427 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002428 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2429 */
2430 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2431
2432 /**
2433 * @hide
2434 *
2435 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2436 * out of the public fields to keep the undefined bits out of the developer's way.
2437 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002438 * Flag to hide only the clock. You might use this if your activity has
2439 * its own clock making the status bar's clock redundant.
2440 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002441 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2442
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002443 /**
2444 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002445 *
2446 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2447 * out of the public fields to keep the undefined bits out of the developer's way.
2448 *
2449 * Flag to hide only the recent apps button. Don't use this
2450 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2451 */
2452 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2453
2454 /**
2455 * @hide
Daniel Sandlerd5483c32012-10-19 16:44:15 -04002456 *
2457 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2458 * out of the public fields to keep the undefined bits out of the developer's way.
2459 *
2460 * Flag to disable the global search gesture. Don't use this
2461 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2462 */
2463 public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
2464
2465 /**
2466 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002467 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002468 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002469
2470 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002471 * These are the system UI flags that can be cleared by events outside
2472 * of an application. Currently this is just the ability to tap on the
2473 * screen while hiding the navigation bar to have it return.
2474 * @hide
2475 */
2476 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002477 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2478 | SYSTEM_UI_FLAG_FULLSCREEN;
2479
2480 /**
2481 * Flags that can impact the layout in relation to system UI.
2482 */
2483 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2484 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2485 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002486
2487 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002488 * Find views that render the specified text.
2489 *
2490 * @see #findViewsWithText(ArrayList, CharSequence, int)
2491 */
2492 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2493
2494 /**
2495 * Find find views that contain the specified content description.
2496 *
2497 * @see #findViewsWithText(ArrayList, CharSequence, int)
2498 */
2499 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2500
2501 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002502 * Find views that contain {@link AccessibilityNodeProvider}. Such
2503 * a View is a root of virtual view hierarchy and may contain the searched
2504 * text. If this flag is set Views with providers are automatically
2505 * added and it is a responsibility of the client to call the APIs of
2506 * the provider to determine whether the virtual tree rooted at this View
2507 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2508 * represeting the virtual views with this text.
2509 *
2510 * @see #findViewsWithText(ArrayList, CharSequence, int)
2511 *
2512 * @hide
2513 */
2514 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2515
2516 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002517 * The undefined cursor position.
2518 */
2519 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2520
2521 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002522 * Indicates that the screen has changed state and is now off.
2523 *
2524 * @see #onScreenStateChanged(int)
2525 */
2526 public static final int SCREEN_STATE_OFF = 0x0;
2527
2528 /**
2529 * Indicates that the screen has changed state and is now on.
2530 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002531 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002532 */
2533 public static final int SCREEN_STATE_ON = 0x1;
2534
2535 /**
Adam Powell637d3372010-08-25 14:37:03 -07002536 * Controls the over-scroll mode for this view.
2537 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2538 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2539 * and {@link #OVER_SCROLL_NEVER}.
2540 */
2541 private int mOverScrollMode;
2542
2543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 * The parent this view is attached to.
2545 * {@hide}
2546 *
2547 * @see #getParent()
2548 */
2549 protected ViewParent mParent;
2550
2551 /**
2552 * {@hide}
2553 */
2554 AttachInfo mAttachInfo;
2555
2556 /**
2557 * {@hide}
2558 */
Romain Guy809a7f62009-05-14 15:44:42 -07002559 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002560 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002561 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002562 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002563 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002564 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002565 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002566 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2567 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2568 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2569 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002570 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002572 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002573 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574
2575 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002576 * This view's request for the visibility of the status bar.
2577 * @hide
2578 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002579 @ViewDebug.ExportedProperty(flagMapping = {
2580 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2581 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2582 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2583 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2584 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2585 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2586 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2587 equals = SYSTEM_UI_FLAG_VISIBLE,
2588 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2589 })
Joe Onorato664644d2011-01-23 17:53:23 -08002590 int mSystemUiVisibility;
2591
2592 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002593 * Reference count for transient state.
2594 * @see #setHasTransientState(boolean)
2595 */
2596 int mTransientStateCount = 0;
2597
2598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 * Count of how many windows this view has been attached to.
2600 */
2601 int mWindowAttachCount;
2602
2603 /**
2604 * The layout parameters associated with this view and used by the parent
2605 * {@link android.view.ViewGroup} to determine how this view should be
2606 * laid out.
2607 * {@hide}
2608 */
2609 protected ViewGroup.LayoutParams mLayoutParams;
2610
2611 /**
2612 * The view flags hold various views states.
2613 * {@hide}
2614 */
2615 @ViewDebug.ExportedProperty
2616 int mViewFlags;
2617
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002618 static class TransformationInfo {
2619 /**
2620 * The transform matrix for the View. This transform is calculated internally
2621 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2622 * is used by default. Do *not* use this variable directly; instead call
2623 * getMatrix(), which will automatically recalculate the matrix if necessary
2624 * to get the correct matrix based on the latest rotation and scale properties.
2625 */
2626 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002627
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002628 /**
2629 * The transform matrix for the View. This transform is calculated internally
2630 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2631 * is used by default. Do *not* use this variable directly; instead call
2632 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2633 * to get the correct matrix based on the latest rotation and scale properties.
2634 */
2635 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002636
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002637 /**
2638 * An internal variable that tracks whether we need to recalculate the
2639 * transform matrix, based on whether the rotation or scaleX/Y properties
2640 * have changed since the matrix was last calculated.
2641 */
2642 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002643
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002644 /**
2645 * An internal variable that tracks whether we need to recalculate the
2646 * transform matrix, based on whether the rotation or scaleX/Y properties
2647 * have changed since the matrix was last calculated.
2648 */
2649 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002650
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002651 /**
2652 * A variable that tracks whether we need to recalculate the
2653 * transform matrix, based on whether the rotation or scaleX/Y properties
2654 * have changed since the matrix was last calculated. This variable
2655 * is only valid after a call to updateMatrix() or to a function that
2656 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2657 */
2658 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002659
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002660 /**
2661 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2662 */
2663 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002664
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002665 /**
2666 * This matrix is used when computing the matrix for 3D rotations.
2667 */
2668 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002669
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002670 /**
2671 * These prev values are used to recalculate a centered pivot point when necessary. The
2672 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2673 * set), so thes values are only used then as well.
2674 */
2675 private int mPrevWidth = -1;
2676 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002677
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002678 /**
2679 * The degrees rotation around the vertical axis through the pivot point.
2680 */
2681 @ViewDebug.ExportedProperty
2682 float mRotationY = 0f;
2683
2684 /**
2685 * The degrees rotation around the horizontal axis through the pivot point.
2686 */
2687 @ViewDebug.ExportedProperty
2688 float mRotationX = 0f;
2689
2690 /**
2691 * The degrees rotation around the pivot point.
2692 */
2693 @ViewDebug.ExportedProperty
2694 float mRotation = 0f;
2695
2696 /**
2697 * The amount of translation of the object away from its left property (post-layout).
2698 */
2699 @ViewDebug.ExportedProperty
2700 float mTranslationX = 0f;
2701
2702 /**
2703 * The amount of translation of the object away from its top property (post-layout).
2704 */
2705 @ViewDebug.ExportedProperty
2706 float mTranslationY = 0f;
2707
2708 /**
2709 * The amount of scale in the x direction around the pivot point. A
2710 * value of 1 means no scaling is applied.
2711 */
2712 @ViewDebug.ExportedProperty
2713 float mScaleX = 1f;
2714
2715 /**
2716 * The amount of scale in the y direction around the pivot point. A
2717 * value of 1 means no scaling is applied.
2718 */
2719 @ViewDebug.ExportedProperty
2720 float mScaleY = 1f;
2721
2722 /**
Chet Haasea33de552012-02-03 16:28:24 -08002723 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002724 */
2725 @ViewDebug.ExportedProperty
2726 float mPivotX = 0f;
2727
2728 /**
Chet Haasea33de552012-02-03 16:28:24 -08002729 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002730 */
2731 @ViewDebug.ExportedProperty
2732 float mPivotY = 0f;
2733
2734 /**
2735 * The opacity of the View. This is a value from 0 to 1, where 0 means
2736 * completely transparent and 1 means completely opaque.
2737 */
2738 @ViewDebug.ExportedProperty
2739 float mAlpha = 1f;
2740 }
2741
2742 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002743
Joe Malin32736f02011-01-19 16:14:20 -08002744 private boolean mLastIsOpaque;
2745
Chet Haasefd2b0022010-08-06 13:08:56 -07002746 /**
2747 * Convenience value to check for float values that are close enough to zero to be considered
2748 * zero.
2749 */
Romain Guy2542d192010-08-18 11:47:12 -07002750 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002751
2752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 * The distance in pixels from the left edge of this view's parent
2754 * to the left edge of this view.
2755 * {@hide}
2756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002757 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 protected int mLeft;
2759 /**
2760 * The distance in pixels from the left edge of this view's parent
2761 * to the right edge of this view.
2762 * {@hide}
2763 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002764 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 protected int mRight;
2766 /**
2767 * The distance in pixels from the top edge of this view's parent
2768 * to the top edge of this view.
2769 * {@hide}
2770 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002771 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 protected int mTop;
2773 /**
2774 * The distance in pixels from the top edge of this view's parent
2775 * to the bottom edge of this view.
2776 * {@hide}
2777 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002778 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 protected int mBottom;
2780
2781 /**
2782 * The offset, in pixels, by which the content of this view is scrolled
2783 * horizontally.
2784 * {@hide}
2785 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002786 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 protected int mScrollX;
2788 /**
2789 * The offset, in pixels, by which the content of this view is scrolled
2790 * vertically.
2791 * {@hide}
2792 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002793 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 protected int mScrollY;
2795
2796 /**
2797 * The left padding in pixels, that is the distance in pixels between the
2798 * left edge of this view and the left edge of its content.
2799 * {@hide}
2800 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002801 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002802 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 /**
2804 * The right padding in pixels, that is the distance in pixels between the
2805 * right edge of this view and the right edge of its content.
2806 * {@hide}
2807 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002808 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002809 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 /**
2811 * The top padding in pixels, that is the distance in pixels between the
2812 * top edge of this view and the top edge of its content.
2813 * {@hide}
2814 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002815 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 protected int mPaddingTop;
2817 /**
2818 * The bottom padding in pixels, that is the distance in pixels between the
2819 * bottom edge of this view and the bottom edge of its content.
2820 * {@hide}
2821 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002822 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 protected int mPaddingBottom;
2824
2825 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002826 * The layout insets in pixels, that is the distance in pixels between the
2827 * visible edges of this view its bounds.
2828 */
2829 private Insets mLayoutInsets;
2830
2831 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002832 * Briefly describes the view and is primarily used for accessibility support.
2833 */
2834 private CharSequence mContentDescription;
2835
2836 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002837 * Specifies the id of a view for which this view serves as a label for
2838 * accessibility purposes.
2839 */
2840 private int mLabelForId = View.NO_ID;
2841
2842 /**
2843 * Predicate for matching labeled view id with its label for
2844 * accessibility purposes.
2845 */
2846 private MatchLabelForPredicate mMatchLabelForPredicate;
2847
2848 /**
2849 * Predicate for matching a view by its id.
2850 */
2851 private MatchIdPredicate mMatchIdPredicate;
2852
2853 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002855 *
2856 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002858 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002859 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860
2861 /**
2862 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002863 *
2864 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002866 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002867 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002869 /**
Adam Powell20232d02010-12-08 21:08:53 -08002870 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002871 *
2872 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002873 */
2874 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002875 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002876
2877 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002878 * Cache the paddingStart set by the user to append to the scrollbar's size.
2879 *
2880 */
2881 @ViewDebug.ExportedProperty(category = "padding")
2882 int mUserPaddingStart;
2883
2884 /**
2885 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2886 *
2887 */
2888 @ViewDebug.ExportedProperty(category = "padding")
2889 int mUserPaddingEnd;
2890
2891 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002892 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002893 *
2894 * @hide
2895 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002896 int mUserPaddingLeftInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002897
2898 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002899 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002900 *
2901 * @hide
2902 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002903 int mUserPaddingRightInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002904
2905 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002906 * Default undefined padding
2907 */
2908 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2909
2910 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002911 * @hide
2912 */
2913 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2914 /**
2915 * @hide
2916 */
2917 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918
Romain Guydfab3632012-10-03 14:53:25 -07002919 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002920 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921
2922 private int mBackgroundResource;
2923 private boolean mBackgroundSizeChanged;
2924
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002925 static class ListenerInfo {
2926 /**
2927 * Listener used to dispatch focus change events.
2928 * This field should be made private, so it is hidden from the SDK.
2929 * {@hide}
2930 */
2931 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002933 /**
2934 * Listeners for layout change events.
2935 */
2936 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002937
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002938 /**
2939 * Listeners for attach events.
2940 */
2941 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002942
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002943 /**
2944 * Listener used to dispatch click events.
2945 * This field should be made private, so it is hidden from the SDK.
2946 * {@hide}
2947 */
2948 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002950 /**
2951 * Listener used to dispatch long click events.
2952 * This field should be made private, so it is hidden from the SDK.
2953 * {@hide}
2954 */
2955 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002957 /**
2958 * Listener used to build the context menu.
2959 * This field should be made private, so it is hidden from the SDK.
2960 * {@hide}
2961 */
2962 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002964 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002966 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002968 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002969
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002970 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002971
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002972 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002973
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002974 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2975 }
2976
2977 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 /**
2980 * The application environment this view lives in.
2981 * This field should be made private, so it is hidden from the SDK.
2982 * {@hide}
2983 */
2984 protected Context mContext;
2985
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002986 private final Resources mResources;
2987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 private ScrollabilityCache mScrollCache;
2989
2990 private int[] mDrawableState = null;
2991
Romain Guy0211a0a2011-02-14 16:34:59 -08002992 /**
2993 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002994 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002995 * @hide
2996 */
2997 public boolean mCachingFailed;
2998
Romain Guy02890fd2010-08-06 17:58:44 -07002999 private Bitmap mDrawingCache;
3000 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08003001 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003002 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003
3004 /**
3005 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3006 * the user may specify which view to go to next.
3007 */
3008 private int mNextFocusLeftId = View.NO_ID;
3009
3010 /**
3011 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3012 * the user may specify which view to go to next.
3013 */
3014 private int mNextFocusRightId = View.NO_ID;
3015
3016 /**
3017 * When this view has focus and the next focus is {@link #FOCUS_UP},
3018 * the user may specify which view to go to next.
3019 */
3020 private int mNextFocusUpId = View.NO_ID;
3021
3022 /**
3023 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3024 * the user may specify which view to go to next.
3025 */
3026 private int mNextFocusDownId = View.NO_ID;
3027
Jeff Brown4e6319b2010-12-13 10:36:51 -08003028 /**
3029 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3030 * the user may specify which view to go to next.
3031 */
3032 int mNextFocusForwardId = View.NO_ID;
3033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003035 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003036 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003037 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 private UnsetPressedState mUnsetPressedState;
3040
3041 /**
3042 * Whether the long press's action has been invoked. The tap's action is invoked on the
3043 * up event while a long press is invoked as soon as the long press duration is reached, so
3044 * a long press could be performed before the tap is checked, in which case the tap's action
3045 * should not be invoked.
3046 */
3047 private boolean mHasPerformedLongPress;
3048
3049 /**
3050 * The minimum height of the view. We'll try our best to have the height
3051 * of this view to at least this amount.
3052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003053 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 private int mMinHeight;
3055
3056 /**
3057 * The minimum width of the view. We'll try our best to have the width
3058 * of this view to at least this amount.
3059 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003060 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 private int mMinWidth;
3062
3063 /**
3064 * The delegate to handle touch events that are physically in this view
3065 * but should be handled by another view.
3066 */
3067 private TouchDelegate mTouchDelegate = null;
3068
3069 /**
3070 * Solid color to use as a background when creating the drawing cache. Enables
3071 * the cache to use 16 bit bitmaps instead of 32 bit.
3072 */
3073 private int mDrawingCacheBackgroundColor = 0;
3074
3075 /**
3076 * Special tree observer used when mAttachInfo is null.
3077 */
3078 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003079
Adam Powelle14579b2009-12-16 18:39:52 -08003080 /**
3081 * Cache the touch slop from the context that created the view.
3082 */
3083 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003086 * Object that handles automatic animation of view properties.
3087 */
3088 private ViewPropertyAnimator mAnimator = null;
3089
3090 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003091 * Flag indicating that a drag can cross window boundaries. When
3092 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3093 * with this flag set, all visible applications will be able to participate
3094 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003095 *
3096 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003097 */
3098 public static final int DRAG_FLAG_GLOBAL = 1;
3099
3100 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003101 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3102 */
3103 private float mVerticalScrollFactor;
3104
3105 /**
Adam Powell20232d02010-12-08 21:08:53 -08003106 * Position of the vertical scroll bar.
3107 */
3108 private int mVerticalScrollbarPosition;
3109
3110 /**
3111 * Position the scroll bar at the default position as determined by the system.
3112 */
3113 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3114
3115 /**
3116 * Position the scroll bar along the left edge.
3117 */
3118 public static final int SCROLLBAR_POSITION_LEFT = 1;
3119
3120 /**
3121 * Position the scroll bar along the right edge.
3122 */
3123 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3124
3125 /**
Romain Guy171c5922011-01-06 10:04:23 -08003126 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003127 *
3128 * @see #getLayerType()
3129 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003130 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003131 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003132 */
3133 public static final int LAYER_TYPE_NONE = 0;
3134
3135 /**
3136 * <p>Indicates that the view has a software layer. A software layer is backed
3137 * by a bitmap and causes the view to be rendered using Android's software
3138 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003139 *
Romain Guy171c5922011-01-06 10:04:23 -08003140 * <p>Software layers have various usages:</p>
3141 * <p>When the application is not using hardware acceleration, a software layer
3142 * is useful to apply a specific color filter and/or blending mode and/or
3143 * translucency to a view and all its children.</p>
3144 * <p>When the application is using hardware acceleration, a software layer
3145 * is useful to render drawing primitives not supported by the hardware
3146 * accelerated pipeline. It can also be used to cache a complex view tree
3147 * into a texture and reduce the complexity of drawing operations. For instance,
3148 * when animating a complex view tree with a translation, a software layer can
3149 * be used to render the view tree only once.</p>
3150 * <p>Software layers should be avoided when the affected view tree updates
3151 * often. Every update will require to re-render the software layer, which can
3152 * potentially be slow (particularly when hardware acceleration is turned on
3153 * since the layer will have to be uploaded into a hardware texture after every
3154 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003155 *
3156 * @see #getLayerType()
3157 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003158 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003159 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003160 */
3161 public static final int LAYER_TYPE_SOFTWARE = 1;
3162
3163 /**
3164 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3165 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3166 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3167 * rendering pipeline, but only if hardware acceleration is turned on for the
3168 * view hierarchy. When hardware acceleration is turned off, hardware layers
3169 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003170 *
Romain Guy171c5922011-01-06 10:04:23 -08003171 * <p>A hardware layer is useful to apply a specific color filter and/or
3172 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003173 * <p>A hardware layer can be used to cache a complex view tree into a
3174 * texture and reduce the complexity of drawing operations. For instance,
3175 * when animating a complex view tree with a translation, a hardware layer can
3176 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003177 * <p>A hardware layer can also be used to increase the rendering quality when
3178 * rotation transformations are applied on a view. It can also be used to
3179 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003180 *
3181 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003182 * @see #setLayerType(int, android.graphics.Paint)
3183 * @see #LAYER_TYPE_NONE
3184 * @see #LAYER_TYPE_SOFTWARE
3185 */
3186 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003187
Romain Guy3aaff3a2011-01-12 14:18:47 -08003188 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3189 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3190 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3191 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3192 })
Romain Guy171c5922011-01-06 10:04:23 -08003193 int mLayerType = LAYER_TYPE_NONE;
3194 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003195 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003196
3197 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003198 * Set to true when the view is sending hover accessibility events because it
3199 * is the innermost hovered view.
3200 */
3201 private boolean mSendingHoverAccessibilityEvents;
3202
Dianne Hackborn4702a852012-08-17 15:18:29 -07003203 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003204 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003205 */
3206 AccessibilityDelegate mAccessibilityDelegate;
3207
3208 /**
3209 * Consistency verifier for debugging purposes.
3210 * @hide
3211 */
3212 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3213 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3214 new InputEventConsistencyVerifier(this, 0) : null;
3215
Adam Powella9108a22012-07-18 11:18:09 -07003216 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3217
Jeff Brown87b7f802011-06-21 18:35:45 -07003218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 * Simple constructor to use when creating a view from code.
3220 *
3221 * @param context The Context the view is running in, through which it can
3222 * access the current theme, resources, etc.
3223 */
3224 public View(Context context) {
3225 mContext = context;
3226 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003227 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07003228 // Set some flags defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003229 mPrivateFlags2 =
3230 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003231 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003232 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003233 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003234 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003235 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003236 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003237 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003238 mUserPaddingStart = UNDEFINED_PADDING;
3239 mUserPaddingEnd = UNDEFINED_PADDING;
Adam Powell7da4b732012-12-07 15:28:33 -08003240
3241 if (!sUseBrokenMakeMeasureSpec && context.getApplicationInfo().targetSdkVersion <=
3242 Build.VERSION_CODES.JELLY_BEAN_MR1 ) {
3243 // Older apps may need this compatibility hack for measurement.
3244 sUseBrokenMakeMeasureSpec = true;
3245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 }
3247
3248 /**
3249 * Constructor that is called when inflating a view from XML. This is called
3250 * when a view is being constructed from an XML file, supplying attributes
3251 * that were specified in the XML file. This version uses a default style of
3252 * 0, so the only attribute values applied are those in the Context's Theme
3253 * and the given AttributeSet.
3254 *
3255 * <p>
3256 * The method onFinishInflate() will be called after all children have been
3257 * added.
3258 *
3259 * @param context The Context the view is running in, through which it can
3260 * access the current theme, resources, etc.
3261 * @param attrs The attributes of the XML tag that is inflating the view.
3262 * @see #View(Context, AttributeSet, int)
3263 */
3264 public View(Context context, AttributeSet attrs) {
3265 this(context, attrs, 0);
3266 }
3267
3268 /**
3269 * Perform inflation from XML and apply a class-specific base style. This
3270 * constructor of View allows subclasses to use their own base style when
3271 * they are inflating. For example, a Button class's constructor would call
3272 * this version of the super class constructor and supply
3273 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3274 * the theme's button style to modify all of the base view attributes (in
3275 * particular its background) as well as the Button class's attributes.
3276 *
3277 * @param context The Context the view is running in, through which it can
3278 * access the current theme, resources, etc.
3279 * @param attrs The attributes of the XML tag that is inflating the view.
3280 * @param defStyle The default style to apply to this view. If 0, no style
3281 * will be applied (beyond what is included in the theme). This may
3282 * either be an attribute resource, whose value will be retrieved
3283 * from the current theme, or an explicit style resource.
3284 * @see #View(Context, AttributeSet)
3285 */
3286 public View(Context context, AttributeSet attrs, int defStyle) {
3287 this(context);
3288
3289 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3290 defStyle, 0);
3291
3292 Drawable background = null;
3293
3294 int leftPadding = -1;
3295 int topPadding = -1;
3296 int rightPadding = -1;
3297 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003298 int startPadding = UNDEFINED_PADDING;
3299 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300
3301 int padding = -1;
3302
3303 int viewFlagValues = 0;
3304 int viewFlagMasks = 0;
3305
3306 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 int x = 0;
3309 int y = 0;
3310
Chet Haase73066682010-11-29 15:55:32 -08003311 float tx = 0;
3312 float ty = 0;
3313 float rotation = 0;
3314 float rotationX = 0;
3315 float rotationY = 0;
3316 float sx = 1f;
3317 float sy = 1f;
3318 boolean transformSet = false;
3319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003321 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003322 boolean initializeScrollbars = false;
3323
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003324 boolean leftPaddingDefined = false;
3325 boolean rightPaddingDefined = false;
3326 boolean startPaddingDefined = false;
3327 boolean endPaddingDefined = false;
3328
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003329 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 final int N = a.getIndexCount();
3332 for (int i = 0; i < N; i++) {
3333 int attr = a.getIndex(i);
3334 switch (attr) {
3335 case com.android.internal.R.styleable.View_background:
3336 background = a.getDrawable(attr);
3337 break;
3338 case com.android.internal.R.styleable.View_padding:
3339 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003340 mUserPaddingLeftInitial = padding;
3341 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003342 leftPaddingDefined = true;
3343 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 break;
3345 case com.android.internal.R.styleable.View_paddingLeft:
3346 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003347 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003348 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 break;
3350 case com.android.internal.R.styleable.View_paddingTop:
3351 topPadding = a.getDimensionPixelSize(attr, -1);
3352 break;
3353 case com.android.internal.R.styleable.View_paddingRight:
3354 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003355 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003356 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 break;
3358 case com.android.internal.R.styleable.View_paddingBottom:
3359 bottomPadding = a.getDimensionPixelSize(attr, -1);
3360 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003361 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003362 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003363 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003364 break;
3365 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003366 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003367 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003368 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 case com.android.internal.R.styleable.View_scrollX:
3370 x = a.getDimensionPixelOffset(attr, 0);
3371 break;
3372 case com.android.internal.R.styleable.View_scrollY:
3373 y = a.getDimensionPixelOffset(attr, 0);
3374 break;
Chet Haase73066682010-11-29 15:55:32 -08003375 case com.android.internal.R.styleable.View_alpha:
3376 setAlpha(a.getFloat(attr, 1f));
3377 break;
3378 case com.android.internal.R.styleable.View_transformPivotX:
3379 setPivotX(a.getDimensionPixelOffset(attr, 0));
3380 break;
3381 case com.android.internal.R.styleable.View_transformPivotY:
3382 setPivotY(a.getDimensionPixelOffset(attr, 0));
3383 break;
3384 case com.android.internal.R.styleable.View_translationX:
3385 tx = a.getDimensionPixelOffset(attr, 0);
3386 transformSet = true;
3387 break;
3388 case com.android.internal.R.styleable.View_translationY:
3389 ty = a.getDimensionPixelOffset(attr, 0);
3390 transformSet = true;
3391 break;
3392 case com.android.internal.R.styleable.View_rotation:
3393 rotation = a.getFloat(attr, 0);
3394 transformSet = true;
3395 break;
3396 case com.android.internal.R.styleable.View_rotationX:
3397 rotationX = a.getFloat(attr, 0);
3398 transformSet = true;
3399 break;
3400 case com.android.internal.R.styleable.View_rotationY:
3401 rotationY = a.getFloat(attr, 0);
3402 transformSet = true;
3403 break;
3404 case com.android.internal.R.styleable.View_scaleX:
3405 sx = a.getFloat(attr, 1f);
3406 transformSet = true;
3407 break;
3408 case com.android.internal.R.styleable.View_scaleY:
3409 sy = a.getFloat(attr, 1f);
3410 transformSet = true;
3411 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 case com.android.internal.R.styleable.View_id:
3413 mID = a.getResourceId(attr, NO_ID);
3414 break;
3415 case com.android.internal.R.styleable.View_tag:
3416 mTag = a.getText(attr);
3417 break;
3418 case com.android.internal.R.styleable.View_fitsSystemWindows:
3419 if (a.getBoolean(attr, false)) {
3420 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3421 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_focusable:
3425 if (a.getBoolean(attr, false)) {
3426 viewFlagValues |= FOCUSABLE;
3427 viewFlagMasks |= FOCUSABLE_MASK;
3428 }
3429 break;
3430 case com.android.internal.R.styleable.View_focusableInTouchMode:
3431 if (a.getBoolean(attr, false)) {
3432 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3433 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3434 }
3435 break;
3436 case com.android.internal.R.styleable.View_clickable:
3437 if (a.getBoolean(attr, false)) {
3438 viewFlagValues |= CLICKABLE;
3439 viewFlagMasks |= CLICKABLE;
3440 }
3441 break;
3442 case com.android.internal.R.styleable.View_longClickable:
3443 if (a.getBoolean(attr, false)) {
3444 viewFlagValues |= LONG_CLICKABLE;
3445 viewFlagMasks |= LONG_CLICKABLE;
3446 }
3447 break;
3448 case com.android.internal.R.styleable.View_saveEnabled:
3449 if (!a.getBoolean(attr, true)) {
3450 viewFlagValues |= SAVE_DISABLED;
3451 viewFlagMasks |= SAVE_DISABLED_MASK;
3452 }
3453 break;
3454 case com.android.internal.R.styleable.View_duplicateParentState:
3455 if (a.getBoolean(attr, false)) {
3456 viewFlagValues |= DUPLICATE_PARENT_STATE;
3457 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3458 }
3459 break;
3460 case com.android.internal.R.styleable.View_visibility:
3461 final int visibility = a.getInt(attr, 0);
3462 if (visibility != 0) {
3463 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3464 viewFlagMasks |= VISIBILITY_MASK;
3465 }
3466 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003467 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003468 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003469 mPrivateFlags2 &=
3470 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003471 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003472 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003473 final int value = (layoutDirection != -1) ?
3474 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003475 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003476 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 case com.android.internal.R.styleable.View_drawingCacheQuality:
3478 final int cacheQuality = a.getInt(attr, 0);
3479 if (cacheQuality != 0) {
3480 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3481 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3482 }
3483 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003484 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003485 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003486 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003487 case com.android.internal.R.styleable.View_labelFor:
3488 setLabelFor(a.getResourceId(attr, NO_ID));
3489 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3491 if (!a.getBoolean(attr, true)) {
3492 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3493 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3494 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003495 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3497 if (!a.getBoolean(attr, true)) {
3498 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3499 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3500 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003501 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 case R.styleable.View_scrollbars:
3503 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3504 if (scrollbars != SCROLLBARS_NONE) {
3505 viewFlagValues |= scrollbars;
3506 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003507 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 }
3509 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003510 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003512 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003513 // Ignore the attribute starting with ICS
3514 break;
3515 }
3516 // With builds < ICS, fall through and apply fading edges
3517 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3519 if (fadingEdge != FADING_EDGE_NONE) {
3520 viewFlagValues |= fadingEdge;
3521 viewFlagMasks |= FADING_EDGE_MASK;
3522 initializeFadingEdge(a);
3523 }
3524 break;
3525 case R.styleable.View_scrollbarStyle:
3526 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3527 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3528 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3529 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3530 }
3531 break;
3532 case R.styleable.View_isScrollContainer:
3533 setScrollContainer = true;
3534 if (a.getBoolean(attr, false)) {
3535 setScrollContainer(true);
3536 }
3537 break;
3538 case com.android.internal.R.styleable.View_keepScreenOn:
3539 if (a.getBoolean(attr, false)) {
3540 viewFlagValues |= KEEP_SCREEN_ON;
3541 viewFlagMasks |= KEEP_SCREEN_ON;
3542 }
3543 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003544 case R.styleable.View_filterTouchesWhenObscured:
3545 if (a.getBoolean(attr, false)) {
3546 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3547 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3548 }
3549 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 case R.styleable.View_nextFocusLeft:
3551 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3552 break;
3553 case R.styleable.View_nextFocusRight:
3554 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3555 break;
3556 case R.styleable.View_nextFocusUp:
3557 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3558 break;
3559 case R.styleable.View_nextFocusDown:
3560 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3561 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003562 case R.styleable.View_nextFocusForward:
3563 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3564 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 case R.styleable.View_minWidth:
3566 mMinWidth = a.getDimensionPixelSize(attr, 0);
3567 break;
3568 case R.styleable.View_minHeight:
3569 mMinHeight = a.getDimensionPixelSize(attr, 0);
3570 break;
Romain Guy9a817362009-05-01 10:57:14 -07003571 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003572 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003573 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003574 + "be used within a restricted context");
3575 }
3576
Romain Guy9a817362009-05-01 10:57:14 -07003577 final String handlerName = a.getString(attr);
3578 if (handlerName != null) {
3579 setOnClickListener(new OnClickListener() {
3580 private Method mHandler;
3581
3582 public void onClick(View v) {
3583 if (mHandler == null) {
3584 try {
3585 mHandler = getContext().getClass().getMethod(handlerName,
3586 View.class);
3587 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003588 int id = getId();
3589 String idText = id == NO_ID ? "" : " with id '"
3590 + getContext().getResources().getResourceEntryName(
3591 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003592 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003593 handlerName + "(View) in the activity "
3594 + getContext().getClass() + " for onClick handler"
3595 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003596 }
3597 }
3598
3599 try {
3600 mHandler.invoke(getContext(), View.this);
3601 } catch (IllegalAccessException e) {
3602 throw new IllegalStateException("Could not execute non "
3603 + "public method of the activity", e);
3604 } catch (InvocationTargetException e) {
3605 throw new IllegalStateException("Could not execute "
3606 + "method of the activity", e);
3607 }
3608 }
3609 });
3610 }
3611 break;
Adam Powell637d3372010-08-25 14:37:03 -07003612 case R.styleable.View_overScrollMode:
3613 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3614 break;
Adam Powell20232d02010-12-08 21:08:53 -08003615 case R.styleable.View_verticalScrollbarPosition:
3616 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3617 break;
Romain Guy171c5922011-01-06 10:04:23 -08003618 case R.styleable.View_layerType:
3619 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3620 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003621 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003622 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003623 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003624 // Set the text direction flags depending on the value of the attribute
3625 final int textDirection = a.getInt(attr, -1);
3626 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003627 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003628 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003629 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003630 case R.styleable.View_textAlignment:
3631 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003632 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003633 // Set the text alignment flag depending on the value of the attribute
3634 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003635 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003636 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003637 case R.styleable.View_importantForAccessibility:
3638 setImportantForAccessibility(a.getInt(attr,
3639 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003640 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 }
3642 }
3643
Adam Powell637d3372010-08-25 14:37:03 -07003644 setOverScrollMode(overScrollMode);
3645
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003646 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3647 // the resolved layout direction). Those cached values will be used later during padding
3648 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003649 mUserPaddingStart = startPadding;
3650 mUserPaddingEnd = endPadding;
3651
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003652 if (background != null) {
3653 setBackground(background);
3654 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 if (padding >= 0) {
3657 leftPadding = padding;
3658 topPadding = padding;
3659 rightPadding = padding;
3660 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003661 mUserPaddingLeftInitial = padding;
3662 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 }
3664
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003665 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003666 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3667 // left / right padding are used if defined (meaning here nothing to do). If they are not
3668 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3669 // start / end and resolve them as left / right (layout direction is not taken into account).
3670 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3671 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3672 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003673 if (!leftPaddingDefined && startPaddingDefined) {
3674 leftPadding = startPadding;
3675 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003676 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003677 if (!rightPaddingDefined && endPaddingDefined) {
3678 rightPadding = endPadding;
3679 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003680 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003681 } else {
3682 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3683 // values defined. Otherwise, left /right values are used.
3684 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3685 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3686 // defined.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003687 if (leftPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003688 mUserPaddingLeftInitial = leftPadding;
3689 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003690 if (rightPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003691 mUserPaddingRightInitial = rightPadding;
3692 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003693 }
3694
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003695 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003696 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003698 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3700
3701 if (viewFlagMasks != 0) {
3702 setFlags(viewFlagValues, viewFlagMasks);
3703 }
3704
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003705 if (initializeScrollbars) {
3706 initializeScrollbars(a);
3707 }
3708
3709 a.recycle();
3710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 // Needs to be called after mViewFlags is set
3712 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3713 recomputePadding();
3714 }
3715
3716 if (x != 0 || y != 0) {
3717 scrollTo(x, y);
3718 }
3719
Chet Haase73066682010-11-29 15:55:32 -08003720 if (transformSet) {
3721 setTranslationX(tx);
3722 setTranslationY(ty);
3723 setRotation(rotation);
3724 setRotationX(rotationX);
3725 setRotationY(rotationY);
3726 setScaleX(sx);
3727 setScaleY(sy);
3728 }
3729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3731 setScrollContainer(true);
3732 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003733
3734 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 }
3736
3737 /**
3738 * Non-public constructor for use in testing
3739 */
3740 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003741 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 }
3743
Dianne Hackborn4702a852012-08-17 15:18:29 -07003744 public String toString() {
3745 StringBuilder out = new StringBuilder(128);
3746 out.append(getClass().getName());
3747 out.append('{');
3748 out.append(Integer.toHexString(System.identityHashCode(this)));
3749 out.append(' ');
3750 switch (mViewFlags&VISIBILITY_MASK) {
3751 case VISIBLE: out.append('V'); break;
3752 case INVISIBLE: out.append('I'); break;
3753 case GONE: out.append('G'); break;
3754 default: out.append('.'); break;
3755 }
3756 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3757 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3758 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3759 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3760 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3761 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3762 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3763 out.append(' ');
3764 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3765 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3766 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3767 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3768 out.append('p');
3769 } else {
3770 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3771 }
3772 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3773 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3774 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3775 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3776 out.append(' ');
3777 out.append(mLeft);
3778 out.append(',');
3779 out.append(mTop);
3780 out.append('-');
3781 out.append(mRight);
3782 out.append(',');
3783 out.append(mBottom);
3784 final int id = getId();
3785 if (id != NO_ID) {
3786 out.append(" #");
3787 out.append(Integer.toHexString(id));
3788 final Resources r = mResources;
3789 if (id != 0 && r != null) {
3790 try {
3791 String pkgname;
3792 switch (id&0xff000000) {
3793 case 0x7f000000:
3794 pkgname="app";
3795 break;
3796 case 0x01000000:
3797 pkgname="android";
3798 break;
3799 default:
3800 pkgname = r.getResourcePackageName(id);
3801 break;
3802 }
3803 String typename = r.getResourceTypeName(id);
3804 String entryname = r.getResourceEntryName(id);
3805 out.append(" ");
3806 out.append(pkgname);
3807 out.append(":");
3808 out.append(typename);
3809 out.append("/");
3810 out.append(entryname);
3811 } catch (Resources.NotFoundException e) {
3812 }
3813 }
3814 }
3815 out.append("}");
3816 return out.toString();
3817 }
3818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 /**
3820 * <p>
3821 * Initializes the fading edges from a given set of styled attributes. This
3822 * method should be called by subclasses that need fading edges and when an
3823 * instance of these subclasses is created programmatically rather than
3824 * being inflated from XML. This method is automatically called when the XML
3825 * is inflated.
3826 * </p>
3827 *
3828 * @param a the styled attributes set to initialize the fading edges from
3829 */
3830 protected void initializeFadingEdge(TypedArray a) {
3831 initScrollCache();
3832
3833 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3834 R.styleable.View_fadingEdgeLength,
3835 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3836 }
3837
3838 /**
3839 * Returns the size of the vertical faded edges used to indicate that more
3840 * content in this view is visible.
3841 *
3842 * @return The size in pixels of the vertical faded edge or 0 if vertical
3843 * faded edges are not enabled for this view.
3844 * @attr ref android.R.styleable#View_fadingEdgeLength
3845 */
3846 public int getVerticalFadingEdgeLength() {
3847 if (isVerticalFadingEdgeEnabled()) {
3848 ScrollabilityCache cache = mScrollCache;
3849 if (cache != null) {
3850 return cache.fadingEdgeLength;
3851 }
3852 }
3853 return 0;
3854 }
3855
3856 /**
3857 * Set the size of the faded edge used to indicate that more content in this
3858 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003859 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3860 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3861 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 *
3863 * @param length The size in pixels of the faded edge used to indicate that more
3864 * content in this view is visible.
3865 */
3866 public void setFadingEdgeLength(int length) {
3867 initScrollCache();
3868 mScrollCache.fadingEdgeLength = length;
3869 }
3870
3871 /**
3872 * Returns the size of the horizontal faded edges used to indicate that more
3873 * content in this view is visible.
3874 *
3875 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3876 * faded edges are not enabled for this view.
3877 * @attr ref android.R.styleable#View_fadingEdgeLength
3878 */
3879 public int getHorizontalFadingEdgeLength() {
3880 if (isHorizontalFadingEdgeEnabled()) {
3881 ScrollabilityCache cache = mScrollCache;
3882 if (cache != null) {
3883 return cache.fadingEdgeLength;
3884 }
3885 }
3886 return 0;
3887 }
3888
3889 /**
3890 * Returns the width of the vertical scrollbar.
3891 *
3892 * @return The width in pixels of the vertical scrollbar or 0 if there
3893 * is no vertical scrollbar.
3894 */
3895 public int getVerticalScrollbarWidth() {
3896 ScrollabilityCache cache = mScrollCache;
3897 if (cache != null) {
3898 ScrollBarDrawable scrollBar = cache.scrollBar;
3899 if (scrollBar != null) {
3900 int size = scrollBar.getSize(true);
3901 if (size <= 0) {
3902 size = cache.scrollBarSize;
3903 }
3904 return size;
3905 }
3906 return 0;
3907 }
3908 return 0;
3909 }
3910
3911 /**
3912 * Returns the height of the horizontal scrollbar.
3913 *
3914 * @return The height in pixels of the horizontal scrollbar or 0 if
3915 * there is no horizontal scrollbar.
3916 */
3917 protected int getHorizontalScrollbarHeight() {
3918 ScrollabilityCache cache = mScrollCache;
3919 if (cache != null) {
3920 ScrollBarDrawable scrollBar = cache.scrollBar;
3921 if (scrollBar != null) {
3922 int size = scrollBar.getSize(false);
3923 if (size <= 0) {
3924 size = cache.scrollBarSize;
3925 }
3926 return size;
3927 }
3928 return 0;
3929 }
3930 return 0;
3931 }
3932
3933 /**
3934 * <p>
3935 * Initializes the scrollbars from a given set of styled attributes. This
3936 * method should be called by subclasses that need scrollbars and when an
3937 * instance of these subclasses is created programmatically rather than
3938 * being inflated from XML. This method is automatically called when the XML
3939 * is inflated.
3940 * </p>
3941 *
3942 * @param a the styled attributes set to initialize the scrollbars from
3943 */
3944 protected void initializeScrollbars(TypedArray a) {
3945 initScrollCache();
3946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003948
Mike Cleronf116bf82009-09-27 19:14:12 -07003949 if (scrollabilityCache.scrollBar == null) {
3950 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3951 }
Joe Malin32736f02011-01-19 16:14:20 -08003952
Romain Guy8bda2482010-03-02 11:42:11 -08003953 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954
Mike Cleronf116bf82009-09-27 19:14:12 -07003955 if (!fadeScrollbars) {
3956 scrollabilityCache.state = ScrollabilityCache.ON;
3957 }
3958 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003959
3960
Mike Cleronf116bf82009-09-27 19:14:12 -07003961 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3962 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3963 .getScrollBarFadeDuration());
3964 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3965 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3966 ViewConfiguration.getScrollDefaultDelay());
3967
Joe Malin32736f02011-01-19 16:14:20 -08003968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3970 com.android.internal.R.styleable.View_scrollbarSize,
3971 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3972
3973 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3974 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3975
3976 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3977 if (thumb != null) {
3978 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3979 }
3980
3981 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3982 false);
3983 if (alwaysDraw) {
3984 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3985 }
3986
3987 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3988 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3989
3990 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3991 if (thumb != null) {
3992 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3993 }
3994
3995 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3996 false);
3997 if (alwaysDraw) {
3998 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3999 }
4000
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07004001 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07004002 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07004003 if (track != null) {
4004 track.setLayoutDirection(layoutDirection);
4005 }
4006 if (thumb != null) {
4007 thumb.setLayoutDirection(layoutDirection);
4008 }
4009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004011 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 }
4013
4014 /**
4015 * <p>
4016 * Initalizes the scrollability cache if necessary.
4017 * </p>
4018 */
4019 private void initScrollCache() {
4020 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004021 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 }
4023 }
4024
Philip Milne6c8ea062012-04-03 17:38:43 -07004025 private ScrollabilityCache getScrollCache() {
4026 initScrollCache();
4027 return mScrollCache;
4028 }
4029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 /**
Adam Powell20232d02010-12-08 21:08:53 -08004031 * Set the position of the vertical scroll bar. Should be one of
4032 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4033 * {@link #SCROLLBAR_POSITION_RIGHT}.
4034 *
4035 * @param position Where the vertical scroll bar should be positioned.
4036 */
4037 public void setVerticalScrollbarPosition(int position) {
4038 if (mVerticalScrollbarPosition != position) {
4039 mVerticalScrollbarPosition = position;
4040 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004041 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004042 }
4043 }
4044
4045 /**
4046 * @return The position where the vertical scroll bar will show, if applicable.
4047 * @see #setVerticalScrollbarPosition(int)
4048 */
4049 public int getVerticalScrollbarPosition() {
4050 return mVerticalScrollbarPosition;
4051 }
4052
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004053 ListenerInfo getListenerInfo() {
4054 if (mListenerInfo != null) {
4055 return mListenerInfo;
4056 }
4057 mListenerInfo = new ListenerInfo();
4058 return mListenerInfo;
4059 }
4060
Adam Powell20232d02010-12-08 21:08:53 -08004061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 * Register a callback to be invoked when focus of this view changed.
4063 *
4064 * @param l The callback that will run.
4065 */
4066 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004067 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 }
4069
4070 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004071 * Add a listener that will be called when the bounds of the view change due to
4072 * layout processing.
4073 *
4074 * @param listener The listener that will be called when layout bounds change.
4075 */
4076 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004077 ListenerInfo li = getListenerInfo();
4078 if (li.mOnLayoutChangeListeners == null) {
4079 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004080 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004081 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4082 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004083 }
Chet Haase21cd1382010-09-01 17:42:29 -07004084 }
4085
4086 /**
4087 * Remove a listener for layout changes.
4088 *
4089 * @param listener The listener for layout bounds change.
4090 */
4091 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004092 ListenerInfo li = mListenerInfo;
4093 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004094 return;
4095 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004096 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004097 }
4098
4099 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004100 * Add a listener for attach state changes.
4101 *
4102 * This listener will be called whenever this view is attached or detached
4103 * from a window. Remove the listener using
4104 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4105 *
4106 * @param listener Listener to attach
4107 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4108 */
4109 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004110 ListenerInfo li = getListenerInfo();
4111 if (li.mOnAttachStateChangeListeners == null) {
4112 li.mOnAttachStateChangeListeners
4113 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004114 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004115 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004116 }
4117
4118 /**
4119 * Remove a listener for attach state changes. The listener will receive no further
4120 * notification of window attach/detach events.
4121 *
4122 * @param listener Listener to remove
4123 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4124 */
4125 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004126 ListenerInfo li = mListenerInfo;
4127 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004128 return;
4129 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004130 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004131 }
4132
4133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * Returns the focus-change callback registered for this view.
4135 *
4136 * @return The callback, or null if one is not registered.
4137 */
4138 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004139 ListenerInfo li = mListenerInfo;
4140 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 }
4142
4143 /**
4144 * Register a callback to be invoked when this view is clicked. If this view is not
4145 * clickable, it becomes clickable.
4146 *
4147 * @param l The callback that will run
4148 *
4149 * @see #setClickable(boolean)
4150 */
4151 public void setOnClickListener(OnClickListener l) {
4152 if (!isClickable()) {
4153 setClickable(true);
4154 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004155 getListenerInfo().mOnClickListener = l;
4156 }
4157
4158 /**
4159 * Return whether this view has an attached OnClickListener. Returns
4160 * true if there is a listener, false if there is none.
4161 */
4162 public boolean hasOnClickListeners() {
4163 ListenerInfo li = mListenerInfo;
4164 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
4166
4167 /**
4168 * Register a callback to be invoked when this view is clicked and held. If this view is not
4169 * long clickable, it becomes long clickable.
4170 *
4171 * @param l The callback that will run
4172 *
4173 * @see #setLongClickable(boolean)
4174 */
4175 public void setOnLongClickListener(OnLongClickListener l) {
4176 if (!isLongClickable()) {
4177 setLongClickable(true);
4178 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004179 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
4181
4182 /**
4183 * Register a callback to be invoked when the context menu for this view is
4184 * being built. If this view is not long clickable, it becomes long clickable.
4185 *
4186 * @param l The callback that will run
4187 *
4188 */
4189 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4190 if (!isLongClickable()) {
4191 setLongClickable(true);
4192 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004193 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 }
4195
4196 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004197 * Call this view's OnClickListener, if it is defined. Performs all normal
4198 * actions associated with clicking: reporting accessibility event, playing
4199 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 *
4201 * @return True there was an assigned OnClickListener that was called, false
4202 * otherwise is returned.
4203 */
4204 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004205 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4206
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004207 ListenerInfo li = mListenerInfo;
4208 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004210 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 return true;
4212 }
4213
4214 return false;
4215 }
4216
4217 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004218 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4219 * this only calls the listener, and does not do any associated clicking
4220 * actions like reporting an accessibility event.
4221 *
4222 * @return True there was an assigned OnClickListener that was called, false
4223 * otherwise is returned.
4224 */
4225 public boolean callOnClick() {
4226 ListenerInfo li = mListenerInfo;
4227 if (li != null && li.mOnClickListener != null) {
4228 li.mOnClickListener.onClick(this);
4229 return true;
4230 }
4231 return false;
4232 }
4233
4234 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004235 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4236 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004238 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 */
4240 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004241 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004244 ListenerInfo li = mListenerInfo;
4245 if (li != null && li.mOnLongClickListener != null) {
4246 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 }
4248 if (!handled) {
4249 handled = showContextMenu();
4250 }
4251 if (handled) {
4252 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4253 }
4254 return handled;
4255 }
4256
4257 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004258 * Performs button-related actions during a touch down event.
4259 *
4260 * @param event The event.
4261 * @return True if the down was consumed.
4262 *
4263 * @hide
4264 */
4265 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4266 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4267 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4268 return true;
4269 }
4270 }
4271 return false;
4272 }
4273
4274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 * Bring up the context menu for this view.
4276 *
4277 * @return Whether a context menu was displayed.
4278 */
4279 public boolean showContextMenu() {
4280 return getParent().showContextMenuForChild(this);
4281 }
4282
4283 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004284 * Bring up the context menu for this view, referring to the item under the specified point.
4285 *
4286 * @param x The referenced x coordinate.
4287 * @param y The referenced y coordinate.
4288 * @param metaState The keyboard modifiers that were pressed.
4289 * @return Whether a context menu was displayed.
4290 *
4291 * @hide
4292 */
4293 public boolean showContextMenu(float x, float y, int metaState) {
4294 return showContextMenu();
4295 }
4296
4297 /**
Adam Powell6e346362010-07-23 10:18:23 -07004298 * Start an action mode.
4299 *
4300 * @param callback Callback that will control the lifecycle of the action mode
4301 * @return The new action mode if it is started, null otherwise
4302 *
4303 * @see ActionMode
4304 */
4305 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004306 ViewParent parent = getParent();
4307 if (parent == null) return null;
4308 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004309 }
4310
4311 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004312 * Register a callback to be invoked when a hardware key is pressed in this view.
4313 * Key presses in software input methods will generally not trigger the methods of
4314 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 * @param l the key listener to attach to this view
4316 */
4317 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004318 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
4320
4321 /**
4322 * Register a callback to be invoked when a touch event is sent to this view.
4323 * @param l the touch listener to attach to this view
4324 */
4325 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004326 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
4328
4329 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004330 * Register a callback to be invoked when a generic motion event is sent to this view.
4331 * @param l the generic motion listener to attach to this view
4332 */
4333 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004334 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004335 }
4336
4337 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004338 * Register a callback to be invoked when a hover event is sent to this view.
4339 * @param l the hover listener to attach to this view
4340 */
4341 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004342 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004343 }
4344
4345 /**
Joe Malin32736f02011-01-19 16:14:20 -08004346 * Register a drag event listener callback object for this View. The parameter is
4347 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4348 * View, the system calls the
4349 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4350 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004351 */
4352 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004353 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004354 }
4355
4356 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004357 * Give this view focus. This will cause
4358 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 *
4360 * Note: this does not check whether this {@link View} should get focus, it just
4361 * gives it focus no matter what. It should only be called internally by framework
4362 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4363 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004364 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4365 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 * focus moved when requestFocus() is called. It may not always
4367 * apply, in which case use the default View.FOCUS_DOWN.
4368 * @param previouslyFocusedRect The rectangle of the view that had focus
4369 * prior in this View's coordinate system.
4370 */
4371 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4372 if (DBG) {
4373 System.out.println(this + " requestFocus()");
4374 }
4375
Dianne Hackborn4702a852012-08-17 15:18:29 -07004376 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4377 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378
4379 if (mParent != null) {
4380 mParent.requestChildFocus(this, this);
4381 }
4382
4383 onFocusChanged(true, direction, previouslyFocusedRect);
4384 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004385
4386 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4387 notifyAccessibilityStateChanged();
4388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 }
4390 }
4391
4392 /**
4393 * Request that a rectangle of this view be visible on the screen,
4394 * scrolling if necessary just enough.
4395 *
4396 * <p>A View should call this if it maintains some notion of which part
4397 * of its content is interesting. For example, a text editing view
4398 * should call this when its cursor moves.
4399 *
4400 * @param rectangle The rectangle.
4401 * @return Whether any parent scrolled.
4402 */
4403 public boolean requestRectangleOnScreen(Rect rectangle) {
4404 return requestRectangleOnScreen(rectangle, false);
4405 }
4406
4407 /**
4408 * Request that a rectangle of this view be visible on the screen,
4409 * scrolling if necessary just enough.
4410 *
4411 * <p>A View should call this if it maintains some notion of which part
4412 * of its content is interesting. For example, a text editing view
4413 * should call this when its cursor moves.
4414 *
4415 * <p>When <code>immediate</code> is set to true, scrolling will not be
4416 * animated.
4417 *
4418 * @param rectangle The rectangle.
4419 * @param immediate True to forbid animated scrolling, false otherwise
4420 * @return Whether any parent scrolled.
4421 */
4422 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004423 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004424 return false;
4425 }
4426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004428
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004429 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004430 position.set(rectangle);
4431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 ViewParent parent = mParent;
4433 boolean scrolled = false;
4434 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004435 rectangle.set((int) position.left, (int) position.top,
4436 (int) position.right, (int) position.bottom);
4437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 scrolled |= parent.requestChildRectangleOnScreen(child,
4439 rectangle, immediate);
4440
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004441 if (!child.hasIdentityMatrix()) {
4442 child.getMatrix().mapRect(position);
4443 }
4444
4445 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446
4447 if (!(parent instanceof View)) {
4448 break;
4449 }
Romain Guy8506ab42009-06-11 17:35:47 -07004450
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004451 View parentView = (View) parent;
4452
4453 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4454
4455 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 parent = child.getParent();
4457 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 return scrolled;
4460 }
4461
4462 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004463 * Called when this view wants to give up focus. If focus is cleared
4464 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4465 * <p>
4466 * <strong>Note:</strong> When a View clears focus the framework is trying
4467 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004468 * View is the first from the top that can take focus, then all callbacks
4469 * related to clearing focus will be invoked after wich the framework will
4470 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004471 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 */
4473 public void clearFocus() {
4474 if (DBG) {
4475 System.out.println(this + " clearFocus()");
4476 }
4477
Dianne Hackborn4702a852012-08-17 15:18:29 -07004478 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4479 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480
4481 if (mParent != null) {
4482 mParent.clearChildFocus(this);
4483 }
4484
4485 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004488
4489 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004490
4491 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4492 notifyAccessibilityStateChanged();
4493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
4495 }
4496
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004497 void ensureInputFocusOnFirstFocusable() {
4498 View root = getRootView();
4499 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004500 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 }
4502 }
4503
4504 /**
4505 * Called internally by the view system when a new view is getting focus.
4506 * This is what clears the old focus.
4507 */
4508 void unFocus() {
4509 if (DBG) {
4510 System.out.println(this + " unFocus()");
4511 }
4512
Dianne Hackborn4702a852012-08-17 15:18:29 -07004513 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4514 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515
4516 onFocusChanged(false, 0, null);
4517 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004518
4519 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4520 notifyAccessibilityStateChanged();
4521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 }
4523 }
4524
4525 /**
4526 * Returns true if this view has focus iteself, or is the ancestor of the
4527 * view that has focus.
4528 *
4529 * @return True if this view has or contains focus, false otherwise.
4530 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004531 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004533 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 }
4535
4536 /**
4537 * Returns true if this view is focusable or if it contains a reachable View
4538 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4539 * is a View whose parents do not block descendants focus.
4540 *
4541 * Only {@link #VISIBLE} views are considered focusable.
4542 *
4543 * @return True if the view is focusable or if the view contains a focusable
4544 * View, false otherwise.
4545 *
4546 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4547 */
4548 public boolean hasFocusable() {
4549 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4550 }
4551
4552 /**
4553 * Called by the view system when the focus state of this view changes.
4554 * When the focus change event is caused by directional navigation, direction
4555 * and previouslyFocusedRect provide insight into where the focus is coming from.
4556 * When overriding, be sure to call up through to the super class so that
4557 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004558 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 * @param gainFocus True if the View has focus; false otherwise.
4560 * @param direction The direction focus has moved when requestFocus()
4561 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004562 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4563 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4564 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4566 * system, of the previously focused view. If applicable, this will be
4567 * passed in as finer grained information about where the focus is coming
4568 * from (in addition to direction). Will be <code>null</code> otherwise.
4569 */
4570 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004571 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004572 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4573 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004574 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004575 }
4576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 InputMethodManager imm = InputMethodManager.peekInstance();
4578 if (!gainFocus) {
4579 if (isPressed()) {
4580 setPressed(false);
4581 }
4582 if (imm != null && mAttachInfo != null
4583 && mAttachInfo.mHasWindowFocus) {
4584 imm.focusOut(this);
4585 }
Romain Guya2431d02009-04-30 16:30:00 -07004586 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 } else if (imm != null && mAttachInfo != null
4588 && mAttachInfo.mHasWindowFocus) {
4589 imm.focusIn(this);
4590 }
Romain Guy8506ab42009-06-11 17:35:47 -07004591
Romain Guy0fd89bf2011-01-26 15:41:30 -08004592 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004593 ListenerInfo li = mListenerInfo;
4594 if (li != null && li.mOnFocusChangeListener != null) {
4595 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 }
Joe Malin32736f02011-01-19 16:14:20 -08004597
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004598 if (mAttachInfo != null) {
4599 mAttachInfo.mKeyDispatchState.reset(this);
4600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 }
4602
4603 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004604 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004605 * not enabled this method has no effect. The default implementation calls
4606 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4607 * to populate information about the event source (this View), then calls
4608 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4609 * populate the text content of the event source including its descendants,
4610 * and last calls
4611 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4612 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004613 * <p>
4614 * If an {@link AccessibilityDelegate} has been specified via calling
4615 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4616 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4617 * responsible for handling this call.
4618 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004619 *
Scott Mainb303d832011-10-12 16:45:18 -07004620 * @param eventType The type of the event to send, as defined by several types from
4621 * {@link android.view.accessibility.AccessibilityEvent}, such as
4622 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4623 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004624 *
4625 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4626 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4627 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004628 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004629 */
4630 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004631 if (mAccessibilityDelegate != null) {
4632 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4633 } else {
4634 sendAccessibilityEventInternal(eventType);
4635 }
4636 }
4637
4638 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004639 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4640 * {@link AccessibilityEvent} to make an announcement which is related to some
4641 * sort of a context change for which none of the events representing UI transitions
4642 * is a good fit. For example, announcing a new page in a book. If accessibility
4643 * is not enabled this method does nothing.
4644 *
4645 * @param text The announcement text.
4646 */
4647 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004648 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004649 AccessibilityEvent event = AccessibilityEvent.obtain(
4650 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004651 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004652 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004653 event.setContentDescription(null);
4654 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004655 }
4656 }
4657
4658 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004659 * @see #sendAccessibilityEvent(int)
4660 *
4661 * Note: Called from the default {@link AccessibilityDelegate}.
4662 */
4663 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004664 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4665 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4666 }
4667 }
4668
4669 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004670 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4671 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004672 * perform a check whether accessibility is enabled.
4673 * <p>
4674 * If an {@link AccessibilityDelegate} has been specified via calling
4675 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4676 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4677 * is responsible for handling this call.
4678 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004679 *
4680 * @param event The event to send.
4681 *
4682 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004683 */
4684 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004685 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004686 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004687 } else {
4688 sendAccessibilityEventUncheckedInternal(event);
4689 }
4690 }
4691
4692 /**
4693 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4694 *
4695 * Note: Called from the default {@link AccessibilityDelegate}.
4696 */
4697 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004698 if (!isShown()) {
4699 return;
4700 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004701 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004702 // Only a subset of accessibility events populates text content.
4703 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4704 dispatchPopulateAccessibilityEvent(event);
4705 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004706 // In the beginning we called #isShown(), so we know that getParent() is not null.
4707 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004708 }
4709
4710 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004711 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4712 * to its children for adding their text content to the event. Note that the
4713 * event text is populated in a separate dispatch path since we add to the
4714 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004715 * A typical implementation will call
4716 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4717 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4718 * on each child. Override this method if custom population of the event text
4719 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004720 * <p>
4721 * If an {@link AccessibilityDelegate} has been specified via calling
4722 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4723 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4724 * is responsible for handling this call.
4725 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004726 * <p>
4727 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4728 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4729 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004730 *
4731 * @param event The event.
4732 *
4733 * @return True if the event population was completed.
4734 */
4735 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004736 if (mAccessibilityDelegate != null) {
4737 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4738 } else {
4739 return dispatchPopulateAccessibilityEventInternal(event);
4740 }
4741 }
4742
4743 /**
4744 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4745 *
4746 * Note: Called from the default {@link AccessibilityDelegate}.
4747 */
4748 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004749 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004750 return false;
4751 }
4752
4753 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004754 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004755 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004756 * text content. While this method is free to modify event
4757 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004758 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4759 * <p>
4760 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004761 * to the text added by the super implementation:
4762 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004763 * super.onPopulateAccessibilityEvent(event);
4764 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4765 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4766 * mCurrentDate.getTimeInMillis(), flags);
4767 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004768 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004769 * <p>
4770 * If an {@link AccessibilityDelegate} has been specified via calling
4771 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4772 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4773 * is responsible for handling this call.
4774 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004775 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4776 * information to the event, in case the default implementation has basic information to add.
4777 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004778 *
4779 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004780 *
4781 * @see #sendAccessibilityEvent(int)
4782 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004783 */
4784 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004785 if (mAccessibilityDelegate != null) {
4786 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4787 } else {
4788 onPopulateAccessibilityEventInternal(event);
4789 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004790 }
4791
4792 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004793 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4794 *
4795 * Note: Called from the default {@link AccessibilityDelegate}.
4796 */
4797 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4798
4799 }
4800
4801 /**
4802 * Initializes an {@link AccessibilityEvent} with information about
4803 * this View which is the event source. In other words, the source of
4804 * an accessibility event is the view whose state change triggered firing
4805 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004806 * <p>
4807 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004808 * to properties set by the super implementation:
4809 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4810 * super.onInitializeAccessibilityEvent(event);
4811 * event.setPassword(true);
4812 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004813 * <p>
4814 * If an {@link AccessibilityDelegate} has been specified via calling
4815 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4816 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4817 * is responsible for handling this call.
4818 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004819 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4820 * information to the event, in case the default implementation has basic information to add.
4821 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004822 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004823 *
4824 * @see #sendAccessibilityEvent(int)
4825 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4826 */
4827 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004828 if (mAccessibilityDelegate != null) {
4829 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4830 } else {
4831 onInitializeAccessibilityEventInternal(event);
4832 }
4833 }
4834
4835 /**
4836 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4837 *
4838 * Note: Called from the default {@link AccessibilityDelegate}.
4839 */
4840 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004841 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004842 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004843 event.setPackageName(getContext().getPackageName());
4844 event.setEnabled(isEnabled());
4845 event.setContentDescription(mContentDescription);
4846
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004847 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004848 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004849 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4850 FOCUSABLES_ALL);
4851 event.setItemCount(focusablesTempList.size());
4852 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4853 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004854 }
4855 }
4856
4857 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004858 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4859 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4860 * This method is responsible for obtaining an accessibility node info from a
4861 * pool of reusable instances and calling
4862 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4863 * initialize the former.
4864 * <p>
4865 * Note: The client is responsible for recycling the obtained instance by calling
4866 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4867 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004868 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004869 * @return A populated {@link AccessibilityNodeInfo}.
4870 *
4871 * @see AccessibilityNodeInfo
4872 */
4873 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004874 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4875 if (provider != null) {
4876 return provider.createAccessibilityNodeInfo(View.NO_ID);
4877 } else {
4878 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4879 onInitializeAccessibilityNodeInfo(info);
4880 return info;
4881 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004882 }
4883
4884 /**
4885 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4886 * The base implementation sets:
4887 * <ul>
4888 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004889 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4890 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004891 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4892 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4893 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4894 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4895 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4896 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4897 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4898 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4899 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4900 * </ul>
4901 * <p>
4902 * Subclasses should override this method, call the super implementation,
4903 * and set additional attributes.
4904 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004905 * <p>
4906 * If an {@link AccessibilityDelegate} has been specified via calling
4907 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4908 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4909 * is responsible for handling this call.
4910 * </p>
4911 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004912 * @param info The instance to initialize.
4913 */
4914 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004915 if (mAccessibilityDelegate != null) {
4916 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4917 } else {
4918 onInitializeAccessibilityNodeInfoInternal(info);
4919 }
4920 }
4921
4922 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004923 * Gets the location of this view in screen coordintates.
4924 *
4925 * @param outRect The output location
4926 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004927 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004928 if (mAttachInfo == null) {
4929 return;
4930 }
4931
4932 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004933 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004934
4935 if (!hasIdentityMatrix()) {
4936 getMatrix().mapRect(position);
4937 }
4938
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004939 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004940
4941 ViewParent parent = mParent;
4942 while (parent instanceof View) {
4943 View parentView = (View) parent;
4944
4945 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4946
4947 if (!parentView.hasIdentityMatrix()) {
4948 parentView.getMatrix().mapRect(position);
4949 }
4950
4951 position.offset(parentView.mLeft, parentView.mTop);
4952
4953 parent = parentView.mParent;
4954 }
4955
4956 if (parent instanceof ViewRootImpl) {
4957 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4958 position.offset(0, -viewRootImpl.mCurScrollY);
4959 }
4960
4961 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4962
4963 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4964 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4965 }
4966
4967 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004968 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4969 *
4970 * Note: Called from the default {@link AccessibilityDelegate}.
4971 */
4972 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004973 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004974
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004975 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004976 info.setBoundsInParent(bounds);
4977
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004978 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004979 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004980
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004981 ViewParent parent = getParentForAccessibility();
4982 if (parent instanceof View) {
4983 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004984 }
4985
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004986 if (mID != View.NO_ID) {
4987 View rootView = getRootView();
4988 if (rootView == null) {
4989 rootView = this;
4990 }
4991 View label = rootView.findLabelForView(this, mID);
4992 if (label != null) {
4993 info.setLabeledBy(label);
4994 }
4995 }
4996
4997 if (mLabelForId != View.NO_ID) {
4998 View rootView = getRootView();
4999 if (rootView == null) {
5000 rootView = this;
5001 }
5002 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
5003 if (labeled != null) {
5004 info.setLabelFor(labeled);
5005 }
5006 }
5007
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005008 info.setVisibleToUser(isVisibleToUser());
5009
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005010 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005011 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005012 info.setContentDescription(getContentDescription());
5013
5014 info.setEnabled(isEnabled());
5015 info.setClickable(isClickable());
5016 info.setFocusable(isFocusable());
5017 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005018 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005019 info.setSelected(isSelected());
5020 info.setLongClickable(isLongClickable());
5021
5022 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005023 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005024 // we should report as selectable view in an AdapterView.
5025 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5026 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5027
5028 if (isFocusable()) {
5029 if (isFocused()) {
5030 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5031 } else {
5032 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5033 }
5034 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005035
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005036 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005037 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005038 } else {
5039 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5040 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005041
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005042 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005043 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5044 }
5045
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005046 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005047 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5048 }
5049
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005050 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005051 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5052 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5053 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005054 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5055 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005056 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005057 }
5058
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005059 private View findLabelForView(View view, int labeledId) {
5060 if (mMatchLabelForPredicate == null) {
5061 mMatchLabelForPredicate = new MatchLabelForPredicate();
5062 }
5063 mMatchLabelForPredicate.mLabeledId = labeledId;
5064 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5065 }
5066
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005067 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005068 * Computes whether this view is visible to the user. Such a view is
5069 * attached, visible, all its predecessors are visible, it is not clipped
5070 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005071 *
5072 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005073 *
5074 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005075 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005076 protected boolean isVisibleToUser() {
5077 return isVisibleToUser(null);
5078 }
5079
5080 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005081 * Computes whether the given portion of this view is visible to the user.
5082 * Such a view is attached, visible, all its predecessors are visible,
5083 * has an alpha greater than zero, and the specified portion is not
5084 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005085 *
5086 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5087 * <code>null</code>, and the entire view will be tested in this case.
5088 * When <code>true</code> is returned by the function, the actual visible
5089 * region will be stored in this parameter; that is, if boundInView is fully
5090 * contained within the view, no modification will be made, otherwise regions
5091 * outside of the visible area of the view will be clipped.
5092 *
5093 * @return Whether the specified portion of the view is visible on the screen.
5094 *
5095 * @hide
5096 */
5097 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005098 if (mAttachInfo != null) {
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005099 // Attached to invisible window means this view is not visible.
5100 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
5101 return false;
5102 }
5103 // An invisible predecessor or one with alpha zero means
5104 // that this view is not visible to the user.
5105 Object current = this;
5106 while (current instanceof View) {
5107 View view = (View) current;
5108 // We have attach info so this view is attached and there is no
5109 // need to check whether we reach to ViewRootImpl on the way up.
5110 if (view.getAlpha() <= 0 || view.getVisibility() != VISIBLE) {
5111 return false;
5112 }
5113 current = view.mParent;
5114 }
5115 // Check if the view is entirely covered by its predecessors.
Romain Guyf0af1d52012-07-11 18:31:21 -07005116 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5117 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005118 if (!getGlobalVisibleRect(visibleRect, offset)) {
5119 return false;
Guang Zhu0d607fb2012-05-11 19:34:56 -07005120 }
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005121 // Check if the visible portion intersects the rectangle of interest.
5122 if (boundInView != null) {
5123 visibleRect.offset(-offset.x, -offset.y);
5124 return boundInView.intersect(visibleRect);
5125 }
5126 return true;
Romain Guyf0af1d52012-07-11 18:31:21 -07005127 }
Romain Guyf0af1d52012-07-11 18:31:21 -07005128 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005129 }
5130
5131 /**
alanvb72fe7a2012-08-27 16:44:25 -07005132 * Returns the delegate for implementing accessibility support via
5133 * composition. For more details see {@link AccessibilityDelegate}.
5134 *
5135 * @return The delegate, or null if none set.
5136 *
5137 * @hide
5138 */
5139 public AccessibilityDelegate getAccessibilityDelegate() {
5140 return mAccessibilityDelegate;
5141 }
5142
5143 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005144 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005145 * opposed to inheritance. The delegate's primary use is for implementing
5146 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5147 *
5148 * @param delegate The delegate instance.
5149 *
5150 * @see AccessibilityDelegate
5151 */
5152 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5153 mAccessibilityDelegate = delegate;
5154 }
5155
5156 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005157 * Gets the provider for managing a virtual view hierarchy rooted at this View
5158 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5159 * that explore the window content.
5160 * <p>
5161 * If this method returns an instance, this instance is responsible for managing
5162 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5163 * View including the one representing the View itself. Similarly the returned
5164 * instance is responsible for performing accessibility actions on any virtual
5165 * view or the root view itself.
5166 * </p>
5167 * <p>
5168 * If an {@link AccessibilityDelegate} has been specified via calling
5169 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5170 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5171 * is responsible for handling this call.
5172 * </p>
5173 *
5174 * @return The provider.
5175 *
5176 * @see AccessibilityNodeProvider
5177 */
5178 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5179 if (mAccessibilityDelegate != null) {
5180 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5181 } else {
5182 return null;
5183 }
5184 }
5185
5186 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005187 * Gets the unique identifier of this view on the screen for accessibility purposes.
5188 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5189 *
5190 * @return The view accessibility id.
5191 *
5192 * @hide
5193 */
5194 public int getAccessibilityViewId() {
5195 if (mAccessibilityViewId == NO_ID) {
5196 mAccessibilityViewId = sNextAccessibilityViewId++;
5197 }
5198 return mAccessibilityViewId;
5199 }
5200
5201 /**
5202 * Gets the unique identifier of the window in which this View reseides.
5203 *
5204 * @return The window accessibility id.
5205 *
5206 * @hide
5207 */
5208 public int getAccessibilityWindowId() {
5209 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5210 }
5211
5212 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005213 * Gets the {@link View} description. It briefly describes the view and is
5214 * primarily used for accessibility support. Set this property to enable
5215 * better accessibility support for your application. This is especially
5216 * true for views that do not have textual representation (For example,
5217 * ImageButton).
5218 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005219 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005220 *
5221 * @attr ref android.R.styleable#View_contentDescription
5222 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005223 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005224 public CharSequence getContentDescription() {
5225 return mContentDescription;
5226 }
5227
5228 /**
5229 * Sets the {@link View} description. It briefly describes the view and is
5230 * primarily used for accessibility support. Set this property to enable
5231 * better accessibility support for your application. This is especially
5232 * true for views that do not have textual representation (For example,
5233 * ImageButton).
5234 *
5235 * @param contentDescription The content description.
5236 *
5237 * @attr ref android.R.styleable#View_contentDescription
5238 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005239 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005240 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005241 if (mContentDescription == null) {
5242 if (contentDescription == null) {
5243 return;
5244 }
5245 } else if (mContentDescription.equals(contentDescription)) {
5246 return;
5247 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005248 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005249 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5250 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5251 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5252 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005253 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005254 }
5255
5256 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005257 * Gets the id of a view for which this view serves as a label for
5258 * accessibility purposes.
5259 *
5260 * @return The labeled view id.
5261 */
5262 @ViewDebug.ExportedProperty(category = "accessibility")
5263 public int getLabelFor() {
5264 return mLabelForId;
5265 }
5266
5267 /**
5268 * Sets the id of a view for which this view serves as a label for
5269 * accessibility purposes.
5270 *
5271 * @param id The labeled view id.
5272 */
5273 @RemotableViewMethod
5274 public void setLabelFor(int id) {
5275 mLabelForId = id;
5276 if (mLabelForId != View.NO_ID
5277 && mID == View.NO_ID) {
5278 mID = generateViewId();
5279 }
5280 }
5281
5282 /**
Romain Guya2431d02009-04-30 16:30:00 -07005283 * Invoked whenever this view loses focus, either by losing window focus or by losing
5284 * focus within its window. This method can be used to clear any state tied to the
5285 * focus. For instance, if a button is held pressed with the trackball and the window
5286 * loses focus, this method can be used to cancel the press.
5287 *
5288 * Subclasses of View overriding this method should always call super.onFocusLost().
5289 *
5290 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005291 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005292 *
5293 * @hide pending API council approval
5294 */
5295 protected void onFocusLost() {
5296 resetPressedState();
5297 }
5298
5299 private void resetPressedState() {
5300 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5301 return;
5302 }
5303
5304 if (isPressed()) {
5305 setPressed(false);
5306
5307 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005308 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005309 }
5310 }
5311 }
5312
5313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 * Returns true if this view has focus
5315 *
5316 * @return True if this view has focus, false otherwise.
5317 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005318 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005320 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 }
5322
5323 /**
5324 * Find the view in the hierarchy rooted at this view that currently has
5325 * focus.
5326 *
5327 * @return The view that currently has focus, or null if no focused view can
5328 * be found.
5329 */
5330 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005331 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 }
5333
5334 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005335 * Indicates whether this view is one of the set of scrollable containers in
5336 * its window.
5337 *
5338 * @return whether this view is one of the set of scrollable containers in
5339 * its window
5340 *
5341 * @attr ref android.R.styleable#View_isScrollContainer
5342 */
5343 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005344 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005345 }
5346
5347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 * Change whether this view is one of the set of scrollable containers in
5349 * its window. This will be used to determine whether the window can
5350 * resize or must pan when a soft input area is open -- scrollable
5351 * containers allow the window to use resize mode since the container
5352 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005353 *
5354 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 */
5356 public void setScrollContainer(boolean isScrollContainer) {
5357 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005358 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005360 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005362 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005364 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 mAttachInfo.mScrollContainers.remove(this);
5366 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005367 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 }
5369 }
5370
5371 /**
5372 * Returns the quality of the drawing cache.
5373 *
5374 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5375 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5376 *
5377 * @see #setDrawingCacheQuality(int)
5378 * @see #setDrawingCacheEnabled(boolean)
5379 * @see #isDrawingCacheEnabled()
5380 *
5381 * @attr ref android.R.styleable#View_drawingCacheQuality
5382 */
5383 public int getDrawingCacheQuality() {
5384 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5385 }
5386
5387 /**
5388 * Set the drawing cache quality of this view. This value is used only when the
5389 * drawing cache is enabled
5390 *
5391 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5392 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5393 *
5394 * @see #getDrawingCacheQuality()
5395 * @see #setDrawingCacheEnabled(boolean)
5396 * @see #isDrawingCacheEnabled()
5397 *
5398 * @attr ref android.R.styleable#View_drawingCacheQuality
5399 */
5400 public void setDrawingCacheQuality(int quality) {
5401 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5402 }
5403
5404 /**
5405 * Returns whether the screen should remain on, corresponding to the current
5406 * value of {@link #KEEP_SCREEN_ON}.
5407 *
5408 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5409 *
5410 * @see #setKeepScreenOn(boolean)
5411 *
5412 * @attr ref android.R.styleable#View_keepScreenOn
5413 */
5414 public boolean getKeepScreenOn() {
5415 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5416 }
5417
5418 /**
5419 * Controls whether the screen should remain on, modifying the
5420 * value of {@link #KEEP_SCREEN_ON}.
5421 *
5422 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5423 *
5424 * @see #getKeepScreenOn()
5425 *
5426 * @attr ref android.R.styleable#View_keepScreenOn
5427 */
5428 public void setKeepScreenOn(boolean keepScreenOn) {
5429 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5430 }
5431
5432 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005433 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5434 * @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 -08005435 *
5436 * @attr ref android.R.styleable#View_nextFocusLeft
5437 */
5438 public int getNextFocusLeftId() {
5439 return mNextFocusLeftId;
5440 }
5441
5442 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005443 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5444 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5445 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 *
5447 * @attr ref android.R.styleable#View_nextFocusLeft
5448 */
5449 public void setNextFocusLeftId(int nextFocusLeftId) {
5450 mNextFocusLeftId = nextFocusLeftId;
5451 }
5452
5453 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005454 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5455 * @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 -08005456 *
5457 * @attr ref android.R.styleable#View_nextFocusRight
5458 */
5459 public int getNextFocusRightId() {
5460 return mNextFocusRightId;
5461 }
5462
5463 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005464 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5465 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5466 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 *
5468 * @attr ref android.R.styleable#View_nextFocusRight
5469 */
5470 public void setNextFocusRightId(int nextFocusRightId) {
5471 mNextFocusRightId = nextFocusRightId;
5472 }
5473
5474 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005475 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5476 * @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 -08005477 *
5478 * @attr ref android.R.styleable#View_nextFocusUp
5479 */
5480 public int getNextFocusUpId() {
5481 return mNextFocusUpId;
5482 }
5483
5484 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005485 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5486 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5487 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 *
5489 * @attr ref android.R.styleable#View_nextFocusUp
5490 */
5491 public void setNextFocusUpId(int nextFocusUpId) {
5492 mNextFocusUpId = nextFocusUpId;
5493 }
5494
5495 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005496 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5497 * @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 -08005498 *
5499 * @attr ref android.R.styleable#View_nextFocusDown
5500 */
5501 public int getNextFocusDownId() {
5502 return mNextFocusDownId;
5503 }
5504
5505 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005506 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5507 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5508 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 *
5510 * @attr ref android.R.styleable#View_nextFocusDown
5511 */
5512 public void setNextFocusDownId(int nextFocusDownId) {
5513 mNextFocusDownId = nextFocusDownId;
5514 }
5515
5516 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005517 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5518 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5519 *
5520 * @attr ref android.R.styleable#View_nextFocusForward
5521 */
5522 public int getNextFocusForwardId() {
5523 return mNextFocusForwardId;
5524 }
5525
5526 /**
5527 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5528 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5529 * decide automatically.
5530 *
5531 * @attr ref android.R.styleable#View_nextFocusForward
5532 */
5533 public void setNextFocusForwardId(int nextFocusForwardId) {
5534 mNextFocusForwardId = nextFocusForwardId;
5535 }
5536
5537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 * Returns the visibility of this view and all of its ancestors
5539 *
5540 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5541 */
5542 public boolean isShown() {
5543 View current = this;
5544 //noinspection ConstantConditions
5545 do {
5546 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5547 return false;
5548 }
5549 ViewParent parent = current.mParent;
5550 if (parent == null) {
5551 return false; // We are not attached to the view root
5552 }
5553 if (!(parent instanceof View)) {
5554 return true;
5555 }
5556 current = (View) parent;
5557 } while (current != null);
5558
5559 return false;
5560 }
5561
5562 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005563 * Called by the view hierarchy when the content insets for a window have
5564 * changed, to allow it to adjust its content to fit within those windows.
5565 * The content insets tell you the space that the status bar, input method,
5566 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005568 * <p>You do not normally need to deal with this function, since the default
5569 * window decoration given to applications takes care of applying it to the
5570 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5571 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5572 * and your content can be placed under those system elements. You can then
5573 * use this method within your view hierarchy if you have parts of your UI
5574 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005576 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005577 * inset's to the view's padding, consuming that content (modifying the
5578 * insets to be 0), and returning true. This behavior is off by default, but can
5579 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5580 *
5581 * <p>This function's traversal down the hierarchy is depth-first. The same content
5582 * insets object is propagated down the hierarchy, so any changes made to it will
5583 * be seen by all following views (including potentially ones above in
5584 * the hierarchy since this is a depth-first traversal). The first view
5585 * that returns true will abort the entire traversal.
5586 *
5587 * <p>The default implementation works well for a situation where it is
5588 * used with a container that covers the entire window, allowing it to
5589 * apply the appropriate insets to its content on all edges. If you need
5590 * a more complicated layout (such as two different views fitting system
5591 * windows, one on the top of the window, and one on the bottom),
5592 * you can override the method and handle the insets however you would like.
5593 * Note that the insets provided by the framework are always relative to the
5594 * far edges of the window, not accounting for the location of the called view
5595 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005596 * where the layout will place the view, as it is done before layout happens.)
5597 *
5598 * <p>Note: unlike many View methods, there is no dispatch phase to this
5599 * call. If you are overriding it in a ViewGroup and want to allow the
5600 * call to continue to your children, you must be sure to call the super
5601 * implementation.
5602 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005603 * <p>Here is a sample layout that makes use of fitting system windows
5604 * to have controls for a video view placed inside of the window decorations
5605 * that it hides and shows. This can be used with code like the second
5606 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5607 *
5608 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5609 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005610 * @param insets Current content insets of the window. Prior to
5611 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5612 * the insets or else you and Android will be unhappy.
5613 *
5614 * @return Return true if this view applied the insets and it should not
5615 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005616 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005617 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005618 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 */
5620 protected boolean fitSystemWindows(Rect insets) {
5621 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005622 mUserPaddingStart = UNDEFINED_PADDING;
5623 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005624 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5625 || mAttachInfo == null
5626 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5627 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5628 return true;
5629 } else {
5630 internalSetPadding(0, 0, 0, 0);
5631 return false;
5632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 }
5634 return false;
5635 }
5636
5637 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005638 * Sets whether or not this view should account for system screen decorations
5639 * such as the status bar and inset its content; that is, controlling whether
5640 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5641 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005642 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005643 * <p>Note that if you are providing your own implementation of
5644 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5645 * flag to true -- your implementation will be overriding the default
5646 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005647 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005648 * @param fitSystemWindows If true, then the default implementation of
5649 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005650 *
5651 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005652 * @see #getFitsSystemWindows()
5653 * @see #fitSystemWindows(Rect)
5654 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005655 */
5656 public void setFitsSystemWindows(boolean fitSystemWindows) {
5657 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5658 }
5659
5660 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005661 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005662 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5663 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005664 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005665 * @return Returns true if the default implementation of
5666 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005667 *
5668 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005669 * @see #setFitsSystemWindows()
5670 * @see #fitSystemWindows(Rect)
5671 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005672 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005673 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005674 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5675 }
5676
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005677 /** @hide */
5678 public boolean fitsSystemWindows() {
5679 return getFitsSystemWindows();
5680 }
5681
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005682 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005683 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5684 */
5685 public void requestFitSystemWindows() {
5686 if (mParent != null) {
5687 mParent.requestFitSystemWindows();
5688 }
5689 }
5690
5691 /**
5692 * For use by PhoneWindow to make its own system window fitting optional.
5693 * @hide
5694 */
5695 public void makeOptionalFitsSystemWindows() {
5696 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5697 }
5698
5699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 * Returns the visibility status for this view.
5701 *
5702 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5703 * @attr ref android.R.styleable#View_visibility
5704 */
5705 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005706 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5707 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5708 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 })
5710 public int getVisibility() {
5711 return mViewFlags & VISIBILITY_MASK;
5712 }
5713
5714 /**
5715 * Set the enabled state of this view.
5716 *
5717 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5718 * @attr ref android.R.styleable#View_visibility
5719 */
5720 @RemotableViewMethod
5721 public void setVisibility(int visibility) {
5722 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005723 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 }
5725
5726 /**
5727 * Returns the enabled status for this view. The interpretation of the
5728 * enabled state varies by subclass.
5729 *
5730 * @return True if this view is enabled, false otherwise.
5731 */
5732 @ViewDebug.ExportedProperty
5733 public boolean isEnabled() {
5734 return (mViewFlags & ENABLED_MASK) == ENABLED;
5735 }
5736
5737 /**
5738 * Set the enabled state of this view. The interpretation of the enabled
5739 * state varies by subclass.
5740 *
5741 * @param enabled True if this view is enabled, false otherwise.
5742 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005743 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005745 if (enabled == isEnabled()) return;
5746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5748
5749 /*
5750 * The View most likely has to change its appearance, so refresh
5751 * the drawable state.
5752 */
5753 refreshDrawableState();
5754
5755 // Invalidate too, since the default behavior for views is to be
5756 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005757 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 }
5759
5760 /**
5761 * Set whether this view can receive the focus.
5762 *
5763 * Setting this to false will also ensure that this view is not focusable
5764 * in touch mode.
5765 *
5766 * @param focusable If true, this view can receive the focus.
5767 *
5768 * @see #setFocusableInTouchMode(boolean)
5769 * @attr ref android.R.styleable#View_focusable
5770 */
5771 public void setFocusable(boolean focusable) {
5772 if (!focusable) {
5773 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5774 }
5775 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5776 }
5777
5778 /**
5779 * Set whether this view can receive focus while in touch mode.
5780 *
5781 * Setting this to true will also ensure that this view is focusable.
5782 *
5783 * @param focusableInTouchMode If true, this view can receive the focus while
5784 * in touch mode.
5785 *
5786 * @see #setFocusable(boolean)
5787 * @attr ref android.R.styleable#View_focusableInTouchMode
5788 */
5789 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5790 // Focusable in touch mode should always be set before the focusable flag
5791 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5792 // which, in touch mode, will not successfully request focus on this view
5793 // because the focusable in touch mode flag is not set
5794 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5795 if (focusableInTouchMode) {
5796 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5797 }
5798 }
5799
5800 /**
5801 * Set whether this view should have sound effects enabled for events such as
5802 * clicking and touching.
5803 *
5804 * <p>You may wish to disable sound effects for a view if you already play sounds,
5805 * for instance, a dial key that plays dtmf tones.
5806 *
5807 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5808 * @see #isSoundEffectsEnabled()
5809 * @see #playSoundEffect(int)
5810 * @attr ref android.R.styleable#View_soundEffectsEnabled
5811 */
5812 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5813 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5814 }
5815
5816 /**
5817 * @return whether this view should have sound effects enabled for events such as
5818 * clicking and touching.
5819 *
5820 * @see #setSoundEffectsEnabled(boolean)
5821 * @see #playSoundEffect(int)
5822 * @attr ref android.R.styleable#View_soundEffectsEnabled
5823 */
5824 @ViewDebug.ExportedProperty
5825 public boolean isSoundEffectsEnabled() {
5826 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5827 }
5828
5829 /**
5830 * Set whether this view should have haptic feedback for events such as
5831 * long presses.
5832 *
5833 * <p>You may wish to disable haptic feedback if your view already controls
5834 * its own haptic feedback.
5835 *
5836 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5837 * @see #isHapticFeedbackEnabled()
5838 * @see #performHapticFeedback(int)
5839 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5840 */
5841 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5842 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5843 }
5844
5845 /**
5846 * @return whether this view should have haptic feedback enabled for events
5847 * long presses.
5848 *
5849 * @see #setHapticFeedbackEnabled(boolean)
5850 * @see #performHapticFeedback(int)
5851 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5852 */
5853 @ViewDebug.ExportedProperty
5854 public boolean isHapticFeedbackEnabled() {
5855 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5856 }
5857
5858 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005859 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005860 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005861 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5862 * {@link #LAYOUT_DIRECTION_RTL},
5863 * {@link #LAYOUT_DIRECTION_INHERIT} or
5864 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005865 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005866 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005867 *
5868 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005869 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005870 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005871 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5872 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5873 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5874 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005875 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005876 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005877 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005878 }
5879
5880 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005881 * Set the layout direction for this view. This will propagate a reset of layout direction
5882 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005883 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005884 * @param layoutDirection the layout direction to set. Should be one of:
5885 *
5886 * {@link #LAYOUT_DIRECTION_LTR},
5887 * {@link #LAYOUT_DIRECTION_RTL},
5888 * {@link #LAYOUT_DIRECTION_INHERIT},
5889 * {@link #LAYOUT_DIRECTION_LOCALE}.
5890 *
5891 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5892 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5893 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005894 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005895 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005896 */
5897 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005898 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005899 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005900 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005901 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005902 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005903 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005904 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005905 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005906 // We need to resolve all RTL properties as they all depend on layout direction
5907 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005908 requestLayout();
5909 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005910 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005911 }
5912
5913 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005914 * Returns the resolved layout direction for this view.
5915 *
5916 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005917 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005918 *
5919 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5920 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005921 *
5922 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005923 */
5924 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005925 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5926 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005927 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005928 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005929 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5930 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005931 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005932 return LAYOUT_DIRECTION_LTR;
5933 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005934 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5935 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005936 }
5937
5938 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005939 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5940 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005941 *
5942 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005943 *
5944 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005945 */
5946 @ViewDebug.ExportedProperty(category = "layout")
5947 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005948 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005949 }
5950
5951 /**
Adam Powell539ee872012-02-03 19:00:49 -08005952 * Indicates whether the view is currently tracking transient state that the
5953 * app should not need to concern itself with saving and restoring, but that
5954 * the framework should take special note to preserve when possible.
5955 *
Adam Powell785c4472012-05-02 21:25:39 -07005956 * <p>A view with transient state cannot be trivially rebound from an external
5957 * data source, such as an adapter binding item views in a list. This may be
5958 * because the view is performing an animation, tracking user selection
5959 * of content, or similar.</p>
5960 *
Adam Powell539ee872012-02-03 19:00:49 -08005961 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005962 */
5963 @ViewDebug.ExportedProperty(category = "layout")
5964 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005965 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005966 }
5967
5968 /**
5969 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005970 * framework should attempt to preserve when possible. This flag is reference counted,
5971 * so every call to setHasTransientState(true) should be paired with a later call
5972 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005973 *
Adam Powell785c4472012-05-02 21:25:39 -07005974 * <p>A view with transient state cannot be trivially rebound from an external
5975 * data source, such as an adapter binding item views in a list. This may be
5976 * because the view is performing an animation, tracking user selection
5977 * of content, or similar.</p>
5978 *
Adam Powell539ee872012-02-03 19:00:49 -08005979 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005980 */
5981 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005982 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5983 mTransientStateCount - 1;
5984 if (mTransientStateCount < 0) {
5985 mTransientStateCount = 0;
5986 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5987 "unmatched pair of setHasTransientState calls");
5988 }
5989 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005990 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005991 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005992 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5993 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005994 if (mParent != null) {
5995 try {
5996 mParent.childHasTransientStateChanged(this, hasTransientState);
5997 } catch (AbstractMethodError e) {
5998 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5999 " does not fully implement ViewParent", e);
6000 }
Adam Powell539ee872012-02-03 19:00:49 -08006001 }
6002 }
6003 }
6004
6005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 * If this view doesn't do any drawing on its own, set this flag to
6007 * allow further optimizations. By default, this flag is not set on
6008 * View, but could be set on some View subclasses such as ViewGroup.
6009 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006010 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
6011 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 *
6013 * @param willNotDraw whether or not this View draw on its own
6014 */
6015 public void setWillNotDraw(boolean willNotDraw) {
6016 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
6017 }
6018
6019 /**
6020 * Returns whether or not this View draws on its own.
6021 *
6022 * @return true if this view has nothing to draw, false otherwise
6023 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006024 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 public boolean willNotDraw() {
6026 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6027 }
6028
6029 /**
6030 * When a View's drawing cache is enabled, drawing is redirected to an
6031 * offscreen bitmap. Some views, like an ImageView, must be able to
6032 * bypass this mechanism if they already draw a single bitmap, to avoid
6033 * unnecessary usage of the memory.
6034 *
6035 * @param willNotCacheDrawing true if this view does not cache its
6036 * drawing, false otherwise
6037 */
6038 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6039 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6040 }
6041
6042 /**
6043 * Returns whether or not this View can cache its drawing or not.
6044 *
6045 * @return true if this view does not cache its drawing, false otherwise
6046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006047 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 public boolean willNotCacheDrawing() {
6049 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6050 }
6051
6052 /**
6053 * Indicates whether this view reacts to click events or not.
6054 *
6055 * @return true if the view is clickable, false otherwise
6056 *
6057 * @see #setClickable(boolean)
6058 * @attr ref android.R.styleable#View_clickable
6059 */
6060 @ViewDebug.ExportedProperty
6061 public boolean isClickable() {
6062 return (mViewFlags & CLICKABLE) == CLICKABLE;
6063 }
6064
6065 /**
6066 * Enables or disables click events for this view. When a view
6067 * is clickable it will change its state to "pressed" on every click.
6068 * Subclasses should set the view clickable to visually react to
6069 * user's clicks.
6070 *
6071 * @param clickable true to make the view clickable, false otherwise
6072 *
6073 * @see #isClickable()
6074 * @attr ref android.R.styleable#View_clickable
6075 */
6076 public void setClickable(boolean clickable) {
6077 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6078 }
6079
6080 /**
6081 * Indicates whether this view reacts to long click events or not.
6082 *
6083 * @return true if the view is long clickable, false otherwise
6084 *
6085 * @see #setLongClickable(boolean)
6086 * @attr ref android.R.styleable#View_longClickable
6087 */
6088 public boolean isLongClickable() {
6089 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6090 }
6091
6092 /**
6093 * Enables or disables long click events for this view. When a view is long
6094 * clickable it reacts to the user holding down the button for a longer
6095 * duration than a tap. This event can either launch the listener or a
6096 * context menu.
6097 *
6098 * @param longClickable true to make the view long clickable, false otherwise
6099 * @see #isLongClickable()
6100 * @attr ref android.R.styleable#View_longClickable
6101 */
6102 public void setLongClickable(boolean longClickable) {
6103 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6104 }
6105
6106 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006107 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 *
6109 * @see #isClickable()
6110 * @see #setClickable(boolean)
6111 *
6112 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6113 * the View's internal state from a previously set "pressed" state.
6114 */
6115 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006116 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006119 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006121 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006123
6124 if (needsRefresh) {
6125 refreshDrawableState();
6126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 dispatchSetPressed(pressed);
6128 }
6129
6130 /**
6131 * Dispatch setPressed to all of this View's children.
6132 *
6133 * @see #setPressed(boolean)
6134 *
6135 * @param pressed The new pressed state
6136 */
6137 protected void dispatchSetPressed(boolean pressed) {
6138 }
6139
6140 /**
6141 * Indicates whether the view is currently in pressed state. Unless
6142 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6143 * the pressed state.
6144 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006145 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 * @see #isClickable()
6147 * @see #setClickable(boolean)
6148 *
6149 * @return true if the view is currently pressed, false otherwise
6150 */
6151 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006152 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 }
6154
6155 /**
6156 * Indicates whether this view will save its state (that is,
6157 * whether its {@link #onSaveInstanceState} method will be called).
6158 *
6159 * @return Returns true if the view state saving is enabled, else false.
6160 *
6161 * @see #setSaveEnabled(boolean)
6162 * @attr ref android.R.styleable#View_saveEnabled
6163 */
6164 public boolean isSaveEnabled() {
6165 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6166 }
6167
6168 /**
6169 * Controls whether the saving of this view's state is
6170 * enabled (that is, whether its {@link #onSaveInstanceState} method
6171 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006172 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 * for its state to be saved. This flag can only disable the
6174 * saving of this view; any child views may still have their state saved.
6175 *
6176 * @param enabled Set to false to <em>disable</em> state saving, or true
6177 * (the default) to allow it.
6178 *
6179 * @see #isSaveEnabled()
6180 * @see #setId(int)
6181 * @see #onSaveInstanceState()
6182 * @attr ref android.R.styleable#View_saveEnabled
6183 */
6184 public void setSaveEnabled(boolean enabled) {
6185 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6186 }
6187
Jeff Brown85a31762010-09-01 17:01:00 -07006188 /**
6189 * Gets whether the framework should discard touches when the view's
6190 * window is obscured by another visible window.
6191 * Refer to the {@link View} security documentation for more details.
6192 *
6193 * @return True if touch filtering is enabled.
6194 *
6195 * @see #setFilterTouchesWhenObscured(boolean)
6196 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6197 */
6198 @ViewDebug.ExportedProperty
6199 public boolean getFilterTouchesWhenObscured() {
6200 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6201 }
6202
6203 /**
6204 * Sets whether the framework should discard touches when the view's
6205 * window is obscured by another visible window.
6206 * Refer to the {@link View} security documentation for more details.
6207 *
6208 * @param enabled True if touch filtering should be enabled.
6209 *
6210 * @see #getFilterTouchesWhenObscured
6211 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6212 */
6213 public void setFilterTouchesWhenObscured(boolean enabled) {
6214 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6215 FILTER_TOUCHES_WHEN_OBSCURED);
6216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217
6218 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006219 * Indicates whether the entire hierarchy under this view will save its
6220 * state when a state saving traversal occurs from its parent. The default
6221 * is true; if false, these views will not be saved unless
6222 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6223 *
6224 * @return Returns true if the view state saving from parent is enabled, else false.
6225 *
6226 * @see #setSaveFromParentEnabled(boolean)
6227 */
6228 public boolean isSaveFromParentEnabled() {
6229 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6230 }
6231
6232 /**
6233 * Controls whether the entire hierarchy under this view will save its
6234 * state when a state saving traversal occurs from its parent. The default
6235 * is true; if false, these views will not be saved unless
6236 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6237 *
6238 * @param enabled Set to false to <em>disable</em> state saving, or true
6239 * (the default) to allow it.
6240 *
6241 * @see #isSaveFromParentEnabled()
6242 * @see #setId(int)
6243 * @see #onSaveInstanceState()
6244 */
6245 public void setSaveFromParentEnabled(boolean enabled) {
6246 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6247 }
6248
6249
6250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 * Returns whether this View is able to take focus.
6252 *
6253 * @return True if this view can take focus, or false otherwise.
6254 * @attr ref android.R.styleable#View_focusable
6255 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006256 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 public final boolean isFocusable() {
6258 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6259 }
6260
6261 /**
6262 * When a view is focusable, it may not want to take focus when in touch mode.
6263 * For example, a button would like focus when the user is navigating via a D-pad
6264 * so that the user can click on it, but once the user starts touching the screen,
6265 * the button shouldn't take focus
6266 * @return Whether the view is focusable in touch mode.
6267 * @attr ref android.R.styleable#View_focusableInTouchMode
6268 */
6269 @ViewDebug.ExportedProperty
6270 public final boolean isFocusableInTouchMode() {
6271 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6272 }
6273
6274 /**
6275 * Find the nearest view in the specified direction that can take focus.
6276 * This does not actually give focus to that view.
6277 *
6278 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6279 *
6280 * @return The nearest focusable in the specified direction, or null if none
6281 * can be found.
6282 */
6283 public View focusSearch(int direction) {
6284 if (mParent != null) {
6285 return mParent.focusSearch(this, direction);
6286 } else {
6287 return null;
6288 }
6289 }
6290
6291 /**
6292 * This method is the last chance for the focused view and its ancestors to
6293 * respond to an arrow key. This is called when the focused view did not
6294 * consume the key internally, nor could the view system find a new view in
6295 * the requested direction to give focus to.
6296 *
6297 * @param focused The currently focused view.
6298 * @param direction The direction focus wants to move. One of FOCUS_UP,
6299 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6300 * @return True if the this view consumed this unhandled move.
6301 */
6302 public boolean dispatchUnhandledMove(View focused, int direction) {
6303 return false;
6304 }
6305
6306 /**
6307 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006308 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006310 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6311 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 * @return The user specified next view, or null if there is none.
6313 */
6314 View findUserSetNextFocus(View root, int direction) {
6315 switch (direction) {
6316 case FOCUS_LEFT:
6317 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006318 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 case FOCUS_RIGHT:
6320 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006321 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 case FOCUS_UP:
6323 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006324 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 case FOCUS_DOWN:
6326 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006327 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006328 case FOCUS_FORWARD:
6329 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006330 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006331 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006332 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006333 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006334 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006335 @Override
6336 public boolean apply(View t) {
6337 return t.mNextFocusForwardId == id;
6338 }
6339 });
6340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 }
6342 return null;
6343 }
6344
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006345 private View findViewInsideOutShouldExist(View root, int id) {
6346 if (mMatchIdPredicate == null) {
6347 mMatchIdPredicate = new MatchIdPredicate();
6348 }
6349 mMatchIdPredicate.mId = id;
6350 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006352 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 }
6354 return result;
6355 }
6356
6357 /**
6358 * Find and return all focusable views that are descendants of this view,
6359 * possibly including this view if it is focusable itself.
6360 *
6361 * @param direction The direction of the focus
6362 * @return A list of focusable views
6363 */
6364 public ArrayList<View> getFocusables(int direction) {
6365 ArrayList<View> result = new ArrayList<View>(24);
6366 addFocusables(result, direction);
6367 return result;
6368 }
6369
6370 /**
6371 * Add any focusable views that are descendants of this view (possibly
6372 * including this view if it is focusable itself) to views. If we are in touch mode,
6373 * only add views that are also focusable in touch mode.
6374 *
6375 * @param views Focusable views found so far
6376 * @param direction The direction of the focus
6377 */
6378 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006379 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381
svetoslavganov75986cf2009-05-14 22:28:01 -07006382 /**
6383 * Adds any focusable views that are descendants of this view (possibly
6384 * including this view if it is focusable itself) to views. This method
6385 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006386 * only views focusable in touch mode if we are in touch mode or
6387 * only views that can take accessibility focus if accessibility is enabeld
6388 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006389 *
6390 * @param views Focusable views found so far or null if all we are interested is
6391 * the number of focusables.
6392 * @param direction The direction of the focus.
6393 * @param focusableMode The type of focusables to be added.
6394 *
6395 * @see #FOCUSABLES_ALL
6396 * @see #FOCUSABLES_TOUCH_MODE
6397 */
6398 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006399 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006400 return;
6401 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006402 if (!isFocusable()) {
6403 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006404 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006405 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6406 && isInTouchMode() && !isFocusableInTouchMode()) {
6407 return;
6408 }
6409 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 }
6411
6412 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006413 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006414 * The search is performed by either the text that the View renders or the content
6415 * description that describes the view for accessibility purposes and the view does
6416 * not render or both. Clients can specify how the search is to be performed via
6417 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6418 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006419 *
6420 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006421 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006422 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006423 * @see #FIND_VIEWS_WITH_TEXT
6424 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6425 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006426 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006427 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006428 if (getAccessibilityNodeProvider() != null) {
6429 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6430 outViews.add(this);
6431 }
6432 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006433 && (searched != null && searched.length() > 0)
6434 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006435 String searchedLowerCase = searched.toString().toLowerCase();
6436 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6437 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6438 outViews.add(this);
6439 }
6440 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006441 }
6442
6443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 * Find and return all touchable views that are descendants of this view,
6445 * possibly including this view if it is touchable itself.
6446 *
6447 * @return A list of touchable views
6448 */
6449 public ArrayList<View> getTouchables() {
6450 ArrayList<View> result = new ArrayList<View>();
6451 addTouchables(result);
6452 return result;
6453 }
6454
6455 /**
6456 * Add any touchable views that are descendants of this view (possibly
6457 * including this view if it is touchable itself) to views.
6458 *
6459 * @param views Touchable views found so far
6460 */
6461 public void addTouchables(ArrayList<View> views) {
6462 final int viewFlags = mViewFlags;
6463
6464 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6465 && (viewFlags & ENABLED_MASK) == ENABLED) {
6466 views.add(this);
6467 }
6468 }
6469
6470 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006471 * Returns whether this View is accessibility focused.
6472 *
6473 * @return True if this View is accessibility focused.
6474 */
6475 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006476 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006477 }
6478
6479 /**
6480 * Call this to try to give accessibility focus to this view.
6481 *
6482 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6483 * returns false or the view is no visible or the view already has accessibility
6484 * focus.
6485 *
6486 * See also {@link #focusSearch(int)}, which is what you call to say that you
6487 * have focus, and you want your parent to look for the next one.
6488 *
6489 * @return Whether this view actually took accessibility focus.
6490 *
6491 * @hide
6492 */
6493 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006494 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6495 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006496 return false;
6497 }
6498 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6499 return false;
6500 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006501 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6502 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006503 ViewRootImpl viewRootImpl = getViewRootImpl();
6504 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006505 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006506 }
6507 invalidate();
6508 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6509 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006510 return true;
6511 }
6512 return false;
6513 }
6514
6515 /**
6516 * Call this to try to clear accessibility focus of this view.
6517 *
6518 * See also {@link #focusSearch(int)}, which is what you call to say that you
6519 * have focus, and you want your parent to look for the next one.
6520 *
6521 * @hide
6522 */
6523 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006524 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6525 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006526 invalidate();
6527 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6528 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006529 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006530 // Clear the global reference of accessibility focus if this
6531 // view or any of its descendants had accessibility focus.
6532 ViewRootImpl viewRootImpl = getViewRootImpl();
6533 if (viewRootImpl != null) {
6534 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6535 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006536 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006537 }
6538 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006539 }
6540
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006541 private void sendAccessibilityHoverEvent(int eventType) {
6542 // Since we are not delivering to a client accessibility events from not
6543 // important views (unless the clinet request that) we need to fire the
6544 // event from the deepest view exposed to the client. As a consequence if
6545 // the user crosses a not exposed view the client will see enter and exit
6546 // of the exposed predecessor followed by and enter and exit of that same
6547 // predecessor when entering and exiting the not exposed descendant. This
6548 // is fine since the client has a clear idea which view is hovered at the
6549 // price of a couple more events being sent. This is a simple and
6550 // working solution.
6551 View source = this;
6552 while (true) {
6553 if (source.includeForAccessibility()) {
6554 source.sendAccessibilityEvent(eventType);
6555 return;
6556 }
6557 ViewParent parent = source.getParent();
6558 if (parent instanceof View) {
6559 source = (View) parent;
6560 } else {
6561 return;
6562 }
6563 }
6564 }
6565
Svetoslav Ganov42138042012-03-20 11:51:39 -07006566 /**
6567 * Clears accessibility focus without calling any callback methods
6568 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6569 * is used for clearing accessibility focus when giving this focus to
6570 * another view.
6571 */
6572 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006573 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6574 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006575 invalidate();
6576 }
6577 }
6578
6579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 * Call this to try to give focus to a specific view or to one of its
6581 * descendants.
6582 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006583 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6584 * false), or if it is focusable and it is not focusable in touch mode
6585 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006587 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 * have focus, and you want your parent to look for the next one.
6589 *
6590 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6591 * {@link #FOCUS_DOWN} and <code>null</code>.
6592 *
6593 * @return Whether this view or one of its descendants actually took focus.
6594 */
6595 public final boolean requestFocus() {
6596 return requestFocus(View.FOCUS_DOWN);
6597 }
6598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 /**
6600 * Call this to try to give focus to a specific view or to one of its
6601 * descendants and give it a hint about what direction focus is heading.
6602 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006603 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6604 * false), or if it is focusable and it is not focusable in touch mode
6605 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006607 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 * have focus, and you want your parent to look for the next one.
6609 *
6610 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6611 * <code>null</code> set for the previously focused rectangle.
6612 *
6613 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6614 * @return Whether this view or one of its descendants actually took focus.
6615 */
6616 public final boolean requestFocus(int direction) {
6617 return requestFocus(direction, null);
6618 }
6619
6620 /**
6621 * Call this to try to give focus to a specific view or to one of its descendants
6622 * and give it hints about the direction and a specific rectangle that the focus
6623 * is coming from. The rectangle can help give larger views a finer grained hint
6624 * about where focus is coming from, and therefore, where to show selection, or
6625 * forward focus change internally.
6626 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006627 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6628 * false), or if it is focusable and it is not focusable in touch mode
6629 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 *
6631 * A View will not take focus if it is not visible.
6632 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006633 * A View will not take focus if one of its parents has
6634 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6635 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006637 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 * have focus, and you want your parent to look for the next one.
6639 *
6640 * You may wish to override this method if your custom {@link View} has an internal
6641 * {@link View} that it wishes to forward the request to.
6642 *
6643 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6644 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6645 * to give a finer grained hint about where focus is coming from. May be null
6646 * if there is no hint.
6647 * @return Whether this view or one of its descendants actually took focus.
6648 */
6649 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006650 return requestFocusNoSearch(direction, previouslyFocusedRect);
6651 }
6652
6653 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654 // need to be focusable
6655 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6656 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6657 return false;
6658 }
6659
6660 // need to be focusable in touch mode if in touch mode
6661 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006662 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6663 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 }
6665
6666 // need to not have any parents blocking us
6667 if (hasAncestorThatBlocksDescendantFocus()) {
6668 return false;
6669 }
6670
6671 handleFocusGainInternal(direction, previouslyFocusedRect);
6672 return true;
6673 }
6674
6675 /**
6676 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6677 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6678 * touch mode to request focus when they are touched.
6679 *
6680 * @return Whether this view or one of its descendants actually took focus.
6681 *
6682 * @see #isInTouchMode()
6683 *
6684 */
6685 public final boolean requestFocusFromTouch() {
6686 // Leave touch mode if we need to
6687 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006688 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006689 if (viewRoot != null) {
6690 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006691 }
6692 }
6693 return requestFocus(View.FOCUS_DOWN);
6694 }
6695
6696 /**
6697 * @return Whether any ancestor of this view blocks descendant focus.
6698 */
6699 private boolean hasAncestorThatBlocksDescendantFocus() {
6700 ViewParent ancestor = mParent;
6701 while (ancestor instanceof ViewGroup) {
6702 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6703 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6704 return true;
6705 } else {
6706 ancestor = vgAncestor.getParent();
6707 }
6708 }
6709 return false;
6710 }
6711
6712 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006713 * Gets the mode for determining whether this View is important for accessibility
6714 * which is if it fires accessibility events and if it is reported to
6715 * accessibility services that query the screen.
6716 *
6717 * @return The mode for determining whether a View is important for accessibility.
6718 *
6719 * @attr ref android.R.styleable#View_importantForAccessibility
6720 *
6721 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6722 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6723 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6724 */
6725 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006726 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6727 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6728 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006729 })
6730 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006731 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6732 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006733 }
6734
6735 /**
6736 * Sets how to determine whether this view is important for accessibility
6737 * which is if it fires accessibility events and if it is reported to
6738 * accessibility services that query the screen.
6739 *
6740 * @param mode How to determine whether this view is important for accessibility.
6741 *
6742 * @attr ref android.R.styleable#View_importantForAccessibility
6743 *
6744 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6745 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6746 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6747 */
6748 public void setImportantForAccessibility(int mode) {
6749 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006750 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6751 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6752 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006753 notifyAccessibilityStateChanged();
6754 }
6755 }
6756
6757 /**
6758 * Gets whether this view should be exposed for accessibility.
6759 *
6760 * @return Whether the view is exposed for accessibility.
6761 *
6762 * @hide
6763 */
6764 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006765 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6766 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006767 switch (mode) {
6768 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6769 return true;
6770 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6771 return false;
6772 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006773 return isActionableForAccessibility() || hasListenersForAccessibility()
6774 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006775 default:
6776 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6777 + mode);
6778 }
6779 }
6780
6781 /**
6782 * Gets the parent for accessibility purposes. Note that the parent for
6783 * accessibility is not necessary the immediate parent. It is the first
6784 * predecessor that is important for accessibility.
6785 *
6786 * @return The parent for accessibility purposes.
6787 */
6788 public ViewParent getParentForAccessibility() {
6789 if (mParent instanceof View) {
6790 View parentView = (View) mParent;
6791 if (parentView.includeForAccessibility()) {
6792 return mParent;
6793 } else {
6794 return mParent.getParentForAccessibility();
6795 }
6796 }
6797 return null;
6798 }
6799
6800 /**
6801 * Adds the children of a given View for accessibility. Since some Views are
6802 * not important for accessibility the children for accessibility are not
6803 * necessarily direct children of the riew, rather they are the first level of
6804 * descendants important for accessibility.
6805 *
6806 * @param children The list of children for accessibility.
6807 */
6808 public void addChildrenForAccessibility(ArrayList<View> children) {
6809 if (includeForAccessibility()) {
6810 children.add(this);
6811 }
6812 }
6813
6814 /**
6815 * Whether to regard this view for accessibility. A view is regarded for
6816 * accessibility if it is important for accessibility or the querying
6817 * accessibility service has explicitly requested that view not
6818 * important for accessibility are regarded.
6819 *
6820 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006821 *
6822 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006823 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006824 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006825 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006826 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006827 }
6828 return false;
6829 }
6830
6831 /**
6832 * Returns whether the View is considered actionable from
6833 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006834 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006835 *
6836 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006837 *
6838 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006839 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006840 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006841 return (isClickable() || isLongClickable() || isFocusable());
6842 }
6843
6844 /**
6845 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006846 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006847 *
6848 * @return True if the view is actionable for accessibility.
6849 */
6850 private boolean hasListenersForAccessibility() {
6851 ListenerInfo info = getListenerInfo();
6852 return mTouchDelegate != null || info.mOnKeyListener != null
6853 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6854 || info.mOnHoverListener != null || info.mOnDragListener != null;
6855 }
6856
6857 /**
6858 * Notifies accessibility services that some view's important for
6859 * accessibility state has changed. Note that such notifications
6860 * are made at most once every
6861 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6862 * to avoid unnecessary load to the system. Also once a view has
6863 * made a notifucation this method is a NOP until the notification has
6864 * been sent to clients.
6865 *
6866 * @hide
6867 *
6868 * TODO: Makse sure this method is called for any view state change
6869 * that is interesting for accessilility purposes.
6870 */
6871 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006872 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6873 return;
6874 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006875 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6876 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006877 if (mParent != null) {
6878 mParent.childAccessibilityStateChanged(this);
6879 }
6880 }
6881 }
6882
6883 /**
6884 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006885 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006886 *
6887 * @hide
6888 */
6889 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006890 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006891 }
6892
6893 /**
6894 * Performs the specified accessibility action on the view. For
6895 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006896 * <p>
6897 * If an {@link AccessibilityDelegate} has been specified via calling
6898 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6899 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6900 * is responsible for handling this call.
6901 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006902 *
6903 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006904 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006905 * @return Whether the action was performed.
6906 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006907 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006908 if (mAccessibilityDelegate != null) {
6909 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6910 } else {
6911 return performAccessibilityActionInternal(action, arguments);
6912 }
6913 }
6914
6915 /**
6916 * @see #performAccessibilityAction(int, Bundle)
6917 *
6918 * Note: Called from the default {@link AccessibilityDelegate}.
6919 */
6920 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006921 switch (action) {
6922 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006923 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006924 performClick();
6925 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006926 }
6927 } break;
6928 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6929 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006930 performLongClick();
6931 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006932 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006933 } break;
6934 case AccessibilityNodeInfo.ACTION_FOCUS: {
6935 if (!hasFocus()) {
6936 // Get out of touch mode since accessibility
6937 // wants to move focus around.
6938 getViewRootImpl().ensureTouchMode(false);
6939 return requestFocus();
6940 }
6941 } break;
6942 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6943 if (hasFocus()) {
6944 clearFocus();
6945 return !isFocused();
6946 }
6947 } break;
6948 case AccessibilityNodeInfo.ACTION_SELECT: {
6949 if (!isSelected()) {
6950 setSelected(true);
6951 return isSelected();
6952 }
6953 } break;
6954 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6955 if (isSelected()) {
6956 setSelected(false);
6957 return !isSelected();
6958 }
6959 } break;
6960 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006961 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006962 return requestAccessibilityFocus();
6963 }
6964 } break;
6965 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6966 if (isAccessibilityFocused()) {
6967 clearAccessibilityFocus();
6968 return true;
6969 }
6970 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006971 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6972 if (arguments != null) {
6973 final int granularity = arguments.getInt(
6974 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6975 return nextAtGranularity(granularity);
6976 }
6977 } break;
6978 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6979 if (arguments != null) {
6980 final int granularity = arguments.getInt(
6981 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6982 return previousAtGranularity(granularity);
6983 }
6984 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006985 }
6986 return false;
6987 }
6988
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006989 private boolean nextAtGranularity(int granularity) {
6990 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006991 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006992 return false;
6993 }
6994 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6995 if (iterator == null) {
6996 return false;
6997 }
6998 final int current = getAccessibilityCursorPosition();
6999 final int[] range = iterator.following(current);
7000 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007001 return false;
7002 }
7003 final int start = range[0];
7004 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007005 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007006 sendViewTextTraversedAtGranularityEvent(
7007 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
7008 granularity, start, end);
7009 return true;
7010 }
7011
7012 private boolean previousAtGranularity(int granularity) {
7013 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007014 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007015 return false;
7016 }
7017 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7018 if (iterator == null) {
7019 return false;
7020 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007021 int current = getAccessibilityCursorPosition();
7022 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7023 current = text.length();
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007024 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007025 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7026 // When traversing by character we always put the cursor after the character
7027 // to ease edit and have to compensate before asking the for previous segment.
7028 current--;
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007029 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007030 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007031 final int[] range = iterator.preceding(current);
7032 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007033 return false;
7034 }
7035 final int start = range[0];
7036 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007037 // Always put the cursor after the character to ease edit.
7038 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7039 setAccessibilityCursorPosition(end);
7040 } else {
7041 setAccessibilityCursorPosition(start);
7042 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007043 sendViewTextTraversedAtGranularityEvent(
7044 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7045 granularity, start, end);
7046 return true;
7047 }
7048
7049 /**
7050 * Gets the text reported for accessibility purposes.
7051 *
7052 * @return The accessibility text.
7053 *
7054 * @hide
7055 */
7056 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007057 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007058 }
7059
7060 /**
7061 * @hide
7062 */
7063 public int getAccessibilityCursorPosition() {
7064 return mAccessibilityCursorPosition;
7065 }
7066
7067 /**
7068 * @hide
7069 */
7070 public void setAccessibilityCursorPosition(int position) {
7071 mAccessibilityCursorPosition = position;
7072 }
7073
7074 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7075 int fromIndex, int toIndex) {
7076 if (mParent == null) {
7077 return;
7078 }
7079 AccessibilityEvent event = AccessibilityEvent.obtain(
7080 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7081 onInitializeAccessibilityEvent(event);
7082 onPopulateAccessibilityEvent(event);
7083 event.setFromIndex(fromIndex);
7084 event.setToIndex(toIndex);
7085 event.setAction(action);
7086 event.setMovementGranularity(granularity);
7087 mParent.requestSendAccessibilityEvent(this, event);
7088 }
7089
7090 /**
7091 * @hide
7092 */
7093 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7094 switch (granularity) {
7095 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7096 CharSequence text = getIterableTextForAccessibility();
7097 if (text != null && text.length() > 0) {
7098 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007099 CharacterTextSegmentIterator.getInstance(
7100 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007101 iterator.initialize(text.toString());
7102 return iterator;
7103 }
7104 } break;
7105 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7106 CharSequence text = getIterableTextForAccessibility();
7107 if (text != null && text.length() > 0) {
7108 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007109 WordTextSegmentIterator.getInstance(
7110 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007111 iterator.initialize(text.toString());
7112 return iterator;
7113 }
7114 } break;
7115 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7116 CharSequence text = getIterableTextForAccessibility();
7117 if (text != null && text.length() > 0) {
7118 ParagraphTextSegmentIterator iterator =
7119 ParagraphTextSegmentIterator.getInstance();
7120 iterator.initialize(text.toString());
7121 return iterator;
7122 }
7123 } break;
7124 }
7125 return null;
7126 }
7127
Svetoslav Ganov42138042012-03-20 11:51:39 -07007128 /**
Romain Guya440b002010-02-24 15:57:54 -08007129 * @hide
7130 */
7131 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007132 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007133 clearDisplayList();
7134
Romain Guya440b002010-02-24 15:57:54 -08007135 onStartTemporaryDetach();
7136 }
7137
7138 /**
7139 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7141 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007142 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 */
7144 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007145 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007146 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007147 }
7148
7149 /**
7150 * @hide
7151 */
7152 public void dispatchFinishTemporaryDetach() {
7153 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 }
Romain Guy8506ab42009-06-11 17:35:47 -07007155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 /**
7157 * Called after {@link #onStartTemporaryDetach} when the container is done
7158 * changing the view.
7159 */
7160 public void onFinishTemporaryDetach() {
7161 }
Romain Guy8506ab42009-06-11 17:35:47 -07007162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007163 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007164 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7165 * for this view's window. Returns null if the view is not currently attached
7166 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007167 * just use the standard high-level event callbacks like
7168 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007169 */
7170 public KeyEvent.DispatcherState getKeyDispatcherState() {
7171 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7172 }
Joe Malin32736f02011-01-19 16:14:20 -08007173
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 * Dispatch a key event before it is processed by any input method
7176 * associated with the view hierarchy. This can be used to intercept
7177 * key events in special situations before the IME consumes them; a
7178 * typical example would be handling the BACK key to update the application's
7179 * UI instead of allowing the IME to see it and close itself.
7180 *
7181 * @param event The key event to be dispatched.
7182 * @return True if the event was handled, false otherwise.
7183 */
7184 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7185 return onKeyPreIme(event.getKeyCode(), event);
7186 }
7187
7188 /**
7189 * Dispatch a key event to the next view on the focus path. This path runs
7190 * from the top of the view tree down to the currently focused view. If this
7191 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7192 * the next node down the focus path. This method also fires any key
7193 * listeners.
7194 *
7195 * @param event The key event to be dispatched.
7196 * @return True if the event was handled, false otherwise.
7197 */
7198 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007199 if (mInputEventConsistencyVerifier != null) {
7200 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202
Jeff Brown21bc5c92011-02-28 18:27:14 -08007203 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007204 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007205 ListenerInfo li = mListenerInfo;
7206 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7207 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 return true;
7209 }
7210
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007211 if (event.dispatch(this, mAttachInfo != null
7212 ? mAttachInfo.mKeyDispatchState : null, this)) {
7213 return true;
7214 }
7215
7216 if (mInputEventConsistencyVerifier != null) {
7217 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7218 }
7219 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 }
7221
7222 /**
7223 * Dispatches a key shortcut event.
7224 *
7225 * @param event The key event to be dispatched.
7226 * @return True if the event was handled by the view, false otherwise.
7227 */
7228 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7229 return onKeyShortcut(event.getKeyCode(), event);
7230 }
7231
7232 /**
7233 * Pass the touch screen motion event down to the target view, or this
7234 * view if it is the target.
7235 *
7236 * @param event The motion event to be dispatched.
7237 * @return True if the event was handled by the view, false otherwise.
7238 */
7239 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007240 if (mInputEventConsistencyVerifier != null) {
7241 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7242 }
7243
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007244 if (onFilterTouchEventForSecurity(event)) {
7245 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007246 ListenerInfo li = mListenerInfo;
7247 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7248 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007249 return true;
7250 }
7251
7252 if (onTouchEvent(event)) {
7253 return true;
7254 }
Jeff Brown85a31762010-09-01 17:01:00 -07007255 }
7256
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007257 if (mInputEventConsistencyVerifier != null) {
7258 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007260 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 }
7262
7263 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007264 * Filter the touch event to apply security policies.
7265 *
7266 * @param event The motion event to be filtered.
7267 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007268 *
Jeff Brown85a31762010-09-01 17:01:00 -07007269 * @see #getFilterTouchesWhenObscured
7270 */
7271 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007272 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007273 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7274 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7275 // Window is obscured, drop this touch.
7276 return false;
7277 }
7278 return true;
7279 }
7280
7281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 * Pass a trackball motion event down to the focused view.
7283 *
7284 * @param event The motion event to be dispatched.
7285 * @return True if the event was handled by the view, false otherwise.
7286 */
7287 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007288 if (mInputEventConsistencyVerifier != null) {
7289 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7290 }
7291
Romain Guy02ccac62011-06-24 13:20:23 -07007292 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 }
7294
7295 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007296 * Dispatch a generic motion event.
7297 * <p>
7298 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7299 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007300 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007301 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007302 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007303 *
7304 * @param event The motion event to be dispatched.
7305 * @return True if the event was handled by the view, false otherwise.
7306 */
7307 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007308 if (mInputEventConsistencyVerifier != null) {
7309 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7310 }
7311
Jeff Browna032cc02011-03-07 16:56:21 -08007312 final int source = event.getSource();
7313 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7314 final int action = event.getAction();
7315 if (action == MotionEvent.ACTION_HOVER_ENTER
7316 || action == MotionEvent.ACTION_HOVER_MOVE
7317 || action == MotionEvent.ACTION_HOVER_EXIT) {
7318 if (dispatchHoverEvent(event)) {
7319 return true;
7320 }
7321 } else if (dispatchGenericPointerEvent(event)) {
7322 return true;
7323 }
7324 } else if (dispatchGenericFocusedEvent(event)) {
7325 return true;
7326 }
7327
Jeff Brown10b62902011-06-20 16:40:37 -07007328 if (dispatchGenericMotionEventInternal(event)) {
7329 return true;
7330 }
7331
7332 if (mInputEventConsistencyVerifier != null) {
7333 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7334 }
7335 return false;
7336 }
7337
7338 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007339 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007340 ListenerInfo li = mListenerInfo;
7341 if (li != null && li.mOnGenericMotionListener != null
7342 && (mViewFlags & ENABLED_MASK) == ENABLED
7343 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007344 return true;
7345 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007346
7347 if (onGenericMotionEvent(event)) {
7348 return true;
7349 }
7350
7351 if (mInputEventConsistencyVerifier != null) {
7352 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7353 }
7354 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007355 }
7356
7357 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007358 * Dispatch a hover event.
7359 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007360 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007361 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007362 * </p>
7363 *
7364 * @param event The motion event to be dispatched.
7365 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007366 */
7367 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007368 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007369 ListenerInfo li = mListenerInfo;
7370 if (li != null && li.mOnHoverListener != null
7371 && (mViewFlags & ENABLED_MASK) == ENABLED
7372 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007373 return true;
7374 }
7375
Jeff Browna032cc02011-03-07 16:56:21 -08007376 return onHoverEvent(event);
7377 }
7378
7379 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007380 * Returns true if the view has a child to which it has recently sent
7381 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7382 * it does not have a hovered child, then it must be the innermost hovered view.
7383 * @hide
7384 */
7385 protected boolean hasHoveredChild() {
7386 return false;
7387 }
7388
7389 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007390 * Dispatch a generic motion event to the view under the first pointer.
7391 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007392 * Do not call this method directly.
7393 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007394 * </p>
7395 *
7396 * @param event The motion event to be dispatched.
7397 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007398 */
7399 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7400 return false;
7401 }
7402
7403 /**
7404 * Dispatch a generic motion event to the currently focused view.
7405 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007406 * Do not call this method directly.
7407 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007408 * </p>
7409 *
7410 * @param event The motion event to be dispatched.
7411 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007412 */
7413 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7414 return false;
7415 }
7416
7417 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007418 * Dispatch a pointer event.
7419 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007420 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7421 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7422 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007423 * and should not be expected to handle other pointing device features.
7424 * </p>
7425 *
7426 * @param event The motion event to be dispatched.
7427 * @return True if the event was handled by the view, false otherwise.
7428 * @hide
7429 */
7430 public final boolean dispatchPointerEvent(MotionEvent event) {
7431 if (event.isTouchEvent()) {
7432 return dispatchTouchEvent(event);
7433 } else {
7434 return dispatchGenericMotionEvent(event);
7435 }
7436 }
7437
7438 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007439 * Called when the window containing this view gains or loses window focus.
7440 * ViewGroups should override to route to their children.
7441 *
7442 * @param hasFocus True if the window containing this view now has focus,
7443 * false otherwise.
7444 */
7445 public void dispatchWindowFocusChanged(boolean hasFocus) {
7446 onWindowFocusChanged(hasFocus);
7447 }
7448
7449 /**
7450 * Called when the window containing this view gains or loses focus. Note
7451 * that this is separate from view focus: to receive key events, both
7452 * your view and its window must have focus. If a window is displayed
7453 * on top of yours that takes input focus, then your own window will lose
7454 * focus but the view focus will remain unchanged.
7455 *
7456 * @param hasWindowFocus True if the window containing this view now has
7457 * focus, false otherwise.
7458 */
7459 public void onWindowFocusChanged(boolean hasWindowFocus) {
7460 InputMethodManager imm = InputMethodManager.peekInstance();
7461 if (!hasWindowFocus) {
7462 if (isPressed()) {
7463 setPressed(false);
7464 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007465 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 imm.focusOut(this);
7467 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007468 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007469 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007470 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007471 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 imm.focusIn(this);
7473 }
7474 refreshDrawableState();
7475 }
7476
7477 /**
7478 * Returns true if this view is in a window that currently has window focus.
7479 * Note that this is not the same as the view itself having focus.
7480 *
7481 * @return True if this view is in a window that currently has window focus.
7482 */
7483 public boolean hasWindowFocus() {
7484 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7485 }
7486
7487 /**
Adam Powell326d8082009-12-09 15:10:07 -08007488 * Dispatch a view visibility change down the view hierarchy.
7489 * ViewGroups should override to route to their children.
7490 * @param changedView The view whose visibility changed. Could be 'this' or
7491 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007492 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7493 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007494 */
7495 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7496 onVisibilityChanged(changedView, visibility);
7497 }
7498
7499 /**
7500 * Called when the visibility of the view or an ancestor of the view is changed.
7501 * @param changedView The view whose visibility changed. Could be 'this' or
7502 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007503 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7504 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007505 */
7506 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007507 if (visibility == VISIBLE) {
7508 if (mAttachInfo != null) {
7509 initialAwakenScrollBars();
7510 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007511 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007512 }
7513 }
Adam Powell326d8082009-12-09 15:10:07 -08007514 }
7515
7516 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007517 * Dispatch a hint about whether this view is displayed. For instance, when
7518 * a View moves out of the screen, it might receives a display hint indicating
7519 * the view is not displayed. Applications should not <em>rely</em> on this hint
7520 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007521 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007522 * @param hint A hint about whether or not this view is displayed:
7523 * {@link #VISIBLE} or {@link #INVISIBLE}.
7524 */
7525 public void dispatchDisplayHint(int hint) {
7526 onDisplayHint(hint);
7527 }
7528
7529 /**
7530 * Gives this view a hint about whether is displayed or not. For instance, when
7531 * a View moves out of the screen, it might receives a display hint indicating
7532 * the view is not displayed. Applications should not <em>rely</em> on this hint
7533 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007534 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007535 * @param hint A hint about whether or not this view is displayed:
7536 * {@link #VISIBLE} or {@link #INVISIBLE}.
7537 */
7538 protected void onDisplayHint(int hint) {
7539 }
7540
7541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 * Dispatch a window visibility change down the view hierarchy.
7543 * ViewGroups should override to route to their children.
7544 *
7545 * @param visibility The new visibility of the window.
7546 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007547 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 */
7549 public void dispatchWindowVisibilityChanged(int visibility) {
7550 onWindowVisibilityChanged(visibility);
7551 }
7552
7553 /**
7554 * Called when the window containing has change its visibility
7555 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7556 * that this tells you whether or not your window is being made visible
7557 * to the window manager; this does <em>not</em> tell you whether or not
7558 * your window is obscured by other windows on the screen, even if it
7559 * is itself visible.
7560 *
7561 * @param visibility The new visibility of the window.
7562 */
7563 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007564 if (visibility == VISIBLE) {
7565 initialAwakenScrollBars();
7566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 }
7568
7569 /**
7570 * Returns the current visibility of the window this view is attached to
7571 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7572 *
7573 * @return Returns the current visibility of the view's window.
7574 */
7575 public int getWindowVisibility() {
7576 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7577 }
7578
7579 /**
7580 * Retrieve the overall visible display size in which the window this view is
7581 * attached to has been positioned in. This takes into account screen
7582 * decorations above the window, for both cases where the window itself
7583 * is being position inside of them or the window is being placed under
7584 * then and covered insets are used for the window to position its content
7585 * inside. In effect, this tells you the available area where content can
7586 * be placed and remain visible to users.
7587 *
7588 * <p>This function requires an IPC back to the window manager to retrieve
7589 * the requested information, so should not be used in performance critical
7590 * code like drawing.
7591 *
7592 * @param outRect Filled in with the visible display frame. If the view
7593 * is not attached to a window, this is simply the raw display size.
7594 */
7595 public void getWindowVisibleDisplayFrame(Rect outRect) {
7596 if (mAttachInfo != null) {
7597 try {
7598 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7599 } catch (RemoteException e) {
7600 return;
7601 }
7602 // XXX This is really broken, and probably all needs to be done
7603 // in the window manager, and we need to know more about whether
7604 // we want the area behind or in front of the IME.
7605 final Rect insets = mAttachInfo.mVisibleInsets;
7606 outRect.left += insets.left;
7607 outRect.top += insets.top;
7608 outRect.right -= insets.right;
7609 outRect.bottom -= insets.bottom;
7610 return;
7611 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007612 // The view is not attached to a display so we don't have a context.
7613 // Make a best guess about the display size.
7614 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007615 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 }
7617
7618 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007619 * Dispatch a notification about a resource configuration change down
7620 * the view hierarchy.
7621 * ViewGroups should override to route to their children.
7622 *
7623 * @param newConfig The new resource configuration.
7624 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007625 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007626 */
7627 public void dispatchConfigurationChanged(Configuration newConfig) {
7628 onConfigurationChanged(newConfig);
7629 }
7630
7631 /**
7632 * Called when the current configuration of the resources being used
7633 * by the application have changed. You can use this to decide when
7634 * to reload resources that can changed based on orientation and other
7635 * configuration characterstics. You only need to use this if you are
7636 * not relying on the normal {@link android.app.Activity} mechanism of
7637 * recreating the activity instance upon a configuration change.
7638 *
7639 * @param newConfig The new resource configuration.
7640 */
7641 protected void onConfigurationChanged(Configuration newConfig) {
7642 }
7643
7644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 * Private function to aggregate all per-view attributes in to the view
7646 * root.
7647 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007648 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7649 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 }
7651
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007652 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7653 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007654 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007655 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007656 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007657 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007658 ListenerInfo li = mListenerInfo;
7659 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007660 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 }
7663 }
7664
7665 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007666 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007667 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007668 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7669 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 ai.mRecomputeGlobalAttributes = true;
7671 }
7672 }
7673 }
7674
7675 /**
7676 * Returns whether the device is currently in touch mode. Touch mode is entered
7677 * once the user begins interacting with the device by touch, and affects various
7678 * things like whether focus is always visible to the user.
7679 *
7680 * @return Whether the device is in touch mode.
7681 */
7682 @ViewDebug.ExportedProperty
7683 public boolean isInTouchMode() {
7684 if (mAttachInfo != null) {
7685 return mAttachInfo.mInTouchMode;
7686 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007687 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688 }
7689 }
7690
7691 /**
7692 * Returns the context the view is running in, through which it can
7693 * access the current theme, resources, etc.
7694 *
7695 * @return The view's Context.
7696 */
7697 @ViewDebug.CapturedViewProperty
7698 public final Context getContext() {
7699 return mContext;
7700 }
7701
7702 /**
7703 * Handle a key event before it is processed by any input method
7704 * associated with the view hierarchy. This can be used to intercept
7705 * key events in special situations before the IME consumes them; a
7706 * typical example would be handling the BACK key to update the application's
7707 * UI instead of allowing the IME to see it and close itself.
7708 *
7709 * @param keyCode The value in event.getKeyCode().
7710 * @param event Description of the key event.
7711 * @return If you handled the event, return true. If you want to allow the
7712 * event to be handled by the next receiver, return false.
7713 */
7714 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7715 return false;
7716 }
7717
7718 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007719 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7720 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7722 * is released, if the view is enabled and clickable.
7723 *
Jean Chalard405bc512012-05-29 19:12:34 +09007724 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7725 * although some may elect to do so in some situations. Do not rely on this to
7726 * catch software key presses.
7727 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 * @param keyCode A key code that represents the button pressed, from
7729 * {@link android.view.KeyEvent}.
7730 * @param event The KeyEvent object that defines the button action.
7731 */
7732 public boolean onKeyDown(int keyCode, KeyEvent event) {
7733 boolean result = false;
7734
7735 switch (keyCode) {
7736 case KeyEvent.KEYCODE_DPAD_CENTER:
7737 case KeyEvent.KEYCODE_ENTER: {
7738 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7739 return true;
7740 }
7741 // Long clickable items don't necessarily have to be clickable
7742 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7743 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7744 (event.getRepeatCount() == 0)) {
7745 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007746 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 return true;
7748 }
7749 break;
7750 }
7751 }
7752 return result;
7753 }
7754
7755 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007756 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7757 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7758 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007759 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7760 * although some may elect to do so in some situations. Do not rely on this to
7761 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007762 */
7763 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7764 return false;
7765 }
7766
7767 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007768 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7769 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7771 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007772 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7773 * although some may elect to do so in some situations. Do not rely on this to
7774 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 *
7776 * @param keyCode A key code that represents the button pressed, from
7777 * {@link android.view.KeyEvent}.
7778 * @param event The KeyEvent object that defines the button action.
7779 */
7780 public boolean onKeyUp(int keyCode, KeyEvent event) {
7781 boolean result = false;
7782
7783 switch (keyCode) {
7784 case KeyEvent.KEYCODE_DPAD_CENTER:
7785 case KeyEvent.KEYCODE_ENTER: {
7786 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7787 return true;
7788 }
7789 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7790 setPressed(false);
7791
7792 if (!mHasPerformedLongPress) {
7793 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007794 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795
7796 result = performClick();
7797 }
7798 }
7799 break;
7800 }
7801 }
7802 return result;
7803 }
7804
7805 /**
7806 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7807 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7808 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007809 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7810 * although some may elect to do so in some situations. Do not rely on this to
7811 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 *
7813 * @param keyCode A key code that represents the button pressed, from
7814 * {@link android.view.KeyEvent}.
7815 * @param repeatCount The number of times the action was made.
7816 * @param event The KeyEvent object that defines the button action.
7817 */
7818 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7819 return false;
7820 }
7821
7822 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007823 * Called on the focused view when a key shortcut event is not handled.
7824 * Override this method to implement local key shortcuts for the View.
7825 * Key shortcuts can also be implemented by setting the
7826 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 *
7828 * @param keyCode The value in event.getKeyCode().
7829 * @param event Description of the key event.
7830 * @return If you handled the event, return true. If you want to allow the
7831 * event to be handled by the next receiver, return false.
7832 */
7833 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7834 return false;
7835 }
7836
7837 /**
7838 * Check whether the called view is a text editor, in which case it
7839 * would make sense to automatically display a soft input window for
7840 * it. Subclasses should override this if they implement
7841 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007842 * a call on that method would return a non-null InputConnection, and
7843 * they are really a first-class editor that the user would normally
7844 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007845 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007846 * <p>The default implementation always returns false. This does
7847 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7848 * will not be called or the user can not otherwise perform edits on your
7849 * view; it is just a hint to the system that this is not the primary
7850 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 * @return Returns true if this view is a text editor, else false.
7853 */
7854 public boolean onCheckIsTextEditor() {
7855 return false;
7856 }
Romain Guy8506ab42009-06-11 17:35:47 -07007857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 /**
7859 * Create a new InputConnection for an InputMethod to interact
7860 * with the view. The default implementation returns null, since it doesn't
7861 * support input methods. You can override this to implement such support.
7862 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 * <p>When implementing this, you probably also want to implement
7865 * {@link #onCheckIsTextEditor()} to indicate you will return a
7866 * non-null InputConnection.
7867 *
7868 * @param outAttrs Fill in with attribute information about the connection.
7869 */
7870 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7871 return null;
7872 }
7873
7874 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007875 * Called by the {@link android.view.inputmethod.InputMethodManager}
7876 * when a view who is not the current
7877 * input connection target is trying to make a call on the manager. The
7878 * default implementation returns false; you can override this to return
7879 * true for certain views if you are performing InputConnection proxying
7880 * to them.
7881 * @param view The View that is making the InputMethodManager call.
7882 * @return Return true to allow the call, false to reject.
7883 */
7884 public boolean checkInputConnectionProxy(View view) {
7885 return false;
7886 }
Romain Guy8506ab42009-06-11 17:35:47 -07007887
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 * Show the context menu for this view. It is not safe to hold on to the
7890 * menu after returning from this method.
7891 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007892 * You should normally not overload this method. Overload
7893 * {@link #onCreateContextMenu(ContextMenu)} or define an
7894 * {@link OnCreateContextMenuListener} to add items to the context menu.
7895 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 * @param menu The context menu to populate
7897 */
7898 public void createContextMenu(ContextMenu menu) {
7899 ContextMenuInfo menuInfo = getContextMenuInfo();
7900
7901 // Sets the current menu info so all items added to menu will have
7902 // my extra info set.
7903 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7904
7905 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007906 ListenerInfo li = mListenerInfo;
7907 if (li != null && li.mOnCreateContextMenuListener != null) {
7908 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007909 }
7910
7911 // Clear the extra information so subsequent items that aren't mine don't
7912 // have my extra info.
7913 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7914
7915 if (mParent != null) {
7916 mParent.createContextMenu(menu);
7917 }
7918 }
7919
7920 /**
7921 * Views should implement this if they have extra information to associate
7922 * with the context menu. The return result is supplied as a parameter to
7923 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7924 * callback.
7925 *
7926 * @return Extra information about the item for which the context menu
7927 * should be shown. This information will vary across different
7928 * subclasses of View.
7929 */
7930 protected ContextMenuInfo getContextMenuInfo() {
7931 return null;
7932 }
7933
7934 /**
7935 * Views should implement this if the view itself is going to add items to
7936 * the context menu.
7937 *
7938 * @param menu the context menu to populate
7939 */
7940 protected void onCreateContextMenu(ContextMenu menu) {
7941 }
7942
7943 /**
7944 * Implement this method to handle trackball motion events. The
7945 * <em>relative</em> movement of the trackball since the last event
7946 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7947 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7948 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7949 * they will often be fractional values, representing the more fine-grained
7950 * movement information available from a trackball).
7951 *
7952 * @param event The motion event.
7953 * @return True if the event was handled, false otherwise.
7954 */
7955 public boolean onTrackballEvent(MotionEvent event) {
7956 return false;
7957 }
7958
7959 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007960 * Implement this method to handle generic motion events.
7961 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007962 * Generic motion events describe joystick movements, mouse hovers, track pad
7963 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007964 * {@link MotionEvent#getSource() source} of the motion event specifies
7965 * the class of input that was received. Implementations of this method
7966 * must examine the bits in the source before processing the event.
7967 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007968 * </p><p>
7969 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7970 * are delivered to the view under the pointer. All other generic motion events are
7971 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007972 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007973 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007974 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007975 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7976 * // process the joystick movement...
7977 * return true;
7978 * }
7979 * }
7980 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7981 * switch (event.getAction()) {
7982 * case MotionEvent.ACTION_HOVER_MOVE:
7983 * // process the mouse hover movement...
7984 * return true;
7985 * case MotionEvent.ACTION_SCROLL:
7986 * // process the scroll wheel movement...
7987 * return true;
7988 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007989 * }
7990 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007991 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007992 *
7993 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007994 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007995 */
7996 public boolean onGenericMotionEvent(MotionEvent event) {
7997 return false;
7998 }
7999
8000 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008001 * Implement this method to handle hover events.
8002 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07008003 * This method is called whenever a pointer is hovering into, over, or out of the
8004 * bounds of a view and the view is not currently being touched.
8005 * Hover events are represented as pointer events with action
8006 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
8007 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
8008 * </p>
8009 * <ul>
8010 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8011 * when the pointer enters the bounds of the view.</li>
8012 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8013 * when the pointer has already entered the bounds of the view and has moved.</li>
8014 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8015 * when the pointer has exited the bounds of the view or when the pointer is
8016 * about to go down due to a button click, tap, or similar user action that
8017 * causes the view to be touched.</li>
8018 * </ul>
8019 * <p>
8020 * The view should implement this method to return true to indicate that it is
8021 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008022 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008023 * The default implementation calls {@link #setHovered} to update the hovered state
8024 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008025 * is enabled and is clickable. The default implementation also sends hover
8026 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008027 * </p>
8028 *
8029 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008030 * @return True if the view handled the hover event.
8031 *
8032 * @see #isHovered
8033 * @see #setHovered
8034 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008035 */
8036 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008037 // The root view may receive hover (or touch) events that are outside the bounds of
8038 // the window. This code ensures that we only send accessibility events for
8039 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008040 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008041 if (!mSendingHoverAccessibilityEvents) {
8042 if ((action == MotionEvent.ACTION_HOVER_ENTER
8043 || action == MotionEvent.ACTION_HOVER_MOVE)
8044 && !hasHoveredChild()
8045 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008046 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008047 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008048 }
8049 } else {
8050 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008051 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008052 && !pointInView(event.getX(), event.getY()))) {
8053 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008054 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008055 // If the window does not have input focus we take away accessibility
8056 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008057 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008058 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008059 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008060 }
Jeff Browna1b24182011-07-28 13:38:24 -07008061 }
8062
Jeff Brown87b7f802011-06-21 18:35:45 -07008063 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008064 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008065 case MotionEvent.ACTION_HOVER_ENTER:
8066 setHovered(true);
8067 break;
8068 case MotionEvent.ACTION_HOVER_EXIT:
8069 setHovered(false);
8070 break;
8071 }
Jeff Browna1b24182011-07-28 13:38:24 -07008072
8073 // Dispatch the event to onGenericMotionEvent before returning true.
8074 // This is to provide compatibility with existing applications that
8075 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8076 // break because of the new default handling for hoverable views
8077 // in onHoverEvent.
8078 // Note that onGenericMotionEvent will be called by default when
8079 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8080 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008081 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008082 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008083
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008084 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008085 }
8086
8087 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008088 * Returns true if the view should handle {@link #onHoverEvent}
8089 * by calling {@link #setHovered} to change its hovered state.
8090 *
8091 * @return True if the view is hoverable.
8092 */
8093 private boolean isHoverable() {
8094 final int viewFlags = mViewFlags;
8095 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8096 return false;
8097 }
8098
8099 return (viewFlags & CLICKABLE) == CLICKABLE
8100 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8101 }
8102
8103 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008104 * Returns true if the view is currently hovered.
8105 *
8106 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008107 *
8108 * @see #setHovered
8109 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008110 */
Jeff Brown10b62902011-06-20 16:40:37 -07008111 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008112 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008113 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008114 }
8115
8116 /**
8117 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008118 * <p>
8119 * Calling this method also changes the drawable state of the view. This
8120 * enables the view to react to hover by using different drawable resources
8121 * to change its appearance.
8122 * </p><p>
8123 * The {@link #onHoverChanged} method is called when the hovered state changes.
8124 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008125 *
8126 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008127 *
8128 * @see #isHovered
8129 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008130 */
8131 public void setHovered(boolean hovered) {
8132 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008133 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8134 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008135 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008136 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008137 }
8138 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008139 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8140 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008141 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008142 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008143 }
8144 }
8145 }
8146
8147 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008148 * Implement this method to handle hover state changes.
8149 * <p>
8150 * This method is called whenever the hover state changes as a result of a
8151 * call to {@link #setHovered}.
8152 * </p>
8153 *
8154 * @param hovered The current hover state, as returned by {@link #isHovered}.
8155 *
8156 * @see #isHovered
8157 * @see #setHovered
8158 */
8159 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008160 }
8161
8162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 * Implement this method to handle touch screen motion events.
8164 *
8165 * @param event The motion event.
8166 * @return True if the event was handled, false otherwise.
8167 */
8168 public boolean onTouchEvent(MotionEvent event) {
8169 final int viewFlags = mViewFlags;
8170
8171 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008172 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008173 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 // A disabled view that is clickable still consumes the touch
8176 // events, it just doesn't respond to them.
8177 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8178 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8179 }
8180
8181 if (mTouchDelegate != null) {
8182 if (mTouchDelegate.onTouchEvent(event)) {
8183 return true;
8184 }
8185 }
8186
8187 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8188 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8189 switch (event.getAction()) {
8190 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008191 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8192 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 // take focus if we don't have it already and we should in
8194 // touch mode.
8195 boolean focusTaken = false;
8196 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8197 focusTaken = requestFocus();
8198 }
8199
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008200 if (prepressed) {
8201 // The button is being released before we actually
8202 // showed it as pressed. Make it show the pressed
8203 // state now (before scheduling the click) to ensure
8204 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008205 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008206 }
Joe Malin32736f02011-01-19 16:14:20 -08008207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008208 if (!mHasPerformedLongPress) {
8209 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008210 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211
8212 // Only perform take click actions if we were in the pressed state
8213 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008214 // Use a Runnable and post this rather than calling
8215 // performClick directly. This lets other visual state
8216 // of the view update before click actions start.
8217 if (mPerformClick == null) {
8218 mPerformClick = new PerformClick();
8219 }
8220 if (!post(mPerformClick)) {
8221 performClick();
8222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 }
8224 }
8225
8226 if (mUnsetPressedState == null) {
8227 mUnsetPressedState = new UnsetPressedState();
8228 }
8229
Adam Powelle14579b2009-12-16 18:39:52 -08008230 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008231 postDelayed(mUnsetPressedState,
8232 ViewConfiguration.getPressedStateDuration());
8233 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008234 // If the post failed, unpress right now
8235 mUnsetPressedState.run();
8236 }
Adam Powelle14579b2009-12-16 18:39:52 -08008237 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 }
8239 break;
8240
8241 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008242 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008243
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008244 if (performButtonActionOnTouchDown(event)) {
8245 break;
8246 }
8247
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008248 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008249 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008250
8251 // For views inside a scrolling container, delay the pressed feedback for
8252 // a short period in case this is a scroll.
8253 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008254 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008255 if (mPendingCheckForTap == null) {
8256 mPendingCheckForTap = new CheckForTap();
8257 }
8258 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8259 } else {
8260 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008261 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008262 checkForLongClick(0);
8263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 break;
8265
8266 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008267 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008268 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008269 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 break;
8271
8272 case MotionEvent.ACTION_MOVE:
8273 final int x = (int) event.getX();
8274 final int y = (int) event.getY();
8275
8276 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008277 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008279 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008280 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008281 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008282 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008283
Adam Powell4d6f0662012-02-21 15:11:11 -08008284 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 }
8287 break;
8288 }
8289 return true;
8290 }
8291
8292 return false;
8293 }
8294
8295 /**
Adam Powell10298662011-08-14 18:26:30 -07008296 * @hide
8297 */
8298 public boolean isInScrollingContainer() {
8299 ViewParent p = getParent();
8300 while (p != null && p instanceof ViewGroup) {
8301 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8302 return true;
8303 }
8304 p = p.getParent();
8305 }
8306 return false;
8307 }
8308
8309 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008310 * Remove the longpress detection timer.
8311 */
8312 private void removeLongPressCallback() {
8313 if (mPendingCheckForLongPress != null) {
8314 removeCallbacks(mPendingCheckForLongPress);
8315 }
8316 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008317
8318 /**
8319 * Remove the pending click action
8320 */
8321 private void removePerformClickCallback() {
8322 if (mPerformClick != null) {
8323 removeCallbacks(mPerformClick);
8324 }
8325 }
8326
Adam Powelle14579b2009-12-16 18:39:52 -08008327 /**
Romain Guya440b002010-02-24 15:57:54 -08008328 * Remove the prepress detection timer.
8329 */
8330 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008331 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008332 setPressed(false);
8333 removeCallbacks(mUnsetPressedState);
8334 }
8335 }
8336
8337 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008338 * Remove the tap detection timer.
8339 */
8340 private void removeTapCallback() {
8341 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008342 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008343 removeCallbacks(mPendingCheckForTap);
8344 }
8345 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008346
8347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 * Cancels a pending long press. Your subclass can use this if you
8349 * want the context menu to come up if the user presses and holds
8350 * at the same place, but you don't want it to come up if they press
8351 * and then move around enough to cause scrolling.
8352 */
8353 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008354 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008355
8356 /*
8357 * The prepressed state handled by the tap callback is a display
8358 * construct, but the tap callback will post a long press callback
8359 * less its own timeout. Remove it here.
8360 */
8361 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 }
8363
8364 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008365 * Remove the pending callback for sending a
8366 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8367 */
8368 private void removeSendViewScrolledAccessibilityEventCallback() {
8369 if (mSendViewScrolledAccessibilityEvent != null) {
8370 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008371 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008372 }
8373 }
8374
8375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 * Sets the TouchDelegate for this View.
8377 */
8378 public void setTouchDelegate(TouchDelegate delegate) {
8379 mTouchDelegate = delegate;
8380 }
8381
8382 /**
8383 * Gets the TouchDelegate for this View.
8384 */
8385 public TouchDelegate getTouchDelegate() {
8386 return mTouchDelegate;
8387 }
8388
8389 /**
8390 * Set flags controlling behavior of this view.
8391 *
8392 * @param flags Constant indicating the value which should be set
8393 * @param mask Constant indicating the bit range that should be changed
8394 */
8395 void setFlags(int flags, int mask) {
8396 int old = mViewFlags;
8397 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8398
8399 int changed = mViewFlags ^ old;
8400 if (changed == 0) {
8401 return;
8402 }
8403 int privateFlags = mPrivateFlags;
8404
8405 /* Check if the FOCUSABLE bit has changed */
8406 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008407 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008409 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410 /* Give up focus if we are no longer focusable */
8411 clearFocus();
8412 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008413 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 /*
8415 * Tell the view system that we are now available to take focus
8416 * if no one else already has it.
8417 */
8418 if (mParent != null) mParent.focusableViewAvailable(this);
8419 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008420 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8421 notifyAccessibilityStateChanged();
8422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 }
8424
8425 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8426 if ((changed & VISIBILITY_MASK) != 0) {
8427 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008428 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008429 * it was not visible. Marking it drawn ensures that the invalidation will
8430 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008432 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008433 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008434
8435 needGlobalAttributesUpdate(true);
8436
8437 // a view becoming visible is worth notifying the parent
8438 // about in case nothing has focus. even if this specific view
8439 // isn't focusable, it may contain something that is, so let
8440 // the root view try to give this focus if nothing else does.
8441 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8442 mParent.focusableViewAvailable(this);
8443 }
8444 }
8445 }
8446
8447 /* Check if the GONE bit has changed */
8448 if ((changed & GONE) != 0) {
8449 needGlobalAttributesUpdate(false);
8450 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451
Romain Guyecd80ee2009-12-03 17:13:02 -08008452 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8453 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008454 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008455 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008456 if (mParent instanceof View) {
8457 // GONE views noop invalidation, so invalidate the parent
8458 ((View) mParent).invalidate(true);
8459 }
8460 // Mark the view drawn to ensure that it gets invalidated properly the next
8461 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008462 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008463 }
8464 if (mAttachInfo != null) {
8465 mAttachInfo.mViewVisibilityChanged = true;
8466 }
8467 }
8468
8469 /* Check if the VISIBLE bit has changed */
8470 if ((changed & INVISIBLE) != 0) {
8471 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008472 /*
8473 * If this view is becoming invisible, set the DRAWN flag so that
8474 * the next invalidate() will not be skipped.
8475 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008476 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477
8478 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008479 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 if (getRootView() != this) {
8481 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008482 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 }
8484 }
8485 if (mAttachInfo != null) {
8486 mAttachInfo.mViewVisibilityChanged = true;
8487 }
8488 }
8489
Adam Powell326d8082009-12-09 15:10:07 -08008490 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008491 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008492 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8493 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008494 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008495 } else if (mParent != null) {
8496 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008497 }
Adam Powell326d8082009-12-09 15:10:07 -08008498 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8499 }
8500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008501 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8502 destroyDrawingCache();
8503 }
8504
8505 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8506 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008507 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008508 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 }
8510
8511 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8512 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008513 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 }
8515
8516 if ((changed & DRAW_MASK) != 0) {
8517 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008518 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008519 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8520 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008522 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 }
8524 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008525 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 }
8527 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008528 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 }
8530
8531 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008532 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 mParent.recomputeViewAttributes(this);
8534 }
8535 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008536
8537 if (AccessibilityManager.getInstance(mContext).isEnabled()
8538 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8539 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8540 notifyAccessibilityStateChanged();
8541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 }
8543
8544 /**
8545 * Change the view's z order in the tree, so it's on top of other sibling
8546 * views
8547 */
8548 public void bringToFront() {
8549 if (mParent != null) {
8550 mParent.bringChildToFront(this);
8551 }
8552 }
8553
8554 /**
8555 * This is called in response to an internal scroll in this view (i.e., the
8556 * view scrolled its own contents). This is typically as a result of
8557 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8558 * called.
8559 *
8560 * @param l Current horizontal scroll origin.
8561 * @param t Current vertical scroll origin.
8562 * @param oldl Previous horizontal scroll origin.
8563 * @param oldt Previous vertical scroll origin.
8564 */
8565 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008566 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8567 postSendViewScrolledAccessibilityEventCallback();
8568 }
8569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 mBackgroundSizeChanged = true;
8571
8572 final AttachInfo ai = mAttachInfo;
8573 if (ai != null) {
8574 ai.mViewScrollChanged = true;
8575 }
8576 }
8577
8578 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008579 * Interface definition for a callback to be invoked when the layout bounds of a view
8580 * changes due to layout processing.
8581 */
8582 public interface OnLayoutChangeListener {
8583 /**
8584 * Called when the focus state of a view has changed.
8585 *
8586 * @param v The view whose state has changed.
8587 * @param left The new value of the view's left property.
8588 * @param top The new value of the view's top property.
8589 * @param right The new value of the view's right property.
8590 * @param bottom The new value of the view's bottom property.
8591 * @param oldLeft The previous value of the view's left property.
8592 * @param oldTop The previous value of the view's top property.
8593 * @param oldRight The previous value of the view's right property.
8594 * @param oldBottom The previous value of the view's bottom property.
8595 */
8596 void onLayoutChange(View v, int left, int top, int right, int bottom,
8597 int oldLeft, int oldTop, int oldRight, int oldBottom);
8598 }
8599
8600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 * This is called during layout when the size of this view has changed. If
8602 * you were just added to the view hierarchy, you're called with the old
8603 * values of 0.
8604 *
8605 * @param w Current width of this view.
8606 * @param h Current height of this view.
8607 * @param oldw Old width of this view.
8608 * @param oldh Old height of this view.
8609 */
8610 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8611 }
8612
8613 /**
8614 * Called by draw to draw the child views. This may be overridden
8615 * by derived classes to gain control just before its children are drawn
8616 * (but after its own view has been drawn).
8617 * @param canvas the canvas on which to draw the view
8618 */
8619 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 }
8622
8623 /**
8624 * Gets the parent of this view. Note that the parent is a
8625 * ViewParent and not necessarily a View.
8626 *
8627 * @return Parent of this view.
8628 */
8629 public final ViewParent getParent() {
8630 return mParent;
8631 }
8632
8633 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008634 * Set the horizontal scrolled position of your view. This will cause a call to
8635 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8636 * invalidated.
8637 * @param value the x position to scroll to
8638 */
8639 public void setScrollX(int value) {
8640 scrollTo(value, mScrollY);
8641 }
8642
8643 /**
8644 * Set the vertical scrolled position of your view. This will cause a call to
8645 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8646 * invalidated.
8647 * @param value the y position to scroll to
8648 */
8649 public void setScrollY(int value) {
8650 scrollTo(mScrollX, value);
8651 }
8652
8653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008654 * Return the scrolled left position of this view. This is the left edge of
8655 * the displayed part of your view. You do not need to draw any pixels
8656 * farther left, since those are outside of the frame of your view on
8657 * screen.
8658 *
8659 * @return The left edge of the displayed part of your view, in pixels.
8660 */
8661 public final int getScrollX() {
8662 return mScrollX;
8663 }
8664
8665 /**
8666 * Return the scrolled top position of this view. This is the top edge of
8667 * the displayed part of your view. You do not need to draw any pixels above
8668 * it, since those are outside of the frame of your view on screen.
8669 *
8670 * @return The top edge of the displayed part of your view, in pixels.
8671 */
8672 public final int getScrollY() {
8673 return mScrollY;
8674 }
8675
8676 /**
8677 * Return the width of the your view.
8678 *
8679 * @return The width of your view, in pixels.
8680 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008681 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 public final int getWidth() {
8683 return mRight - mLeft;
8684 }
8685
8686 /**
8687 * Return the height of your view.
8688 *
8689 * @return The height of your view, in pixels.
8690 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008691 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 public final int getHeight() {
8693 return mBottom - mTop;
8694 }
8695
8696 /**
8697 * Return the visible drawing bounds of your view. Fills in the output
8698 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008699 * getWidth(), and getHeight(). These bounds do not account for any
8700 * transformation properties currently set on the view, such as
8701 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 *
8703 * @param outRect The (scrolled) drawing bounds of the view.
8704 */
8705 public void getDrawingRect(Rect outRect) {
8706 outRect.left = mScrollX;
8707 outRect.top = mScrollY;
8708 outRect.right = mScrollX + (mRight - mLeft);
8709 outRect.bottom = mScrollY + (mBottom - mTop);
8710 }
8711
8712 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008713 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8714 * raw width component (that is the result is masked by
8715 * {@link #MEASURED_SIZE_MASK}).
8716 *
8717 * @return The raw measured width of this view.
8718 */
8719 public final int getMeasuredWidth() {
8720 return mMeasuredWidth & MEASURED_SIZE_MASK;
8721 }
8722
8723 /**
8724 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008725 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008726 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008727 * This should be used during measurement and layout calculations only. Use
8728 * {@link #getWidth()} to see how wide a view is after layout.
8729 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008730 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008732 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 return mMeasuredWidth;
8734 }
8735
8736 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008737 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8738 * raw width component (that is the result is masked by
8739 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008741 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008742 */
8743 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008744 return mMeasuredHeight & MEASURED_SIZE_MASK;
8745 }
8746
8747 /**
8748 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008749 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008750 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8751 * This should be used during measurement and layout calculations only. Use
8752 * {@link #getHeight()} to see how wide a view is after layout.
8753 *
8754 * @return The measured width of this view as a bit mask.
8755 */
8756 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 return mMeasuredHeight;
8758 }
8759
8760 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008761 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8762 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8763 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8764 * and the height component is at the shifted bits
8765 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8766 */
8767 public final int getMeasuredState() {
8768 return (mMeasuredWidth&MEASURED_STATE_MASK)
8769 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8770 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8771 }
8772
8773 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008774 * The transform matrix of this view, which is calculated based on the current
8775 * roation, scale, and pivot properties.
8776 *
8777 * @see #getRotation()
8778 * @see #getScaleX()
8779 * @see #getScaleY()
8780 * @see #getPivotX()
8781 * @see #getPivotY()
8782 * @return The current transform matrix for the view
8783 */
8784 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008785 if (mTransformationInfo != null) {
8786 updateMatrix();
8787 return mTransformationInfo.mMatrix;
8788 }
8789 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008790 }
8791
8792 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008793 * Utility function to determine if the value is far enough away from zero to be
8794 * considered non-zero.
8795 * @param value A floating point value to check for zero-ness
8796 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8797 */
8798 private static boolean nonzero(float value) {
8799 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8800 }
8801
8802 /**
Jeff Brown86671742010-09-30 20:00:15 -07008803 * Returns true if the transform matrix is the identity matrix.
8804 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008805 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008806 * @return True if the transform matrix is the identity matrix, false otherwise.
8807 */
Jeff Brown86671742010-09-30 20:00:15 -07008808 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008809 if (mTransformationInfo != null) {
8810 updateMatrix();
8811 return mTransformationInfo.mMatrixIsIdentity;
8812 }
8813 return true;
8814 }
8815
8816 void ensureTransformationInfo() {
8817 if (mTransformationInfo == null) {
8818 mTransformationInfo = new TransformationInfo();
8819 }
Jeff Brown86671742010-09-30 20:00:15 -07008820 }
8821
8822 /**
8823 * Recomputes the transform matrix if necessary.
8824 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008825 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008826 final TransformationInfo info = mTransformationInfo;
8827 if (info == null) {
8828 return;
8829 }
8830 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 // transform-related properties have changed since the last time someone
8832 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008833
8834 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008835 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008836 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8837 info.mPrevWidth = mRight - mLeft;
8838 info.mPrevHeight = mBottom - mTop;
8839 info.mPivotX = info.mPrevWidth / 2f;
8840 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008841 }
8842 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008843 info.mMatrix.reset();
8844 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8845 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8846 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8847 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008848 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008849 if (info.mCamera == null) {
8850 info.mCamera = new Camera();
8851 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008852 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008853 info.mCamera.save();
8854 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8855 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8856 info.mCamera.getMatrix(info.matrix3D);
8857 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8858 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8859 info.mPivotY + info.mTranslationY);
8860 info.mMatrix.postConcat(info.matrix3D);
8861 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008862 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008863 info.mMatrixDirty = false;
8864 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8865 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008866 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008867 }
8868
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008869 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008870 * Utility method to retrieve the inverse of the current mMatrix property.
8871 * We cache the matrix to avoid recalculating it when transform properties
8872 * have not changed.
8873 *
8874 * @return The inverse of the current matrix of this view.
8875 */
Jeff Brown86671742010-09-30 20:00:15 -07008876 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008877 final TransformationInfo info = mTransformationInfo;
8878 if (info != null) {
8879 updateMatrix();
8880 if (info.mInverseMatrixDirty) {
8881 if (info.mInverseMatrix == null) {
8882 info.mInverseMatrix = new Matrix();
8883 }
8884 info.mMatrix.invert(info.mInverseMatrix);
8885 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008886 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008887 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008888 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008889 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008890 }
8891
8892 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008893 * Gets the distance along the Z axis from the camera to this view.
8894 *
8895 * @see #setCameraDistance(float)
8896 *
8897 * @return The distance along the Z axis.
8898 */
8899 public float getCameraDistance() {
8900 ensureTransformationInfo();
8901 final float dpi = mResources.getDisplayMetrics().densityDpi;
8902 final TransformationInfo info = mTransformationInfo;
8903 if (info.mCamera == null) {
8904 info.mCamera = new Camera();
8905 info.matrix3D = new Matrix();
8906 }
8907 return -(info.mCamera.getLocationZ() * dpi);
8908 }
8909
8910 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008911 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8912 * views are drawn) from the camera to this view. The camera's distance
8913 * affects 3D transformations, for instance rotations around the X and Y
8914 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008915 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008916 * use a camera distance that's greater than the height (X axis rotation) or
8917 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008918 *
Romain Guya5364ee2011-02-24 14:46:04 -08008919 * <p>The distance of the camera from the view plane can have an affect on the
8920 * perspective distortion of the view when it is rotated around the x or y axis.
8921 * For example, a large distance will result in a large viewing angle, and there
8922 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008923 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008924 * also result in some drawing artifacts if the rotated view ends up partially
8925 * behind the camera (which is why the recommendation is to use a distance at
8926 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008927 *
Romain Guya5364ee2011-02-24 14:46:04 -08008928 * <p>The distance is expressed in "depth pixels." The default distance depends
8929 * on the screen density. For instance, on a medium density display, the
8930 * default distance is 1280. On a high density display, the default distance
8931 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008932 *
Romain Guya5364ee2011-02-24 14:46:04 -08008933 * <p>If you want to specify a distance that leads to visually consistent
8934 * results across various densities, use the following formula:</p>
8935 * <pre>
8936 * float scale = context.getResources().getDisplayMetrics().density;
8937 * view.setCameraDistance(distance * scale);
8938 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008939 *
Romain Guya5364ee2011-02-24 14:46:04 -08008940 * <p>The density scale factor of a high density display is 1.5,
8941 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008942 *
Romain Guya5364ee2011-02-24 14:46:04 -08008943 * @param distance The distance in "depth pixels", if negative the opposite
8944 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008945 *
8946 * @see #setRotationX(float)
8947 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008948 */
8949 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008950 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008951
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008952 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008953 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008954 final TransformationInfo info = mTransformationInfo;
8955 if (info.mCamera == null) {
8956 info.mCamera = new Camera();
8957 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008958 }
8959
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008960 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8961 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008962
Chet Haase9d1992d2012-03-13 11:03:25 -07008963 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008964 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008965 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008966 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008967 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008968 // View was rejected last time it was drawn by its parent; this may have changed
8969 invalidateParentIfNeeded();
8970 }
Romain Guya5364ee2011-02-24 14:46:04 -08008971 }
8972
8973 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008974 * The degrees that the view is rotated around the pivot point.
8975 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008976 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008977 * @see #getPivotX()
8978 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008979 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008980 * @return The degrees of rotation.
8981 */
Chet Haasea5531132012-02-02 13:41:44 -08008982 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008983 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008984 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008985 }
8986
8987 /**
Chet Haase897247b2010-09-09 14:54:47 -07008988 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8989 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008990 *
8991 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008992 *
8993 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008994 * @see #getPivotX()
8995 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008996 * @see #setRotationX(float)
8997 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008998 *
8999 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07009000 */
9001 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009002 ensureTransformationInfo();
9003 final TransformationInfo info = mTransformationInfo;
9004 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009005 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009006 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009007 info.mRotation = rotation;
9008 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009009 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009010 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009011 mDisplayList.setRotation(rotation);
9012 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009013 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009014 // View was rejected last time it was drawn by its parent; this may have changed
9015 invalidateParentIfNeeded();
9016 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 }
9018 }
9019
9020 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009021 * The degrees that the view is rotated around the vertical axis through the pivot point.
9022 *
9023 * @see #getPivotX()
9024 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009025 * @see #setRotationY(float)
9026 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009027 * @return The degrees of Y rotation.
9028 */
Chet Haasea5531132012-02-02 13:41:44 -08009029 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009030 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009031 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009032 }
9033
9034 /**
Chet Haase897247b2010-09-09 14:54:47 -07009035 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9036 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9037 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009038 *
Romain Guya5364ee2011-02-24 14:46:04 -08009039 * When rotating large views, it is recommended to adjust the camera distance
9040 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009041 *
9042 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009043 *
9044 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009045 * @see #getPivotX()
9046 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009047 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009048 * @see #setRotationX(float)
9049 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009050 *
9051 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009052 */
9053 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009054 ensureTransformationInfo();
9055 final TransformationInfo info = mTransformationInfo;
9056 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009057 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009058 info.mRotationY = rotationY;
9059 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009060 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009061 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009062 mDisplayList.setRotationY(rotationY);
9063 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009064 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009065 // View was rejected last time it was drawn by its parent; this may have changed
9066 invalidateParentIfNeeded();
9067 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009068 }
9069 }
9070
9071 /**
9072 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9073 *
9074 * @see #getPivotX()
9075 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009076 * @see #setRotationX(float)
9077 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009078 * @return The degrees of X rotation.
9079 */
Chet Haasea5531132012-02-02 13:41:44 -08009080 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009081 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009082 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009083 }
9084
9085 /**
Chet Haase897247b2010-09-09 14:54:47 -07009086 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9087 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9088 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009089 *
Romain Guya5364ee2011-02-24 14:46:04 -08009090 * When rotating large views, it is recommended to adjust the camera distance
9091 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009092 *
9093 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009094 *
9095 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009096 * @see #getPivotX()
9097 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009098 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009099 * @see #setRotationY(float)
9100 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009101 *
9102 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009103 */
9104 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009105 ensureTransformationInfo();
9106 final TransformationInfo info = mTransformationInfo;
9107 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009108 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009109 info.mRotationX = rotationX;
9110 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009111 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009112 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009113 mDisplayList.setRotationX(rotationX);
9114 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009115 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009116 // View was rejected last time it was drawn by its parent; this may have changed
9117 invalidateParentIfNeeded();
9118 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009119 }
9120 }
9121
9122 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009123 * The amount that the view is scaled in x around the pivot point, as a proportion of
9124 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9125 *
Joe Onorato93162322010-09-16 15:42:01 -04009126 * <p>By default, this is 1.0f.
9127 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009128 * @see #getPivotX()
9129 * @see #getPivotY()
9130 * @return The scaling factor.
9131 */
Chet Haasea5531132012-02-02 13:41:44 -08009132 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009133 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009134 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009135 }
9136
9137 /**
9138 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9139 * the view's unscaled width. A value of 1 means that no scaling is applied.
9140 *
9141 * @param scaleX The scaling factor.
9142 * @see #getPivotX()
9143 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009144 *
9145 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009146 */
9147 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009148 ensureTransformationInfo();
9149 final TransformationInfo info = mTransformationInfo;
9150 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009151 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009152 info.mScaleX = scaleX;
9153 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009154 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009155 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009156 mDisplayList.setScaleX(scaleX);
9157 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009158 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009159 // View was rejected last time it was drawn by its parent; this may have changed
9160 invalidateParentIfNeeded();
9161 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009162 }
9163 }
9164
9165 /**
9166 * The amount that the view is scaled in y around the pivot point, as a proportion of
9167 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9168 *
Joe Onorato93162322010-09-16 15:42:01 -04009169 * <p>By default, this is 1.0f.
9170 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009171 * @see #getPivotX()
9172 * @see #getPivotY()
9173 * @return The scaling factor.
9174 */
Chet Haasea5531132012-02-02 13:41:44 -08009175 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009176 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009177 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009178 }
9179
9180 /**
9181 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9182 * the view's unscaled width. A value of 1 means that no scaling is applied.
9183 *
9184 * @param scaleY The scaling factor.
9185 * @see #getPivotX()
9186 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009187 *
9188 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009189 */
9190 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009191 ensureTransformationInfo();
9192 final TransformationInfo info = mTransformationInfo;
9193 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009194 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009195 info.mScaleY = scaleY;
9196 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009197 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009198 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009199 mDisplayList.setScaleY(scaleY);
9200 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009201 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009202 // View was rejected last time it was drawn by its parent; this may have changed
9203 invalidateParentIfNeeded();
9204 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009205 }
9206 }
9207
9208 /**
9209 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9210 * and {@link #setScaleX(float) scaled}.
9211 *
9212 * @see #getRotation()
9213 * @see #getScaleX()
9214 * @see #getScaleY()
9215 * @see #getPivotY()
9216 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009217 *
9218 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009219 */
Chet Haasea5531132012-02-02 13:41:44 -08009220 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009221 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009222 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009223 }
9224
9225 /**
9226 * Sets the x location of the point around which the view is
9227 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009228 * By default, the pivot point is centered on the object.
9229 * Setting this property disables this behavior and causes the view to use only the
9230 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009231 *
9232 * @param pivotX The x location of the pivot point.
9233 * @see #getRotation()
9234 * @see #getScaleX()
9235 * @see #getScaleY()
9236 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009237 *
9238 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009239 */
9240 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009241 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009242 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009243 final TransformationInfo info = mTransformationInfo;
9244 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009245 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009246 info.mPivotX = pivotX;
9247 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009248 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009249 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009250 mDisplayList.setPivotX(pivotX);
9251 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009252 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009253 // View was rejected last time it was drawn by its parent; this may have changed
9254 invalidateParentIfNeeded();
9255 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009256 }
9257 }
9258
9259 /**
9260 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9261 * and {@link #setScaleY(float) scaled}.
9262 *
9263 * @see #getRotation()
9264 * @see #getScaleX()
9265 * @see #getScaleY()
9266 * @see #getPivotY()
9267 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009268 *
9269 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009270 */
Chet Haasea5531132012-02-02 13:41:44 -08009271 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009272 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009273 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009274 }
9275
9276 /**
9277 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009278 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9279 * Setting this property disables this behavior and causes the view to use only the
9280 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009281 *
9282 * @param pivotY The y location of the pivot point.
9283 * @see #getRotation()
9284 * @see #getScaleX()
9285 * @see #getScaleY()
9286 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009287 *
9288 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009289 */
9290 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009291 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009292 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009293 final TransformationInfo info = mTransformationInfo;
9294 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009295 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009296 info.mPivotY = pivotY;
9297 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009298 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009299 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009300 mDisplayList.setPivotY(pivotY);
9301 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009302 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009303 // View was rejected last time it was drawn by its parent; this may have changed
9304 invalidateParentIfNeeded();
9305 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009306 }
9307 }
9308
9309 /**
9310 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9311 * completely transparent and 1 means the view is completely opaque.
9312 *
Joe Onorato93162322010-09-16 15:42:01 -04009313 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009314 * @return The opacity of the view.
9315 */
Chet Haasea5531132012-02-02 13:41:44 -08009316 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009317 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009318 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009319 }
9320
9321 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009322 * Returns whether this View has content which overlaps. This function, intended to be
9323 * overridden by specific View types, is an optimization when alpha is set on a view. If
9324 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9325 * and then composited it into place, which can be expensive. If the view has no overlapping
9326 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9327 * An example of overlapping rendering is a TextView with a background image, such as a
9328 * Button. An example of non-overlapping rendering is a TextView with no background, or
9329 * an ImageView with only the foreground image. The default implementation returns true;
9330 * subclasses should override if they have cases which can be optimized.
9331 *
9332 * @return true if the content in this view might overlap, false otherwise.
9333 */
9334 public boolean hasOverlappingRendering() {
9335 return true;
9336 }
9337
9338 /**
Romain Guy171c5922011-01-06 10:04:23 -08009339 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9340 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009341 *
Romain Guy171c5922011-01-06 10:04:23 -08009342 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9343 * responsible for applying the opacity itself. Otherwise, calling this method is
9344 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009345 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009346 *
Chet Haasea5531132012-02-02 13:41:44 -08009347 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9348 * performance implications. It is generally best to use the alpha property sparingly and
9349 * transiently, as in the case of fading animations.</p>
9350 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009351 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009352 *
Joe Malin32736f02011-01-19 16:14:20 -08009353 * @see #setLayerType(int, android.graphics.Paint)
9354 *
Chet Haase73066682010-11-29 15:55:32 -08009355 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009356 */
9357 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009358 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009359 if (mTransformationInfo.mAlpha != alpha) {
9360 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009361 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009362 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009363 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009364 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009365 invalidate(true);
9366 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009367 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009368 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009369 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009370 mDisplayList.setAlpha(alpha);
9371 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009372 }
Chet Haaseed032702010-10-01 14:05:54 -07009373 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009374 }
9375
9376 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009377 * Faster version of setAlpha() which performs the same steps except there are
9378 * no calls to invalidate(). The caller of this function should perform proper invalidation
9379 * on the parent and this object. The return value indicates whether the subclass handles
9380 * alpha (the return value for onSetAlpha()).
9381 *
9382 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009383 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9384 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009385 */
9386 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009387 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009388 if (mTransformationInfo.mAlpha != alpha) {
9389 mTransformationInfo.mAlpha = alpha;
9390 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9391 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009392 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009393 return true;
9394 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009395 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009396 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009397 mDisplayList.setAlpha(alpha);
9398 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009399 }
Chet Haasea00f3862011-02-22 06:34:40 -08009400 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009401 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009402 }
9403
9404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 * Top position of this view relative to its parent.
9406 *
9407 * @return The top of this view, in pixels.
9408 */
9409 @ViewDebug.CapturedViewProperty
9410 public final int getTop() {
9411 return mTop;
9412 }
9413
9414 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009415 * Sets the top position of this view relative to its parent. This method is meant to be called
9416 * by the layout system and should not generally be called otherwise, because the property
9417 * may be changed at any time by the layout.
9418 *
9419 * @param top The top of this view, in pixels.
9420 */
9421 public final void setTop(int top) {
9422 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009423 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009424 final boolean matrixIsIdentity = mTransformationInfo == null
9425 || mTransformationInfo.mMatrixIsIdentity;
9426 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009427 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009428 int minTop;
9429 int yLoc;
9430 if (top < mTop) {
9431 minTop = top;
9432 yLoc = top - mTop;
9433 } else {
9434 minTop = mTop;
9435 yLoc = 0;
9436 }
Chet Haasee9140a72011-02-16 16:23:29 -08009437 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009438 }
9439 } else {
9440 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009441 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009442 }
9443
Chet Haaseed032702010-10-01 14:05:54 -07009444 int width = mRight - mLeft;
9445 int oldHeight = mBottom - mTop;
9446
Chet Haase21cd1382010-09-01 17:42:29 -07009447 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009448 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009449 mDisplayList.setTop(mTop);
9450 }
Chet Haase21cd1382010-09-01 17:42:29 -07009451
Chet Haaseed032702010-10-01 14:05:54 -07009452 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9453
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009454 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009455 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009456 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009457 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009458 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009459 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009460 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009461 }
Chet Haase55dbb652010-12-21 20:15:08 -08009462 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009463 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009464 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009465 // View was rejected last time it was drawn by its parent; this may have changed
9466 invalidateParentIfNeeded();
9467 }
Chet Haase21cd1382010-09-01 17:42:29 -07009468 }
9469 }
9470
9471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 * Bottom position of this view relative to its parent.
9473 *
9474 * @return The bottom of this view, in pixels.
9475 */
9476 @ViewDebug.CapturedViewProperty
9477 public final int getBottom() {
9478 return mBottom;
9479 }
9480
9481 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009482 * True if this view has changed since the last time being drawn.
9483 *
9484 * @return The dirty state of this view.
9485 */
9486 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009487 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009488 }
9489
9490 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009491 * Sets the bottom position of this view relative to its parent. This method is meant to be
9492 * called by the layout system and should not generally be called otherwise, because the
9493 * property may be changed at any time by the layout.
9494 *
9495 * @param bottom The bottom of this view, in pixels.
9496 */
9497 public final void setBottom(int bottom) {
9498 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009499 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009500 final boolean matrixIsIdentity = mTransformationInfo == null
9501 || mTransformationInfo.mMatrixIsIdentity;
9502 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009503 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009504 int maxBottom;
9505 if (bottom < mBottom) {
9506 maxBottom = mBottom;
9507 } else {
9508 maxBottom = bottom;
9509 }
Chet Haasee9140a72011-02-16 16:23:29 -08009510 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009511 }
9512 } else {
9513 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009514 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009515 }
9516
Chet Haaseed032702010-10-01 14:05:54 -07009517 int width = mRight - mLeft;
9518 int oldHeight = mBottom - mTop;
9519
Chet Haase21cd1382010-09-01 17:42:29 -07009520 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009521 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009522 mDisplayList.setBottom(mBottom);
9523 }
Chet Haase21cd1382010-09-01 17:42:29 -07009524
Chet Haaseed032702010-10-01 14:05:54 -07009525 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9526
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009527 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009528 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009529 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009530 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009531 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009532 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009533 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009534 }
Chet Haase55dbb652010-12-21 20:15:08 -08009535 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009536 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009537 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009538 // View was rejected last time it was drawn by its parent; this may have changed
9539 invalidateParentIfNeeded();
9540 }
Chet Haase21cd1382010-09-01 17:42:29 -07009541 }
9542 }
9543
9544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 * Left position of this view relative to its parent.
9546 *
9547 * @return The left edge of this view, in pixels.
9548 */
9549 @ViewDebug.CapturedViewProperty
9550 public final int getLeft() {
9551 return mLeft;
9552 }
9553
9554 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009555 * Sets the left position of this view relative to its parent. This method is meant to be called
9556 * by the layout system and should not generally be called otherwise, because the property
9557 * may be changed at any time by the layout.
9558 *
9559 * @param left The bottom of this view, in pixels.
9560 */
9561 public final void setLeft(int left) {
9562 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009563 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009564 final boolean matrixIsIdentity = mTransformationInfo == null
9565 || mTransformationInfo.mMatrixIsIdentity;
9566 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009567 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009568 int minLeft;
9569 int xLoc;
9570 if (left < mLeft) {
9571 minLeft = left;
9572 xLoc = left - mLeft;
9573 } else {
9574 minLeft = mLeft;
9575 xLoc = 0;
9576 }
Chet Haasee9140a72011-02-16 16:23:29 -08009577 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009578 }
9579 } else {
9580 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009581 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009582 }
9583
Chet Haaseed032702010-10-01 14:05:54 -07009584 int oldWidth = mRight - mLeft;
9585 int height = mBottom - mTop;
9586
Chet Haase21cd1382010-09-01 17:42:29 -07009587 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009588 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009589 mDisplayList.setLeft(left);
9590 }
Chet Haase21cd1382010-09-01 17:42:29 -07009591
Chet Haaseed032702010-10-01 14:05:54 -07009592 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9593
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009594 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009595 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009596 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009597 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009598 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009599 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009600 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009601 }
Chet Haase55dbb652010-12-21 20:15:08 -08009602 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009603 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009604 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009605 // View was rejected last time it was drawn by its parent; this may have changed
9606 invalidateParentIfNeeded();
9607 }
Chet Haase21cd1382010-09-01 17:42:29 -07009608 }
9609 }
9610
9611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 * Right position of this view relative to its parent.
9613 *
9614 * @return The right edge of this view, in pixels.
9615 */
9616 @ViewDebug.CapturedViewProperty
9617 public final int getRight() {
9618 return mRight;
9619 }
9620
9621 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009622 * Sets the right position of this view relative to its parent. This method is meant to be called
9623 * by the layout system and should not generally be called otherwise, because the property
9624 * may be changed at any time by the layout.
9625 *
9626 * @param right The bottom of this view, in pixels.
9627 */
9628 public final void setRight(int right) {
9629 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009630 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009631 final boolean matrixIsIdentity = mTransformationInfo == null
9632 || mTransformationInfo.mMatrixIsIdentity;
9633 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009634 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009635 int maxRight;
9636 if (right < mRight) {
9637 maxRight = mRight;
9638 } else {
9639 maxRight = right;
9640 }
Chet Haasee9140a72011-02-16 16:23:29 -08009641 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009642 }
9643 } else {
9644 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009645 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009646 }
9647
Chet Haaseed032702010-10-01 14:05:54 -07009648 int oldWidth = mRight - mLeft;
9649 int height = mBottom - mTop;
9650
Chet Haase21cd1382010-09-01 17:42:29 -07009651 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009652 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009653 mDisplayList.setRight(mRight);
9654 }
Chet Haase21cd1382010-09-01 17:42:29 -07009655
Chet Haaseed032702010-10-01 14:05:54 -07009656 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9657
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009658 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009659 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009660 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009661 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009662 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009663 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009664 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009665 }
Chet Haase55dbb652010-12-21 20:15:08 -08009666 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009667 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009668 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009669 // View was rejected last time it was drawn by its parent; this may have changed
9670 invalidateParentIfNeeded();
9671 }
Chet Haase21cd1382010-09-01 17:42:29 -07009672 }
9673 }
9674
9675 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009676 * The visual x position of this view, in pixels. This is equivalent to the
9677 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009678 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009679 *
Chet Haasedf030d22010-07-30 17:22:38 -07009680 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009681 */
Chet Haasea5531132012-02-02 13:41:44 -08009682 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009683 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009684 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009685 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009686
Chet Haasedf030d22010-07-30 17:22:38 -07009687 /**
9688 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9689 * {@link #setTranslationX(float) translationX} property to be the difference between
9690 * the x value passed in and the current {@link #getLeft() left} property.
9691 *
9692 * @param x The visual x position of this view, in pixels.
9693 */
9694 public void setX(float x) {
9695 setTranslationX(x - mLeft);
9696 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009697
Chet Haasedf030d22010-07-30 17:22:38 -07009698 /**
9699 * The visual y position of this view, in pixels. This is equivalent to the
9700 * {@link #setTranslationY(float) translationY} property plus the current
9701 * {@link #getTop() top} property.
9702 *
9703 * @return The visual y position of this view, in pixels.
9704 */
Chet Haasea5531132012-02-02 13:41:44 -08009705 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009706 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009707 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009708 }
9709
9710 /**
9711 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9712 * {@link #setTranslationY(float) translationY} property to be the difference between
9713 * the y value passed in and the current {@link #getTop() top} property.
9714 *
9715 * @param y The visual y position of this view, in pixels.
9716 */
9717 public void setY(float y) {
9718 setTranslationY(y - mTop);
9719 }
9720
9721
9722 /**
9723 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9724 * This position is post-layout, in addition to wherever the object's
9725 * layout placed it.
9726 *
9727 * @return The horizontal position of this view relative to its left position, in pixels.
9728 */
Chet Haasea5531132012-02-02 13:41:44 -08009729 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009730 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009731 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009732 }
9733
9734 /**
9735 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9736 * This effectively positions the object post-layout, in addition to wherever the object's
9737 * layout placed it.
9738 *
9739 * @param translationX The horizontal position of this view relative to its left position,
9740 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009741 *
9742 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009743 */
9744 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009745 ensureTransformationInfo();
9746 final TransformationInfo info = mTransformationInfo;
9747 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009748 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009749 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009750 info.mTranslationX = translationX;
9751 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009752 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009753 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009754 mDisplayList.setTranslationX(translationX);
9755 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009756 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009757 // View was rejected last time it was drawn by its parent; this may have changed
9758 invalidateParentIfNeeded();
9759 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009760 }
9761 }
9762
9763 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009764 * The horizontal location of this view relative to its {@link #getTop() top} position.
9765 * This position is post-layout, in addition to wherever the object's
9766 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009767 *
Chet Haasedf030d22010-07-30 17:22:38 -07009768 * @return The vertical position of this view relative to its top position,
9769 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009770 */
Chet Haasea5531132012-02-02 13:41:44 -08009771 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009772 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009773 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009774 }
9775
9776 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009777 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9778 * This effectively positions the object post-layout, in addition to wherever the object's
9779 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009780 *
Chet Haasedf030d22010-07-30 17:22:38 -07009781 * @param translationY The vertical position of this view relative to its top position,
9782 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009783 *
9784 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009785 */
Chet Haasedf030d22010-07-30 17:22:38 -07009786 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009787 ensureTransformationInfo();
9788 final TransformationInfo info = mTransformationInfo;
9789 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009790 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009791 info.mTranslationY = translationY;
9792 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009793 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009794 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009795 mDisplayList.setTranslationY(translationY);
9796 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009797 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009798 // View was rejected last time it was drawn by its parent; this may have changed
9799 invalidateParentIfNeeded();
9800 }
Chet Haasedf030d22010-07-30 17:22:38 -07009801 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009802 }
9803
9804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 * Hit rectangle in parent's coordinates
9806 *
9807 * @param outRect The hit rectangle of the view.
9808 */
9809 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009810 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009811 final TransformationInfo info = mTransformationInfo;
9812 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009813 outRect.set(mLeft, mTop, mRight, mBottom);
9814 } else {
9815 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009816 tmpRect.set(-info.mPivotX, -info.mPivotY,
9817 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9818 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009819 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9820 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009821 }
9822 }
9823
9824 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009825 * Determines whether the given point, in local coordinates is inside the view.
9826 */
9827 /*package*/ final boolean pointInView(float localX, float localY) {
9828 return localX >= 0 && localX < (mRight - mLeft)
9829 && localY >= 0 && localY < (mBottom - mTop);
9830 }
9831
9832 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009833 * Utility method to determine whether the given point, in local coordinates,
9834 * is inside the view, where the area of the view is expanded by the slop factor.
9835 * This method is called while processing touch-move events to determine if the event
9836 * is still within the view.
9837 */
9838 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009839 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009840 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 }
9842
9843 /**
9844 * When a view has focus and the user navigates away from it, the next view is searched for
9845 * starting from the rectangle filled in by this method.
9846 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009847 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009848 * of the view. However, if your view maintains some idea of internal selection,
9849 * such as a cursor, or a selected row or column, you should override this method and
9850 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 *
9852 * @param r The rectangle to fill in, in this view's coordinates.
9853 */
9854 public void getFocusedRect(Rect r) {
9855 getDrawingRect(r);
9856 }
9857
9858 /**
9859 * If some part of this view is not clipped by any of its parents, then
9860 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009861 * coordinates (without taking possible View rotations into account), offset
9862 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9863 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864 *
9865 * @param r If true is returned, r holds the global coordinates of the
9866 * visible portion of this view.
9867 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9868 * between this view and its root. globalOffet may be null.
9869 * @return true if r is non-empty (i.e. part of the view is visible at the
9870 * root level.
9871 */
9872 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9873 int width = mRight - mLeft;
9874 int height = mBottom - mTop;
9875 if (width > 0 && height > 0) {
9876 r.set(0, 0, width, height);
9877 if (globalOffset != null) {
9878 globalOffset.set(-mScrollX, -mScrollY);
9879 }
9880 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9881 }
9882 return false;
9883 }
9884
9885 public final boolean getGlobalVisibleRect(Rect r) {
9886 return getGlobalVisibleRect(r, null);
9887 }
9888
9889 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009890 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 if (getGlobalVisibleRect(r, offset)) {
9892 r.offset(-offset.x, -offset.y); // make r local
9893 return true;
9894 }
9895 return false;
9896 }
9897
9898 /**
9899 * Offset this view's vertical location by the specified number of pixels.
9900 *
9901 * @param offset the number of pixels to offset the view by
9902 */
9903 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009904 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009905 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009906 final boolean matrixIsIdentity = mTransformationInfo == null
9907 || mTransformationInfo.mMatrixIsIdentity;
9908 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009909 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009910 invalidateViewProperty(false, false);
9911 } else {
9912 final ViewParent p = mParent;
9913 if (p != null && mAttachInfo != null) {
9914 final Rect r = mAttachInfo.mTmpInvalRect;
9915 int minTop;
9916 int maxBottom;
9917 int yLoc;
9918 if (offset < 0) {
9919 minTop = mTop + offset;
9920 maxBottom = mBottom;
9921 yLoc = offset;
9922 } else {
9923 minTop = mTop;
9924 maxBottom = mBottom + offset;
9925 yLoc = 0;
9926 }
9927 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9928 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009929 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009930 }
9931 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009932 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009933 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009934
Chet Haasec3aa3612010-06-17 08:50:37 -07009935 mTop += offset;
9936 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009937 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009938 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009939 invalidateViewProperty(false, false);
9940 } else {
9941 if (!matrixIsIdentity) {
9942 invalidateViewProperty(false, true);
9943 }
9944 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009945 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 }
9948
9949 /**
9950 * Offset this view's horizontal location by the specified amount of pixels.
9951 *
9952 * @param offset the numer of pixels to offset the view by
9953 */
9954 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009955 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009956 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009957 final boolean matrixIsIdentity = mTransformationInfo == null
9958 || mTransformationInfo.mMatrixIsIdentity;
9959 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009960 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009961 invalidateViewProperty(false, false);
9962 } else {
9963 final ViewParent p = mParent;
9964 if (p != null && mAttachInfo != null) {
9965 final Rect r = mAttachInfo.mTmpInvalRect;
9966 int minLeft;
9967 int maxRight;
9968 if (offset < 0) {
9969 minLeft = mLeft + offset;
9970 maxRight = mRight;
9971 } else {
9972 minLeft = mLeft;
9973 maxRight = mRight + offset;
9974 }
9975 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9976 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009977 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009978 }
9979 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009980 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009981 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009982
Chet Haasec3aa3612010-06-17 08:50:37 -07009983 mLeft += offset;
9984 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009985 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009986 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009987 invalidateViewProperty(false, false);
9988 } else {
9989 if (!matrixIsIdentity) {
9990 invalidateViewProperty(false, true);
9991 }
9992 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009993 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 }
9996
9997 /**
9998 * Get the LayoutParams associated with this view. All views should have
9999 * layout parameters. These supply parameters to the <i>parent</i> of this
10000 * view specifying how it should be arranged. There are many subclasses of
10001 * ViewGroup.LayoutParams, and these correspond to the different subclasses
10002 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -080010003 *
10004 * This method may return null if this View is not attached to a parent
10005 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
10006 * was not invoked successfully. When a View is attached to a parent
10007 * ViewGroup, this method must not return null.
10008 *
10009 * @return The LayoutParams associated with this view, or null if no
10010 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010012 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 public ViewGroup.LayoutParams getLayoutParams() {
10014 return mLayoutParams;
10015 }
10016
10017 /**
10018 * Set the layout parameters associated with this view. These supply
10019 * parameters to the <i>parent</i> of this view specifying how it should be
10020 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10021 * correspond to the different subclasses of ViewGroup that are responsible
10022 * for arranging their children.
10023 *
Romain Guy01c174b2011-02-22 11:51:06 -080010024 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 */
10026 public void setLayoutParams(ViewGroup.LayoutParams params) {
10027 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010028 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 }
10030 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010031 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010032 if (mParent instanceof ViewGroup) {
10033 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010035 requestLayout();
10036 }
10037
10038 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010039 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010040 *
10041 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010042 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010043 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010044 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010045 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010046 }
10047 }
10048
10049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 * Set the scrolled position of your view. This will cause a call to
10051 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10052 * invalidated.
10053 * @param x the x position to scroll to
10054 * @param y the y position to scroll to
10055 */
10056 public void scrollTo(int x, int y) {
10057 if (mScrollX != x || mScrollY != y) {
10058 int oldX = mScrollX;
10059 int oldY = mScrollY;
10060 mScrollX = x;
10061 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010062 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010064 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010065 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 }
10068 }
10069
10070 /**
10071 * Move the scrolled position of your view. This will cause a call to
10072 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10073 * invalidated.
10074 * @param x the amount of pixels to scroll by horizontally
10075 * @param y the amount of pixels to scroll by vertically
10076 */
10077 public void scrollBy(int x, int y) {
10078 scrollTo(mScrollX + x, mScrollY + y);
10079 }
10080
10081 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010082 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10083 * animation to fade the scrollbars out after a default delay. If a subclass
10084 * provides animated scrolling, the start delay should equal the duration
10085 * of the scrolling animation.</p>
10086 *
10087 * <p>The animation starts only if at least one of the scrollbars is
10088 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10089 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10090 * this method returns true, and false otherwise. If the animation is
10091 * started, this method calls {@link #invalidate()}; in that case the
10092 * caller should not call {@link #invalidate()}.</p>
10093 *
10094 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010095 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010096 *
10097 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10098 * and {@link #scrollTo(int, int)}.</p>
10099 *
10100 * @return true if the animation is played, false otherwise
10101 *
10102 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010103 * @see #scrollBy(int, int)
10104 * @see #scrollTo(int, int)
10105 * @see #isHorizontalScrollBarEnabled()
10106 * @see #isVerticalScrollBarEnabled()
10107 * @see #setHorizontalScrollBarEnabled(boolean)
10108 * @see #setVerticalScrollBarEnabled(boolean)
10109 */
10110 protected boolean awakenScrollBars() {
10111 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010112 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010113 }
10114
10115 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010116 * Trigger the scrollbars to draw.
10117 * This method differs from awakenScrollBars() only in its default duration.
10118 * initialAwakenScrollBars() will show the scroll bars for longer than
10119 * usual to give the user more of a chance to notice them.
10120 *
10121 * @return true if the animation is played, false otherwise.
10122 */
10123 private boolean initialAwakenScrollBars() {
10124 return mScrollCache != null &&
10125 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10126 }
10127
10128 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010129 * <p>
10130 * Trigger the scrollbars to draw. When invoked this method starts an
10131 * animation to fade the scrollbars out after a fixed delay. If a subclass
10132 * provides animated scrolling, the start delay should equal the duration of
10133 * the scrolling animation.
10134 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010135 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010136 * <p>
10137 * The animation starts only if at least one of the scrollbars is enabled,
10138 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10139 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10140 * this method returns true, and false otherwise. If the animation is
10141 * started, this method calls {@link #invalidate()}; in that case the caller
10142 * should not call {@link #invalidate()}.
10143 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010144 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010145 * <p>
10146 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010147 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010148 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010149 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010150 * @param startDelay the delay, in milliseconds, after which the animation
10151 * should start; when the delay is 0, the animation starts
10152 * immediately
10153 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010154 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010155 * @see #scrollBy(int, int)
10156 * @see #scrollTo(int, int)
10157 * @see #isHorizontalScrollBarEnabled()
10158 * @see #isVerticalScrollBarEnabled()
10159 * @see #setHorizontalScrollBarEnabled(boolean)
10160 * @see #setVerticalScrollBarEnabled(boolean)
10161 */
10162 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010163 return awakenScrollBars(startDelay, true);
10164 }
Joe Malin32736f02011-01-19 16:14:20 -080010165
Mike Cleron290947b2009-09-29 18:34:32 -070010166 /**
10167 * <p>
10168 * Trigger the scrollbars to draw. When invoked this method starts an
10169 * animation to fade the scrollbars out after a fixed delay. If a subclass
10170 * provides animated scrolling, the start delay should equal the duration of
10171 * the scrolling animation.
10172 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010173 *
Mike Cleron290947b2009-09-29 18:34:32 -070010174 * <p>
10175 * The animation starts only if at least one of the scrollbars is enabled,
10176 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10177 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10178 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010179 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010180 * is set to true; in that case the caller
10181 * should not call {@link #invalidate()}.
10182 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010183 *
Mike Cleron290947b2009-09-29 18:34:32 -070010184 * <p>
10185 * This method should be invoked everytime a subclass directly updates the
10186 * scroll parameters.
10187 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010188 *
Mike Cleron290947b2009-09-29 18:34:32 -070010189 * @param startDelay the delay, in milliseconds, after which the animation
10190 * should start; when the delay is 0, the animation starts
10191 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010192 *
Mike Cleron290947b2009-09-29 18:34:32 -070010193 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010194 *
Mike Cleron290947b2009-09-29 18:34:32 -070010195 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010196 *
Mike Cleron290947b2009-09-29 18:34:32 -070010197 * @see #scrollBy(int, int)
10198 * @see #scrollTo(int, int)
10199 * @see #isHorizontalScrollBarEnabled()
10200 * @see #isVerticalScrollBarEnabled()
10201 * @see #setHorizontalScrollBarEnabled(boolean)
10202 * @see #setVerticalScrollBarEnabled(boolean)
10203 */
10204 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010205 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010206
Mike Cleronf116bf82009-09-27 19:14:12 -070010207 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10208 return false;
10209 }
10210
10211 if (scrollCache.scrollBar == null) {
10212 scrollCache.scrollBar = new ScrollBarDrawable();
10213 }
10214
10215 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10216
Mike Cleron290947b2009-09-29 18:34:32 -070010217 if (invalidate) {
10218 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010219 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010220 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010221
10222 if (scrollCache.state == ScrollabilityCache.OFF) {
10223 // FIXME: this is copied from WindowManagerService.
10224 // We should get this value from the system when it
10225 // is possible to do so.
10226 final int KEY_REPEAT_FIRST_DELAY = 750;
10227 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10228 }
10229
10230 // Tell mScrollCache when we should start fading. This may
10231 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010232 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010233 scrollCache.fadeStartTime = fadeStartTime;
10234 scrollCache.state = ScrollabilityCache.ON;
10235
10236 // Schedule our fader to run, unscheduling any old ones first
10237 if (mAttachInfo != null) {
10238 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10239 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10240 }
10241
10242 return true;
10243 }
10244
10245 return false;
10246 }
10247
10248 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010249 * Do not invalidate views which are not visible and which are not running an animation. They
10250 * will not get drawn and they should not set dirty flags as if they will be drawn
10251 */
10252 private boolean skipInvalidate() {
10253 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10254 (!(mParent instanceof ViewGroup) ||
10255 !((ViewGroup) mParent).isViewTransitioning(this));
10256 }
10257 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010258 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010259 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10260 * in the future. This must be called from a UI thread. To call from a non-UI
10261 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010262 *
10263 * WARNING: This method is destructive to dirty.
10264 * @param dirty the rectangle representing the bounds of the dirty region
10265 */
10266 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010267 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010268 return;
10269 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010270 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10271 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10272 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10273 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10274 mPrivateFlags |= PFLAG_INVALIDATED;
10275 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 final ViewParent p = mParent;
10277 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010278 //noinspection PointlessBooleanExpression,ConstantConditions
10279 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10280 if (p != null && ai != null && ai.mHardwareAccelerated) {
10281 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010282 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010283 p.invalidateChild(this, null);
10284 return;
10285 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 if (p != null && ai != null) {
10288 final int scrollX = mScrollX;
10289 final int scrollY = mScrollY;
10290 final Rect r = ai.mTmpInvalRect;
10291 r.set(dirty.left - scrollX, dirty.top - scrollY,
10292 dirty.right - scrollX, dirty.bottom - scrollY);
10293 mParent.invalidateChild(this, r);
10294 }
10295 }
10296 }
10297
10298 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010299 * 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 -080010300 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010301 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10302 * will be called at some point in the future. This must be called from
10303 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 * @param l the left position of the dirty region
10305 * @param t the top position of the dirty region
10306 * @param r the right position of the dirty region
10307 * @param b the bottom position of the dirty region
10308 */
10309 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010310 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010311 return;
10312 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010313 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10314 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10315 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10316 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10317 mPrivateFlags |= PFLAG_INVALIDATED;
10318 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 final ViewParent p = mParent;
10320 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010321 //noinspection PointlessBooleanExpression,ConstantConditions
10322 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10323 if (p != null && ai != null && ai.mHardwareAccelerated) {
10324 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010325 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010326 p.invalidateChild(this, null);
10327 return;
10328 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 if (p != null && ai != null && l < r && t < b) {
10331 final int scrollX = mScrollX;
10332 final int scrollY = mScrollY;
10333 final Rect tmpr = ai.mTmpInvalRect;
10334 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10335 p.invalidateChild(this, tmpr);
10336 }
10337 }
10338 }
10339
10340 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010341 * Invalidate the whole view. If the view is visible,
10342 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10343 * the future. This must be called from a UI thread. To call from a non-UI thread,
10344 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 */
10346 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010347 invalidate(true);
10348 }
Joe Malin32736f02011-01-19 16:14:20 -080010349
Chet Haaseed032702010-10-01 14:05:54 -070010350 /**
10351 * This is where the invalidate() work actually happens. A full invalidate()
10352 * causes the drawing cache to be invalidated, but this function can be called with
10353 * invalidateCache set to false to skip that invalidation step for cases that do not
10354 * need it (for example, a component that remains at the same dimensions with the same
10355 * content).
10356 *
10357 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10358 * well. This is usually true for a full invalidate, but may be set to false if the
10359 * View's contents or dimensions have not changed.
10360 */
Romain Guy849d0a32011-02-01 17:20:48 -080010361 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010362 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010363 return;
10364 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010365 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10366 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10367 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010368 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010369 mPrivateFlags &= ~PFLAG_DRAWN;
10370 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010371 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010372 mPrivateFlags |= PFLAG_INVALIDATED;
10373 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010376 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010377 //noinspection PointlessBooleanExpression,ConstantConditions
10378 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10379 if (p != null && ai != null && ai.mHardwareAccelerated) {
10380 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010381 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010382 p.invalidateChild(this, null);
10383 return;
10384 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010385 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 if (p != null && ai != null) {
10388 final Rect r = ai.mTmpInvalRect;
10389 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10390 // Don't call invalidate -- we don't want to internally scroll
10391 // our own bounds
10392 p.invalidateChild(this, r);
10393 }
10394 }
10395 }
10396
10397 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010398 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10399 * set any flags or handle all of the cases handled by the default invalidation methods.
10400 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10401 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10402 * walk up the hierarchy, transforming the dirty rect as necessary.
10403 *
10404 * The method also handles normal invalidation logic if display list properties are not
10405 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10406 * backup approach, to handle these cases used in the various property-setting methods.
10407 *
10408 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10409 * are not being used in this view
10410 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10411 * list properties are not being used in this view
10412 */
10413 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010414 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010415 if (invalidateParent) {
10416 invalidateParentCaches();
10417 }
10418 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010419 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010420 }
10421 invalidate(false);
10422 } else {
10423 final AttachInfo ai = mAttachInfo;
10424 final ViewParent p = mParent;
10425 if (p != null && ai != null) {
10426 final Rect r = ai.mTmpInvalRect;
10427 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10428 if (mParent instanceof ViewGroup) {
10429 ((ViewGroup) mParent).invalidateChildFast(this, r);
10430 } else {
10431 mParent.invalidateChild(this, r);
10432 }
10433 }
10434 }
10435 }
10436
10437 /**
10438 * Utility method to transform a given Rect by the current matrix of this view.
10439 */
10440 void transformRect(final Rect rect) {
10441 if (!getMatrix().isIdentity()) {
10442 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10443 boundingRect.set(rect);
10444 getMatrix().mapRect(boundingRect);
10445 rect.set((int) (boundingRect.left - 0.5f),
10446 (int) (boundingRect.top - 0.5f),
10447 (int) (boundingRect.right + 0.5f),
10448 (int) (boundingRect.bottom + 0.5f));
10449 }
10450 }
10451
10452 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010453 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010454 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10455 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010456 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10457 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010458 *
10459 * @hide
10460 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010461 protected void invalidateParentCaches() {
10462 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010463 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010464 }
10465 }
Joe Malin32736f02011-01-19 16:14:20 -080010466
Romain Guy0fd89bf2011-01-26 15:41:30 -080010467 /**
10468 * Used to indicate that the parent of this view should be invalidated. This functionality
10469 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10470 * which is necessary when various parent-managed properties of the view change, such as
10471 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10472 * an invalidation event to the parent.
10473 *
10474 * @hide
10475 */
10476 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010477 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010478 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010479 }
10480 }
10481
10482 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010483 * Indicates whether this View is opaque. An opaque View guarantees that it will
10484 * draw all the pixels overlapping its bounds using a fully opaque color.
10485 *
10486 * Subclasses of View should override this method whenever possible to indicate
10487 * whether an instance is opaque. Opaque Views are treated in a special way by
10488 * the View hierarchy, possibly allowing it to perform optimizations during
10489 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010490 *
Romain Guy24443ea2009-05-11 11:56:30 -070010491 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010492 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010493 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010494 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010495 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010496 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010497 }
10498
Adam Powell20232d02010-12-08 21:08:53 -080010499 /**
10500 * @hide
10501 */
10502 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010503 // Opaque if:
10504 // - Has a background
10505 // - Background is opaque
10506 // - Doesn't have scrollbars or scrollbars are inside overlay
10507
Philip Milne6c8ea062012-04-03 17:38:43 -070010508 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010509 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010510 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010511 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010512 }
10513
10514 final int flags = mViewFlags;
10515 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10516 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010517 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010518 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010519 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010520 }
10521 }
10522
10523 /**
10524 * @hide
10525 */
10526 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010527 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010528 }
10529
10530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010531 * @return A handler associated with the thread running the View. This
10532 * handler can be used to pump events in the UI events queue.
10533 */
10534 public Handler getHandler() {
10535 if (mAttachInfo != null) {
10536 return mAttachInfo.mHandler;
10537 }
10538 return null;
10539 }
10540
10541 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010542 * Gets the view root associated with the View.
10543 * @return The view root, or null if none.
10544 * @hide
10545 */
10546 public ViewRootImpl getViewRootImpl() {
10547 if (mAttachInfo != null) {
10548 return mAttachInfo.mViewRootImpl;
10549 }
10550 return null;
10551 }
10552
10553 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010554 * <p>Causes the Runnable to be added to the message queue.
10555 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 * @param action The Runnable that will be executed.
10558 *
10559 * @return Returns true if the Runnable was successfully placed in to the
10560 * message queue. Returns false on failure, usually because the
10561 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010562 *
10563 * @see #postDelayed
10564 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 */
10566 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010567 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010568 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010569 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010571 // Assume that post will succeed later
10572 ViewRootImpl.getRunQueue().post(action);
10573 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 }
10575
10576 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010577 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010579 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010580 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 * @param action The Runnable that will be executed.
10582 * @param delayMillis The delay (in milliseconds) until the Runnable
10583 * will be executed.
10584 *
10585 * @return true if the Runnable was successfully placed in to the
10586 * message queue. Returns false on failure, usually because the
10587 * looper processing the message queue is exiting. Note that a
10588 * result of true does not mean the Runnable will be processed --
10589 * if the looper is quit before the delivery time of the message
10590 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010591 *
10592 * @see #post
10593 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 */
10595 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010596 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010597 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010598 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010600 // Assume that post will succeed later
10601 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10602 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 }
10604
10605 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010606 * <p>Causes the Runnable to execute on the next animation time step.
10607 * The runnable will be run on the user interface thread.</p>
10608 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010609 * @param action The Runnable that will be executed.
10610 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010611 * @see #postOnAnimationDelayed
10612 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010613 */
10614 public void postOnAnimation(Runnable action) {
10615 final AttachInfo attachInfo = mAttachInfo;
10616 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010617 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10618 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010619 } else {
10620 // Assume that post will succeed later
10621 ViewRootImpl.getRunQueue().post(action);
10622 }
10623 }
10624
10625 /**
10626 * <p>Causes the Runnable to execute on the next animation time step,
10627 * after the specified amount of time elapses.
10628 * The runnable will be run on the user interface thread.</p>
10629 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010630 * @param action The Runnable that will be executed.
10631 * @param delayMillis The delay (in milliseconds) until the Runnable
10632 * will be executed.
10633 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010634 * @see #postOnAnimation
10635 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010636 */
10637 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10638 final AttachInfo attachInfo = mAttachInfo;
10639 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010640 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10641 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010642 } else {
10643 // Assume that post will succeed later
10644 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10645 }
10646 }
10647
10648 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010649 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 * @param action The Runnable to remove from the message handling queue
10652 *
10653 * @return true if this view could ask the Handler to remove the Runnable,
10654 * false otherwise. When the returned value is true, the Runnable
10655 * may or may not have been actually removed from the message queue
10656 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010657 *
10658 * @see #post
10659 * @see #postDelayed
10660 * @see #postOnAnimation
10661 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 */
10663 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010664 if (action != null) {
10665 final AttachInfo attachInfo = mAttachInfo;
10666 if (attachInfo != null) {
10667 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010668 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10669 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010670 } else {
10671 // Assume that post will succeed later
10672 ViewRootImpl.getRunQueue().removeCallbacks(action);
10673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 return true;
10676 }
10677
10678 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010679 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10680 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 *
Romain Guye63a4f32011-08-11 11:33:31 -070010682 * <p>This method can be invoked from outside of the UI thread
10683 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010684 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010685 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010686 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 */
10688 public void postInvalidate() {
10689 postInvalidateDelayed(0);
10690 }
10691
10692 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010693 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10694 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010695 *
Romain Guye63a4f32011-08-11 11:33:31 -070010696 * <p>This method can be invoked from outside of the UI thread
10697 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 *
10699 * @param left The left coordinate of the rectangle to invalidate.
10700 * @param top The top coordinate of the rectangle to invalidate.
10701 * @param right The right coordinate of the rectangle to invalidate.
10702 * @param bottom The bottom coordinate of the rectangle to invalidate.
10703 *
10704 * @see #invalidate(int, int, int, int)
10705 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010706 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 */
10708 public void postInvalidate(int left, int top, int right, int bottom) {
10709 postInvalidateDelayed(0, left, top, right, bottom);
10710 }
10711
10712 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010713 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10714 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010715 *
Romain Guye63a4f32011-08-11 11:33:31 -070010716 * <p>This method can be invoked from outside of the UI thread
10717 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010718 *
10719 * @param delayMilliseconds the duration in milliseconds to delay the
10720 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010721 *
10722 * @see #invalidate()
10723 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010724 */
10725 public void postInvalidateDelayed(long delayMilliseconds) {
10726 // We try only with the AttachInfo because there's no point in invalidating
10727 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010728 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010729 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010730 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 }
10732 }
10733
10734 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010735 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10736 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010737 *
Romain Guye63a4f32011-08-11 11:33:31 -070010738 * <p>This method can be invoked from outside of the UI thread
10739 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 *
10741 * @param delayMilliseconds the duration in milliseconds to delay the
10742 * invalidation by
10743 * @param left The left coordinate of the rectangle to invalidate.
10744 * @param top The top coordinate of the rectangle to invalidate.
10745 * @param right The right coordinate of the rectangle to invalidate.
10746 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010747 *
10748 * @see #invalidate(int, int, int, int)
10749 * @see #invalidate(Rect)
10750 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 */
10752 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10753 int right, int bottom) {
10754
10755 // We try only with the AttachInfo because there's no point in invalidating
10756 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010757 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010758 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010759 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 info.target = this;
10761 info.left = left;
10762 info.top = top;
10763 info.right = right;
10764 info.bottom = bottom;
10765
Jeff Browna175a5b2012-02-15 19:18:31 -080010766 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010767 }
10768 }
10769
10770 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010771 * <p>Cause an invalidate to happen on the next animation time step, typically the
10772 * next display frame.</p>
10773 *
10774 * <p>This method can be invoked from outside of the UI thread
10775 * only when this View is attached to a window.</p>
10776 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010777 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010778 */
10779 public void postInvalidateOnAnimation() {
10780 // We try only with the AttachInfo because there's no point in invalidating
10781 // if we are not attached to our window
10782 final AttachInfo attachInfo = mAttachInfo;
10783 if (attachInfo != null) {
10784 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10785 }
10786 }
10787
10788 /**
10789 * <p>Cause an invalidate of the specified area to happen on the next animation
10790 * time step, typically the next display frame.</p>
10791 *
10792 * <p>This method can be invoked from outside of the UI thread
10793 * only when this View is attached to a window.</p>
10794 *
10795 * @param left The left coordinate of the rectangle to invalidate.
10796 * @param top The top coordinate of the rectangle to invalidate.
10797 * @param right The right coordinate of the rectangle to invalidate.
10798 * @param bottom The bottom coordinate of the rectangle to invalidate.
10799 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010800 * @see #invalidate(int, int, int, int)
10801 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010802 */
10803 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10804 // We try only with the AttachInfo because there's no point in invalidating
10805 // if we are not attached to our window
10806 final AttachInfo attachInfo = mAttachInfo;
10807 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010808 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010809 info.target = this;
10810 info.left = left;
10811 info.top = top;
10812 info.right = right;
10813 info.bottom = bottom;
10814
10815 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10816 }
10817 }
10818
10819 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010820 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10821 * This event is sent at most once every
10822 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10823 */
10824 private void postSendViewScrolledAccessibilityEventCallback() {
10825 if (mSendViewScrolledAccessibilityEvent == null) {
10826 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10827 }
10828 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10829 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10830 postDelayed(mSendViewScrolledAccessibilityEvent,
10831 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10832 }
10833 }
10834
10835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 * Called by a parent to request that a child update its values for mScrollX
10837 * and mScrollY if necessary. This will typically be done if the child is
10838 * animating a scroll using a {@link android.widget.Scroller Scroller}
10839 * object.
10840 */
10841 public void computeScroll() {
10842 }
10843
10844 /**
10845 * <p>Indicate whether the horizontal edges are faded when the view is
10846 * scrolled horizontally.</p>
10847 *
10848 * @return true if the horizontal edges should are faded on scroll, false
10849 * otherwise
10850 *
10851 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010852 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010853 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 */
10855 public boolean isHorizontalFadingEdgeEnabled() {
10856 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10857 }
10858
10859 /**
10860 * <p>Define whether the horizontal edges should be faded when this view
10861 * is scrolled horizontally.</p>
10862 *
10863 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10864 * be faded when the view is scrolled
10865 * horizontally
10866 *
10867 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010868 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010869 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 */
10871 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10872 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10873 if (horizontalFadingEdgeEnabled) {
10874 initScrollCache();
10875 }
10876
10877 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10878 }
10879 }
10880
10881 /**
10882 * <p>Indicate whether the vertical edges are faded when the view is
10883 * scrolled horizontally.</p>
10884 *
10885 * @return true if the vertical edges should are faded on scroll, false
10886 * otherwise
10887 *
10888 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010889 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010890 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010891 */
10892 public boolean isVerticalFadingEdgeEnabled() {
10893 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10894 }
10895
10896 /**
10897 * <p>Define whether the vertical edges should be faded when this view
10898 * is scrolled vertically.</p>
10899 *
10900 * @param verticalFadingEdgeEnabled true if the vertical edges should
10901 * be faded when the view is scrolled
10902 * vertically
10903 *
10904 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010905 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010906 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 */
10908 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10909 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10910 if (verticalFadingEdgeEnabled) {
10911 initScrollCache();
10912 }
10913
10914 mViewFlags ^= FADING_EDGE_VERTICAL;
10915 }
10916 }
10917
10918 /**
10919 * Returns the strength, or intensity, of the top faded edge. The strength is
10920 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10921 * returns 0.0 or 1.0 but no value in between.
10922 *
10923 * Subclasses should override this method to provide a smoother fade transition
10924 * when scrolling occurs.
10925 *
10926 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10927 */
10928 protected float getTopFadingEdgeStrength() {
10929 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10930 }
10931
10932 /**
10933 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10934 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10935 * returns 0.0 or 1.0 but no value in between.
10936 *
10937 * Subclasses should override this method to provide a smoother fade transition
10938 * when scrolling occurs.
10939 *
10940 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10941 */
10942 protected float getBottomFadingEdgeStrength() {
10943 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10944 computeVerticalScrollRange() ? 1.0f : 0.0f;
10945 }
10946
10947 /**
10948 * Returns the strength, or intensity, of the left faded edge. The strength is
10949 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10950 * returns 0.0 or 1.0 but no value in between.
10951 *
10952 * Subclasses should override this method to provide a smoother fade transition
10953 * when scrolling occurs.
10954 *
10955 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10956 */
10957 protected float getLeftFadingEdgeStrength() {
10958 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10959 }
10960
10961 /**
10962 * Returns the strength, or intensity, of the right faded edge. The strength is
10963 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10964 * returns 0.0 or 1.0 but no value in between.
10965 *
10966 * Subclasses should override this method to provide a smoother fade transition
10967 * when scrolling occurs.
10968 *
10969 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10970 */
10971 protected float getRightFadingEdgeStrength() {
10972 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10973 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10974 }
10975
10976 /**
10977 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10978 * scrollbar is not drawn by default.</p>
10979 *
10980 * @return true if the horizontal scrollbar should be painted, false
10981 * otherwise
10982 *
10983 * @see #setHorizontalScrollBarEnabled(boolean)
10984 */
10985 public boolean isHorizontalScrollBarEnabled() {
10986 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10987 }
10988
10989 /**
10990 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10991 * scrollbar is not drawn by default.</p>
10992 *
10993 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10994 * be painted
10995 *
10996 * @see #isHorizontalScrollBarEnabled()
10997 */
10998 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10999 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
11000 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011001 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011002 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 }
11004 }
11005
11006 /**
11007 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
11008 * scrollbar is not drawn by default.</p>
11009 *
11010 * @return true if the vertical scrollbar should be painted, false
11011 * otherwise
11012 *
11013 * @see #setVerticalScrollBarEnabled(boolean)
11014 */
11015 public boolean isVerticalScrollBarEnabled() {
11016 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11017 }
11018
11019 /**
11020 * <p>Define whether the vertical scrollbar should be drawn or not. The
11021 * scrollbar is not drawn by default.</p>
11022 *
11023 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11024 * be painted
11025 *
11026 * @see #isVerticalScrollBarEnabled()
11027 */
11028 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11029 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11030 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011031 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011032 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033 }
11034 }
11035
Adam Powell20232d02010-12-08 21:08:53 -080011036 /**
11037 * @hide
11038 */
11039 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011040 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011041 }
Joe Malin32736f02011-01-19 16:14:20 -080011042
Mike Cleronfe81d382009-09-28 14:22:16 -070011043 /**
11044 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011045 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011046 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011047 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011048 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011049 */
11050 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11051 initScrollCache();
11052 final ScrollabilityCache scrollabilityCache = mScrollCache;
11053 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011054 if (fadeScrollbars) {
11055 scrollabilityCache.state = ScrollabilityCache.OFF;
11056 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011057 scrollabilityCache.state = ScrollabilityCache.ON;
11058 }
11059 }
Joe Malin32736f02011-01-19 16:14:20 -080011060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061 /**
Joe Malin32736f02011-01-19 16:14:20 -080011062 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011063 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011064 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011065 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011066 *
11067 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011068 */
11069 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011070 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011071 }
Joe Malin32736f02011-01-19 16:14:20 -080011072
Mike Cleron52f0a642009-09-28 18:21:37 -070011073 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011074 *
11075 * Returns the delay before scrollbars fade.
11076 *
11077 * @return the delay before scrollbars fade
11078 *
11079 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11080 */
11081 public int getScrollBarDefaultDelayBeforeFade() {
11082 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11083 mScrollCache.scrollBarDefaultDelayBeforeFade;
11084 }
11085
11086 /**
11087 * Define the delay before scrollbars fade.
11088 *
11089 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11090 *
11091 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11092 */
11093 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11094 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11095 }
11096
11097 /**
11098 *
11099 * Returns the scrollbar fade duration.
11100 *
11101 * @return the scrollbar fade duration
11102 *
11103 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11104 */
11105 public int getScrollBarFadeDuration() {
11106 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11107 mScrollCache.scrollBarFadeDuration;
11108 }
11109
11110 /**
11111 * Define the scrollbar fade duration.
11112 *
11113 * @param scrollBarFadeDuration - the scrollbar fade duration
11114 *
11115 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11116 */
11117 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11118 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11119 }
11120
11121 /**
11122 *
11123 * Returns the scrollbar size.
11124 *
11125 * @return the scrollbar size
11126 *
11127 * @attr ref android.R.styleable#View_scrollbarSize
11128 */
11129 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011130 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011131 mScrollCache.scrollBarSize;
11132 }
11133
11134 /**
11135 * Define the scrollbar size.
11136 *
11137 * @param scrollBarSize - the scrollbar size
11138 *
11139 * @attr ref android.R.styleable#View_scrollbarSize
11140 */
11141 public void setScrollBarSize(int scrollBarSize) {
11142 getScrollCache().scrollBarSize = scrollBarSize;
11143 }
11144
11145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011146 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11147 * inset. When inset, they add to the padding of the view. And the scrollbars
11148 * can be drawn inside the padding area or on the edge of the view. For example,
11149 * if a view has a background drawable and you want to draw the scrollbars
11150 * inside the padding specified by the drawable, you can use
11151 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11152 * appear at the edge of the view, ignoring the padding, then you can use
11153 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11154 * @param style the style of the scrollbars. Should be one of
11155 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11156 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11157 * @see #SCROLLBARS_INSIDE_OVERLAY
11158 * @see #SCROLLBARS_INSIDE_INSET
11159 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11160 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011161 *
11162 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 */
11164 public void setScrollBarStyle(int style) {
11165 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11166 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011167 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011168 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 }
11170 }
11171
11172 /**
11173 * <p>Returns the current scrollbar style.</p>
11174 * @return the current scrollbar style
11175 * @see #SCROLLBARS_INSIDE_OVERLAY
11176 * @see #SCROLLBARS_INSIDE_INSET
11177 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11178 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011179 *
11180 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011182 @ViewDebug.ExportedProperty(mapping = {
11183 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11184 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11185 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11186 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11187 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 public int getScrollBarStyle() {
11189 return mViewFlags & SCROLLBARS_STYLE_MASK;
11190 }
11191
11192 /**
11193 * <p>Compute the horizontal range that the horizontal scrollbar
11194 * represents.</p>
11195 *
11196 * <p>The range is expressed in arbitrary units that must be the same as the
11197 * units used by {@link #computeHorizontalScrollExtent()} and
11198 * {@link #computeHorizontalScrollOffset()}.</p>
11199 *
11200 * <p>The default range is the drawing width of this view.</p>
11201 *
11202 * @return the total horizontal range represented by the horizontal
11203 * scrollbar
11204 *
11205 * @see #computeHorizontalScrollExtent()
11206 * @see #computeHorizontalScrollOffset()
11207 * @see android.widget.ScrollBarDrawable
11208 */
11209 protected int computeHorizontalScrollRange() {
11210 return getWidth();
11211 }
11212
11213 /**
11214 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11215 * within the horizontal range. This value is used to compute the position
11216 * of the thumb within the scrollbar's track.</p>
11217 *
11218 * <p>The range is expressed in arbitrary units that must be the same as the
11219 * units used by {@link #computeHorizontalScrollRange()} and
11220 * {@link #computeHorizontalScrollExtent()}.</p>
11221 *
11222 * <p>The default offset is the scroll offset of this view.</p>
11223 *
11224 * @return the horizontal offset of the scrollbar's thumb
11225 *
11226 * @see #computeHorizontalScrollRange()
11227 * @see #computeHorizontalScrollExtent()
11228 * @see android.widget.ScrollBarDrawable
11229 */
11230 protected int computeHorizontalScrollOffset() {
11231 return mScrollX;
11232 }
11233
11234 /**
11235 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11236 * within the horizontal range. This value is used to compute the length
11237 * of the thumb within the scrollbar's track.</p>
11238 *
11239 * <p>The range is expressed in arbitrary units that must be the same as the
11240 * units used by {@link #computeHorizontalScrollRange()} and
11241 * {@link #computeHorizontalScrollOffset()}.</p>
11242 *
11243 * <p>The default extent is the drawing width of this view.</p>
11244 *
11245 * @return the horizontal extent of the scrollbar's thumb
11246 *
11247 * @see #computeHorizontalScrollRange()
11248 * @see #computeHorizontalScrollOffset()
11249 * @see android.widget.ScrollBarDrawable
11250 */
11251 protected int computeHorizontalScrollExtent() {
11252 return getWidth();
11253 }
11254
11255 /**
11256 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11257 *
11258 * <p>The range is expressed in arbitrary units that must be the same as the
11259 * units used by {@link #computeVerticalScrollExtent()} and
11260 * {@link #computeVerticalScrollOffset()}.</p>
11261 *
11262 * @return the total vertical range represented by the vertical scrollbar
11263 *
11264 * <p>The default range is the drawing height of this view.</p>
11265 *
11266 * @see #computeVerticalScrollExtent()
11267 * @see #computeVerticalScrollOffset()
11268 * @see android.widget.ScrollBarDrawable
11269 */
11270 protected int computeVerticalScrollRange() {
11271 return getHeight();
11272 }
11273
11274 /**
11275 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11276 * within the horizontal range. This value is used to compute the position
11277 * of the thumb within the scrollbar's track.</p>
11278 *
11279 * <p>The range is expressed in arbitrary units that must be the same as the
11280 * units used by {@link #computeVerticalScrollRange()} and
11281 * {@link #computeVerticalScrollExtent()}.</p>
11282 *
11283 * <p>The default offset is the scroll offset of this view.</p>
11284 *
11285 * @return the vertical offset of the scrollbar's thumb
11286 *
11287 * @see #computeVerticalScrollRange()
11288 * @see #computeVerticalScrollExtent()
11289 * @see android.widget.ScrollBarDrawable
11290 */
11291 protected int computeVerticalScrollOffset() {
11292 return mScrollY;
11293 }
11294
11295 /**
11296 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11297 * within the vertical range. This value is used to compute the length
11298 * of the thumb within the scrollbar's track.</p>
11299 *
11300 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011301 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011302 * {@link #computeVerticalScrollOffset()}.</p>
11303 *
11304 * <p>The default extent is the drawing height of this view.</p>
11305 *
11306 * @return the vertical extent of the scrollbar's thumb
11307 *
11308 * @see #computeVerticalScrollRange()
11309 * @see #computeVerticalScrollOffset()
11310 * @see android.widget.ScrollBarDrawable
11311 */
11312 protected int computeVerticalScrollExtent() {
11313 return getHeight();
11314 }
11315
11316 /**
Adam Powell69159442011-06-13 17:53:06 -070011317 * Check if this view can be scrolled horizontally in a certain direction.
11318 *
11319 * @param direction Negative to check scrolling left, positive to check scrolling right.
11320 * @return true if this view can be scrolled in the specified direction, false otherwise.
11321 */
11322 public boolean canScrollHorizontally(int direction) {
11323 final int offset = computeHorizontalScrollOffset();
11324 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11325 if (range == 0) return false;
11326 if (direction < 0) {
11327 return offset > 0;
11328 } else {
11329 return offset < range - 1;
11330 }
11331 }
11332
11333 /**
11334 * Check if this view can be scrolled vertically in a certain direction.
11335 *
11336 * @param direction Negative to check scrolling up, positive to check scrolling down.
11337 * @return true if this view can be scrolled in the specified direction, false otherwise.
11338 */
11339 public boolean canScrollVertically(int direction) {
11340 final int offset = computeVerticalScrollOffset();
11341 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11342 if (range == 0) return false;
11343 if (direction < 0) {
11344 return offset > 0;
11345 } else {
11346 return offset < range - 1;
11347 }
11348 }
11349
11350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011351 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11352 * scrollbars are painted only if they have been awakened first.</p>
11353 *
11354 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011355 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011356 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011357 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011358 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011359 // scrollbars are drawn only when the animation is running
11360 final ScrollabilityCache cache = mScrollCache;
11361 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011362
Mike Cleronf116bf82009-09-27 19:14:12 -070011363 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011364
Mike Cleronf116bf82009-09-27 19:14:12 -070011365 if (state == ScrollabilityCache.OFF) {
11366 return;
11367 }
Joe Malin32736f02011-01-19 16:14:20 -080011368
Mike Cleronf116bf82009-09-27 19:14:12 -070011369 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011370
Mike Cleronf116bf82009-09-27 19:14:12 -070011371 if (state == ScrollabilityCache.FADING) {
11372 // We're fading -- get our fade interpolation
11373 if (cache.interpolatorValues == null) {
11374 cache.interpolatorValues = new float[1];
11375 }
Joe Malin32736f02011-01-19 16:14:20 -080011376
Mike Cleronf116bf82009-09-27 19:14:12 -070011377 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011378
Mike Cleronf116bf82009-09-27 19:14:12 -070011379 // Stops the animation if we're done
11380 if (cache.scrollBarInterpolator.timeToValues(values) ==
11381 Interpolator.Result.FREEZE_END) {
11382 cache.state = ScrollabilityCache.OFF;
11383 } else {
11384 cache.scrollBar.setAlpha(Math.round(values[0]));
11385 }
Joe Malin32736f02011-01-19 16:14:20 -080011386
11387 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011388 // drawing. We only want this when we're fading so that
11389 // we prevent excessive redraws
11390 invalidate = true;
11391 } else {
11392 // We're just on -- but we may have been fading before so
11393 // reset alpha
11394 cache.scrollBar.setAlpha(255);
11395 }
11396
Joe Malin32736f02011-01-19 16:14:20 -080011397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011398 final int viewFlags = mViewFlags;
11399
11400 final boolean drawHorizontalScrollBar =
11401 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11402 final boolean drawVerticalScrollBar =
11403 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11404 && !isVerticalScrollBarHidden();
11405
11406 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11407 final int width = mRight - mLeft;
11408 final int height = mBottom - mTop;
11409
11410 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011411
Mike Reede8853fc2009-09-04 14:01:48 -040011412 final int scrollX = mScrollX;
11413 final int scrollY = mScrollY;
11414 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11415
Mike Cleronf116bf82009-09-27 19:14:12 -070011416 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011418 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011419 int size = scrollBar.getSize(false);
11420 if (size <= 0) {
11421 size = cache.scrollBarSize;
11422 }
11423
Mike Cleronf116bf82009-09-27 19:14:12 -070011424 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011425 computeHorizontalScrollOffset(),
11426 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011427 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011428 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011429 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011430 left = scrollX + (mPaddingLeft & inside);
11431 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11432 bottom = top + size;
11433 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11434 if (invalidate) {
11435 invalidate(left, top, right, bottom);
11436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011437 }
11438
11439 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011440 int size = scrollBar.getSize(true);
11441 if (size <= 0) {
11442 size = cache.scrollBarSize;
11443 }
11444
Mike Reede8853fc2009-09-04 14:01:48 -040011445 scrollBar.setParameters(computeVerticalScrollRange(),
11446 computeVerticalScrollOffset(),
11447 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011448 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11449 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11450 verticalScrollbarPosition = isLayoutRtl() ?
11451 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11452 }
11453 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011454 default:
Adam Powell20232d02010-12-08 21:08:53 -080011455 case SCROLLBAR_POSITION_RIGHT:
11456 left = scrollX + width - size - (mUserPaddingRight & inside);
11457 break;
11458 case SCROLLBAR_POSITION_LEFT:
11459 left = scrollX + (mUserPaddingLeft & inside);
11460 break;
11461 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011462 top = scrollY + (mPaddingTop & inside);
11463 right = left + size;
11464 bottom = scrollY + height - (mUserPaddingBottom & inside);
11465 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11466 if (invalidate) {
11467 invalidate(left, top, right, bottom);
11468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 }
11470 }
11471 }
11472 }
Romain Guy8506ab42009-06-11 17:35:47 -070011473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011474 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011475 * 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 -080011476 * FastScroller is visible.
11477 * @return whether to temporarily hide the vertical scrollbar
11478 * @hide
11479 */
11480 protected boolean isVerticalScrollBarHidden() {
11481 return false;
11482 }
11483
11484 /**
11485 * <p>Draw the horizontal scrollbar if
11486 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011488 * @param canvas the canvas on which to draw the scrollbar
11489 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490 *
11491 * @see #isHorizontalScrollBarEnabled()
11492 * @see #computeHorizontalScrollRange()
11493 * @see #computeHorizontalScrollExtent()
11494 * @see #computeHorizontalScrollOffset()
11495 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011496 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011497 */
Romain Guy8fb95422010-08-17 18:38:51 -070011498 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11499 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011500 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011501 scrollBar.draw(canvas);
11502 }
Mike Reede8853fc2009-09-04 14:01:48 -040011503
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011505 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11506 * returns true.</p>
11507 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508 * @param canvas the canvas on which to draw the scrollbar
11509 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011510 *
11511 * @see #isVerticalScrollBarEnabled()
11512 * @see #computeVerticalScrollRange()
11513 * @see #computeVerticalScrollExtent()
11514 * @see #computeVerticalScrollOffset()
11515 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011516 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011517 */
Romain Guy8fb95422010-08-17 18:38:51 -070011518 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11519 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011520 scrollBar.setBounds(l, t, r, b);
11521 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011522 }
11523
11524 /**
11525 * Implement this to do your drawing.
11526 *
11527 * @param canvas the canvas on which the background will be drawn
11528 */
11529 protected void onDraw(Canvas canvas) {
11530 }
11531
11532 /*
11533 * Caller is responsible for calling requestLayout if necessary.
11534 * (This allows addViewInLayout to not request a new layout.)
11535 */
11536 void assignParent(ViewParent parent) {
11537 if (mParent == null) {
11538 mParent = parent;
11539 } else if (parent == null) {
11540 mParent = null;
11541 } else {
11542 throw new RuntimeException("view " + this + " being added, but"
11543 + " it already has a parent");
11544 }
11545 }
11546
11547 /**
11548 * This is called when the view is attached to a window. At this point it
11549 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011550 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11551 * however it may be called any time before the first onDraw -- including
11552 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011553 *
11554 * @see #onDetachedFromWindow()
11555 */
11556 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011557 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011558 mParent.requestTransparentRegion(this);
11559 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011560
Dianne Hackborn4702a852012-08-17 15:18:29 -070011561 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011562 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011563 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011564 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011565
Chet Haasea9b61ac2010-12-20 07:40:25 -080011566 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011567
Svetoslav Ganov42138042012-03-20 11:51:39 -070011568 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011569 if (isFocused()) {
11570 InputMethodManager imm = InputMethodManager.peekInstance();
11571 imm.focusIn(this);
11572 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011573
Romain Guyfbb93fa2012-12-03 18:50:35 -080011574 if (mDisplayList != null) {
11575 mDisplayList.setDirty(false);
Romain Guy2a0f2282012-05-08 14:43:12 -070011576 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011577 }
Cibu Johny86666632010-02-22 13:01:02 -080011578
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011579 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011580 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011581 *
11582 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011583 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011584 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011585 if (!needRtlPropertiesResolution()) return;
11586
11587 // Order is important here: LayoutDirection MUST be resolved first
11588 if (!isLayoutDirectionResolved()) {
11589 resolveLayoutDirection();
11590 resolveLayoutParams();
11591 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011592 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011593 if (!isTextDirectionResolved()) {
11594 resolveTextDirection();
11595 }
11596 if (!isTextAlignmentResolved()) {
11597 resolveTextAlignment();
11598 }
11599 if (!isPaddingResolved()) {
11600 resolvePadding();
11601 }
11602 if (!isDrawablesResolved()) {
11603 resolveDrawables();
11604 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011605 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011606 }
11607
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011608 /**
11609 * Reset resolution of all RTL related properties.
11610 *
11611 * @hide
11612 */
11613 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011614 resetResolvedLayoutDirection();
11615 resetResolvedTextDirection();
11616 resetResolvedTextAlignment();
11617 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011618 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011619 }
11620
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011621 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011622 * @see #onScreenStateChanged(int)
11623 */
11624 void dispatchScreenStateChanged(int screenState) {
11625 onScreenStateChanged(screenState);
11626 }
11627
11628 /**
11629 * This method is called whenever the state of the screen this view is
11630 * attached to changes. A state change will usually occurs when the screen
11631 * turns on or off (whether it happens automatically or the user does it
11632 * manually.)
11633 *
11634 * @param screenState The new state of the screen. Can be either
11635 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11636 */
11637 public void onScreenStateChanged(int screenState) {
11638 }
11639
11640 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011641 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11642 */
11643 private boolean hasRtlSupport() {
11644 return mContext.getApplicationInfo().hasRtlSupport();
11645 }
11646
11647 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011648 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11649 * RTL not supported)
11650 */
11651 private boolean isRtlCompatibilityMode() {
11652 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11653 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11654 }
11655
11656 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011657 * @return true if RTL properties need resolution.
11658 */
11659 private boolean needRtlPropertiesResolution() {
11660 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11661 }
11662
11663 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011664 * Called when any RTL property (layout direction or text direction or text alignment) has
11665 * been changed.
11666 *
11667 * Subclasses need to override this method to take care of cached information that depends on the
11668 * resolved layout direction, or to inform child views that inherit their layout direction.
11669 *
11670 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011671 *
11672 * @param layoutDirection the direction of the layout
11673 *
11674 * @see #LAYOUT_DIRECTION_LTR
11675 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011676 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011677 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011678 }
11679
11680 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011681 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11682 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011683 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011684 * @return true if resolution has been done, false otherwise.
11685 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011686 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011687 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011688 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011689 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011690 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011691
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011692 if (hasRtlSupport()) {
11693 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011694 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11695 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011696 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011697 // We cannot resolve yet. LTR is by default and let the resolution happen again
11698 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011699 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011700
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011701 View parent = ((View) mParent);
11702 // Parent has not yet resolved, LTR is still the default
11703 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011704
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011705 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011706 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011707 }
11708 break;
11709 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011710 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011711 break;
11712 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011713 if((LAYOUT_DIRECTION_RTL ==
11714 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011715 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011716 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011717 break;
11718 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011719 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011720 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011721 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011722
11723 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011724 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011725 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011726 }
11727
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011728 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011729 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011730 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011731 * @return true if layout direction resolution can be done otherwise return false.
11732 *
11733 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011734 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011735 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011736 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011737 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011738 return (mParent != null) && (mParent instanceof ViewGroup) &&
11739 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011740 default:
11741 return true;
11742 }
11743 }
11744
11745 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011746 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11747 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011748 *
11749 * @hide
11750 */
11751 public void resetResolvedLayoutDirection() {
11752 // Reset the current resolved bits
11753 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011754 }
11755
11756 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011757 * @return true if the layout direction is inherited.
11758 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011759 * @hide
11760 */
11761 public boolean isLayoutDirectionInherited() {
11762 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11763 }
11764
11765 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011766 * @return true if layout direction has been resolved.
11767 */
11768 private boolean isLayoutDirectionResolved() {
11769 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11770 }
11771
11772 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011773 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011774 *
11775 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011776 */
11777 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011778 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011779 }
11780
11781 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011782 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011783 *
11784 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011785 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011786 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011787 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011788 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11789 // If start / end padding are defined, they will be resolved (hence overriding) to
11790 // left / right or right / left depending on the resolved layout direction.
11791 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011792 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011793 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011794 mUserPaddingLeft = mUserPaddingLeftInitial;
11795 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011796 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011797 switch (resolvedLayoutDirection) {
11798 case LAYOUT_DIRECTION_RTL:
11799 if (mUserPaddingStart != UNDEFINED_PADDING) {
11800 mUserPaddingRight = mUserPaddingStart;
11801 }
11802 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11803 mUserPaddingLeft = mUserPaddingEnd;
11804 }
11805 break;
11806 case LAYOUT_DIRECTION_LTR:
11807 default:
11808 if (mUserPaddingStart != UNDEFINED_PADDING) {
11809 mUserPaddingLeft = mUserPaddingStart;
11810 }
11811 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11812 mUserPaddingRight = mUserPaddingEnd;
11813 }
11814 }
11815
11816 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11817
11818 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11819 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011820 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011821 }
11822
Dianne Hackborn4702a852012-08-17 15:18:29 -070011823 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011824 }
11825
11826 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011827 * Reset the resolved layout direction.
11828 *
11829 * @hide
11830 */
11831 public void resetResolvedPadding() {
11832 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11833 }
11834
11835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011836 * This is called when the view is detached from a window. At this point it
11837 * no longer has a surface for drawing.
11838 *
11839 * @see #onAttachedToWindow()
11840 */
11841 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011842 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011843
Romain Guya440b002010-02-24 15:57:54 -080011844 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011845 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011846 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011847 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011850
Romain Guya998dff2012-03-23 18:58:36 -070011851 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011852
11853 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011854 if (mDisplayList != null) {
Romain Guyfbb93fa2012-12-03 18:50:35 -080011855 mDisplayList.setDirty(true);
Romain Guy2a0f2282012-05-08 14:43:12 -070011856 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011857 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011858 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011859 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011860 // Should never happen
11861 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011862 }
11863
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011864 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011865
Svetoslav Ganov42138042012-03-20 11:51:39 -070011866 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011867 }
11868
11869 /**
11870 * @return The number of times this view has been attached to a window
11871 */
11872 protected int getWindowAttachCount() {
11873 return mWindowAttachCount;
11874 }
11875
11876 /**
11877 * Retrieve a unique token identifying the window this view is attached to.
11878 * @return Return the window's token for use in
11879 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11880 */
11881 public IBinder getWindowToken() {
11882 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11883 }
11884
11885 /**
11886 * Retrieve a unique token identifying the top-level "real" window of
11887 * the window that this view is attached to. That is, this is like
11888 * {@link #getWindowToken}, except if the window this view in is a panel
11889 * window (attached to another containing window), then the token of
11890 * the containing window is returned instead.
11891 *
11892 * @return Returns the associated window token, either
11893 * {@link #getWindowToken()} or the containing window's token.
11894 */
11895 public IBinder getApplicationWindowToken() {
11896 AttachInfo ai = mAttachInfo;
11897 if (ai != null) {
11898 IBinder appWindowToken = ai.mPanelParentWindowToken;
11899 if (appWindowToken == null) {
11900 appWindowToken = ai.mWindowToken;
11901 }
11902 return appWindowToken;
11903 }
11904 return null;
11905 }
11906
11907 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011908 * Gets the logical display to which the view's window has been attached.
11909 *
11910 * @return The logical display, or null if the view is not currently attached to a window.
11911 */
11912 public Display getDisplay() {
11913 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11914 }
11915
11916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011917 * Retrieve private session object this view hierarchy is using to
11918 * communicate with the window manager.
11919 * @return the session object to communicate with the window manager
11920 */
11921 /*package*/ IWindowSession getWindowSession() {
11922 return mAttachInfo != null ? mAttachInfo.mSession : null;
11923 }
11924
11925 /**
11926 * @param info the {@link android.view.View.AttachInfo} to associated with
11927 * this view
11928 */
11929 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11930 //System.out.println("Attached! " + this);
11931 mAttachInfo = info;
11932 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011933 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011934 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 if (mFloatingTreeObserver != null) {
11936 info.mTreeObserver.merge(mFloatingTreeObserver);
11937 mFloatingTreeObserver = null;
11938 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011939 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011940 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011941 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011942 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011943 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011945
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011946 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011947 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011948 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011949 if (listeners != null && listeners.size() > 0) {
11950 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11951 // perform the dispatching. The iterator is a safe guard against listeners that
11952 // could mutate the list by calling the various add/remove methods. This prevents
11953 // the array from being modified while we iterate it.
11954 for (OnAttachStateChangeListener listener : listeners) {
11955 listener.onViewAttachedToWindow(this);
11956 }
11957 }
11958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011959 int vis = info.mWindowVisibility;
11960 if (vis != GONE) {
11961 onWindowVisibilityChanged(vis);
11962 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011963 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011964 // If nobody has evaluated the drawable state yet, then do it now.
11965 refreshDrawableState();
11966 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011967 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011968 }
11969
11970 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971 AttachInfo info = mAttachInfo;
11972 if (info != null) {
11973 int vis = info.mWindowVisibility;
11974 if (vis != GONE) {
11975 onWindowVisibilityChanged(GONE);
11976 }
11977 }
11978
11979 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011980
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011981 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011982 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011983 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011984 if (listeners != null && listeners.size() > 0) {
11985 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11986 // perform the dispatching. The iterator is a safe guard against listeners that
11987 // could mutate the list by calling the various add/remove methods. This prevents
11988 // the array from being modified while we iterate it.
11989 for (OnAttachStateChangeListener listener : listeners) {
11990 listener.onViewDetachedFromWindow(this);
11991 }
11992 }
11993
Dianne Hackborn4702a852012-08-17 15:18:29 -070011994 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011996 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011997 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 mAttachInfo = null;
12000 }
12001
12002 /**
12003 * Store this view hierarchy's frozen state into the given container.
12004 *
12005 * @param container The SparseArray in which to save the view's state.
12006 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012007 * @see #restoreHierarchyState(android.util.SparseArray)
12008 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12009 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 */
12011 public void saveHierarchyState(SparseArray<Parcelable> container) {
12012 dispatchSaveInstanceState(container);
12013 }
12014
12015 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012016 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12017 * this view and its children. May be overridden to modify how freezing happens to a
12018 * 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 -080012019 *
12020 * @param container The SparseArray in which to save the view's state.
12021 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012022 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12023 * @see #saveHierarchyState(android.util.SparseArray)
12024 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012025 */
12026 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12027 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012028 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012029 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012030 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 throw new IllegalStateException(
12032 "Derived class did not call super.onSaveInstanceState()");
12033 }
12034 if (state != null) {
12035 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12036 // + ": " + state);
12037 container.put(mID, state);
12038 }
12039 }
12040 }
12041
12042 /**
12043 * Hook allowing a view to generate a representation of its internal state
12044 * that can later be used to create a new instance with that same state.
12045 * This state should only contain information that is not persistent or can
12046 * not be reconstructed later. For example, you will never store your
12047 * current position on screen because that will be computed again when a
12048 * new instance of the view is placed in its view hierarchy.
12049 * <p>
12050 * Some examples of things you may store here: the current cursor position
12051 * in a text view (but usually not the text itself since that is stored in a
12052 * content provider or other persistent storage), the currently selected
12053 * item in a list view.
12054 *
12055 * @return Returns a Parcelable object containing the view's current dynamic
12056 * state, or null if there is nothing interesting to save. The
12057 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012058 * @see #onRestoreInstanceState(android.os.Parcelable)
12059 * @see #saveHierarchyState(android.util.SparseArray)
12060 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012061 * @see #setSaveEnabled(boolean)
12062 */
12063 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012064 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065 return BaseSavedState.EMPTY_STATE;
12066 }
12067
12068 /**
12069 * Restore this view hierarchy's frozen state from the given container.
12070 *
12071 * @param container The SparseArray which holds previously frozen states.
12072 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012073 * @see #saveHierarchyState(android.util.SparseArray)
12074 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12075 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076 */
12077 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12078 dispatchRestoreInstanceState(container);
12079 }
12080
12081 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012082 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12083 * state for this view and its children. May be overridden to modify how restoring
12084 * happens to a view's children; for example, some views may want to not store state
12085 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 *
12087 * @param container The SparseArray which holds previously saved state.
12088 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012089 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12090 * @see #restoreHierarchyState(android.util.SparseArray)
12091 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012092 */
12093 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12094 if (mID != NO_ID) {
12095 Parcelable state = container.get(mID);
12096 if (state != null) {
12097 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12098 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012099 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012100 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012101 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012102 throw new IllegalStateException(
12103 "Derived class did not call super.onRestoreInstanceState()");
12104 }
12105 }
12106 }
12107 }
12108
12109 /**
12110 * Hook allowing a view to re-apply a representation of its internal state that had previously
12111 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12112 * null state.
12113 *
12114 * @param state The frozen state that had previously been returned by
12115 * {@link #onSaveInstanceState}.
12116 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012117 * @see #onSaveInstanceState()
12118 * @see #restoreHierarchyState(android.util.SparseArray)
12119 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012120 */
12121 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012122 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012124 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12125 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012126 + "when two views of different type have the same id in the same hierarchy. "
12127 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012128 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012129 }
12130 }
12131
12132 /**
12133 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12134 *
12135 * @return the drawing start time in milliseconds
12136 */
12137 public long getDrawingTime() {
12138 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12139 }
12140
12141 /**
12142 * <p>Enables or disables the duplication of the parent's state into this view. When
12143 * duplication is enabled, this view gets its drawable state from its parent rather
12144 * than from its own internal properties.</p>
12145 *
12146 * <p>Note: in the current implementation, setting this property to true after the
12147 * view was added to a ViewGroup might have no effect at all. This property should
12148 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12149 *
12150 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12151 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012152 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012153 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12154 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 *
12156 * @param enabled True to enable duplication of the parent's drawable state, false
12157 * to disable it.
12158 *
12159 * @see #getDrawableState()
12160 * @see #isDuplicateParentStateEnabled()
12161 */
12162 public void setDuplicateParentStateEnabled(boolean enabled) {
12163 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12164 }
12165
12166 /**
12167 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12168 *
12169 * @return True if this view's drawable state is duplicated from the parent,
12170 * false otherwise
12171 *
12172 * @see #getDrawableState()
12173 * @see #setDuplicateParentStateEnabled(boolean)
12174 */
12175 public boolean isDuplicateParentStateEnabled() {
12176 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12177 }
12178
12179 /**
Romain Guy171c5922011-01-06 10:04:23 -080012180 * <p>Specifies the type of layer backing this view. The layer can be
12181 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12182 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012183 *
Romain Guy171c5922011-01-06 10:04:23 -080012184 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12185 * instance that controls how the layer is composed on screen. The following
12186 * properties of the paint are taken into account when composing the layer:</p>
12187 * <ul>
12188 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12189 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12190 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12191 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012192 *
Romain Guy171c5922011-01-06 10:04:23 -080012193 * <p>If this view has an alpha value set to < 1.0 by calling
12194 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12195 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12196 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012197 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012198 *
Romain Guy171c5922011-01-06 10:04:23 -080012199 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12200 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12201 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012202 *
Chet Haased15ebf22012-09-05 11:40:29 -070012203 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012204 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12205 * {@link #LAYER_TYPE_HARDWARE}
12206 * @param paint The paint used to compose the layer. This argument is optional
12207 * and can be null. It is ignored when the layer type is
12208 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012209 *
12210 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012211 * @see #LAYER_TYPE_NONE
12212 * @see #LAYER_TYPE_SOFTWARE
12213 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012214 * @see #setAlpha(float)
12215 *
Romain Guy171c5922011-01-06 10:04:23 -080012216 * @attr ref android.R.styleable#View_layerType
12217 */
12218 public void setLayerType(int layerType, Paint paint) {
12219 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012220 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012221 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12222 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012223
Romain Guyd6cd5722011-01-17 14:42:41 -080012224 if (layerType == mLayerType) {
12225 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12226 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012227 invalidateParentCaches();
12228 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012229 }
12230 return;
12231 }
Romain Guy171c5922011-01-06 10:04:23 -080012232
12233 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012234 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012235 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012236 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012237 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012238 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012239 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012240 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012241 default:
12242 break;
Romain Guy171c5922011-01-06 10:04:23 -080012243 }
12244
12245 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012246 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12247 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12248 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012249
Romain Guy0fd89bf2011-01-26 15:41:30 -080012250 invalidateParentCaches();
12251 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012252 }
12253
12254 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012255 * Updates the {@link Paint} object used with the current layer (used only if the current
12256 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12257 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12258 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12259 * ensure that the view gets redrawn immediately.
12260 *
12261 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12262 * instance that controls how the layer is composed on screen. The following
12263 * properties of the paint are taken into account when composing the layer:</p>
12264 * <ul>
12265 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12266 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12267 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12268 * </ul>
12269 *
12270 * <p>If this view has an alpha value set to < 1.0 by calling
12271 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12272 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12273 * equivalent to setting a hardware layer on this view and providing a paint with
12274 * the desired alpha value.</p>
12275 *
12276 * @param paint The paint used to compose the layer. This argument is optional
12277 * and can be null. It is ignored when the layer type is
12278 * {@link #LAYER_TYPE_NONE}
12279 *
12280 * @see #setLayerType(int, android.graphics.Paint)
12281 */
12282 public void setLayerPaint(Paint paint) {
12283 int layerType = getLayerType();
12284 if (layerType != LAYER_TYPE_NONE) {
12285 mLayerPaint = paint == null ? new Paint() : paint;
12286 if (layerType == LAYER_TYPE_HARDWARE) {
12287 HardwareLayer layer = getHardwareLayer();
12288 if (layer != null) {
12289 layer.setLayerPaint(paint);
12290 }
12291 invalidateViewProperty(false, false);
12292 } else {
12293 invalidate();
12294 }
12295 }
12296 }
12297
12298 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012299 * Indicates whether this view has a static layer. A view with layer type
12300 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12301 * dynamic.
12302 */
12303 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012304 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012305 }
12306
12307 /**
Romain Guy171c5922011-01-06 10:04:23 -080012308 * Indicates what type of layer is currently associated with this view. By default
12309 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12310 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12311 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012312 *
Romain Guy171c5922011-01-06 10:04:23 -080012313 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12314 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012315 *
12316 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012317 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012318 * @see #LAYER_TYPE_NONE
12319 * @see #LAYER_TYPE_SOFTWARE
12320 * @see #LAYER_TYPE_HARDWARE
12321 */
12322 public int getLayerType() {
12323 return mLayerType;
12324 }
Joe Malin32736f02011-01-19 16:14:20 -080012325
Romain Guy6c319ca2011-01-11 14:29:25 -080012326 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012327 * Forces this view's layer to be created and this view to be rendered
12328 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12329 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012330 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012331 * This method can for instance be used to render a view into its layer before
12332 * starting an animation. If this view is complex, rendering into the layer
12333 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012334 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012335 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012336 *
12337 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012338 */
12339 public void buildLayer() {
12340 if (mLayerType == LAYER_TYPE_NONE) return;
12341
12342 if (mAttachInfo == null) {
12343 throw new IllegalStateException("This view must be attached to a window first");
12344 }
12345
12346 switch (mLayerType) {
12347 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012348 if (mAttachInfo.mHardwareRenderer != null &&
12349 mAttachInfo.mHardwareRenderer.isEnabled() &&
12350 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012351 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012352 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012353 break;
12354 case LAYER_TYPE_SOFTWARE:
12355 buildDrawingCache(true);
12356 break;
12357 }
12358 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012359
Romain Guyf1ae1062011-03-02 18:16:04 -080012360 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012361 * <p>Returns a hardware layer that can be used to draw this view again
12362 * without executing its draw method.</p>
12363 *
12364 * @return A HardwareLayer ready to render, or null if an error occurred.
12365 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012366 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012367 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12368 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012369 return null;
12370 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012371
Romain Guy9c4b79a2011-11-10 19:23:58 -080012372 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012373
12374 final int width = mRight - mLeft;
12375 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012376
Romain Guy6c319ca2011-01-11 14:29:25 -080012377 if (width == 0 || height == 0) {
12378 return null;
12379 }
12380
Dianne Hackborn4702a852012-08-17 15:18:29 -070012381 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012382 if (mHardwareLayer == null) {
12383 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12384 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012385 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012386 } else {
12387 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012388 if (mHardwareLayer.resize(width, height)) {
12389 mLocalDirtyRect.set(0, 0, width, height);
12390 }
Romain Guy846a5332012-07-11 17:44:57 -070012391 }
12392
12393 // This should not be necessary but applications that change
12394 // the parameters of their background drawable without calling
12395 // this.setBackground(Drawable) can leave the view in a bad state
12396 // (for instance isOpaque() returns true, but the background is
12397 // not opaque.)
12398 computeOpaqueFlags();
12399
12400 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012401 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012402 mHardwareLayer.setOpaque(opaque);
12403 mLocalDirtyRect.set(0, 0, width, height);
12404 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012405 }
12406
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012407 // The layer is not valid if the underlying GPU resources cannot be allocated
12408 if (!mHardwareLayer.isValid()) {
12409 return null;
12410 }
12411
Romain Guy11cb6422012-09-21 00:39:43 -070012412 mHardwareLayer.setLayerPaint(mLayerPaint);
12413 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12414 ViewRootImpl viewRoot = getViewRootImpl();
12415 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12416
Michael Jurka7e52caf2012-03-06 15:57:06 -080012417 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012418 }
12419
12420 return mHardwareLayer;
12421 }
Romain Guy171c5922011-01-06 10:04:23 -080012422
Romain Guy589b0bb2011-10-10 13:57:47 -070012423 /**
12424 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012425 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012426 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012427 *
12428 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012429 * @see #LAYER_TYPE_HARDWARE
12430 */
Romain Guya998dff2012-03-23 18:58:36 -070012431 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012432 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012433 AttachInfo info = mAttachInfo;
12434 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012435 info.mHardwareRenderer.isEnabled() &&
12436 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012437 mHardwareLayer.destroy();
12438 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012439
Chet Haase6a2d17f2012-09-30 12:14:13 -070012440 if (mDisplayList != null) {
12441 mDisplayList.reset();
12442 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012443 invalidate(true);
12444 invalidateParentCaches();
12445 }
Romain Guy65b345f2011-07-27 18:51:50 -070012446 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012447 }
Romain Guy65b345f2011-07-27 18:51:50 -070012448 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012449 }
12450
Romain Guy171c5922011-01-06 10:04:23 -080012451 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012452 * Destroys all hardware rendering resources. This method is invoked
12453 * when the system needs to reclaim resources. Upon execution of this
12454 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012455 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012456 * Note: you <strong>must</strong> call
12457 * <code>super.destroyHardwareResources()</code> when overriding
12458 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012459 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012460 * @hide
12461 */
12462 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012463 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012464 }
12465
12466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12468 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12469 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12470 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12471 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12472 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012473 *
Romain Guy171c5922011-01-06 10:04:23 -080012474 * <p>Enabling the drawing cache is similar to
12475 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012476 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12477 * drawing cache has no effect on rendering because the system uses a different mechanism
12478 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12479 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12480 * for information on how to enable software and hardware layers.</p>
12481 *
12482 * <p>This API can be used to manually generate
12483 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12484 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012485 *
12486 * @param enabled true to enable the drawing cache, false otherwise
12487 *
12488 * @see #isDrawingCacheEnabled()
12489 * @see #getDrawingCache()
12490 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012491 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012492 */
12493 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012494 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12496 }
12497
12498 /**
12499 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12500 *
12501 * @return true if the drawing cache is enabled
12502 *
12503 * @see #setDrawingCacheEnabled(boolean)
12504 * @see #getDrawingCache()
12505 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012506 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 public boolean isDrawingCacheEnabled() {
12508 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12509 }
12510
12511 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012512 * Debugging utility which recursively outputs the dirty state of a view and its
12513 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012514 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012515 * @hide
12516 */
Romain Guy676b1732011-02-14 14:45:33 -080012517 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012518 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012519 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12520 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12521 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12522 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012523 if (clear) {
12524 mPrivateFlags &= clearMask;
12525 }
12526 if (this instanceof ViewGroup) {
12527 ViewGroup parent = (ViewGroup) this;
12528 final int count = parent.getChildCount();
12529 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012530 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012531 child.outputDirtyFlags(indent + " ", clear, clearMask);
12532 }
12533 }
12534 }
12535
12536 /**
12537 * This method is used by ViewGroup to cause its children to restore or recreate their
12538 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12539 * to recreate its own display list, which would happen if it went through the normal
12540 * draw/dispatchDraw mechanisms.
12541 *
12542 * @hide
12543 */
12544 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012545
12546 /**
12547 * A view that is not attached or hardware accelerated cannot create a display list.
12548 * This method checks these conditions and returns the appropriate result.
12549 *
12550 * @return true if view has the ability to create a display list, false otherwise.
12551 *
12552 * @hide
12553 */
12554 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012555 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012556 }
Joe Malin32736f02011-01-19 16:14:20 -080012557
Chet Haasedaf98e92011-01-10 14:10:36 -080012558 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012559 * @return The HardwareRenderer associated with that view or null if hardware rendering
12560 * is not supported or this this has not been attached to a window.
12561 *
12562 * @hide
12563 */
12564 public HardwareRenderer getHardwareRenderer() {
12565 if (mAttachInfo != null) {
12566 return mAttachInfo.mHardwareRenderer;
12567 }
12568 return null;
12569 }
12570
12571 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012572 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12573 * Otherwise, the same display list will be returned (after having been rendered into
12574 * along the way, depending on the invalidation state of the view).
12575 *
12576 * @param displayList The previous version of this displayList, could be null.
12577 * @param isLayer Whether the requester of the display list is a layer. If so,
12578 * the view will avoid creating a layer inside the resulting display list.
12579 * @return A new or reused DisplayList object.
12580 */
12581 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12582 if (!canHaveDisplayList()) {
12583 return null;
12584 }
12585
Dianne Hackborn4702a852012-08-17 15:18:29 -070012586 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012587 displayList == null || !displayList.isValid() ||
12588 (!isLayer && mRecreateDisplayList))) {
12589 // Don't need to recreate the display list, just need to tell our
12590 // children to restore/recreate theirs
12591 if (displayList != null && displayList.isValid() &&
12592 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012593 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12594 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012595 dispatchGetDisplayList();
12596
12597 return displayList;
12598 }
12599
12600 if (!isLayer) {
12601 // If we got here, we're recreating it. Mark it as such to ensure that
12602 // we copy in child display lists into ours in drawChild()
12603 mRecreateDisplayList = true;
12604 }
12605 if (displayList == null) {
12606 final String name = getClass().getSimpleName();
12607 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12608 // If we're creating a new display list, make sure our parent gets invalidated
12609 // since they will need to recreate their display list to account for this
12610 // new child display list.
12611 invalidateParentCaches();
12612 }
12613
12614 boolean caching = false;
12615 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012616 int width = mRight - mLeft;
12617 int height = mBottom - mTop;
12618
12619 try {
12620 canvas.setViewport(width, height);
12621 // The dirty rect should always be null for a display list
12622 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012623 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012624 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012625 if (layerType == LAYER_TYPE_HARDWARE) {
12626 final HardwareLayer layer = getHardwareLayer();
12627 if (layer != null && layer.isValid()) {
12628 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12629 } else {
12630 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12631 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12632 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12633 }
12634 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012635 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012636 buildDrawingCache(true);
12637 Bitmap cache = getDrawingCache(true);
12638 if (cache != null) {
12639 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12640 caching = true;
12641 }
Chet Haasea1cff502012-02-21 13:43:44 -080012642 }
Chet Haasea1cff502012-02-21 13:43:44 -080012643 } else {
12644
12645 computeScroll();
12646
Chet Haasea1cff502012-02-21 13:43:44 -080012647 canvas.translate(-mScrollX, -mScrollY);
12648 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012649 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12650 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012651 }
12652
12653 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012654 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012655 dispatchDraw(canvas);
12656 } else {
12657 draw(canvas);
12658 }
12659 }
12660 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012661 canvas.onPostDraw();
12662
12663 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012664 displayList.setCaching(caching);
12665 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012666 displayList.setLeftTopRightBottom(0, 0, width, height);
12667 } else {
12668 setDisplayListProperties(displayList);
12669 }
12670 }
12671 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012672 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12673 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012674 }
12675
12676 return displayList;
12677 }
12678
12679 /**
12680 * Get the DisplayList for the HardwareLayer
12681 *
12682 * @param layer The HardwareLayer whose DisplayList we want
12683 * @return A DisplayList fopr the specified HardwareLayer
12684 */
12685 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12686 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12687 layer.setDisplayList(displayList);
12688 return displayList;
12689 }
12690
12691
12692 /**
Romain Guyb051e892010-09-28 19:09:36 -070012693 * <p>Returns a display list that can be used to draw this view again
12694 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012695 *
Romain Guyb051e892010-09-28 19:09:36 -070012696 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012697 *
12698 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012699 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012700 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012701 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012702 return mDisplayList;
12703 }
12704
Romain Guy38c2ece2012-05-24 14:20:56 -070012705 private void clearDisplayList() {
12706 if (mDisplayList != null) {
12707 mDisplayList.invalidate();
12708 mDisplayList.clear();
12709 }
12710 }
12711
Romain Guyb051e892010-09-28 19:09:36 -070012712 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012713 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012714 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012715 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012716 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012717 * @see #getDrawingCache(boolean)
12718 */
12719 public Bitmap getDrawingCache() {
12720 return getDrawingCache(false);
12721 }
12722
12723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12725 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12726 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12727 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12728 * request the drawing cache by calling this method and draw it on screen if the
12729 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012730 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012731 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12732 * this method will create a bitmap of the same size as this view. Because this bitmap
12733 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12734 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12735 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12736 * size than the view. This implies that your application must be able to handle this
12737 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012738 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012739 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12740 * the current density of the screen when the application is in compatibility
12741 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012742 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012743 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012745 * @see #setDrawingCacheEnabled(boolean)
12746 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012747 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748 * @see #destroyDrawingCache()
12749 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012750 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12752 return null;
12753 }
12754 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012755 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756 }
Romain Guy02890fd2010-08-06 17:58:44 -070012757 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012758 }
12759
12760 /**
12761 * <p>Frees the resources used by the drawing cache. If you call
12762 * {@link #buildDrawingCache()} manually without calling
12763 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12764 * should cleanup the cache with this method afterwards.</p>
12765 *
12766 * @see #setDrawingCacheEnabled(boolean)
12767 * @see #buildDrawingCache()
12768 * @see #getDrawingCache()
12769 */
12770 public void destroyDrawingCache() {
12771 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012772 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012773 mDrawingCache = null;
12774 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012775 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012776 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012777 mUnscaledDrawingCache = null;
12778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012779 }
12780
12781 /**
12782 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012783 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012784 * view will always be drawn on top of a solid color.
12785 *
12786 * @param color The background color to use for the drawing cache's bitmap
12787 *
12788 * @see #setDrawingCacheEnabled(boolean)
12789 * @see #buildDrawingCache()
12790 * @see #getDrawingCache()
12791 */
12792 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012793 if (color != mDrawingCacheBackgroundColor) {
12794 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012795 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012797 }
12798
12799 /**
12800 * @see #setDrawingCacheBackgroundColor(int)
12801 *
12802 * @return The background color to used for the drawing cache's bitmap
12803 */
12804 public int getDrawingCacheBackgroundColor() {
12805 return mDrawingCacheBackgroundColor;
12806 }
12807
12808 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012809 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012810 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012811 * @see #buildDrawingCache(boolean)
12812 */
12813 public void buildDrawingCache() {
12814 buildDrawingCache(false);
12815 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012816
Romain Guyfbd8f692009-06-26 14:51:58 -070012817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012818 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12819 *
12820 * <p>If you call {@link #buildDrawingCache()} manually without calling
12821 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12822 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012823 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012824 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12825 * this method will create a bitmap of the same size as this view. Because this bitmap
12826 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12827 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12828 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12829 * size than the view. This implies that your application must be able to handle this
12830 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012831 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012832 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12833 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012834 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012835 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836 *
12837 * @see #getDrawingCache()
12838 * @see #destroyDrawingCache()
12839 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012840 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012841 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012842 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012843 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012844
Romain Guy8506ab42009-06-11 17:35:47 -070012845 int width = mRight - mLeft;
12846 int height = mBottom - mTop;
12847
12848 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012849 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012850
Romain Guye1123222009-06-29 14:24:56 -070012851 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012852 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12853 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855
12856 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012857 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012858 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859
Romain Guyaf61cc42012-09-30 10:49:15 -070012860 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12861 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012862 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12863 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012864 if (width > 0 && height > 0) {
12865 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12866 + projectedBitmapSize + " bytes, only "
12867 + drawingCacheSize + " available");
12868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012869 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012870 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012871 return;
12872 }
12873
12874 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012875 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012876
12877 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012878 Bitmap.Config quality;
12879 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012880 // Never pick ARGB_4444 because it looks awful
12881 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12883 case DRAWING_CACHE_QUALITY_AUTO:
12884 quality = Bitmap.Config.ARGB_8888;
12885 break;
12886 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012887 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012888 break;
12889 case DRAWING_CACHE_QUALITY_HIGH:
12890 quality = Bitmap.Config.ARGB_8888;
12891 break;
12892 default:
12893 quality = Bitmap.Config.ARGB_8888;
12894 break;
12895 }
12896 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012897 // Optimization for translucent windows
12898 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012899 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 }
12901
12902 // Try to cleanup memory
12903 if (bitmap != null) bitmap.recycle();
12904
12905 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012906 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12907 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012908 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012909 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012910 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012911 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012912 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012913 }
Adam Powell26153a32010-11-08 15:22:27 -080012914 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 } catch (OutOfMemoryError e) {
12916 // If there is not enough memory to create the bitmap cache, just
12917 // ignore the issue as bitmap caches are not required to draw the
12918 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012919 if (autoScale) {
12920 mDrawingCache = null;
12921 } else {
12922 mUnscaledDrawingCache = null;
12923 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012924 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 return;
12926 }
12927
12928 clear = drawingCacheBackgroundColor != 0;
12929 }
12930
12931 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012932 if (attachInfo != null) {
12933 canvas = attachInfo.mCanvas;
12934 if (canvas == null) {
12935 canvas = new Canvas();
12936 }
12937 canvas.setBitmap(bitmap);
12938 // Temporarily clobber the cached Canvas in case one of our children
12939 // is also using a drawing cache. Without this, the children would
12940 // steal the canvas by attaching their own bitmap to it and bad, bad
12941 // thing would happen (invisible views, corrupted drawings, etc.)
12942 attachInfo.mCanvas = null;
12943 } else {
12944 // This case should hopefully never or seldom happen
12945 canvas = new Canvas(bitmap);
12946 }
12947
12948 if (clear) {
12949 bitmap.eraseColor(drawingCacheBackgroundColor);
12950 }
12951
12952 computeScroll();
12953 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012954
Romain Guye1123222009-06-29 14:24:56 -070012955 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012956 final float scale = attachInfo.mApplicationScale;
12957 canvas.scale(scale, scale);
12958 }
Joe Malin32736f02011-01-19 16:14:20 -080012959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012960 canvas.translate(-mScrollX, -mScrollY);
12961
Dianne Hackborn4702a852012-08-17 15:18:29 -070012962 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012963 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12964 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012965 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012967
12968 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012969 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12970 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012971 dispatchDraw(canvas);
12972 } else {
12973 draw(canvas);
12974 }
12975
12976 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012977 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012978
12979 if (attachInfo != null) {
12980 // Restore the cached Canvas for our siblings
12981 attachInfo.mCanvas = canvas;
12982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012983 }
12984 }
12985
12986 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012987 * Create a snapshot of the view into a bitmap. We should probably make
12988 * some form of this public, but should think about the API.
12989 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012990 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012991 int width = mRight - mLeft;
12992 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012993
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012994 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012995 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012996 width = (int) ((width * scale) + 0.5f);
12997 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012998
Dianne Hackborndde331c2012-08-03 14:01:57 -070012999 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
13000 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013001 if (bitmap == null) {
13002 throw new OutOfMemoryError();
13003 }
13004
Romain Guyc529d8d2011-09-06 15:01:39 -070013005 Resources resources = getResources();
13006 if (resources != null) {
13007 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
13008 }
Joe Malin32736f02011-01-19 16:14:20 -080013009
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013010 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013011 if (attachInfo != null) {
13012 canvas = attachInfo.mCanvas;
13013 if (canvas == null) {
13014 canvas = new Canvas();
13015 }
13016 canvas.setBitmap(bitmap);
13017 // Temporarily clobber the cached Canvas in case one of our children
13018 // is also using a drawing cache. Without this, the children would
13019 // steal the canvas by attaching their own bitmap to it and bad, bad
13020 // things would happen (invisible views, corrupted drawings, etc.)
13021 attachInfo.mCanvas = null;
13022 } else {
13023 // This case should hopefully never or seldom happen
13024 canvas = new Canvas(bitmap);
13025 }
13026
Romain Guy5bcdff42009-05-14 21:27:18 -070013027 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013028 bitmap.eraseColor(backgroundColor);
13029 }
13030
13031 computeScroll();
13032 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013033 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013034 canvas.translate(-mScrollX, -mScrollY);
13035
Romain Guy5bcdff42009-05-14 21:27:18 -070013036 // Temporarily remove the dirty mask
13037 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013038 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013039
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013040 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013041 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013042 dispatchDraw(canvas);
13043 } else {
13044 draw(canvas);
13045 }
13046
Romain Guy5bcdff42009-05-14 21:27:18 -070013047 mPrivateFlags = flags;
13048
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013049 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013050 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013051
13052 if (attachInfo != null) {
13053 // Restore the cached Canvas for our siblings
13054 attachInfo.mCanvas = canvas;
13055 }
Romain Guy8506ab42009-06-11 17:35:47 -070013056
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013057 return bitmap;
13058 }
13059
13060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061 * Indicates whether this View is currently in edit mode. A View is usually
13062 * in edit mode when displayed within a developer tool. For instance, if
13063 * this View is being drawn by a visual user interface builder, this method
13064 * should return true.
13065 *
13066 * Subclasses should check the return value of this method to provide
13067 * different behaviors if their normal behavior might interfere with the
13068 * host environment. For instance: the class spawns a thread in its
13069 * constructor, the drawing code relies on device-specific features, etc.
13070 *
13071 * This method is usually checked in the drawing code of custom widgets.
13072 *
13073 * @return True if this View is in edit mode, false otherwise.
13074 */
13075 public boolean isInEditMode() {
13076 return false;
13077 }
13078
13079 /**
13080 * If the View draws content inside its padding and enables fading edges,
13081 * it needs to support padding offsets. Padding offsets are added to the
13082 * fading edges to extend the length of the fade so that it covers pixels
13083 * drawn inside the padding.
13084 *
13085 * Subclasses of this class should override this method if they need
13086 * to draw content inside the padding.
13087 *
13088 * @return True if padding offset must be applied, false otherwise.
13089 *
13090 * @see #getLeftPaddingOffset()
13091 * @see #getRightPaddingOffset()
13092 * @see #getTopPaddingOffset()
13093 * @see #getBottomPaddingOffset()
13094 *
13095 * @since CURRENT
13096 */
13097 protected boolean isPaddingOffsetRequired() {
13098 return false;
13099 }
13100
13101 /**
13102 * Amount by which to extend the left fading region. Called only when
13103 * {@link #isPaddingOffsetRequired()} returns true.
13104 *
13105 * @return The left padding offset in pixels.
13106 *
13107 * @see #isPaddingOffsetRequired()
13108 *
13109 * @since CURRENT
13110 */
13111 protected int getLeftPaddingOffset() {
13112 return 0;
13113 }
13114
13115 /**
13116 * Amount by which to extend the right fading region. Called only when
13117 * {@link #isPaddingOffsetRequired()} returns true.
13118 *
13119 * @return The right padding offset in pixels.
13120 *
13121 * @see #isPaddingOffsetRequired()
13122 *
13123 * @since CURRENT
13124 */
13125 protected int getRightPaddingOffset() {
13126 return 0;
13127 }
13128
13129 /**
13130 * Amount by which to extend the top fading region. Called only when
13131 * {@link #isPaddingOffsetRequired()} returns true.
13132 *
13133 * @return The top padding offset in pixels.
13134 *
13135 * @see #isPaddingOffsetRequired()
13136 *
13137 * @since CURRENT
13138 */
13139 protected int getTopPaddingOffset() {
13140 return 0;
13141 }
13142
13143 /**
13144 * Amount by which to extend the bottom fading region. Called only when
13145 * {@link #isPaddingOffsetRequired()} returns true.
13146 *
13147 * @return The bottom padding offset in pixels.
13148 *
13149 * @see #isPaddingOffsetRequired()
13150 *
13151 * @since CURRENT
13152 */
13153 protected int getBottomPaddingOffset() {
13154 return 0;
13155 }
13156
13157 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013158 * @hide
13159 * @param offsetRequired
13160 */
13161 protected int getFadeTop(boolean offsetRequired) {
13162 int top = mPaddingTop;
13163 if (offsetRequired) top += getTopPaddingOffset();
13164 return top;
13165 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013166
Romain Guyf2fc4602011-07-19 15:20:03 -070013167 /**
13168 * @hide
13169 * @param offsetRequired
13170 */
13171 protected int getFadeHeight(boolean offsetRequired) {
13172 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013173 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013174 return mBottom - mTop - mPaddingBottom - padding;
13175 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013176
Romain Guyf2fc4602011-07-19 15:20:03 -070013177 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013178 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013179 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013180 *
Romain Guy2bffd262010-09-12 17:40:02 -070013181 * <p>Even if this method returns true, it does not mean that every call
13182 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13183 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013184 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013185 * window is hardware accelerated,
13186 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13187 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013188 *
Romain Guy2bffd262010-09-12 17:40:02 -070013189 * @return True if the view is attached to a window and the window is
13190 * hardware accelerated; false in any other case.
13191 */
13192 public boolean isHardwareAccelerated() {
13193 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13194 }
Joe Malin32736f02011-01-19 16:14:20 -080013195
Romain Guy2bffd262010-09-12 17:40:02 -070013196 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013197 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13198 * case of an active Animation being run on the view.
13199 */
13200 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13201 Animation a, boolean scalingRequired) {
13202 Transformation invalidationTransform;
13203 final int flags = parent.mGroupFlags;
13204 final boolean initialized = a.isInitialized();
13205 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013206 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013207 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013208 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013209 onAnimationStart();
13210 }
13211
13212 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13213 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13214 if (parent.mInvalidationTransformation == null) {
13215 parent.mInvalidationTransformation = new Transformation();
13216 }
13217 invalidationTransform = parent.mInvalidationTransformation;
13218 a.getTransformation(drawingTime, invalidationTransform, 1f);
13219 } else {
13220 invalidationTransform = parent.mChildTransformation;
13221 }
Romain Guy393a52c2012-05-22 20:21:08 -070013222
Chet Haasebcca79a2012-02-14 08:45:14 -080013223 if (more) {
13224 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013225 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13226 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13227 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13228 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013229 // The child need to draw an animation, potentially offscreen, so
13230 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013231 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013232 parent.invalidate(mLeft, mTop, mRight, mBottom);
13233 }
13234 } else {
13235 if (parent.mInvalidateRegion == null) {
13236 parent.mInvalidateRegion = new RectF();
13237 }
13238 final RectF region = parent.mInvalidateRegion;
13239 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13240 invalidationTransform);
13241
13242 // The child need to draw an animation, potentially offscreen, so
13243 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013244 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013245
13246 final int left = mLeft + (int) region.left;
13247 final int top = mTop + (int) region.top;
13248 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13249 top + (int) (region.height() + .5f));
13250 }
13251 }
13252 return more;
13253 }
13254
Chet Haasea1cff502012-02-21 13:43:44 -080013255 /**
13256 * This method is called by getDisplayList() when a display list is created or re-rendered.
13257 * It sets or resets the current value of all properties on that display list (resetting is
13258 * necessary when a display list is being re-created, because we need to make sure that
13259 * previously-set transform values
13260 */
13261 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013262 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013263 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013264 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013265 if (mParent instanceof ViewGroup) {
13266 displayList.setClipChildren(
13267 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13268 }
Chet Haase9420abd2012-03-29 16:28:32 -070013269 float alpha = 1;
13270 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13271 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13272 ViewGroup parentVG = (ViewGroup) mParent;
13273 final boolean hasTransform =
13274 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13275 if (hasTransform) {
13276 Transformation transform = parentVG.mChildTransformation;
13277 final int transformType = parentVG.mChildTransformation.getTransformationType();
13278 if (transformType != Transformation.TYPE_IDENTITY) {
13279 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13280 alpha = transform.getAlpha();
13281 }
13282 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13283 displayList.setStaticMatrix(transform.getMatrix());
13284 }
13285 }
13286 }
Chet Haasea1cff502012-02-21 13:43:44 -080013287 }
13288 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013289 alpha *= mTransformationInfo.mAlpha;
13290 if (alpha < 1) {
13291 final int multipliedAlpha = (int) (255 * alpha);
13292 if (onSetAlpha(multipliedAlpha)) {
13293 alpha = 1;
13294 }
13295 }
13296 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013297 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13298 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13299 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13300 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013301 if (mTransformationInfo.mCamera == null) {
13302 mTransformationInfo.mCamera = new Camera();
13303 mTransformationInfo.matrix3D = new Matrix();
13304 }
13305 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013306 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013307 displayList.setPivotX(getPivotX());
13308 displayList.setPivotY(getPivotY());
13309 }
Chet Haase9420abd2012-03-29 16:28:32 -070013310 } else if (alpha < 1) {
13311 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013312 }
13313 }
13314 }
13315
Chet Haasebcca79a2012-02-14 08:45:14 -080013316 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013317 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13318 * This draw() method is an implementation detail and is not intended to be overridden or
13319 * to be called from anywhere else other than ViewGroup.drawChild().
13320 */
13321 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013322 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013323 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013324 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013325 final int flags = parent.mGroupFlags;
13326
Chet Haasea1cff502012-02-21 13:43:44 -080013327 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013328 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013329 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013330 }
13331
13332 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013333 boolean concatMatrix = false;
13334
13335 boolean scalingRequired = false;
13336 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013337 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013338
13339 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013340 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13341 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013342 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013343 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013344 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13345 } else {
13346 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13347 }
13348
Chet Haasebcca79a2012-02-14 08:45:14 -080013349 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013350 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013351 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013352 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013353 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013354 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013355 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013356 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013357 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013358 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013359 mDisplayList != null) {
13360 // No longer animating: clear out old animation matrix
13361 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013362 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013363 }
13364 if (!useDisplayListProperties &&
13365 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13366 final boolean hasTransform =
13367 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13368 if (hasTransform) {
13369 final int transformType = parent.mChildTransformation.getTransformationType();
13370 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13371 parent.mChildTransformation : null;
13372 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13373 }
Chet Haase64a48c12012-02-13 16:33:29 -080013374 }
13375 }
13376
13377 concatMatrix |= !childHasIdentityMatrix;
13378
13379 // Sets the flag as early as possible to allow draw() implementations
13380 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013381 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013382
Romain Guyfbb43212012-08-30 15:19:27 -070013383 if (!concatMatrix &&
13384 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13385 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013386 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013387 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13388 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013389 return more;
13390 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013391 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013392
13393 if (hardwareAccelerated) {
13394 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13395 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013396 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13397 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013398 }
13399
Chet Haase64a48c12012-02-13 16:33:29 -080013400 DisplayList displayList = null;
13401 Bitmap cache = null;
13402 boolean hasDisplayList = false;
13403 if (caching) {
13404 if (!hardwareAccelerated) {
13405 if (layerType != LAYER_TYPE_NONE) {
13406 layerType = LAYER_TYPE_SOFTWARE;
13407 buildDrawingCache(true);
13408 }
13409 cache = getDrawingCache(true);
13410 } else {
13411 switch (layerType) {
13412 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013413 if (useDisplayListProperties) {
13414 hasDisplayList = canHaveDisplayList();
13415 } else {
13416 buildDrawingCache(true);
13417 cache = getDrawingCache(true);
13418 }
Chet Haase64a48c12012-02-13 16:33:29 -080013419 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013420 case LAYER_TYPE_HARDWARE:
13421 if (useDisplayListProperties) {
13422 hasDisplayList = canHaveDisplayList();
13423 }
13424 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013425 case LAYER_TYPE_NONE:
13426 // Delay getting the display list until animation-driven alpha values are
13427 // set up and possibly passed on to the view
13428 hasDisplayList = canHaveDisplayList();
13429 break;
13430 }
13431 }
13432 }
Chet Haasea1cff502012-02-21 13:43:44 -080013433 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013434 if (useDisplayListProperties) {
13435 displayList = getDisplayList();
13436 if (!displayList.isValid()) {
13437 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13438 // to getDisplayList(), the display list will be marked invalid and we should not
13439 // try to use it again.
13440 displayList = null;
13441 hasDisplayList = false;
13442 useDisplayListProperties = false;
13443 }
13444 }
Chet Haase64a48c12012-02-13 16:33:29 -080013445
Chet Haase526057b2012-07-12 17:50:41 -070013446 int sx = 0;
13447 int sy = 0;
13448 if (!hasDisplayList) {
13449 computeScroll();
13450 sx = mScrollX;
13451 sy = mScrollY;
13452 }
13453
Chet Haase64a48c12012-02-13 16:33:29 -080013454 final boolean hasNoCache = cache == null || hasDisplayList;
13455 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13456 layerType != LAYER_TYPE_HARDWARE;
13457
Chet Haasea1cff502012-02-21 13:43:44 -080013458 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013459 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013460 restoreTo = canvas.save();
13461 }
Chet Haase64a48c12012-02-13 16:33:29 -080013462 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013463 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013464 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013465 if (!useDisplayListProperties) {
13466 canvas.translate(mLeft, mTop);
13467 }
Chet Haase64a48c12012-02-13 16:33:29 -080013468 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013469 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013470 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013471 restoreTo = canvas.save();
13472 }
Chet Haase64a48c12012-02-13 16:33:29 -080013473 // mAttachInfo cannot be null, otherwise scalingRequired == false
13474 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13475 canvas.scale(scale, scale);
13476 }
13477 }
13478
Chet Haasea1cff502012-02-21 13:43:44 -080013479 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013480 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013481 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013482 if (transformToApply != null || !childHasIdentityMatrix) {
13483 int transX = 0;
13484 int transY = 0;
13485
13486 if (offsetForScroll) {
13487 transX = -sx;
13488 transY = -sy;
13489 }
13490
13491 if (transformToApply != null) {
13492 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013493 if (useDisplayListProperties) {
13494 displayList.setAnimationMatrix(transformToApply.getMatrix());
13495 } else {
13496 // Undo the scroll translation, apply the transformation matrix,
13497 // then redo the scroll translate to get the correct result.
13498 canvas.translate(-transX, -transY);
13499 canvas.concat(transformToApply.getMatrix());
13500 canvas.translate(transX, transY);
13501 }
Chet Haasea1cff502012-02-21 13:43:44 -080013502 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013503 }
13504
13505 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013506 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013507 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013508 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013509 }
13510 }
13511
Chet Haasea1cff502012-02-21 13:43:44 -080013512 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013513 canvas.translate(-transX, -transY);
13514 canvas.concat(getMatrix());
13515 canvas.translate(transX, transY);
13516 }
13517 }
13518
Chet Haase21433372012-06-05 07:54:09 -070013519 // Deal with alpha if it is or used to be <1
13520 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013521 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013522 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013523 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013524 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013525 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013526 }
Chet Haasea1cff502012-02-21 13:43:44 -080013527 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013528 if (hasNoCache) {
13529 final int multipliedAlpha = (int) (255 * alpha);
13530 if (!onSetAlpha(multipliedAlpha)) {
13531 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013532 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013533 layerType != LAYER_TYPE_NONE) {
13534 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13535 }
Chet Haase9420abd2012-03-29 16:28:32 -070013536 if (useDisplayListProperties) {
13537 displayList.setAlpha(alpha * getAlpha());
13538 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013539 final int scrollX = hasDisplayList ? 0 : sx;
13540 final int scrollY = hasDisplayList ? 0 : sy;
13541 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13542 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013543 }
13544 } else {
13545 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013546 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013547 }
13548 }
13549 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013550 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013551 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013552 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013553 }
13554
Chet Haasea1cff502012-02-21 13:43:44 -080013555 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13556 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013557 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013558 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013559 } else {
13560 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013561 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013562 } else {
13563 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13564 }
13565 }
13566 }
13567
Chet Haase9420abd2012-03-29 16:28:32 -070013568 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013569 displayList = getDisplayList();
13570 if (!displayList.isValid()) {
13571 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13572 // to getDisplayList(), the display list will be marked invalid and we should not
13573 // try to use it again.
13574 displayList = null;
13575 hasDisplayList = false;
13576 }
13577 }
13578
13579 if (hasNoCache) {
13580 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013581 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013582 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013583 if (layer != null && layer.isValid()) {
13584 mLayerPaint.setAlpha((int) (alpha * 255));
13585 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13586 layerRendered = true;
13587 } else {
13588 final int scrollX = hasDisplayList ? 0 : sx;
13589 final int scrollY = hasDisplayList ? 0 : sy;
13590 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013591 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013592 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13593 }
13594 }
13595
13596 if (!layerRendered) {
13597 if (!hasDisplayList) {
13598 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013599 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13600 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013601 dispatchDraw(canvas);
13602 } else {
13603 draw(canvas);
13604 }
13605 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013606 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013607 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013608 }
13609 }
13610 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013611 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013612 Paint cachePaint;
13613
13614 if (layerType == LAYER_TYPE_NONE) {
13615 cachePaint = parent.mCachePaint;
13616 if (cachePaint == null) {
13617 cachePaint = new Paint();
13618 cachePaint.setDither(false);
13619 parent.mCachePaint = cachePaint;
13620 }
Chet Haase9420abd2012-03-29 16:28:32 -070013621 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013622 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013623 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13624 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013625 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013626 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013627 }
13628 } else {
13629 cachePaint = mLayerPaint;
13630 cachePaint.setAlpha((int) (alpha * 255));
13631 }
13632 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13633 }
13634
Chet Haasea1cff502012-02-21 13:43:44 -080013635 if (restoreTo >= 0) {
13636 canvas.restoreToCount(restoreTo);
13637 }
Chet Haase64a48c12012-02-13 16:33:29 -080013638
13639 if (a != null && !more) {
13640 if (!hardwareAccelerated && !a.getFillAfter()) {
13641 onSetAlpha(255);
13642 }
13643 parent.finishAnimatingView(this, a);
13644 }
13645
13646 if (more && hardwareAccelerated) {
13647 // invalidation is the trigger to recreate display lists, so if we're using
13648 // display lists to render, force an invalidate to allow the animation to
13649 // continue drawing another frame
13650 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013651 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013652 // alpha animations should cause the child to recreate its display list
13653 invalidate(true);
13654 }
13655 }
13656
13657 mRecreateDisplayList = false;
13658
13659 return more;
13660 }
13661
13662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663 * Manually render this view (and all of its children) to the given Canvas.
13664 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013665 * called. When implementing a view, implement
13666 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13667 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013668 *
13669 * @param canvas The Canvas to which the View is rendered.
13670 */
13671 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013672 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013673 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013674 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013675 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013677 /*
13678 * Draw traversal performs several drawing steps which must be executed
13679 * in the appropriate order:
13680 *
13681 * 1. Draw the background
13682 * 2. If necessary, save the canvas' layers to prepare for fading
13683 * 3. Draw view's content
13684 * 4. Draw children
13685 * 5. If necessary, draw the fading edges and restore layers
13686 * 6. Draw decorations (scrollbars for instance)
13687 */
13688
13689 // Step 1, draw the background, if needed
13690 int saveCount;
13691
Romain Guy24443ea2009-05-11 11:56:30 -070013692 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013693 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013694 if (background != null) {
13695 final int scrollX = mScrollX;
13696 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013697
Romain Guy24443ea2009-05-11 11:56:30 -070013698 if (mBackgroundSizeChanged) {
13699 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13700 mBackgroundSizeChanged = false;
13701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013702
Romain Guy24443ea2009-05-11 11:56:30 -070013703 if ((scrollX | scrollY) == 0) {
13704 background.draw(canvas);
13705 } else {
13706 canvas.translate(scrollX, scrollY);
13707 background.draw(canvas);
13708 canvas.translate(-scrollX, -scrollY);
13709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013710 }
13711 }
13712
13713 // skip step 2 & 5 if possible (common case)
13714 final int viewFlags = mViewFlags;
13715 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13716 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13717 if (!verticalEdges && !horizontalEdges) {
13718 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013719 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013720
13721 // Step 4, draw the children
13722 dispatchDraw(canvas);
13723
13724 // Step 6, draw decorations (scrollbars)
13725 onDrawScrollBars(canvas);
13726
13727 // we're done...
13728 return;
13729 }
13730
13731 /*
13732 * Here we do the full fledged routine...
13733 * (this is an uncommon case where speed matters less,
13734 * this is why we repeat some of the tests that have been
13735 * done above)
13736 */
13737
13738 boolean drawTop = false;
13739 boolean drawBottom = false;
13740 boolean drawLeft = false;
13741 boolean drawRight = false;
13742
13743 float topFadeStrength = 0.0f;
13744 float bottomFadeStrength = 0.0f;
13745 float leftFadeStrength = 0.0f;
13746 float rightFadeStrength = 0.0f;
13747
13748 // Step 2, save the canvas' layers
13749 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750
13751 final boolean offsetRequired = isPaddingOffsetRequired();
13752 if (offsetRequired) {
13753 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013754 }
13755
13756 int left = mScrollX + paddingLeft;
13757 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013758 int top = mScrollY + getFadeTop(offsetRequired);
13759 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013760
13761 if (offsetRequired) {
13762 right += getRightPaddingOffset();
13763 bottom += getBottomPaddingOffset();
13764 }
13765
13766 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013767 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013768 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769
13770 // clip the fade length if top and bottom fades overlap
13771 // overlapping fades produce odd-looking artifacts
13772 if (verticalEdges && (top + length > bottom - length)) {
13773 length = (bottom - top) / 2;
13774 }
13775
13776 // also clip horizontal fades if necessary
13777 if (horizontalEdges && (left + length > right - length)) {
13778 length = (right - left) / 2;
13779 }
13780
13781 if (verticalEdges) {
13782 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013783 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013785 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013786 }
13787
13788 if (horizontalEdges) {
13789 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013790 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013792 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013793 }
13794
13795 saveCount = canvas.getSaveCount();
13796
13797 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013798 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13800
13801 if (drawTop) {
13802 canvas.saveLayer(left, top, right, top + length, null, flags);
13803 }
13804
13805 if (drawBottom) {
13806 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13807 }
13808
13809 if (drawLeft) {
13810 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13811 }
13812
13813 if (drawRight) {
13814 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13815 }
13816 } else {
13817 scrollabilityCache.setFadeColor(solidColor);
13818 }
13819
13820 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013821 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013822
13823 // Step 4, draw the children
13824 dispatchDraw(canvas);
13825
13826 // Step 5, draw the fade effect and restore layers
13827 final Paint p = scrollabilityCache.paint;
13828 final Matrix matrix = scrollabilityCache.matrix;
13829 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013830
13831 if (drawTop) {
13832 matrix.setScale(1, fadeHeight * topFadeStrength);
13833 matrix.postTranslate(left, top);
13834 fade.setLocalMatrix(matrix);
13835 canvas.drawRect(left, top, right, top + length, p);
13836 }
13837
13838 if (drawBottom) {
13839 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13840 matrix.postRotate(180);
13841 matrix.postTranslate(left, bottom);
13842 fade.setLocalMatrix(matrix);
13843 canvas.drawRect(left, bottom - length, right, bottom, p);
13844 }
13845
13846 if (drawLeft) {
13847 matrix.setScale(1, fadeHeight * leftFadeStrength);
13848 matrix.postRotate(-90);
13849 matrix.postTranslate(left, top);
13850 fade.setLocalMatrix(matrix);
13851 canvas.drawRect(left, top, left + length, bottom, p);
13852 }
13853
13854 if (drawRight) {
13855 matrix.setScale(1, fadeHeight * rightFadeStrength);
13856 matrix.postRotate(90);
13857 matrix.postTranslate(right, top);
13858 fade.setLocalMatrix(matrix);
13859 canvas.drawRect(right - length, top, right, bottom, p);
13860 }
13861
13862 canvas.restoreToCount(saveCount);
13863
13864 // Step 6, draw decorations (scrollbars)
13865 onDrawScrollBars(canvas);
13866 }
13867
13868 /**
13869 * Override this if your view is known to always be drawn on top of a solid color background,
13870 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13871 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13872 * should be set to 0xFF.
13873 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013874 * @see #setVerticalFadingEdgeEnabled(boolean)
13875 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013876 *
13877 * @return The known solid color background for this view, or 0 if the color may vary
13878 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013879 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013880 public int getSolidColor() {
13881 return 0;
13882 }
13883
13884 /**
13885 * Build a human readable string representation of the specified view flags.
13886 *
13887 * @param flags the view flags to convert to a string
13888 * @return a String representing the supplied flags
13889 */
13890 private static String printFlags(int flags) {
13891 String output = "";
13892 int numFlags = 0;
13893 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13894 output += "TAKES_FOCUS";
13895 numFlags++;
13896 }
13897
13898 switch (flags & VISIBILITY_MASK) {
13899 case INVISIBLE:
13900 if (numFlags > 0) {
13901 output += " ";
13902 }
13903 output += "INVISIBLE";
13904 // USELESS HERE numFlags++;
13905 break;
13906 case GONE:
13907 if (numFlags > 0) {
13908 output += " ";
13909 }
13910 output += "GONE";
13911 // USELESS HERE numFlags++;
13912 break;
13913 default:
13914 break;
13915 }
13916 return output;
13917 }
13918
13919 /**
13920 * Build a human readable string representation of the specified private
13921 * view flags.
13922 *
13923 * @param privateFlags the private view flags to convert to a string
13924 * @return a String representing the supplied flags
13925 */
13926 private static String printPrivateFlags(int privateFlags) {
13927 String output = "";
13928 int numFlags = 0;
13929
Dianne Hackborn4702a852012-08-17 15:18:29 -070013930 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013931 output += "WANTS_FOCUS";
13932 numFlags++;
13933 }
13934
Dianne Hackborn4702a852012-08-17 15:18:29 -070013935 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013936 if (numFlags > 0) {
13937 output += " ";
13938 }
13939 output += "FOCUSED";
13940 numFlags++;
13941 }
13942
Dianne Hackborn4702a852012-08-17 15:18:29 -070013943 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 if (numFlags > 0) {
13945 output += " ";
13946 }
13947 output += "SELECTED";
13948 numFlags++;
13949 }
13950
Dianne Hackborn4702a852012-08-17 15:18:29 -070013951 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013952 if (numFlags > 0) {
13953 output += " ";
13954 }
13955 output += "IS_ROOT_NAMESPACE";
13956 numFlags++;
13957 }
13958
Dianne Hackborn4702a852012-08-17 15:18:29 -070013959 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 if (numFlags > 0) {
13961 output += " ";
13962 }
13963 output += "HAS_BOUNDS";
13964 numFlags++;
13965 }
13966
Dianne Hackborn4702a852012-08-17 15:18:29 -070013967 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013968 if (numFlags > 0) {
13969 output += " ";
13970 }
13971 output += "DRAWN";
13972 // USELESS HERE numFlags++;
13973 }
13974 return output;
13975 }
13976
13977 /**
13978 * <p>Indicates whether or not this view's layout will be requested during
13979 * the next hierarchy layout pass.</p>
13980 *
13981 * @return true if the layout will be forced during next layout pass
13982 */
13983 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013984 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013985 }
13986
13987 /**
Philip Milne7b757812012-09-19 18:13:44 -070013988 * Return true if o is a ViewGroup that is laying out using optical bounds.
13989 * @hide
13990 */
13991 public static boolean isLayoutModeOptical(Object o) {
13992 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
13993 }
13994
13995 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
13996 Insets parentInsets = mParent instanceof View ?
13997 ((View) mParent).getOpticalInsets() : Insets.NONE;
13998 Insets childInsets = getOpticalInsets();
13999 return setFrame(
14000 left + parentInsets.left - childInsets.left,
14001 top + parentInsets.top - childInsets.top,
14002 right + parentInsets.left + childInsets.right,
14003 bottom + parentInsets.top + childInsets.bottom);
14004 }
14005
14006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014007 * Assign a size and position to a view and all of its
14008 * descendants
14009 *
14010 * <p>This is the second phase of the layout mechanism.
14011 * (The first is measuring). In this phase, each parent calls
14012 * layout on all of its children to position them.
14013 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014014 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014015 *
Chet Haase9c087442011-01-12 16:20:16 -080014016 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 * Derived classes with children should override
14018 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014019 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014020 *
14021 * @param l Left position, relative to parent
14022 * @param t Top position, relative to parent
14023 * @param r Right position, relative to parent
14024 * @param b Bottom position, relative to parent
14025 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014026 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014027 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014028 int oldL = mLeft;
14029 int oldT = mTop;
14030 int oldB = mBottom;
14031 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014032 boolean changed = isLayoutModeOptical(mParent) ?
14033 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014034 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014035 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014036 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014037
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014038 ListenerInfo li = mListenerInfo;
14039 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014040 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014041 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014042 int numListeners = listenersCopy.size();
14043 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014044 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014045 }
14046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014047 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014048 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014049 }
14050
14051 /**
14052 * Called from layout when this view should
14053 * assign a size and position to each of its children.
14054 *
14055 * Derived classes with children should override
14056 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014057 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 * @param changed This is a new size or position for this view
14059 * @param left Left position, relative to parent
14060 * @param top Top position, relative to parent
14061 * @param right Right position, relative to parent
14062 * @param bottom Bottom position, relative to parent
14063 */
14064 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14065 }
14066
14067 /**
14068 * Assign a size and position to this view.
14069 *
14070 * This is called from layout.
14071 *
14072 * @param left Left position, relative to parent
14073 * @param top Top position, relative to parent
14074 * @param right Right position, relative to parent
14075 * @param bottom Bottom position, relative to parent
14076 * @return true if the new size and position are different than the
14077 * previous ones
14078 * {@hide}
14079 */
14080 protected boolean setFrame(int left, int top, int right, int bottom) {
14081 boolean changed = false;
14082
14083 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014084 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 + right + "," + bottom + ")");
14086 }
14087
14088 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14089 changed = true;
14090
14091 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014092 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014094 int oldWidth = mRight - mLeft;
14095 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014096 int newWidth = right - left;
14097 int newHeight = bottom - top;
14098 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14099
14100 // Invalidate our old position
14101 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014102
14103 mLeft = left;
14104 mTop = top;
14105 mRight = right;
14106 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014107 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014108 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014110
Dianne Hackborn4702a852012-08-17 15:18:29 -070014111 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014113
Chet Haase75755e22011-07-18 17:48:25 -070014114 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014115 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014116 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014117 if (mTransformationInfo != null) {
14118 mTransformationInfo.mMatrixDirty = true;
14119 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014121 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14122 }
14123
14124 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14125 // If we are visible, force the DRAWN bit to on so that
14126 // this invalidate will go through (at least to our parent).
14127 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014128 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014129 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014130 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014131 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014132 // parent display list may need to be recreated based on a change in the bounds
14133 // of any child
14134 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014135 }
14136
14137 // Reset drawn bit to original value (invalidate turns it off)
14138 mPrivateFlags |= drawn;
14139
14140 mBackgroundSizeChanged = true;
14141 }
14142 return changed;
14143 }
14144
14145 /**
14146 * Finalize inflating a view from XML. This is called as the last phase
14147 * of inflation, after all child views have been added.
14148 *
14149 * <p>Even if the subclass overrides onFinishInflate, they should always be
14150 * sure to call the super method, so that we get called.
14151 */
14152 protected void onFinishInflate() {
14153 }
14154
14155 /**
14156 * Returns the resources associated with this view.
14157 *
14158 * @return Resources object.
14159 */
14160 public Resources getResources() {
14161 return mResources;
14162 }
14163
14164 /**
14165 * Invalidates the specified Drawable.
14166 *
14167 * @param drawable the drawable to invalidate
14168 */
14169 public void invalidateDrawable(Drawable drawable) {
14170 if (verifyDrawable(drawable)) {
14171 final Rect dirty = drawable.getBounds();
14172 final int scrollX = mScrollX;
14173 final int scrollY = mScrollY;
14174
14175 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14176 dirty.right + scrollX, dirty.bottom + scrollY);
14177 }
14178 }
14179
14180 /**
14181 * Schedules an action on a drawable to occur at a specified time.
14182 *
14183 * @param who the recipient of the action
14184 * @param what the action to run on the drawable
14185 * @param when the time at which the action must occur. Uses the
14186 * {@link SystemClock#uptimeMillis} timebase.
14187 */
14188 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014189 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014190 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014191 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014192 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14193 Choreographer.CALLBACK_ANIMATION, what, who,
14194 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014195 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014196 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014198 }
14199 }
14200
14201 /**
14202 * Cancels a scheduled action on a drawable.
14203 *
14204 * @param who the recipient of the action
14205 * @param what the action to cancel
14206 */
14207 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014208 if (verifyDrawable(who) && what != null) {
14209 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014210 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14211 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014212 } else {
14213 ViewRootImpl.getRunQueue().removeCallbacks(what);
14214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215 }
14216 }
14217
14218 /**
14219 * Unschedule any events associated with the given Drawable. This can be
14220 * used when selecting a new Drawable into a view, so that the previous
14221 * one is completely unscheduled.
14222 *
14223 * @param who The Drawable to unschedule.
14224 *
14225 * @see #drawableStateChanged
14226 */
14227 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014228 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014229 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14230 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014231 }
14232 }
14233
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014234 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014235 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14236 * that the View directionality can and will be resolved before its Drawables.
14237 *
14238 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014239 *
14240 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014241 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014242 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014243 if (canResolveLayoutDirection()) {
14244 if (mBackground != null) {
14245 mBackground.setLayoutDirection(getLayoutDirection());
14246 }
14247 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14248 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014249 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014250 }
14251
14252 /**
14253 * Called when layout direction has been resolved.
14254 *
14255 * The default implementation does nothing.
14256 *
14257 * @param layoutDirection The resolved layout direction.
14258 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014259 * @see #LAYOUT_DIRECTION_LTR
14260 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014261 *
14262 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014263 */
14264 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014265 }
14266
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014267 /**
14268 * @hide
14269 */
14270 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014271 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14272 }
14273
14274 private boolean isDrawablesResolved() {
14275 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14276 }
14277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014278 /**
14279 * If your view subclass is displaying its own Drawable objects, it should
14280 * override this function and return true for any Drawable it is
14281 * displaying. This allows animations for those drawables to be
14282 * scheduled.
14283 *
14284 * <p>Be sure to call through to the super class when overriding this
14285 * function.
14286 *
14287 * @param who The Drawable to verify. Return true if it is one you are
14288 * displaying, else return the result of calling through to the
14289 * super class.
14290 *
14291 * @return boolean If true than the Drawable is being displayed in the
14292 * view; else false and it is not allowed to animate.
14293 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014294 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14295 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014296 */
14297 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014298 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014299 }
14300
14301 /**
14302 * This function is called whenever the state of the view changes in such
14303 * a way that it impacts the state of drawables being shown.
14304 *
14305 * <p>Be sure to call through to the superclass when overriding this
14306 * function.
14307 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014308 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014309 */
14310 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014311 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 if (d != null && d.isStateful()) {
14313 d.setState(getDrawableState());
14314 }
14315 }
14316
14317 /**
14318 * Call this to force a view to update its drawable state. This will cause
14319 * drawableStateChanged to be called on this view. Views that are interested
14320 * in the new state should call getDrawableState.
14321 *
14322 * @see #drawableStateChanged
14323 * @see #getDrawableState
14324 */
14325 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014326 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014327 drawableStateChanged();
14328
14329 ViewParent parent = mParent;
14330 if (parent != null) {
14331 parent.childDrawableStateChanged(this);
14332 }
14333 }
14334
14335 /**
14336 * Return an array of resource IDs of the drawable states representing the
14337 * current state of the view.
14338 *
14339 * @return The current drawable state
14340 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014341 * @see Drawable#setState(int[])
14342 * @see #drawableStateChanged()
14343 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014344 */
14345 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014346 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014347 return mDrawableState;
14348 } else {
14349 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014350 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014351 return mDrawableState;
14352 }
14353 }
14354
14355 /**
14356 * Generate the new {@link android.graphics.drawable.Drawable} state for
14357 * this view. This is called by the view
14358 * system when the cached Drawable state is determined to be invalid. To
14359 * retrieve the current state, you should use {@link #getDrawableState}.
14360 *
14361 * @param extraSpace if non-zero, this is the number of extra entries you
14362 * would like in the returned array in which you can place your own
14363 * states.
14364 *
14365 * @return Returns an array holding the current {@link Drawable} state of
14366 * the view.
14367 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014368 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014369 */
14370 protected int[] onCreateDrawableState(int extraSpace) {
14371 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14372 mParent instanceof View) {
14373 return ((View) mParent).onCreateDrawableState(extraSpace);
14374 }
14375
14376 int[] drawableState;
14377
14378 int privateFlags = mPrivateFlags;
14379
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014380 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014381 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014382 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14383 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014384 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014385 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014386 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014387 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14388 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014389 // This is set if HW acceleration is requested, even if the current
14390 // process doesn't allow it. This is just to allow app preview
14391 // windows to better match their app.
14392 viewStateIndex |= VIEW_STATE_ACCELERATED;
14393 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014394 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014395
Christopher Tate3d4bf172011-03-28 16:16:46 -070014396 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014397 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14398 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014400 drawableState = VIEW_STATE_SETS[viewStateIndex];
14401
14402 //noinspection ConstantIfStatement
14403 if (false) {
14404 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14405 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014406 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14408 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014409 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014410 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 + ": " + Arrays.toString(drawableState));
14412 }
14413
14414 if (extraSpace == 0) {
14415 return drawableState;
14416 }
14417
14418 final int[] fullState;
14419 if (drawableState != null) {
14420 fullState = new int[drawableState.length + extraSpace];
14421 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14422 } else {
14423 fullState = new int[extraSpace];
14424 }
14425
14426 return fullState;
14427 }
14428
14429 /**
14430 * Merge your own state values in <var>additionalState</var> into the base
14431 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014432 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014433 *
14434 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014435 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014436 * own additional state values.
14437 *
14438 * @param additionalState The additional state values you would like
14439 * added to <var>baseState</var>; this array is not modified.
14440 *
14441 * @return As a convenience, the <var>baseState</var> array you originally
14442 * passed into the function is returned.
14443 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014444 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014445 */
14446 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14447 final int N = baseState.length;
14448 int i = N - 1;
14449 while (i >= 0 && baseState[i] == 0) {
14450 i--;
14451 }
14452 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14453 return baseState;
14454 }
14455
14456 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014457 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14458 * on all Drawable objects associated with this view.
14459 */
14460 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014461 if (mBackground != null) {
14462 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014463 }
14464 }
14465
14466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 * Sets the background color for this view.
14468 * @param color the color of the background
14469 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014470 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014471 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014472 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014473 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014474 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014475 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014476 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014477 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 }
14480
14481 /**
14482 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014483 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014484 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014485 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014486 * @attr ref android.R.styleable#View_background
14487 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014488 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489 public void setBackgroundResource(int resid) {
14490 if (resid != 0 && resid == mBackgroundResource) {
14491 return;
14492 }
14493
14494 Drawable d= null;
14495 if (resid != 0) {
14496 d = mResources.getDrawable(resid);
14497 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014498 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014499
14500 mBackgroundResource = resid;
14501 }
14502
14503 /**
14504 * Set the background to a given Drawable, or remove the background. If the
14505 * background has padding, this View's padding is set to the background's
14506 * padding. However, when a background is removed, this View's padding isn't
14507 * touched. If setting the padding is desired, please use
14508 * {@link #setPadding(int, int, int, int)}.
14509 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014510 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014511 * background
14512 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014513 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014514 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014515 setBackgroundDrawable(background);
14516 }
14517
14518 /**
14519 * @deprecated use {@link #setBackground(Drawable)} instead
14520 */
14521 @Deprecated
14522 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014523 computeOpaqueFlags();
14524
Philip Milne6c8ea062012-04-03 17:38:43 -070014525 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014526 return;
14527 }
14528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014529 boolean requestLayout = false;
14530
14531 mBackgroundResource = 0;
14532
14533 /*
14534 * Regardless of whether we're setting a new background or not, we want
14535 * to clear the previous drawable.
14536 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014537 if (mBackground != null) {
14538 mBackground.setCallback(null);
14539 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014540 }
14541
Philip Milne6c8ea062012-04-03 17:38:43 -070014542 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014543 Rect padding = sThreadLocal.get();
14544 if (padding == null) {
14545 padding = new Rect();
14546 sThreadLocal.set(padding);
14547 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014548 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014549 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014550 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014551 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014552 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014553 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014554 mUserPaddingLeftInitial = padding.right;
14555 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014556 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014557 break;
14558 case LAYOUT_DIRECTION_LTR:
14559 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014560 mUserPaddingLeftInitial = padding.left;
14561 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014562 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014564 }
14565
14566 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14567 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014568 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14569 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014570 requestLayout = true;
14571 }
14572
Philip Milne6c8ea062012-04-03 17:38:43 -070014573 background.setCallback(this);
14574 if (background.isStateful()) {
14575 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014576 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014577 background.setVisible(getVisibility() == VISIBLE, false);
14578 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014579
Dianne Hackborn4702a852012-08-17 15:18:29 -070014580 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14581 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14582 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014583 requestLayout = true;
14584 }
14585 } else {
14586 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014587 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014588
Dianne Hackborn4702a852012-08-17 15:18:29 -070014589 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 /*
14591 * This view ONLY drew the background before and we're removing
14592 * the background, so now it won't draw anything
14593 * (hence we SKIP_DRAW)
14594 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014595 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14596 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014597 }
14598
14599 /*
14600 * When the background is set, we try to apply its padding to this
14601 * View. When the background is removed, we don't touch this View's
14602 * padding. This is noted in the Javadocs. Hence, we don't need to
14603 * requestLayout(), the invalidate() below is sufficient.
14604 */
14605
14606 // The old background's minimum size could have affected this
14607 // View's layout, so let's requestLayout
14608 requestLayout = true;
14609 }
14610
Romain Guy8f1344f52009-05-15 16:03:59 -070014611 computeOpaqueFlags();
14612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014613 if (requestLayout) {
14614 requestLayout();
14615 }
14616
14617 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014618 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014619 }
14620
14621 /**
14622 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014623 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014625 *
14626 * @see #setBackground(Drawable)
14627 *
14628 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014629 */
14630 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014631 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014632 }
14633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014634 /**
14635 * Sets the padding. The view may add on the space required to display
14636 * the scrollbars, depending on the style and visibility of the scrollbars.
14637 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14638 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14639 * from the values set in this call.
14640 *
14641 * @attr ref android.R.styleable#View_padding
14642 * @attr ref android.R.styleable#View_paddingBottom
14643 * @attr ref android.R.styleable#View_paddingLeft
14644 * @attr ref android.R.styleable#View_paddingRight
14645 * @attr ref android.R.styleable#View_paddingTop
14646 * @param left the left padding in pixels
14647 * @param top the top padding in pixels
14648 * @param right the right padding in pixels
14649 * @param bottom the bottom padding in pixels
14650 */
14651 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014652 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014653
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014654 mUserPaddingStart = UNDEFINED_PADDING;
14655 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014656
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014657 mUserPaddingLeftInitial = left;
14658 mUserPaddingRightInitial = right;
14659
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014660 internalSetPadding(left, top, right, bottom);
14661 }
14662
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014663 /**
14664 * @hide
14665 */
14666 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014667 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014668 mUserPaddingRight = right;
14669 mUserPaddingBottom = bottom;
14670
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014671 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014672 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014673
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014674 // Common case is there are no scroll bars.
14675 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014676 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014677 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014678 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014679 switch (mVerticalScrollbarPosition) {
14680 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014681 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014682 left += offset;
14683 } else {
14684 right += offset;
14685 }
14686 break;
Adam Powell20232d02010-12-08 21:08:53 -080014687 case SCROLLBAR_POSITION_RIGHT:
14688 right += offset;
14689 break;
14690 case SCROLLBAR_POSITION_LEFT:
14691 left += offset;
14692 break;
14693 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014694 }
Adam Powell20232d02010-12-08 21:08:53 -080014695 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014696 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14697 ? 0 : getHorizontalScrollbarHeight();
14698 }
14699 }
Romain Guy8506ab42009-06-11 17:35:47 -070014700
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014701 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014702 changed = true;
14703 mPaddingLeft = left;
14704 }
14705 if (mPaddingTop != top) {
14706 changed = true;
14707 mPaddingTop = top;
14708 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014709 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014710 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014711 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014712 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014713 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014714 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014715 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014716 }
14717
14718 if (changed) {
14719 requestLayout();
14720 }
14721 }
14722
14723 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014724 * Sets the relative padding. The view may add on the space required to display
14725 * the scrollbars, depending on the style and visibility of the scrollbars.
14726 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14727 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14728 * from the values set in this call.
14729 *
14730 * @attr ref android.R.styleable#View_padding
14731 * @attr ref android.R.styleable#View_paddingBottom
14732 * @attr ref android.R.styleable#View_paddingStart
14733 * @attr ref android.R.styleable#View_paddingEnd
14734 * @attr ref android.R.styleable#View_paddingTop
14735 * @param start the start padding in pixels
14736 * @param top the top padding in pixels
14737 * @param end the end padding in pixels
14738 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014739 */
14740 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014741 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014742
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014743 mUserPaddingStart = start;
14744 mUserPaddingEnd = end;
14745
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014746 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014747 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014748 mUserPaddingLeftInitial = end;
14749 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014750 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014751 break;
14752 case LAYOUT_DIRECTION_LTR:
14753 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014754 mUserPaddingLeftInitial = start;
14755 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014756 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014757 }
14758 }
14759
14760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014761 * Returns the top padding of this view.
14762 *
14763 * @return the top padding in pixels
14764 */
14765 public int getPaddingTop() {
14766 return mPaddingTop;
14767 }
14768
14769 /**
14770 * Returns the bottom padding of this view. If there are inset and enabled
14771 * scrollbars, this value may include the space required to display the
14772 * scrollbars as well.
14773 *
14774 * @return the bottom padding in pixels
14775 */
14776 public int getPaddingBottom() {
14777 return mPaddingBottom;
14778 }
14779
14780 /**
14781 * Returns the left padding of this view. If there are inset and enabled
14782 * scrollbars, this value may include the space required to display the
14783 * scrollbars as well.
14784 *
14785 * @return the left padding in pixels
14786 */
14787 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014788 if (!isPaddingResolved()) {
14789 resolvePadding();
14790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014791 return mPaddingLeft;
14792 }
14793
14794 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014795 * Returns the start padding of this view depending on its resolved layout direction.
14796 * If there are inset and enabled scrollbars, this value may include the space
14797 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014798 *
14799 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014800 */
14801 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014802 if (!isPaddingResolved()) {
14803 resolvePadding();
14804 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014805 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014806 mPaddingRight : mPaddingLeft;
14807 }
14808
14809 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014810 * Returns the right padding of this view. If there are inset and enabled
14811 * scrollbars, this value may include the space required to display the
14812 * scrollbars as well.
14813 *
14814 * @return the right padding in pixels
14815 */
14816 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014817 if (!isPaddingResolved()) {
14818 resolvePadding();
14819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014820 return mPaddingRight;
14821 }
14822
14823 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014824 * Returns the end padding of this view depending on its resolved layout direction.
14825 * If there are inset and enabled scrollbars, this value may include the space
14826 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014827 *
14828 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014829 */
14830 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014831 if (!isPaddingResolved()) {
14832 resolvePadding();
14833 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014834 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014835 mPaddingLeft : mPaddingRight;
14836 }
14837
14838 /**
14839 * Return if the padding as been set thru relative values
14840 * {@link #setPaddingRelative(int, int, int, int)} or thru
14841 * @attr ref android.R.styleable#View_paddingStart or
14842 * @attr ref android.R.styleable#View_paddingEnd
14843 *
14844 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014845 */
14846 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014847 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014848 }
14849
Philip Milne7b757812012-09-19 18:13:44 -070014850 Insets computeOpticalInsets() {
14851 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014852 }
14853
14854 /**
14855 * @hide
14856 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014857 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014858 if (isRtlCompatibilityMode()) {
14859 mPaddingLeft = mUserPaddingLeftInitial;
14860 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014861 return;
14862 }
14863 if (isLayoutRtl()) {
14864 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14865 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014866 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014867 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14868 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014869 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014870 }
14871
14872 /**
14873 * @hide
14874 */
Philip Milne7b757812012-09-19 18:13:44 -070014875 public Insets getOpticalInsets() {
14876 if (mLayoutInsets == null) {
14877 mLayoutInsets = computeOpticalInsets();
14878 }
14879 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070014880 }
14881
14882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014883 * Changes the selection state of this view. A view can be selected or not.
14884 * Note that selection is not the same as focus. Views are typically
14885 * selected in the context of an AdapterView like ListView or GridView;
14886 * the selected view is the view that is highlighted.
14887 *
14888 * @param selected true if the view must be selected, false otherwise
14889 */
14890 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014891 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14892 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014893 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014894 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014895 refreshDrawableState();
14896 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014897 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14898 notifyAccessibilityStateChanged();
14899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014900 }
14901 }
14902
14903 /**
14904 * Dispatch setSelected to all of this View's children.
14905 *
14906 * @see #setSelected(boolean)
14907 *
14908 * @param selected The new selected state
14909 */
14910 protected void dispatchSetSelected(boolean selected) {
14911 }
14912
14913 /**
14914 * Indicates the selection state of this view.
14915 *
14916 * @return true if the view is selected, false otherwise
14917 */
14918 @ViewDebug.ExportedProperty
14919 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014920 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014921 }
14922
14923 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014924 * Changes the activated state of this view. A view can be activated or not.
14925 * Note that activation is not the same as selection. Selection is
14926 * a transient property, representing the view (hierarchy) the user is
14927 * currently interacting with. Activation is a longer-term state that the
14928 * user can move views in and out of. For example, in a list view with
14929 * single or multiple selection enabled, the views in the current selection
14930 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14931 * here.) The activated state is propagated down to children of the view it
14932 * is set on.
14933 *
14934 * @param activated true if the view must be activated, false otherwise
14935 */
14936 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014937 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14938 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014939 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014940 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014941 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014942 }
14943 }
14944
14945 /**
14946 * Dispatch setActivated to all of this View's children.
14947 *
14948 * @see #setActivated(boolean)
14949 *
14950 * @param activated The new activated state
14951 */
14952 protected void dispatchSetActivated(boolean activated) {
14953 }
14954
14955 /**
14956 * Indicates the activation state of this view.
14957 *
14958 * @return true if the view is activated, false otherwise
14959 */
14960 @ViewDebug.ExportedProperty
14961 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014962 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014963 }
14964
14965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014966 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14967 * observer can be used to get notifications when global events, like
14968 * layout, happen.
14969 *
14970 * The returned ViewTreeObserver observer is not guaranteed to remain
14971 * valid for the lifetime of this View. If the caller of this method keeps
14972 * a long-lived reference to ViewTreeObserver, it should always check for
14973 * the return value of {@link ViewTreeObserver#isAlive()}.
14974 *
14975 * @return The ViewTreeObserver for this view's hierarchy.
14976 */
14977 public ViewTreeObserver getViewTreeObserver() {
14978 if (mAttachInfo != null) {
14979 return mAttachInfo.mTreeObserver;
14980 }
14981 if (mFloatingTreeObserver == null) {
14982 mFloatingTreeObserver = new ViewTreeObserver();
14983 }
14984 return mFloatingTreeObserver;
14985 }
14986
14987 /**
14988 * <p>Finds the topmost view in the current view hierarchy.</p>
14989 *
14990 * @return the topmost view containing this view
14991 */
14992 public View getRootView() {
14993 if (mAttachInfo != null) {
14994 final View v = mAttachInfo.mRootView;
14995 if (v != null) {
14996 return v;
14997 }
14998 }
Romain Guy8506ab42009-06-11 17:35:47 -070014999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015000 View parent = this;
15001
15002 while (parent.mParent != null && parent.mParent instanceof View) {
15003 parent = (View) parent.mParent;
15004 }
15005
15006 return parent;
15007 }
15008
15009 /**
15010 * <p>Computes the coordinates of this view on the screen. The argument
15011 * must be an array of two integers. After the method returns, the array
15012 * contains the x and y location in that order.</p>
15013 *
15014 * @param location an array of two integers in which to hold the coordinates
15015 */
15016 public void getLocationOnScreen(int[] location) {
15017 getLocationInWindow(location);
15018
15019 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015020 if (info != null) {
15021 location[0] += info.mWindowLeft;
15022 location[1] += info.mWindowTop;
15023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015024 }
15025
15026 /**
15027 * <p>Computes the coordinates of this view in its window. The argument
15028 * must be an array of two integers. After the method returns, the array
15029 * contains the x and y location in that order.</p>
15030 *
15031 * @param location an array of two integers in which to hold the coordinates
15032 */
15033 public void getLocationInWindow(int[] location) {
15034 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015035 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015036 }
15037
Gilles Debunne6583ce52011-12-06 18:09:02 -080015038 if (mAttachInfo == null) {
15039 // When the view is not attached to a window, this method does not make sense
15040 location[0] = location[1] = 0;
15041 return;
15042 }
15043
Gilles Debunnecea45132011-11-24 02:19:27 +010015044 float[] position = mAttachInfo.mTmpTransformLocation;
15045 position[0] = position[1] = 0.0f;
15046
15047 if (!hasIdentityMatrix()) {
15048 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015050
Gilles Debunnecea45132011-11-24 02:19:27 +010015051 position[0] += mLeft;
15052 position[1] += mTop;
15053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015054 ViewParent viewParent = mParent;
15055 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015056 final View view = (View) viewParent;
15057
15058 position[0] -= view.mScrollX;
15059 position[1] -= view.mScrollY;
15060
15061 if (!view.hasIdentityMatrix()) {
15062 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015063 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015064
15065 position[0] += view.mLeft;
15066 position[1] += view.mTop;
15067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015068 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015069 }
Romain Guy8506ab42009-06-11 17:35:47 -070015070
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015071 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015072 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015073 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15074 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015075 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015076
15077 location[0] = (int) (position[0] + 0.5f);
15078 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015079 }
15080
15081 /**
15082 * {@hide}
15083 * @param id the id of the view to be found
15084 * @return the view of the specified id, null if cannot be found
15085 */
15086 protected View findViewTraversal(int id) {
15087 if (id == mID) {
15088 return this;
15089 }
15090 return null;
15091 }
15092
15093 /**
15094 * {@hide}
15095 * @param tag the tag of the view to be found
15096 * @return the view of specified tag, null if cannot be found
15097 */
15098 protected View findViewWithTagTraversal(Object tag) {
15099 if (tag != null && tag.equals(mTag)) {
15100 return this;
15101 }
15102 return null;
15103 }
15104
15105 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015106 * {@hide}
15107 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015108 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015109 * @return The first view that matches the predicate or null.
15110 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015111 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015112 if (predicate.apply(this)) {
15113 return this;
15114 }
15115 return null;
15116 }
15117
15118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015119 * Look for a child view with the given id. If this view has the given
15120 * id, return this view.
15121 *
15122 * @param id The id to search for.
15123 * @return The view that has the given id in the hierarchy or null
15124 */
15125 public final View findViewById(int id) {
15126 if (id < 0) {
15127 return null;
15128 }
15129 return findViewTraversal(id);
15130 }
15131
15132 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015133 * Finds a view by its unuque and stable accessibility id.
15134 *
15135 * @param accessibilityId The searched accessibility id.
15136 * @return The found view.
15137 */
15138 final View findViewByAccessibilityId(int accessibilityId) {
15139 if (accessibilityId < 0) {
15140 return null;
15141 }
15142 return findViewByAccessibilityIdTraversal(accessibilityId);
15143 }
15144
15145 /**
15146 * Performs the traversal to find a view by its unuque and stable accessibility id.
15147 *
15148 * <strong>Note:</strong>This method does not stop at the root namespace
15149 * boundary since the user can touch the screen at an arbitrary location
15150 * potentially crossing the root namespace bounday which will send an
15151 * accessibility event to accessibility services and they should be able
15152 * to obtain the event source. Also accessibility ids are guaranteed to be
15153 * unique in the window.
15154 *
15155 * @param accessibilityId The accessibility id.
15156 * @return The found view.
15157 */
15158 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15159 if (getAccessibilityViewId() == accessibilityId) {
15160 return this;
15161 }
15162 return null;
15163 }
15164
15165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015166 * Look for a child view with the given tag. If this view has the given
15167 * tag, return this view.
15168 *
15169 * @param tag The tag to search for, using "tag.equals(getTag())".
15170 * @return The View that has the given tag in the hierarchy or null
15171 */
15172 public final View findViewWithTag(Object tag) {
15173 if (tag == null) {
15174 return null;
15175 }
15176 return findViewWithTagTraversal(tag);
15177 }
15178
15179 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015180 * {@hide}
15181 * Look for a child view that matches the specified predicate.
15182 * If this view matches the predicate, return this view.
15183 *
15184 * @param predicate The predicate to evaluate.
15185 * @return The first view that matches the predicate or null.
15186 */
15187 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015188 return findViewByPredicateTraversal(predicate, null);
15189 }
15190
15191 /**
15192 * {@hide}
15193 * Look for a child view that matches the specified predicate,
15194 * starting with the specified view and its descendents and then
15195 * recusively searching the ancestors and siblings of that view
15196 * until this view is reached.
15197 *
15198 * This method is useful in cases where the predicate does not match
15199 * a single unique view (perhaps multiple views use the same id)
15200 * and we are trying to find the view that is "closest" in scope to the
15201 * starting view.
15202 *
15203 * @param start The view to start from.
15204 * @param predicate The predicate to evaluate.
15205 * @return The first view that matches the predicate or null.
15206 */
15207 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15208 View childToSkip = null;
15209 for (;;) {
15210 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15211 if (view != null || start == this) {
15212 return view;
15213 }
15214
15215 ViewParent parent = start.getParent();
15216 if (parent == null || !(parent instanceof View)) {
15217 return null;
15218 }
15219
15220 childToSkip = start;
15221 start = (View) parent;
15222 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015223 }
15224
15225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015226 * Sets the identifier for this view. The identifier does not have to be
15227 * unique in this view's hierarchy. The identifier should be a positive
15228 * number.
15229 *
15230 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015231 * @see #getId()
15232 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015233 *
15234 * @param id a number used to identify the view
15235 *
15236 * @attr ref android.R.styleable#View_id
15237 */
15238 public void setId(int id) {
15239 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015240 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15241 mID = generateViewId();
15242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015243 }
15244
15245 /**
15246 * {@hide}
15247 *
15248 * @param isRoot true if the view belongs to the root namespace, false
15249 * otherwise
15250 */
15251 public void setIsRootNamespace(boolean isRoot) {
15252 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015253 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015254 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015255 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015256 }
15257 }
15258
15259 /**
15260 * {@hide}
15261 *
15262 * @return true if the view belongs to the root namespace, false otherwise
15263 */
15264 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015265 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015266 }
15267
15268 /**
15269 * Returns this view's identifier.
15270 *
15271 * @return a positive integer used to identify the view or {@link #NO_ID}
15272 * if the view has no ID
15273 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015274 * @see #setId(int)
15275 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015276 * @attr ref android.R.styleable#View_id
15277 */
15278 @ViewDebug.CapturedViewProperty
15279 public int getId() {
15280 return mID;
15281 }
15282
15283 /**
15284 * Returns this view's tag.
15285 *
15286 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015287 *
15288 * @see #setTag(Object)
15289 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015290 */
15291 @ViewDebug.ExportedProperty
15292 public Object getTag() {
15293 return mTag;
15294 }
15295
15296 /**
15297 * Sets the tag associated with this view. A tag can be used to mark
15298 * a view in its hierarchy and does not have to be unique within the
15299 * hierarchy. Tags can also be used to store data within a view without
15300 * resorting to another data structure.
15301 *
15302 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015303 *
15304 * @see #getTag()
15305 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015306 */
15307 public void setTag(final Object tag) {
15308 mTag = tag;
15309 }
15310
15311 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015312 * Returns the tag associated with this view and the specified key.
15313 *
15314 * @param key The key identifying the tag
15315 *
15316 * @return the Object stored in this view as a tag
15317 *
15318 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015319 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015320 */
15321 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015322 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015323 return null;
15324 }
15325
15326 /**
15327 * Sets a tag associated with this view and a key. A tag can be used
15328 * to mark a view in its hierarchy and does not have to be unique within
15329 * the hierarchy. Tags can also be used to store data within a view
15330 * without resorting to another data structure.
15331 *
15332 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015333 * application to ensure it is unique (see the <a
15334 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15335 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015336 * the Android framework or not associated with any package will cause
15337 * an {@link IllegalArgumentException} to be thrown.
15338 *
15339 * @param key The key identifying the tag
15340 * @param tag An Object to tag the view with
15341 *
15342 * @throws IllegalArgumentException If they specified key is not valid
15343 *
15344 * @see #setTag(Object)
15345 * @see #getTag(int)
15346 */
15347 public void setTag(int key, final Object tag) {
15348 // If the package id is 0x00 or 0x01, it's either an undefined package
15349 // or a framework id
15350 if ((key >>> 24) < 2) {
15351 throw new IllegalArgumentException("The key must be an application-specific "
15352 + "resource id.");
15353 }
15354
Adam Powell2b2f6d62011-09-23 11:15:39 -070015355 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015356 }
15357
15358 /**
15359 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15360 * framework id.
15361 *
15362 * @hide
15363 */
15364 public void setTagInternal(int key, Object tag) {
15365 if ((key >>> 24) != 0x1) {
15366 throw new IllegalArgumentException("The key must be a framework-specific "
15367 + "resource id.");
15368 }
15369
Adam Powell2b2f6d62011-09-23 11:15:39 -070015370 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015371 }
15372
Adam Powell2b2f6d62011-09-23 11:15:39 -070015373 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015374 if (mKeyedTags == null) {
15375 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015376 }
15377
Adam Powell7db82ac2011-09-22 19:44:04 -070015378 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015379 }
15380
15381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015382 * Prints information about this view in the log output, with the tag
15383 * {@link #VIEW_LOG_TAG}.
15384 *
15385 * @hide
15386 */
15387 public void debug() {
15388 debug(0);
15389 }
15390
15391 /**
15392 * Prints information about this view in the log output, with the tag
15393 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15394 * indentation defined by the <code>depth</code>.
15395 *
15396 * @param depth the indentation level
15397 *
15398 * @hide
15399 */
15400 protected void debug(int depth) {
15401 String output = debugIndent(depth - 1);
15402
15403 output += "+ " + this;
15404 int id = getId();
15405 if (id != -1) {
15406 output += " (id=" + id + ")";
15407 }
15408 Object tag = getTag();
15409 if (tag != null) {
15410 output += " (tag=" + tag + ")";
15411 }
15412 Log.d(VIEW_LOG_TAG, output);
15413
Dianne Hackborn4702a852012-08-17 15:18:29 -070015414 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015415 output = debugIndent(depth) + " FOCUSED";
15416 Log.d(VIEW_LOG_TAG, output);
15417 }
15418
15419 output = debugIndent(depth);
15420 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15421 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15422 + "} ";
15423 Log.d(VIEW_LOG_TAG, output);
15424
15425 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15426 || mPaddingBottom != 0) {
15427 output = debugIndent(depth);
15428 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15429 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15430 Log.d(VIEW_LOG_TAG, output);
15431 }
15432
15433 output = debugIndent(depth);
15434 output += "mMeasureWidth=" + mMeasuredWidth +
15435 " mMeasureHeight=" + mMeasuredHeight;
15436 Log.d(VIEW_LOG_TAG, output);
15437
15438 output = debugIndent(depth);
15439 if (mLayoutParams == null) {
15440 output += "BAD! no layout params";
15441 } else {
15442 output = mLayoutParams.debug(output);
15443 }
15444 Log.d(VIEW_LOG_TAG, output);
15445
15446 output = debugIndent(depth);
15447 output += "flags={";
15448 output += View.printFlags(mViewFlags);
15449 output += "}";
15450 Log.d(VIEW_LOG_TAG, output);
15451
15452 output = debugIndent(depth);
15453 output += "privateFlags={";
15454 output += View.printPrivateFlags(mPrivateFlags);
15455 output += "}";
15456 Log.d(VIEW_LOG_TAG, output);
15457 }
15458
15459 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015460 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015461 *
15462 * @param depth the indentation level
15463 * @return a String containing (depth * 2 + 3) * 2 white spaces
15464 *
15465 * @hide
15466 */
15467 protected static String debugIndent(int depth) {
15468 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15469 for (int i = 0; i < (depth * 2) + 3; i++) {
15470 spaces.append(' ').append(' ');
15471 }
15472 return spaces.toString();
15473 }
15474
15475 /**
15476 * <p>Return the offset of the widget's text baseline from the widget's top
15477 * boundary. If this widget does not support baseline alignment, this
15478 * method returns -1. </p>
15479 *
15480 * @return the offset of the baseline within the widget's bounds or -1
15481 * if baseline alignment is not supported
15482 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015483 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015484 public int getBaseline() {
15485 return -1;
15486 }
15487
15488 /**
Chet Haase97140572012-09-13 14:56:47 -070015489 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15490 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15491 * a layout pass.
15492 *
15493 * @return whether the view hierarchy is currently undergoing a layout pass
15494 */
15495 public boolean isInLayout() {
15496 ViewRootImpl viewRoot = getViewRootImpl();
15497 return (viewRoot != null && viewRoot.isInLayout());
15498 }
15499
15500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015501 * Call this when something has changed which has invalidated the
15502 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015503 * tree. This should not be called while the view hierarchy is currently in a layout
15504 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15505 * end of the current layout pass (and then layout will run again) or after the current
15506 * frame is drawn and the next layout occurs.
15507 *
15508 * <p>Subclasses which override this method should call the superclass method to
15509 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015510 */
15511 public void requestLayout() {
Chet Haasecc699b42012-12-13 09:06:55 -080015512 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
15513 // Only trigger request-during-layout logic if this is the view requesting it,
15514 // not the views in its parent hierarchy
15515 ViewRootImpl viewRoot = getViewRootImpl();
15516 if (viewRoot != null && viewRoot.isInLayout()) {
15517 if (!viewRoot.requestLayoutDuringLayout(this)) {
15518 return;
15519 }
15520 }
15521 mAttachInfo.mViewRequestingLayout = this;
Chet Haase97140572012-09-13 14:56:47 -070015522 }
Chet Haasecc699b42012-12-13 09:06:55 -080015523
Dianne Hackborn4702a852012-08-17 15:18:29 -070015524 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15525 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015527 if (mParent != null && !mParent.isLayoutRequested()) {
15528 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015529 }
Chet Haasecc699b42012-12-13 09:06:55 -080015530 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
15531 mAttachInfo.mViewRequestingLayout = null;
15532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015533 }
15534
15535 /**
15536 * Forces this view to be laid out during the next layout pass.
15537 * This method does not call requestLayout() or forceLayout()
15538 * on the parent.
15539 */
15540 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015541 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15542 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015543 }
15544
15545 /**
15546 * <p>
15547 * This is called to find out how big a view should be. The parent
15548 * supplies constraint information in the width and height parameters.
15549 * </p>
15550 *
15551 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015552 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015553 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015554 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015555 * </p>
15556 *
15557 *
15558 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15559 * parent
15560 * @param heightMeasureSpec Vertical space requirements as imposed by the
15561 * parent
15562 *
15563 * @see #onMeasure(int, int)
15564 */
15565 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015566 boolean optical = isLayoutModeOptical(this);
15567 if (optical != isLayoutModeOptical(mParent)) {
15568 Insets insets = getOpticalInsets();
15569 int oWidth = insets.left + insets.right;
15570 int oHeight = insets.top + insets.bottom;
15571 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15572 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15573 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015574 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015575 widthMeasureSpec != mOldWidthMeasureSpec ||
15576 heightMeasureSpec != mOldHeightMeasureSpec) {
15577
15578 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015579 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015580
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015581 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015583 // measure ourselves, this should set the measured dimension flag back
15584 onMeasure(widthMeasureSpec, heightMeasureSpec);
15585
15586 // flag not set, setMeasuredDimension() was not invoked, we raise
15587 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015588 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015589 throw new IllegalStateException("onMeasure() did not set the"
15590 + " measured dimension by calling"
15591 + " setMeasuredDimension()");
15592 }
15593
Dianne Hackborn4702a852012-08-17 15:18:29 -070015594 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015595 }
15596
15597 mOldWidthMeasureSpec = widthMeasureSpec;
15598 mOldHeightMeasureSpec = heightMeasureSpec;
15599 }
15600
15601 /**
15602 * <p>
15603 * Measure the view and its content to determine the measured width and the
15604 * measured height. This method is invoked by {@link #measure(int, int)} and
15605 * should be overriden by subclasses to provide accurate and efficient
15606 * measurement of their contents.
15607 * </p>
15608 *
15609 * <p>
15610 * <strong>CONTRACT:</strong> When overriding this method, you
15611 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15612 * measured width and height of this view. Failure to do so will trigger an
15613 * <code>IllegalStateException</code>, thrown by
15614 * {@link #measure(int, int)}. Calling the superclass'
15615 * {@link #onMeasure(int, int)} is a valid use.
15616 * </p>
15617 *
15618 * <p>
15619 * The base class implementation of measure defaults to the background size,
15620 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15621 * override {@link #onMeasure(int, int)} to provide better measurements of
15622 * their content.
15623 * </p>
15624 *
15625 * <p>
15626 * If this method is overridden, it is the subclass's responsibility to make
15627 * sure the measured height and width are at least the view's minimum height
15628 * and width ({@link #getSuggestedMinimumHeight()} and
15629 * {@link #getSuggestedMinimumWidth()}).
15630 * </p>
15631 *
15632 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15633 * The requirements are encoded with
15634 * {@link android.view.View.MeasureSpec}.
15635 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15636 * The requirements are encoded with
15637 * {@link android.view.View.MeasureSpec}.
15638 *
15639 * @see #getMeasuredWidth()
15640 * @see #getMeasuredHeight()
15641 * @see #setMeasuredDimension(int, int)
15642 * @see #getSuggestedMinimumHeight()
15643 * @see #getSuggestedMinimumWidth()
15644 * @see android.view.View.MeasureSpec#getMode(int)
15645 * @see android.view.View.MeasureSpec#getSize(int)
15646 */
15647 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15648 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15649 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15650 }
15651
15652 /**
15653 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15654 * measured width and measured height. Failing to do so will trigger an
15655 * exception at measurement time.</p>
15656 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015657 * @param measuredWidth The measured width of this view. May be a complex
15658 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15659 * {@link #MEASURED_STATE_TOO_SMALL}.
15660 * @param measuredHeight The measured height of this view. May be a complex
15661 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15662 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015663 */
15664 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015665 boolean optical = isLayoutModeOptical(this);
15666 if (optical != isLayoutModeOptical(mParent)) {
15667 Insets insets = getOpticalInsets();
15668 int opticalWidth = insets.left + insets.right;
15669 int opticalHeight = insets.top + insets.bottom;
15670
15671 measuredWidth += optical ? opticalWidth : -opticalWidth;
15672 measuredHeight += optical ? opticalHeight : -opticalHeight;
15673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015674 mMeasuredWidth = measuredWidth;
15675 mMeasuredHeight = measuredHeight;
15676
Dianne Hackborn4702a852012-08-17 15:18:29 -070015677 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015678 }
15679
15680 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015681 * Merge two states as returned by {@link #getMeasuredState()}.
15682 * @param curState The current state as returned from a view or the result
15683 * of combining multiple views.
15684 * @param newState The new view state to combine.
15685 * @return Returns a new integer reflecting the combination of the two
15686 * states.
15687 */
15688 public static int combineMeasuredStates(int curState, int newState) {
15689 return curState | newState;
15690 }
15691
15692 /**
15693 * Version of {@link #resolveSizeAndState(int, int, int)}
15694 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15695 */
15696 public static int resolveSize(int size, int measureSpec) {
15697 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15698 }
15699
15700 /**
15701 * Utility to reconcile a desired size and state, with constraints imposed
15702 * by a MeasureSpec. Will take the desired size, unless a different size
15703 * is imposed by the constraints. The returned value is a compound integer,
15704 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15705 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15706 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015707 *
15708 * @param size How big the view wants to be
15709 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015710 * @return Size information bit mask as defined by
15711 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015712 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015713 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015714 int result = size;
15715 int specMode = MeasureSpec.getMode(measureSpec);
15716 int specSize = MeasureSpec.getSize(measureSpec);
15717 switch (specMode) {
15718 case MeasureSpec.UNSPECIFIED:
15719 result = size;
15720 break;
15721 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015722 if (specSize < size) {
15723 result = specSize | MEASURED_STATE_TOO_SMALL;
15724 } else {
15725 result = size;
15726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015727 break;
15728 case MeasureSpec.EXACTLY:
15729 result = specSize;
15730 break;
15731 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015732 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015733 }
15734
15735 /**
15736 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015737 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015738 * by the MeasureSpec.
15739 *
15740 * @param size Default size for this view
15741 * @param measureSpec Constraints imposed by the parent
15742 * @return The size this view should be.
15743 */
15744 public static int getDefaultSize(int size, int measureSpec) {
15745 int result = size;
15746 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015747 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015748
15749 switch (specMode) {
15750 case MeasureSpec.UNSPECIFIED:
15751 result = size;
15752 break;
15753 case MeasureSpec.AT_MOST:
15754 case MeasureSpec.EXACTLY:
15755 result = specSize;
15756 break;
15757 }
15758 return result;
15759 }
15760
15761 /**
15762 * Returns the suggested minimum height that the view should use. This
15763 * returns the maximum of the view's minimum height
15764 * and the background's minimum height
15765 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15766 * <p>
15767 * When being used in {@link #onMeasure(int, int)}, the caller should still
15768 * ensure the returned height is within the requirements of the parent.
15769 *
15770 * @return The suggested minimum height of the view.
15771 */
15772 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015773 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015775 }
15776
15777 /**
15778 * Returns the suggested minimum width that the view should use. This
15779 * returns the maximum of the view's minimum width)
15780 * and the background's minimum width
15781 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15782 * <p>
15783 * When being used in {@link #onMeasure(int, int)}, the caller should still
15784 * ensure the returned width is within the requirements of the parent.
15785 *
15786 * @return The suggested minimum width of the view.
15787 */
15788 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015789 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015791
Philip Milne6c8ea062012-04-03 17:38:43 -070015792 /**
15793 * Returns the minimum height of the view.
15794 *
15795 * @return the minimum height the view will try to be.
15796 *
15797 * @see #setMinimumHeight(int)
15798 *
15799 * @attr ref android.R.styleable#View_minHeight
15800 */
15801 public int getMinimumHeight() {
15802 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015803 }
15804
15805 /**
15806 * Sets the minimum height of the view. It is not guaranteed the view will
15807 * be able to achieve this minimum height (for example, if its parent layout
15808 * constrains it with less available height).
15809 *
15810 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015811 *
15812 * @see #getMinimumHeight()
15813 *
15814 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015815 */
15816 public void setMinimumHeight(int minHeight) {
15817 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015818 requestLayout();
15819 }
15820
15821 /**
15822 * Returns the minimum width of the view.
15823 *
15824 * @return the minimum width the view will try to be.
15825 *
15826 * @see #setMinimumWidth(int)
15827 *
15828 * @attr ref android.R.styleable#View_minWidth
15829 */
15830 public int getMinimumWidth() {
15831 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015832 }
15833
15834 /**
15835 * Sets the minimum width of the view. It is not guaranteed the view will
15836 * be able to achieve this minimum width (for example, if its parent layout
15837 * constrains it with less available width).
15838 *
15839 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015840 *
15841 * @see #getMinimumWidth()
15842 *
15843 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015844 */
15845 public void setMinimumWidth(int minWidth) {
15846 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015847 requestLayout();
15848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015849 }
15850
15851 /**
15852 * Get the animation currently associated with this view.
15853 *
15854 * @return The animation that is currently playing or
15855 * scheduled to play for this view.
15856 */
15857 public Animation getAnimation() {
15858 return mCurrentAnimation;
15859 }
15860
15861 /**
15862 * Start the specified animation now.
15863 *
15864 * @param animation the animation to start now
15865 */
15866 public void startAnimation(Animation animation) {
15867 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15868 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015869 invalidateParentCaches();
15870 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015871 }
15872
15873 /**
15874 * Cancels any animations for this view.
15875 */
15876 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015877 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015878 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015880 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015881 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015882 }
15883
15884 /**
15885 * Sets the next animation to play for this view.
15886 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015887 * {@link #startAnimation(android.view.animation.Animation)} instead.
15888 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015889 * control over the start time and invalidation, but you
15890 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015891 * 2) the view's parent (which controls animations on its children)
15892 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015893 * start.
15894 *
15895 * @param animation The next animation, or null.
15896 */
15897 public void setAnimation(Animation animation) {
15898 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015900 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015901 // If the screen is off assume the animation start time is now instead of
15902 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15903 // would cause the animation to start when the screen turns back on
15904 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15905 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15906 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015908 animation.reset();
15909 }
15910 }
15911
15912 /**
15913 * Invoked by a parent ViewGroup to notify the start of the animation
15914 * currently associated with this view. If you override this method,
15915 * always call super.onAnimationStart();
15916 *
15917 * @see #setAnimation(android.view.animation.Animation)
15918 * @see #getAnimation()
15919 */
15920 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015921 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015922 }
15923
15924 /**
15925 * Invoked by a parent ViewGroup to notify the end of the animation
15926 * currently associated with this view. If you override this method,
15927 * always call super.onAnimationEnd();
15928 *
15929 * @see #setAnimation(android.view.animation.Animation)
15930 * @see #getAnimation()
15931 */
15932 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015933 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015934 }
15935
15936 /**
15937 * Invoked if there is a Transform that involves alpha. Subclass that can
15938 * draw themselves with the specified alpha should return true, and then
15939 * respect that alpha when their onDraw() is called. If this returns false
15940 * then the view may be redirected to draw into an offscreen buffer to
15941 * fulfill the request, which will look fine, but may be slower than if the
15942 * subclass handles it internally. The default implementation returns false.
15943 *
15944 * @param alpha The alpha (0..255) to apply to the view's drawing
15945 * @return true if the view can draw with the specified alpha.
15946 */
15947 protected boolean onSetAlpha(int alpha) {
15948 return false;
15949 }
15950
15951 /**
15952 * This is used by the RootView to perform an optimization when
15953 * the view hierarchy contains one or several SurfaceView.
15954 * SurfaceView is always considered transparent, but its children are not,
15955 * therefore all View objects remove themselves from the global transparent
15956 * region (passed as a parameter to this function).
15957 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015958 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015959 *
15960 * @return Returns true if the effective visibility of the view at this
15961 * point is opaque, regardless of the transparent region; returns false
15962 * if it is possible for underlying windows to be seen behind the view.
15963 *
15964 * {@hide}
15965 */
15966 public boolean gatherTransparentRegion(Region region) {
15967 final AttachInfo attachInfo = mAttachInfo;
15968 if (region != null && attachInfo != null) {
15969 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015970 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015971 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15972 // remove it from the transparent region.
15973 final int[] location = attachInfo.mTransparentLocation;
15974 getLocationInWindow(location);
15975 region.op(location[0], location[1], location[0] + mRight - mLeft,
15976 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015977 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015978 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15979 // exists, so we remove the background drawable's non-transparent
15980 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015981 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015982 }
15983 }
15984 return true;
15985 }
15986
15987 /**
15988 * Play a sound effect for this view.
15989 *
15990 * <p>The framework will play sound effects for some built in actions, such as
15991 * clicking, but you may wish to play these effects in your widget,
15992 * for instance, for internal navigation.
15993 *
15994 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15995 * {@link #isSoundEffectsEnabled()} is true.
15996 *
15997 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15998 */
15999 public void playSoundEffect(int soundConstant) {
16000 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
16001 return;
16002 }
16003 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
16004 }
16005
16006 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016007 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016008 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016009 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016010 *
16011 * <p>The framework will provide haptic feedback for some built in actions,
16012 * such as long presses, but you may wish to provide feedback for your
16013 * own widget.
16014 *
16015 * <p>The feedback will only be performed if
16016 * {@link #isHapticFeedbackEnabled()} is true.
16017 *
16018 * @param feedbackConstant One of the constants defined in
16019 * {@link HapticFeedbackConstants}
16020 */
16021 public boolean performHapticFeedback(int feedbackConstant) {
16022 return performHapticFeedback(feedbackConstant, 0);
16023 }
16024
16025 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016026 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016027 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016028 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016029 *
16030 * @param feedbackConstant One of the constants defined in
16031 * {@link HapticFeedbackConstants}
16032 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16033 */
16034 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16035 if (mAttachInfo == null) {
16036 return false;
16037 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016038 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016039 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016040 && !isHapticFeedbackEnabled()) {
16041 return false;
16042 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016043 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16044 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016045 }
16046
16047 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016048 * Request that the visibility of the status bar or other screen/window
16049 * decorations be changed.
16050 *
16051 * <p>This method is used to put the over device UI into temporary modes
16052 * where the user's attention is focused more on the application content,
16053 * by dimming or hiding surrounding system affordances. This is typically
16054 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16055 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16056 * to be placed behind the action bar (and with these flags other system
16057 * affordances) so that smooth transitions between hiding and showing them
16058 * can be done.
16059 *
16060 * <p>Two representative examples of the use of system UI visibility is
16061 * implementing a content browsing application (like a magazine reader)
16062 * and a video playing application.
16063 *
16064 * <p>The first code shows a typical implementation of a View in a content
16065 * browsing application. In this implementation, the application goes
16066 * into a content-oriented mode by hiding the status bar and action bar,
16067 * and putting the navigation elements into lights out mode. The user can
16068 * then interact with content while in this mode. Such an application should
16069 * provide an easy way for the user to toggle out of the mode (such as to
16070 * check information in the status bar or access notifications). In the
16071 * implementation here, this is done simply by tapping on the content.
16072 *
16073 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16074 * content}
16075 *
16076 * <p>This second code sample shows a typical implementation of a View
16077 * in a video playing application. In this situation, while the video is
16078 * playing the application would like to go into a complete full-screen mode,
16079 * to use as much of the display as possible for the video. When in this state
16080 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016081 * touching on the screen to pop the UI out of full screen mode. See
16082 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016083 *
16084 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16085 * content}
16086 *
16087 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16088 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16089 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16090 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016091 */
16092 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016093 if (visibility != mSystemUiVisibility) {
16094 mSystemUiVisibility = visibility;
16095 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16096 mParent.recomputeViewAttributes(this);
16097 }
Joe Onorato664644d2011-01-23 17:53:23 -080016098 }
16099 }
16100
16101 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016102 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16103 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16104 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16105 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16106 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016107 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016108 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016109 return mSystemUiVisibility;
16110 }
16111
Scott Mainec6331b2011-05-24 16:55:56 -070016112 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016113 * Returns the current system UI visibility that is currently set for
16114 * the entire window. This is the combination of the
16115 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16116 * views in the window.
16117 */
16118 public int getWindowSystemUiVisibility() {
16119 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16120 }
16121
16122 /**
16123 * Override to find out when the window's requested system UI visibility
16124 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16125 * This is different from the callbacks recieved through
16126 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16127 * in that this is only telling you about the local request of the window,
16128 * not the actual values applied by the system.
16129 */
16130 public void onWindowSystemUiVisibilityChanged(int visible) {
16131 }
16132
16133 /**
16134 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16135 * the view hierarchy.
16136 */
16137 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16138 onWindowSystemUiVisibilityChanged(visible);
16139 }
16140
16141 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016142 * Set a listener to receive callbacks when the visibility of the system bar changes.
16143 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16144 */
Joe Onorato664644d2011-01-23 17:53:23 -080016145 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016146 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016147 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16148 mParent.recomputeViewAttributes(this);
16149 }
16150 }
16151
16152 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016153 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16154 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016155 */
16156 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016157 ListenerInfo li = mListenerInfo;
16158 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16159 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016160 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016161 }
16162 }
16163
Dianne Hackborncf675782012-05-10 15:07:24 -070016164 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016165 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16166 if (val != mSystemUiVisibility) {
16167 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016168 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016169 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016170 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016171 }
16172
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016173 /** @hide */
16174 public void setDisabledSystemUiVisibility(int flags) {
16175 if (mAttachInfo != null) {
16176 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16177 mAttachInfo.mDisabledSystemUiVisibility = flags;
16178 if (mParent != null) {
16179 mParent.recomputeViewAttributes(this);
16180 }
16181 }
16182 }
16183 }
16184
Joe Onorato664644d2011-01-23 17:53:23 -080016185 /**
Joe Malin32736f02011-01-19 16:14:20 -080016186 * Creates an image that the system displays during the drag and drop
16187 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16188 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16189 * appearance as the given View. The default also positions the center of the drag shadow
16190 * directly under the touch point. If no View is provided (the constructor with no parameters
16191 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16192 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16193 * default is an invisible drag shadow.
16194 * <p>
16195 * You are not required to use the View you provide to the constructor as the basis of the
16196 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16197 * anything you want as the drag shadow.
16198 * </p>
16199 * <p>
16200 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16201 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16202 * size and position of the drag shadow. It uses this data to construct a
16203 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16204 * so that your application can draw the shadow image in the Canvas.
16205 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016206 *
16207 * <div class="special reference">
16208 * <h3>Developer Guides</h3>
16209 * <p>For a guide to implementing drag and drop features, read the
16210 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16211 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016212 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016213 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016214 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016215
16216 /**
Joe Malin32736f02011-01-19 16:14:20 -080016217 * Constructs a shadow image builder based on a View. By default, the resulting drag
16218 * shadow will have the same appearance and dimensions as the View, with the touch point
16219 * over the center of the View.
16220 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016221 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016222 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016223 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016224 }
16225
Christopher Tate17ed60c2011-01-18 12:50:26 -080016226 /**
16227 * Construct a shadow builder object with no associated View. This
16228 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16229 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16230 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016231 * reference to any View object. If they are not overridden, then the result is an
16232 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016233 */
16234 public DragShadowBuilder() {
16235 mView = new WeakReference<View>(null);
16236 }
16237
16238 /**
16239 * Returns the View object that had been passed to the
16240 * {@link #View.DragShadowBuilder(View)}
16241 * constructor. If that View parameter was {@code null} or if the
16242 * {@link #View.DragShadowBuilder()}
16243 * constructor was used to instantiate the builder object, this method will return
16244 * null.
16245 *
16246 * @return The View object associate with this builder object.
16247 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016248 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016249 final public View getView() {
16250 return mView.get();
16251 }
16252
Christopher Tate2c095f32010-10-04 14:13:40 -070016253 /**
Joe Malin32736f02011-01-19 16:14:20 -080016254 * Provides the metrics for the shadow image. These include the dimensions of
16255 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016256 * be centered under the touch location while dragging.
16257 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016258 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016259 * same as the dimensions of the View itself and centers the shadow under
16260 * the touch point.
16261 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016262 *
Joe Malin32736f02011-01-19 16:14:20 -080016263 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16264 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16265 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16266 * image.
16267 *
16268 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16269 * shadow image that should be underneath the touch point during the drag and drop
16270 * operation. Your application must set {@link android.graphics.Point#x} to the
16271 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016272 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016273 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016274 final View view = mView.get();
16275 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016276 shadowSize.set(view.getWidth(), view.getHeight());
16277 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016278 } else {
16279 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16280 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016281 }
16282
16283 /**
Joe Malin32736f02011-01-19 16:14:20 -080016284 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16285 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016286 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016287 *
Joe Malin32736f02011-01-19 16:14:20 -080016288 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016289 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016290 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016291 final View view = mView.get();
16292 if (view != null) {
16293 view.draw(canvas);
16294 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016295 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016296 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016297 }
16298 }
16299
16300 /**
Joe Malin32736f02011-01-19 16:14:20 -080016301 * Starts a drag and drop operation. When your application calls this method, it passes a
16302 * {@link android.view.View.DragShadowBuilder} object to the system. The
16303 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16304 * to get metrics for the drag shadow, and then calls the object's
16305 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16306 * <p>
16307 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16308 * drag events to all the View objects in your application that are currently visible. It does
16309 * this either by calling the View object's drag listener (an implementation of
16310 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16311 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16312 * Both are passed a {@link android.view.DragEvent} object that has a
16313 * {@link android.view.DragEvent#getAction()} value of
16314 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16315 * </p>
16316 * <p>
16317 * Your application can invoke startDrag() on any attached View object. The View object does not
16318 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16319 * be related to the View the user selected for dragging.
16320 * </p>
16321 * @param data A {@link android.content.ClipData} object pointing to the data to be
16322 * transferred by the drag and drop operation.
16323 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16324 * drag shadow.
16325 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16326 * drop operation. This Object is put into every DragEvent object sent by the system during the
16327 * current drag.
16328 * <p>
16329 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16330 * to the target Views. For example, it can contain flags that differentiate between a
16331 * a copy operation and a move operation.
16332 * </p>
16333 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16334 * so the parameter should be set to 0.
16335 * @return {@code true} if the method completes successfully, or
16336 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16337 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016338 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016339 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016340 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016341 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016342 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016343 }
16344 boolean okay = false;
16345
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016346 Point shadowSize = new Point();
16347 Point shadowTouchPoint = new Point();
16348 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016349
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016350 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16351 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16352 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016353 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016354
Chris Tatea32dcf72010-10-14 12:13:50 -070016355 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016356 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16357 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016358 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016359 Surface surface = new Surface();
16360 try {
16361 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016362 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016363 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016364 + " surface=" + surface);
16365 if (token != null) {
16366 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016367 try {
Chris Tate6b391282010-10-14 15:48:59 -070016368 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016369 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016370 } finally {
16371 surface.unlockCanvasAndPost(canvas);
16372 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016373
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016374 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016375
16376 // Cache the local state object for delivery with DragEvents
16377 root.setLocalDragState(myLocalState);
16378
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016379 // repurpose 'shadowSize' for the last touch point
16380 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016381
Christopher Tatea53146c2010-09-07 11:57:52 -070016382 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016383 shadowSize.x, shadowSize.y,
16384 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016385 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016386
16387 // Off and running! Release our local surface instance; the drag
16388 // shadow surface is now managed by the system process.
16389 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016390 }
16391 } catch (Exception e) {
16392 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16393 surface.destroy();
16394 }
16395
16396 return okay;
16397 }
16398
Christopher Tatea53146c2010-09-07 11:57:52 -070016399 /**
Joe Malin32736f02011-01-19 16:14:20 -080016400 * Handles drag events sent by the system following a call to
16401 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16402 *<p>
16403 * When the system calls this method, it passes a
16404 * {@link android.view.DragEvent} object. A call to
16405 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16406 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16407 * operation.
16408 * @param event The {@link android.view.DragEvent} sent by the system.
16409 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16410 * in DragEvent, indicating the type of drag event represented by this object.
16411 * @return {@code true} if the method was successful, otherwise {@code false}.
16412 * <p>
16413 * The method should return {@code true} in response to an action type of
16414 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16415 * operation.
16416 * </p>
16417 * <p>
16418 * The method should also return {@code true} in response to an action type of
16419 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16420 * {@code false} if it didn't.
16421 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016422 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016423 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016424 return false;
16425 }
16426
16427 /**
Joe Malin32736f02011-01-19 16:14:20 -080016428 * Detects if this View is enabled and has a drag event listener.
16429 * If both are true, then it calls the drag event listener with the
16430 * {@link android.view.DragEvent} it received. If the drag event listener returns
16431 * {@code true}, then dispatchDragEvent() returns {@code true}.
16432 * <p>
16433 * For all other cases, the method calls the
16434 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16435 * method and returns its result.
16436 * </p>
16437 * <p>
16438 * This ensures that a drag event is always consumed, even if the View does not have a drag
16439 * event listener. However, if the View has a listener and the listener returns true, then
16440 * onDragEvent() is not called.
16441 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016442 */
16443 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016444 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016445 ListenerInfo li = mListenerInfo;
16446 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16447 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016448 return true;
16449 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016450 return onDragEvent(event);
16451 }
16452
Christopher Tate3d4bf172011-03-28 16:16:46 -070016453 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016454 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016455 }
16456
Christopher Tatea53146c2010-09-07 11:57:52 -070016457 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016458 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16459 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016460 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016461 */
16462 public void onCloseSystemDialogs(String reason) {
16463 }
Joe Malin32736f02011-01-19 16:14:20 -080016464
Dianne Hackbornffa42482009-09-23 22:20:11 -070016465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016466 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016467 * update a Region being computed for
16468 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016469 * that any non-transparent parts of the Drawable are removed from the
16470 * given transparent region.
16471 *
16472 * @param dr The Drawable whose transparency is to be applied to the region.
16473 * @param region A Region holding the current transparency information,
16474 * where any parts of the region that are set are considered to be
16475 * transparent. On return, this region will be modified to have the
16476 * transparency information reduced by the corresponding parts of the
16477 * Drawable that are not transparent.
16478 * {@hide}
16479 */
16480 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16481 if (DBG) {
16482 Log.i("View", "Getting transparent region for: " + this);
16483 }
16484 final Region r = dr.getTransparentRegion();
16485 final Rect db = dr.getBounds();
16486 final AttachInfo attachInfo = mAttachInfo;
16487 if (r != null && attachInfo != null) {
16488 final int w = getRight()-getLeft();
16489 final int h = getBottom()-getTop();
16490 if (db.left > 0) {
16491 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16492 r.op(0, 0, db.left, h, Region.Op.UNION);
16493 }
16494 if (db.right < w) {
16495 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16496 r.op(db.right, 0, w, h, Region.Op.UNION);
16497 }
16498 if (db.top > 0) {
16499 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16500 r.op(0, 0, w, db.top, Region.Op.UNION);
16501 }
16502 if (db.bottom < h) {
16503 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16504 r.op(0, db.bottom, w, h, Region.Op.UNION);
16505 }
16506 final int[] location = attachInfo.mTransparentLocation;
16507 getLocationInWindow(location);
16508 r.translate(location[0], location[1]);
16509 region.op(r, Region.Op.INTERSECT);
16510 } else {
16511 region.op(db, Region.Op.DIFFERENCE);
16512 }
16513 }
16514
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016515 private void checkForLongClick(int delayOffset) {
16516 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16517 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016518
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016519 if (mPendingCheckForLongPress == null) {
16520 mPendingCheckForLongPress = new CheckForLongPress();
16521 }
16522 mPendingCheckForLongPress.rememberWindowAttachCount();
16523 postDelayed(mPendingCheckForLongPress,
16524 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016526 }
16527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016528 /**
16529 * Inflate a view from an XML resource. This convenience method wraps the {@link
16530 * LayoutInflater} class, which provides a full range of options for view inflation.
16531 *
16532 * @param context The Context object for your activity or application.
16533 * @param resource The resource ID to inflate
16534 * @param root A view group that will be the parent. Used to properly inflate the
16535 * layout_* parameters.
16536 * @see LayoutInflater
16537 */
16538 public static View inflate(Context context, int resource, ViewGroup root) {
16539 LayoutInflater factory = LayoutInflater.from(context);
16540 return factory.inflate(resource, root);
16541 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016543 /**
Adam Powell637d3372010-08-25 14:37:03 -070016544 * Scroll the view with standard behavior for scrolling beyond the normal
16545 * content boundaries. Views that call this method should override
16546 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16547 * results of an over-scroll operation.
16548 *
16549 * Views can use this method to handle any touch or fling-based scrolling.
16550 *
16551 * @param deltaX Change in X in pixels
16552 * @param deltaY Change in Y in pixels
16553 * @param scrollX Current X scroll value in pixels before applying deltaX
16554 * @param scrollY Current Y scroll value in pixels before applying deltaY
16555 * @param scrollRangeX Maximum content scroll range along the X axis
16556 * @param scrollRangeY Maximum content scroll range along the Y axis
16557 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16558 * along the X axis.
16559 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16560 * along the Y axis.
16561 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16562 * @return true if scrolling was clamped to an over-scroll boundary along either
16563 * axis, false otherwise.
16564 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016565 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016566 protected boolean overScrollBy(int deltaX, int deltaY,
16567 int scrollX, int scrollY,
16568 int scrollRangeX, int scrollRangeY,
16569 int maxOverScrollX, int maxOverScrollY,
16570 boolean isTouchEvent) {
16571 final int overScrollMode = mOverScrollMode;
16572 final boolean canScrollHorizontal =
16573 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16574 final boolean canScrollVertical =
16575 computeVerticalScrollRange() > computeVerticalScrollExtent();
16576 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16577 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16578 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16579 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16580
16581 int newScrollX = scrollX + deltaX;
16582 if (!overScrollHorizontal) {
16583 maxOverScrollX = 0;
16584 }
16585
16586 int newScrollY = scrollY + deltaY;
16587 if (!overScrollVertical) {
16588 maxOverScrollY = 0;
16589 }
16590
16591 // Clamp values if at the limits and record
16592 final int left = -maxOverScrollX;
16593 final int right = maxOverScrollX + scrollRangeX;
16594 final int top = -maxOverScrollY;
16595 final int bottom = maxOverScrollY + scrollRangeY;
16596
16597 boolean clampedX = false;
16598 if (newScrollX > right) {
16599 newScrollX = right;
16600 clampedX = true;
16601 } else if (newScrollX < left) {
16602 newScrollX = left;
16603 clampedX = true;
16604 }
16605
16606 boolean clampedY = false;
16607 if (newScrollY > bottom) {
16608 newScrollY = bottom;
16609 clampedY = true;
16610 } else if (newScrollY < top) {
16611 newScrollY = top;
16612 clampedY = true;
16613 }
16614
16615 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16616
16617 return clampedX || clampedY;
16618 }
16619
16620 /**
16621 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16622 * respond to the results of an over-scroll operation.
16623 *
16624 * @param scrollX New X scroll value in pixels
16625 * @param scrollY New Y scroll value in pixels
16626 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16627 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16628 */
16629 protected void onOverScrolled(int scrollX, int scrollY,
16630 boolean clampedX, boolean clampedY) {
16631 // Intentionally empty.
16632 }
16633
16634 /**
16635 * Returns the over-scroll mode for this view. The result will be
16636 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16637 * (allow over-scrolling only if the view content is larger than the container),
16638 * or {@link #OVER_SCROLL_NEVER}.
16639 *
16640 * @return This view's over-scroll mode.
16641 */
16642 public int getOverScrollMode() {
16643 return mOverScrollMode;
16644 }
16645
16646 /**
16647 * Set the over-scroll mode for this view. Valid over-scroll modes are
16648 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16649 * (allow over-scrolling only if the view content is larger than the container),
16650 * or {@link #OVER_SCROLL_NEVER}.
16651 *
16652 * Setting the over-scroll mode of a view will have an effect only if the
16653 * view is capable of scrolling.
16654 *
16655 * @param overScrollMode The new over-scroll mode for this view.
16656 */
16657 public void setOverScrollMode(int overScrollMode) {
16658 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16659 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16660 overScrollMode != OVER_SCROLL_NEVER) {
16661 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16662 }
16663 mOverScrollMode = overScrollMode;
16664 }
16665
16666 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016667 * Gets a scale factor that determines the distance the view should scroll
16668 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16669 * @return The vertical scroll scale factor.
16670 * @hide
16671 */
16672 protected float getVerticalScrollFactor() {
16673 if (mVerticalScrollFactor == 0) {
16674 TypedValue outValue = new TypedValue();
16675 if (!mContext.getTheme().resolveAttribute(
16676 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16677 throw new IllegalStateException(
16678 "Expected theme to define listPreferredItemHeight.");
16679 }
16680 mVerticalScrollFactor = outValue.getDimension(
16681 mContext.getResources().getDisplayMetrics());
16682 }
16683 return mVerticalScrollFactor;
16684 }
16685
16686 /**
16687 * Gets a scale factor that determines the distance the view should scroll
16688 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16689 * @return The horizontal scroll scale factor.
16690 * @hide
16691 */
16692 protected float getHorizontalScrollFactor() {
16693 // TODO: Should use something else.
16694 return getVerticalScrollFactor();
16695 }
16696
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016697 /**
16698 * Return the value specifying the text direction or policy that was set with
16699 * {@link #setTextDirection(int)}.
16700 *
16701 * @return the defined text direction. It can be one of:
16702 *
16703 * {@link #TEXT_DIRECTION_INHERIT},
16704 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16705 * {@link #TEXT_DIRECTION_ANY_RTL},
16706 * {@link #TEXT_DIRECTION_LTR},
16707 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016708 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016709 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016710 * @attr ref android.R.styleable#View_textDirection
16711 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016712 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016713 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016714 @ViewDebug.ExportedProperty(category = "text", mapping = {
16715 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16716 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16717 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16718 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16719 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16720 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16721 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016722 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016723 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016724 }
16725
16726 /**
16727 * Set the text direction.
16728 *
16729 * @param textDirection the direction to set. Should be one of:
16730 *
16731 * {@link #TEXT_DIRECTION_INHERIT},
16732 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16733 * {@link #TEXT_DIRECTION_ANY_RTL},
16734 * {@link #TEXT_DIRECTION_LTR},
16735 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016736 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016737 *
16738 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16739 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16740 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016741 *
16742 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016743 */
16744 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016745 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016746 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016747 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016748 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016749 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016750 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016751 // Do resolution
16752 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016753 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016754 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016755 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016756 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016757 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016758 }
16759 }
16760
16761 /**
16762 * Return the resolved text direction.
16763 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016764 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016765 *
Doug Feltcb3791202011-07-07 11:57:48 -070016766 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16767 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016768 * {@link #TEXT_DIRECTION_LTR},
16769 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016770 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016771 *
16772 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016773 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016774 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016775 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016776 }
16777
16778 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016779 * Resolve the text direction.
16780 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016781 * @return true if resolution has been done, false otherwise.
16782 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016783 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016784 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016785 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016786 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016787 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016788
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016789 if (hasRtlSupport()) {
16790 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016791 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016792 switch(textDirection) {
16793 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016794 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016795 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016796 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016797 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016798 return false;
16799 }
16800
16801 View parent = ((View) mParent);
16802 // Parent has not yet resolved, so we still return the default
16803 if (!parent.isTextDirectionResolved()) {
16804 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16805 // Resolution will need to happen again later
16806 return false;
16807 }
16808
16809 // Set current resolved direction to the same value as the parent's one
16810 final int parentResolvedDirection = parent.getTextDirection();
16811 switch (parentResolvedDirection) {
16812 case TEXT_DIRECTION_FIRST_STRONG:
16813 case TEXT_DIRECTION_ANY_RTL:
16814 case TEXT_DIRECTION_LTR:
16815 case TEXT_DIRECTION_RTL:
16816 case TEXT_DIRECTION_LOCALE:
16817 mPrivateFlags2 |=
16818 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16819 break;
16820 default:
16821 // Default resolved direction is "first strong" heuristic
16822 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016823 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016824 break;
16825 case TEXT_DIRECTION_FIRST_STRONG:
16826 case TEXT_DIRECTION_ANY_RTL:
16827 case TEXT_DIRECTION_LTR:
16828 case TEXT_DIRECTION_RTL:
16829 case TEXT_DIRECTION_LOCALE:
16830 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016831 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016832 break;
16833 default:
16834 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016835 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016836 }
16837 } else {
16838 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016839 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016840 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016841
16842 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016843 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016844 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016845 }
16846
16847 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016848 * Check if text direction resolution can be done.
16849 *
16850 * @return true if text direction resolution can be done otherwise return false.
16851 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016852 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016853 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016854 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016855 return (mParent != null) && (mParent instanceof View) &&
16856 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016857 default:
16858 return true;
16859 }
16860 }
16861
16862 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016863 * Reset resolved text direction. Text direction will be resolved during a call to
16864 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016865 *
16866 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016867 */
16868 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016869 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016870 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016871 // Set to default value
16872 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016873 }
16874
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016875 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016876 * @return true if text direction is inherited.
16877 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016878 * @hide
16879 */
16880 public boolean isTextDirectionInherited() {
16881 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16882 }
16883
16884 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016885 * @return true if text direction is resolved.
16886 */
16887 private boolean isTextDirectionResolved() {
16888 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16889 }
16890
16891 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016892 * Return the value specifying the text alignment or policy that was set with
16893 * {@link #setTextAlignment(int)}.
16894 *
16895 * @return the defined text alignment. It can be one of:
16896 *
16897 * {@link #TEXT_ALIGNMENT_INHERIT},
16898 * {@link #TEXT_ALIGNMENT_GRAVITY},
16899 * {@link #TEXT_ALIGNMENT_CENTER},
16900 * {@link #TEXT_ALIGNMENT_TEXT_START},
16901 * {@link #TEXT_ALIGNMENT_TEXT_END},
16902 * {@link #TEXT_ALIGNMENT_VIEW_START},
16903 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016904 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016905 * @attr ref android.R.styleable#View_textAlignment
16906 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016907 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016908 */
16909 @ViewDebug.ExportedProperty(category = "text", mapping = {
16910 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16911 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16912 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16913 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16914 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16915 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16916 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16917 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016918 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016919 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016920 }
16921
16922 /**
16923 * Set the text alignment.
16924 *
16925 * @param textAlignment The text alignment to set. Should be one of
16926 *
16927 * {@link #TEXT_ALIGNMENT_INHERIT},
16928 * {@link #TEXT_ALIGNMENT_GRAVITY},
16929 * {@link #TEXT_ALIGNMENT_CENTER},
16930 * {@link #TEXT_ALIGNMENT_TEXT_START},
16931 * {@link #TEXT_ALIGNMENT_TEXT_END},
16932 * {@link #TEXT_ALIGNMENT_VIEW_START},
16933 * {@link #TEXT_ALIGNMENT_VIEW_END}
16934 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016935 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16936 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16937 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16938 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016939 * @attr ref android.R.styleable#View_textAlignment
16940 */
16941 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016942 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016943 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016944 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016945 resetResolvedTextAlignment();
16946 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016947 mPrivateFlags2 |=
16948 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16949 // Do resolution
16950 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016951 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016952 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016953 // Refresh
16954 requestLayout();
16955 invalidate(true);
16956 }
16957 }
16958
16959 /**
16960 * Return the resolved text alignment.
16961 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016962 * @return the resolved text alignment. Returns one of:
16963 *
16964 * {@link #TEXT_ALIGNMENT_GRAVITY},
16965 * {@link #TEXT_ALIGNMENT_CENTER},
16966 * {@link #TEXT_ALIGNMENT_TEXT_START},
16967 * {@link #TEXT_ALIGNMENT_TEXT_END},
16968 * {@link #TEXT_ALIGNMENT_VIEW_START},
16969 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016970 *
16971 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016972 */
16973 @ViewDebug.ExportedProperty(category = "text", mapping = {
16974 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16975 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16976 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16977 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16978 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16979 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16980 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16981 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016982 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016983 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16984 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016985 }
16986
16987 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016988 * Resolve the text alignment.
16989 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016990 * @return true if resolution has been done, false otherwise.
16991 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016992 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016993 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016994 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016995 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016996 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016997
16998 if (hasRtlSupport()) {
16999 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017000 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017001 switch (textAlignment) {
17002 case TEXT_ALIGNMENT_INHERIT:
17003 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017004 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017005 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070017006 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017007 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017008 return false;
17009 }
17010 View parent = (View) mParent;
17011
17012 // Parent has not yet resolved, so we still return the default
17013 if (!parent.isTextAlignmentResolved()) {
17014 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
17015 // Resolution will need to happen again later
17016 return false;
17017 }
17018
17019 final int parentResolvedTextAlignment = parent.getTextAlignment();
17020 switch (parentResolvedTextAlignment) {
17021 case TEXT_ALIGNMENT_GRAVITY:
17022 case TEXT_ALIGNMENT_TEXT_START:
17023 case TEXT_ALIGNMENT_TEXT_END:
17024 case TEXT_ALIGNMENT_CENTER:
17025 case TEXT_ALIGNMENT_VIEW_START:
17026 case TEXT_ALIGNMENT_VIEW_END:
17027 // Resolved text alignment is the same as the parent resolved
17028 // text alignment
17029 mPrivateFlags2 |=
17030 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17031 break;
17032 default:
17033 // Use default resolved text alignment
17034 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017035 }
17036 break;
17037 case TEXT_ALIGNMENT_GRAVITY:
17038 case TEXT_ALIGNMENT_TEXT_START:
17039 case TEXT_ALIGNMENT_TEXT_END:
17040 case TEXT_ALIGNMENT_CENTER:
17041 case TEXT_ALIGNMENT_VIEW_START:
17042 case TEXT_ALIGNMENT_VIEW_END:
17043 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017044 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017045 break;
17046 default:
17047 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017048 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017049 }
17050 } else {
17051 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017052 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017053 }
17054
17055 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017056 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017057 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017058 }
17059
17060 /**
17061 * Check if text alignment resolution can be done.
17062 *
17063 * @return true if text alignment resolution can be done otherwise return false.
17064 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017065 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017066 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017067 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017068 return (mParent != null) && (mParent instanceof View) &&
17069 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017070 default:
17071 return true;
17072 }
17073 }
17074
17075 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017076 * Reset resolved text alignment. Text alignment will be resolved during a call to
17077 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017078 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017079 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017080 */
17081 public void resetResolvedTextAlignment() {
17082 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017083 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017084 // Set to default
17085 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017086 }
17087
Adam Powella9108a22012-07-18 11:18:09 -070017088 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017089 * @return true if text alignment is inherited.
17090 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017091 * @hide
17092 */
17093 public boolean isTextAlignmentInherited() {
17094 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17095 }
17096
17097 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017098 * @return true if text alignment is resolved.
17099 */
17100 private boolean isTextAlignmentResolved() {
17101 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17102 }
17103
17104 /**
Adam Powella9108a22012-07-18 11:18:09 -070017105 * Generate a value suitable for use in {@link #setId(int)}.
17106 * This value will not collide with ID values generated at build time by aapt for R.id.
17107 *
17108 * @return a generated ID value
17109 */
17110 public static int generateViewId() {
17111 for (;;) {
17112 final int result = sNextGeneratedId.get();
17113 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17114 int newValue = result + 1;
17115 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17116 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17117 return result;
17118 }
17119 }
17120 }
17121
Chet Haaseb39f0512011-05-24 14:36:40 -070017122 //
17123 // Properties
17124 //
17125 /**
17126 * A Property wrapper around the <code>alpha</code> functionality handled by the
17127 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17128 */
Chet Haased47f1532011-12-16 11:18:52 -080017129 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017130 @Override
17131 public void setValue(View object, float value) {
17132 object.setAlpha(value);
17133 }
17134
17135 @Override
17136 public Float get(View object) {
17137 return object.getAlpha();
17138 }
17139 };
17140
17141 /**
17142 * A Property wrapper around the <code>translationX</code> functionality handled by the
17143 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17144 */
Chet Haased47f1532011-12-16 11:18:52 -080017145 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017146 @Override
17147 public void setValue(View object, float value) {
17148 object.setTranslationX(value);
17149 }
17150
17151 @Override
17152 public Float get(View object) {
17153 return object.getTranslationX();
17154 }
17155 };
17156
17157 /**
17158 * A Property wrapper around the <code>translationY</code> functionality handled by the
17159 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17160 */
Chet Haased47f1532011-12-16 11:18:52 -080017161 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017162 @Override
17163 public void setValue(View object, float value) {
17164 object.setTranslationY(value);
17165 }
17166
17167 @Override
17168 public Float get(View object) {
17169 return object.getTranslationY();
17170 }
17171 };
17172
17173 /**
17174 * A Property wrapper around the <code>x</code> functionality handled by the
17175 * {@link View#setX(float)} and {@link View#getX()} methods.
17176 */
Chet Haased47f1532011-12-16 11:18:52 -080017177 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017178 @Override
17179 public void setValue(View object, float value) {
17180 object.setX(value);
17181 }
17182
17183 @Override
17184 public Float get(View object) {
17185 return object.getX();
17186 }
17187 };
17188
17189 /**
17190 * A Property wrapper around the <code>y</code> functionality handled by the
17191 * {@link View#setY(float)} and {@link View#getY()} methods.
17192 */
Chet Haased47f1532011-12-16 11:18:52 -080017193 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017194 @Override
17195 public void setValue(View object, float value) {
17196 object.setY(value);
17197 }
17198
17199 @Override
17200 public Float get(View object) {
17201 return object.getY();
17202 }
17203 };
17204
17205 /**
17206 * A Property wrapper around the <code>rotation</code> functionality handled by the
17207 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17208 */
Chet Haased47f1532011-12-16 11:18:52 -080017209 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017210 @Override
17211 public void setValue(View object, float value) {
17212 object.setRotation(value);
17213 }
17214
17215 @Override
17216 public Float get(View object) {
17217 return object.getRotation();
17218 }
17219 };
17220
17221 /**
17222 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17223 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17224 */
Chet Haased47f1532011-12-16 11:18:52 -080017225 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017226 @Override
17227 public void setValue(View object, float value) {
17228 object.setRotationX(value);
17229 }
17230
17231 @Override
17232 public Float get(View object) {
17233 return object.getRotationX();
17234 }
17235 };
17236
17237 /**
17238 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17239 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17240 */
Chet Haased47f1532011-12-16 11:18:52 -080017241 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017242 @Override
17243 public void setValue(View object, float value) {
17244 object.setRotationY(value);
17245 }
17246
17247 @Override
17248 public Float get(View object) {
17249 return object.getRotationY();
17250 }
17251 };
17252
17253 /**
17254 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17255 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17256 */
Chet Haased47f1532011-12-16 11:18:52 -080017257 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017258 @Override
17259 public void setValue(View object, float value) {
17260 object.setScaleX(value);
17261 }
17262
17263 @Override
17264 public Float get(View object) {
17265 return object.getScaleX();
17266 }
17267 };
17268
17269 /**
17270 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17271 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17272 */
Chet Haased47f1532011-12-16 11:18:52 -080017273 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017274 @Override
17275 public void setValue(View object, float value) {
17276 object.setScaleY(value);
17277 }
17278
17279 @Override
17280 public Float get(View object) {
17281 return object.getScaleY();
17282 }
17283 };
17284
Jeff Brown33bbfd22011-02-24 20:55:35 -080017285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017286 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17287 * Each MeasureSpec represents a requirement for either the width or the height.
17288 * A MeasureSpec is comprised of a size and a mode. There are three possible
17289 * modes:
17290 * <dl>
17291 * <dt>UNSPECIFIED</dt>
17292 * <dd>
17293 * The parent has not imposed any constraint on the child. It can be whatever size
17294 * it wants.
17295 * </dd>
17296 *
17297 * <dt>EXACTLY</dt>
17298 * <dd>
17299 * The parent has determined an exact size for the child. The child is going to be
17300 * given those bounds regardless of how big it wants to be.
17301 * </dd>
17302 *
17303 * <dt>AT_MOST</dt>
17304 * <dd>
17305 * The child can be as large as it wants up to the specified size.
17306 * </dd>
17307 * </dl>
17308 *
17309 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17310 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17311 */
17312 public static class MeasureSpec {
17313 private static final int MODE_SHIFT = 30;
17314 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17315
17316 /**
17317 * Measure specification mode: The parent has not imposed any constraint
17318 * on the child. It can be whatever size it wants.
17319 */
17320 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17321
17322 /**
17323 * Measure specification mode: The parent has determined an exact size
17324 * for the child. The child is going to be given those bounds regardless
17325 * of how big it wants to be.
17326 */
17327 public static final int EXACTLY = 1 << MODE_SHIFT;
17328
17329 /**
17330 * Measure specification mode: The child can be as large as it wants up
17331 * to the specified size.
17332 */
17333 public static final int AT_MOST = 2 << MODE_SHIFT;
17334
17335 /**
17336 * Creates a measure specification based on the supplied size and mode.
17337 *
17338 * The mode must always be one of the following:
17339 * <ul>
17340 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17341 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17342 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17343 * </ul>
17344 *
Adam Powell2c8cc972012-12-07 18:04:51 -080017345 * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
17346 * implementation was such that the order of arguments did not matter
17347 * and overflow in either value could impact the resulting MeasureSpec.
17348 * {@link android.widget.RelativeLayout} was affected by this bug.
17349 * Apps targeting API levels greater than 17 will get the fixed, more strict
17350 * behavior.</p>
17351 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 * @param size the size of the measure specification
17353 * @param mode the mode of the measure specification
17354 * @return the measure specification based on size and mode
17355 */
17356 public static int makeMeasureSpec(int size, int mode) {
Adam Powell7da4b732012-12-07 15:28:33 -080017357 if (sUseBrokenMakeMeasureSpec) {
17358 return size + mode;
17359 } else {
17360 return (size & ~MODE_MASK) | (mode & MODE_MASK);
17361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017362 }
17363
17364 /**
17365 * Extracts the mode from the supplied measure specification.
17366 *
17367 * @param measureSpec the measure specification to extract the mode from
17368 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17369 * {@link android.view.View.MeasureSpec#AT_MOST} or
17370 * {@link android.view.View.MeasureSpec#EXACTLY}
17371 */
17372 public static int getMode(int measureSpec) {
17373 return (measureSpec & MODE_MASK);
17374 }
17375
17376 /**
17377 * Extracts the size from the supplied measure specification.
17378 *
17379 * @param measureSpec the measure specification to extract the size from
17380 * @return the size in pixels defined in the supplied measure specification
17381 */
17382 public static int getSize(int measureSpec) {
17383 return (measureSpec & ~MODE_MASK);
17384 }
17385
Philip Milne7b757812012-09-19 18:13:44 -070017386 static int adjust(int measureSpec, int delta) {
17387 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17388 }
17389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017390 /**
17391 * Returns a String representation of the specified measure
17392 * specification.
17393 *
17394 * @param measureSpec the measure specification to convert to a String
17395 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17396 */
17397 public static String toString(int measureSpec) {
17398 int mode = getMode(measureSpec);
17399 int size = getSize(measureSpec);
17400
17401 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17402
17403 if (mode == UNSPECIFIED)
17404 sb.append("UNSPECIFIED ");
17405 else if (mode == EXACTLY)
17406 sb.append("EXACTLY ");
17407 else if (mode == AT_MOST)
17408 sb.append("AT_MOST ");
17409 else
17410 sb.append(mode).append(" ");
17411
17412 sb.append(size);
17413 return sb.toString();
17414 }
17415 }
17416
17417 class CheckForLongPress implements Runnable {
17418
17419 private int mOriginalWindowAttachCount;
17420
17421 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017422 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423 && mOriginalWindowAttachCount == mWindowAttachCount) {
17424 if (performLongClick()) {
17425 mHasPerformedLongPress = true;
17426 }
17427 }
17428 }
17429
17430 public void rememberWindowAttachCount() {
17431 mOriginalWindowAttachCount = mWindowAttachCount;
17432 }
17433 }
Joe Malin32736f02011-01-19 16:14:20 -080017434
Adam Powelle14579b2009-12-16 18:39:52 -080017435 private final class CheckForTap implements Runnable {
17436 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017437 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017438 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017439 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017440 }
17441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017442
Adam Powella35d7682010-03-12 14:48:13 -080017443 private final class PerformClick implements Runnable {
17444 public void run() {
17445 performClick();
17446 }
17447 }
17448
Dianne Hackborn63042d62011-01-26 18:56:29 -080017449 /** @hide */
17450 public void hackTurnOffWindowResizeAnim(boolean off) {
17451 mAttachInfo.mTurnOffWindowResizeAnim = off;
17452 }
Joe Malin32736f02011-01-19 16:14:20 -080017453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017454 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017455 * This method returns a ViewPropertyAnimator object, which can be used to animate
17456 * specific properties on this View.
17457 *
17458 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17459 */
17460 public ViewPropertyAnimator animate() {
17461 if (mAnimator == null) {
17462 mAnimator = new ViewPropertyAnimator(this);
17463 }
17464 return mAnimator;
17465 }
17466
17467 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017468 * Interface definition for a callback to be invoked when a hardware key event is
17469 * dispatched to this view. The callback will be invoked before the key event is
17470 * given to the view. This is only useful for hardware keyboards; a software input
17471 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017472 */
17473 public interface OnKeyListener {
17474 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017475 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017476 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017477 * <p>Key presses in software keyboards will generally NOT trigger this method,
17478 * although some may elect to do so in some situations. Do not assume a
17479 * software input method has to be key-based; even if it is, it may use key presses
17480 * in a different way than you expect, so there is no way to reliably catch soft
17481 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017482 *
17483 * @param v The view the key has been dispatched to.
17484 * @param keyCode The code for the physical key that was pressed
17485 * @param event The KeyEvent object containing full information about
17486 * the event.
17487 * @return True if the listener has consumed the event, false otherwise.
17488 */
17489 boolean onKey(View v, int keyCode, KeyEvent event);
17490 }
17491
17492 /**
17493 * Interface definition for a callback to be invoked when a touch event is
17494 * dispatched to this view. The callback will be invoked before the touch
17495 * event is given to the view.
17496 */
17497 public interface OnTouchListener {
17498 /**
17499 * Called when a touch event is dispatched to a view. This allows listeners to
17500 * get a chance to respond before the target view.
17501 *
17502 * @param v The view the touch event has been dispatched to.
17503 * @param event The MotionEvent object containing full information about
17504 * the event.
17505 * @return True if the listener has consumed the event, false otherwise.
17506 */
17507 boolean onTouch(View v, MotionEvent event);
17508 }
17509
17510 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017511 * Interface definition for a callback to be invoked when a hover event is
17512 * dispatched to this view. The callback will be invoked before the hover
17513 * event is given to the view.
17514 */
17515 public interface OnHoverListener {
17516 /**
17517 * Called when a hover event is dispatched to a view. This allows listeners to
17518 * get a chance to respond before the target view.
17519 *
17520 * @param v The view the hover event has been dispatched to.
17521 * @param event The MotionEvent object containing full information about
17522 * the event.
17523 * @return True if the listener has consumed the event, false otherwise.
17524 */
17525 boolean onHover(View v, MotionEvent event);
17526 }
17527
17528 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017529 * Interface definition for a callback to be invoked when a generic motion event is
17530 * dispatched to this view. The callback will be invoked before the generic motion
17531 * event is given to the view.
17532 */
17533 public interface OnGenericMotionListener {
17534 /**
17535 * Called when a generic motion event is dispatched to a view. This allows listeners to
17536 * get a chance to respond before the target view.
17537 *
17538 * @param v The view the generic motion event has been dispatched to.
17539 * @param event The MotionEvent object containing full information about
17540 * the event.
17541 * @return True if the listener has consumed the event, false otherwise.
17542 */
17543 boolean onGenericMotion(View v, MotionEvent event);
17544 }
17545
17546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017547 * Interface definition for a callback to be invoked when a view has been clicked and held.
17548 */
17549 public interface OnLongClickListener {
17550 /**
17551 * Called when a view has been clicked and held.
17552 *
17553 * @param v The view that was clicked and held.
17554 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017555 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017556 */
17557 boolean onLongClick(View v);
17558 }
17559
17560 /**
Chris Tate32affef2010-10-18 15:29:21 -070017561 * Interface definition for a callback to be invoked when a drag is being dispatched
17562 * to this view. The callback will be invoked before the hosting view's own
17563 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17564 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017565 *
17566 * <div class="special reference">
17567 * <h3>Developer Guides</h3>
17568 * <p>For a guide to implementing drag and drop features, read the
17569 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17570 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017571 */
17572 public interface OnDragListener {
17573 /**
17574 * Called when a drag event is dispatched to a view. This allows listeners
17575 * to get a chance to override base View behavior.
17576 *
Joe Malin32736f02011-01-19 16:14:20 -080017577 * @param v The View that received the drag event.
17578 * @param event The {@link android.view.DragEvent} object for the drag event.
17579 * @return {@code true} if the drag event was handled successfully, or {@code false}
17580 * if the drag event was not handled. Note that {@code false} will trigger the View
17581 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017582 */
17583 boolean onDrag(View v, DragEvent event);
17584 }
17585
17586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017587 * Interface definition for a callback to be invoked when the focus state of
17588 * a view changed.
17589 */
17590 public interface OnFocusChangeListener {
17591 /**
17592 * Called when the focus state of a view has changed.
17593 *
17594 * @param v The view whose state has changed.
17595 * @param hasFocus The new focus state of v.
17596 */
17597 void onFocusChange(View v, boolean hasFocus);
17598 }
17599
17600 /**
17601 * Interface definition for a callback to be invoked when a view is clicked.
17602 */
17603 public interface OnClickListener {
17604 /**
17605 * Called when a view has been clicked.
17606 *
17607 * @param v The view that was clicked.
17608 */
17609 void onClick(View v);
17610 }
17611
17612 /**
17613 * Interface definition for a callback to be invoked when the context menu
17614 * for this view is being built.
17615 */
17616 public interface OnCreateContextMenuListener {
17617 /**
17618 * Called when the context menu for this view is being built. It is not
17619 * safe to hold onto the menu after this method returns.
17620 *
17621 * @param menu The context menu that is being built
17622 * @param v The view for which the context menu is being built
17623 * @param menuInfo Extra information about the item for which the
17624 * context menu should be shown. This information will vary
17625 * depending on the class of v.
17626 */
17627 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17628 }
17629
Joe Onorato664644d2011-01-23 17:53:23 -080017630 /**
17631 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017632 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017633 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017634 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017635 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017636 */
17637 public interface OnSystemUiVisibilityChangeListener {
17638 /**
17639 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017640 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017641 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017642 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17643 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17644 * This tells you the <strong>global</strong> state of these UI visibility
17645 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017646 */
17647 public void onSystemUiVisibilityChange(int visibility);
17648 }
17649
Adam Powell4afd62b2011-02-18 15:02:18 -080017650 /**
17651 * Interface definition for a callback to be invoked when this view is attached
17652 * or detached from its window.
17653 */
17654 public interface OnAttachStateChangeListener {
17655 /**
17656 * Called when the view is attached to a window.
17657 * @param v The view that was attached
17658 */
17659 public void onViewAttachedToWindow(View v);
17660 /**
17661 * Called when the view is detached from a window.
17662 * @param v The view that was detached
17663 */
17664 public void onViewDetachedFromWindow(View v);
17665 }
17666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017667 private final class UnsetPressedState implements Runnable {
17668 public void run() {
17669 setPressed(false);
17670 }
17671 }
17672
17673 /**
17674 * Base class for derived classes that want to save and restore their own
17675 * state in {@link android.view.View#onSaveInstanceState()}.
17676 */
17677 public static class BaseSavedState extends AbsSavedState {
17678 /**
17679 * Constructor used when reading from a parcel. Reads the state of the superclass.
17680 *
17681 * @param source
17682 */
17683 public BaseSavedState(Parcel source) {
17684 super(source);
17685 }
17686
17687 /**
17688 * Constructor called by derived classes when creating their SavedState objects
17689 *
17690 * @param superState The state of the superclass of this view
17691 */
17692 public BaseSavedState(Parcelable superState) {
17693 super(superState);
17694 }
17695
17696 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17697 new Parcelable.Creator<BaseSavedState>() {
17698 public BaseSavedState createFromParcel(Parcel in) {
17699 return new BaseSavedState(in);
17700 }
17701
17702 public BaseSavedState[] newArray(int size) {
17703 return new BaseSavedState[size];
17704 }
17705 };
17706 }
17707
17708 /**
17709 * A set of information given to a view when it is attached to its parent
17710 * window.
17711 */
17712 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017713 interface Callbacks {
17714 void playSoundEffect(int effectId);
17715 boolean performHapticFeedback(int effectId, boolean always);
17716 }
17717
17718 /**
17719 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17720 * to a Handler. This class contains the target (View) to invalidate and
17721 * the coordinates of the dirty rectangle.
17722 *
17723 * For performance purposes, this class also implements a pool of up to
17724 * POOL_LIMIT objects that get reused. This reduces memory allocations
17725 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017726 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017727 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017728 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017729
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017730 private static final SynchronizedPool<InvalidateInfo> sPool =
17731 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017732
17733 View target;
17734
17735 int left;
17736 int top;
17737 int right;
17738 int bottom;
17739
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017740 public static InvalidateInfo obtain() {
17741 InvalidateInfo instance = sPool.acquire();
17742 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070017743 }
17744
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017745 public void recycle() {
17746 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017747 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017748 }
17749 }
17750
17751 final IWindowSession mSession;
17752
17753 final IWindow mWindow;
17754
17755 final IBinder mWindowToken;
17756
Jeff Brown98365d72012-08-19 20:30:52 -070017757 final Display mDisplay;
17758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017759 final Callbacks mRootCallbacks;
17760
Romain Guy59a12ca2011-06-09 17:48:21 -070017761 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017763 /**
17764 * The top view of the hierarchy.
17765 */
17766 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017768 IBinder mPanelParentWindowToken;
17769 Surface mSurface;
17770
Romain Guyb051e892010-09-28 19:09:36 -070017771 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017772 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017773 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017774
Romain Guy7e4e5612012-03-05 14:37:29 -080017775 boolean mScreenOn;
17776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017777 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017778 * Scale factor used by the compatibility mode
17779 */
17780 float mApplicationScale;
17781
17782 /**
17783 * Indicates whether the application is in compatibility mode
17784 */
17785 boolean mScalingRequired;
17786
17787 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017788 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017789 */
17790 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017791
Dianne Hackborn63042d62011-01-26 18:56:29 -080017792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017793 * Left position of this view's window
17794 */
17795 int mWindowLeft;
17796
17797 /**
17798 * Top position of this view's window
17799 */
17800 int mWindowTop;
17801
17802 /**
Adam Powell26153a32010-11-08 15:22:27 -080017803 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017804 */
Adam Powell26153a32010-11-08 15:22:27 -080017805 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017806
17807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017808 * For windows that are full-screen but using insets to layout inside
17809 * of the screen decorations, these are the current insets for the
17810 * content of the window.
17811 */
17812 final Rect mContentInsets = new Rect();
17813
17814 /**
17815 * For windows that are full-screen but using insets to layout inside
17816 * of the screen decorations, these are the current insets for the
17817 * actual visible parts of the window.
17818 */
17819 final Rect mVisibleInsets = new Rect();
17820
17821 /**
17822 * The internal insets given by this window. This value is
17823 * supplied by the client (through
17824 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17825 * be given to the window manager when changed to be used in laying
17826 * out windows behind it.
17827 */
17828 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17829 = new ViewTreeObserver.InternalInsetsInfo();
17830
17831 /**
17832 * All views in the window's hierarchy that serve as scroll containers,
17833 * used to determine if the window can be resized or must be panned
17834 * to adjust for a soft input area.
17835 */
17836 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17837
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017838 final KeyEvent.DispatcherState mKeyDispatchState
17839 = new KeyEvent.DispatcherState();
17840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017841 /**
17842 * Indicates whether the view's window currently has the focus.
17843 */
17844 boolean mHasWindowFocus;
17845
17846 /**
17847 * The current visibility of the window.
17848 */
17849 int mWindowVisibility;
17850
17851 /**
17852 * Indicates the time at which drawing started to occur.
17853 */
17854 long mDrawingTime;
17855
17856 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017857 * Indicates whether or not ignoring the DIRTY_MASK flags.
17858 */
17859 boolean mIgnoreDirtyState;
17860
17861 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017862 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17863 * to avoid clearing that flag prematurely.
17864 */
17865 boolean mSetIgnoreDirtyState = false;
17866
17867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017868 * Indicates whether the view's window is currently in touch mode.
17869 */
17870 boolean mInTouchMode;
17871
17872 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017873 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017874 * the next time it performs a traversal
17875 */
17876 boolean mRecomputeGlobalAttributes;
17877
17878 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017879 * Always report new attributes at next traversal.
17880 */
17881 boolean mForceReportNewAttributes;
17882
17883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017884 * Set during a traveral if any views want to keep the screen on.
17885 */
17886 boolean mKeepScreenOn;
17887
17888 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017889 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17890 */
17891 int mSystemUiVisibility;
17892
17893 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017894 * Hack to force certain system UI visibility flags to be cleared.
17895 */
17896 int mDisabledSystemUiVisibility;
17897
17898 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017899 * Last global system UI visibility reported by the window manager.
17900 */
17901 int mGlobalSystemUiVisibility;
17902
17903 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017904 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17905 * attached.
17906 */
17907 boolean mHasSystemUiListeners;
17908
17909 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017910 * Set if the visibility of any views has changed.
17911 */
17912 boolean mViewVisibilityChanged;
17913
17914 /**
17915 * Set to true if a view has been scrolled.
17916 */
17917 boolean mViewScrollChanged;
17918
17919 /**
17920 * Global to the view hierarchy used as a temporary for dealing with
17921 * x/y points in the transparent region computations.
17922 */
17923 final int[] mTransparentLocation = new int[2];
17924
17925 /**
17926 * Global to the view hierarchy used as a temporary for dealing with
17927 * x/y points in the ViewGroup.invalidateChild implementation.
17928 */
17929 final int[] mInvalidateChildLocation = new int[2];
17930
Chet Haasec3aa3612010-06-17 08:50:37 -070017931
17932 /**
17933 * Global to the view hierarchy used as a temporary for dealing with
17934 * x/y location when view is transformed.
17935 */
17936 final float[] mTmpTransformLocation = new float[2];
17937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017938 /**
17939 * The view tree observer used to dispatch global events like
17940 * layout, pre-draw, touch mode change, etc.
17941 */
17942 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17943
17944 /**
17945 * A Canvas used by the view hierarchy to perform bitmap caching.
17946 */
17947 Canvas mCanvas;
17948
17949 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017950 * The view root impl.
17951 */
17952 final ViewRootImpl mViewRootImpl;
17953
17954 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017955 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017956 * handler can be used to pump events in the UI events queue.
17957 */
17958 final Handler mHandler;
17959
17960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017961 * Temporary for use in computing invalidate rectangles while
17962 * calling up the hierarchy.
17963 */
17964 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017965
17966 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017967 * Temporary for use in computing hit areas with transformed views
17968 */
17969 final RectF mTmpTransformRect = new RectF();
17970
17971 /**
Chet Haase599913d2012-07-23 16:22:05 -070017972 * Temporary for use in transforming invalidation rect
17973 */
17974 final Matrix mTmpMatrix = new Matrix();
17975
17976 /**
17977 * Temporary for use in transforming invalidation rect
17978 */
17979 final Transformation mTmpTransformation = new Transformation();
17980
17981 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017982 * Temporary list for use in collecting focusable descendents of a view.
17983 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017984 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017986 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017987 * The id of the window for accessibility purposes.
17988 */
17989 int mAccessibilityWindowId = View.NO_ID;
17990
17991 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017992 * Whether to ingore not exposed for accessibility Views when
17993 * reporting the view tree to accessibility services.
17994 */
17995 boolean mIncludeNotImportantViews;
17996
17997 /**
17998 * The drawable for highlighting accessibility focus.
17999 */
18000 Drawable mAccessibilityFocusDrawable;
18001
18002 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070018003 * Show where the margins, bounds and layout bounds are for each view.
18004 */
Dianne Hackborna53de062012-05-08 18:53:51 -070018005 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070018006
18007 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070018008 * Point used to compute visible regions.
18009 */
18010 final Point mPoint = new Point();
18011
18012 /**
Chet Haasecc699b42012-12-13 09:06:55 -080018013 * Used to track which View originated a requestLayout() call, used when
18014 * requestLayout() is called during layout.
18015 */
18016 View mViewRequestingLayout;
18017
18018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018019 * Creates a new set of attachment information with the specified
18020 * events handler and thread.
18021 *
18022 * @param handler the events handler the view must use
18023 */
Jeff Brown98365d72012-08-19 20:30:52 -070018024 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080018025 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018026 mSession = session;
18027 mWindow = window;
18028 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070018029 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080018030 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018031 mHandler = handler;
18032 mRootCallbacks = effectPlayer;
18033 }
18034 }
18035
18036 /**
18037 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18038 * is supported. This avoids keeping too many unused fields in most
18039 * instances of View.</p>
18040 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018041 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018042
Mike Cleronf116bf82009-09-27 19:14:12 -070018043 /**
18044 * Scrollbars are not visible
18045 */
18046 public static final int OFF = 0;
18047
18048 /**
18049 * Scrollbars are visible
18050 */
18051 public static final int ON = 1;
18052
18053 /**
18054 * Scrollbars are fading away
18055 */
18056 public static final int FADING = 2;
18057
18058 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018060 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018061 public int scrollBarDefaultDelayBeforeFade;
18062 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018063
18064 public int scrollBarSize;
18065 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018066 public float[] interpolatorValues;
18067 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018068
18069 public final Paint paint;
18070 public final Matrix matrix;
18071 public Shader shader;
18072
Mike Cleronf116bf82009-09-27 19:14:12 -070018073 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18074
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018075 private static final float[] OPAQUE = { 255 };
18076 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018077
Mike Cleronf116bf82009-09-27 19:14:12 -070018078 /**
18079 * When fading should start. This time moves into the future every time
18080 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18081 */
18082 public long fadeStartTime;
18083
18084
18085 /**
18086 * The current state of the scrollbars: ON, OFF, or FADING
18087 */
18088 public int state = OFF;
18089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018090 private int mLastColor;
18091
Mike Cleronf116bf82009-09-27 19:14:12 -070018092 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018093 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18094 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018095 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18096 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018097
18098 paint = new Paint();
18099 matrix = new Matrix();
18100 // use use a height of 1, and then wack the matrix each time we
18101 // actually use it.
18102 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018103 paint.setShader(shader);
18104 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018105
Mike Cleronf116bf82009-09-27 19:14:12 -070018106 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018107 }
Romain Guy8506ab42009-06-11 17:35:47 -070018108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018109 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018110 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018111 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018112
Romain Guyd679b572012-08-29 21:49:00 -070018113 if (color != 0) {
18114 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18115 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18116 paint.setShader(shader);
18117 // Restore the default transfer mode (src_over)
18118 paint.setXfermode(null);
18119 } else {
18120 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18121 paint.setShader(shader);
18122 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018124 }
18125 }
Joe Malin32736f02011-01-19 16:14:20 -080018126
Mike Cleronf116bf82009-09-27 19:14:12 -070018127 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018128 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018129 if (now >= fadeStartTime) {
18130
18131 // the animation fades the scrollbars out by changing
18132 // the opacity (alpha) from fully opaque to fully
18133 // transparent
18134 int nextFrame = (int) now;
18135 int framesCount = 0;
18136
18137 Interpolator interpolator = scrollBarInterpolator;
18138
18139 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018140 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018141
18142 // End transparent
18143 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018144 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018145
18146 state = FADING;
18147
18148 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018149 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018150 }
18151 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018152 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018153
Svetoslav Ganova0156172011-06-26 17:55:44 -070018154 /**
18155 * Resuable callback for sending
18156 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18157 */
18158 private class SendViewScrolledAccessibilityEvent implements Runnable {
18159 public volatile boolean mIsPending;
18160
18161 public void run() {
18162 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18163 mIsPending = false;
18164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018165 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018166
18167 /**
18168 * <p>
18169 * This class represents a delegate that can be registered in a {@link View}
18170 * to enhance accessibility support via composition rather via inheritance.
18171 * It is specifically targeted to widget developers that extend basic View
18172 * classes i.e. classes in package android.view, that would like their
18173 * applications to be backwards compatible.
18174 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018175 * <div class="special reference">
18176 * <h3>Developer Guides</h3>
18177 * <p>For more information about making applications accessible, read the
18178 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18179 * developer guide.</p>
18180 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018181 * <p>
18182 * A scenario in which a developer would like to use an accessibility delegate
18183 * is overriding a method introduced in a later API version then the minimal API
18184 * version supported by the application. For example, the method
18185 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18186 * in API version 4 when the accessibility APIs were first introduced. If a
18187 * developer would like his application to run on API version 4 devices (assuming
18188 * all other APIs used by the application are version 4 or lower) and take advantage
18189 * of this method, instead of overriding the method which would break the application's
18190 * backwards compatibility, he can override the corresponding method in this
18191 * delegate and register the delegate in the target View if the API version of
18192 * the system is high enough i.e. the API version is same or higher to the API
18193 * version that introduced
18194 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18195 * </p>
18196 * <p>
18197 * Here is an example implementation:
18198 * </p>
18199 * <code><pre><p>
18200 * if (Build.VERSION.SDK_INT >= 14) {
18201 * // If the API version is equal of higher than the version in
18202 * // which onInitializeAccessibilityNodeInfo was introduced we
18203 * // register a delegate with a customized implementation.
18204 * View view = findViewById(R.id.view_id);
18205 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18206 * public void onInitializeAccessibilityNodeInfo(View host,
18207 * AccessibilityNodeInfo info) {
18208 * // Let the default implementation populate the info.
18209 * super.onInitializeAccessibilityNodeInfo(host, info);
18210 * // Set some other information.
18211 * info.setEnabled(host.isEnabled());
18212 * }
18213 * });
18214 * }
18215 * </code></pre></p>
18216 * <p>
18217 * This delegate contains methods that correspond to the accessibility methods
18218 * in View. If a delegate has been specified the implementation in View hands
18219 * off handling to the corresponding method in this delegate. The default
18220 * implementation the delegate methods behaves exactly as the corresponding
18221 * method in View for the case of no accessibility delegate been set. Hence,
18222 * to customize the behavior of a View method, clients can override only the
18223 * corresponding delegate method without altering the behavior of the rest
18224 * accessibility related methods of the host view.
18225 * </p>
18226 */
18227 public static class AccessibilityDelegate {
18228
18229 /**
18230 * Sends an accessibility event of the given type. If accessibility is not
18231 * enabled this method has no effect.
18232 * <p>
18233 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18234 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18235 * been set.
18236 * </p>
18237 *
18238 * @param host The View hosting the delegate.
18239 * @param eventType The type of the event to send.
18240 *
18241 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18242 */
18243 public void sendAccessibilityEvent(View host, int eventType) {
18244 host.sendAccessibilityEventInternal(eventType);
18245 }
18246
18247 /**
alanv8eeefef2012-05-07 16:57:53 -070018248 * Performs the specified accessibility action on the view. For
18249 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18250 * <p>
18251 * The default implementation behaves as
18252 * {@link View#performAccessibilityAction(int, Bundle)
18253 * View#performAccessibilityAction(int, Bundle)} for the case of
18254 * no accessibility delegate been set.
18255 * </p>
18256 *
18257 * @param action The action to perform.
18258 * @return Whether the action was performed.
18259 *
18260 * @see View#performAccessibilityAction(int, Bundle)
18261 * View#performAccessibilityAction(int, Bundle)
18262 */
18263 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18264 return host.performAccessibilityActionInternal(action, args);
18265 }
18266
18267 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018268 * Sends an accessibility event. This method behaves exactly as
18269 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18270 * empty {@link AccessibilityEvent} and does not perform a check whether
18271 * accessibility is enabled.
18272 * <p>
18273 * The default implementation behaves as
18274 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18275 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18276 * the case of no accessibility delegate been set.
18277 * </p>
18278 *
18279 * @param host The View hosting the delegate.
18280 * @param event The event to send.
18281 *
18282 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18283 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18284 */
18285 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18286 host.sendAccessibilityEventUncheckedInternal(event);
18287 }
18288
18289 /**
18290 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18291 * to its children for adding their text content to the event.
18292 * <p>
18293 * The default implementation behaves as
18294 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18295 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18296 * the case of no accessibility delegate been set.
18297 * </p>
18298 *
18299 * @param host The View hosting the delegate.
18300 * @param event The event.
18301 * @return True if the event population was completed.
18302 *
18303 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18304 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18305 */
18306 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18307 return host.dispatchPopulateAccessibilityEventInternal(event);
18308 }
18309
18310 /**
18311 * Gives a chance to the host View to populate the accessibility event with its
18312 * text content.
18313 * <p>
18314 * The default implementation behaves as
18315 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18316 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18317 * the case of no accessibility delegate been set.
18318 * </p>
18319 *
18320 * @param host The View hosting the delegate.
18321 * @param event The accessibility event which to populate.
18322 *
18323 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18324 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18325 */
18326 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18327 host.onPopulateAccessibilityEventInternal(event);
18328 }
18329
18330 /**
18331 * Initializes an {@link AccessibilityEvent} with information about the
18332 * the host View which is the event source.
18333 * <p>
18334 * The default implementation behaves as
18335 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18336 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18337 * the case of no accessibility delegate been set.
18338 * </p>
18339 *
18340 * @param host The View hosting the delegate.
18341 * @param event The event to initialize.
18342 *
18343 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18344 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18345 */
18346 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18347 host.onInitializeAccessibilityEventInternal(event);
18348 }
18349
18350 /**
18351 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18352 * <p>
18353 * The default implementation behaves as
18354 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18355 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18356 * the case of no accessibility delegate been set.
18357 * </p>
18358 *
18359 * @param host The View hosting the delegate.
18360 * @param info The instance to initialize.
18361 *
18362 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18363 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18364 */
18365 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18366 host.onInitializeAccessibilityNodeInfoInternal(info);
18367 }
18368
18369 /**
18370 * Called when a child of the host View has requested sending an
18371 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18372 * to augment the event.
18373 * <p>
18374 * The default implementation behaves as
18375 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18376 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18377 * the case of no accessibility delegate been set.
18378 * </p>
18379 *
18380 * @param host The View hosting the delegate.
18381 * @param child The child which requests sending the event.
18382 * @param event The event to be sent.
18383 * @return True if the event should be sent
18384 *
18385 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18386 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18387 */
18388 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18389 AccessibilityEvent event) {
18390 return host.onRequestSendAccessibilityEventInternal(child, event);
18391 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018392
18393 /**
18394 * Gets the provider for managing a virtual view hierarchy rooted at this View
18395 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18396 * that explore the window content.
18397 * <p>
18398 * The default implementation behaves as
18399 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18400 * the case of no accessibility delegate been set.
18401 * </p>
18402 *
18403 * @return The provider.
18404 *
18405 * @see AccessibilityNodeProvider
18406 */
18407 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18408 return null;
18409 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018410 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018411
18412 private class MatchIdPredicate implements Predicate<View> {
18413 public int mId;
18414
18415 @Override
18416 public boolean apply(View view) {
18417 return (view.mID == mId);
18418 }
18419 }
18420
18421 private class MatchLabelForPredicate implements Predicate<View> {
18422 private int mLabeledId;
18423
18424 @Override
18425 public boolean apply(View view) {
18426 return (view.mLabelForId == mLabeledId);
18427 }
18428 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018429
18430 /**
18431 * Dump all private flags in readable format, useful for documentation and
18432 * sanity checking.
18433 */
18434 private static void dumpFlags() {
18435 final HashMap<String, String> found = Maps.newHashMap();
18436 try {
18437 for (Field field : View.class.getDeclaredFields()) {
18438 final int modifiers = field.getModifiers();
18439 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18440 if (field.getType().equals(int.class)) {
18441 final int value = field.getInt(null);
18442 dumpFlag(found, field.getName(), value);
18443 } else if (field.getType().equals(int[].class)) {
18444 final int[] values = (int[]) field.get(null);
18445 for (int i = 0; i < values.length; i++) {
18446 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18447 }
18448 }
18449 }
18450 }
18451 } catch (IllegalAccessException e) {
18452 throw new RuntimeException(e);
18453 }
18454
18455 final ArrayList<String> keys = Lists.newArrayList();
18456 keys.addAll(found.keySet());
18457 Collections.sort(keys);
18458 for (String key : keys) {
18459 Log.d(VIEW_LOG_TAG, found.get(key));
18460 }
18461 }
18462
18463 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18464 // Sort flags by prefix, then by bits, always keeping unique keys
18465 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18466 final int prefix = name.indexOf('_');
18467 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18468 final String output = bits + " " + name;
18469 found.put(key, output);
18470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018471}