blob: a4e4f37927a8acc0d2498554f936c27f81a77c4e [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
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004379 View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null;
4380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 if (mParent != null) {
4382 mParent.requestChildFocus(this, this);
4383 }
4384
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004385 if (mAttachInfo != null) {
4386 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, this);
4387 }
4388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389 onFocusChanged(true, direction, previouslyFocusedRect);
4390 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004391
4392 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4393 notifyAccessibilityStateChanged();
4394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 }
4396 }
4397
4398 /**
4399 * Request that a rectangle of this view be visible on the screen,
4400 * scrolling if necessary just enough.
4401 *
4402 * <p>A View should call this if it maintains some notion of which part
4403 * of its content is interesting. For example, a text editing view
4404 * should call this when its cursor moves.
4405 *
4406 * @param rectangle The rectangle.
4407 * @return Whether any parent scrolled.
4408 */
4409 public boolean requestRectangleOnScreen(Rect rectangle) {
4410 return requestRectangleOnScreen(rectangle, false);
4411 }
4412
4413 /**
4414 * Request that a rectangle of this view be visible on the screen,
4415 * scrolling if necessary just enough.
4416 *
4417 * <p>A View should call this if it maintains some notion of which part
4418 * of its content is interesting. For example, a text editing view
4419 * should call this when its cursor moves.
4420 *
4421 * <p>When <code>immediate</code> is set to true, scrolling will not be
4422 * animated.
4423 *
4424 * @param rectangle The rectangle.
4425 * @param immediate True to forbid animated scrolling, false otherwise
4426 * @return Whether any parent scrolled.
4427 */
4428 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004429 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004430 return false;
4431 }
4432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004434
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004435 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004436 position.set(rectangle);
4437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 ViewParent parent = mParent;
4439 boolean scrolled = false;
4440 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004441 rectangle.set((int) position.left, (int) position.top,
4442 (int) position.right, (int) position.bottom);
4443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 scrolled |= parent.requestChildRectangleOnScreen(child,
4445 rectangle, immediate);
4446
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004447 if (!child.hasIdentityMatrix()) {
4448 child.getMatrix().mapRect(position);
4449 }
4450
4451 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452
4453 if (!(parent instanceof View)) {
4454 break;
4455 }
Romain Guy8506ab42009-06-11 17:35:47 -07004456
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004457 View parentView = (View) parent;
4458
4459 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4460
4461 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 parent = child.getParent();
4463 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 return scrolled;
4466 }
4467
4468 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004469 * Called when this view wants to give up focus. If focus is cleared
4470 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4471 * <p>
4472 * <strong>Note:</strong> When a View clears focus the framework is trying
4473 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004474 * View is the first from the top that can take focus, then all callbacks
4475 * related to clearing focus will be invoked after wich the framework will
4476 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004477 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 */
4479 public void clearFocus() {
4480 if (DBG) {
4481 System.out.println(this + " clearFocus()");
4482 }
4483
Dianne Hackborn4702a852012-08-17 15:18:29 -07004484 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4485 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486
4487 if (mParent != null) {
4488 mParent.clearChildFocus(this);
4489 }
4490
4491 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004494
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004495 if (!rootViewRequestFocus()) {
4496 notifyGlobalFocusCleared(this);
4497 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004498
4499 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4500 notifyAccessibilityStateChanged();
4501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 }
4503 }
4504
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004505 void notifyGlobalFocusCleared(View oldFocus) {
4506 if (oldFocus != null && mAttachInfo != null) {
4507 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
4508 }
4509 }
4510
4511 boolean rootViewRequestFocus() {
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004512 View root = getRootView();
4513 if (root != null) {
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004514 return root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08004516 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 }
4518
4519 /**
4520 * Called internally by the view system when a new view is getting focus.
4521 * This is what clears the old focus.
4522 */
4523 void unFocus() {
4524 if (DBG) {
4525 System.out.println(this + " unFocus()");
4526 }
4527
Dianne Hackborn4702a852012-08-17 15:18:29 -07004528 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4529 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530
4531 onFocusChanged(false, 0, null);
4532 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004533
4534 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4535 notifyAccessibilityStateChanged();
4536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 }
4538 }
4539
4540 /**
4541 * Returns true if this view has focus iteself, or is the ancestor of the
4542 * view that has focus.
4543 *
4544 * @return True if this view has or contains focus, false otherwise.
4545 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004546 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004548 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 }
4550
4551 /**
4552 * Returns true if this view is focusable or if it contains a reachable View
4553 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4554 * is a View whose parents do not block descendants focus.
4555 *
4556 * Only {@link #VISIBLE} views are considered focusable.
4557 *
4558 * @return True if the view is focusable or if the view contains a focusable
4559 * View, false otherwise.
4560 *
4561 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4562 */
4563 public boolean hasFocusable() {
4564 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4565 }
4566
4567 /**
4568 * Called by the view system when the focus state of this view changes.
4569 * When the focus change event is caused by directional navigation, direction
4570 * and previouslyFocusedRect provide insight into where the focus is coming from.
4571 * When overriding, be sure to call up through to the super class so that
4572 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 * @param gainFocus True if the View has focus; false otherwise.
4575 * @param direction The direction focus has moved when requestFocus()
4576 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004577 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4578 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4579 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4581 * system, of the previously focused view. If applicable, this will be
4582 * passed in as finer grained information about where the focus is coming
4583 * from (in addition to direction). Will be <code>null</code> otherwise.
4584 */
4585 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004586 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004587 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4588 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004589 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004590 }
4591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 InputMethodManager imm = InputMethodManager.peekInstance();
4593 if (!gainFocus) {
4594 if (isPressed()) {
4595 setPressed(false);
4596 }
4597 if (imm != null && mAttachInfo != null
4598 && mAttachInfo.mHasWindowFocus) {
4599 imm.focusOut(this);
4600 }
Romain Guya2431d02009-04-30 16:30:00 -07004601 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 } else if (imm != null && mAttachInfo != null
4603 && mAttachInfo.mHasWindowFocus) {
4604 imm.focusIn(this);
4605 }
Romain Guy8506ab42009-06-11 17:35:47 -07004606
Romain Guy0fd89bf2011-01-26 15:41:30 -08004607 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004608 ListenerInfo li = mListenerInfo;
4609 if (li != null && li.mOnFocusChangeListener != null) {
4610 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 }
Joe Malin32736f02011-01-19 16:14:20 -08004612
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004613 if (mAttachInfo != null) {
4614 mAttachInfo.mKeyDispatchState.reset(this);
4615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 }
4617
4618 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004619 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004620 * not enabled this method has no effect. The default implementation calls
4621 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4622 * to populate information about the event source (this View), then calls
4623 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4624 * populate the text content of the event source including its descendants,
4625 * and last calls
4626 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4627 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004628 * <p>
4629 * If an {@link AccessibilityDelegate} has been specified via calling
4630 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4631 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4632 * responsible for handling this call.
4633 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004634 *
Scott Mainb303d832011-10-12 16:45:18 -07004635 * @param eventType The type of the event to send, as defined by several types from
4636 * {@link android.view.accessibility.AccessibilityEvent}, such as
4637 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4638 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004639 *
4640 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4641 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4642 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004643 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004644 */
4645 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004646 if (mAccessibilityDelegate != null) {
4647 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4648 } else {
4649 sendAccessibilityEventInternal(eventType);
4650 }
4651 }
4652
4653 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004654 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4655 * {@link AccessibilityEvent} to make an announcement which is related to some
4656 * sort of a context change for which none of the events representing UI transitions
4657 * is a good fit. For example, announcing a new page in a book. If accessibility
4658 * is not enabled this method does nothing.
4659 *
4660 * @param text The announcement text.
4661 */
4662 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004663 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004664 AccessibilityEvent event = AccessibilityEvent.obtain(
4665 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004666 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004667 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004668 event.setContentDescription(null);
4669 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004670 }
4671 }
4672
4673 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004674 * @see #sendAccessibilityEvent(int)
4675 *
4676 * Note: Called from the default {@link AccessibilityDelegate}.
4677 */
4678 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004679 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4680 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4681 }
4682 }
4683
4684 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004685 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4686 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004687 * perform a check whether accessibility is enabled.
4688 * <p>
4689 * If an {@link AccessibilityDelegate} has been specified via calling
4690 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4691 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4692 * is responsible for handling this call.
4693 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004694 *
4695 * @param event The event to send.
4696 *
4697 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004698 */
4699 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004700 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004701 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004702 } else {
4703 sendAccessibilityEventUncheckedInternal(event);
4704 }
4705 }
4706
4707 /**
4708 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4709 *
4710 * Note: Called from the default {@link AccessibilityDelegate}.
4711 */
4712 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004713 if (!isShown()) {
4714 return;
4715 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004716 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004717 // Only a subset of accessibility events populates text content.
4718 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4719 dispatchPopulateAccessibilityEvent(event);
4720 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004721 // In the beginning we called #isShown(), so we know that getParent() is not null.
4722 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004723 }
4724
4725 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004726 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4727 * to its children for adding their text content to the event. Note that the
4728 * event text is populated in a separate dispatch path since we add to the
4729 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004730 * A typical implementation will call
4731 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4732 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4733 * on each child. Override this method if custom population of the event text
4734 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004735 * <p>
4736 * If an {@link AccessibilityDelegate} has been specified via calling
4737 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4738 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4739 * is responsible for handling this call.
4740 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004741 * <p>
4742 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4743 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4744 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004745 *
4746 * @param event The event.
4747 *
4748 * @return True if the event population was completed.
4749 */
4750 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004751 if (mAccessibilityDelegate != null) {
4752 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4753 } else {
4754 return dispatchPopulateAccessibilityEventInternal(event);
4755 }
4756 }
4757
4758 /**
4759 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4760 *
4761 * Note: Called from the default {@link AccessibilityDelegate}.
4762 */
4763 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004764 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004765 return false;
4766 }
4767
4768 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004769 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004770 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004771 * text content. While this method is free to modify event
4772 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004773 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4774 * <p>
4775 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004776 * to the text added by the super implementation:
4777 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004778 * super.onPopulateAccessibilityEvent(event);
4779 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4780 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4781 * mCurrentDate.getTimeInMillis(), flags);
4782 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004783 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004784 * <p>
4785 * If an {@link AccessibilityDelegate} has been specified via calling
4786 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4787 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4788 * is responsible for handling this call.
4789 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004790 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4791 * information to the event, in case the default implementation has basic information to add.
4792 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004793 *
4794 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004795 *
4796 * @see #sendAccessibilityEvent(int)
4797 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004798 */
4799 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004800 if (mAccessibilityDelegate != null) {
4801 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4802 } else {
4803 onPopulateAccessibilityEventInternal(event);
4804 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004805 }
4806
4807 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004808 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4809 *
4810 * Note: Called from the default {@link AccessibilityDelegate}.
4811 */
4812 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4813
4814 }
4815
4816 /**
4817 * Initializes an {@link AccessibilityEvent} with information about
4818 * this View which is the event source. In other words, the source of
4819 * an accessibility event is the view whose state change triggered firing
4820 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004821 * <p>
4822 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004823 * to properties set by the super implementation:
4824 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4825 * super.onInitializeAccessibilityEvent(event);
4826 * event.setPassword(true);
4827 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004828 * <p>
4829 * If an {@link AccessibilityDelegate} has been specified via calling
4830 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4831 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4832 * is responsible for handling this call.
4833 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004834 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4835 * information to the event, in case the default implementation has basic information to add.
4836 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004837 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004838 *
4839 * @see #sendAccessibilityEvent(int)
4840 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4841 */
4842 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004843 if (mAccessibilityDelegate != null) {
4844 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4845 } else {
4846 onInitializeAccessibilityEventInternal(event);
4847 }
4848 }
4849
4850 /**
4851 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4852 *
4853 * Note: Called from the default {@link AccessibilityDelegate}.
4854 */
4855 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004856 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004857 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004858 event.setPackageName(getContext().getPackageName());
4859 event.setEnabled(isEnabled());
4860 event.setContentDescription(mContentDescription);
4861
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004862 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004863 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004864 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4865 FOCUSABLES_ALL);
4866 event.setItemCount(focusablesTempList.size());
4867 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4868 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004869 }
4870 }
4871
4872 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004873 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4874 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4875 * This method is responsible for obtaining an accessibility node info from a
4876 * pool of reusable instances and calling
4877 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4878 * initialize the former.
4879 * <p>
4880 * Note: The client is responsible for recycling the obtained instance by calling
4881 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4882 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004883 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004884 * @return A populated {@link AccessibilityNodeInfo}.
4885 *
4886 * @see AccessibilityNodeInfo
4887 */
4888 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004889 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4890 if (provider != null) {
4891 return provider.createAccessibilityNodeInfo(View.NO_ID);
4892 } else {
4893 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4894 onInitializeAccessibilityNodeInfo(info);
4895 return info;
4896 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004897 }
4898
4899 /**
4900 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4901 * The base implementation sets:
4902 * <ul>
4903 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004904 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4905 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004906 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4907 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4908 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4909 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4910 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4911 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4912 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4913 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4914 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4915 * </ul>
4916 * <p>
4917 * Subclasses should override this method, call the super implementation,
4918 * and set additional attributes.
4919 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004920 * <p>
4921 * If an {@link AccessibilityDelegate} has been specified via calling
4922 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4923 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4924 * is responsible for handling this call.
4925 * </p>
4926 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004927 * @param info The instance to initialize.
4928 */
4929 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004930 if (mAccessibilityDelegate != null) {
4931 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4932 } else {
4933 onInitializeAccessibilityNodeInfoInternal(info);
4934 }
4935 }
4936
4937 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004938 * Gets the location of this view in screen coordintates.
4939 *
4940 * @param outRect The output location
4941 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004942 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004943 if (mAttachInfo == null) {
4944 return;
4945 }
4946
4947 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004948 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004949
4950 if (!hasIdentityMatrix()) {
4951 getMatrix().mapRect(position);
4952 }
4953
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004954 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004955
4956 ViewParent parent = mParent;
4957 while (parent instanceof View) {
4958 View parentView = (View) parent;
4959
4960 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4961
4962 if (!parentView.hasIdentityMatrix()) {
4963 parentView.getMatrix().mapRect(position);
4964 }
4965
4966 position.offset(parentView.mLeft, parentView.mTop);
4967
4968 parent = parentView.mParent;
4969 }
4970
4971 if (parent instanceof ViewRootImpl) {
4972 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4973 position.offset(0, -viewRootImpl.mCurScrollY);
4974 }
4975
4976 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4977
4978 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4979 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4980 }
4981
4982 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004983 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4984 *
4985 * Note: Called from the default {@link AccessibilityDelegate}.
4986 */
4987 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004988 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004989
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004990 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004991 info.setBoundsInParent(bounds);
4992
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004993 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004994 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004995
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004996 ViewParent parent = getParentForAccessibility();
4997 if (parent instanceof View) {
4998 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004999 }
5000
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005001 if (mID != View.NO_ID) {
5002 View rootView = getRootView();
5003 if (rootView == null) {
5004 rootView = this;
5005 }
5006 View label = rootView.findLabelForView(this, mID);
5007 if (label != null) {
5008 info.setLabeledBy(label);
5009 }
5010 }
5011
5012 if (mLabelForId != View.NO_ID) {
5013 View rootView = getRootView();
5014 if (rootView == null) {
5015 rootView = this;
5016 }
5017 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
5018 if (labeled != null) {
5019 info.setLabelFor(labeled);
5020 }
5021 }
5022
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005023 info.setVisibleToUser(isVisibleToUser());
5024
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005025 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005026 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005027 info.setContentDescription(getContentDescription());
5028
5029 info.setEnabled(isEnabled());
5030 info.setClickable(isClickable());
5031 info.setFocusable(isFocusable());
5032 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005033 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005034 info.setSelected(isSelected());
5035 info.setLongClickable(isLongClickable());
5036
5037 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005038 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005039 // we should report as selectable view in an AdapterView.
5040 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5041 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5042
5043 if (isFocusable()) {
5044 if (isFocused()) {
5045 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5046 } else {
5047 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5048 }
5049 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005050
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005051 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005052 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005053 } else {
5054 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5055 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005056
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005057 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005058 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5059 }
5060
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005061 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005062 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5063 }
5064
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005065 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005066 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5067 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5068 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005069 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5070 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005071 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005072 }
5073
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005074 private View findLabelForView(View view, int labeledId) {
5075 if (mMatchLabelForPredicate == null) {
5076 mMatchLabelForPredicate = new MatchLabelForPredicate();
5077 }
5078 mMatchLabelForPredicate.mLabeledId = labeledId;
5079 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5080 }
5081
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005082 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005083 * Computes whether this view is visible to the user. Such a view is
5084 * attached, visible, all its predecessors are visible, it is not clipped
5085 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005086 *
5087 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005088 *
5089 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005090 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005091 protected boolean isVisibleToUser() {
5092 return isVisibleToUser(null);
5093 }
5094
5095 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005096 * Computes whether the given portion of this view is visible to the user.
5097 * Such a view is attached, visible, all its predecessors are visible,
5098 * has an alpha greater than zero, and the specified portion is not
5099 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005100 *
5101 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5102 * <code>null</code>, and the entire view will be tested in this case.
5103 * When <code>true</code> is returned by the function, the actual visible
5104 * region will be stored in this parameter; that is, if boundInView is fully
5105 * contained within the view, no modification will be made, otherwise regions
5106 * outside of the visible area of the view will be clipped.
5107 *
5108 * @return Whether the specified portion of the view is visible on the screen.
5109 *
5110 * @hide
5111 */
5112 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005113 if (mAttachInfo != null) {
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005114 // Attached to invisible window means this view is not visible.
5115 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
5116 return false;
5117 }
5118 // An invisible predecessor or one with alpha zero means
5119 // that this view is not visible to the user.
5120 Object current = this;
5121 while (current instanceof View) {
5122 View view = (View) current;
5123 // We have attach info so this view is attached and there is no
5124 // need to check whether we reach to ViewRootImpl on the way up.
5125 if (view.getAlpha() <= 0 || view.getVisibility() != VISIBLE) {
5126 return false;
5127 }
5128 current = view.mParent;
5129 }
5130 // Check if the view is entirely covered by its predecessors.
Romain Guyf0af1d52012-07-11 18:31:21 -07005131 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5132 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005133 if (!getGlobalVisibleRect(visibleRect, offset)) {
5134 return false;
Guang Zhu0d607fb2012-05-11 19:34:56 -07005135 }
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005136 // Check if the visible portion intersects the rectangle of interest.
5137 if (boundInView != null) {
5138 visibleRect.offset(-offset.x, -offset.y);
5139 return boundInView.intersect(visibleRect);
5140 }
5141 return true;
Romain Guyf0af1d52012-07-11 18:31:21 -07005142 }
Romain Guyf0af1d52012-07-11 18:31:21 -07005143 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005144 }
5145
5146 /**
alanvb72fe7a2012-08-27 16:44:25 -07005147 * Returns the delegate for implementing accessibility support via
5148 * composition. For more details see {@link AccessibilityDelegate}.
5149 *
5150 * @return The delegate, or null if none set.
5151 *
5152 * @hide
5153 */
5154 public AccessibilityDelegate getAccessibilityDelegate() {
5155 return mAccessibilityDelegate;
5156 }
5157
5158 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005159 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005160 * opposed to inheritance. The delegate's primary use is for implementing
5161 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5162 *
5163 * @param delegate The delegate instance.
5164 *
5165 * @see AccessibilityDelegate
5166 */
5167 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5168 mAccessibilityDelegate = delegate;
5169 }
5170
5171 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005172 * Gets the provider for managing a virtual view hierarchy rooted at this View
5173 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5174 * that explore the window content.
5175 * <p>
5176 * If this method returns an instance, this instance is responsible for managing
5177 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5178 * View including the one representing the View itself. Similarly the returned
5179 * instance is responsible for performing accessibility actions on any virtual
5180 * view or the root view itself.
5181 * </p>
5182 * <p>
5183 * If an {@link AccessibilityDelegate} has been specified via calling
5184 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5185 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5186 * is responsible for handling this call.
5187 * </p>
5188 *
5189 * @return The provider.
5190 *
5191 * @see AccessibilityNodeProvider
5192 */
5193 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5194 if (mAccessibilityDelegate != null) {
5195 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5196 } else {
5197 return null;
5198 }
5199 }
5200
5201 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005202 * Gets the unique identifier of this view on the screen for accessibility purposes.
5203 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5204 *
5205 * @return The view accessibility id.
5206 *
5207 * @hide
5208 */
5209 public int getAccessibilityViewId() {
5210 if (mAccessibilityViewId == NO_ID) {
5211 mAccessibilityViewId = sNextAccessibilityViewId++;
5212 }
5213 return mAccessibilityViewId;
5214 }
5215
5216 /**
5217 * Gets the unique identifier of the window in which this View reseides.
5218 *
5219 * @return The window accessibility id.
5220 *
5221 * @hide
5222 */
5223 public int getAccessibilityWindowId() {
5224 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5225 }
5226
5227 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005228 * Gets the {@link View} description. It briefly describes the view and is
5229 * primarily used for accessibility support. Set this property to enable
5230 * better accessibility support for your application. This is especially
5231 * true for views that do not have textual representation (For example,
5232 * ImageButton).
5233 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005234 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005235 *
5236 * @attr ref android.R.styleable#View_contentDescription
5237 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005238 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005239 public CharSequence getContentDescription() {
5240 return mContentDescription;
5241 }
5242
5243 /**
5244 * Sets the {@link View} description. It briefly describes the view and is
5245 * primarily used for accessibility support. Set this property to enable
5246 * better accessibility support for your application. This is especially
5247 * true for views that do not have textual representation (For example,
5248 * ImageButton).
5249 *
5250 * @param contentDescription The content description.
5251 *
5252 * @attr ref android.R.styleable#View_contentDescription
5253 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005254 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005255 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005256 if (mContentDescription == null) {
5257 if (contentDescription == null) {
5258 return;
5259 }
5260 } else if (mContentDescription.equals(contentDescription)) {
5261 return;
5262 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005263 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005264 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5265 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5266 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5267 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005268 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005269 }
5270
5271 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005272 * Gets the id of a view for which this view serves as a label for
5273 * accessibility purposes.
5274 *
5275 * @return The labeled view id.
5276 */
5277 @ViewDebug.ExportedProperty(category = "accessibility")
5278 public int getLabelFor() {
5279 return mLabelForId;
5280 }
5281
5282 /**
5283 * Sets the id of a view for which this view serves as a label for
5284 * accessibility purposes.
5285 *
5286 * @param id The labeled view id.
5287 */
5288 @RemotableViewMethod
5289 public void setLabelFor(int id) {
5290 mLabelForId = id;
5291 if (mLabelForId != View.NO_ID
5292 && mID == View.NO_ID) {
5293 mID = generateViewId();
5294 }
5295 }
5296
5297 /**
Romain Guya2431d02009-04-30 16:30:00 -07005298 * Invoked whenever this view loses focus, either by losing window focus or by losing
5299 * focus within its window. This method can be used to clear any state tied to the
5300 * focus. For instance, if a button is held pressed with the trackball and the window
5301 * loses focus, this method can be used to cancel the press.
5302 *
5303 * Subclasses of View overriding this method should always call super.onFocusLost().
5304 *
5305 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005306 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005307 *
5308 * @hide pending API council approval
5309 */
5310 protected void onFocusLost() {
5311 resetPressedState();
5312 }
5313
5314 private void resetPressedState() {
5315 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5316 return;
5317 }
5318
5319 if (isPressed()) {
5320 setPressed(false);
5321
5322 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005323 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005324 }
5325 }
5326 }
5327
5328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 * Returns true if this view has focus
5330 *
5331 * @return True if this view has focus, false otherwise.
5332 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005333 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005335 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 }
5337
5338 /**
5339 * Find the view in the hierarchy rooted at this view that currently has
5340 * focus.
5341 *
5342 * @return The view that currently has focus, or null if no focused view can
5343 * be found.
5344 */
5345 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005346 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 }
5348
5349 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005350 * Indicates whether this view is one of the set of scrollable containers in
5351 * its window.
5352 *
5353 * @return whether this view is one of the set of scrollable containers in
5354 * its window
5355 *
5356 * @attr ref android.R.styleable#View_isScrollContainer
5357 */
5358 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005359 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005360 }
5361
5362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 * Change whether this view is one of the set of scrollable containers in
5364 * its window. This will be used to determine whether the window can
5365 * resize or must pan when a soft input area is open -- scrollable
5366 * containers allow the window to use resize mode since the container
5367 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005368 *
5369 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 */
5371 public void setScrollContainer(boolean isScrollContainer) {
5372 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005373 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005375 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005377 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005379 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 mAttachInfo.mScrollContainers.remove(this);
5381 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005382 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 }
5384 }
5385
5386 /**
5387 * Returns the quality of the drawing cache.
5388 *
5389 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5390 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5391 *
5392 * @see #setDrawingCacheQuality(int)
5393 * @see #setDrawingCacheEnabled(boolean)
5394 * @see #isDrawingCacheEnabled()
5395 *
5396 * @attr ref android.R.styleable#View_drawingCacheQuality
5397 */
5398 public int getDrawingCacheQuality() {
5399 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5400 }
5401
5402 /**
5403 * Set the drawing cache quality of this view. This value is used only when the
5404 * drawing cache is enabled
5405 *
5406 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5407 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5408 *
5409 * @see #getDrawingCacheQuality()
5410 * @see #setDrawingCacheEnabled(boolean)
5411 * @see #isDrawingCacheEnabled()
5412 *
5413 * @attr ref android.R.styleable#View_drawingCacheQuality
5414 */
5415 public void setDrawingCacheQuality(int quality) {
5416 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5417 }
5418
5419 /**
5420 * Returns whether the screen should remain on, corresponding to the current
5421 * value of {@link #KEEP_SCREEN_ON}.
5422 *
5423 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5424 *
5425 * @see #setKeepScreenOn(boolean)
5426 *
5427 * @attr ref android.R.styleable#View_keepScreenOn
5428 */
5429 public boolean getKeepScreenOn() {
5430 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5431 }
5432
5433 /**
5434 * Controls whether the screen should remain on, modifying the
5435 * value of {@link #KEEP_SCREEN_ON}.
5436 *
5437 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5438 *
5439 * @see #getKeepScreenOn()
5440 *
5441 * @attr ref android.R.styleable#View_keepScreenOn
5442 */
5443 public void setKeepScreenOn(boolean keepScreenOn) {
5444 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5445 }
5446
5447 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005448 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5449 * @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 -08005450 *
5451 * @attr ref android.R.styleable#View_nextFocusLeft
5452 */
5453 public int getNextFocusLeftId() {
5454 return mNextFocusLeftId;
5455 }
5456
5457 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005458 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5459 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5460 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 *
5462 * @attr ref android.R.styleable#View_nextFocusLeft
5463 */
5464 public void setNextFocusLeftId(int nextFocusLeftId) {
5465 mNextFocusLeftId = nextFocusLeftId;
5466 }
5467
5468 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005469 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5470 * @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 -08005471 *
5472 * @attr ref android.R.styleable#View_nextFocusRight
5473 */
5474 public int getNextFocusRightId() {
5475 return mNextFocusRightId;
5476 }
5477
5478 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005479 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5480 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5481 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 *
5483 * @attr ref android.R.styleable#View_nextFocusRight
5484 */
5485 public void setNextFocusRightId(int nextFocusRightId) {
5486 mNextFocusRightId = nextFocusRightId;
5487 }
5488
5489 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005490 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5491 * @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 -08005492 *
5493 * @attr ref android.R.styleable#View_nextFocusUp
5494 */
5495 public int getNextFocusUpId() {
5496 return mNextFocusUpId;
5497 }
5498
5499 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005500 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5501 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5502 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 *
5504 * @attr ref android.R.styleable#View_nextFocusUp
5505 */
5506 public void setNextFocusUpId(int nextFocusUpId) {
5507 mNextFocusUpId = nextFocusUpId;
5508 }
5509
5510 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005511 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5512 * @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 -08005513 *
5514 * @attr ref android.R.styleable#View_nextFocusDown
5515 */
5516 public int getNextFocusDownId() {
5517 return mNextFocusDownId;
5518 }
5519
5520 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005521 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5522 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5523 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 *
5525 * @attr ref android.R.styleable#View_nextFocusDown
5526 */
5527 public void setNextFocusDownId(int nextFocusDownId) {
5528 mNextFocusDownId = nextFocusDownId;
5529 }
5530
5531 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005532 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5533 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5534 *
5535 * @attr ref android.R.styleable#View_nextFocusForward
5536 */
5537 public int getNextFocusForwardId() {
5538 return mNextFocusForwardId;
5539 }
5540
5541 /**
5542 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5543 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5544 * decide automatically.
5545 *
5546 * @attr ref android.R.styleable#View_nextFocusForward
5547 */
5548 public void setNextFocusForwardId(int nextFocusForwardId) {
5549 mNextFocusForwardId = nextFocusForwardId;
5550 }
5551
5552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 * Returns the visibility of this view and all of its ancestors
5554 *
5555 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5556 */
5557 public boolean isShown() {
5558 View current = this;
5559 //noinspection ConstantConditions
5560 do {
5561 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5562 return false;
5563 }
5564 ViewParent parent = current.mParent;
5565 if (parent == null) {
5566 return false; // We are not attached to the view root
5567 }
5568 if (!(parent instanceof View)) {
5569 return true;
5570 }
5571 current = (View) parent;
5572 } while (current != null);
5573
5574 return false;
5575 }
5576
5577 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005578 * Called by the view hierarchy when the content insets for a window have
5579 * changed, to allow it to adjust its content to fit within those windows.
5580 * The content insets tell you the space that the status bar, input method,
5581 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005583 * <p>You do not normally need to deal with this function, since the default
5584 * window decoration given to applications takes care of applying it to the
5585 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5586 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5587 * and your content can be placed under those system elements. You can then
5588 * use this method within your view hierarchy if you have parts of your UI
5589 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005591 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005592 * inset's to the view's padding, consuming that content (modifying the
5593 * insets to be 0), and returning true. This behavior is off by default, but can
5594 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5595 *
5596 * <p>This function's traversal down the hierarchy is depth-first. The same content
5597 * insets object is propagated down the hierarchy, so any changes made to it will
5598 * be seen by all following views (including potentially ones above in
5599 * the hierarchy since this is a depth-first traversal). The first view
5600 * that returns true will abort the entire traversal.
5601 *
5602 * <p>The default implementation works well for a situation where it is
5603 * used with a container that covers the entire window, allowing it to
5604 * apply the appropriate insets to its content on all edges. If you need
5605 * a more complicated layout (such as two different views fitting system
5606 * windows, one on the top of the window, and one on the bottom),
5607 * you can override the method and handle the insets however you would like.
5608 * Note that the insets provided by the framework are always relative to the
5609 * far edges of the window, not accounting for the location of the called view
5610 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005611 * where the layout will place the view, as it is done before layout happens.)
5612 *
5613 * <p>Note: unlike many View methods, there is no dispatch phase to this
5614 * call. If you are overriding it in a ViewGroup and want to allow the
5615 * call to continue to your children, you must be sure to call the super
5616 * implementation.
5617 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005618 * <p>Here is a sample layout that makes use of fitting system windows
5619 * to have controls for a video view placed inside of the window decorations
5620 * that it hides and shows. This can be used with code like the second
5621 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5622 *
5623 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5624 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005625 * @param insets Current content insets of the window. Prior to
5626 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5627 * the insets or else you and Android will be unhappy.
5628 *
5629 * @return Return true if this view applied the insets and it should not
5630 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005631 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005632 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005633 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 */
5635 protected boolean fitSystemWindows(Rect insets) {
5636 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005637 mUserPaddingStart = UNDEFINED_PADDING;
5638 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005639 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5640 || mAttachInfo == null
5641 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5642 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5643 return true;
5644 } else {
5645 internalSetPadding(0, 0, 0, 0);
5646 return false;
5647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 }
5649 return false;
5650 }
5651
5652 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005653 * Sets whether or not this view should account for system screen decorations
5654 * such as the status bar and inset its content; that is, controlling whether
5655 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5656 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005657 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005658 * <p>Note that if you are providing your own implementation of
5659 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5660 * flag to true -- your implementation will be overriding the default
5661 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005662 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005663 * @param fitSystemWindows If true, then the default implementation of
5664 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005665 *
5666 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005667 * @see #getFitsSystemWindows()
5668 * @see #fitSystemWindows(Rect)
5669 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005670 */
5671 public void setFitsSystemWindows(boolean fitSystemWindows) {
5672 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5673 }
5674
5675 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005676 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005677 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5678 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005679 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005680 * @return Returns true if the default implementation of
5681 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005682 *
5683 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005684 * @see #setFitsSystemWindows()
5685 * @see #fitSystemWindows(Rect)
5686 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005687 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005688 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005689 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5690 }
5691
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005692 /** @hide */
5693 public boolean fitsSystemWindows() {
5694 return getFitsSystemWindows();
5695 }
5696
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005697 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005698 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5699 */
5700 public void requestFitSystemWindows() {
5701 if (mParent != null) {
5702 mParent.requestFitSystemWindows();
5703 }
5704 }
5705
5706 /**
5707 * For use by PhoneWindow to make its own system window fitting optional.
5708 * @hide
5709 */
5710 public void makeOptionalFitsSystemWindows() {
5711 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5712 }
5713
5714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 * Returns the visibility status for this view.
5716 *
5717 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5718 * @attr ref android.R.styleable#View_visibility
5719 */
5720 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005721 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5722 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5723 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 })
5725 public int getVisibility() {
5726 return mViewFlags & VISIBILITY_MASK;
5727 }
5728
5729 /**
5730 * Set the enabled state of this view.
5731 *
5732 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5733 * @attr ref android.R.styleable#View_visibility
5734 */
5735 @RemotableViewMethod
5736 public void setVisibility(int visibility) {
5737 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005738 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 }
5740
5741 /**
5742 * Returns the enabled status for this view. The interpretation of the
5743 * enabled state varies by subclass.
5744 *
5745 * @return True if this view is enabled, false otherwise.
5746 */
5747 @ViewDebug.ExportedProperty
5748 public boolean isEnabled() {
5749 return (mViewFlags & ENABLED_MASK) == ENABLED;
5750 }
5751
5752 /**
5753 * Set the enabled state of this view. The interpretation of the enabled
5754 * state varies by subclass.
5755 *
5756 * @param enabled True if this view is enabled, false otherwise.
5757 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005758 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005760 if (enabled == isEnabled()) return;
5761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5763
5764 /*
5765 * The View most likely has to change its appearance, so refresh
5766 * the drawable state.
5767 */
5768 refreshDrawableState();
5769
5770 // Invalidate too, since the default behavior for views is to be
5771 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005772 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 }
5774
5775 /**
5776 * Set whether this view can receive the focus.
5777 *
5778 * Setting this to false will also ensure that this view is not focusable
5779 * in touch mode.
5780 *
5781 * @param focusable If true, this view can receive the focus.
5782 *
5783 * @see #setFocusableInTouchMode(boolean)
5784 * @attr ref android.R.styleable#View_focusable
5785 */
5786 public void setFocusable(boolean focusable) {
5787 if (!focusable) {
5788 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5789 }
5790 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5791 }
5792
5793 /**
5794 * Set whether this view can receive focus while in touch mode.
5795 *
5796 * Setting this to true will also ensure that this view is focusable.
5797 *
5798 * @param focusableInTouchMode If true, this view can receive the focus while
5799 * in touch mode.
5800 *
5801 * @see #setFocusable(boolean)
5802 * @attr ref android.R.styleable#View_focusableInTouchMode
5803 */
5804 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5805 // Focusable in touch mode should always be set before the focusable flag
5806 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5807 // which, in touch mode, will not successfully request focus on this view
5808 // because the focusable in touch mode flag is not set
5809 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5810 if (focusableInTouchMode) {
5811 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5812 }
5813 }
5814
5815 /**
5816 * Set whether this view should have sound effects enabled for events such as
5817 * clicking and touching.
5818 *
5819 * <p>You may wish to disable sound effects for a view if you already play sounds,
5820 * for instance, a dial key that plays dtmf tones.
5821 *
5822 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5823 * @see #isSoundEffectsEnabled()
5824 * @see #playSoundEffect(int)
5825 * @attr ref android.R.styleable#View_soundEffectsEnabled
5826 */
5827 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5828 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5829 }
5830
5831 /**
5832 * @return whether this view should have sound effects enabled for events such as
5833 * clicking and touching.
5834 *
5835 * @see #setSoundEffectsEnabled(boolean)
5836 * @see #playSoundEffect(int)
5837 * @attr ref android.R.styleable#View_soundEffectsEnabled
5838 */
5839 @ViewDebug.ExportedProperty
5840 public boolean isSoundEffectsEnabled() {
5841 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5842 }
5843
5844 /**
5845 * Set whether this view should have haptic feedback for events such as
5846 * long presses.
5847 *
5848 * <p>You may wish to disable haptic feedback if your view already controls
5849 * its own haptic feedback.
5850 *
5851 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5852 * @see #isHapticFeedbackEnabled()
5853 * @see #performHapticFeedback(int)
5854 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5855 */
5856 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5857 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5858 }
5859
5860 /**
5861 * @return whether this view should have haptic feedback enabled for events
5862 * long presses.
5863 *
5864 * @see #setHapticFeedbackEnabled(boolean)
5865 * @see #performHapticFeedback(int)
5866 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5867 */
5868 @ViewDebug.ExportedProperty
5869 public boolean isHapticFeedbackEnabled() {
5870 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5871 }
5872
5873 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005874 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005875 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005876 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5877 * {@link #LAYOUT_DIRECTION_RTL},
5878 * {@link #LAYOUT_DIRECTION_INHERIT} or
5879 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005880 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005881 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005882 *
5883 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005884 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005885 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005886 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5887 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5888 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5889 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005890 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005891 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005892 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005893 }
5894
5895 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005896 * Set the layout direction for this view. This will propagate a reset of layout direction
5897 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005898 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005899 * @param layoutDirection the layout direction to set. Should be one of:
5900 *
5901 * {@link #LAYOUT_DIRECTION_LTR},
5902 * {@link #LAYOUT_DIRECTION_RTL},
5903 * {@link #LAYOUT_DIRECTION_INHERIT},
5904 * {@link #LAYOUT_DIRECTION_LOCALE}.
5905 *
5906 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5907 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5908 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005909 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005910 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005911 */
5912 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005913 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005914 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005915 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005916 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005917 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005918 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005919 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005920 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005921 // We need to resolve all RTL properties as they all depend on layout direction
5922 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005923 requestLayout();
5924 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005925 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005926 }
5927
5928 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005929 * Returns the resolved layout direction for this view.
5930 *
5931 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005932 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005933 *
5934 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5935 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005936 *
5937 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005938 */
5939 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005940 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5941 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005942 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005943 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005944 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5945 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005946 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005947 return LAYOUT_DIRECTION_LTR;
5948 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005949 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5950 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005951 }
5952
5953 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005954 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5955 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005956 *
5957 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005958 *
5959 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005960 */
5961 @ViewDebug.ExportedProperty(category = "layout")
5962 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005963 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005964 }
5965
5966 /**
Adam Powell539ee872012-02-03 19:00:49 -08005967 * Indicates whether the view is currently tracking transient state that the
5968 * app should not need to concern itself with saving and restoring, but that
5969 * the framework should take special note to preserve when possible.
5970 *
Adam Powell785c4472012-05-02 21:25:39 -07005971 * <p>A view with transient state cannot be trivially rebound from an external
5972 * data source, such as an adapter binding item views in a list. This may be
5973 * because the view is performing an animation, tracking user selection
5974 * of content, or similar.</p>
5975 *
Adam Powell539ee872012-02-03 19:00:49 -08005976 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005977 */
5978 @ViewDebug.ExportedProperty(category = "layout")
5979 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005980 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005981 }
5982
5983 /**
5984 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005985 * framework should attempt to preserve when possible. This flag is reference counted,
5986 * so every call to setHasTransientState(true) should be paired with a later call
5987 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005988 *
Adam Powell785c4472012-05-02 21:25:39 -07005989 * <p>A view with transient state cannot be trivially rebound from an external
5990 * data source, such as an adapter binding item views in a list. This may be
5991 * because the view is performing an animation, tracking user selection
5992 * of content, or similar.</p>
5993 *
Adam Powell539ee872012-02-03 19:00:49 -08005994 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005995 */
5996 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005997 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5998 mTransientStateCount - 1;
5999 if (mTransientStateCount < 0) {
6000 mTransientStateCount = 0;
6001 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
6002 "unmatched pair of setHasTransientState calls");
6003 }
6004 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07006005 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07006006 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07006007 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
6008 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07006009 if (mParent != null) {
6010 try {
6011 mParent.childHasTransientStateChanged(this, hasTransientState);
6012 } catch (AbstractMethodError e) {
6013 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
6014 " does not fully implement ViewParent", e);
6015 }
Adam Powell539ee872012-02-03 19:00:49 -08006016 }
6017 }
6018 }
6019
6020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 * If this view doesn't do any drawing on its own, set this flag to
6022 * allow further optimizations. By default, this flag is not set on
6023 * View, but could be set on some View subclasses such as ViewGroup.
6024 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006025 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
6026 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 *
6028 * @param willNotDraw whether or not this View draw on its own
6029 */
6030 public void setWillNotDraw(boolean willNotDraw) {
6031 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
6032 }
6033
6034 /**
6035 * Returns whether or not this View draws on its own.
6036 *
6037 * @return true if this view has nothing to draw, false otherwise
6038 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006039 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 public boolean willNotDraw() {
6041 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6042 }
6043
6044 /**
6045 * When a View's drawing cache is enabled, drawing is redirected to an
6046 * offscreen bitmap. Some views, like an ImageView, must be able to
6047 * bypass this mechanism if they already draw a single bitmap, to avoid
6048 * unnecessary usage of the memory.
6049 *
6050 * @param willNotCacheDrawing true if this view does not cache its
6051 * drawing, false otherwise
6052 */
6053 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6054 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6055 }
6056
6057 /**
6058 * Returns whether or not this View can cache its drawing or not.
6059 *
6060 * @return true if this view does not cache its drawing, false otherwise
6061 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006062 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 public boolean willNotCacheDrawing() {
6064 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6065 }
6066
6067 /**
6068 * Indicates whether this view reacts to click events or not.
6069 *
6070 * @return true if the view is clickable, false otherwise
6071 *
6072 * @see #setClickable(boolean)
6073 * @attr ref android.R.styleable#View_clickable
6074 */
6075 @ViewDebug.ExportedProperty
6076 public boolean isClickable() {
6077 return (mViewFlags & CLICKABLE) == CLICKABLE;
6078 }
6079
6080 /**
6081 * Enables or disables click events for this view. When a view
6082 * is clickable it will change its state to "pressed" on every click.
6083 * Subclasses should set the view clickable to visually react to
6084 * user's clicks.
6085 *
6086 * @param clickable true to make the view clickable, false otherwise
6087 *
6088 * @see #isClickable()
6089 * @attr ref android.R.styleable#View_clickable
6090 */
6091 public void setClickable(boolean clickable) {
6092 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6093 }
6094
6095 /**
6096 * Indicates whether this view reacts to long click events or not.
6097 *
6098 * @return true if the view is long clickable, false otherwise
6099 *
6100 * @see #setLongClickable(boolean)
6101 * @attr ref android.R.styleable#View_longClickable
6102 */
6103 public boolean isLongClickable() {
6104 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6105 }
6106
6107 /**
6108 * Enables or disables long click events for this view. When a view is long
6109 * clickable it reacts to the user holding down the button for a longer
6110 * duration than a tap. This event can either launch the listener or a
6111 * context menu.
6112 *
6113 * @param longClickable true to make the view long clickable, false otherwise
6114 * @see #isLongClickable()
6115 * @attr ref android.R.styleable#View_longClickable
6116 */
6117 public void setLongClickable(boolean longClickable) {
6118 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6119 }
6120
6121 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006122 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006123 *
6124 * @see #isClickable()
6125 * @see #setClickable(boolean)
6126 *
6127 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6128 * the View's internal state from a previously set "pressed" state.
6129 */
6130 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006131 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006134 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006136 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006138
6139 if (needsRefresh) {
6140 refreshDrawableState();
6141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 dispatchSetPressed(pressed);
6143 }
6144
6145 /**
6146 * Dispatch setPressed to all of this View's children.
6147 *
6148 * @see #setPressed(boolean)
6149 *
6150 * @param pressed The new pressed state
6151 */
6152 protected void dispatchSetPressed(boolean pressed) {
6153 }
6154
6155 /**
6156 * Indicates whether the view is currently in pressed state. Unless
6157 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6158 * the pressed state.
6159 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006160 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161 * @see #isClickable()
6162 * @see #setClickable(boolean)
6163 *
6164 * @return true if the view is currently pressed, false otherwise
6165 */
6166 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006167 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 }
6169
6170 /**
6171 * Indicates whether this view will save its state (that is,
6172 * whether its {@link #onSaveInstanceState} method will be called).
6173 *
6174 * @return Returns true if the view state saving is enabled, else false.
6175 *
6176 * @see #setSaveEnabled(boolean)
6177 * @attr ref android.R.styleable#View_saveEnabled
6178 */
6179 public boolean isSaveEnabled() {
6180 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6181 }
6182
6183 /**
6184 * Controls whether the saving of this view's state is
6185 * enabled (that is, whether its {@link #onSaveInstanceState} method
6186 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006187 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 * for its state to be saved. This flag can only disable the
6189 * saving of this view; any child views may still have their state saved.
6190 *
6191 * @param enabled Set to false to <em>disable</em> state saving, or true
6192 * (the default) to allow it.
6193 *
6194 * @see #isSaveEnabled()
6195 * @see #setId(int)
6196 * @see #onSaveInstanceState()
6197 * @attr ref android.R.styleable#View_saveEnabled
6198 */
6199 public void setSaveEnabled(boolean enabled) {
6200 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6201 }
6202
Jeff Brown85a31762010-09-01 17:01:00 -07006203 /**
6204 * Gets 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 * @return True if touch filtering is enabled.
6209 *
6210 * @see #setFilterTouchesWhenObscured(boolean)
6211 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6212 */
6213 @ViewDebug.ExportedProperty
6214 public boolean getFilterTouchesWhenObscured() {
6215 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6216 }
6217
6218 /**
6219 * Sets whether the framework should discard touches when the view's
6220 * window is obscured by another visible window.
6221 * Refer to the {@link View} security documentation for more details.
6222 *
6223 * @param enabled True if touch filtering should be enabled.
6224 *
6225 * @see #getFilterTouchesWhenObscured
6226 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6227 */
6228 public void setFilterTouchesWhenObscured(boolean enabled) {
6229 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6230 FILTER_TOUCHES_WHEN_OBSCURED);
6231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232
6233 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006234 * Indicates whether the entire hierarchy under this view will save its
6235 * state when a state saving traversal occurs from its parent. The default
6236 * is true; if false, these views will not be saved unless
6237 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6238 *
6239 * @return Returns true if the view state saving from parent is enabled, else false.
6240 *
6241 * @see #setSaveFromParentEnabled(boolean)
6242 */
6243 public boolean isSaveFromParentEnabled() {
6244 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6245 }
6246
6247 /**
6248 * Controls whether the entire hierarchy under this view will save its
6249 * state when a state saving traversal occurs from its parent. The default
6250 * is true; if false, these views will not be saved unless
6251 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6252 *
6253 * @param enabled Set to false to <em>disable</em> state saving, or true
6254 * (the default) to allow it.
6255 *
6256 * @see #isSaveFromParentEnabled()
6257 * @see #setId(int)
6258 * @see #onSaveInstanceState()
6259 */
6260 public void setSaveFromParentEnabled(boolean enabled) {
6261 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6262 }
6263
6264
6265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 * Returns whether this View is able to take focus.
6267 *
6268 * @return True if this view can take focus, or false otherwise.
6269 * @attr ref android.R.styleable#View_focusable
6270 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006271 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 public final boolean isFocusable() {
6273 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6274 }
6275
6276 /**
6277 * When a view is focusable, it may not want to take focus when in touch mode.
6278 * For example, a button would like focus when the user is navigating via a D-pad
6279 * so that the user can click on it, but once the user starts touching the screen,
6280 * the button shouldn't take focus
6281 * @return Whether the view is focusable in touch mode.
6282 * @attr ref android.R.styleable#View_focusableInTouchMode
6283 */
6284 @ViewDebug.ExportedProperty
6285 public final boolean isFocusableInTouchMode() {
6286 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6287 }
6288
6289 /**
6290 * Find the nearest view in the specified direction that can take focus.
6291 * This does not actually give focus to that view.
6292 *
6293 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6294 *
6295 * @return The nearest focusable in the specified direction, or null if none
6296 * can be found.
6297 */
6298 public View focusSearch(int direction) {
6299 if (mParent != null) {
6300 return mParent.focusSearch(this, direction);
6301 } else {
6302 return null;
6303 }
6304 }
6305
6306 /**
6307 * This method is the last chance for the focused view and its ancestors to
6308 * respond to an arrow key. This is called when the focused view did not
6309 * consume the key internally, nor could the view system find a new view in
6310 * the requested direction to give focus to.
6311 *
6312 * @param focused The currently focused view.
6313 * @param direction The direction focus wants to move. One of FOCUS_UP,
6314 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6315 * @return True if the this view consumed this unhandled move.
6316 */
6317 public boolean dispatchUnhandledMove(View focused, int direction) {
6318 return false;
6319 }
6320
6321 /**
6322 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006323 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006325 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6326 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 * @return The user specified next view, or null if there is none.
6328 */
6329 View findUserSetNextFocus(View root, int direction) {
6330 switch (direction) {
6331 case FOCUS_LEFT:
6332 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006333 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 case FOCUS_RIGHT:
6335 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006336 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 case FOCUS_UP:
6338 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006339 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 case FOCUS_DOWN:
6341 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006342 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006343 case FOCUS_FORWARD:
6344 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006345 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006346 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006347 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006348 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006349 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006350 @Override
6351 public boolean apply(View t) {
6352 return t.mNextFocusForwardId == id;
6353 }
6354 });
6355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 }
6357 return null;
6358 }
6359
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006360 private View findViewInsideOutShouldExist(View root, int id) {
6361 if (mMatchIdPredicate == null) {
6362 mMatchIdPredicate = new MatchIdPredicate();
6363 }
6364 mMatchIdPredicate.mId = id;
6365 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006367 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 }
6369 return result;
6370 }
6371
6372 /**
6373 * Find and return all focusable views that are descendants of this view,
6374 * possibly including this view if it is focusable itself.
6375 *
6376 * @param direction The direction of the focus
6377 * @return A list of focusable views
6378 */
6379 public ArrayList<View> getFocusables(int direction) {
6380 ArrayList<View> result = new ArrayList<View>(24);
6381 addFocusables(result, direction);
6382 return result;
6383 }
6384
6385 /**
6386 * Add any focusable views that are descendants of this view (possibly
6387 * including this view if it is focusable itself) to views. If we are in touch mode,
6388 * only add views that are also focusable in touch mode.
6389 *
6390 * @param views Focusable views found so far
6391 * @param direction The direction of the focus
6392 */
6393 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006394 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396
svetoslavganov75986cf2009-05-14 22:28:01 -07006397 /**
6398 * Adds any focusable views that are descendants of this view (possibly
6399 * including this view if it is focusable itself) to views. This method
6400 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006401 * only views focusable in touch mode if we are in touch mode or
6402 * only views that can take accessibility focus if accessibility is enabeld
6403 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006404 *
6405 * @param views Focusable views found so far or null if all we are interested is
6406 * the number of focusables.
6407 * @param direction The direction of the focus.
6408 * @param focusableMode The type of focusables to be added.
6409 *
6410 * @see #FOCUSABLES_ALL
6411 * @see #FOCUSABLES_TOUCH_MODE
6412 */
6413 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006414 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006415 return;
6416 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006417 if (!isFocusable()) {
6418 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006419 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006420 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6421 && isInTouchMode() && !isFocusableInTouchMode()) {
6422 return;
6423 }
6424 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 }
6426
6427 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006428 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006429 * The search is performed by either the text that the View renders or the content
6430 * description that describes the view for accessibility purposes and the view does
6431 * not render or both. Clients can specify how the search is to be performed via
6432 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6433 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006434 *
6435 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006436 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006437 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006438 * @see #FIND_VIEWS_WITH_TEXT
6439 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6440 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006441 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006442 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006443 if (getAccessibilityNodeProvider() != null) {
6444 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6445 outViews.add(this);
6446 }
6447 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006448 && (searched != null && searched.length() > 0)
6449 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006450 String searchedLowerCase = searched.toString().toLowerCase();
6451 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6452 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6453 outViews.add(this);
6454 }
6455 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006456 }
6457
6458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 * Find and return all touchable views that are descendants of this view,
6460 * possibly including this view if it is touchable itself.
6461 *
6462 * @return A list of touchable views
6463 */
6464 public ArrayList<View> getTouchables() {
6465 ArrayList<View> result = new ArrayList<View>();
6466 addTouchables(result);
6467 return result;
6468 }
6469
6470 /**
6471 * Add any touchable views that are descendants of this view (possibly
6472 * including this view if it is touchable itself) to views.
6473 *
6474 * @param views Touchable views found so far
6475 */
6476 public void addTouchables(ArrayList<View> views) {
6477 final int viewFlags = mViewFlags;
6478
6479 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6480 && (viewFlags & ENABLED_MASK) == ENABLED) {
6481 views.add(this);
6482 }
6483 }
6484
6485 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006486 * Returns whether this View is accessibility focused.
6487 *
6488 * @return True if this View is accessibility focused.
6489 */
6490 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006491 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006492 }
6493
6494 /**
6495 * Call this to try to give accessibility focus to this view.
6496 *
6497 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6498 * returns false or the view is no visible or the view already has accessibility
6499 * focus.
6500 *
6501 * See also {@link #focusSearch(int)}, which is what you call to say that you
6502 * have focus, and you want your parent to look for the next one.
6503 *
6504 * @return Whether this view actually took accessibility focus.
6505 *
6506 * @hide
6507 */
6508 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006509 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6510 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006511 return false;
6512 }
6513 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6514 return false;
6515 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006516 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6517 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006518 ViewRootImpl viewRootImpl = getViewRootImpl();
6519 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006520 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006521 }
6522 invalidate();
6523 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6524 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006525 return true;
6526 }
6527 return false;
6528 }
6529
6530 /**
6531 * Call this to try to clear accessibility focus of this view.
6532 *
6533 * See also {@link #focusSearch(int)}, which is what you call to say that you
6534 * have focus, and you want your parent to look for the next one.
6535 *
6536 * @hide
6537 */
6538 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006539 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6540 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006541 invalidate();
6542 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6543 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006544 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006545 // Clear the global reference of accessibility focus if this
6546 // view or any of its descendants had accessibility focus.
6547 ViewRootImpl viewRootImpl = getViewRootImpl();
6548 if (viewRootImpl != null) {
6549 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6550 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006551 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006552 }
6553 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006554 }
6555
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006556 private void sendAccessibilityHoverEvent(int eventType) {
6557 // Since we are not delivering to a client accessibility events from not
6558 // important views (unless the clinet request that) we need to fire the
6559 // event from the deepest view exposed to the client. As a consequence if
6560 // the user crosses a not exposed view the client will see enter and exit
6561 // of the exposed predecessor followed by and enter and exit of that same
6562 // predecessor when entering and exiting the not exposed descendant. This
6563 // is fine since the client has a clear idea which view is hovered at the
6564 // price of a couple more events being sent. This is a simple and
6565 // working solution.
6566 View source = this;
6567 while (true) {
6568 if (source.includeForAccessibility()) {
6569 source.sendAccessibilityEvent(eventType);
6570 return;
6571 }
6572 ViewParent parent = source.getParent();
6573 if (parent instanceof View) {
6574 source = (View) parent;
6575 } else {
6576 return;
6577 }
6578 }
6579 }
6580
Svetoslav Ganov42138042012-03-20 11:51:39 -07006581 /**
6582 * Clears accessibility focus without calling any callback methods
6583 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6584 * is used for clearing accessibility focus when giving this focus to
6585 * another view.
6586 */
6587 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006588 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6589 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006590 invalidate();
6591 }
6592 }
6593
6594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 * Call this to try to give focus to a specific view or to one of its
6596 * descendants.
6597 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006598 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6599 * false), or if it is focusable and it is not focusable in touch mode
6600 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006602 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 * have focus, and you want your parent to look for the next one.
6604 *
6605 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6606 * {@link #FOCUS_DOWN} and <code>null</code>.
6607 *
6608 * @return Whether this view or one of its descendants actually took focus.
6609 */
6610 public final boolean requestFocus() {
6611 return requestFocus(View.FOCUS_DOWN);
6612 }
6613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 /**
6615 * Call this to try to give focus to a specific view or to one of its
6616 * descendants and give it a hint about what direction focus is heading.
6617 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006618 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6619 * false), or if it is focusable and it is not focusable in touch mode
6620 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006622 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 * have focus, and you want your parent to look for the next one.
6624 *
6625 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6626 * <code>null</code> set for the previously focused rectangle.
6627 *
6628 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6629 * @return Whether this view or one of its descendants actually took focus.
6630 */
6631 public final boolean requestFocus(int direction) {
6632 return requestFocus(direction, null);
6633 }
6634
6635 /**
6636 * Call this to try to give focus to a specific view or to one of its descendants
6637 * and give it hints about the direction and a specific rectangle that the focus
6638 * is coming from. The rectangle can help give larger views a finer grained hint
6639 * about where focus is coming from, and therefore, where to show selection, or
6640 * forward focus change internally.
6641 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006642 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6643 * false), or if it is focusable and it is not focusable in touch mode
6644 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 *
6646 * A View will not take focus if it is not visible.
6647 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006648 * A View will not take focus if one of its parents has
6649 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6650 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006652 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 * have focus, and you want your parent to look for the next one.
6654 *
6655 * You may wish to override this method if your custom {@link View} has an internal
6656 * {@link View} that it wishes to forward the request to.
6657 *
6658 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6659 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6660 * to give a finer grained hint about where focus is coming from. May be null
6661 * if there is no hint.
6662 * @return Whether this view or one of its descendants actually took focus.
6663 */
6664 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006665 return requestFocusNoSearch(direction, previouslyFocusedRect);
6666 }
6667
6668 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 // need to be focusable
6670 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6671 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6672 return false;
6673 }
6674
6675 // need to be focusable in touch mode if in touch mode
6676 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006677 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6678 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 }
6680
6681 // need to not have any parents blocking us
6682 if (hasAncestorThatBlocksDescendantFocus()) {
6683 return false;
6684 }
6685
6686 handleFocusGainInternal(direction, previouslyFocusedRect);
6687 return true;
6688 }
6689
6690 /**
6691 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6692 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6693 * touch mode to request focus when they are touched.
6694 *
6695 * @return Whether this view or one of its descendants actually took focus.
6696 *
6697 * @see #isInTouchMode()
6698 *
6699 */
6700 public final boolean requestFocusFromTouch() {
6701 // Leave touch mode if we need to
6702 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006703 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006704 if (viewRoot != null) {
6705 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 }
6707 }
6708 return requestFocus(View.FOCUS_DOWN);
6709 }
6710
6711 /**
6712 * @return Whether any ancestor of this view blocks descendant focus.
6713 */
6714 private boolean hasAncestorThatBlocksDescendantFocus() {
6715 ViewParent ancestor = mParent;
6716 while (ancestor instanceof ViewGroup) {
6717 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6718 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6719 return true;
6720 } else {
6721 ancestor = vgAncestor.getParent();
6722 }
6723 }
6724 return false;
6725 }
6726
6727 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006728 * Gets the mode for determining whether this View is important for accessibility
6729 * which is if it fires accessibility events and if it is reported to
6730 * accessibility services that query the screen.
6731 *
6732 * @return The mode for determining whether a View is important for accessibility.
6733 *
6734 * @attr ref android.R.styleable#View_importantForAccessibility
6735 *
6736 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6737 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6738 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6739 */
6740 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006741 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6742 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6743 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006744 })
6745 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006746 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6747 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006748 }
6749
6750 /**
6751 * Sets how to determine whether this view is important for accessibility
6752 * which is if it fires accessibility events and if it is reported to
6753 * accessibility services that query the screen.
6754 *
6755 * @param mode How to determine whether this view is important for accessibility.
6756 *
6757 * @attr ref android.R.styleable#View_importantForAccessibility
6758 *
6759 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6760 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6761 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6762 */
6763 public void setImportantForAccessibility(int mode) {
6764 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006765 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6766 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6767 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006768 notifyAccessibilityStateChanged();
6769 }
6770 }
6771
6772 /**
6773 * Gets whether this view should be exposed for accessibility.
6774 *
6775 * @return Whether the view is exposed for accessibility.
6776 *
6777 * @hide
6778 */
6779 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006780 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6781 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006782 switch (mode) {
6783 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6784 return true;
6785 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6786 return false;
6787 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006788 return isActionableForAccessibility() || hasListenersForAccessibility()
6789 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006790 default:
6791 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6792 + mode);
6793 }
6794 }
6795
6796 /**
6797 * Gets the parent for accessibility purposes. Note that the parent for
6798 * accessibility is not necessary the immediate parent. It is the first
6799 * predecessor that is important for accessibility.
6800 *
6801 * @return The parent for accessibility purposes.
6802 */
6803 public ViewParent getParentForAccessibility() {
6804 if (mParent instanceof View) {
6805 View parentView = (View) mParent;
6806 if (parentView.includeForAccessibility()) {
6807 return mParent;
6808 } else {
6809 return mParent.getParentForAccessibility();
6810 }
6811 }
6812 return null;
6813 }
6814
6815 /**
6816 * Adds the children of a given View for accessibility. Since some Views are
6817 * not important for accessibility the children for accessibility are not
6818 * necessarily direct children of the riew, rather they are the first level of
6819 * descendants important for accessibility.
6820 *
6821 * @param children The list of children for accessibility.
6822 */
6823 public void addChildrenForAccessibility(ArrayList<View> children) {
6824 if (includeForAccessibility()) {
6825 children.add(this);
6826 }
6827 }
6828
6829 /**
6830 * Whether to regard this view for accessibility. A view is regarded for
6831 * accessibility if it is important for accessibility or the querying
6832 * accessibility service has explicitly requested that view not
6833 * important for accessibility are regarded.
6834 *
6835 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006836 *
6837 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006838 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006839 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006840 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006841 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006842 }
6843 return false;
6844 }
6845
6846 /**
6847 * Returns whether the View is considered actionable from
6848 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006849 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006850 *
6851 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006852 *
6853 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006854 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006855 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006856 return (isClickable() || isLongClickable() || isFocusable());
6857 }
6858
6859 /**
6860 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006861 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006862 *
6863 * @return True if the view is actionable for accessibility.
6864 */
6865 private boolean hasListenersForAccessibility() {
6866 ListenerInfo info = getListenerInfo();
6867 return mTouchDelegate != null || info.mOnKeyListener != null
6868 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6869 || info.mOnHoverListener != null || info.mOnDragListener != null;
6870 }
6871
6872 /**
6873 * Notifies accessibility services that some view's important for
6874 * accessibility state has changed. Note that such notifications
6875 * are made at most once every
6876 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6877 * to avoid unnecessary load to the system. Also once a view has
6878 * made a notifucation this method is a NOP until the notification has
6879 * been sent to clients.
6880 *
6881 * @hide
6882 *
6883 * TODO: Makse sure this method is called for any view state change
6884 * that is interesting for accessilility purposes.
6885 */
6886 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006887 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6888 return;
6889 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006890 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6891 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006892 if (mParent != null) {
6893 mParent.childAccessibilityStateChanged(this);
6894 }
6895 }
6896 }
6897
6898 /**
6899 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006900 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006901 *
6902 * @hide
6903 */
6904 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006905 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006906 }
6907
6908 /**
6909 * Performs the specified accessibility action on the view. For
6910 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006911 * <p>
6912 * If an {@link AccessibilityDelegate} has been specified via calling
6913 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6914 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6915 * is responsible for handling this call.
6916 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006917 *
6918 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006919 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006920 * @return Whether the action was performed.
6921 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006922 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006923 if (mAccessibilityDelegate != null) {
6924 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6925 } else {
6926 return performAccessibilityActionInternal(action, arguments);
6927 }
6928 }
6929
6930 /**
6931 * @see #performAccessibilityAction(int, Bundle)
6932 *
6933 * Note: Called from the default {@link AccessibilityDelegate}.
6934 */
6935 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006936 switch (action) {
6937 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006938 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006939 performClick();
6940 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006941 }
6942 } break;
6943 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6944 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006945 performLongClick();
6946 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006947 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006948 } break;
6949 case AccessibilityNodeInfo.ACTION_FOCUS: {
6950 if (!hasFocus()) {
6951 // Get out of touch mode since accessibility
6952 // wants to move focus around.
6953 getViewRootImpl().ensureTouchMode(false);
6954 return requestFocus();
6955 }
6956 } break;
6957 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6958 if (hasFocus()) {
6959 clearFocus();
6960 return !isFocused();
6961 }
6962 } break;
6963 case AccessibilityNodeInfo.ACTION_SELECT: {
6964 if (!isSelected()) {
6965 setSelected(true);
6966 return isSelected();
6967 }
6968 } break;
6969 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6970 if (isSelected()) {
6971 setSelected(false);
6972 return !isSelected();
6973 }
6974 } break;
6975 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006976 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006977 return requestAccessibilityFocus();
6978 }
6979 } break;
6980 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6981 if (isAccessibilityFocused()) {
6982 clearAccessibilityFocus();
6983 return true;
6984 }
6985 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006986 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6987 if (arguments != null) {
6988 final int granularity = arguments.getInt(
6989 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6990 return nextAtGranularity(granularity);
6991 }
6992 } break;
6993 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6994 if (arguments != null) {
6995 final int granularity = arguments.getInt(
6996 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6997 return previousAtGranularity(granularity);
6998 }
6999 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007000 }
7001 return false;
7002 }
7003
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007004 private boolean nextAtGranularity(int granularity) {
7005 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007006 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007007 return false;
7008 }
7009 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7010 if (iterator == null) {
7011 return false;
7012 }
7013 final int current = getAccessibilityCursorPosition();
7014 final int[] range = iterator.following(current);
7015 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007016 return false;
7017 }
7018 final int start = range[0];
7019 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007020 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007021 sendViewTextTraversedAtGranularityEvent(
7022 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
7023 granularity, start, end);
7024 return true;
7025 }
7026
7027 private boolean previousAtGranularity(int granularity) {
7028 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007029 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007030 return false;
7031 }
7032 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7033 if (iterator == null) {
7034 return false;
7035 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007036 int current = getAccessibilityCursorPosition();
7037 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7038 current = text.length();
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007039 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007040 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7041 // When traversing by character we always put the cursor after the character
7042 // to ease edit and have to compensate before asking the for previous segment.
7043 current--;
Svetoslav Ganov02c6fca2012-11-26 17:32:17 -08007044 setAccessibilityCursorPosition(current);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007045 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007046 final int[] range = iterator.preceding(current);
7047 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007048 return false;
7049 }
7050 final int start = range[0];
7051 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007052 // Always put the cursor after the character to ease edit.
7053 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7054 setAccessibilityCursorPosition(end);
7055 } else {
7056 setAccessibilityCursorPosition(start);
7057 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007058 sendViewTextTraversedAtGranularityEvent(
7059 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7060 granularity, start, end);
7061 return true;
7062 }
7063
7064 /**
7065 * Gets the text reported for accessibility purposes.
7066 *
7067 * @return The accessibility text.
7068 *
7069 * @hide
7070 */
7071 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007072 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007073 }
7074
7075 /**
7076 * @hide
7077 */
7078 public int getAccessibilityCursorPosition() {
7079 return mAccessibilityCursorPosition;
7080 }
7081
7082 /**
7083 * @hide
7084 */
7085 public void setAccessibilityCursorPosition(int position) {
7086 mAccessibilityCursorPosition = position;
7087 }
7088
7089 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7090 int fromIndex, int toIndex) {
7091 if (mParent == null) {
7092 return;
7093 }
7094 AccessibilityEvent event = AccessibilityEvent.obtain(
7095 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7096 onInitializeAccessibilityEvent(event);
7097 onPopulateAccessibilityEvent(event);
7098 event.setFromIndex(fromIndex);
7099 event.setToIndex(toIndex);
7100 event.setAction(action);
7101 event.setMovementGranularity(granularity);
7102 mParent.requestSendAccessibilityEvent(this, event);
7103 }
7104
7105 /**
7106 * @hide
7107 */
7108 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7109 switch (granularity) {
7110 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7111 CharSequence text = getIterableTextForAccessibility();
7112 if (text != null && text.length() > 0) {
7113 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007114 CharacterTextSegmentIterator.getInstance(
7115 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007116 iterator.initialize(text.toString());
7117 return iterator;
7118 }
7119 } break;
7120 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7121 CharSequence text = getIterableTextForAccessibility();
7122 if (text != null && text.length() > 0) {
7123 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007124 WordTextSegmentIterator.getInstance(
7125 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007126 iterator.initialize(text.toString());
7127 return iterator;
7128 }
7129 } break;
7130 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7131 CharSequence text = getIterableTextForAccessibility();
7132 if (text != null && text.length() > 0) {
7133 ParagraphTextSegmentIterator iterator =
7134 ParagraphTextSegmentIterator.getInstance();
7135 iterator.initialize(text.toString());
7136 return iterator;
7137 }
7138 } break;
7139 }
7140 return null;
7141 }
7142
Svetoslav Ganov42138042012-03-20 11:51:39 -07007143 /**
Romain Guya440b002010-02-24 15:57:54 -08007144 * @hide
7145 */
7146 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007147 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007148 clearDisplayList();
7149
Romain Guya440b002010-02-24 15:57:54 -08007150 onStartTemporaryDetach();
7151 }
7152
7153 /**
7154 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7156 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007157 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 */
7159 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007160 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007161 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007162 }
7163
7164 /**
7165 * @hide
7166 */
7167 public void dispatchFinishTemporaryDetach() {
7168 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 }
Romain Guy8506ab42009-06-11 17:35:47 -07007170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 /**
7172 * Called after {@link #onStartTemporaryDetach} when the container is done
7173 * changing the view.
7174 */
7175 public void onFinishTemporaryDetach() {
7176 }
Romain Guy8506ab42009-06-11 17:35:47 -07007177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007179 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7180 * for this view's window. Returns null if the view is not currently attached
7181 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007182 * just use the standard high-level event callbacks like
7183 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007184 */
7185 public KeyEvent.DispatcherState getKeyDispatcherState() {
7186 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7187 }
Joe Malin32736f02011-01-19 16:14:20 -08007188
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 * Dispatch a key event before it is processed by any input method
7191 * associated with the view hierarchy. This can be used to intercept
7192 * key events in special situations before the IME consumes them; a
7193 * typical example would be handling the BACK key to update the application's
7194 * UI instead of allowing the IME to see it and close itself.
7195 *
7196 * @param event The key event to be dispatched.
7197 * @return True if the event was handled, false otherwise.
7198 */
7199 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7200 return onKeyPreIme(event.getKeyCode(), event);
7201 }
7202
7203 /**
7204 * Dispatch a key event to the next view on the focus path. This path runs
7205 * from the top of the view tree down to the currently focused view. If this
7206 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7207 * the next node down the focus path. This method also fires any key
7208 * listeners.
7209 *
7210 * @param event The key event to be dispatched.
7211 * @return True if the event was handled, false otherwise.
7212 */
7213 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007214 if (mInputEventConsistencyVerifier != null) {
7215 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007217
Jeff Brown21bc5c92011-02-28 18:27:14 -08007218 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007219 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007220 ListenerInfo li = mListenerInfo;
7221 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7222 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223 return true;
7224 }
7225
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007226 if (event.dispatch(this, mAttachInfo != null
7227 ? mAttachInfo.mKeyDispatchState : null, this)) {
7228 return true;
7229 }
7230
7231 if (mInputEventConsistencyVerifier != null) {
7232 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7233 }
7234 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 }
7236
7237 /**
7238 * Dispatches a key shortcut event.
7239 *
7240 * @param event The key event to be dispatched.
7241 * @return True if the event was handled by the view, false otherwise.
7242 */
7243 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7244 return onKeyShortcut(event.getKeyCode(), event);
7245 }
7246
7247 /**
7248 * Pass the touch screen motion event down to the target view, or this
7249 * view if it is the target.
7250 *
7251 * @param event The motion event to be dispatched.
7252 * @return True if the event was handled by the view, false otherwise.
7253 */
7254 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007255 if (mInputEventConsistencyVerifier != null) {
7256 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7257 }
7258
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007259 if (onFilterTouchEventForSecurity(event)) {
7260 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007261 ListenerInfo li = mListenerInfo;
7262 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7263 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007264 return true;
7265 }
7266
7267 if (onTouchEvent(event)) {
7268 return true;
7269 }
Jeff Brown85a31762010-09-01 17:01:00 -07007270 }
7271
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007272 if (mInputEventConsistencyVerifier != null) {
7273 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007275 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 }
7277
7278 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007279 * Filter the touch event to apply security policies.
7280 *
7281 * @param event The motion event to be filtered.
7282 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007283 *
Jeff Brown85a31762010-09-01 17:01:00 -07007284 * @see #getFilterTouchesWhenObscured
7285 */
7286 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007287 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007288 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7289 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7290 // Window is obscured, drop this touch.
7291 return false;
7292 }
7293 return true;
7294 }
7295
7296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 * Pass a trackball motion event down to the focused view.
7298 *
7299 * @param event The motion event to be dispatched.
7300 * @return True if the event was handled by the view, false otherwise.
7301 */
7302 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007303 if (mInputEventConsistencyVerifier != null) {
7304 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7305 }
7306
Romain Guy02ccac62011-06-24 13:20:23 -07007307 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 }
7309
7310 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007311 * Dispatch a generic motion event.
7312 * <p>
7313 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7314 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007315 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007316 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007317 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007318 *
7319 * @param event The motion event to be dispatched.
7320 * @return True if the event was handled by the view, false otherwise.
7321 */
7322 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007323 if (mInputEventConsistencyVerifier != null) {
7324 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7325 }
7326
Jeff Browna032cc02011-03-07 16:56:21 -08007327 final int source = event.getSource();
7328 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7329 final int action = event.getAction();
7330 if (action == MotionEvent.ACTION_HOVER_ENTER
7331 || action == MotionEvent.ACTION_HOVER_MOVE
7332 || action == MotionEvent.ACTION_HOVER_EXIT) {
7333 if (dispatchHoverEvent(event)) {
7334 return true;
7335 }
7336 } else if (dispatchGenericPointerEvent(event)) {
7337 return true;
7338 }
7339 } else if (dispatchGenericFocusedEvent(event)) {
7340 return true;
7341 }
7342
Jeff Brown10b62902011-06-20 16:40:37 -07007343 if (dispatchGenericMotionEventInternal(event)) {
7344 return true;
7345 }
7346
7347 if (mInputEventConsistencyVerifier != null) {
7348 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7349 }
7350 return false;
7351 }
7352
7353 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007354 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007355 ListenerInfo li = mListenerInfo;
7356 if (li != null && li.mOnGenericMotionListener != null
7357 && (mViewFlags & ENABLED_MASK) == ENABLED
7358 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007359 return true;
7360 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007361
7362 if (onGenericMotionEvent(event)) {
7363 return true;
7364 }
7365
7366 if (mInputEventConsistencyVerifier != null) {
7367 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7368 }
7369 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007370 }
7371
7372 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007373 * Dispatch a hover event.
7374 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007375 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007376 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007377 * </p>
7378 *
7379 * @param event The motion event to be dispatched.
7380 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007381 */
7382 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007383 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007384 ListenerInfo li = mListenerInfo;
7385 if (li != null && li.mOnHoverListener != null
7386 && (mViewFlags & ENABLED_MASK) == ENABLED
7387 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007388 return true;
7389 }
7390
Jeff Browna032cc02011-03-07 16:56:21 -08007391 return onHoverEvent(event);
7392 }
7393
7394 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007395 * Returns true if the view has a child to which it has recently sent
7396 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7397 * it does not have a hovered child, then it must be the innermost hovered view.
7398 * @hide
7399 */
7400 protected boolean hasHoveredChild() {
7401 return false;
7402 }
7403
7404 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007405 * Dispatch a generic motion event to the view under the first pointer.
7406 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007407 * Do not call this method directly.
7408 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007409 * </p>
7410 *
7411 * @param event The motion event to be dispatched.
7412 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007413 */
7414 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7415 return false;
7416 }
7417
7418 /**
7419 * Dispatch a generic motion event to the currently focused view.
7420 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007421 * Do not call this method directly.
7422 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007423 * </p>
7424 *
7425 * @param event The motion event to be dispatched.
7426 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007427 */
7428 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7429 return false;
7430 }
7431
7432 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007433 * Dispatch a pointer event.
7434 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007435 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7436 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7437 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007438 * and should not be expected to handle other pointing device features.
7439 * </p>
7440 *
7441 * @param event The motion event to be dispatched.
7442 * @return True if the event was handled by the view, false otherwise.
7443 * @hide
7444 */
7445 public final boolean dispatchPointerEvent(MotionEvent event) {
7446 if (event.isTouchEvent()) {
7447 return dispatchTouchEvent(event);
7448 } else {
7449 return dispatchGenericMotionEvent(event);
7450 }
7451 }
7452
7453 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 * Called when the window containing this view gains or loses window focus.
7455 * ViewGroups should override to route to their children.
7456 *
7457 * @param hasFocus True if the window containing this view now has focus,
7458 * false otherwise.
7459 */
7460 public void dispatchWindowFocusChanged(boolean hasFocus) {
7461 onWindowFocusChanged(hasFocus);
7462 }
7463
7464 /**
7465 * Called when the window containing this view gains or loses focus. Note
7466 * that this is separate from view focus: to receive key events, both
7467 * your view and its window must have focus. If a window is displayed
7468 * on top of yours that takes input focus, then your own window will lose
7469 * focus but the view focus will remain unchanged.
7470 *
7471 * @param hasWindowFocus True if the window containing this view now has
7472 * focus, false otherwise.
7473 */
7474 public void onWindowFocusChanged(boolean hasWindowFocus) {
7475 InputMethodManager imm = InputMethodManager.peekInstance();
7476 if (!hasWindowFocus) {
7477 if (isPressed()) {
7478 setPressed(false);
7479 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007480 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 imm.focusOut(this);
7482 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007483 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007484 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007485 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007486 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 imm.focusIn(this);
7488 }
7489 refreshDrawableState();
7490 }
7491
7492 /**
7493 * Returns true if this view is in a window that currently has window focus.
7494 * Note that this is not the same as the view itself having focus.
7495 *
7496 * @return True if this view is in a window that currently has window focus.
7497 */
7498 public boolean hasWindowFocus() {
7499 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7500 }
7501
7502 /**
Adam Powell326d8082009-12-09 15:10:07 -08007503 * Dispatch a view visibility change down the view hierarchy.
7504 * ViewGroups should override to route to their children.
7505 * @param changedView The view whose visibility changed. Could be 'this' or
7506 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007507 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7508 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007509 */
7510 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7511 onVisibilityChanged(changedView, visibility);
7512 }
7513
7514 /**
7515 * Called when the visibility of the view or an ancestor of the view is changed.
7516 * @param changedView The view whose visibility changed. Could be 'this' or
7517 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007518 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7519 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007520 */
7521 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007522 if (visibility == VISIBLE) {
7523 if (mAttachInfo != null) {
7524 initialAwakenScrollBars();
7525 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007526 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007527 }
7528 }
Adam Powell326d8082009-12-09 15:10:07 -08007529 }
7530
7531 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007532 * Dispatch a hint about whether this view is displayed. For instance, when
7533 * a View moves out of the screen, it might receives a display hint indicating
7534 * the view is not displayed. Applications should not <em>rely</em> on this hint
7535 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007536 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007537 * @param hint A hint about whether or not this view is displayed:
7538 * {@link #VISIBLE} or {@link #INVISIBLE}.
7539 */
7540 public void dispatchDisplayHint(int hint) {
7541 onDisplayHint(hint);
7542 }
7543
7544 /**
7545 * Gives this view a hint about whether is displayed or not. For instance, when
7546 * a View moves out of the screen, it might receives a display hint indicating
7547 * the view is not displayed. Applications should not <em>rely</em> on this hint
7548 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007549 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007550 * @param hint A hint about whether or not this view is displayed:
7551 * {@link #VISIBLE} or {@link #INVISIBLE}.
7552 */
7553 protected void onDisplayHint(int hint) {
7554 }
7555
7556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007557 * Dispatch a window visibility change down the view hierarchy.
7558 * ViewGroups should override to route to their children.
7559 *
7560 * @param visibility The new visibility of the window.
7561 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007562 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 */
7564 public void dispatchWindowVisibilityChanged(int visibility) {
7565 onWindowVisibilityChanged(visibility);
7566 }
7567
7568 /**
7569 * Called when the window containing has change its visibility
7570 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7571 * that this tells you whether or not your window is being made visible
7572 * to the window manager; this does <em>not</em> tell you whether or not
7573 * your window is obscured by other windows on the screen, even if it
7574 * is itself visible.
7575 *
7576 * @param visibility The new visibility of the window.
7577 */
7578 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007579 if (visibility == VISIBLE) {
7580 initialAwakenScrollBars();
7581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 }
7583
7584 /**
7585 * Returns the current visibility of the window this view is attached to
7586 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7587 *
7588 * @return Returns the current visibility of the view's window.
7589 */
7590 public int getWindowVisibility() {
7591 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7592 }
7593
7594 /**
7595 * Retrieve the overall visible display size in which the window this view is
7596 * attached to has been positioned in. This takes into account screen
7597 * decorations above the window, for both cases where the window itself
7598 * is being position inside of them or the window is being placed under
7599 * then and covered insets are used for the window to position its content
7600 * inside. In effect, this tells you the available area where content can
7601 * be placed and remain visible to users.
7602 *
7603 * <p>This function requires an IPC back to the window manager to retrieve
7604 * the requested information, so should not be used in performance critical
7605 * code like drawing.
7606 *
7607 * @param outRect Filled in with the visible display frame. If the view
7608 * is not attached to a window, this is simply the raw display size.
7609 */
7610 public void getWindowVisibleDisplayFrame(Rect outRect) {
7611 if (mAttachInfo != null) {
7612 try {
7613 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7614 } catch (RemoteException e) {
7615 return;
7616 }
7617 // XXX This is really broken, and probably all needs to be done
7618 // in the window manager, and we need to know more about whether
7619 // we want the area behind or in front of the IME.
7620 final Rect insets = mAttachInfo.mVisibleInsets;
7621 outRect.left += insets.left;
7622 outRect.top += insets.top;
7623 outRect.right -= insets.right;
7624 outRect.bottom -= insets.bottom;
7625 return;
7626 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007627 // The view is not attached to a display so we don't have a context.
7628 // Make a best guess about the display size.
7629 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007630 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 }
7632
7633 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007634 * Dispatch a notification about a resource configuration change down
7635 * the view hierarchy.
7636 * ViewGroups should override to route to their children.
7637 *
7638 * @param newConfig The new resource configuration.
7639 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007640 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007641 */
7642 public void dispatchConfigurationChanged(Configuration newConfig) {
7643 onConfigurationChanged(newConfig);
7644 }
7645
7646 /**
7647 * Called when the current configuration of the resources being used
7648 * by the application have changed. You can use this to decide when
7649 * to reload resources that can changed based on orientation and other
7650 * configuration characterstics. You only need to use this if you are
7651 * not relying on the normal {@link android.app.Activity} mechanism of
7652 * recreating the activity instance upon a configuration change.
7653 *
7654 * @param newConfig The new resource configuration.
7655 */
7656 protected void onConfigurationChanged(Configuration newConfig) {
7657 }
7658
7659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 * Private function to aggregate all per-view attributes in to the view
7661 * root.
7662 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007663 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7664 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
7666
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007667 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7668 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007669 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007670 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007671 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007672 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007673 ListenerInfo li = mListenerInfo;
7674 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007675 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 }
7678 }
7679
7680 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007681 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007682 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007683 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7684 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 ai.mRecomputeGlobalAttributes = true;
7686 }
7687 }
7688 }
7689
7690 /**
7691 * Returns whether the device is currently in touch mode. Touch mode is entered
7692 * once the user begins interacting with the device by touch, and affects various
7693 * things like whether focus is always visible to the user.
7694 *
7695 * @return Whether the device is in touch mode.
7696 */
7697 @ViewDebug.ExportedProperty
7698 public boolean isInTouchMode() {
7699 if (mAttachInfo != null) {
7700 return mAttachInfo.mInTouchMode;
7701 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007702 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 }
7704 }
7705
7706 /**
7707 * Returns the context the view is running in, through which it can
7708 * access the current theme, resources, etc.
7709 *
7710 * @return The view's Context.
7711 */
7712 @ViewDebug.CapturedViewProperty
7713 public final Context getContext() {
7714 return mContext;
7715 }
7716
7717 /**
7718 * Handle a key event before it is processed by any input method
7719 * associated with the view hierarchy. This can be used to intercept
7720 * key events in special situations before the IME consumes them; a
7721 * typical example would be handling the BACK key to update the application's
7722 * UI instead of allowing the IME to see it and close itself.
7723 *
7724 * @param keyCode The value in event.getKeyCode().
7725 * @param event Description of the key event.
7726 * @return If you handled the event, return true. If you want to allow the
7727 * event to be handled by the next receiver, return false.
7728 */
7729 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7730 return false;
7731 }
7732
7733 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007734 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7735 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7737 * is released, if the view is enabled and clickable.
7738 *
Jean Chalard405bc512012-05-29 19:12:34 +09007739 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7740 * although some may elect to do so in some situations. Do not rely on this to
7741 * catch software key presses.
7742 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 * @param keyCode A key code that represents the button pressed, from
7744 * {@link android.view.KeyEvent}.
7745 * @param event The KeyEvent object that defines the button action.
7746 */
7747 public boolean onKeyDown(int keyCode, KeyEvent event) {
7748 boolean result = false;
7749
7750 switch (keyCode) {
7751 case KeyEvent.KEYCODE_DPAD_CENTER:
7752 case KeyEvent.KEYCODE_ENTER: {
7753 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7754 return true;
7755 }
7756 // Long clickable items don't necessarily have to be clickable
7757 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7758 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7759 (event.getRepeatCount() == 0)) {
7760 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007761 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 return true;
7763 }
7764 break;
7765 }
7766 }
7767 return result;
7768 }
7769
7770 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007771 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7772 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7773 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007774 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7775 * although some may elect to do so in some situations. Do not rely on this to
7776 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007777 */
7778 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7779 return false;
7780 }
7781
7782 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007783 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7784 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7786 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007787 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7788 * although some may elect to do so in some situations. Do not rely on this to
7789 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 *
7791 * @param keyCode A key code that represents the button pressed, from
7792 * {@link android.view.KeyEvent}.
7793 * @param event The KeyEvent object that defines the button action.
7794 */
7795 public boolean onKeyUp(int keyCode, KeyEvent event) {
7796 boolean result = false;
7797
7798 switch (keyCode) {
7799 case KeyEvent.KEYCODE_DPAD_CENTER:
7800 case KeyEvent.KEYCODE_ENTER: {
7801 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7802 return true;
7803 }
7804 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7805 setPressed(false);
7806
7807 if (!mHasPerformedLongPress) {
7808 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007809 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810
7811 result = performClick();
7812 }
7813 }
7814 break;
7815 }
7816 }
7817 return result;
7818 }
7819
7820 /**
7821 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7822 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7823 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007824 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7825 * although some may elect to do so in some situations. Do not rely on this to
7826 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 *
7828 * @param keyCode A key code that represents the button pressed, from
7829 * {@link android.view.KeyEvent}.
7830 * @param repeatCount The number of times the action was made.
7831 * @param event The KeyEvent object that defines the button action.
7832 */
7833 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7834 return false;
7835 }
7836
7837 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007838 * Called on the focused view when a key shortcut event is not handled.
7839 * Override this method to implement local key shortcuts for the View.
7840 * Key shortcuts can also be implemented by setting the
7841 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 *
7843 * @param keyCode The value in event.getKeyCode().
7844 * @param event Description of the key event.
7845 * @return If you handled the event, return true. If you want to allow the
7846 * event to be handled by the next receiver, return false.
7847 */
7848 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7849 return false;
7850 }
7851
7852 /**
7853 * Check whether the called view is a text editor, in which case it
7854 * would make sense to automatically display a soft input window for
7855 * it. Subclasses should override this if they implement
7856 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007857 * a call on that method would return a non-null InputConnection, and
7858 * they are really a first-class editor that the user would normally
7859 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007860 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007861 * <p>The default implementation always returns false. This does
7862 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7863 * will not be called or the user can not otherwise perform edits on your
7864 * view; it is just a hint to the system that this is not the primary
7865 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007866 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 * @return Returns true if this view is a text editor, else false.
7868 */
7869 public boolean onCheckIsTextEditor() {
7870 return false;
7871 }
Romain Guy8506ab42009-06-11 17:35:47 -07007872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 /**
7874 * Create a new InputConnection for an InputMethod to interact
7875 * with the view. The default implementation returns null, since it doesn't
7876 * support input methods. You can override this to implement such support.
7877 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007878 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 * <p>When implementing this, you probably also want to implement
7880 * {@link #onCheckIsTextEditor()} to indicate you will return a
7881 * non-null InputConnection.
7882 *
7883 * @param outAttrs Fill in with attribute information about the connection.
7884 */
7885 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7886 return null;
7887 }
7888
7889 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007890 * Called by the {@link android.view.inputmethod.InputMethodManager}
7891 * when a view who is not the current
7892 * input connection target is trying to make a call on the manager. The
7893 * default implementation returns false; you can override this to return
7894 * true for certain views if you are performing InputConnection proxying
7895 * to them.
7896 * @param view The View that is making the InputMethodManager call.
7897 * @return Return true to allow the call, false to reject.
7898 */
7899 public boolean checkInputConnectionProxy(View view) {
7900 return false;
7901 }
Romain Guy8506ab42009-06-11 17:35:47 -07007902
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 * Show the context menu for this view. It is not safe to hold on to the
7905 * menu after returning from this method.
7906 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007907 * You should normally not overload this method. Overload
7908 * {@link #onCreateContextMenu(ContextMenu)} or define an
7909 * {@link OnCreateContextMenuListener} to add items to the context menu.
7910 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007911 * @param menu The context menu to populate
7912 */
7913 public void createContextMenu(ContextMenu menu) {
7914 ContextMenuInfo menuInfo = getContextMenuInfo();
7915
7916 // Sets the current menu info so all items added to menu will have
7917 // my extra info set.
7918 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7919
7920 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007921 ListenerInfo li = mListenerInfo;
7922 if (li != null && li.mOnCreateContextMenuListener != null) {
7923 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007924 }
7925
7926 // Clear the extra information so subsequent items that aren't mine don't
7927 // have my extra info.
7928 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7929
7930 if (mParent != null) {
7931 mParent.createContextMenu(menu);
7932 }
7933 }
7934
7935 /**
7936 * Views should implement this if they have extra information to associate
7937 * with the context menu. The return result is supplied as a parameter to
7938 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7939 * callback.
7940 *
7941 * @return Extra information about the item for which the context menu
7942 * should be shown. This information will vary across different
7943 * subclasses of View.
7944 */
7945 protected ContextMenuInfo getContextMenuInfo() {
7946 return null;
7947 }
7948
7949 /**
7950 * Views should implement this if the view itself is going to add items to
7951 * the context menu.
7952 *
7953 * @param menu the context menu to populate
7954 */
7955 protected void onCreateContextMenu(ContextMenu menu) {
7956 }
7957
7958 /**
7959 * Implement this method to handle trackball motion events. The
7960 * <em>relative</em> movement of the trackball since the last event
7961 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7962 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7963 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7964 * they will often be fractional values, representing the more fine-grained
7965 * movement information available from a trackball).
7966 *
7967 * @param event The motion event.
7968 * @return True if the event was handled, false otherwise.
7969 */
7970 public boolean onTrackballEvent(MotionEvent event) {
7971 return false;
7972 }
7973
7974 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007975 * Implement this method to handle generic motion events.
7976 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007977 * Generic motion events describe joystick movements, mouse hovers, track pad
7978 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007979 * {@link MotionEvent#getSource() source} of the motion event specifies
7980 * the class of input that was received. Implementations of this method
7981 * must examine the bits in the source before processing the event.
7982 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007983 * </p><p>
7984 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7985 * are delivered to the view under the pointer. All other generic motion events are
7986 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007987 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007988 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007989 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007990 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7991 * // process the joystick movement...
7992 * return true;
7993 * }
7994 * }
7995 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7996 * switch (event.getAction()) {
7997 * case MotionEvent.ACTION_HOVER_MOVE:
7998 * // process the mouse hover movement...
7999 * return true;
8000 * case MotionEvent.ACTION_SCROLL:
8001 * // process the scroll wheel movement...
8002 * return true;
8003 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08008004 * }
8005 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07008006 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08008007 *
8008 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08008009 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08008010 */
8011 public boolean onGenericMotionEvent(MotionEvent event) {
8012 return false;
8013 }
8014
8015 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008016 * Implement this method to handle hover events.
8017 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07008018 * This method is called whenever a pointer is hovering into, over, or out of the
8019 * bounds of a view and the view is not currently being touched.
8020 * Hover events are represented as pointer events with action
8021 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
8022 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
8023 * </p>
8024 * <ul>
8025 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8026 * when the pointer enters the bounds of the view.</li>
8027 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8028 * when the pointer has already entered the bounds of the view and has moved.</li>
8029 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8030 * when the pointer has exited the bounds of the view or when the pointer is
8031 * about to go down due to a button click, tap, or similar user action that
8032 * causes the view to be touched.</li>
8033 * </ul>
8034 * <p>
8035 * The view should implement this method to return true to indicate that it is
8036 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008037 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008038 * The default implementation calls {@link #setHovered} to update the hovered state
8039 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008040 * is enabled and is clickable. The default implementation also sends hover
8041 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008042 * </p>
8043 *
8044 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008045 * @return True if the view handled the hover event.
8046 *
8047 * @see #isHovered
8048 * @see #setHovered
8049 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008050 */
8051 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008052 // The root view may receive hover (or touch) events that are outside the bounds of
8053 // the window. This code ensures that we only send accessibility events for
8054 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008055 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008056 if (!mSendingHoverAccessibilityEvents) {
8057 if ((action == MotionEvent.ACTION_HOVER_ENTER
8058 || action == MotionEvent.ACTION_HOVER_MOVE)
8059 && !hasHoveredChild()
8060 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008061 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008062 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008063 }
8064 } else {
8065 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008066 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008067 && !pointInView(event.getX(), event.getY()))) {
8068 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008069 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008070 // If the window does not have input focus we take away accessibility
8071 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008072 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008073 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008074 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008075 }
Jeff Browna1b24182011-07-28 13:38:24 -07008076 }
8077
Jeff Brown87b7f802011-06-21 18:35:45 -07008078 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008079 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008080 case MotionEvent.ACTION_HOVER_ENTER:
8081 setHovered(true);
8082 break;
8083 case MotionEvent.ACTION_HOVER_EXIT:
8084 setHovered(false);
8085 break;
8086 }
Jeff Browna1b24182011-07-28 13:38:24 -07008087
8088 // Dispatch the event to onGenericMotionEvent before returning true.
8089 // This is to provide compatibility with existing applications that
8090 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8091 // break because of the new default handling for hoverable views
8092 // in onHoverEvent.
8093 // Note that onGenericMotionEvent will be called by default when
8094 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8095 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008096 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008097 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008098
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008099 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008100 }
8101
8102 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008103 * Returns true if the view should handle {@link #onHoverEvent}
8104 * by calling {@link #setHovered} to change its hovered state.
8105 *
8106 * @return True if the view is hoverable.
8107 */
8108 private boolean isHoverable() {
8109 final int viewFlags = mViewFlags;
8110 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8111 return false;
8112 }
8113
8114 return (viewFlags & CLICKABLE) == CLICKABLE
8115 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8116 }
8117
8118 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008119 * Returns true if the view is currently hovered.
8120 *
8121 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008122 *
8123 * @see #setHovered
8124 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008125 */
Jeff Brown10b62902011-06-20 16:40:37 -07008126 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008127 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008128 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008129 }
8130
8131 /**
8132 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008133 * <p>
8134 * Calling this method also changes the drawable state of the view. This
8135 * enables the view to react to hover by using different drawable resources
8136 * to change its appearance.
8137 * </p><p>
8138 * The {@link #onHoverChanged} method is called when the hovered state changes.
8139 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008140 *
8141 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008142 *
8143 * @see #isHovered
8144 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008145 */
8146 public void setHovered(boolean hovered) {
8147 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008148 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8149 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008150 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008151 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008152 }
8153 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008154 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8155 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008156 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008157 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008158 }
8159 }
8160 }
8161
8162 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008163 * Implement this method to handle hover state changes.
8164 * <p>
8165 * This method is called whenever the hover state changes as a result of a
8166 * call to {@link #setHovered}.
8167 * </p>
8168 *
8169 * @param hovered The current hover state, as returned by {@link #isHovered}.
8170 *
8171 * @see #isHovered
8172 * @see #setHovered
8173 */
8174 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008175 }
8176
8177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 * Implement this method to handle touch screen motion events.
8179 *
8180 * @param event The motion event.
8181 * @return True if the event was handled, false otherwise.
8182 */
8183 public boolean onTouchEvent(MotionEvent event) {
8184 final int viewFlags = mViewFlags;
8185
8186 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008187 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008188 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 // A disabled view that is clickable still consumes the touch
8191 // events, it just doesn't respond to them.
8192 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8193 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8194 }
8195
8196 if (mTouchDelegate != null) {
8197 if (mTouchDelegate.onTouchEvent(event)) {
8198 return true;
8199 }
8200 }
8201
8202 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8203 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8204 switch (event.getAction()) {
8205 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008206 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8207 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008208 // take focus if we don't have it already and we should in
8209 // touch mode.
8210 boolean focusTaken = false;
8211 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8212 focusTaken = requestFocus();
8213 }
8214
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008215 if (prepressed) {
8216 // The button is being released before we actually
8217 // showed it as pressed. Make it show the pressed
8218 // state now (before scheduling the click) to ensure
8219 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008220 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008221 }
Joe Malin32736f02011-01-19 16:14:20 -08008222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 if (!mHasPerformedLongPress) {
8224 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008225 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226
8227 // Only perform take click actions if we were in the pressed state
8228 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008229 // Use a Runnable and post this rather than calling
8230 // performClick directly. This lets other visual state
8231 // of the view update before click actions start.
8232 if (mPerformClick == null) {
8233 mPerformClick = new PerformClick();
8234 }
8235 if (!post(mPerformClick)) {
8236 performClick();
8237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 }
8239 }
8240
8241 if (mUnsetPressedState == null) {
8242 mUnsetPressedState = new UnsetPressedState();
8243 }
8244
Adam Powelle14579b2009-12-16 18:39:52 -08008245 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008246 postDelayed(mUnsetPressedState,
8247 ViewConfiguration.getPressedStateDuration());
8248 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 // If the post failed, unpress right now
8250 mUnsetPressedState.run();
8251 }
Adam Powelle14579b2009-12-16 18:39:52 -08008252 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 }
8254 break;
8255
8256 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008257 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008258
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008259 if (performButtonActionOnTouchDown(event)) {
8260 break;
8261 }
8262
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008263 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008264 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008265
8266 // For views inside a scrolling container, delay the pressed feedback for
8267 // a short period in case this is a scroll.
8268 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008269 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008270 if (mPendingCheckForTap == null) {
8271 mPendingCheckForTap = new CheckForTap();
8272 }
8273 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8274 } else {
8275 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008276 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008277 checkForLongClick(0);
8278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 break;
8280
8281 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008282 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008283 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008284 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 break;
8286
8287 case MotionEvent.ACTION_MOVE:
8288 final int x = (int) event.getX();
8289 final int y = (int) event.getY();
8290
8291 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008292 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008294 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008295 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008296 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008297 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298
Adam Powell4d6f0662012-02-21 15:11:11 -08008299 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008301 }
8302 break;
8303 }
8304 return true;
8305 }
8306
8307 return false;
8308 }
8309
8310 /**
Adam Powell10298662011-08-14 18:26:30 -07008311 * @hide
8312 */
8313 public boolean isInScrollingContainer() {
8314 ViewParent p = getParent();
8315 while (p != null && p instanceof ViewGroup) {
8316 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8317 return true;
8318 }
8319 p = p.getParent();
8320 }
8321 return false;
8322 }
8323
8324 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008325 * Remove the longpress detection timer.
8326 */
8327 private void removeLongPressCallback() {
8328 if (mPendingCheckForLongPress != null) {
8329 removeCallbacks(mPendingCheckForLongPress);
8330 }
8331 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008332
8333 /**
8334 * Remove the pending click action
8335 */
8336 private void removePerformClickCallback() {
8337 if (mPerformClick != null) {
8338 removeCallbacks(mPerformClick);
8339 }
8340 }
8341
Adam Powelle14579b2009-12-16 18:39:52 -08008342 /**
Romain Guya440b002010-02-24 15:57:54 -08008343 * Remove the prepress detection timer.
8344 */
8345 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008346 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008347 setPressed(false);
8348 removeCallbacks(mUnsetPressedState);
8349 }
8350 }
8351
8352 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008353 * Remove the tap detection timer.
8354 */
8355 private void removeTapCallback() {
8356 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008357 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008358 removeCallbacks(mPendingCheckForTap);
8359 }
8360 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008361
8362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 * Cancels a pending long press. Your subclass can use this if you
8364 * want the context menu to come up if the user presses and holds
8365 * at the same place, but you don't want it to come up if they press
8366 * and then move around enough to cause scrolling.
8367 */
8368 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008369 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008370
8371 /*
8372 * The prepressed state handled by the tap callback is a display
8373 * construct, but the tap callback will post a long press callback
8374 * less its own timeout. Remove it here.
8375 */
8376 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 }
8378
8379 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008380 * Remove the pending callback for sending a
8381 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8382 */
8383 private void removeSendViewScrolledAccessibilityEventCallback() {
8384 if (mSendViewScrolledAccessibilityEvent != null) {
8385 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008386 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008387 }
8388 }
8389
8390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 * Sets the TouchDelegate for this View.
8392 */
8393 public void setTouchDelegate(TouchDelegate delegate) {
8394 mTouchDelegate = delegate;
8395 }
8396
8397 /**
8398 * Gets the TouchDelegate for this View.
8399 */
8400 public TouchDelegate getTouchDelegate() {
8401 return mTouchDelegate;
8402 }
8403
8404 /**
8405 * Set flags controlling behavior of this view.
8406 *
8407 * @param flags Constant indicating the value which should be set
8408 * @param mask Constant indicating the bit range that should be changed
8409 */
8410 void setFlags(int flags, int mask) {
8411 int old = mViewFlags;
8412 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8413
8414 int changed = mViewFlags ^ old;
8415 if (changed == 0) {
8416 return;
8417 }
8418 int privateFlags = mPrivateFlags;
8419
8420 /* Check if the FOCUSABLE bit has changed */
8421 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008422 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008424 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425 /* Give up focus if we are no longer focusable */
8426 clearFocus();
8427 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008428 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008429 /*
8430 * Tell the view system that we are now available to take focus
8431 * if no one else already has it.
8432 */
8433 if (mParent != null) mParent.focusableViewAvailable(this);
8434 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008435 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8436 notifyAccessibilityStateChanged();
8437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 }
8439
8440 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8441 if ((changed & VISIBILITY_MASK) != 0) {
8442 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008443 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008444 * it was not visible. Marking it drawn ensures that the invalidation will
8445 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008447 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008448 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449
8450 needGlobalAttributesUpdate(true);
8451
8452 // a view becoming visible is worth notifying the parent
8453 // about in case nothing has focus. even if this specific view
8454 // isn't focusable, it may contain something that is, so let
8455 // the root view try to give this focus if nothing else does.
8456 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8457 mParent.focusableViewAvailable(this);
8458 }
8459 }
8460 }
8461
8462 /* Check if the GONE bit has changed */
8463 if ((changed & GONE) != 0) {
8464 needGlobalAttributesUpdate(false);
8465 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466
Romain Guyecd80ee2009-12-03 17:13:02 -08008467 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8468 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008469 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008470 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008471 if (mParent instanceof View) {
8472 // GONE views noop invalidation, so invalidate the parent
8473 ((View) mParent).invalidate(true);
8474 }
8475 // Mark the view drawn to ensure that it gets invalidated properly the next
8476 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008477 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 }
8479 if (mAttachInfo != null) {
8480 mAttachInfo.mViewVisibilityChanged = true;
8481 }
8482 }
8483
8484 /* Check if the VISIBLE bit has changed */
8485 if ((changed & INVISIBLE) != 0) {
8486 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008487 /*
8488 * If this view is becoming invisible, set the DRAWN flag so that
8489 * the next invalidate() will not be skipped.
8490 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008491 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492
8493 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008494 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008495 if (getRootView() != this) {
8496 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008497 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 }
8499 }
8500 if (mAttachInfo != null) {
8501 mAttachInfo.mViewVisibilityChanged = true;
8502 }
8503 }
8504
Adam Powell326d8082009-12-09 15:10:07 -08008505 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008506 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008507 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8508 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008509 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008510 } else if (mParent != null) {
8511 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008512 }
Adam Powell326d8082009-12-09 15:10:07 -08008513 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8514 }
8515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8517 destroyDrawingCache();
8518 }
8519
8520 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8521 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008522 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008523 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008524 }
8525
8526 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8527 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008528 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 }
8530
8531 if ((changed & DRAW_MASK) != 0) {
8532 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008533 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008534 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8535 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008537 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008538 }
8539 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008540 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 }
8542 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008543 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 }
8545
8546 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008547 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 mParent.recomputeViewAttributes(this);
8549 }
8550 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008551
8552 if (AccessibilityManager.getInstance(mContext).isEnabled()
8553 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8554 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8555 notifyAccessibilityStateChanged();
8556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 }
8558
8559 /**
8560 * Change the view's z order in the tree, so it's on top of other sibling
8561 * views
8562 */
8563 public void bringToFront() {
8564 if (mParent != null) {
8565 mParent.bringChildToFront(this);
8566 }
8567 }
8568
8569 /**
8570 * This is called in response to an internal scroll in this view (i.e., the
8571 * view scrolled its own contents). This is typically as a result of
8572 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8573 * called.
8574 *
8575 * @param l Current horizontal scroll origin.
8576 * @param t Current vertical scroll origin.
8577 * @param oldl Previous horizontal scroll origin.
8578 * @param oldt Previous vertical scroll origin.
8579 */
8580 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008581 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8582 postSendViewScrolledAccessibilityEventCallback();
8583 }
8584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 mBackgroundSizeChanged = true;
8586
8587 final AttachInfo ai = mAttachInfo;
8588 if (ai != null) {
8589 ai.mViewScrollChanged = true;
8590 }
8591 }
8592
8593 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008594 * Interface definition for a callback to be invoked when the layout bounds of a view
8595 * changes due to layout processing.
8596 */
8597 public interface OnLayoutChangeListener {
8598 /**
8599 * Called when the focus state of a view has changed.
8600 *
8601 * @param v The view whose state has changed.
8602 * @param left The new value of the view's left property.
8603 * @param top The new value of the view's top property.
8604 * @param right The new value of the view's right property.
8605 * @param bottom The new value of the view's bottom property.
8606 * @param oldLeft The previous value of the view's left property.
8607 * @param oldTop The previous value of the view's top property.
8608 * @param oldRight The previous value of the view's right property.
8609 * @param oldBottom The previous value of the view's bottom property.
8610 */
8611 void onLayoutChange(View v, int left, int top, int right, int bottom,
8612 int oldLeft, int oldTop, int oldRight, int oldBottom);
8613 }
8614
8615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 * This is called during layout when the size of this view has changed. If
8617 * you were just added to the view hierarchy, you're called with the old
8618 * values of 0.
8619 *
8620 * @param w Current width of this view.
8621 * @param h Current height of this view.
8622 * @param oldw Old width of this view.
8623 * @param oldh Old height of this view.
8624 */
8625 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8626 }
8627
8628 /**
8629 * Called by draw to draw the child views. This may be overridden
8630 * by derived classes to gain control just before its children are drawn
8631 * (but after its own view has been drawn).
8632 * @param canvas the canvas on which to draw the view
8633 */
8634 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 }
8637
8638 /**
8639 * Gets the parent of this view. Note that the parent is a
8640 * ViewParent and not necessarily a View.
8641 *
8642 * @return Parent of this view.
8643 */
8644 public final ViewParent getParent() {
8645 return mParent;
8646 }
8647
8648 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008649 * Set the horizontal scrolled position of your view. This will cause a call to
8650 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8651 * invalidated.
8652 * @param value the x position to scroll to
8653 */
8654 public void setScrollX(int value) {
8655 scrollTo(value, mScrollY);
8656 }
8657
8658 /**
8659 * Set the vertical scrolled position of your view. This will cause a call to
8660 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8661 * invalidated.
8662 * @param value the y position to scroll to
8663 */
8664 public void setScrollY(int value) {
8665 scrollTo(mScrollX, value);
8666 }
8667
8668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 * Return the scrolled left position of this view. This is the left edge of
8670 * the displayed part of your view. You do not need to draw any pixels
8671 * farther left, since those are outside of the frame of your view on
8672 * screen.
8673 *
8674 * @return The left edge of the displayed part of your view, in pixels.
8675 */
8676 public final int getScrollX() {
8677 return mScrollX;
8678 }
8679
8680 /**
8681 * Return the scrolled top position of this view. This is the top edge of
8682 * the displayed part of your view. You do not need to draw any pixels above
8683 * it, since those are outside of the frame of your view on screen.
8684 *
8685 * @return The top edge of the displayed part of your view, in pixels.
8686 */
8687 public final int getScrollY() {
8688 return mScrollY;
8689 }
8690
8691 /**
8692 * Return the width of the your view.
8693 *
8694 * @return The width of your view, in pixels.
8695 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008696 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 public final int getWidth() {
8698 return mRight - mLeft;
8699 }
8700
8701 /**
8702 * Return the height of your view.
8703 *
8704 * @return The height of your view, in pixels.
8705 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008706 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 public final int getHeight() {
8708 return mBottom - mTop;
8709 }
8710
8711 /**
8712 * Return the visible drawing bounds of your view. Fills in the output
8713 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008714 * getWidth(), and getHeight(). These bounds do not account for any
8715 * transformation properties currently set on the view, such as
8716 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 *
8718 * @param outRect The (scrolled) drawing bounds of the view.
8719 */
8720 public void getDrawingRect(Rect outRect) {
8721 outRect.left = mScrollX;
8722 outRect.top = mScrollY;
8723 outRect.right = mScrollX + (mRight - mLeft);
8724 outRect.bottom = mScrollY + (mBottom - mTop);
8725 }
8726
8727 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008728 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8729 * raw width component (that is the result is masked by
8730 * {@link #MEASURED_SIZE_MASK}).
8731 *
8732 * @return The raw measured width of this view.
8733 */
8734 public final int getMeasuredWidth() {
8735 return mMeasuredWidth & MEASURED_SIZE_MASK;
8736 }
8737
8738 /**
8739 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008740 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008741 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008742 * This should be used during measurement and layout calculations only. Use
8743 * {@link #getWidth()} to see how wide a view is after layout.
8744 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008745 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008747 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 return mMeasuredWidth;
8749 }
8750
8751 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008752 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8753 * raw width component (that is the result is masked by
8754 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008756 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 */
8758 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008759 return mMeasuredHeight & MEASURED_SIZE_MASK;
8760 }
8761
8762 /**
8763 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008764 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008765 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8766 * This should be used during measurement and layout calculations only. Use
8767 * {@link #getHeight()} to see how wide a view is after layout.
8768 *
8769 * @return The measured width of this view as a bit mask.
8770 */
8771 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 return mMeasuredHeight;
8773 }
8774
8775 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008776 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8777 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8778 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8779 * and the height component is at the shifted bits
8780 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8781 */
8782 public final int getMeasuredState() {
8783 return (mMeasuredWidth&MEASURED_STATE_MASK)
8784 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8785 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8786 }
8787
8788 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008789 * The transform matrix of this view, which is calculated based on the current
8790 * roation, scale, and pivot properties.
8791 *
8792 * @see #getRotation()
8793 * @see #getScaleX()
8794 * @see #getScaleY()
8795 * @see #getPivotX()
8796 * @see #getPivotY()
8797 * @return The current transform matrix for the view
8798 */
8799 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008800 if (mTransformationInfo != null) {
8801 updateMatrix();
8802 return mTransformationInfo.mMatrix;
8803 }
8804 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008805 }
8806
8807 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008808 * Utility function to determine if the value is far enough away from zero to be
8809 * considered non-zero.
8810 * @param value A floating point value to check for zero-ness
8811 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8812 */
8813 private static boolean nonzero(float value) {
8814 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8815 }
8816
8817 /**
Jeff Brown86671742010-09-30 20:00:15 -07008818 * Returns true if the transform matrix is the identity matrix.
8819 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008820 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008821 * @return True if the transform matrix is the identity matrix, false otherwise.
8822 */
Jeff Brown86671742010-09-30 20:00:15 -07008823 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008824 if (mTransformationInfo != null) {
8825 updateMatrix();
8826 return mTransformationInfo.mMatrixIsIdentity;
8827 }
8828 return true;
8829 }
8830
8831 void ensureTransformationInfo() {
8832 if (mTransformationInfo == null) {
8833 mTransformationInfo = new TransformationInfo();
8834 }
Jeff Brown86671742010-09-30 20:00:15 -07008835 }
8836
8837 /**
8838 * Recomputes the transform matrix if necessary.
8839 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008840 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008841 final TransformationInfo info = mTransformationInfo;
8842 if (info == null) {
8843 return;
8844 }
8845 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008846 // transform-related properties have changed since the last time someone
8847 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008848
8849 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008850 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008851 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8852 info.mPrevWidth = mRight - mLeft;
8853 info.mPrevHeight = mBottom - mTop;
8854 info.mPivotX = info.mPrevWidth / 2f;
8855 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008856 }
8857 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008858 info.mMatrix.reset();
8859 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8860 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8861 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8862 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008863 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008864 if (info.mCamera == null) {
8865 info.mCamera = new Camera();
8866 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008867 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008868 info.mCamera.save();
8869 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8870 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8871 info.mCamera.getMatrix(info.matrix3D);
8872 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8873 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8874 info.mPivotY + info.mTranslationY);
8875 info.mMatrix.postConcat(info.matrix3D);
8876 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008877 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008878 info.mMatrixDirty = false;
8879 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8880 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008881 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008882 }
8883
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008884 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008885 * Utility method to retrieve the inverse of the current mMatrix property.
8886 * We cache the matrix to avoid recalculating it when transform properties
8887 * have not changed.
8888 *
8889 * @return The inverse of the current matrix of this view.
8890 */
Jeff Brown86671742010-09-30 20:00:15 -07008891 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008892 final TransformationInfo info = mTransformationInfo;
8893 if (info != null) {
8894 updateMatrix();
8895 if (info.mInverseMatrixDirty) {
8896 if (info.mInverseMatrix == null) {
8897 info.mInverseMatrix = new Matrix();
8898 }
8899 info.mMatrix.invert(info.mInverseMatrix);
8900 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008901 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008902 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008903 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008904 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008905 }
8906
8907 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008908 * Gets the distance along the Z axis from the camera to this view.
8909 *
8910 * @see #setCameraDistance(float)
8911 *
8912 * @return The distance along the Z axis.
8913 */
8914 public float getCameraDistance() {
8915 ensureTransformationInfo();
8916 final float dpi = mResources.getDisplayMetrics().densityDpi;
8917 final TransformationInfo info = mTransformationInfo;
8918 if (info.mCamera == null) {
8919 info.mCamera = new Camera();
8920 info.matrix3D = new Matrix();
8921 }
8922 return -(info.mCamera.getLocationZ() * dpi);
8923 }
8924
8925 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008926 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8927 * views are drawn) from the camera to this view. The camera's distance
8928 * affects 3D transformations, for instance rotations around the X and Y
8929 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008930 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008931 * use a camera distance that's greater than the height (X axis rotation) or
8932 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008933 *
Romain Guya5364ee2011-02-24 14:46:04 -08008934 * <p>The distance of the camera from the view plane can have an affect on the
8935 * perspective distortion of the view when it is rotated around the x or y axis.
8936 * For example, a large distance will result in a large viewing angle, and there
8937 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008938 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008939 * also result in some drawing artifacts if the rotated view ends up partially
8940 * behind the camera (which is why the recommendation is to use a distance at
8941 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008942 *
Romain Guya5364ee2011-02-24 14:46:04 -08008943 * <p>The distance is expressed in "depth pixels." The default distance depends
8944 * on the screen density. For instance, on a medium density display, the
8945 * default distance is 1280. On a high density display, the default distance
8946 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008947 *
Romain Guya5364ee2011-02-24 14:46:04 -08008948 * <p>If you want to specify a distance that leads to visually consistent
8949 * results across various densities, use the following formula:</p>
8950 * <pre>
8951 * float scale = context.getResources().getDisplayMetrics().density;
8952 * view.setCameraDistance(distance * scale);
8953 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008954 *
Romain Guya5364ee2011-02-24 14:46:04 -08008955 * <p>The density scale factor of a high density display is 1.5,
8956 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008957 *
Romain Guya5364ee2011-02-24 14:46:04 -08008958 * @param distance The distance in "depth pixels", if negative the opposite
8959 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008960 *
8961 * @see #setRotationX(float)
8962 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008963 */
8964 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008965 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008966
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008967 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008968 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008969 final TransformationInfo info = mTransformationInfo;
8970 if (info.mCamera == null) {
8971 info.mCamera = new Camera();
8972 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008973 }
8974
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008975 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8976 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008977
Chet Haase9d1992d2012-03-13 11:03:25 -07008978 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008979 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008980 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008981 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008982 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008983 // View was rejected last time it was drawn by its parent; this may have changed
8984 invalidateParentIfNeeded();
8985 }
Romain Guya5364ee2011-02-24 14:46:04 -08008986 }
8987
8988 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008989 * The degrees that the view is rotated around the pivot point.
8990 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008991 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008992 * @see #getPivotX()
8993 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008994 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008995 * @return The degrees of rotation.
8996 */
Chet Haasea5531132012-02-02 13:41:44 -08008997 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008998 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008999 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009000 }
9001
9002 /**
Chet Haase897247b2010-09-09 14:54:47 -07009003 * Sets the degrees that the view is rotated around the pivot point. Increasing values
9004 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07009005 *
9006 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009007 *
9008 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07009009 * @see #getPivotX()
9010 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009011 * @see #setRotationX(float)
9012 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08009013 *
9014 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07009015 */
9016 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009017 ensureTransformationInfo();
9018 final TransformationInfo info = mTransformationInfo;
9019 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009020 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009021 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009022 info.mRotation = rotation;
9023 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009024 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009025 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009026 mDisplayList.setRotation(rotation);
9027 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009028 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009029 // View was rejected last time it was drawn by its parent; this may have changed
9030 invalidateParentIfNeeded();
9031 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009032 }
9033 }
9034
9035 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009036 * The degrees that the view is rotated around the vertical axis through the pivot point.
9037 *
9038 * @see #getPivotX()
9039 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009040 * @see #setRotationY(float)
9041 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009042 * @return The degrees of Y rotation.
9043 */
Chet Haasea5531132012-02-02 13:41:44 -08009044 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009045 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009046 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009047 }
9048
9049 /**
Chet Haase897247b2010-09-09 14:54:47 -07009050 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9051 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9052 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009053 *
Romain Guya5364ee2011-02-24 14:46:04 -08009054 * When rotating large views, it is recommended to adjust the camera distance
9055 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009056 *
9057 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009058 *
9059 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009060 * @see #getPivotX()
9061 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009062 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009063 * @see #setRotationX(float)
9064 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009065 *
9066 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009067 */
9068 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009069 ensureTransformationInfo();
9070 final TransformationInfo info = mTransformationInfo;
9071 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009072 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009073 info.mRotationY = rotationY;
9074 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009075 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009076 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009077 mDisplayList.setRotationY(rotationY);
9078 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009079 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009080 // View was rejected last time it was drawn by its parent; this may have changed
9081 invalidateParentIfNeeded();
9082 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009083 }
9084 }
9085
9086 /**
9087 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9088 *
9089 * @see #getPivotX()
9090 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009091 * @see #setRotationX(float)
9092 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009093 * @return The degrees of X rotation.
9094 */
Chet Haasea5531132012-02-02 13:41:44 -08009095 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009096 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009097 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009098 }
9099
9100 /**
Chet Haase897247b2010-09-09 14:54:47 -07009101 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9102 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9103 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009104 *
Romain Guya5364ee2011-02-24 14:46:04 -08009105 * When rotating large views, it is recommended to adjust the camera distance
9106 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009107 *
9108 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009109 *
9110 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009111 * @see #getPivotX()
9112 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009113 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009114 * @see #setRotationY(float)
9115 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009116 *
9117 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009118 */
9119 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009120 ensureTransformationInfo();
9121 final TransformationInfo info = mTransformationInfo;
9122 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009123 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009124 info.mRotationX = rotationX;
9125 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009126 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009127 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009128 mDisplayList.setRotationX(rotationX);
9129 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009130 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009131 // View was rejected last time it was drawn by its parent; this may have changed
9132 invalidateParentIfNeeded();
9133 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009134 }
9135 }
9136
9137 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009138 * 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, the default, means that no scaling is applied.
9140 *
Joe Onorato93162322010-09-16 15:42:01 -04009141 * <p>By default, this is 1.0f.
9142 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009143 * @see #getPivotX()
9144 * @see #getPivotY()
9145 * @return The scaling factor.
9146 */
Chet Haasea5531132012-02-02 13:41:44 -08009147 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009148 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009149 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009150 }
9151
9152 /**
9153 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9154 * the view's unscaled width. A value of 1 means that no scaling is applied.
9155 *
9156 * @param scaleX The scaling factor.
9157 * @see #getPivotX()
9158 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009159 *
9160 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009161 */
9162 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009163 ensureTransformationInfo();
9164 final TransformationInfo info = mTransformationInfo;
9165 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009166 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009167 info.mScaleX = scaleX;
9168 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009169 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009170 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009171 mDisplayList.setScaleX(scaleX);
9172 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009173 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009174 // View was rejected last time it was drawn by its parent; this may have changed
9175 invalidateParentIfNeeded();
9176 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009177 }
9178 }
9179
9180 /**
9181 * The amount that the view is scaled in y around the pivot point, as a proportion of
9182 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9183 *
Joe Onorato93162322010-09-16 15:42:01 -04009184 * <p>By default, this is 1.0f.
9185 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009186 * @see #getPivotX()
9187 * @see #getPivotY()
9188 * @return The scaling factor.
9189 */
Chet Haasea5531132012-02-02 13:41:44 -08009190 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009191 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009192 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009193 }
9194
9195 /**
9196 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9197 * the view's unscaled width. A value of 1 means that no scaling is applied.
9198 *
9199 * @param scaleY The scaling factor.
9200 * @see #getPivotX()
9201 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009202 *
9203 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009204 */
9205 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009206 ensureTransformationInfo();
9207 final TransformationInfo info = mTransformationInfo;
9208 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009209 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009210 info.mScaleY = scaleY;
9211 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009212 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009213 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009214 mDisplayList.setScaleY(scaleY);
9215 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009216 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009217 // View was rejected last time it was drawn by its parent; this may have changed
9218 invalidateParentIfNeeded();
9219 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009220 }
9221 }
9222
9223 /**
9224 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9225 * and {@link #setScaleX(float) scaled}.
9226 *
9227 * @see #getRotation()
9228 * @see #getScaleX()
9229 * @see #getScaleY()
9230 * @see #getPivotY()
9231 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009232 *
9233 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009234 */
Chet Haasea5531132012-02-02 13:41:44 -08009235 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009236 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009237 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009238 }
9239
9240 /**
9241 * Sets the x location of the point around which the view is
9242 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009243 * By default, the pivot point is centered on the object.
9244 * Setting this property disables this behavior and causes the view to use only the
9245 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009246 *
9247 * @param pivotX The x location of the pivot point.
9248 * @see #getRotation()
9249 * @see #getScaleX()
9250 * @see #getScaleY()
9251 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009252 *
9253 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009254 */
9255 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009256 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009257 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009258 final TransformationInfo info = mTransformationInfo;
9259 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009260 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009261 info.mPivotX = pivotX;
9262 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009263 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009264 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009265 mDisplayList.setPivotX(pivotX);
9266 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009267 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009268 // View was rejected last time it was drawn by its parent; this may have changed
9269 invalidateParentIfNeeded();
9270 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009271 }
9272 }
9273
9274 /**
9275 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9276 * and {@link #setScaleY(float) scaled}.
9277 *
9278 * @see #getRotation()
9279 * @see #getScaleX()
9280 * @see #getScaleY()
9281 * @see #getPivotY()
9282 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009283 *
9284 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009285 */
Chet Haasea5531132012-02-02 13:41:44 -08009286 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009287 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009288 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009289 }
9290
9291 /**
9292 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009293 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9294 * Setting this property disables this behavior and causes the view to use only the
9295 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009296 *
9297 * @param pivotY The y location of the pivot point.
9298 * @see #getRotation()
9299 * @see #getScaleX()
9300 * @see #getScaleY()
9301 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009302 *
9303 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009304 */
9305 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009306 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009307 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009308 final TransformationInfo info = mTransformationInfo;
9309 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009310 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009311 info.mPivotY = pivotY;
9312 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009313 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009314 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009315 mDisplayList.setPivotY(pivotY);
9316 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009317 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009318 // View was rejected last time it was drawn by its parent; this may have changed
9319 invalidateParentIfNeeded();
9320 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009321 }
9322 }
9323
9324 /**
9325 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9326 * completely transparent and 1 means the view is completely opaque.
9327 *
Joe Onorato93162322010-09-16 15:42:01 -04009328 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009329 * @return The opacity of the view.
9330 */
Chet Haasea5531132012-02-02 13:41:44 -08009331 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009332 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009333 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009334 }
9335
9336 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009337 * Returns whether this View has content which overlaps. This function, intended to be
9338 * overridden by specific View types, is an optimization when alpha is set on a view. If
9339 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9340 * and then composited it into place, which can be expensive. If the view has no overlapping
9341 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9342 * An example of overlapping rendering is a TextView with a background image, such as a
9343 * Button. An example of non-overlapping rendering is a TextView with no background, or
9344 * an ImageView with only the foreground image. The default implementation returns true;
9345 * subclasses should override if they have cases which can be optimized.
9346 *
9347 * @return true if the content in this view might overlap, false otherwise.
9348 */
9349 public boolean hasOverlappingRendering() {
9350 return true;
9351 }
9352
9353 /**
Romain Guy171c5922011-01-06 10:04:23 -08009354 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9355 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009356 *
Romain Guy171c5922011-01-06 10:04:23 -08009357 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9358 * responsible for applying the opacity itself. Otherwise, calling this method is
9359 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009360 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009361 *
Chet Haasea5531132012-02-02 13:41:44 -08009362 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9363 * performance implications. It is generally best to use the alpha property sparingly and
9364 * transiently, as in the case of fading animations.</p>
9365 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009366 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009367 *
Joe Malin32736f02011-01-19 16:14:20 -08009368 * @see #setLayerType(int, android.graphics.Paint)
9369 *
Chet Haase73066682010-11-29 15:55:32 -08009370 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009371 */
9372 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009373 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009374 if (mTransformationInfo.mAlpha != alpha) {
9375 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009376 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009377 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009378 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009379 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009380 invalidate(true);
9381 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009382 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009383 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009384 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009385 mDisplayList.setAlpha(alpha);
9386 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009387 }
Chet Haaseed032702010-10-01 14:05:54 -07009388 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009389 }
9390
9391 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009392 * Faster version of setAlpha() which performs the same steps except there are
9393 * no calls to invalidate(). The caller of this function should perform proper invalidation
9394 * on the parent and this object. The return value indicates whether the subclass handles
9395 * alpha (the return value for onSetAlpha()).
9396 *
9397 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009398 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9399 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009400 */
9401 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009402 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009403 if (mTransformationInfo.mAlpha != alpha) {
9404 mTransformationInfo.mAlpha = alpha;
9405 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9406 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009407 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009408 return true;
9409 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009410 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009411 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009412 mDisplayList.setAlpha(alpha);
9413 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009414 }
Chet Haasea00f3862011-02-22 06:34:40 -08009415 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009416 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009417 }
9418
9419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 * Top position of this view relative to its parent.
9421 *
9422 * @return The top of this view, in pixels.
9423 */
9424 @ViewDebug.CapturedViewProperty
9425 public final int getTop() {
9426 return mTop;
9427 }
9428
9429 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009430 * Sets the top position of this view relative to its parent. This method is meant to be called
9431 * by the layout system and should not generally be called otherwise, because the property
9432 * may be changed at any time by the layout.
9433 *
9434 * @param top The top of this view, in pixels.
9435 */
9436 public final void setTop(int top) {
9437 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009438 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009439 final boolean matrixIsIdentity = mTransformationInfo == null
9440 || mTransformationInfo.mMatrixIsIdentity;
9441 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009442 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009443 int minTop;
9444 int yLoc;
9445 if (top < mTop) {
9446 minTop = top;
9447 yLoc = top - mTop;
9448 } else {
9449 minTop = mTop;
9450 yLoc = 0;
9451 }
Chet Haasee9140a72011-02-16 16:23:29 -08009452 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009453 }
9454 } else {
9455 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009456 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009457 }
9458
Chet Haaseed032702010-10-01 14:05:54 -07009459 int width = mRight - mLeft;
9460 int oldHeight = mBottom - mTop;
9461
Chet Haase21cd1382010-09-01 17:42:29 -07009462 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009463 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009464 mDisplayList.setTop(mTop);
9465 }
Chet Haase21cd1382010-09-01 17:42:29 -07009466
Chet Haaseed032702010-10-01 14:05:54 -07009467 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9468
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009469 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009470 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009471 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009472 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009473 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009474 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009475 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009476 }
Chet Haase55dbb652010-12-21 20:15:08 -08009477 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009478 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009479 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009480 // View was rejected last time it was drawn by its parent; this may have changed
9481 invalidateParentIfNeeded();
9482 }
Chet Haase21cd1382010-09-01 17:42:29 -07009483 }
9484 }
9485
9486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 * Bottom position of this view relative to its parent.
9488 *
9489 * @return The bottom of this view, in pixels.
9490 */
9491 @ViewDebug.CapturedViewProperty
9492 public final int getBottom() {
9493 return mBottom;
9494 }
9495
9496 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009497 * True if this view has changed since the last time being drawn.
9498 *
9499 * @return The dirty state of this view.
9500 */
9501 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009502 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009503 }
9504
9505 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009506 * Sets the bottom position of this view relative to its parent. This method is meant to be
9507 * called by the layout system and should not generally be called otherwise, because the
9508 * property may be changed at any time by the layout.
9509 *
9510 * @param bottom The bottom of this view, in pixels.
9511 */
9512 public final void setBottom(int bottom) {
9513 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009514 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009515 final boolean matrixIsIdentity = mTransformationInfo == null
9516 || mTransformationInfo.mMatrixIsIdentity;
9517 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009518 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009519 int maxBottom;
9520 if (bottom < mBottom) {
9521 maxBottom = mBottom;
9522 } else {
9523 maxBottom = bottom;
9524 }
Chet Haasee9140a72011-02-16 16:23:29 -08009525 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009526 }
9527 } else {
9528 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009529 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009530 }
9531
Chet Haaseed032702010-10-01 14:05:54 -07009532 int width = mRight - mLeft;
9533 int oldHeight = mBottom - mTop;
9534
Chet Haase21cd1382010-09-01 17:42:29 -07009535 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009536 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009537 mDisplayList.setBottom(mBottom);
9538 }
Chet Haase21cd1382010-09-01 17:42:29 -07009539
Chet Haaseed032702010-10-01 14:05:54 -07009540 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9541
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009542 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009543 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009544 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009545 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009546 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009547 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009548 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009549 }
Chet Haase55dbb652010-12-21 20:15:08 -08009550 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009551 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009552 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009553 // View was rejected last time it was drawn by its parent; this may have changed
9554 invalidateParentIfNeeded();
9555 }
Chet Haase21cd1382010-09-01 17:42:29 -07009556 }
9557 }
9558
9559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 * Left position of this view relative to its parent.
9561 *
9562 * @return The left edge of this view, in pixels.
9563 */
9564 @ViewDebug.CapturedViewProperty
9565 public final int getLeft() {
9566 return mLeft;
9567 }
9568
9569 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009570 * Sets the left position of this view relative to its parent. This method is meant to be called
9571 * by the layout system and should not generally be called otherwise, because the property
9572 * may be changed at any time by the layout.
9573 *
9574 * @param left The bottom of this view, in pixels.
9575 */
9576 public final void setLeft(int left) {
9577 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009578 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009579 final boolean matrixIsIdentity = mTransformationInfo == null
9580 || mTransformationInfo.mMatrixIsIdentity;
9581 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009582 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009583 int minLeft;
9584 int xLoc;
9585 if (left < mLeft) {
9586 minLeft = left;
9587 xLoc = left - mLeft;
9588 } else {
9589 minLeft = mLeft;
9590 xLoc = 0;
9591 }
Chet Haasee9140a72011-02-16 16:23:29 -08009592 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009593 }
9594 } else {
9595 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009596 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009597 }
9598
Chet Haaseed032702010-10-01 14:05:54 -07009599 int oldWidth = mRight - mLeft;
9600 int height = mBottom - mTop;
9601
Chet Haase21cd1382010-09-01 17:42:29 -07009602 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009603 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009604 mDisplayList.setLeft(left);
9605 }
Chet Haase21cd1382010-09-01 17:42:29 -07009606
Chet Haaseed032702010-10-01 14:05:54 -07009607 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9608
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009609 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009610 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009611 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009612 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009613 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009614 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009615 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009616 }
Chet Haase55dbb652010-12-21 20:15:08 -08009617 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009618 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009619 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009620 // View was rejected last time it was drawn by its parent; this may have changed
9621 invalidateParentIfNeeded();
9622 }
Chet Haase21cd1382010-09-01 17:42:29 -07009623 }
9624 }
9625
9626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 * Right position of this view relative to its parent.
9628 *
9629 * @return The right edge of this view, in pixels.
9630 */
9631 @ViewDebug.CapturedViewProperty
9632 public final int getRight() {
9633 return mRight;
9634 }
9635
9636 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009637 * Sets the right position of this view relative to its parent. This method is meant to be called
9638 * by the layout system and should not generally be called otherwise, because the property
9639 * may be changed at any time by the layout.
9640 *
9641 * @param right The bottom of this view, in pixels.
9642 */
9643 public final void setRight(int right) {
9644 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009645 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009646 final boolean matrixIsIdentity = mTransformationInfo == null
9647 || mTransformationInfo.mMatrixIsIdentity;
9648 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009649 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009650 int maxRight;
9651 if (right < mRight) {
9652 maxRight = mRight;
9653 } else {
9654 maxRight = right;
9655 }
Chet Haasee9140a72011-02-16 16:23:29 -08009656 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009657 }
9658 } else {
9659 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009660 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009661 }
9662
Chet Haaseed032702010-10-01 14:05:54 -07009663 int oldWidth = mRight - mLeft;
9664 int height = mBottom - mTop;
9665
Chet Haase21cd1382010-09-01 17:42:29 -07009666 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009667 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009668 mDisplayList.setRight(mRight);
9669 }
Chet Haase21cd1382010-09-01 17:42:29 -07009670
Chet Haaseed032702010-10-01 14:05:54 -07009671 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9672
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009673 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009674 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009675 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009676 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009677 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009678 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009679 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009680 }
Chet Haase55dbb652010-12-21 20:15:08 -08009681 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009682 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009683 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009684 // View was rejected last time it was drawn by its parent; this may have changed
9685 invalidateParentIfNeeded();
9686 }
Chet Haase21cd1382010-09-01 17:42:29 -07009687 }
9688 }
9689
9690 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009691 * The visual x position of this view, in pixels. This is equivalent to the
9692 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009693 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009694 *
Chet Haasedf030d22010-07-30 17:22:38 -07009695 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009696 */
Chet Haasea5531132012-02-02 13:41:44 -08009697 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009698 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009699 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009700 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009701
Chet Haasedf030d22010-07-30 17:22:38 -07009702 /**
9703 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9704 * {@link #setTranslationX(float) translationX} property to be the difference between
9705 * the x value passed in and the current {@link #getLeft() left} property.
9706 *
9707 * @param x The visual x position of this view, in pixels.
9708 */
9709 public void setX(float x) {
9710 setTranslationX(x - mLeft);
9711 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009712
Chet Haasedf030d22010-07-30 17:22:38 -07009713 /**
9714 * The visual y position of this view, in pixels. This is equivalent to the
9715 * {@link #setTranslationY(float) translationY} property plus the current
9716 * {@link #getTop() top} property.
9717 *
9718 * @return The visual y position of this view, in pixels.
9719 */
Chet Haasea5531132012-02-02 13:41:44 -08009720 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009721 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009722 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009723 }
9724
9725 /**
9726 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9727 * {@link #setTranslationY(float) translationY} property to be the difference between
9728 * the y value passed in and the current {@link #getTop() top} property.
9729 *
9730 * @param y The visual y position of this view, in pixels.
9731 */
9732 public void setY(float y) {
9733 setTranslationY(y - mTop);
9734 }
9735
9736
9737 /**
9738 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9739 * This position is post-layout, in addition to wherever the object's
9740 * layout placed it.
9741 *
9742 * @return The horizontal position of this view relative to its left position, in pixels.
9743 */
Chet Haasea5531132012-02-02 13:41:44 -08009744 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009745 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009746 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009747 }
9748
9749 /**
9750 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9751 * This effectively positions the object post-layout, in addition to wherever the object's
9752 * layout placed it.
9753 *
9754 * @param translationX The horizontal position of this view relative to its left position,
9755 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009756 *
9757 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009758 */
9759 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009760 ensureTransformationInfo();
9761 final TransformationInfo info = mTransformationInfo;
9762 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009763 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009764 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009765 info.mTranslationX = translationX;
9766 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009767 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009768 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009769 mDisplayList.setTranslationX(translationX);
9770 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009771 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009772 // View was rejected last time it was drawn by its parent; this may have changed
9773 invalidateParentIfNeeded();
9774 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009775 }
9776 }
9777
9778 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009779 * The horizontal location of this view relative to its {@link #getTop() top} position.
9780 * This position is post-layout, in addition to wherever the object's
9781 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009782 *
Chet Haasedf030d22010-07-30 17:22:38 -07009783 * @return The vertical position of this view relative to its top position,
9784 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009785 */
Chet Haasea5531132012-02-02 13:41:44 -08009786 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009787 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009788 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009789 }
9790
9791 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009792 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9793 * This effectively positions the object post-layout, in addition to wherever the object's
9794 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009795 *
Chet Haasedf030d22010-07-30 17:22:38 -07009796 * @param translationY The vertical position of this view relative to its top position,
9797 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009798 *
9799 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009800 */
Chet Haasedf030d22010-07-30 17:22:38 -07009801 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009802 ensureTransformationInfo();
9803 final TransformationInfo info = mTransformationInfo;
9804 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009805 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009806 info.mTranslationY = translationY;
9807 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009808 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009809 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009810 mDisplayList.setTranslationY(translationY);
9811 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009812 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009813 // View was rejected last time it was drawn by its parent; this may have changed
9814 invalidateParentIfNeeded();
9815 }
Chet Haasedf030d22010-07-30 17:22:38 -07009816 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009817 }
9818
9819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 * Hit rectangle in parent's coordinates
9821 *
9822 * @param outRect The hit rectangle of the view.
9823 */
9824 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009825 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009826 final TransformationInfo info = mTransformationInfo;
9827 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009828 outRect.set(mLeft, mTop, mRight, mBottom);
9829 } else {
9830 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009831 tmpRect.set(-info.mPivotX, -info.mPivotY,
9832 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9833 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009834 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9835 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009836 }
9837 }
9838
9839 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009840 * Determines whether the given point, in local coordinates is inside the view.
9841 */
9842 /*package*/ final boolean pointInView(float localX, float localY) {
9843 return localX >= 0 && localX < (mRight - mLeft)
9844 && localY >= 0 && localY < (mBottom - mTop);
9845 }
9846
9847 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009848 * Utility method to determine whether the given point, in local coordinates,
9849 * is inside the view, where the area of the view is expanded by the slop factor.
9850 * This method is called while processing touch-move events to determine if the event
9851 * is still within the view.
9852 */
9853 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009854 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009855 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 }
9857
9858 /**
9859 * When a view has focus and the user navigates away from it, the next view is searched for
9860 * starting from the rectangle filled in by this method.
9861 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009862 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009863 * of the view. However, if your view maintains some idea of internal selection,
9864 * such as a cursor, or a selected row or column, you should override this method and
9865 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 *
9867 * @param r The rectangle to fill in, in this view's coordinates.
9868 */
9869 public void getFocusedRect(Rect r) {
9870 getDrawingRect(r);
9871 }
9872
9873 /**
9874 * If some part of this view is not clipped by any of its parents, then
9875 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009876 * coordinates (without taking possible View rotations into account), offset
9877 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9878 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 *
9880 * @param r If true is returned, r holds the global coordinates of the
9881 * visible portion of this view.
9882 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9883 * between this view and its root. globalOffet may be null.
9884 * @return true if r is non-empty (i.e. part of the view is visible at the
9885 * root level.
9886 */
9887 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9888 int width = mRight - mLeft;
9889 int height = mBottom - mTop;
9890 if (width > 0 && height > 0) {
9891 r.set(0, 0, width, height);
9892 if (globalOffset != null) {
9893 globalOffset.set(-mScrollX, -mScrollY);
9894 }
9895 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9896 }
9897 return false;
9898 }
9899
9900 public final boolean getGlobalVisibleRect(Rect r) {
9901 return getGlobalVisibleRect(r, null);
9902 }
9903
9904 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009905 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009906 if (getGlobalVisibleRect(r, offset)) {
9907 r.offset(-offset.x, -offset.y); // make r local
9908 return true;
9909 }
9910 return false;
9911 }
9912
9913 /**
9914 * Offset this view's vertical location by the specified number of pixels.
9915 *
9916 * @param offset the number of pixels to offset the view by
9917 */
9918 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009919 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009920 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009921 final boolean matrixIsIdentity = mTransformationInfo == null
9922 || mTransformationInfo.mMatrixIsIdentity;
9923 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009924 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009925 invalidateViewProperty(false, false);
9926 } else {
9927 final ViewParent p = mParent;
9928 if (p != null && mAttachInfo != null) {
9929 final Rect r = mAttachInfo.mTmpInvalRect;
9930 int minTop;
9931 int maxBottom;
9932 int yLoc;
9933 if (offset < 0) {
9934 minTop = mTop + offset;
9935 maxBottom = mBottom;
9936 yLoc = offset;
9937 } else {
9938 minTop = mTop;
9939 maxBottom = mBottom + offset;
9940 yLoc = 0;
9941 }
9942 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9943 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009944 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009945 }
9946 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009947 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009948 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009949
Chet Haasec3aa3612010-06-17 08:50:37 -07009950 mTop += offset;
9951 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009952 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009953 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009954 invalidateViewProperty(false, false);
9955 } else {
9956 if (!matrixIsIdentity) {
9957 invalidateViewProperty(false, true);
9958 }
9959 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009960 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 }
9963
9964 /**
9965 * Offset this view's horizontal location by the specified amount of pixels.
9966 *
9967 * @param offset the numer of pixels to offset the view by
9968 */
9969 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009970 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009971 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009972 final boolean matrixIsIdentity = mTransformationInfo == null
9973 || mTransformationInfo.mMatrixIsIdentity;
9974 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009975 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009976 invalidateViewProperty(false, false);
9977 } else {
9978 final ViewParent p = mParent;
9979 if (p != null && mAttachInfo != null) {
9980 final Rect r = mAttachInfo.mTmpInvalRect;
9981 int minLeft;
9982 int maxRight;
9983 if (offset < 0) {
9984 minLeft = mLeft + offset;
9985 maxRight = mRight;
9986 } else {
9987 minLeft = mLeft;
9988 maxRight = mRight + offset;
9989 }
9990 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9991 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009992 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009993 }
9994 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009995 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009996 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009997
Chet Haasec3aa3612010-06-17 08:50:37 -07009998 mLeft += offset;
9999 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -070010000 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080010001 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -070010002 invalidateViewProperty(false, false);
10003 } else {
10004 if (!matrixIsIdentity) {
10005 invalidateViewProperty(false, true);
10006 }
10007 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -080010008 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 }
10011
10012 /**
10013 * Get the LayoutParams associated with this view. All views should have
10014 * layout parameters. These supply parameters to the <i>parent</i> of this
10015 * view specifying how it should be arranged. There are many subclasses of
10016 * ViewGroup.LayoutParams, and these correspond to the different subclasses
10017 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -080010018 *
10019 * This method may return null if this View is not attached to a parent
10020 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
10021 * was not invoked successfully. When a View is attached to a parent
10022 * ViewGroup, this method must not return null.
10023 *
10024 * @return The LayoutParams associated with this view, or null if no
10025 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010027 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 public ViewGroup.LayoutParams getLayoutParams() {
10029 return mLayoutParams;
10030 }
10031
10032 /**
10033 * Set the layout parameters associated with this view. These supply
10034 * parameters to the <i>parent</i> of this view specifying how it should be
10035 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10036 * correspond to the different subclasses of ViewGroup that are responsible
10037 * for arranging their children.
10038 *
Romain Guy01c174b2011-02-22 11:51:06 -080010039 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 */
10041 public void setLayoutParams(ViewGroup.LayoutParams params) {
10042 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010043 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010044 }
10045 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010046 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010047 if (mParent instanceof ViewGroup) {
10048 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 requestLayout();
10051 }
10052
10053 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010054 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010055 *
10056 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010057 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010058 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010059 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010060 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010061 }
10062 }
10063
10064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 * Set the scrolled position of your view. This will cause a call to
10066 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10067 * invalidated.
10068 * @param x the x position to scroll to
10069 * @param y the y position to scroll to
10070 */
10071 public void scrollTo(int x, int y) {
10072 if (mScrollX != x || mScrollY != y) {
10073 int oldX = mScrollX;
10074 int oldY = mScrollY;
10075 mScrollX = x;
10076 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010077 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010079 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010080 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 }
10083 }
10084
10085 /**
10086 * Move the scrolled position of your view. This will cause a call to
10087 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10088 * invalidated.
10089 * @param x the amount of pixels to scroll by horizontally
10090 * @param y the amount of pixels to scroll by vertically
10091 */
10092 public void scrollBy(int x, int y) {
10093 scrollTo(mScrollX + x, mScrollY + y);
10094 }
10095
10096 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010097 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10098 * animation to fade the scrollbars out after a default delay. If a subclass
10099 * provides animated scrolling, the start delay should equal the duration
10100 * of the scrolling animation.</p>
10101 *
10102 * <p>The animation starts only if at least one of the scrollbars is
10103 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10104 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10105 * this method returns true, and false otherwise. If the animation is
10106 * started, this method calls {@link #invalidate()}; in that case the
10107 * caller should not call {@link #invalidate()}.</p>
10108 *
10109 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010110 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010111 *
10112 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10113 * and {@link #scrollTo(int, int)}.</p>
10114 *
10115 * @return true if the animation is played, false otherwise
10116 *
10117 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010118 * @see #scrollBy(int, int)
10119 * @see #scrollTo(int, int)
10120 * @see #isHorizontalScrollBarEnabled()
10121 * @see #isVerticalScrollBarEnabled()
10122 * @see #setHorizontalScrollBarEnabled(boolean)
10123 * @see #setVerticalScrollBarEnabled(boolean)
10124 */
10125 protected boolean awakenScrollBars() {
10126 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010127 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010128 }
10129
10130 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010131 * Trigger the scrollbars to draw.
10132 * This method differs from awakenScrollBars() only in its default duration.
10133 * initialAwakenScrollBars() will show the scroll bars for longer than
10134 * usual to give the user more of a chance to notice them.
10135 *
10136 * @return true if the animation is played, false otherwise.
10137 */
10138 private boolean initialAwakenScrollBars() {
10139 return mScrollCache != null &&
10140 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10141 }
10142
10143 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010144 * <p>
10145 * Trigger the scrollbars to draw. When invoked this method starts an
10146 * animation to fade the scrollbars out after a fixed delay. If a subclass
10147 * provides animated scrolling, the start delay should equal the duration of
10148 * the scrolling animation.
10149 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010150 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010151 * <p>
10152 * The animation starts only if at least one of the scrollbars is enabled,
10153 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10154 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10155 * this method returns true, and false otherwise. If the animation is
10156 * started, this method calls {@link #invalidate()}; in that case the caller
10157 * should not call {@link #invalidate()}.
10158 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010159 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010160 * <p>
10161 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010162 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010163 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010164 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010165 * @param startDelay the delay, in milliseconds, after which the animation
10166 * should start; when the delay is 0, the animation starts
10167 * immediately
10168 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010169 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010170 * @see #scrollBy(int, int)
10171 * @see #scrollTo(int, int)
10172 * @see #isHorizontalScrollBarEnabled()
10173 * @see #isVerticalScrollBarEnabled()
10174 * @see #setHorizontalScrollBarEnabled(boolean)
10175 * @see #setVerticalScrollBarEnabled(boolean)
10176 */
10177 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010178 return awakenScrollBars(startDelay, true);
10179 }
Joe Malin32736f02011-01-19 16:14:20 -080010180
Mike Cleron290947b2009-09-29 18:34:32 -070010181 /**
10182 * <p>
10183 * Trigger the scrollbars to draw. When invoked this method starts an
10184 * animation to fade the scrollbars out after a fixed delay. If a subclass
10185 * provides animated scrolling, the start delay should equal the duration of
10186 * the scrolling animation.
10187 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010188 *
Mike Cleron290947b2009-09-29 18:34:32 -070010189 * <p>
10190 * The animation starts only if at least one of the scrollbars is enabled,
10191 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10192 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10193 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010194 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010195 * is set to true; in that case the caller
10196 * should not call {@link #invalidate()}.
10197 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010198 *
Mike Cleron290947b2009-09-29 18:34:32 -070010199 * <p>
10200 * This method should be invoked everytime a subclass directly updates the
10201 * scroll parameters.
10202 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010203 *
Mike Cleron290947b2009-09-29 18:34:32 -070010204 * @param startDelay the delay, in milliseconds, after which the animation
10205 * should start; when the delay is 0, the animation starts
10206 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010207 *
Mike Cleron290947b2009-09-29 18:34:32 -070010208 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010209 *
Mike Cleron290947b2009-09-29 18:34:32 -070010210 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010211 *
Mike Cleron290947b2009-09-29 18:34:32 -070010212 * @see #scrollBy(int, int)
10213 * @see #scrollTo(int, int)
10214 * @see #isHorizontalScrollBarEnabled()
10215 * @see #isVerticalScrollBarEnabled()
10216 * @see #setHorizontalScrollBarEnabled(boolean)
10217 * @see #setVerticalScrollBarEnabled(boolean)
10218 */
10219 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010220 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010221
Mike Cleronf116bf82009-09-27 19:14:12 -070010222 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10223 return false;
10224 }
10225
10226 if (scrollCache.scrollBar == null) {
10227 scrollCache.scrollBar = new ScrollBarDrawable();
10228 }
10229
10230 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10231
Mike Cleron290947b2009-09-29 18:34:32 -070010232 if (invalidate) {
10233 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010234 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010235 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010236
10237 if (scrollCache.state == ScrollabilityCache.OFF) {
10238 // FIXME: this is copied from WindowManagerService.
10239 // We should get this value from the system when it
10240 // is possible to do so.
10241 final int KEY_REPEAT_FIRST_DELAY = 750;
10242 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10243 }
10244
10245 // Tell mScrollCache when we should start fading. This may
10246 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010247 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010248 scrollCache.fadeStartTime = fadeStartTime;
10249 scrollCache.state = ScrollabilityCache.ON;
10250
10251 // Schedule our fader to run, unscheduling any old ones first
10252 if (mAttachInfo != null) {
10253 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10254 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10255 }
10256
10257 return true;
10258 }
10259
10260 return false;
10261 }
10262
10263 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010264 * Do not invalidate views which are not visible and which are not running an animation. They
10265 * will not get drawn and they should not set dirty flags as if they will be drawn
10266 */
10267 private boolean skipInvalidate() {
10268 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10269 (!(mParent instanceof ViewGroup) ||
10270 !((ViewGroup) mParent).isViewTransitioning(this));
10271 }
10272 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010273 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010274 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10275 * in the future. This must be called from a UI thread. To call from a non-UI
10276 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 *
10278 * WARNING: This method is destructive to dirty.
10279 * @param dirty the rectangle representing the bounds of the dirty region
10280 */
10281 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010282 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010283 return;
10284 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010285 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10286 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10287 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10288 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10289 mPrivateFlags |= PFLAG_INVALIDATED;
10290 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010291 final ViewParent p = mParent;
10292 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010293 //noinspection PointlessBooleanExpression,ConstantConditions
10294 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10295 if (p != null && ai != null && ai.mHardwareAccelerated) {
10296 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010297 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010298 p.invalidateChild(this, null);
10299 return;
10300 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 if (p != null && ai != null) {
10303 final int scrollX = mScrollX;
10304 final int scrollY = mScrollY;
10305 final Rect r = ai.mTmpInvalRect;
10306 r.set(dirty.left - scrollX, dirty.top - scrollY,
10307 dirty.right - scrollX, dirty.bottom - scrollY);
10308 mParent.invalidateChild(this, r);
10309 }
10310 }
10311 }
10312
10313 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010314 * 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 -080010315 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010316 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10317 * will be called at some point in the future. This must be called from
10318 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 * @param l the left position of the dirty region
10320 * @param t the top position of the dirty region
10321 * @param r the right position of the dirty region
10322 * @param b the bottom position of the dirty region
10323 */
10324 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010325 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010326 return;
10327 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010328 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10329 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10330 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10331 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10332 mPrivateFlags |= PFLAG_INVALIDATED;
10333 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 final ViewParent p = mParent;
10335 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010336 //noinspection PointlessBooleanExpression,ConstantConditions
10337 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10338 if (p != null && ai != null && ai.mHardwareAccelerated) {
10339 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010340 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010341 p.invalidateChild(this, null);
10342 return;
10343 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 if (p != null && ai != null && l < r && t < b) {
10346 final int scrollX = mScrollX;
10347 final int scrollY = mScrollY;
10348 final Rect tmpr = ai.mTmpInvalRect;
10349 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10350 p.invalidateChild(this, tmpr);
10351 }
10352 }
10353 }
10354
10355 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010356 * Invalidate the whole view. If the view is visible,
10357 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10358 * the future. This must be called from a UI thread. To call from a non-UI thread,
10359 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 */
10361 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010362 invalidate(true);
10363 }
Joe Malin32736f02011-01-19 16:14:20 -080010364
Chet Haaseed032702010-10-01 14:05:54 -070010365 /**
10366 * This is where the invalidate() work actually happens. A full invalidate()
10367 * causes the drawing cache to be invalidated, but this function can be called with
10368 * invalidateCache set to false to skip that invalidation step for cases that do not
10369 * need it (for example, a component that remains at the same dimensions with the same
10370 * content).
10371 *
10372 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10373 * well. This is usually true for a full invalidate, but may be set to false if the
10374 * View's contents or dimensions have not changed.
10375 */
Romain Guy849d0a32011-02-01 17:20:48 -080010376 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010377 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010378 return;
10379 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010380 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10381 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10382 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010383 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010384 mPrivateFlags &= ~PFLAG_DRAWN;
10385 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010386 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010387 mPrivateFlags |= PFLAG_INVALIDATED;
10388 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010391 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010392 //noinspection PointlessBooleanExpression,ConstantConditions
10393 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10394 if (p != null && ai != null && ai.mHardwareAccelerated) {
10395 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010396 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010397 p.invalidateChild(this, null);
10398 return;
10399 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010400 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 if (p != null && ai != null) {
10403 final Rect r = ai.mTmpInvalRect;
10404 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10405 // Don't call invalidate -- we don't want to internally scroll
10406 // our own bounds
10407 p.invalidateChild(this, r);
10408 }
10409 }
10410 }
10411
10412 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010413 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10414 * set any flags or handle all of the cases handled by the default invalidation methods.
10415 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10416 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10417 * walk up the hierarchy, transforming the dirty rect as necessary.
10418 *
10419 * The method also handles normal invalidation logic if display list properties are not
10420 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10421 * backup approach, to handle these cases used in the various property-setting methods.
10422 *
10423 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10424 * are not being used in this view
10425 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10426 * list properties are not being used in this view
10427 */
10428 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010429 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010430 if (invalidateParent) {
10431 invalidateParentCaches();
10432 }
10433 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010434 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010435 }
10436 invalidate(false);
10437 } else {
10438 final AttachInfo ai = mAttachInfo;
10439 final ViewParent p = mParent;
10440 if (p != null && ai != null) {
10441 final Rect r = ai.mTmpInvalRect;
10442 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10443 if (mParent instanceof ViewGroup) {
10444 ((ViewGroup) mParent).invalidateChildFast(this, r);
10445 } else {
10446 mParent.invalidateChild(this, r);
10447 }
10448 }
10449 }
10450 }
10451
10452 /**
10453 * Utility method to transform a given Rect by the current matrix of this view.
10454 */
10455 void transformRect(final Rect rect) {
10456 if (!getMatrix().isIdentity()) {
10457 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10458 boundingRect.set(rect);
10459 getMatrix().mapRect(boundingRect);
10460 rect.set((int) (boundingRect.left - 0.5f),
10461 (int) (boundingRect.top - 0.5f),
10462 (int) (boundingRect.right + 0.5f),
10463 (int) (boundingRect.bottom + 0.5f));
10464 }
10465 }
10466
10467 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010468 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010469 * 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
Romain Guy0fd89bf2011-01-26 15:41:30 -080010471 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10472 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010473 *
10474 * @hide
10475 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010476 protected void invalidateParentCaches() {
10477 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010478 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010479 }
10480 }
Joe Malin32736f02011-01-19 16:14:20 -080010481
Romain Guy0fd89bf2011-01-26 15:41:30 -080010482 /**
10483 * Used to indicate that the parent of this view should be invalidated. This functionality
10484 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10485 * which is necessary when various parent-managed properties of the view change, such as
10486 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10487 * an invalidation event to the parent.
10488 *
10489 * @hide
10490 */
10491 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010492 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010493 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010494 }
10495 }
10496
10497 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010498 * Indicates whether this View is opaque. An opaque View guarantees that it will
10499 * draw all the pixels overlapping its bounds using a fully opaque color.
10500 *
10501 * Subclasses of View should override this method whenever possible to indicate
10502 * whether an instance is opaque. Opaque Views are treated in a special way by
10503 * the View hierarchy, possibly allowing it to perform optimizations during
10504 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010505 *
Romain Guy24443ea2009-05-11 11:56:30 -070010506 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010507 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010508 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010509 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010510 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010511 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010512 }
10513
Adam Powell20232d02010-12-08 21:08:53 -080010514 /**
10515 * @hide
10516 */
10517 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010518 // Opaque if:
10519 // - Has a background
10520 // - Background is opaque
10521 // - Doesn't have scrollbars or scrollbars are inside overlay
10522
Philip Milne6c8ea062012-04-03 17:38:43 -070010523 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010524 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010525 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010526 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010527 }
10528
10529 final int flags = mViewFlags;
10530 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10531 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010532 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010533 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010534 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010535 }
10536 }
10537
10538 /**
10539 * @hide
10540 */
10541 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010542 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010543 }
10544
10545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 * @return A handler associated with the thread running the View. This
10547 * handler can be used to pump events in the UI events queue.
10548 */
10549 public Handler getHandler() {
10550 if (mAttachInfo != null) {
10551 return mAttachInfo.mHandler;
10552 }
10553 return null;
10554 }
10555
10556 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010557 * Gets the view root associated with the View.
10558 * @return The view root, or null if none.
10559 * @hide
10560 */
10561 public ViewRootImpl getViewRootImpl() {
10562 if (mAttachInfo != null) {
10563 return mAttachInfo.mViewRootImpl;
10564 }
10565 return null;
10566 }
10567
10568 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010569 * <p>Causes the Runnable to be added to the message queue.
10570 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 * @param action The Runnable that will be executed.
10573 *
10574 * @return Returns true if the Runnable was successfully placed in to the
10575 * message queue. Returns false on failure, usually because the
10576 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010577 *
10578 * @see #postDelayed
10579 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 */
10581 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010582 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010583 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010584 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010586 // Assume that post will succeed later
10587 ViewRootImpl.getRunQueue().post(action);
10588 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 }
10590
10591 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010592 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010594 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010595 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 * @param action The Runnable that will be executed.
10597 * @param delayMillis The delay (in milliseconds) until the Runnable
10598 * will be executed.
10599 *
10600 * @return true if the Runnable was successfully placed in to the
10601 * message queue. Returns false on failure, usually because the
10602 * looper processing the message queue is exiting. Note that a
10603 * result of true does not mean the Runnable will be processed --
10604 * if the looper is quit before the delivery time of the message
10605 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010606 *
10607 * @see #post
10608 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 */
10610 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010611 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010612 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010613 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010615 // Assume that post will succeed later
10616 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10617 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 }
10619
10620 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010621 * <p>Causes the Runnable to execute on the next animation time step.
10622 * The runnable will be run on the user interface thread.</p>
10623 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010624 * @param action The Runnable that will be executed.
10625 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010626 * @see #postOnAnimationDelayed
10627 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010628 */
10629 public void postOnAnimation(Runnable action) {
10630 final AttachInfo attachInfo = mAttachInfo;
10631 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010632 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10633 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010634 } else {
10635 // Assume that post will succeed later
10636 ViewRootImpl.getRunQueue().post(action);
10637 }
10638 }
10639
10640 /**
10641 * <p>Causes the Runnable to execute on the next animation time step,
10642 * after the specified amount of time elapses.
10643 * The runnable will be run on the user interface thread.</p>
10644 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010645 * @param action The Runnable that will be executed.
10646 * @param delayMillis The delay (in milliseconds) until the Runnable
10647 * will be executed.
10648 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010649 * @see #postOnAnimation
10650 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010651 */
10652 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10653 final AttachInfo attachInfo = mAttachInfo;
10654 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010655 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10656 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010657 } else {
10658 // Assume that post will succeed later
10659 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10660 }
10661 }
10662
10663 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010664 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 * @param action The Runnable to remove from the message handling queue
10667 *
10668 * @return true if this view could ask the Handler to remove the Runnable,
10669 * false otherwise. When the returned value is true, the Runnable
10670 * may or may not have been actually removed from the message queue
10671 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010672 *
10673 * @see #post
10674 * @see #postDelayed
10675 * @see #postOnAnimation
10676 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 */
10678 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010679 if (action != null) {
10680 final AttachInfo attachInfo = mAttachInfo;
10681 if (attachInfo != null) {
10682 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010683 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10684 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010685 } else {
10686 // Assume that post will succeed later
10687 ViewRootImpl.getRunQueue().removeCallbacks(action);
10688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010690 return true;
10691 }
10692
10693 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010694 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10695 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 *
Romain Guye63a4f32011-08-11 11:33:31 -070010697 * <p>This method can be invoked from outside of the UI thread
10698 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010699 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010701 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 */
10703 public void postInvalidate() {
10704 postInvalidateDelayed(0);
10705 }
10706
10707 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010708 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10709 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010710 *
Romain Guye63a4f32011-08-11 11:33:31 -070010711 * <p>This method can be invoked from outside of the UI thread
10712 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 *
10714 * @param left The left coordinate of the rectangle to invalidate.
10715 * @param top The top coordinate of the rectangle to invalidate.
10716 * @param right The right coordinate of the rectangle to invalidate.
10717 * @param bottom The bottom coordinate of the rectangle to invalidate.
10718 *
10719 * @see #invalidate(int, int, int, int)
10720 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010721 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 */
10723 public void postInvalidate(int left, int top, int right, int bottom) {
10724 postInvalidateDelayed(0, left, top, right, bottom);
10725 }
10726
10727 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010728 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10729 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010730 *
Romain Guye63a4f32011-08-11 11:33:31 -070010731 * <p>This method can be invoked from outside of the UI thread
10732 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010733 *
10734 * @param delayMilliseconds the duration in milliseconds to delay the
10735 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010736 *
10737 * @see #invalidate()
10738 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010739 */
10740 public void postInvalidateDelayed(long delayMilliseconds) {
10741 // We try only with the AttachInfo because there's no point in invalidating
10742 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010743 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010744 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010745 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 }
10747 }
10748
10749 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010750 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10751 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010752 *
Romain Guye63a4f32011-08-11 11:33:31 -070010753 * <p>This method can be invoked from outside of the UI thread
10754 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755 *
10756 * @param delayMilliseconds the duration in milliseconds to delay the
10757 * invalidation by
10758 * @param left The left coordinate of the rectangle to invalidate.
10759 * @param top The top coordinate of the rectangle to invalidate.
10760 * @param right The right coordinate of the rectangle to invalidate.
10761 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010762 *
10763 * @see #invalidate(int, int, int, int)
10764 * @see #invalidate(Rect)
10765 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 */
10767 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10768 int right, int bottom) {
10769
10770 // We try only with the AttachInfo because there's no point in invalidating
10771 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010772 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010773 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010774 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 info.target = this;
10776 info.left = left;
10777 info.top = top;
10778 info.right = right;
10779 info.bottom = bottom;
10780
Jeff Browna175a5b2012-02-15 19:18:31 -080010781 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 }
10783 }
10784
10785 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010786 * <p>Cause an invalidate to happen on the next animation time step, typically the
10787 * next display frame.</p>
10788 *
10789 * <p>This method can be invoked from outside of the UI thread
10790 * only when this View is attached to a window.</p>
10791 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010792 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010793 */
10794 public void postInvalidateOnAnimation() {
10795 // We try only with the AttachInfo because there's no point in invalidating
10796 // if we are not attached to our window
10797 final AttachInfo attachInfo = mAttachInfo;
10798 if (attachInfo != null) {
10799 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10800 }
10801 }
10802
10803 /**
10804 * <p>Cause an invalidate of the specified area to happen on the next animation
10805 * time step, typically the next display frame.</p>
10806 *
10807 * <p>This method can be invoked from outside of the UI thread
10808 * only when this View is attached to a window.</p>
10809 *
10810 * @param left The left coordinate of the rectangle to invalidate.
10811 * @param top The top coordinate of the rectangle to invalidate.
10812 * @param right The right coordinate of the rectangle to invalidate.
10813 * @param bottom The bottom coordinate of the rectangle to invalidate.
10814 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010815 * @see #invalidate(int, int, int, int)
10816 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010817 */
10818 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10819 // We try only with the AttachInfo because there's no point in invalidating
10820 // if we are not attached to our window
10821 final AttachInfo attachInfo = mAttachInfo;
10822 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010823 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010824 info.target = this;
10825 info.left = left;
10826 info.top = top;
10827 info.right = right;
10828 info.bottom = bottom;
10829
10830 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10831 }
10832 }
10833
10834 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010835 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10836 * This event is sent at most once every
10837 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10838 */
10839 private void postSendViewScrolledAccessibilityEventCallback() {
10840 if (mSendViewScrolledAccessibilityEvent == null) {
10841 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10842 }
10843 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10844 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10845 postDelayed(mSendViewScrolledAccessibilityEvent,
10846 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10847 }
10848 }
10849
10850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851 * Called by a parent to request that a child update its values for mScrollX
10852 * and mScrollY if necessary. This will typically be done if the child is
10853 * animating a scroll using a {@link android.widget.Scroller Scroller}
10854 * object.
10855 */
10856 public void computeScroll() {
10857 }
10858
10859 /**
10860 * <p>Indicate whether the horizontal edges are faded when the view is
10861 * scrolled horizontally.</p>
10862 *
10863 * @return true if the horizontal edges should are faded on scroll, false
10864 * otherwise
10865 *
10866 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010867 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010868 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 */
10870 public boolean isHorizontalFadingEdgeEnabled() {
10871 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10872 }
10873
10874 /**
10875 * <p>Define whether the horizontal edges should be faded when this view
10876 * is scrolled horizontally.</p>
10877 *
10878 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10879 * be faded when the view is scrolled
10880 * horizontally
10881 *
10882 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010883 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010884 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 */
10886 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10887 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10888 if (horizontalFadingEdgeEnabled) {
10889 initScrollCache();
10890 }
10891
10892 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10893 }
10894 }
10895
10896 /**
10897 * <p>Indicate whether the vertical edges are faded when the view is
10898 * scrolled horizontally.</p>
10899 *
10900 * @return true if the vertical edges should are faded on scroll, false
10901 * otherwise
10902 *
10903 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010904 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010905 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010906 */
10907 public boolean isVerticalFadingEdgeEnabled() {
10908 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10909 }
10910
10911 /**
10912 * <p>Define whether the vertical edges should be faded when this view
10913 * is scrolled vertically.</p>
10914 *
10915 * @param verticalFadingEdgeEnabled true if the vertical edges should
10916 * be faded when the view is scrolled
10917 * vertically
10918 *
10919 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010920 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010921 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 */
10923 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10924 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10925 if (verticalFadingEdgeEnabled) {
10926 initScrollCache();
10927 }
10928
10929 mViewFlags ^= FADING_EDGE_VERTICAL;
10930 }
10931 }
10932
10933 /**
10934 * Returns the strength, or intensity, of the top faded edge. The strength is
10935 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10936 * returns 0.0 or 1.0 but no value in between.
10937 *
10938 * Subclasses should override this method to provide a smoother fade transition
10939 * when scrolling occurs.
10940 *
10941 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10942 */
10943 protected float getTopFadingEdgeStrength() {
10944 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10945 }
10946
10947 /**
10948 * Returns the strength, or intensity, of the bottom 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 bottom fade as a float between 0.0f and 1.0f
10956 */
10957 protected float getBottomFadingEdgeStrength() {
10958 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10959 computeVerticalScrollRange() ? 1.0f : 0.0f;
10960 }
10961
10962 /**
10963 * Returns the strength, or intensity, of the left faded edge. The strength is
10964 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10965 * returns 0.0 or 1.0 but no value in between.
10966 *
10967 * Subclasses should override this method to provide a smoother fade transition
10968 * when scrolling occurs.
10969 *
10970 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10971 */
10972 protected float getLeftFadingEdgeStrength() {
10973 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10974 }
10975
10976 /**
10977 * Returns the strength, or intensity, of the right faded edge. The strength is
10978 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10979 * returns 0.0 or 1.0 but no value in between.
10980 *
10981 * Subclasses should override this method to provide a smoother fade transition
10982 * when scrolling occurs.
10983 *
10984 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10985 */
10986 protected float getRightFadingEdgeStrength() {
10987 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10988 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10989 }
10990
10991 /**
10992 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10993 * scrollbar is not drawn by default.</p>
10994 *
10995 * @return true if the horizontal scrollbar should be painted, false
10996 * otherwise
10997 *
10998 * @see #setHorizontalScrollBarEnabled(boolean)
10999 */
11000 public boolean isHorizontalScrollBarEnabled() {
11001 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11002 }
11003
11004 /**
11005 * <p>Define whether the horizontal scrollbar should be drawn or not. The
11006 * scrollbar is not drawn by default.</p>
11007 *
11008 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
11009 * be painted
11010 *
11011 * @see #isHorizontalScrollBarEnabled()
11012 */
11013 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
11014 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
11015 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011016 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011017 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011018 }
11019 }
11020
11021 /**
11022 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
11023 * scrollbar is not drawn by default.</p>
11024 *
11025 * @return true if the vertical scrollbar should be painted, false
11026 * otherwise
11027 *
11028 * @see #setVerticalScrollBarEnabled(boolean)
11029 */
11030 public boolean isVerticalScrollBarEnabled() {
11031 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11032 }
11033
11034 /**
11035 * <p>Define whether the vertical scrollbar should be drawn or not. The
11036 * scrollbar is not drawn by default.</p>
11037 *
11038 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11039 * be painted
11040 *
11041 * @see #isVerticalScrollBarEnabled()
11042 */
11043 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11044 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11045 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011046 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011047 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 }
11049 }
11050
Adam Powell20232d02010-12-08 21:08:53 -080011051 /**
11052 * @hide
11053 */
11054 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011055 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011056 }
Joe Malin32736f02011-01-19 16:14:20 -080011057
Mike Cleronfe81d382009-09-28 14:22:16 -070011058 /**
11059 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011060 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011061 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011062 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011063 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011064 */
11065 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11066 initScrollCache();
11067 final ScrollabilityCache scrollabilityCache = mScrollCache;
11068 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011069 if (fadeScrollbars) {
11070 scrollabilityCache.state = ScrollabilityCache.OFF;
11071 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011072 scrollabilityCache.state = ScrollabilityCache.ON;
11073 }
11074 }
Joe Malin32736f02011-01-19 16:14:20 -080011075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011076 /**
Joe Malin32736f02011-01-19 16:14:20 -080011077 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011078 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011079 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011080 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011081 *
11082 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011083 */
11084 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011085 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011086 }
Joe Malin32736f02011-01-19 16:14:20 -080011087
Mike Cleron52f0a642009-09-28 18:21:37 -070011088 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011089 *
11090 * Returns the delay before scrollbars fade.
11091 *
11092 * @return the delay before scrollbars fade
11093 *
11094 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11095 */
11096 public int getScrollBarDefaultDelayBeforeFade() {
11097 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11098 mScrollCache.scrollBarDefaultDelayBeforeFade;
11099 }
11100
11101 /**
11102 * Define the delay before scrollbars fade.
11103 *
11104 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11105 *
11106 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11107 */
11108 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11109 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11110 }
11111
11112 /**
11113 *
11114 * Returns the scrollbar fade duration.
11115 *
11116 * @return the scrollbar fade duration
11117 *
11118 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11119 */
11120 public int getScrollBarFadeDuration() {
11121 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11122 mScrollCache.scrollBarFadeDuration;
11123 }
11124
11125 /**
11126 * Define the scrollbar fade duration.
11127 *
11128 * @param scrollBarFadeDuration - the scrollbar fade duration
11129 *
11130 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11131 */
11132 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11133 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11134 }
11135
11136 /**
11137 *
11138 * Returns the scrollbar size.
11139 *
11140 * @return the scrollbar size
11141 *
11142 * @attr ref android.R.styleable#View_scrollbarSize
11143 */
11144 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011145 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011146 mScrollCache.scrollBarSize;
11147 }
11148
11149 /**
11150 * Define the scrollbar size.
11151 *
11152 * @param scrollBarSize - the scrollbar size
11153 *
11154 * @attr ref android.R.styleable#View_scrollbarSize
11155 */
11156 public void setScrollBarSize(int scrollBarSize) {
11157 getScrollCache().scrollBarSize = scrollBarSize;
11158 }
11159
11160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11162 * inset. When inset, they add to the padding of the view. And the scrollbars
11163 * can be drawn inside the padding area or on the edge of the view. For example,
11164 * if a view has a background drawable and you want to draw the scrollbars
11165 * inside the padding specified by the drawable, you can use
11166 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11167 * appear at the edge of the view, ignoring the padding, then you can use
11168 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11169 * @param style the style of the scrollbars. Should be one of
11170 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11171 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11172 * @see #SCROLLBARS_INSIDE_OVERLAY
11173 * @see #SCROLLBARS_INSIDE_INSET
11174 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11175 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011176 *
11177 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 */
11179 public void setScrollBarStyle(int style) {
11180 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11181 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011182 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011183 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011184 }
11185 }
11186
11187 /**
11188 * <p>Returns the current scrollbar style.</p>
11189 * @return the current scrollbar style
11190 * @see #SCROLLBARS_INSIDE_OVERLAY
11191 * @see #SCROLLBARS_INSIDE_INSET
11192 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11193 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011194 *
11195 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011196 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011197 @ViewDebug.ExportedProperty(mapping = {
11198 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11199 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11200 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11201 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11202 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 public int getScrollBarStyle() {
11204 return mViewFlags & SCROLLBARS_STYLE_MASK;
11205 }
11206
11207 /**
11208 * <p>Compute the horizontal range that the horizontal scrollbar
11209 * represents.</p>
11210 *
11211 * <p>The range is expressed in arbitrary units that must be the same as the
11212 * units used by {@link #computeHorizontalScrollExtent()} and
11213 * {@link #computeHorizontalScrollOffset()}.</p>
11214 *
11215 * <p>The default range is the drawing width of this view.</p>
11216 *
11217 * @return the total horizontal range represented by the horizontal
11218 * scrollbar
11219 *
11220 * @see #computeHorizontalScrollExtent()
11221 * @see #computeHorizontalScrollOffset()
11222 * @see android.widget.ScrollBarDrawable
11223 */
11224 protected int computeHorizontalScrollRange() {
11225 return getWidth();
11226 }
11227
11228 /**
11229 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11230 * within the horizontal range. This value is used to compute the position
11231 * of the thumb within the scrollbar's track.</p>
11232 *
11233 * <p>The range is expressed in arbitrary units that must be the same as the
11234 * units used by {@link #computeHorizontalScrollRange()} and
11235 * {@link #computeHorizontalScrollExtent()}.</p>
11236 *
11237 * <p>The default offset is the scroll offset of this view.</p>
11238 *
11239 * @return the horizontal offset of the scrollbar's thumb
11240 *
11241 * @see #computeHorizontalScrollRange()
11242 * @see #computeHorizontalScrollExtent()
11243 * @see android.widget.ScrollBarDrawable
11244 */
11245 protected int computeHorizontalScrollOffset() {
11246 return mScrollX;
11247 }
11248
11249 /**
11250 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11251 * within the horizontal range. This value is used to compute the length
11252 * of the thumb within the scrollbar's track.</p>
11253 *
11254 * <p>The range is expressed in arbitrary units that must be the same as the
11255 * units used by {@link #computeHorizontalScrollRange()} and
11256 * {@link #computeHorizontalScrollOffset()}.</p>
11257 *
11258 * <p>The default extent is the drawing width of this view.</p>
11259 *
11260 * @return the horizontal extent of the scrollbar's thumb
11261 *
11262 * @see #computeHorizontalScrollRange()
11263 * @see #computeHorizontalScrollOffset()
11264 * @see android.widget.ScrollBarDrawable
11265 */
11266 protected int computeHorizontalScrollExtent() {
11267 return getWidth();
11268 }
11269
11270 /**
11271 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11272 *
11273 * <p>The range is expressed in arbitrary units that must be the same as the
11274 * units used by {@link #computeVerticalScrollExtent()} and
11275 * {@link #computeVerticalScrollOffset()}.</p>
11276 *
11277 * @return the total vertical range represented by the vertical scrollbar
11278 *
11279 * <p>The default range is the drawing height of this view.</p>
11280 *
11281 * @see #computeVerticalScrollExtent()
11282 * @see #computeVerticalScrollOffset()
11283 * @see android.widget.ScrollBarDrawable
11284 */
11285 protected int computeVerticalScrollRange() {
11286 return getHeight();
11287 }
11288
11289 /**
11290 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11291 * within the horizontal range. This value is used to compute the position
11292 * of the thumb within the scrollbar's track.</p>
11293 *
11294 * <p>The range is expressed in arbitrary units that must be the same as the
11295 * units used by {@link #computeVerticalScrollRange()} and
11296 * {@link #computeVerticalScrollExtent()}.</p>
11297 *
11298 * <p>The default offset is the scroll offset of this view.</p>
11299 *
11300 * @return the vertical offset of the scrollbar's thumb
11301 *
11302 * @see #computeVerticalScrollRange()
11303 * @see #computeVerticalScrollExtent()
11304 * @see android.widget.ScrollBarDrawable
11305 */
11306 protected int computeVerticalScrollOffset() {
11307 return mScrollY;
11308 }
11309
11310 /**
11311 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11312 * within the vertical range. This value is used to compute the length
11313 * of the thumb within the scrollbar's track.</p>
11314 *
11315 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011316 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011317 * {@link #computeVerticalScrollOffset()}.</p>
11318 *
11319 * <p>The default extent is the drawing height of this view.</p>
11320 *
11321 * @return the vertical extent of the scrollbar's thumb
11322 *
11323 * @see #computeVerticalScrollRange()
11324 * @see #computeVerticalScrollOffset()
11325 * @see android.widget.ScrollBarDrawable
11326 */
11327 protected int computeVerticalScrollExtent() {
11328 return getHeight();
11329 }
11330
11331 /**
Adam Powell69159442011-06-13 17:53:06 -070011332 * Check if this view can be scrolled horizontally in a certain direction.
11333 *
11334 * @param direction Negative to check scrolling left, positive to check scrolling right.
11335 * @return true if this view can be scrolled in the specified direction, false otherwise.
11336 */
11337 public boolean canScrollHorizontally(int direction) {
11338 final int offset = computeHorizontalScrollOffset();
11339 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11340 if (range == 0) return false;
11341 if (direction < 0) {
11342 return offset > 0;
11343 } else {
11344 return offset < range - 1;
11345 }
11346 }
11347
11348 /**
11349 * Check if this view can be scrolled vertically in a certain direction.
11350 *
11351 * @param direction Negative to check scrolling up, positive to check scrolling down.
11352 * @return true if this view can be scrolled in the specified direction, false otherwise.
11353 */
11354 public boolean canScrollVertically(int direction) {
11355 final int offset = computeVerticalScrollOffset();
11356 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11357 if (range == 0) return false;
11358 if (direction < 0) {
11359 return offset > 0;
11360 } else {
11361 return offset < range - 1;
11362 }
11363 }
11364
11365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11367 * scrollbars are painted only if they have been awakened first.</p>
11368 *
11369 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011370 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011371 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011373 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011374 // scrollbars are drawn only when the animation is running
11375 final ScrollabilityCache cache = mScrollCache;
11376 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011377
Mike Cleronf116bf82009-09-27 19:14:12 -070011378 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011379
Mike Cleronf116bf82009-09-27 19:14:12 -070011380 if (state == ScrollabilityCache.OFF) {
11381 return;
11382 }
Joe Malin32736f02011-01-19 16:14:20 -080011383
Mike Cleronf116bf82009-09-27 19:14:12 -070011384 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011385
Mike Cleronf116bf82009-09-27 19:14:12 -070011386 if (state == ScrollabilityCache.FADING) {
11387 // We're fading -- get our fade interpolation
11388 if (cache.interpolatorValues == null) {
11389 cache.interpolatorValues = new float[1];
11390 }
Joe Malin32736f02011-01-19 16:14:20 -080011391
Mike Cleronf116bf82009-09-27 19:14:12 -070011392 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011393
Mike Cleronf116bf82009-09-27 19:14:12 -070011394 // Stops the animation if we're done
11395 if (cache.scrollBarInterpolator.timeToValues(values) ==
11396 Interpolator.Result.FREEZE_END) {
11397 cache.state = ScrollabilityCache.OFF;
11398 } else {
11399 cache.scrollBar.setAlpha(Math.round(values[0]));
11400 }
Joe Malin32736f02011-01-19 16:14:20 -080011401
11402 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011403 // drawing. We only want this when we're fading so that
11404 // we prevent excessive redraws
11405 invalidate = true;
11406 } else {
11407 // We're just on -- but we may have been fading before so
11408 // reset alpha
11409 cache.scrollBar.setAlpha(255);
11410 }
11411
Joe Malin32736f02011-01-19 16:14:20 -080011412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413 final int viewFlags = mViewFlags;
11414
11415 final boolean drawHorizontalScrollBar =
11416 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11417 final boolean drawVerticalScrollBar =
11418 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11419 && !isVerticalScrollBarHidden();
11420
11421 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11422 final int width = mRight - mLeft;
11423 final int height = mBottom - mTop;
11424
11425 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426
Mike Reede8853fc2009-09-04 14:01:48 -040011427 final int scrollX = mScrollX;
11428 final int scrollY = mScrollY;
11429 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11430
Mike Cleronf116bf82009-09-27 19:14:12 -070011431 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011434 int size = scrollBar.getSize(false);
11435 if (size <= 0) {
11436 size = cache.scrollBarSize;
11437 }
11438
Mike Cleronf116bf82009-09-27 19:14:12 -070011439 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011440 computeHorizontalScrollOffset(),
11441 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011442 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011443 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011444 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011445 left = scrollX + (mPaddingLeft & inside);
11446 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11447 bottom = top + size;
11448 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11449 if (invalidate) {
11450 invalidate(left, top, right, bottom);
11451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011452 }
11453
11454 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011455 int size = scrollBar.getSize(true);
11456 if (size <= 0) {
11457 size = cache.scrollBarSize;
11458 }
11459
Mike Reede8853fc2009-09-04 14:01:48 -040011460 scrollBar.setParameters(computeVerticalScrollRange(),
11461 computeVerticalScrollOffset(),
11462 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011463 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11464 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11465 verticalScrollbarPosition = isLayoutRtl() ?
11466 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11467 }
11468 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011469 default:
Adam Powell20232d02010-12-08 21:08:53 -080011470 case SCROLLBAR_POSITION_RIGHT:
11471 left = scrollX + width - size - (mUserPaddingRight & inside);
11472 break;
11473 case SCROLLBAR_POSITION_LEFT:
11474 left = scrollX + (mUserPaddingLeft & inside);
11475 break;
11476 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011477 top = scrollY + (mPaddingTop & inside);
11478 right = left + size;
11479 bottom = scrollY + height - (mUserPaddingBottom & inside);
11480 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11481 if (invalidate) {
11482 invalidate(left, top, right, bottom);
11483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011484 }
11485 }
11486 }
11487 }
Romain Guy8506ab42009-06-11 17:35:47 -070011488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011490 * 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 -080011491 * FastScroller is visible.
11492 * @return whether to temporarily hide the vertical scrollbar
11493 * @hide
11494 */
11495 protected boolean isVerticalScrollBarHidden() {
11496 return false;
11497 }
11498
11499 /**
11500 * <p>Draw the horizontal scrollbar if
11501 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11502 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 * @param canvas the canvas on which to draw the scrollbar
11504 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011505 *
11506 * @see #isHorizontalScrollBarEnabled()
11507 * @see #computeHorizontalScrollRange()
11508 * @see #computeHorizontalScrollExtent()
11509 * @see #computeHorizontalScrollOffset()
11510 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011511 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011512 */
Romain Guy8fb95422010-08-17 18:38:51 -070011513 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11514 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011515 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011516 scrollBar.draw(canvas);
11517 }
Mike Reede8853fc2009-09-04 14:01:48 -040011518
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011520 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11521 * returns true.</p>
11522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011523 * @param canvas the canvas on which to draw the scrollbar
11524 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011525 *
11526 * @see #isVerticalScrollBarEnabled()
11527 * @see #computeVerticalScrollRange()
11528 * @see #computeVerticalScrollExtent()
11529 * @see #computeVerticalScrollOffset()
11530 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011531 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 */
Romain Guy8fb95422010-08-17 18:38:51 -070011533 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11534 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011535 scrollBar.setBounds(l, t, r, b);
11536 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011537 }
11538
11539 /**
11540 * Implement this to do your drawing.
11541 *
11542 * @param canvas the canvas on which the background will be drawn
11543 */
11544 protected void onDraw(Canvas canvas) {
11545 }
11546
11547 /*
11548 * Caller is responsible for calling requestLayout if necessary.
11549 * (This allows addViewInLayout to not request a new layout.)
11550 */
11551 void assignParent(ViewParent parent) {
11552 if (mParent == null) {
11553 mParent = parent;
11554 } else if (parent == null) {
11555 mParent = null;
11556 } else {
11557 throw new RuntimeException("view " + this + " being added, but"
11558 + " it already has a parent");
11559 }
11560 }
11561
11562 /**
11563 * This is called when the view is attached to a window. At this point it
11564 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011565 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11566 * however it may be called any time before the first onDraw -- including
11567 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011568 *
11569 * @see #onDetachedFromWindow()
11570 */
11571 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011572 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011573 mParent.requestTransparentRegion(this);
11574 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011575
Dianne Hackborn4702a852012-08-17 15:18:29 -070011576 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011577 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011578 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011579 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011580
Chet Haasea9b61ac2010-12-20 07:40:25 -080011581 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011582
Svetoslav Ganov42138042012-03-20 11:51:39 -070011583 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011584 if (isFocused()) {
11585 InputMethodManager imm = InputMethodManager.peekInstance();
11586 imm.focusIn(this);
11587 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011588
Romain Guyfbb93fa2012-12-03 18:50:35 -080011589 if (mDisplayList != null) {
11590 mDisplayList.setDirty(false);
Romain Guy2a0f2282012-05-08 14:43:12 -070011591 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011592 }
Cibu Johny86666632010-02-22 13:01:02 -080011593
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011594 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011595 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011596 *
11597 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011598 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011599 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011600 if (!needRtlPropertiesResolution()) return;
11601
11602 // Order is important here: LayoutDirection MUST be resolved first
11603 if (!isLayoutDirectionResolved()) {
11604 resolveLayoutDirection();
11605 resolveLayoutParams();
11606 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011607 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011608 if (!isTextDirectionResolved()) {
11609 resolveTextDirection();
11610 }
11611 if (!isTextAlignmentResolved()) {
11612 resolveTextAlignment();
11613 }
11614 if (!isPaddingResolved()) {
11615 resolvePadding();
11616 }
11617 if (!isDrawablesResolved()) {
11618 resolveDrawables();
11619 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011620 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011621 }
11622
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011623 /**
11624 * Reset resolution of all RTL related properties.
11625 *
11626 * @hide
11627 */
11628 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011629 resetResolvedLayoutDirection();
11630 resetResolvedTextDirection();
11631 resetResolvedTextAlignment();
11632 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011633 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011634 }
11635
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011636 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011637 * @see #onScreenStateChanged(int)
11638 */
11639 void dispatchScreenStateChanged(int screenState) {
11640 onScreenStateChanged(screenState);
11641 }
11642
11643 /**
11644 * This method is called whenever the state of the screen this view is
11645 * attached to changes. A state change will usually occurs when the screen
11646 * turns on or off (whether it happens automatically or the user does it
11647 * manually.)
11648 *
11649 * @param screenState The new state of the screen. Can be either
11650 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11651 */
11652 public void onScreenStateChanged(int screenState) {
11653 }
11654
11655 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011656 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11657 */
11658 private boolean hasRtlSupport() {
11659 return mContext.getApplicationInfo().hasRtlSupport();
11660 }
11661
11662 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011663 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11664 * RTL not supported)
11665 */
11666 private boolean isRtlCompatibilityMode() {
11667 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11668 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11669 }
11670
11671 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011672 * @return true if RTL properties need resolution.
11673 */
11674 private boolean needRtlPropertiesResolution() {
11675 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11676 }
11677
11678 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011679 * Called when any RTL property (layout direction or text direction or text alignment) has
11680 * been changed.
11681 *
11682 * Subclasses need to override this method to take care of cached information that depends on the
11683 * resolved layout direction, or to inform child views that inherit their layout direction.
11684 *
11685 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011686 *
11687 * @param layoutDirection the direction of the layout
11688 *
11689 * @see #LAYOUT_DIRECTION_LTR
11690 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011691 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011692 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011693 }
11694
11695 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011696 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11697 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011698 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011699 * @return true if resolution has been done, false otherwise.
11700 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011701 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011702 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011703 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011704 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011705 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011706
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011707 if (hasRtlSupport()) {
11708 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011709 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11710 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011711 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011712 // We cannot resolve yet. LTR is by default and let the resolution happen again
11713 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011714 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011715
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011716 View parent = ((View) mParent);
11717 // Parent has not yet resolved, LTR is still the default
11718 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011719
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011720 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011721 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011722 }
11723 break;
11724 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011725 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011726 break;
11727 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011728 if((LAYOUT_DIRECTION_RTL ==
11729 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011730 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011731 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011732 break;
11733 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011734 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011735 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011736 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011737
11738 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011739 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011740 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011741 }
11742
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011743 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011744 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011745 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011746 * @return true if layout direction resolution can be done otherwise return false.
11747 *
11748 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011749 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011750 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011751 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011752 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011753 return (mParent != null) && (mParent instanceof ViewGroup) &&
11754 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011755 default:
11756 return true;
11757 }
11758 }
11759
11760 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011761 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11762 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011763 *
11764 * @hide
11765 */
11766 public void resetResolvedLayoutDirection() {
11767 // Reset the current resolved bits
11768 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011769 }
11770
11771 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011772 * @return true if the layout direction is inherited.
11773 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011774 * @hide
11775 */
11776 public boolean isLayoutDirectionInherited() {
11777 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11778 }
11779
11780 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011781 * @return true if layout direction has been resolved.
11782 */
11783 private boolean isLayoutDirectionResolved() {
11784 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11785 }
11786
11787 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011788 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011789 *
11790 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011791 */
11792 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011793 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011794 }
11795
11796 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011797 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011798 *
11799 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011800 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011801 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011802 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011803 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11804 // If start / end padding are defined, they will be resolved (hence overriding) to
11805 // left / right or right / left depending on the resolved layout direction.
11806 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011807 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011808 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011809 mUserPaddingLeft = mUserPaddingLeftInitial;
11810 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011811 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011812 switch (resolvedLayoutDirection) {
11813 case LAYOUT_DIRECTION_RTL:
11814 if (mUserPaddingStart != UNDEFINED_PADDING) {
11815 mUserPaddingRight = mUserPaddingStart;
11816 }
11817 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11818 mUserPaddingLeft = mUserPaddingEnd;
11819 }
11820 break;
11821 case LAYOUT_DIRECTION_LTR:
11822 default:
11823 if (mUserPaddingStart != UNDEFINED_PADDING) {
11824 mUserPaddingLeft = mUserPaddingStart;
11825 }
11826 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11827 mUserPaddingRight = mUserPaddingEnd;
11828 }
11829 }
11830
11831 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11832
11833 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11834 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011835 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011836 }
11837
Dianne Hackborn4702a852012-08-17 15:18:29 -070011838 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011839 }
11840
11841 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011842 * Reset the resolved layout direction.
11843 *
11844 * @hide
11845 */
11846 public void resetResolvedPadding() {
11847 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11848 }
11849
11850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011851 * This is called when the view is detached from a window. At this point it
11852 * no longer has a surface for drawing.
11853 *
11854 * @see #onAttachedToWindow()
11855 */
11856 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011857 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011858
Romain Guya440b002010-02-24 15:57:54 -080011859 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011860 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011861 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011862 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011864 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011865
Romain Guya998dff2012-03-23 18:58:36 -070011866 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011867
11868 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011869 if (mDisplayList != null) {
Romain Guyfbb93fa2012-12-03 18:50:35 -080011870 mDisplayList.setDirty(true);
Romain Guy2a0f2282012-05-08 14:43:12 -070011871 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011872 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011873 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011874 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011875 // Should never happen
11876 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011877 }
11878
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011879 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011880
Svetoslav Ganov42138042012-03-20 11:51:39 -070011881 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011882 }
11883
11884 /**
11885 * @return The number of times this view has been attached to a window
11886 */
11887 protected int getWindowAttachCount() {
11888 return mWindowAttachCount;
11889 }
11890
11891 /**
11892 * Retrieve a unique token identifying the window this view is attached to.
11893 * @return Return the window's token for use in
11894 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11895 */
11896 public IBinder getWindowToken() {
11897 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11898 }
11899
11900 /**
11901 * Retrieve a unique token identifying the top-level "real" window of
11902 * the window that this view is attached to. That is, this is like
11903 * {@link #getWindowToken}, except if the window this view in is a panel
11904 * window (attached to another containing window), then the token of
11905 * the containing window is returned instead.
11906 *
11907 * @return Returns the associated window token, either
11908 * {@link #getWindowToken()} or the containing window's token.
11909 */
11910 public IBinder getApplicationWindowToken() {
11911 AttachInfo ai = mAttachInfo;
11912 if (ai != null) {
11913 IBinder appWindowToken = ai.mPanelParentWindowToken;
11914 if (appWindowToken == null) {
11915 appWindowToken = ai.mWindowToken;
11916 }
11917 return appWindowToken;
11918 }
11919 return null;
11920 }
11921
11922 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011923 * Gets the logical display to which the view's window has been attached.
11924 *
11925 * @return The logical display, or null if the view is not currently attached to a window.
11926 */
11927 public Display getDisplay() {
11928 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11929 }
11930
11931 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011932 * Retrieve private session object this view hierarchy is using to
11933 * communicate with the window manager.
11934 * @return the session object to communicate with the window manager
11935 */
11936 /*package*/ IWindowSession getWindowSession() {
11937 return mAttachInfo != null ? mAttachInfo.mSession : null;
11938 }
11939
11940 /**
11941 * @param info the {@link android.view.View.AttachInfo} to associated with
11942 * this view
11943 */
11944 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11945 //System.out.println("Attached! " + this);
11946 mAttachInfo = info;
11947 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011948 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011949 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011950 if (mFloatingTreeObserver != null) {
11951 info.mTreeObserver.merge(mFloatingTreeObserver);
11952 mFloatingTreeObserver = null;
11953 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011954 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011956 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011957 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011958 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011959 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011960
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011961 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011962 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011963 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011964 if (listeners != null && listeners.size() > 0) {
11965 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11966 // perform the dispatching. The iterator is a safe guard against listeners that
11967 // could mutate the list by calling the various add/remove methods. This prevents
11968 // the array from being modified while we iterate it.
11969 for (OnAttachStateChangeListener listener : listeners) {
11970 listener.onViewAttachedToWindow(this);
11971 }
11972 }
11973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011974 int vis = info.mWindowVisibility;
11975 if (vis != GONE) {
11976 onWindowVisibilityChanged(vis);
11977 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011978 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011979 // If nobody has evaluated the drawable state yet, then do it now.
11980 refreshDrawableState();
11981 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011982 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983 }
11984
11985 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986 AttachInfo info = mAttachInfo;
11987 if (info != null) {
11988 int vis = info.mWindowVisibility;
11989 if (vis != GONE) {
11990 onWindowVisibilityChanged(GONE);
11991 }
11992 }
11993
11994 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011995
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011996 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011997 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011998 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011999 if (listeners != null && listeners.size() > 0) {
12000 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
12001 // perform the dispatching. The iterator is a safe guard against listeners that
12002 // could mutate the list by calling the various add/remove methods. This prevents
12003 // the array from being modified while we iterate it.
12004 for (OnAttachStateChangeListener listener : listeners) {
12005 listener.onViewDetachedFromWindow(this);
12006 }
12007 }
12008
Dianne Hackborn4702a852012-08-17 15:18:29 -070012009 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012011 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012012 }
Romain Guy01d5edc2011-01-28 11:28:53 -080012013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 mAttachInfo = null;
12015 }
12016
12017 /**
12018 * Store this view hierarchy's frozen state into the given container.
12019 *
12020 * @param container The SparseArray in which to save the view's state.
12021 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012022 * @see #restoreHierarchyState(android.util.SparseArray)
12023 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12024 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012025 */
12026 public void saveHierarchyState(SparseArray<Parcelable> container) {
12027 dispatchSaveInstanceState(container);
12028 }
12029
12030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012031 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12032 * this view and its children. May be overridden to modify how freezing happens to a
12033 * 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 -080012034 *
12035 * @param container The SparseArray in which to save the view's state.
12036 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012037 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12038 * @see #saveHierarchyState(android.util.SparseArray)
12039 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012040 */
12041 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12042 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012043 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012044 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012045 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012046 throw new IllegalStateException(
12047 "Derived class did not call super.onSaveInstanceState()");
12048 }
12049 if (state != null) {
12050 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12051 // + ": " + state);
12052 container.put(mID, state);
12053 }
12054 }
12055 }
12056
12057 /**
12058 * Hook allowing a view to generate a representation of its internal state
12059 * that can later be used to create a new instance with that same state.
12060 * This state should only contain information that is not persistent or can
12061 * not be reconstructed later. For example, you will never store your
12062 * current position on screen because that will be computed again when a
12063 * new instance of the view is placed in its view hierarchy.
12064 * <p>
12065 * Some examples of things you may store here: the current cursor position
12066 * in a text view (but usually not the text itself since that is stored in a
12067 * content provider or other persistent storage), the currently selected
12068 * item in a list view.
12069 *
12070 * @return Returns a Parcelable object containing the view's current dynamic
12071 * state, or null if there is nothing interesting to save. The
12072 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012073 * @see #onRestoreInstanceState(android.os.Parcelable)
12074 * @see #saveHierarchyState(android.util.SparseArray)
12075 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076 * @see #setSaveEnabled(boolean)
12077 */
12078 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012079 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 return BaseSavedState.EMPTY_STATE;
12081 }
12082
12083 /**
12084 * Restore this view hierarchy's frozen state from the given container.
12085 *
12086 * @param container The SparseArray which holds previously frozen states.
12087 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012088 * @see #saveHierarchyState(android.util.SparseArray)
12089 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12090 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 */
12092 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12093 dispatchRestoreInstanceState(container);
12094 }
12095
12096 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012097 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12098 * state for this view and its children. May be overridden to modify how restoring
12099 * happens to a view's children; for example, some views may want to not store state
12100 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012101 *
12102 * @param container The SparseArray which holds previously saved state.
12103 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012104 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12105 * @see #restoreHierarchyState(android.util.SparseArray)
12106 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012107 */
12108 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12109 if (mID != NO_ID) {
12110 Parcelable state = container.get(mID);
12111 if (state != null) {
12112 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12113 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012114 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012116 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012117 throw new IllegalStateException(
12118 "Derived class did not call super.onRestoreInstanceState()");
12119 }
12120 }
12121 }
12122 }
12123
12124 /**
12125 * Hook allowing a view to re-apply a representation of its internal state that had previously
12126 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12127 * null state.
12128 *
12129 * @param state The frozen state that had previously been returned by
12130 * {@link #onSaveInstanceState}.
12131 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012132 * @see #onSaveInstanceState()
12133 * @see #restoreHierarchyState(android.util.SparseArray)
12134 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012135 */
12136 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012137 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012138 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012139 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12140 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012141 + "when two views of different type have the same id in the same hierarchy. "
12142 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012143 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012144 }
12145 }
12146
12147 /**
12148 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12149 *
12150 * @return the drawing start time in milliseconds
12151 */
12152 public long getDrawingTime() {
12153 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12154 }
12155
12156 /**
12157 * <p>Enables or disables the duplication of the parent's state into this view. When
12158 * duplication is enabled, this view gets its drawable state from its parent rather
12159 * than from its own internal properties.</p>
12160 *
12161 * <p>Note: in the current implementation, setting this property to true after the
12162 * view was added to a ViewGroup might have no effect at all. This property should
12163 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12164 *
12165 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12166 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012167 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012168 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12169 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012170 *
12171 * @param enabled True to enable duplication of the parent's drawable state, false
12172 * to disable it.
12173 *
12174 * @see #getDrawableState()
12175 * @see #isDuplicateParentStateEnabled()
12176 */
12177 public void setDuplicateParentStateEnabled(boolean enabled) {
12178 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12179 }
12180
12181 /**
12182 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12183 *
12184 * @return True if this view's drawable state is duplicated from the parent,
12185 * false otherwise
12186 *
12187 * @see #getDrawableState()
12188 * @see #setDuplicateParentStateEnabled(boolean)
12189 */
12190 public boolean isDuplicateParentStateEnabled() {
12191 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12192 }
12193
12194 /**
Romain Guy171c5922011-01-06 10:04:23 -080012195 * <p>Specifies the type of layer backing this view. The layer can be
12196 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12197 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012198 *
Romain Guy171c5922011-01-06 10:04:23 -080012199 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12200 * instance that controls how the layer is composed on screen. The following
12201 * properties of the paint are taken into account when composing the layer:</p>
12202 * <ul>
12203 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12204 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12205 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12206 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012207 *
Romain Guy171c5922011-01-06 10:04:23 -080012208 * <p>If this view has an alpha value set to < 1.0 by calling
12209 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12210 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12211 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012212 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012213 *
Romain Guy171c5922011-01-06 10:04:23 -080012214 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12215 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12216 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012217 *
Chet Haased15ebf22012-09-05 11:40:29 -070012218 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012219 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12220 * {@link #LAYER_TYPE_HARDWARE}
12221 * @param paint The paint used to compose the layer. This argument is optional
12222 * and can be null. It is ignored when the layer type is
12223 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012224 *
12225 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012226 * @see #LAYER_TYPE_NONE
12227 * @see #LAYER_TYPE_SOFTWARE
12228 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012229 * @see #setAlpha(float)
12230 *
Romain Guy171c5922011-01-06 10:04:23 -080012231 * @attr ref android.R.styleable#View_layerType
12232 */
12233 public void setLayerType(int layerType, Paint paint) {
12234 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012235 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012236 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12237 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012238
Romain Guyd6cd5722011-01-17 14:42:41 -080012239 if (layerType == mLayerType) {
12240 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12241 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012242 invalidateParentCaches();
12243 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012244 }
12245 return;
12246 }
Romain Guy171c5922011-01-06 10:04:23 -080012247
12248 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012249 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012250 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012251 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012252 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012253 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012254 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012255 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012256 default:
12257 break;
Romain Guy171c5922011-01-06 10:04:23 -080012258 }
12259
12260 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012261 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12262 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12263 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012264
Romain Guy0fd89bf2011-01-26 15:41:30 -080012265 invalidateParentCaches();
12266 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012267 }
12268
12269 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012270 * Updates the {@link Paint} object used with the current layer (used only if the current
12271 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12272 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12273 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12274 * ensure that the view gets redrawn immediately.
12275 *
12276 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12277 * instance that controls how the layer is composed on screen. The following
12278 * properties of the paint are taken into account when composing the layer:</p>
12279 * <ul>
12280 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12281 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12282 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12283 * </ul>
12284 *
12285 * <p>If this view has an alpha value set to < 1.0 by calling
12286 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12287 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12288 * equivalent to setting a hardware layer on this view and providing a paint with
12289 * the desired alpha value.</p>
12290 *
12291 * @param paint The paint used to compose the layer. This argument is optional
12292 * and can be null. It is ignored when the layer type is
12293 * {@link #LAYER_TYPE_NONE}
12294 *
12295 * @see #setLayerType(int, android.graphics.Paint)
12296 */
12297 public void setLayerPaint(Paint paint) {
12298 int layerType = getLayerType();
12299 if (layerType != LAYER_TYPE_NONE) {
12300 mLayerPaint = paint == null ? new Paint() : paint;
12301 if (layerType == LAYER_TYPE_HARDWARE) {
12302 HardwareLayer layer = getHardwareLayer();
12303 if (layer != null) {
12304 layer.setLayerPaint(paint);
12305 }
12306 invalidateViewProperty(false, false);
12307 } else {
12308 invalidate();
12309 }
12310 }
12311 }
12312
12313 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012314 * Indicates whether this view has a static layer. A view with layer type
12315 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12316 * dynamic.
12317 */
12318 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012319 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012320 }
12321
12322 /**
Romain Guy171c5922011-01-06 10:04:23 -080012323 * Indicates what type of layer is currently associated with this view. By default
12324 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12325 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12326 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012327 *
Romain Guy171c5922011-01-06 10:04:23 -080012328 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12329 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012330 *
12331 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012332 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012333 * @see #LAYER_TYPE_NONE
12334 * @see #LAYER_TYPE_SOFTWARE
12335 * @see #LAYER_TYPE_HARDWARE
12336 */
12337 public int getLayerType() {
12338 return mLayerType;
12339 }
Joe Malin32736f02011-01-19 16:14:20 -080012340
Romain Guy6c319ca2011-01-11 14:29:25 -080012341 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012342 * Forces this view's layer to be created and this view to be rendered
12343 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12344 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012345 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012346 * This method can for instance be used to render a view into its layer before
12347 * starting an animation. If this view is complex, rendering into the layer
12348 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012349 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012350 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012351 *
12352 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012353 */
12354 public void buildLayer() {
12355 if (mLayerType == LAYER_TYPE_NONE) return;
12356
12357 if (mAttachInfo == null) {
12358 throw new IllegalStateException("This view must be attached to a window first");
12359 }
12360
12361 switch (mLayerType) {
12362 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012363 if (mAttachInfo.mHardwareRenderer != null &&
12364 mAttachInfo.mHardwareRenderer.isEnabled() &&
12365 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012366 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012367 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012368 break;
12369 case LAYER_TYPE_SOFTWARE:
12370 buildDrawingCache(true);
12371 break;
12372 }
12373 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012374
Romain Guyf1ae1062011-03-02 18:16:04 -080012375 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012376 * <p>Returns a hardware layer that can be used to draw this view again
12377 * without executing its draw method.</p>
12378 *
12379 * @return A HardwareLayer ready to render, or null if an error occurred.
12380 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012381 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012382 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12383 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012384 return null;
12385 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012386
Romain Guy9c4b79a2011-11-10 19:23:58 -080012387 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012388
12389 final int width = mRight - mLeft;
12390 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012391
Romain Guy6c319ca2011-01-11 14:29:25 -080012392 if (width == 0 || height == 0) {
12393 return null;
12394 }
12395
Dianne Hackborn4702a852012-08-17 15:18:29 -070012396 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012397 if (mHardwareLayer == null) {
12398 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12399 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012400 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012401 } else {
12402 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012403 if (mHardwareLayer.resize(width, height)) {
12404 mLocalDirtyRect.set(0, 0, width, height);
12405 }
Romain Guy846a5332012-07-11 17:44:57 -070012406 }
12407
12408 // This should not be necessary but applications that change
12409 // the parameters of their background drawable without calling
12410 // this.setBackground(Drawable) can leave the view in a bad state
12411 // (for instance isOpaque() returns true, but the background is
12412 // not opaque.)
12413 computeOpaqueFlags();
12414
12415 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012416 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012417 mHardwareLayer.setOpaque(opaque);
12418 mLocalDirtyRect.set(0, 0, width, height);
12419 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012420 }
12421
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012422 // The layer is not valid if the underlying GPU resources cannot be allocated
12423 if (!mHardwareLayer.isValid()) {
12424 return null;
12425 }
12426
Romain Guy11cb6422012-09-21 00:39:43 -070012427 mHardwareLayer.setLayerPaint(mLayerPaint);
12428 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12429 ViewRootImpl viewRoot = getViewRootImpl();
12430 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12431
Michael Jurka7e52caf2012-03-06 15:57:06 -080012432 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012433 }
12434
12435 return mHardwareLayer;
12436 }
Romain Guy171c5922011-01-06 10:04:23 -080012437
Romain Guy589b0bb2011-10-10 13:57:47 -070012438 /**
12439 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012440 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012441 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012442 *
12443 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012444 * @see #LAYER_TYPE_HARDWARE
12445 */
Romain Guya998dff2012-03-23 18:58:36 -070012446 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012447 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012448 AttachInfo info = mAttachInfo;
12449 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012450 info.mHardwareRenderer.isEnabled() &&
12451 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012452 mHardwareLayer.destroy();
12453 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012454
Chet Haase6a2d17f2012-09-30 12:14:13 -070012455 if (mDisplayList != null) {
12456 mDisplayList.reset();
12457 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012458 invalidate(true);
12459 invalidateParentCaches();
12460 }
Romain Guy65b345f2011-07-27 18:51:50 -070012461 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012462 }
Romain Guy65b345f2011-07-27 18:51:50 -070012463 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012464 }
12465
Romain Guy171c5922011-01-06 10:04:23 -080012466 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012467 * Destroys all hardware rendering resources. This method is invoked
12468 * when the system needs to reclaim resources. Upon execution of this
12469 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012470 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012471 * Note: you <strong>must</strong> call
12472 * <code>super.destroyHardwareResources()</code> when overriding
12473 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012474 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012475 * @hide
12476 */
12477 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012478 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012479 }
12480
12481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012482 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12483 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12484 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12485 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12486 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12487 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012488 *
Romain Guy171c5922011-01-06 10:04:23 -080012489 * <p>Enabling the drawing cache is similar to
12490 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012491 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12492 * drawing cache has no effect on rendering because the system uses a different mechanism
12493 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12494 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12495 * for information on how to enable software and hardware layers.</p>
12496 *
12497 * <p>This API can be used to manually generate
12498 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12499 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012500 *
12501 * @param enabled true to enable the drawing cache, false otherwise
12502 *
12503 * @see #isDrawingCacheEnabled()
12504 * @see #getDrawingCache()
12505 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012506 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 */
12508 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012509 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012510 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12511 }
12512
12513 /**
12514 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12515 *
12516 * @return true if the drawing cache is enabled
12517 *
12518 * @see #setDrawingCacheEnabled(boolean)
12519 * @see #getDrawingCache()
12520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012521 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012522 public boolean isDrawingCacheEnabled() {
12523 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12524 }
12525
12526 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012527 * Debugging utility which recursively outputs the dirty state of a view and its
12528 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012529 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012530 * @hide
12531 */
Romain Guy676b1732011-02-14 14:45:33 -080012532 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012533 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012534 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12535 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12536 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12537 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012538 if (clear) {
12539 mPrivateFlags &= clearMask;
12540 }
12541 if (this instanceof ViewGroup) {
12542 ViewGroup parent = (ViewGroup) this;
12543 final int count = parent.getChildCount();
12544 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012545 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012546 child.outputDirtyFlags(indent + " ", clear, clearMask);
12547 }
12548 }
12549 }
12550
12551 /**
12552 * This method is used by ViewGroup to cause its children to restore or recreate their
12553 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12554 * to recreate its own display list, which would happen if it went through the normal
12555 * draw/dispatchDraw mechanisms.
12556 *
12557 * @hide
12558 */
12559 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012560
12561 /**
12562 * A view that is not attached or hardware accelerated cannot create a display list.
12563 * This method checks these conditions and returns the appropriate result.
12564 *
12565 * @return true if view has the ability to create a display list, false otherwise.
12566 *
12567 * @hide
12568 */
12569 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012570 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012571 }
Joe Malin32736f02011-01-19 16:14:20 -080012572
Chet Haasedaf98e92011-01-10 14:10:36 -080012573 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012574 * @return The HardwareRenderer associated with that view or null if hardware rendering
12575 * is not supported or this this has not been attached to a window.
12576 *
12577 * @hide
12578 */
12579 public HardwareRenderer getHardwareRenderer() {
12580 if (mAttachInfo != null) {
12581 return mAttachInfo.mHardwareRenderer;
12582 }
12583 return null;
12584 }
12585
12586 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012587 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12588 * Otherwise, the same display list will be returned (after having been rendered into
12589 * along the way, depending on the invalidation state of the view).
12590 *
12591 * @param displayList The previous version of this displayList, could be null.
12592 * @param isLayer Whether the requester of the display list is a layer. If so,
12593 * the view will avoid creating a layer inside the resulting display list.
12594 * @return A new or reused DisplayList object.
12595 */
12596 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12597 if (!canHaveDisplayList()) {
12598 return null;
12599 }
12600
Dianne Hackborn4702a852012-08-17 15:18:29 -070012601 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012602 displayList == null || !displayList.isValid() ||
12603 (!isLayer && mRecreateDisplayList))) {
12604 // Don't need to recreate the display list, just need to tell our
12605 // children to restore/recreate theirs
12606 if (displayList != null && displayList.isValid() &&
12607 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012608 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12609 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012610 dispatchGetDisplayList();
12611
12612 return displayList;
12613 }
12614
12615 if (!isLayer) {
12616 // If we got here, we're recreating it. Mark it as such to ensure that
12617 // we copy in child display lists into ours in drawChild()
12618 mRecreateDisplayList = true;
12619 }
12620 if (displayList == null) {
12621 final String name = getClass().getSimpleName();
12622 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12623 // If we're creating a new display list, make sure our parent gets invalidated
12624 // since they will need to recreate their display list to account for this
12625 // new child display list.
12626 invalidateParentCaches();
12627 }
12628
12629 boolean caching = false;
12630 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012631 int width = mRight - mLeft;
12632 int height = mBottom - mTop;
12633
12634 try {
12635 canvas.setViewport(width, height);
12636 // The dirty rect should always be null for a display list
12637 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012638 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012639 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012640 if (layerType == LAYER_TYPE_HARDWARE) {
12641 final HardwareLayer layer = getHardwareLayer();
12642 if (layer != null && layer.isValid()) {
12643 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12644 } else {
12645 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12646 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12647 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12648 }
12649 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012650 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012651 buildDrawingCache(true);
12652 Bitmap cache = getDrawingCache(true);
12653 if (cache != null) {
12654 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12655 caching = true;
12656 }
Chet Haasea1cff502012-02-21 13:43:44 -080012657 }
Chet Haasea1cff502012-02-21 13:43:44 -080012658 } else {
12659
12660 computeScroll();
12661
Chet Haasea1cff502012-02-21 13:43:44 -080012662 canvas.translate(-mScrollX, -mScrollY);
12663 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012664 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12665 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012666 }
12667
12668 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012669 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012670 dispatchDraw(canvas);
12671 } else {
12672 draw(canvas);
12673 }
12674 }
12675 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012676 canvas.onPostDraw();
12677
12678 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012679 displayList.setCaching(caching);
12680 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012681 displayList.setLeftTopRightBottom(0, 0, width, height);
12682 } else {
12683 setDisplayListProperties(displayList);
12684 }
12685 }
12686 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012687 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12688 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012689 }
12690
12691 return displayList;
12692 }
12693
12694 /**
12695 * Get the DisplayList for the HardwareLayer
12696 *
12697 * @param layer The HardwareLayer whose DisplayList we want
12698 * @return A DisplayList fopr the specified HardwareLayer
12699 */
12700 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12701 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12702 layer.setDisplayList(displayList);
12703 return displayList;
12704 }
12705
12706
12707 /**
Romain Guyb051e892010-09-28 19:09:36 -070012708 * <p>Returns a display list that can be used to draw this view again
12709 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012710 *
Romain Guyb051e892010-09-28 19:09:36 -070012711 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012712 *
12713 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012714 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012715 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012716 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012717 return mDisplayList;
12718 }
12719
Romain Guy38c2ece2012-05-24 14:20:56 -070012720 private void clearDisplayList() {
12721 if (mDisplayList != null) {
12722 mDisplayList.invalidate();
12723 mDisplayList.clear();
12724 }
12725 }
12726
Romain Guyb051e892010-09-28 19:09:36 -070012727 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012728 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012729 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012730 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012731 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012732 * @see #getDrawingCache(boolean)
12733 */
12734 public Bitmap getDrawingCache() {
12735 return getDrawingCache(false);
12736 }
12737
12738 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012739 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12740 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12741 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12742 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12743 * request the drawing cache by calling this method and draw it on screen if the
12744 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012745 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012746 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12747 * this method will create a bitmap of the same size as this view. Because this bitmap
12748 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12749 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12750 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12751 * size than the view. This implies that your application must be able to handle this
12752 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012753 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012754 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12755 * the current density of the screen when the application is in compatibility
12756 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012757 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012758 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012759 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012760 * @see #setDrawingCacheEnabled(boolean)
12761 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012762 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 * @see #destroyDrawingCache()
12764 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012765 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12767 return null;
12768 }
12769 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012770 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771 }
Romain Guy02890fd2010-08-06 17:58:44 -070012772 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012773 }
12774
12775 /**
12776 * <p>Frees the resources used by the drawing cache. If you call
12777 * {@link #buildDrawingCache()} manually without calling
12778 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12779 * should cleanup the cache with this method afterwards.</p>
12780 *
12781 * @see #setDrawingCacheEnabled(boolean)
12782 * @see #buildDrawingCache()
12783 * @see #getDrawingCache()
12784 */
12785 public void destroyDrawingCache() {
12786 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012787 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012788 mDrawingCache = null;
12789 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012790 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012791 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012792 mUnscaledDrawingCache = null;
12793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012794 }
12795
12796 /**
12797 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012798 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012799 * view will always be drawn on top of a solid color.
12800 *
12801 * @param color The background color to use for the drawing cache's bitmap
12802 *
12803 * @see #setDrawingCacheEnabled(boolean)
12804 * @see #buildDrawingCache()
12805 * @see #getDrawingCache()
12806 */
12807 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012808 if (color != mDrawingCacheBackgroundColor) {
12809 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012810 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 }
12813
12814 /**
12815 * @see #setDrawingCacheBackgroundColor(int)
12816 *
12817 * @return The background color to used for the drawing cache's bitmap
12818 */
12819 public int getDrawingCacheBackgroundColor() {
12820 return mDrawingCacheBackgroundColor;
12821 }
12822
12823 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012824 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012825 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012826 * @see #buildDrawingCache(boolean)
12827 */
12828 public void buildDrawingCache() {
12829 buildDrawingCache(false);
12830 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012831
Romain Guyfbd8f692009-06-26 14:51:58 -070012832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12834 *
12835 * <p>If you call {@link #buildDrawingCache()} manually without calling
12836 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12837 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012838 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012839 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12840 * this method will create a bitmap of the same size as this view. Because this bitmap
12841 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12842 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12843 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12844 * size than the view. This implies that your application must be able to handle this
12845 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012846 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012847 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12848 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012849 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012850 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012851 *
12852 * @see #getDrawingCache()
12853 * @see #destroyDrawingCache()
12854 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012855 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012856 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012857 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012858 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859
Romain Guy8506ab42009-06-11 17:35:47 -070012860 int width = mRight - mLeft;
12861 int height = mBottom - mTop;
12862
12863 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012864 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012865
Romain Guye1123222009-06-29 14:24:56 -070012866 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012867 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12868 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012870
12871 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012872 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012873 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012874
Romain Guyaf61cc42012-09-30 10:49:15 -070012875 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12876 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012877 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12878 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012879 if (width > 0 && height > 0) {
12880 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12881 + projectedBitmapSize + " bytes, only "
12882 + drawingCacheSize + " available");
12883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012884 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012885 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012886 return;
12887 }
12888
12889 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012890 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891
12892 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012893 Bitmap.Config quality;
12894 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012895 // Never pick ARGB_4444 because it looks awful
12896 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012897 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12898 case DRAWING_CACHE_QUALITY_AUTO:
12899 quality = Bitmap.Config.ARGB_8888;
12900 break;
12901 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012902 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903 break;
12904 case DRAWING_CACHE_QUALITY_HIGH:
12905 quality = Bitmap.Config.ARGB_8888;
12906 break;
12907 default:
12908 quality = Bitmap.Config.ARGB_8888;
12909 break;
12910 }
12911 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012912 // Optimization for translucent windows
12913 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012914 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915 }
12916
12917 // Try to cleanup memory
12918 if (bitmap != null) bitmap.recycle();
12919
12920 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012921 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12922 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012923 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012924 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012925 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012926 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012927 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012928 }
Adam Powell26153a32010-11-08 15:22:27 -080012929 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012930 } catch (OutOfMemoryError e) {
12931 // If there is not enough memory to create the bitmap cache, just
12932 // ignore the issue as bitmap caches are not required to draw the
12933 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012934 if (autoScale) {
12935 mDrawingCache = null;
12936 } else {
12937 mUnscaledDrawingCache = null;
12938 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012939 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012940 return;
12941 }
12942
12943 clear = drawingCacheBackgroundColor != 0;
12944 }
12945
12946 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012947 if (attachInfo != null) {
12948 canvas = attachInfo.mCanvas;
12949 if (canvas == null) {
12950 canvas = new Canvas();
12951 }
12952 canvas.setBitmap(bitmap);
12953 // Temporarily clobber the cached Canvas in case one of our children
12954 // is also using a drawing cache. Without this, the children would
12955 // steal the canvas by attaching their own bitmap to it and bad, bad
12956 // thing would happen (invisible views, corrupted drawings, etc.)
12957 attachInfo.mCanvas = null;
12958 } else {
12959 // This case should hopefully never or seldom happen
12960 canvas = new Canvas(bitmap);
12961 }
12962
12963 if (clear) {
12964 bitmap.eraseColor(drawingCacheBackgroundColor);
12965 }
12966
12967 computeScroll();
12968 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012969
Romain Guye1123222009-06-29 14:24:56 -070012970 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012971 final float scale = attachInfo.mApplicationScale;
12972 canvas.scale(scale, scale);
12973 }
Joe Malin32736f02011-01-19 16:14:20 -080012974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012975 canvas.translate(-mScrollX, -mScrollY);
12976
Dianne Hackborn4702a852012-08-17 15:18:29 -070012977 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012978 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12979 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012980 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012982
12983 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012984 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12985 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012986 dispatchDraw(canvas);
12987 } else {
12988 draw(canvas);
12989 }
12990
12991 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012992 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012993
12994 if (attachInfo != null) {
12995 // Restore the cached Canvas for our siblings
12996 attachInfo.mCanvas = canvas;
12997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012998 }
12999 }
13000
13001 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013002 * Create a snapshot of the view into a bitmap. We should probably make
13003 * some form of this public, but should think about the API.
13004 */
Romain Guy223ff5c2010-03-02 17:07:47 -080013005 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013006 int width = mRight - mLeft;
13007 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013008
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013009 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070013010 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013011 width = (int) ((width * scale) + 0.5f);
13012 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080013013
Dianne Hackborndde331c2012-08-03 14:01:57 -070013014 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
13015 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013016 if (bitmap == null) {
13017 throw new OutOfMemoryError();
13018 }
13019
Romain Guyc529d8d2011-09-06 15:01:39 -070013020 Resources resources = getResources();
13021 if (resources != null) {
13022 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
13023 }
Joe Malin32736f02011-01-19 16:14:20 -080013024
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013025 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013026 if (attachInfo != null) {
13027 canvas = attachInfo.mCanvas;
13028 if (canvas == null) {
13029 canvas = new Canvas();
13030 }
13031 canvas.setBitmap(bitmap);
13032 // Temporarily clobber the cached Canvas in case one of our children
13033 // is also using a drawing cache. Without this, the children would
13034 // steal the canvas by attaching their own bitmap to it and bad, bad
13035 // things would happen (invisible views, corrupted drawings, etc.)
13036 attachInfo.mCanvas = null;
13037 } else {
13038 // This case should hopefully never or seldom happen
13039 canvas = new Canvas(bitmap);
13040 }
13041
Romain Guy5bcdff42009-05-14 21:27:18 -070013042 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013043 bitmap.eraseColor(backgroundColor);
13044 }
13045
13046 computeScroll();
13047 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013048 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013049 canvas.translate(-mScrollX, -mScrollY);
13050
Romain Guy5bcdff42009-05-14 21:27:18 -070013051 // Temporarily remove the dirty mask
13052 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013053 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013054
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013055 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013056 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013057 dispatchDraw(canvas);
13058 } else {
13059 draw(canvas);
13060 }
13061
Romain Guy5bcdff42009-05-14 21:27:18 -070013062 mPrivateFlags = flags;
13063
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013064 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013065 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013066
13067 if (attachInfo != null) {
13068 // Restore the cached Canvas for our siblings
13069 attachInfo.mCanvas = canvas;
13070 }
Romain Guy8506ab42009-06-11 17:35:47 -070013071
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013072 return bitmap;
13073 }
13074
13075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013076 * Indicates whether this View is currently in edit mode. A View is usually
13077 * in edit mode when displayed within a developer tool. For instance, if
13078 * this View is being drawn by a visual user interface builder, this method
13079 * should return true.
13080 *
13081 * Subclasses should check the return value of this method to provide
13082 * different behaviors if their normal behavior might interfere with the
13083 * host environment. For instance: the class spawns a thread in its
13084 * constructor, the drawing code relies on device-specific features, etc.
13085 *
13086 * This method is usually checked in the drawing code of custom widgets.
13087 *
13088 * @return True if this View is in edit mode, false otherwise.
13089 */
13090 public boolean isInEditMode() {
13091 return false;
13092 }
13093
13094 /**
13095 * If the View draws content inside its padding and enables fading edges,
13096 * it needs to support padding offsets. Padding offsets are added to the
13097 * fading edges to extend the length of the fade so that it covers pixels
13098 * drawn inside the padding.
13099 *
13100 * Subclasses of this class should override this method if they need
13101 * to draw content inside the padding.
13102 *
13103 * @return True if padding offset must be applied, false otherwise.
13104 *
13105 * @see #getLeftPaddingOffset()
13106 * @see #getRightPaddingOffset()
13107 * @see #getTopPaddingOffset()
13108 * @see #getBottomPaddingOffset()
13109 *
13110 * @since CURRENT
13111 */
13112 protected boolean isPaddingOffsetRequired() {
13113 return false;
13114 }
13115
13116 /**
13117 * Amount by which to extend the left fading region. Called only when
13118 * {@link #isPaddingOffsetRequired()} returns true.
13119 *
13120 * @return The left padding offset in pixels.
13121 *
13122 * @see #isPaddingOffsetRequired()
13123 *
13124 * @since CURRENT
13125 */
13126 protected int getLeftPaddingOffset() {
13127 return 0;
13128 }
13129
13130 /**
13131 * Amount by which to extend the right fading region. Called only when
13132 * {@link #isPaddingOffsetRequired()} returns true.
13133 *
13134 * @return The right padding offset in pixels.
13135 *
13136 * @see #isPaddingOffsetRequired()
13137 *
13138 * @since CURRENT
13139 */
13140 protected int getRightPaddingOffset() {
13141 return 0;
13142 }
13143
13144 /**
13145 * Amount by which to extend the top fading region. Called only when
13146 * {@link #isPaddingOffsetRequired()} returns true.
13147 *
13148 * @return The top padding offset in pixels.
13149 *
13150 * @see #isPaddingOffsetRequired()
13151 *
13152 * @since CURRENT
13153 */
13154 protected int getTopPaddingOffset() {
13155 return 0;
13156 }
13157
13158 /**
13159 * Amount by which to extend the bottom fading region. Called only when
13160 * {@link #isPaddingOffsetRequired()} returns true.
13161 *
13162 * @return The bottom padding offset in pixels.
13163 *
13164 * @see #isPaddingOffsetRequired()
13165 *
13166 * @since CURRENT
13167 */
13168 protected int getBottomPaddingOffset() {
13169 return 0;
13170 }
13171
13172 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013173 * @hide
13174 * @param offsetRequired
13175 */
13176 protected int getFadeTop(boolean offsetRequired) {
13177 int top = mPaddingTop;
13178 if (offsetRequired) top += getTopPaddingOffset();
13179 return top;
13180 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013181
Romain Guyf2fc4602011-07-19 15:20:03 -070013182 /**
13183 * @hide
13184 * @param offsetRequired
13185 */
13186 protected int getFadeHeight(boolean offsetRequired) {
13187 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013188 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013189 return mBottom - mTop - mPaddingBottom - padding;
13190 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013191
Romain Guyf2fc4602011-07-19 15:20:03 -070013192 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013193 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013194 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013195 *
Romain Guy2bffd262010-09-12 17:40:02 -070013196 * <p>Even if this method returns true, it does not mean that every call
13197 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13198 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013199 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013200 * window is hardware accelerated,
13201 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13202 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013203 *
Romain Guy2bffd262010-09-12 17:40:02 -070013204 * @return True if the view is attached to a window and the window is
13205 * hardware accelerated; false in any other case.
13206 */
13207 public boolean isHardwareAccelerated() {
13208 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13209 }
Joe Malin32736f02011-01-19 16:14:20 -080013210
Romain Guy2bffd262010-09-12 17:40:02 -070013211 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013212 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13213 * case of an active Animation being run on the view.
13214 */
13215 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13216 Animation a, boolean scalingRequired) {
13217 Transformation invalidationTransform;
13218 final int flags = parent.mGroupFlags;
13219 final boolean initialized = a.isInitialized();
13220 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013221 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013222 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013223 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013224 onAnimationStart();
13225 }
13226
13227 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13228 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13229 if (parent.mInvalidationTransformation == null) {
13230 parent.mInvalidationTransformation = new Transformation();
13231 }
13232 invalidationTransform = parent.mInvalidationTransformation;
13233 a.getTransformation(drawingTime, invalidationTransform, 1f);
13234 } else {
13235 invalidationTransform = parent.mChildTransformation;
13236 }
Romain Guy393a52c2012-05-22 20:21:08 -070013237
Chet Haasebcca79a2012-02-14 08:45:14 -080013238 if (more) {
13239 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013240 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13241 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13242 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13243 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013244 // The child need to draw an animation, potentially offscreen, so
13245 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013246 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013247 parent.invalidate(mLeft, mTop, mRight, mBottom);
13248 }
13249 } else {
13250 if (parent.mInvalidateRegion == null) {
13251 parent.mInvalidateRegion = new RectF();
13252 }
13253 final RectF region = parent.mInvalidateRegion;
13254 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13255 invalidationTransform);
13256
13257 // The child need to draw an animation, potentially offscreen, so
13258 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013259 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013260
13261 final int left = mLeft + (int) region.left;
13262 final int top = mTop + (int) region.top;
13263 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13264 top + (int) (region.height() + .5f));
13265 }
13266 }
13267 return more;
13268 }
13269
Chet Haasea1cff502012-02-21 13:43:44 -080013270 /**
13271 * This method is called by getDisplayList() when a display list is created or re-rendered.
13272 * It sets or resets the current value of all properties on that display list (resetting is
13273 * necessary when a display list is being re-created, because we need to make sure that
13274 * previously-set transform values
13275 */
13276 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013277 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013278 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013279 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013280 if (mParent instanceof ViewGroup) {
13281 displayList.setClipChildren(
13282 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13283 }
Chet Haase9420abd2012-03-29 16:28:32 -070013284 float alpha = 1;
13285 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13286 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13287 ViewGroup parentVG = (ViewGroup) mParent;
13288 final boolean hasTransform =
13289 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13290 if (hasTransform) {
13291 Transformation transform = parentVG.mChildTransformation;
13292 final int transformType = parentVG.mChildTransformation.getTransformationType();
13293 if (transformType != Transformation.TYPE_IDENTITY) {
13294 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13295 alpha = transform.getAlpha();
13296 }
13297 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13298 displayList.setStaticMatrix(transform.getMatrix());
13299 }
13300 }
13301 }
Chet Haasea1cff502012-02-21 13:43:44 -080013302 }
13303 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013304 alpha *= mTransformationInfo.mAlpha;
13305 if (alpha < 1) {
13306 final int multipliedAlpha = (int) (255 * alpha);
13307 if (onSetAlpha(multipliedAlpha)) {
13308 alpha = 1;
13309 }
13310 }
13311 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013312 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13313 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13314 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13315 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013316 if (mTransformationInfo.mCamera == null) {
13317 mTransformationInfo.mCamera = new Camera();
13318 mTransformationInfo.matrix3D = new Matrix();
13319 }
13320 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013321 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013322 displayList.setPivotX(getPivotX());
13323 displayList.setPivotY(getPivotY());
13324 }
Chet Haase9420abd2012-03-29 16:28:32 -070013325 } else if (alpha < 1) {
13326 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013327 }
13328 }
13329 }
13330
Chet Haasebcca79a2012-02-14 08:45:14 -080013331 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013332 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13333 * This draw() method is an implementation detail and is not intended to be overridden or
13334 * to be called from anywhere else other than ViewGroup.drawChild().
13335 */
13336 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013337 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013338 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013339 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013340 final int flags = parent.mGroupFlags;
13341
Chet Haasea1cff502012-02-21 13:43:44 -080013342 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013343 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013344 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013345 }
13346
13347 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013348 boolean concatMatrix = false;
13349
13350 boolean scalingRequired = false;
13351 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013352 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013353
13354 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013355 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13356 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013357 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013358 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013359 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13360 } else {
13361 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13362 }
13363
Chet Haasebcca79a2012-02-14 08:45:14 -080013364 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013365 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013366 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013367 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013368 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013369 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013370 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013371 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013372 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013373 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013374 mDisplayList != null) {
13375 // No longer animating: clear out old animation matrix
13376 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013377 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013378 }
13379 if (!useDisplayListProperties &&
13380 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13381 final boolean hasTransform =
13382 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13383 if (hasTransform) {
13384 final int transformType = parent.mChildTransformation.getTransformationType();
13385 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13386 parent.mChildTransformation : null;
13387 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13388 }
Chet Haase64a48c12012-02-13 16:33:29 -080013389 }
13390 }
13391
13392 concatMatrix |= !childHasIdentityMatrix;
13393
13394 // Sets the flag as early as possible to allow draw() implementations
13395 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013396 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013397
Romain Guyfbb43212012-08-30 15:19:27 -070013398 if (!concatMatrix &&
13399 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13400 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013401 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013402 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13403 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013404 return more;
13405 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013406 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013407
13408 if (hardwareAccelerated) {
13409 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13410 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013411 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13412 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013413 }
13414
Chet Haase64a48c12012-02-13 16:33:29 -080013415 DisplayList displayList = null;
13416 Bitmap cache = null;
13417 boolean hasDisplayList = false;
13418 if (caching) {
13419 if (!hardwareAccelerated) {
13420 if (layerType != LAYER_TYPE_NONE) {
13421 layerType = LAYER_TYPE_SOFTWARE;
13422 buildDrawingCache(true);
13423 }
13424 cache = getDrawingCache(true);
13425 } else {
13426 switch (layerType) {
13427 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013428 if (useDisplayListProperties) {
13429 hasDisplayList = canHaveDisplayList();
13430 } else {
13431 buildDrawingCache(true);
13432 cache = getDrawingCache(true);
13433 }
Chet Haase64a48c12012-02-13 16:33:29 -080013434 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013435 case LAYER_TYPE_HARDWARE:
13436 if (useDisplayListProperties) {
13437 hasDisplayList = canHaveDisplayList();
13438 }
13439 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013440 case LAYER_TYPE_NONE:
13441 // Delay getting the display list until animation-driven alpha values are
13442 // set up and possibly passed on to the view
13443 hasDisplayList = canHaveDisplayList();
13444 break;
13445 }
13446 }
13447 }
Chet Haasea1cff502012-02-21 13:43:44 -080013448 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013449 if (useDisplayListProperties) {
13450 displayList = getDisplayList();
13451 if (!displayList.isValid()) {
13452 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13453 // to getDisplayList(), the display list will be marked invalid and we should not
13454 // try to use it again.
13455 displayList = null;
13456 hasDisplayList = false;
13457 useDisplayListProperties = false;
13458 }
13459 }
Chet Haase64a48c12012-02-13 16:33:29 -080013460
Chet Haase526057b2012-07-12 17:50:41 -070013461 int sx = 0;
13462 int sy = 0;
13463 if (!hasDisplayList) {
13464 computeScroll();
13465 sx = mScrollX;
13466 sy = mScrollY;
13467 }
13468
Chet Haase64a48c12012-02-13 16:33:29 -080013469 final boolean hasNoCache = cache == null || hasDisplayList;
13470 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13471 layerType != LAYER_TYPE_HARDWARE;
13472
Chet Haasea1cff502012-02-21 13:43:44 -080013473 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013474 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013475 restoreTo = canvas.save();
13476 }
Chet Haase64a48c12012-02-13 16:33:29 -080013477 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013478 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013479 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013480 if (!useDisplayListProperties) {
13481 canvas.translate(mLeft, mTop);
13482 }
Chet Haase64a48c12012-02-13 16:33:29 -080013483 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013484 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013485 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013486 restoreTo = canvas.save();
13487 }
Chet Haase64a48c12012-02-13 16:33:29 -080013488 // mAttachInfo cannot be null, otherwise scalingRequired == false
13489 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13490 canvas.scale(scale, scale);
13491 }
13492 }
13493
Chet Haasea1cff502012-02-21 13:43:44 -080013494 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013495 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013496 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013497 if (transformToApply != null || !childHasIdentityMatrix) {
13498 int transX = 0;
13499 int transY = 0;
13500
13501 if (offsetForScroll) {
13502 transX = -sx;
13503 transY = -sy;
13504 }
13505
13506 if (transformToApply != null) {
13507 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013508 if (useDisplayListProperties) {
13509 displayList.setAnimationMatrix(transformToApply.getMatrix());
13510 } else {
13511 // Undo the scroll translation, apply the transformation matrix,
13512 // then redo the scroll translate to get the correct result.
13513 canvas.translate(-transX, -transY);
13514 canvas.concat(transformToApply.getMatrix());
13515 canvas.translate(transX, transY);
13516 }
Chet Haasea1cff502012-02-21 13:43:44 -080013517 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013518 }
13519
13520 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013521 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013522 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013523 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013524 }
13525 }
13526
Chet Haasea1cff502012-02-21 13:43:44 -080013527 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013528 canvas.translate(-transX, -transY);
13529 canvas.concat(getMatrix());
13530 canvas.translate(transX, transY);
13531 }
13532 }
13533
Chet Haase21433372012-06-05 07:54:09 -070013534 // Deal with alpha if it is or used to be <1
13535 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013536 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013537 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013538 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013539 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013540 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013541 }
Chet Haasea1cff502012-02-21 13:43:44 -080013542 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013543 if (hasNoCache) {
13544 final int multipliedAlpha = (int) (255 * alpha);
13545 if (!onSetAlpha(multipliedAlpha)) {
13546 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013547 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013548 layerType != LAYER_TYPE_NONE) {
13549 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13550 }
Chet Haase9420abd2012-03-29 16:28:32 -070013551 if (useDisplayListProperties) {
13552 displayList.setAlpha(alpha * getAlpha());
13553 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013554 final int scrollX = hasDisplayList ? 0 : sx;
13555 final int scrollY = hasDisplayList ? 0 : sy;
13556 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13557 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013558 }
13559 } else {
13560 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013561 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013562 }
13563 }
13564 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013565 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013566 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013567 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013568 }
13569
Chet Haasea1cff502012-02-21 13:43:44 -080013570 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13571 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013572 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013573 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013574 } else {
13575 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013576 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013577 } else {
13578 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13579 }
13580 }
13581 }
13582
Chet Haase9420abd2012-03-29 16:28:32 -070013583 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013584 displayList = getDisplayList();
13585 if (!displayList.isValid()) {
13586 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13587 // to getDisplayList(), the display list will be marked invalid and we should not
13588 // try to use it again.
13589 displayList = null;
13590 hasDisplayList = false;
13591 }
13592 }
13593
13594 if (hasNoCache) {
13595 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013596 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013597 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013598 if (layer != null && layer.isValid()) {
13599 mLayerPaint.setAlpha((int) (alpha * 255));
13600 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13601 layerRendered = true;
13602 } else {
13603 final int scrollX = hasDisplayList ? 0 : sx;
13604 final int scrollY = hasDisplayList ? 0 : sy;
13605 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013606 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013607 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13608 }
13609 }
13610
13611 if (!layerRendered) {
13612 if (!hasDisplayList) {
13613 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013614 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13615 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013616 dispatchDraw(canvas);
13617 } else {
13618 draw(canvas);
13619 }
13620 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013621 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013622 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013623 }
13624 }
13625 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013626 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013627 Paint cachePaint;
13628
13629 if (layerType == LAYER_TYPE_NONE) {
13630 cachePaint = parent.mCachePaint;
13631 if (cachePaint == null) {
13632 cachePaint = new Paint();
13633 cachePaint.setDither(false);
13634 parent.mCachePaint = cachePaint;
13635 }
Chet Haase9420abd2012-03-29 16:28:32 -070013636 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013637 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013638 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13639 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013640 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013641 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013642 }
13643 } else {
13644 cachePaint = mLayerPaint;
13645 cachePaint.setAlpha((int) (alpha * 255));
13646 }
13647 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13648 }
13649
Chet Haasea1cff502012-02-21 13:43:44 -080013650 if (restoreTo >= 0) {
13651 canvas.restoreToCount(restoreTo);
13652 }
Chet Haase64a48c12012-02-13 16:33:29 -080013653
13654 if (a != null && !more) {
13655 if (!hardwareAccelerated && !a.getFillAfter()) {
13656 onSetAlpha(255);
13657 }
13658 parent.finishAnimatingView(this, a);
13659 }
13660
13661 if (more && hardwareAccelerated) {
13662 // invalidation is the trigger to recreate display lists, so if we're using
13663 // display lists to render, force an invalidate to allow the animation to
13664 // continue drawing another frame
13665 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013666 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013667 // alpha animations should cause the child to recreate its display list
13668 invalidate(true);
13669 }
13670 }
13671
13672 mRecreateDisplayList = false;
13673
13674 return more;
13675 }
13676
13677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678 * Manually render this view (and all of its children) to the given Canvas.
13679 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013680 * called. When implementing a view, implement
13681 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13682 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013683 *
13684 * @param canvas The Canvas to which the View is rendered.
13685 */
13686 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013687 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013688 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013689 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013690 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013692 /*
13693 * Draw traversal performs several drawing steps which must be executed
13694 * in the appropriate order:
13695 *
13696 * 1. Draw the background
13697 * 2. If necessary, save the canvas' layers to prepare for fading
13698 * 3. Draw view's content
13699 * 4. Draw children
13700 * 5. If necessary, draw the fading edges and restore layers
13701 * 6. Draw decorations (scrollbars for instance)
13702 */
13703
13704 // Step 1, draw the background, if needed
13705 int saveCount;
13706
Romain Guy24443ea2009-05-11 11:56:30 -070013707 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013708 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013709 if (background != null) {
13710 final int scrollX = mScrollX;
13711 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013712
Romain Guy24443ea2009-05-11 11:56:30 -070013713 if (mBackgroundSizeChanged) {
13714 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13715 mBackgroundSizeChanged = false;
13716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013717
Romain Guy24443ea2009-05-11 11:56:30 -070013718 if ((scrollX | scrollY) == 0) {
13719 background.draw(canvas);
13720 } else {
13721 canvas.translate(scrollX, scrollY);
13722 background.draw(canvas);
13723 canvas.translate(-scrollX, -scrollY);
13724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013725 }
13726 }
13727
13728 // skip step 2 & 5 if possible (common case)
13729 final int viewFlags = mViewFlags;
13730 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13731 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13732 if (!verticalEdges && !horizontalEdges) {
13733 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013734 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735
13736 // Step 4, draw the children
13737 dispatchDraw(canvas);
13738
13739 // Step 6, draw decorations (scrollbars)
13740 onDrawScrollBars(canvas);
13741
13742 // we're done...
13743 return;
13744 }
13745
13746 /*
13747 * Here we do the full fledged routine...
13748 * (this is an uncommon case where speed matters less,
13749 * this is why we repeat some of the tests that have been
13750 * done above)
13751 */
13752
13753 boolean drawTop = false;
13754 boolean drawBottom = false;
13755 boolean drawLeft = false;
13756 boolean drawRight = false;
13757
13758 float topFadeStrength = 0.0f;
13759 float bottomFadeStrength = 0.0f;
13760 float leftFadeStrength = 0.0f;
13761 float rightFadeStrength = 0.0f;
13762
13763 // Step 2, save the canvas' layers
13764 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765
13766 final boolean offsetRequired = isPaddingOffsetRequired();
13767 if (offsetRequired) {
13768 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769 }
13770
13771 int left = mScrollX + paddingLeft;
13772 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013773 int top = mScrollY + getFadeTop(offsetRequired);
13774 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013775
13776 if (offsetRequired) {
13777 right += getRightPaddingOffset();
13778 bottom += getBottomPaddingOffset();
13779 }
13780
13781 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013782 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013783 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784
13785 // clip the fade length if top and bottom fades overlap
13786 // overlapping fades produce odd-looking artifacts
13787 if (verticalEdges && (top + length > bottom - length)) {
13788 length = (bottom - top) / 2;
13789 }
13790
13791 // also clip horizontal fades if necessary
13792 if (horizontalEdges && (left + length > right - length)) {
13793 length = (right - left) / 2;
13794 }
13795
13796 if (verticalEdges) {
13797 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013798 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013800 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013801 }
13802
13803 if (horizontalEdges) {
13804 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013805 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013806 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013807 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013808 }
13809
13810 saveCount = canvas.getSaveCount();
13811
13812 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013813 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013814 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13815
13816 if (drawTop) {
13817 canvas.saveLayer(left, top, right, top + length, null, flags);
13818 }
13819
13820 if (drawBottom) {
13821 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13822 }
13823
13824 if (drawLeft) {
13825 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13826 }
13827
13828 if (drawRight) {
13829 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13830 }
13831 } else {
13832 scrollabilityCache.setFadeColor(solidColor);
13833 }
13834
13835 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013836 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013837
13838 // Step 4, draw the children
13839 dispatchDraw(canvas);
13840
13841 // Step 5, draw the fade effect and restore layers
13842 final Paint p = scrollabilityCache.paint;
13843 final Matrix matrix = scrollabilityCache.matrix;
13844 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013845
13846 if (drawTop) {
13847 matrix.setScale(1, fadeHeight * topFadeStrength);
13848 matrix.postTranslate(left, top);
13849 fade.setLocalMatrix(matrix);
13850 canvas.drawRect(left, top, right, top + length, p);
13851 }
13852
13853 if (drawBottom) {
13854 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13855 matrix.postRotate(180);
13856 matrix.postTranslate(left, bottom);
13857 fade.setLocalMatrix(matrix);
13858 canvas.drawRect(left, bottom - length, right, bottom, p);
13859 }
13860
13861 if (drawLeft) {
13862 matrix.setScale(1, fadeHeight * leftFadeStrength);
13863 matrix.postRotate(-90);
13864 matrix.postTranslate(left, top);
13865 fade.setLocalMatrix(matrix);
13866 canvas.drawRect(left, top, left + length, bottom, p);
13867 }
13868
13869 if (drawRight) {
13870 matrix.setScale(1, fadeHeight * rightFadeStrength);
13871 matrix.postRotate(90);
13872 matrix.postTranslate(right, top);
13873 fade.setLocalMatrix(matrix);
13874 canvas.drawRect(right - length, top, right, bottom, p);
13875 }
13876
13877 canvas.restoreToCount(saveCount);
13878
13879 // Step 6, draw decorations (scrollbars)
13880 onDrawScrollBars(canvas);
13881 }
13882
13883 /**
13884 * Override this if your view is known to always be drawn on top of a solid color background,
13885 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13886 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13887 * should be set to 0xFF.
13888 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013889 * @see #setVerticalFadingEdgeEnabled(boolean)
13890 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013891 *
13892 * @return The known solid color background for this view, or 0 if the color may vary
13893 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013894 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 public int getSolidColor() {
13896 return 0;
13897 }
13898
13899 /**
13900 * Build a human readable string representation of the specified view flags.
13901 *
13902 * @param flags the view flags to convert to a string
13903 * @return a String representing the supplied flags
13904 */
13905 private static String printFlags(int flags) {
13906 String output = "";
13907 int numFlags = 0;
13908 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13909 output += "TAKES_FOCUS";
13910 numFlags++;
13911 }
13912
13913 switch (flags & VISIBILITY_MASK) {
13914 case INVISIBLE:
13915 if (numFlags > 0) {
13916 output += " ";
13917 }
13918 output += "INVISIBLE";
13919 // USELESS HERE numFlags++;
13920 break;
13921 case GONE:
13922 if (numFlags > 0) {
13923 output += " ";
13924 }
13925 output += "GONE";
13926 // USELESS HERE numFlags++;
13927 break;
13928 default:
13929 break;
13930 }
13931 return output;
13932 }
13933
13934 /**
13935 * Build a human readable string representation of the specified private
13936 * view flags.
13937 *
13938 * @param privateFlags the private view flags to convert to a string
13939 * @return a String representing the supplied flags
13940 */
13941 private static String printPrivateFlags(int privateFlags) {
13942 String output = "";
13943 int numFlags = 0;
13944
Dianne Hackborn4702a852012-08-17 15:18:29 -070013945 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013946 output += "WANTS_FOCUS";
13947 numFlags++;
13948 }
13949
Dianne Hackborn4702a852012-08-17 15:18:29 -070013950 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 if (numFlags > 0) {
13952 output += " ";
13953 }
13954 output += "FOCUSED";
13955 numFlags++;
13956 }
13957
Dianne Hackborn4702a852012-08-17 15:18:29 -070013958 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013959 if (numFlags > 0) {
13960 output += " ";
13961 }
13962 output += "SELECTED";
13963 numFlags++;
13964 }
13965
Dianne Hackborn4702a852012-08-17 15:18:29 -070013966 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967 if (numFlags > 0) {
13968 output += " ";
13969 }
13970 output += "IS_ROOT_NAMESPACE";
13971 numFlags++;
13972 }
13973
Dianne Hackborn4702a852012-08-17 15:18:29 -070013974 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 if (numFlags > 0) {
13976 output += " ";
13977 }
13978 output += "HAS_BOUNDS";
13979 numFlags++;
13980 }
13981
Dianne Hackborn4702a852012-08-17 15:18:29 -070013982 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 if (numFlags > 0) {
13984 output += " ";
13985 }
13986 output += "DRAWN";
13987 // USELESS HERE numFlags++;
13988 }
13989 return output;
13990 }
13991
13992 /**
13993 * <p>Indicates whether or not this view's layout will be requested during
13994 * the next hierarchy layout pass.</p>
13995 *
13996 * @return true if the layout will be forced during next layout pass
13997 */
13998 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013999 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 }
14001
14002 /**
Philip Milne7b757812012-09-19 18:13:44 -070014003 * Return true if o is a ViewGroup that is laying out using optical bounds.
14004 * @hide
14005 */
14006 public static boolean isLayoutModeOptical(Object o) {
14007 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
14008 }
14009
14010 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
14011 Insets parentInsets = mParent instanceof View ?
14012 ((View) mParent).getOpticalInsets() : Insets.NONE;
14013 Insets childInsets = getOpticalInsets();
14014 return setFrame(
14015 left + parentInsets.left - childInsets.left,
14016 top + parentInsets.top - childInsets.top,
14017 right + parentInsets.left + childInsets.right,
14018 bottom + parentInsets.top + childInsets.bottom);
14019 }
14020
14021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014022 * Assign a size and position to a view and all of its
14023 * descendants
14024 *
14025 * <p>This is the second phase of the layout mechanism.
14026 * (The first is measuring). In this phase, each parent calls
14027 * layout on all of its children to position them.
14028 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014029 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014030 *
Chet Haase9c087442011-01-12 16:20:16 -080014031 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014032 * Derived classes with children should override
14033 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014034 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014035 *
14036 * @param l Left position, relative to parent
14037 * @param t Top position, relative to parent
14038 * @param r Right position, relative to parent
14039 * @param b Bottom position, relative to parent
14040 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014041 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014042 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014043 int oldL = mLeft;
14044 int oldT = mTop;
14045 int oldB = mBottom;
14046 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014047 boolean changed = isLayoutModeOptical(mParent) ?
14048 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014049 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014050 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014051 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014052
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014053 ListenerInfo li = mListenerInfo;
14054 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014055 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014056 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014057 int numListeners = listenersCopy.size();
14058 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014059 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014060 }
14061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014062 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014063 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014064 }
14065
14066 /**
14067 * Called from layout when this view should
14068 * assign a size and position to each of its children.
14069 *
14070 * Derived classes with children should override
14071 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014072 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014073 * @param changed This is a new size or position for this view
14074 * @param left Left position, relative to parent
14075 * @param top Top position, relative to parent
14076 * @param right Right position, relative to parent
14077 * @param bottom Bottom position, relative to parent
14078 */
14079 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14080 }
14081
14082 /**
14083 * Assign a size and position to this view.
14084 *
14085 * This is called from layout.
14086 *
14087 * @param left Left position, relative to parent
14088 * @param top Top position, relative to parent
14089 * @param right Right position, relative to parent
14090 * @param bottom Bottom position, relative to parent
14091 * @return true if the new size and position are different than the
14092 * previous ones
14093 * {@hide}
14094 */
14095 protected boolean setFrame(int left, int top, int right, int bottom) {
14096 boolean changed = false;
14097
14098 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014099 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014100 + right + "," + bottom + ")");
14101 }
14102
14103 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14104 changed = true;
14105
14106 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014107 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014109 int oldWidth = mRight - mLeft;
14110 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014111 int newWidth = right - left;
14112 int newHeight = bottom - top;
14113 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14114
14115 // Invalidate our old position
14116 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014117
14118 mLeft = left;
14119 mTop = top;
14120 mRight = right;
14121 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014122 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014123 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014125
Dianne Hackborn4702a852012-08-17 15:18:29 -070014126 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128
Chet Haase75755e22011-07-18 17:48:25 -070014129 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014130 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014131 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014132 if (mTransformationInfo != null) {
14133 mTransformationInfo.mMatrixDirty = true;
14134 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014136 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14137 }
14138
14139 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14140 // If we are visible, force the DRAWN bit to on so that
14141 // this invalidate will go through (at least to our parent).
14142 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014143 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014144 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014145 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014146 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014147 // parent display list may need to be recreated based on a change in the bounds
14148 // of any child
14149 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150 }
14151
14152 // Reset drawn bit to original value (invalidate turns it off)
14153 mPrivateFlags |= drawn;
14154
14155 mBackgroundSizeChanged = true;
14156 }
14157 return changed;
14158 }
14159
14160 /**
14161 * Finalize inflating a view from XML. This is called as the last phase
14162 * of inflation, after all child views have been added.
14163 *
14164 * <p>Even if the subclass overrides onFinishInflate, they should always be
14165 * sure to call the super method, so that we get called.
14166 */
14167 protected void onFinishInflate() {
14168 }
14169
14170 /**
14171 * Returns the resources associated with this view.
14172 *
14173 * @return Resources object.
14174 */
14175 public Resources getResources() {
14176 return mResources;
14177 }
14178
14179 /**
14180 * Invalidates the specified Drawable.
14181 *
14182 * @param drawable the drawable to invalidate
14183 */
14184 public void invalidateDrawable(Drawable drawable) {
14185 if (verifyDrawable(drawable)) {
14186 final Rect dirty = drawable.getBounds();
14187 final int scrollX = mScrollX;
14188 final int scrollY = mScrollY;
14189
14190 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14191 dirty.right + scrollX, dirty.bottom + scrollY);
14192 }
14193 }
14194
14195 /**
14196 * Schedules an action on a drawable to occur at a specified time.
14197 *
14198 * @param who the recipient of the action
14199 * @param what the action to run on the drawable
14200 * @param when the time at which the action must occur. Uses the
14201 * {@link SystemClock#uptimeMillis} timebase.
14202 */
14203 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014204 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014205 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014206 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014207 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14208 Choreographer.CALLBACK_ANIMATION, what, who,
14209 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014210 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014211 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014213 }
14214 }
14215
14216 /**
14217 * Cancels a scheduled action on a drawable.
14218 *
14219 * @param who the recipient of the action
14220 * @param what the action to cancel
14221 */
14222 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014223 if (verifyDrawable(who) && what != null) {
14224 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014225 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14226 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014227 } else {
14228 ViewRootImpl.getRunQueue().removeCallbacks(what);
14229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014230 }
14231 }
14232
14233 /**
14234 * Unschedule any events associated with the given Drawable. This can be
14235 * used when selecting a new Drawable into a view, so that the previous
14236 * one is completely unscheduled.
14237 *
14238 * @param who The Drawable to unschedule.
14239 *
14240 * @see #drawableStateChanged
14241 */
14242 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014243 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014244 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14245 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 }
14247 }
14248
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014249 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014250 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14251 * that the View directionality can and will be resolved before its Drawables.
14252 *
14253 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014254 *
14255 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014256 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014257 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014258 if (canResolveLayoutDirection()) {
14259 if (mBackground != null) {
14260 mBackground.setLayoutDirection(getLayoutDirection());
14261 }
14262 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14263 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014264 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014265 }
14266
14267 /**
14268 * Called when layout direction has been resolved.
14269 *
14270 * The default implementation does nothing.
14271 *
14272 * @param layoutDirection The resolved layout direction.
14273 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014274 * @see #LAYOUT_DIRECTION_LTR
14275 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014276 *
14277 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014278 */
14279 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014280 }
14281
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014282 /**
14283 * @hide
14284 */
14285 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014286 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14287 }
14288
14289 private boolean isDrawablesResolved() {
14290 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14291 }
14292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014293 /**
14294 * If your view subclass is displaying its own Drawable objects, it should
14295 * override this function and return true for any Drawable it is
14296 * displaying. This allows animations for those drawables to be
14297 * scheduled.
14298 *
14299 * <p>Be sure to call through to the super class when overriding this
14300 * function.
14301 *
14302 * @param who The Drawable to verify. Return true if it is one you are
14303 * displaying, else return the result of calling through to the
14304 * super class.
14305 *
14306 * @return boolean If true than the Drawable is being displayed in the
14307 * view; else false and it is not allowed to animate.
14308 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014309 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14310 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014311 */
14312 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014313 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014314 }
14315
14316 /**
14317 * This function is called whenever the state of the view changes in such
14318 * a way that it impacts the state of drawables being shown.
14319 *
14320 * <p>Be sure to call through to the superclass when overriding this
14321 * function.
14322 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014323 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014324 */
14325 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014326 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014327 if (d != null && d.isStateful()) {
14328 d.setState(getDrawableState());
14329 }
14330 }
14331
14332 /**
14333 * Call this to force a view to update its drawable state. This will cause
14334 * drawableStateChanged to be called on this view. Views that are interested
14335 * in the new state should call getDrawableState.
14336 *
14337 * @see #drawableStateChanged
14338 * @see #getDrawableState
14339 */
14340 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014341 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014342 drawableStateChanged();
14343
14344 ViewParent parent = mParent;
14345 if (parent != null) {
14346 parent.childDrawableStateChanged(this);
14347 }
14348 }
14349
14350 /**
14351 * Return an array of resource IDs of the drawable states representing the
14352 * current state of the view.
14353 *
14354 * @return The current drawable state
14355 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014356 * @see Drawable#setState(int[])
14357 * @see #drawableStateChanged()
14358 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014359 */
14360 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014361 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362 return mDrawableState;
14363 } else {
14364 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014365 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014366 return mDrawableState;
14367 }
14368 }
14369
14370 /**
14371 * Generate the new {@link android.graphics.drawable.Drawable} state for
14372 * this view. This is called by the view
14373 * system when the cached Drawable state is determined to be invalid. To
14374 * retrieve the current state, you should use {@link #getDrawableState}.
14375 *
14376 * @param extraSpace if non-zero, this is the number of extra entries you
14377 * would like in the returned array in which you can place your own
14378 * states.
14379 *
14380 * @return Returns an array holding the current {@link Drawable} state of
14381 * the view.
14382 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014383 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014384 */
14385 protected int[] onCreateDrawableState(int extraSpace) {
14386 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14387 mParent instanceof View) {
14388 return ((View) mParent).onCreateDrawableState(extraSpace);
14389 }
14390
14391 int[] drawableState;
14392
14393 int privateFlags = mPrivateFlags;
14394
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014395 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014396 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014397 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14398 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014399 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014400 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014401 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014402 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14403 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014404 // This is set if HW acceleration is requested, even if the current
14405 // process doesn't allow it. This is just to allow app preview
14406 // windows to better match their app.
14407 viewStateIndex |= VIEW_STATE_ACCELERATED;
14408 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014409 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014410
Christopher Tate3d4bf172011-03-28 16:16:46 -070014411 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014412 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14413 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014415 drawableState = VIEW_STATE_SETS[viewStateIndex];
14416
14417 //noinspection ConstantIfStatement
14418 if (false) {
14419 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14420 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014421 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14423 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014424 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014425 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 + ": " + Arrays.toString(drawableState));
14427 }
14428
14429 if (extraSpace == 0) {
14430 return drawableState;
14431 }
14432
14433 final int[] fullState;
14434 if (drawableState != null) {
14435 fullState = new int[drawableState.length + extraSpace];
14436 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14437 } else {
14438 fullState = new int[extraSpace];
14439 }
14440
14441 return fullState;
14442 }
14443
14444 /**
14445 * Merge your own state values in <var>additionalState</var> into the base
14446 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014447 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448 *
14449 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014450 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014451 * own additional state values.
14452 *
14453 * @param additionalState The additional state values you would like
14454 * added to <var>baseState</var>; this array is not modified.
14455 *
14456 * @return As a convenience, the <var>baseState</var> array you originally
14457 * passed into the function is returned.
14458 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014459 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014460 */
14461 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14462 final int N = baseState.length;
14463 int i = N - 1;
14464 while (i >= 0 && baseState[i] == 0) {
14465 i--;
14466 }
14467 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14468 return baseState;
14469 }
14470
14471 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014472 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14473 * on all Drawable objects associated with this view.
14474 */
14475 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014476 if (mBackground != null) {
14477 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014478 }
14479 }
14480
14481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014482 * Sets the background color for this view.
14483 * @param color the color of the background
14484 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014485 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014486 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014487 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014488 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014489 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014490 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014491 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014492 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494 }
14495
14496 /**
14497 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014498 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014499 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014500 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 * @attr ref android.R.styleable#View_background
14502 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014503 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 public void setBackgroundResource(int resid) {
14505 if (resid != 0 && resid == mBackgroundResource) {
14506 return;
14507 }
14508
14509 Drawable d= null;
14510 if (resid != 0) {
14511 d = mResources.getDrawable(resid);
14512 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014513 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014514
14515 mBackgroundResource = resid;
14516 }
14517
14518 /**
14519 * Set the background to a given Drawable, or remove the background. If the
14520 * background has padding, this View's padding is set to the background's
14521 * padding. However, when a background is removed, this View's padding isn't
14522 * touched. If setting the padding is desired, please use
14523 * {@link #setPadding(int, int, int, int)}.
14524 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014525 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 * background
14527 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014528 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014529 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014530 setBackgroundDrawable(background);
14531 }
14532
14533 /**
14534 * @deprecated use {@link #setBackground(Drawable)} instead
14535 */
14536 @Deprecated
14537 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014538 computeOpaqueFlags();
14539
Philip Milne6c8ea062012-04-03 17:38:43 -070014540 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014541 return;
14542 }
14543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014544 boolean requestLayout = false;
14545
14546 mBackgroundResource = 0;
14547
14548 /*
14549 * Regardless of whether we're setting a new background or not, we want
14550 * to clear the previous drawable.
14551 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014552 if (mBackground != null) {
14553 mBackground.setCallback(null);
14554 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014555 }
14556
Philip Milne6c8ea062012-04-03 17:38:43 -070014557 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014558 Rect padding = sThreadLocal.get();
14559 if (padding == null) {
14560 padding = new Rect();
14561 sThreadLocal.set(padding);
14562 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014563 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014564 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014565 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014566 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014567 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014568 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014569 mUserPaddingLeftInitial = padding.right;
14570 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014571 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014572 break;
14573 case LAYOUT_DIRECTION_LTR:
14574 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014575 mUserPaddingLeftInitial = padding.left;
14576 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014577 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014579 }
14580
14581 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14582 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014583 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14584 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014585 requestLayout = true;
14586 }
14587
Philip Milne6c8ea062012-04-03 17:38:43 -070014588 background.setCallback(this);
14589 if (background.isStateful()) {
14590 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014591 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014592 background.setVisible(getVisibility() == VISIBLE, false);
14593 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014594
Dianne Hackborn4702a852012-08-17 15:18:29 -070014595 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14596 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14597 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014598 requestLayout = true;
14599 }
14600 } else {
14601 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014602 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014603
Dianne Hackborn4702a852012-08-17 15:18:29 -070014604 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014605 /*
14606 * This view ONLY drew the background before and we're removing
14607 * the background, so now it won't draw anything
14608 * (hence we SKIP_DRAW)
14609 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014610 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14611 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014612 }
14613
14614 /*
14615 * When the background is set, we try to apply its padding to this
14616 * View. When the background is removed, we don't touch this View's
14617 * padding. This is noted in the Javadocs. Hence, we don't need to
14618 * requestLayout(), the invalidate() below is sufficient.
14619 */
14620
14621 // The old background's minimum size could have affected this
14622 // View's layout, so let's requestLayout
14623 requestLayout = true;
14624 }
14625
Romain Guy8f1344f52009-05-15 16:03:59 -070014626 computeOpaqueFlags();
14627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014628 if (requestLayout) {
14629 requestLayout();
14630 }
14631
14632 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014633 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014634 }
14635
14636 /**
14637 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014638 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014639 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014640 *
14641 * @see #setBackground(Drawable)
14642 *
14643 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014644 */
14645 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014646 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014647 }
14648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014649 /**
14650 * Sets the padding. The view may add on the space required to display
14651 * the scrollbars, depending on the style and visibility of the scrollbars.
14652 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14653 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14654 * from the values set in this call.
14655 *
14656 * @attr ref android.R.styleable#View_padding
14657 * @attr ref android.R.styleable#View_paddingBottom
14658 * @attr ref android.R.styleable#View_paddingLeft
14659 * @attr ref android.R.styleable#View_paddingRight
14660 * @attr ref android.R.styleable#View_paddingTop
14661 * @param left the left padding in pixels
14662 * @param top the top padding in pixels
14663 * @param right the right padding in pixels
14664 * @param bottom the bottom padding in pixels
14665 */
14666 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014667 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014668
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014669 mUserPaddingStart = UNDEFINED_PADDING;
14670 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014671
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014672 mUserPaddingLeftInitial = left;
14673 mUserPaddingRightInitial = right;
14674
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014675 internalSetPadding(left, top, right, bottom);
14676 }
14677
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014678 /**
14679 * @hide
14680 */
14681 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014682 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014683 mUserPaddingRight = right;
14684 mUserPaddingBottom = bottom;
14685
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014686 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014687 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014688
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014689 // Common case is there are no scroll bars.
14690 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014691 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014692 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014693 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014694 switch (mVerticalScrollbarPosition) {
14695 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014696 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014697 left += offset;
14698 } else {
14699 right += offset;
14700 }
14701 break;
Adam Powell20232d02010-12-08 21:08:53 -080014702 case SCROLLBAR_POSITION_RIGHT:
14703 right += offset;
14704 break;
14705 case SCROLLBAR_POSITION_LEFT:
14706 left += offset;
14707 break;
14708 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014709 }
Adam Powell20232d02010-12-08 21:08:53 -080014710 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014711 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14712 ? 0 : getHorizontalScrollbarHeight();
14713 }
14714 }
Romain Guy8506ab42009-06-11 17:35:47 -070014715
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014716 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014717 changed = true;
14718 mPaddingLeft = left;
14719 }
14720 if (mPaddingTop != top) {
14721 changed = true;
14722 mPaddingTop = top;
14723 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014724 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014725 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014726 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014727 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014728 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014729 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014730 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014731 }
14732
14733 if (changed) {
14734 requestLayout();
14735 }
14736 }
14737
14738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014739 * Sets the relative padding. The view may add on the space required to display
14740 * the scrollbars, depending on the style and visibility of the scrollbars.
14741 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14742 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14743 * from the values set in this call.
14744 *
14745 * @attr ref android.R.styleable#View_padding
14746 * @attr ref android.R.styleable#View_paddingBottom
14747 * @attr ref android.R.styleable#View_paddingStart
14748 * @attr ref android.R.styleable#View_paddingEnd
14749 * @attr ref android.R.styleable#View_paddingTop
14750 * @param start the start padding in pixels
14751 * @param top the top padding in pixels
14752 * @param end the end padding in pixels
14753 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014754 */
14755 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014756 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014757
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014758 mUserPaddingStart = start;
14759 mUserPaddingEnd = end;
14760
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014761 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014762 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014763 mUserPaddingLeftInitial = end;
14764 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014765 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014766 break;
14767 case LAYOUT_DIRECTION_LTR:
14768 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014769 mUserPaddingLeftInitial = start;
14770 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014771 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014772 }
14773 }
14774
14775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014776 * Returns the top padding of this view.
14777 *
14778 * @return the top padding in pixels
14779 */
14780 public int getPaddingTop() {
14781 return mPaddingTop;
14782 }
14783
14784 /**
14785 * Returns the bottom padding of this view. If there are inset and enabled
14786 * scrollbars, this value may include the space required to display the
14787 * scrollbars as well.
14788 *
14789 * @return the bottom padding in pixels
14790 */
14791 public int getPaddingBottom() {
14792 return mPaddingBottom;
14793 }
14794
14795 /**
14796 * Returns the left padding of this view. If there are inset and enabled
14797 * scrollbars, this value may include the space required to display the
14798 * scrollbars as well.
14799 *
14800 * @return the left padding in pixels
14801 */
14802 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014803 if (!isPaddingResolved()) {
14804 resolvePadding();
14805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806 return mPaddingLeft;
14807 }
14808
14809 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014810 * Returns the start padding of this view depending on its resolved layout direction.
14811 * If there are inset and enabled scrollbars, this value may include the space
14812 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014813 *
14814 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014815 */
14816 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014817 if (!isPaddingResolved()) {
14818 resolvePadding();
14819 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014820 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014821 mPaddingRight : mPaddingLeft;
14822 }
14823
14824 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014825 * Returns the right padding of this view. If there are inset and enabled
14826 * scrollbars, this value may include the space required to display the
14827 * scrollbars as well.
14828 *
14829 * @return the right padding in pixels
14830 */
14831 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014832 if (!isPaddingResolved()) {
14833 resolvePadding();
14834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014835 return mPaddingRight;
14836 }
14837
14838 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014839 * Returns the end padding of this view depending on its resolved layout direction.
14840 * If there are inset and enabled scrollbars, this value may include the space
14841 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014842 *
14843 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014844 */
14845 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014846 if (!isPaddingResolved()) {
14847 resolvePadding();
14848 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014849 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014850 mPaddingLeft : mPaddingRight;
14851 }
14852
14853 /**
14854 * Return if the padding as been set thru relative values
14855 * {@link #setPaddingRelative(int, int, int, int)} or thru
14856 * @attr ref android.R.styleable#View_paddingStart or
14857 * @attr ref android.R.styleable#View_paddingEnd
14858 *
14859 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014860 */
14861 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014862 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014863 }
14864
Philip Milne7b757812012-09-19 18:13:44 -070014865 Insets computeOpticalInsets() {
14866 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014867 }
14868
14869 /**
14870 * @hide
14871 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014872 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014873 if (isRtlCompatibilityMode()) {
14874 mPaddingLeft = mUserPaddingLeftInitial;
14875 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014876 return;
14877 }
14878 if (isLayoutRtl()) {
14879 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14880 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014881 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014882 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14883 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014884 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014885 }
14886
14887 /**
14888 * @hide
14889 */
Philip Milne7b757812012-09-19 18:13:44 -070014890 public Insets getOpticalInsets() {
14891 if (mLayoutInsets == null) {
14892 mLayoutInsets = computeOpticalInsets();
14893 }
14894 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070014895 }
14896
14897 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014898 * Changes the selection state of this view. A view can be selected or not.
14899 * Note that selection is not the same as focus. Views are typically
14900 * selected in the context of an AdapterView like ListView or GridView;
14901 * the selected view is the view that is highlighted.
14902 *
14903 * @param selected true if the view must be selected, false otherwise
14904 */
14905 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014906 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14907 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014908 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014909 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014910 refreshDrawableState();
14911 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014912 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14913 notifyAccessibilityStateChanged();
14914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014915 }
14916 }
14917
14918 /**
14919 * Dispatch setSelected to all of this View's children.
14920 *
14921 * @see #setSelected(boolean)
14922 *
14923 * @param selected The new selected state
14924 */
14925 protected void dispatchSetSelected(boolean selected) {
14926 }
14927
14928 /**
14929 * Indicates the selection state of this view.
14930 *
14931 * @return true if the view is selected, false otherwise
14932 */
14933 @ViewDebug.ExportedProperty
14934 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014935 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936 }
14937
14938 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014939 * Changes the activated state of this view. A view can be activated or not.
14940 * Note that activation is not the same as selection. Selection is
14941 * a transient property, representing the view (hierarchy) the user is
14942 * currently interacting with. Activation is a longer-term state that the
14943 * user can move views in and out of. For example, in a list view with
14944 * single or multiple selection enabled, the views in the current selection
14945 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14946 * here.) The activated state is propagated down to children of the view it
14947 * is set on.
14948 *
14949 * @param activated true if the view must be activated, false otherwise
14950 */
14951 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014952 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14953 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014954 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014955 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014956 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014957 }
14958 }
14959
14960 /**
14961 * Dispatch setActivated to all of this View's children.
14962 *
14963 * @see #setActivated(boolean)
14964 *
14965 * @param activated The new activated state
14966 */
14967 protected void dispatchSetActivated(boolean activated) {
14968 }
14969
14970 /**
14971 * Indicates the activation state of this view.
14972 *
14973 * @return true if the view is activated, false otherwise
14974 */
14975 @ViewDebug.ExportedProperty
14976 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014977 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014978 }
14979
14980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014981 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14982 * observer can be used to get notifications when global events, like
14983 * layout, happen.
14984 *
14985 * The returned ViewTreeObserver observer is not guaranteed to remain
14986 * valid for the lifetime of this View. If the caller of this method keeps
14987 * a long-lived reference to ViewTreeObserver, it should always check for
14988 * the return value of {@link ViewTreeObserver#isAlive()}.
14989 *
14990 * @return The ViewTreeObserver for this view's hierarchy.
14991 */
14992 public ViewTreeObserver getViewTreeObserver() {
14993 if (mAttachInfo != null) {
14994 return mAttachInfo.mTreeObserver;
14995 }
14996 if (mFloatingTreeObserver == null) {
14997 mFloatingTreeObserver = new ViewTreeObserver();
14998 }
14999 return mFloatingTreeObserver;
15000 }
15001
15002 /**
15003 * <p>Finds the topmost view in the current view hierarchy.</p>
15004 *
15005 * @return the topmost view containing this view
15006 */
15007 public View getRootView() {
15008 if (mAttachInfo != null) {
15009 final View v = mAttachInfo.mRootView;
15010 if (v != null) {
15011 return v;
15012 }
15013 }
Romain Guy8506ab42009-06-11 17:35:47 -070015014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015015 View parent = this;
15016
15017 while (parent.mParent != null && parent.mParent instanceof View) {
15018 parent = (View) parent.mParent;
15019 }
15020
15021 return parent;
15022 }
15023
15024 /**
15025 * <p>Computes the coordinates of this view on the screen. The argument
15026 * must be an array of two integers. After the method returns, the array
15027 * contains the x and y location in that order.</p>
15028 *
15029 * @param location an array of two integers in which to hold the coordinates
15030 */
15031 public void getLocationOnScreen(int[] location) {
15032 getLocationInWindow(location);
15033
15034 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015035 if (info != null) {
15036 location[0] += info.mWindowLeft;
15037 location[1] += info.mWindowTop;
15038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015039 }
15040
15041 /**
15042 * <p>Computes the coordinates of this view in its window. The argument
15043 * must be an array of two integers. After the method returns, the array
15044 * contains the x and y location in that order.</p>
15045 *
15046 * @param location an array of two integers in which to hold the coordinates
15047 */
15048 public void getLocationInWindow(int[] location) {
15049 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015050 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015051 }
15052
Gilles Debunne6583ce52011-12-06 18:09:02 -080015053 if (mAttachInfo == null) {
15054 // When the view is not attached to a window, this method does not make sense
15055 location[0] = location[1] = 0;
15056 return;
15057 }
15058
Gilles Debunnecea45132011-11-24 02:19:27 +010015059 float[] position = mAttachInfo.mTmpTransformLocation;
15060 position[0] = position[1] = 0.0f;
15061
15062 if (!hasIdentityMatrix()) {
15063 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065
Gilles Debunnecea45132011-11-24 02:19:27 +010015066 position[0] += mLeft;
15067 position[1] += mTop;
15068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015069 ViewParent viewParent = mParent;
15070 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015071 final View view = (View) viewParent;
15072
15073 position[0] -= view.mScrollX;
15074 position[1] -= view.mScrollY;
15075
15076 if (!view.hasIdentityMatrix()) {
15077 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015078 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015079
15080 position[0] += view.mLeft;
15081 position[1] += view.mTop;
15082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015083 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015084 }
Romain Guy8506ab42009-06-11 17:35:47 -070015085
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015086 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015087 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015088 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15089 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015090 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015091
15092 location[0] = (int) (position[0] + 0.5f);
15093 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 }
15095
15096 /**
15097 * {@hide}
15098 * @param id the id of the view to be found
15099 * @return the view of the specified id, null if cannot be found
15100 */
15101 protected View findViewTraversal(int id) {
15102 if (id == mID) {
15103 return this;
15104 }
15105 return null;
15106 }
15107
15108 /**
15109 * {@hide}
15110 * @param tag the tag of the view to be found
15111 * @return the view of specified tag, null if cannot be found
15112 */
15113 protected View findViewWithTagTraversal(Object tag) {
15114 if (tag != null && tag.equals(mTag)) {
15115 return this;
15116 }
15117 return null;
15118 }
15119
15120 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015121 * {@hide}
15122 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015123 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015124 * @return The first view that matches the predicate or null.
15125 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015126 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015127 if (predicate.apply(this)) {
15128 return this;
15129 }
15130 return null;
15131 }
15132
15133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015134 * Look for a child view with the given id. If this view has the given
15135 * id, return this view.
15136 *
15137 * @param id The id to search for.
15138 * @return The view that has the given id in the hierarchy or null
15139 */
15140 public final View findViewById(int id) {
15141 if (id < 0) {
15142 return null;
15143 }
15144 return findViewTraversal(id);
15145 }
15146
15147 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015148 * Finds a view by its unuque and stable accessibility id.
15149 *
15150 * @param accessibilityId The searched accessibility id.
15151 * @return The found view.
15152 */
15153 final View findViewByAccessibilityId(int accessibilityId) {
15154 if (accessibilityId < 0) {
15155 return null;
15156 }
15157 return findViewByAccessibilityIdTraversal(accessibilityId);
15158 }
15159
15160 /**
15161 * Performs the traversal to find a view by its unuque and stable accessibility id.
15162 *
15163 * <strong>Note:</strong>This method does not stop at the root namespace
15164 * boundary since the user can touch the screen at an arbitrary location
15165 * potentially crossing the root namespace bounday which will send an
15166 * accessibility event to accessibility services and they should be able
15167 * to obtain the event source. Also accessibility ids are guaranteed to be
15168 * unique in the window.
15169 *
15170 * @param accessibilityId The accessibility id.
15171 * @return The found view.
15172 */
15173 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15174 if (getAccessibilityViewId() == accessibilityId) {
15175 return this;
15176 }
15177 return null;
15178 }
15179
15180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015181 * Look for a child view with the given tag. If this view has the given
15182 * tag, return this view.
15183 *
15184 * @param tag The tag to search for, using "tag.equals(getTag())".
15185 * @return The View that has the given tag in the hierarchy or null
15186 */
15187 public final View findViewWithTag(Object tag) {
15188 if (tag == null) {
15189 return null;
15190 }
15191 return findViewWithTagTraversal(tag);
15192 }
15193
15194 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015195 * {@hide}
15196 * Look for a child view that matches the specified predicate.
15197 * If this view matches the predicate, return this view.
15198 *
15199 * @param predicate The predicate to evaluate.
15200 * @return The first view that matches the predicate or null.
15201 */
15202 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015203 return findViewByPredicateTraversal(predicate, null);
15204 }
15205
15206 /**
15207 * {@hide}
15208 * Look for a child view that matches the specified predicate,
15209 * starting with the specified view and its descendents and then
15210 * recusively searching the ancestors and siblings of that view
15211 * until this view is reached.
15212 *
15213 * This method is useful in cases where the predicate does not match
15214 * a single unique view (perhaps multiple views use the same id)
15215 * and we are trying to find the view that is "closest" in scope to the
15216 * starting view.
15217 *
15218 * @param start The view to start from.
15219 * @param predicate The predicate to evaluate.
15220 * @return The first view that matches the predicate or null.
15221 */
15222 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15223 View childToSkip = null;
15224 for (;;) {
15225 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15226 if (view != null || start == this) {
15227 return view;
15228 }
15229
15230 ViewParent parent = start.getParent();
15231 if (parent == null || !(parent instanceof View)) {
15232 return null;
15233 }
15234
15235 childToSkip = start;
15236 start = (View) parent;
15237 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015238 }
15239
15240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015241 * Sets the identifier for this view. The identifier does not have to be
15242 * unique in this view's hierarchy. The identifier should be a positive
15243 * number.
15244 *
15245 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015246 * @see #getId()
15247 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015248 *
15249 * @param id a number used to identify the view
15250 *
15251 * @attr ref android.R.styleable#View_id
15252 */
15253 public void setId(int id) {
15254 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015255 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15256 mID = generateViewId();
15257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015258 }
15259
15260 /**
15261 * {@hide}
15262 *
15263 * @param isRoot true if the view belongs to the root namespace, false
15264 * otherwise
15265 */
15266 public void setIsRootNamespace(boolean isRoot) {
15267 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015268 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015269 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015270 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015271 }
15272 }
15273
15274 /**
15275 * {@hide}
15276 *
15277 * @return true if the view belongs to the root namespace, false otherwise
15278 */
15279 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015280 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015281 }
15282
15283 /**
15284 * Returns this view's identifier.
15285 *
15286 * @return a positive integer used to identify the view or {@link #NO_ID}
15287 * if the view has no ID
15288 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015289 * @see #setId(int)
15290 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015291 * @attr ref android.R.styleable#View_id
15292 */
15293 @ViewDebug.CapturedViewProperty
15294 public int getId() {
15295 return mID;
15296 }
15297
15298 /**
15299 * Returns this view's tag.
15300 *
15301 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015302 *
15303 * @see #setTag(Object)
15304 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015305 */
15306 @ViewDebug.ExportedProperty
15307 public Object getTag() {
15308 return mTag;
15309 }
15310
15311 /**
15312 * Sets the tag associated with this view. A tag can be used to mark
15313 * a view in its hierarchy and does not have to be unique within the
15314 * hierarchy. Tags can also be used to store data within a view without
15315 * resorting to another data structure.
15316 *
15317 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015318 *
15319 * @see #getTag()
15320 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015321 */
15322 public void setTag(final Object tag) {
15323 mTag = tag;
15324 }
15325
15326 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015327 * Returns the tag associated with this view and the specified key.
15328 *
15329 * @param key The key identifying the tag
15330 *
15331 * @return the Object stored in this view as a tag
15332 *
15333 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015334 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015335 */
15336 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015337 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015338 return null;
15339 }
15340
15341 /**
15342 * Sets a tag associated with this view and a key. A tag can be used
15343 * to mark a view in its hierarchy and does not have to be unique within
15344 * the hierarchy. Tags can also be used to store data within a view
15345 * without resorting to another data structure.
15346 *
15347 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015348 * application to ensure it is unique (see the <a
15349 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15350 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015351 * the Android framework or not associated with any package will cause
15352 * an {@link IllegalArgumentException} to be thrown.
15353 *
15354 * @param key The key identifying the tag
15355 * @param tag An Object to tag the view with
15356 *
15357 * @throws IllegalArgumentException If they specified key is not valid
15358 *
15359 * @see #setTag(Object)
15360 * @see #getTag(int)
15361 */
15362 public void setTag(int key, final Object tag) {
15363 // If the package id is 0x00 or 0x01, it's either an undefined package
15364 // or a framework id
15365 if ((key >>> 24) < 2) {
15366 throw new IllegalArgumentException("The key must be an application-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
15373 /**
15374 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15375 * framework id.
15376 *
15377 * @hide
15378 */
15379 public void setTagInternal(int key, Object tag) {
15380 if ((key >>> 24) != 0x1) {
15381 throw new IllegalArgumentException("The key must be a framework-specific "
15382 + "resource id.");
15383 }
15384
Adam Powell2b2f6d62011-09-23 11:15:39 -070015385 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015386 }
15387
Adam Powell2b2f6d62011-09-23 11:15:39 -070015388 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015389 if (mKeyedTags == null) {
15390 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015391 }
15392
Adam Powell7db82ac2011-09-22 19:44:04 -070015393 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015394 }
15395
15396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015397 * Prints information about this view in the log output, with the tag
15398 * {@link #VIEW_LOG_TAG}.
15399 *
15400 * @hide
15401 */
15402 public void debug() {
15403 debug(0);
15404 }
15405
15406 /**
15407 * Prints information about this view in the log output, with the tag
15408 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15409 * indentation defined by the <code>depth</code>.
15410 *
15411 * @param depth the indentation level
15412 *
15413 * @hide
15414 */
15415 protected void debug(int depth) {
15416 String output = debugIndent(depth - 1);
15417
15418 output += "+ " + this;
15419 int id = getId();
15420 if (id != -1) {
15421 output += " (id=" + id + ")";
15422 }
15423 Object tag = getTag();
15424 if (tag != null) {
15425 output += " (tag=" + tag + ")";
15426 }
15427 Log.d(VIEW_LOG_TAG, output);
15428
Dianne Hackborn4702a852012-08-17 15:18:29 -070015429 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015430 output = debugIndent(depth) + " FOCUSED";
15431 Log.d(VIEW_LOG_TAG, output);
15432 }
15433
15434 output = debugIndent(depth);
15435 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15436 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15437 + "} ";
15438 Log.d(VIEW_LOG_TAG, output);
15439
15440 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15441 || mPaddingBottom != 0) {
15442 output = debugIndent(depth);
15443 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15444 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15445 Log.d(VIEW_LOG_TAG, output);
15446 }
15447
15448 output = debugIndent(depth);
15449 output += "mMeasureWidth=" + mMeasuredWidth +
15450 " mMeasureHeight=" + mMeasuredHeight;
15451 Log.d(VIEW_LOG_TAG, output);
15452
15453 output = debugIndent(depth);
15454 if (mLayoutParams == null) {
15455 output += "BAD! no layout params";
15456 } else {
15457 output = mLayoutParams.debug(output);
15458 }
15459 Log.d(VIEW_LOG_TAG, output);
15460
15461 output = debugIndent(depth);
15462 output += "flags={";
15463 output += View.printFlags(mViewFlags);
15464 output += "}";
15465 Log.d(VIEW_LOG_TAG, output);
15466
15467 output = debugIndent(depth);
15468 output += "privateFlags={";
15469 output += View.printPrivateFlags(mPrivateFlags);
15470 output += "}";
15471 Log.d(VIEW_LOG_TAG, output);
15472 }
15473
15474 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015475 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015476 *
15477 * @param depth the indentation level
15478 * @return a String containing (depth * 2 + 3) * 2 white spaces
15479 *
15480 * @hide
15481 */
15482 protected static String debugIndent(int depth) {
15483 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15484 for (int i = 0; i < (depth * 2) + 3; i++) {
15485 spaces.append(' ').append(' ');
15486 }
15487 return spaces.toString();
15488 }
15489
15490 /**
15491 * <p>Return the offset of the widget's text baseline from the widget's top
15492 * boundary. If this widget does not support baseline alignment, this
15493 * method returns -1. </p>
15494 *
15495 * @return the offset of the baseline within the widget's bounds or -1
15496 * if baseline alignment is not supported
15497 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015498 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015499 public int getBaseline() {
15500 return -1;
15501 }
15502
15503 /**
Chet Haase97140572012-09-13 14:56:47 -070015504 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15505 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15506 * a layout pass.
15507 *
15508 * @return whether the view hierarchy is currently undergoing a layout pass
15509 */
15510 public boolean isInLayout() {
15511 ViewRootImpl viewRoot = getViewRootImpl();
15512 return (viewRoot != null && viewRoot.isInLayout());
15513 }
15514
15515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015516 * Call this when something has changed which has invalidated the
15517 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015518 * tree. This should not be called while the view hierarchy is currently in a layout
15519 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15520 * end of the current layout pass (and then layout will run again) or after the current
15521 * frame is drawn and the next layout occurs.
15522 *
15523 * <p>Subclasses which override this method should call the superclass method to
15524 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015525 */
15526 public void requestLayout() {
Chet Haasecc699b42012-12-13 09:06:55 -080015527 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
15528 // Only trigger request-during-layout logic if this is the view requesting it,
15529 // not the views in its parent hierarchy
15530 ViewRootImpl viewRoot = getViewRootImpl();
15531 if (viewRoot != null && viewRoot.isInLayout()) {
15532 if (!viewRoot.requestLayoutDuringLayout(this)) {
15533 return;
15534 }
15535 }
15536 mAttachInfo.mViewRequestingLayout = this;
Chet Haase97140572012-09-13 14:56:47 -070015537 }
Chet Haasecc699b42012-12-13 09:06:55 -080015538
Dianne Hackborn4702a852012-08-17 15:18:29 -070015539 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15540 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015541
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015542 if (mParent != null && !mParent.isLayoutRequested()) {
15543 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015544 }
Chet Haasecc699b42012-12-13 09:06:55 -080015545 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
15546 mAttachInfo.mViewRequestingLayout = null;
15547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015548 }
15549
15550 /**
15551 * Forces this view to be laid out during the next layout pass.
15552 * This method does not call requestLayout() or forceLayout()
15553 * on the parent.
15554 */
15555 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015556 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15557 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015558 }
15559
15560 /**
15561 * <p>
15562 * This is called to find out how big a view should be. The parent
15563 * supplies constraint information in the width and height parameters.
15564 * </p>
15565 *
15566 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015567 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015568 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015569 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015570 * </p>
15571 *
15572 *
15573 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15574 * parent
15575 * @param heightMeasureSpec Vertical space requirements as imposed by the
15576 * parent
15577 *
15578 * @see #onMeasure(int, int)
15579 */
15580 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015581 boolean optical = isLayoutModeOptical(this);
15582 if (optical != isLayoutModeOptical(mParent)) {
15583 Insets insets = getOpticalInsets();
15584 int oWidth = insets.left + insets.right;
15585 int oHeight = insets.top + insets.bottom;
15586 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15587 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15588 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015589 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015590 widthMeasureSpec != mOldWidthMeasureSpec ||
15591 heightMeasureSpec != mOldHeightMeasureSpec) {
15592
15593 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015594 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015595
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015596 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015598 // measure ourselves, this should set the measured dimension flag back
15599 onMeasure(widthMeasureSpec, heightMeasureSpec);
15600
15601 // flag not set, setMeasuredDimension() was not invoked, we raise
15602 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015603 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015604 throw new IllegalStateException("onMeasure() did not set the"
15605 + " measured dimension by calling"
15606 + " setMeasuredDimension()");
15607 }
15608
Dianne Hackborn4702a852012-08-17 15:18:29 -070015609 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015610 }
15611
15612 mOldWidthMeasureSpec = widthMeasureSpec;
15613 mOldHeightMeasureSpec = heightMeasureSpec;
15614 }
15615
15616 /**
15617 * <p>
15618 * Measure the view and its content to determine the measured width and the
15619 * measured height. This method is invoked by {@link #measure(int, int)} and
15620 * should be overriden by subclasses to provide accurate and efficient
15621 * measurement of their contents.
15622 * </p>
15623 *
15624 * <p>
15625 * <strong>CONTRACT:</strong> When overriding this method, you
15626 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15627 * measured width and height of this view. Failure to do so will trigger an
15628 * <code>IllegalStateException</code>, thrown by
15629 * {@link #measure(int, int)}. Calling the superclass'
15630 * {@link #onMeasure(int, int)} is a valid use.
15631 * </p>
15632 *
15633 * <p>
15634 * The base class implementation of measure defaults to the background size,
15635 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15636 * override {@link #onMeasure(int, int)} to provide better measurements of
15637 * their content.
15638 * </p>
15639 *
15640 * <p>
15641 * If this method is overridden, it is the subclass's responsibility to make
15642 * sure the measured height and width are at least the view's minimum height
15643 * and width ({@link #getSuggestedMinimumHeight()} and
15644 * {@link #getSuggestedMinimumWidth()}).
15645 * </p>
15646 *
15647 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15648 * The requirements are encoded with
15649 * {@link android.view.View.MeasureSpec}.
15650 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15651 * The requirements are encoded with
15652 * {@link android.view.View.MeasureSpec}.
15653 *
15654 * @see #getMeasuredWidth()
15655 * @see #getMeasuredHeight()
15656 * @see #setMeasuredDimension(int, int)
15657 * @see #getSuggestedMinimumHeight()
15658 * @see #getSuggestedMinimumWidth()
15659 * @see android.view.View.MeasureSpec#getMode(int)
15660 * @see android.view.View.MeasureSpec#getSize(int)
15661 */
15662 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15663 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15664 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15665 }
15666
15667 /**
15668 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15669 * measured width and measured height. Failing to do so will trigger an
15670 * exception at measurement time.</p>
15671 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015672 * @param measuredWidth The measured width of this view. May be a complex
15673 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15674 * {@link #MEASURED_STATE_TOO_SMALL}.
15675 * @param measuredHeight The measured height of this view. May be a complex
15676 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15677 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015678 */
15679 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015680 boolean optical = isLayoutModeOptical(this);
15681 if (optical != isLayoutModeOptical(mParent)) {
15682 Insets insets = getOpticalInsets();
15683 int opticalWidth = insets.left + insets.right;
15684 int opticalHeight = insets.top + insets.bottom;
15685
15686 measuredWidth += optical ? opticalWidth : -opticalWidth;
15687 measuredHeight += optical ? opticalHeight : -opticalHeight;
15688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015689 mMeasuredWidth = measuredWidth;
15690 mMeasuredHeight = measuredHeight;
15691
Dianne Hackborn4702a852012-08-17 15:18:29 -070015692 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015693 }
15694
15695 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015696 * Merge two states as returned by {@link #getMeasuredState()}.
15697 * @param curState The current state as returned from a view or the result
15698 * of combining multiple views.
15699 * @param newState The new view state to combine.
15700 * @return Returns a new integer reflecting the combination of the two
15701 * states.
15702 */
15703 public static int combineMeasuredStates(int curState, int newState) {
15704 return curState | newState;
15705 }
15706
15707 /**
15708 * Version of {@link #resolveSizeAndState(int, int, int)}
15709 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15710 */
15711 public static int resolveSize(int size, int measureSpec) {
15712 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15713 }
15714
15715 /**
15716 * Utility to reconcile a desired size and state, with constraints imposed
15717 * by a MeasureSpec. Will take the desired size, unless a different size
15718 * is imposed by the constraints. The returned value is a compound integer,
15719 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15720 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15721 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015722 *
15723 * @param size How big the view wants to be
15724 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015725 * @return Size information bit mask as defined by
15726 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015727 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015728 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015729 int result = size;
15730 int specMode = MeasureSpec.getMode(measureSpec);
15731 int specSize = MeasureSpec.getSize(measureSpec);
15732 switch (specMode) {
15733 case MeasureSpec.UNSPECIFIED:
15734 result = size;
15735 break;
15736 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015737 if (specSize < size) {
15738 result = specSize | MEASURED_STATE_TOO_SMALL;
15739 } else {
15740 result = size;
15741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015742 break;
15743 case MeasureSpec.EXACTLY:
15744 result = specSize;
15745 break;
15746 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015747 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015748 }
15749
15750 /**
15751 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015752 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015753 * by the MeasureSpec.
15754 *
15755 * @param size Default size for this view
15756 * @param measureSpec Constraints imposed by the parent
15757 * @return The size this view should be.
15758 */
15759 public static int getDefaultSize(int size, int measureSpec) {
15760 int result = size;
15761 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015762 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015763
15764 switch (specMode) {
15765 case MeasureSpec.UNSPECIFIED:
15766 result = size;
15767 break;
15768 case MeasureSpec.AT_MOST:
15769 case MeasureSpec.EXACTLY:
15770 result = specSize;
15771 break;
15772 }
15773 return result;
15774 }
15775
15776 /**
15777 * Returns the suggested minimum height that the view should use. This
15778 * returns the maximum of the view's minimum height
15779 * and the background's minimum height
15780 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15781 * <p>
15782 * When being used in {@link #onMeasure(int, int)}, the caller should still
15783 * ensure the returned height is within the requirements of the parent.
15784 *
15785 * @return The suggested minimum height of the view.
15786 */
15787 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015788 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015790 }
15791
15792 /**
15793 * Returns the suggested minimum width that the view should use. This
15794 * returns the maximum of the view's minimum width)
15795 * and the background's minimum width
15796 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15797 * <p>
15798 * When being used in {@link #onMeasure(int, int)}, the caller should still
15799 * ensure the returned width is within the requirements of the parent.
15800 *
15801 * @return The suggested minimum width of the view.
15802 */
15803 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015804 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015806
Philip Milne6c8ea062012-04-03 17:38:43 -070015807 /**
15808 * Returns the minimum height of the view.
15809 *
15810 * @return the minimum height the view will try to be.
15811 *
15812 * @see #setMinimumHeight(int)
15813 *
15814 * @attr ref android.R.styleable#View_minHeight
15815 */
15816 public int getMinimumHeight() {
15817 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015818 }
15819
15820 /**
15821 * Sets the minimum height of the view. It is not guaranteed the view will
15822 * be able to achieve this minimum height (for example, if its parent layout
15823 * constrains it with less available height).
15824 *
15825 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015826 *
15827 * @see #getMinimumHeight()
15828 *
15829 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 */
15831 public void setMinimumHeight(int minHeight) {
15832 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015833 requestLayout();
15834 }
15835
15836 /**
15837 * Returns the minimum width of the view.
15838 *
15839 * @return the minimum width the view will try to be.
15840 *
15841 * @see #setMinimumWidth(int)
15842 *
15843 * @attr ref android.R.styleable#View_minWidth
15844 */
15845 public int getMinimumWidth() {
15846 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015847 }
15848
15849 /**
15850 * Sets the minimum width of the view. It is not guaranteed the view will
15851 * be able to achieve this minimum width (for example, if its parent layout
15852 * constrains it with less available width).
15853 *
15854 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015855 *
15856 * @see #getMinimumWidth()
15857 *
15858 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015859 */
15860 public void setMinimumWidth(int minWidth) {
15861 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015862 requestLayout();
15863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015864 }
15865
15866 /**
15867 * Get the animation currently associated with this view.
15868 *
15869 * @return The animation that is currently playing or
15870 * scheduled to play for this view.
15871 */
15872 public Animation getAnimation() {
15873 return mCurrentAnimation;
15874 }
15875
15876 /**
15877 * Start the specified animation now.
15878 *
15879 * @param animation the animation to start now
15880 */
15881 public void startAnimation(Animation animation) {
15882 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15883 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015884 invalidateParentCaches();
15885 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015886 }
15887
15888 /**
15889 * Cancels any animations for this view.
15890 */
15891 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015892 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015893 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015895 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015896 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015897 }
15898
15899 /**
15900 * Sets the next animation to play for this view.
15901 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015902 * {@link #startAnimation(android.view.animation.Animation)} instead.
15903 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015904 * control over the start time and invalidation, but you
15905 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015906 * 2) the view's parent (which controls animations on its children)
15907 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015908 * start.
15909 *
15910 * @param animation The next animation, or null.
15911 */
15912 public void setAnimation(Animation animation) {
15913 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015915 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015916 // If the screen is off assume the animation start time is now instead of
15917 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15918 // would cause the animation to start when the screen turns back on
15919 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15920 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15921 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015923 animation.reset();
15924 }
15925 }
15926
15927 /**
15928 * Invoked by a parent ViewGroup to notify the start of the animation
15929 * currently associated with this view. If you override this method,
15930 * always call super.onAnimationStart();
15931 *
15932 * @see #setAnimation(android.view.animation.Animation)
15933 * @see #getAnimation()
15934 */
15935 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015936 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015937 }
15938
15939 /**
15940 * Invoked by a parent ViewGroup to notify the end of the animation
15941 * currently associated with this view. If you override this method,
15942 * always call super.onAnimationEnd();
15943 *
15944 * @see #setAnimation(android.view.animation.Animation)
15945 * @see #getAnimation()
15946 */
15947 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015948 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015949 }
15950
15951 /**
15952 * Invoked if there is a Transform that involves alpha. Subclass that can
15953 * draw themselves with the specified alpha should return true, and then
15954 * respect that alpha when their onDraw() is called. If this returns false
15955 * then the view may be redirected to draw into an offscreen buffer to
15956 * fulfill the request, which will look fine, but may be slower than if the
15957 * subclass handles it internally. The default implementation returns false.
15958 *
15959 * @param alpha The alpha (0..255) to apply to the view's drawing
15960 * @return true if the view can draw with the specified alpha.
15961 */
15962 protected boolean onSetAlpha(int alpha) {
15963 return false;
15964 }
15965
15966 /**
15967 * This is used by the RootView to perform an optimization when
15968 * the view hierarchy contains one or several SurfaceView.
15969 * SurfaceView is always considered transparent, but its children are not,
15970 * therefore all View objects remove themselves from the global transparent
15971 * region (passed as a parameter to this function).
15972 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015973 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015974 *
15975 * @return Returns true if the effective visibility of the view at this
15976 * point is opaque, regardless of the transparent region; returns false
15977 * if it is possible for underlying windows to be seen behind the view.
15978 *
15979 * {@hide}
15980 */
15981 public boolean gatherTransparentRegion(Region region) {
15982 final AttachInfo attachInfo = mAttachInfo;
15983 if (region != null && attachInfo != null) {
15984 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015985 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015986 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15987 // remove it from the transparent region.
15988 final int[] location = attachInfo.mTransparentLocation;
15989 getLocationInWindow(location);
15990 region.op(location[0], location[1], location[0] + mRight - mLeft,
15991 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015992 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015993 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15994 // exists, so we remove the background drawable's non-transparent
15995 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015996 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015997 }
15998 }
15999 return true;
16000 }
16001
16002 /**
16003 * Play a sound effect for this view.
16004 *
16005 * <p>The framework will play sound effects for some built in actions, such as
16006 * clicking, but you may wish to play these effects in your widget,
16007 * for instance, for internal navigation.
16008 *
16009 * <p>The sound effect will only be played if sound effects are enabled by the user, and
16010 * {@link #isSoundEffectsEnabled()} is true.
16011 *
16012 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
16013 */
16014 public void playSoundEffect(int soundConstant) {
16015 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
16016 return;
16017 }
16018 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
16019 }
16020
16021 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016022 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016023 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016024 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016025 *
16026 * <p>The framework will provide haptic feedback for some built in actions,
16027 * such as long presses, but you may wish to provide feedback for your
16028 * own widget.
16029 *
16030 * <p>The feedback will only be performed if
16031 * {@link #isHapticFeedbackEnabled()} is true.
16032 *
16033 * @param feedbackConstant One of the constants defined in
16034 * {@link HapticFeedbackConstants}
16035 */
16036 public boolean performHapticFeedback(int feedbackConstant) {
16037 return performHapticFeedback(feedbackConstant, 0);
16038 }
16039
16040 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016041 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016042 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016043 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016044 *
16045 * @param feedbackConstant One of the constants defined in
16046 * {@link HapticFeedbackConstants}
16047 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16048 */
16049 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16050 if (mAttachInfo == null) {
16051 return false;
16052 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016053 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016054 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016055 && !isHapticFeedbackEnabled()) {
16056 return false;
16057 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016058 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16059 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016060 }
16061
16062 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016063 * Request that the visibility of the status bar or other screen/window
16064 * decorations be changed.
16065 *
16066 * <p>This method is used to put the over device UI into temporary modes
16067 * where the user's attention is focused more on the application content,
16068 * by dimming or hiding surrounding system affordances. This is typically
16069 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16070 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16071 * to be placed behind the action bar (and with these flags other system
16072 * affordances) so that smooth transitions between hiding and showing them
16073 * can be done.
16074 *
16075 * <p>Two representative examples of the use of system UI visibility is
16076 * implementing a content browsing application (like a magazine reader)
16077 * and a video playing application.
16078 *
16079 * <p>The first code shows a typical implementation of a View in a content
16080 * browsing application. In this implementation, the application goes
16081 * into a content-oriented mode by hiding the status bar and action bar,
16082 * and putting the navigation elements into lights out mode. The user can
16083 * then interact with content while in this mode. Such an application should
16084 * provide an easy way for the user to toggle out of the mode (such as to
16085 * check information in the status bar or access notifications). In the
16086 * implementation here, this is done simply by tapping on the content.
16087 *
16088 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16089 * content}
16090 *
16091 * <p>This second code sample shows a typical implementation of a View
16092 * in a video playing application. In this situation, while the video is
16093 * playing the application would like to go into a complete full-screen mode,
16094 * to use as much of the display as possible for the video. When in this state
16095 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016096 * touching on the screen to pop the UI out of full screen mode. See
16097 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016098 *
16099 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16100 * content}
16101 *
16102 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16103 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16104 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16105 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016106 */
16107 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016108 if (visibility != mSystemUiVisibility) {
16109 mSystemUiVisibility = visibility;
16110 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16111 mParent.recomputeViewAttributes(this);
16112 }
Joe Onorato664644d2011-01-23 17:53:23 -080016113 }
16114 }
16115
16116 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016117 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16118 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16119 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16120 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16121 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016122 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016123 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016124 return mSystemUiVisibility;
16125 }
16126
Scott Mainec6331b2011-05-24 16:55:56 -070016127 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016128 * Returns the current system UI visibility that is currently set for
16129 * the entire window. This is the combination of the
16130 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16131 * views in the window.
16132 */
16133 public int getWindowSystemUiVisibility() {
16134 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16135 }
16136
16137 /**
16138 * Override to find out when the window's requested system UI visibility
16139 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16140 * This is different from the callbacks recieved through
16141 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16142 * in that this is only telling you about the local request of the window,
16143 * not the actual values applied by the system.
16144 */
16145 public void onWindowSystemUiVisibilityChanged(int visible) {
16146 }
16147
16148 /**
16149 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16150 * the view hierarchy.
16151 */
16152 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16153 onWindowSystemUiVisibilityChanged(visible);
16154 }
16155
16156 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016157 * Set a listener to receive callbacks when the visibility of the system bar changes.
16158 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16159 */
Joe Onorato664644d2011-01-23 17:53:23 -080016160 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016161 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016162 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16163 mParent.recomputeViewAttributes(this);
16164 }
16165 }
16166
16167 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016168 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16169 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016170 */
16171 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016172 ListenerInfo li = mListenerInfo;
16173 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16174 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016175 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016176 }
16177 }
16178
Dianne Hackborncf675782012-05-10 15:07:24 -070016179 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016180 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16181 if (val != mSystemUiVisibility) {
16182 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016183 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016184 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016185 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016186 }
16187
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016188 /** @hide */
16189 public void setDisabledSystemUiVisibility(int flags) {
16190 if (mAttachInfo != null) {
16191 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16192 mAttachInfo.mDisabledSystemUiVisibility = flags;
16193 if (mParent != null) {
16194 mParent.recomputeViewAttributes(this);
16195 }
16196 }
16197 }
16198 }
16199
Joe Onorato664644d2011-01-23 17:53:23 -080016200 /**
Joe Malin32736f02011-01-19 16:14:20 -080016201 * Creates an image that the system displays during the drag and drop
16202 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16203 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16204 * appearance as the given View. The default also positions the center of the drag shadow
16205 * directly under the touch point. If no View is provided (the constructor with no parameters
16206 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16207 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16208 * default is an invisible drag shadow.
16209 * <p>
16210 * You are not required to use the View you provide to the constructor as the basis of the
16211 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16212 * anything you want as the drag shadow.
16213 * </p>
16214 * <p>
16215 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16216 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16217 * size and position of the drag shadow. It uses this data to construct a
16218 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16219 * so that your application can draw the shadow image in the Canvas.
16220 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016221 *
16222 * <div class="special reference">
16223 * <h3>Developer Guides</h3>
16224 * <p>For a guide to implementing drag and drop features, read the
16225 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16226 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016227 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016228 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016229 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016230
16231 /**
Joe Malin32736f02011-01-19 16:14:20 -080016232 * Constructs a shadow image builder based on a View. By default, the resulting drag
16233 * shadow will have the same appearance and dimensions as the View, with the touch point
16234 * over the center of the View.
16235 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016236 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016237 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016238 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016239 }
16240
Christopher Tate17ed60c2011-01-18 12:50:26 -080016241 /**
16242 * Construct a shadow builder object with no associated View. This
16243 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16244 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16245 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016246 * reference to any View object. If they are not overridden, then the result is an
16247 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016248 */
16249 public DragShadowBuilder() {
16250 mView = new WeakReference<View>(null);
16251 }
16252
16253 /**
16254 * Returns the View object that had been passed to the
16255 * {@link #View.DragShadowBuilder(View)}
16256 * constructor. If that View parameter was {@code null} or if the
16257 * {@link #View.DragShadowBuilder()}
16258 * constructor was used to instantiate the builder object, this method will return
16259 * null.
16260 *
16261 * @return The View object associate with this builder object.
16262 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016263 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016264 final public View getView() {
16265 return mView.get();
16266 }
16267
Christopher Tate2c095f32010-10-04 14:13:40 -070016268 /**
Joe Malin32736f02011-01-19 16:14:20 -080016269 * Provides the metrics for the shadow image. These include the dimensions of
16270 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016271 * be centered under the touch location while dragging.
16272 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016273 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016274 * same as the dimensions of the View itself and centers the shadow under
16275 * the touch point.
16276 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016277 *
Joe Malin32736f02011-01-19 16:14:20 -080016278 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16279 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16280 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16281 * image.
16282 *
16283 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16284 * shadow image that should be underneath the touch point during the drag and drop
16285 * operation. Your application must set {@link android.graphics.Point#x} to the
16286 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016287 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016288 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016289 final View view = mView.get();
16290 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016291 shadowSize.set(view.getWidth(), view.getHeight());
16292 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016293 } else {
16294 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16295 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016296 }
16297
16298 /**
Joe Malin32736f02011-01-19 16:14:20 -080016299 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16300 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016301 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016302 *
Joe Malin32736f02011-01-19 16:14:20 -080016303 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016304 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016305 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016306 final View view = mView.get();
16307 if (view != null) {
16308 view.draw(canvas);
16309 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016310 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016311 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016312 }
16313 }
16314
16315 /**
Joe Malin32736f02011-01-19 16:14:20 -080016316 * Starts a drag and drop operation. When your application calls this method, it passes a
16317 * {@link android.view.View.DragShadowBuilder} object to the system. The
16318 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16319 * to get metrics for the drag shadow, and then calls the object's
16320 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16321 * <p>
16322 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16323 * drag events to all the View objects in your application that are currently visible. It does
16324 * this either by calling the View object's drag listener (an implementation of
16325 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16326 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16327 * Both are passed a {@link android.view.DragEvent} object that has a
16328 * {@link android.view.DragEvent#getAction()} value of
16329 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16330 * </p>
16331 * <p>
16332 * Your application can invoke startDrag() on any attached View object. The View object does not
16333 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16334 * be related to the View the user selected for dragging.
16335 * </p>
16336 * @param data A {@link android.content.ClipData} object pointing to the data to be
16337 * transferred by the drag and drop operation.
16338 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16339 * drag shadow.
16340 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16341 * drop operation. This Object is put into every DragEvent object sent by the system during the
16342 * current drag.
16343 * <p>
16344 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16345 * to the target Views. For example, it can contain flags that differentiate between a
16346 * a copy operation and a move operation.
16347 * </p>
16348 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16349 * so the parameter should be set to 0.
16350 * @return {@code true} if the method completes successfully, or
16351 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16352 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016353 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016354 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016355 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016356 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016357 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016358 }
16359 boolean okay = false;
16360
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016361 Point shadowSize = new Point();
16362 Point shadowTouchPoint = new Point();
16363 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016364
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016365 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16366 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16367 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016368 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016369
Chris Tatea32dcf72010-10-14 12:13:50 -070016370 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016371 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16372 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016373 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016374 Surface surface = new Surface();
16375 try {
16376 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016377 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016378 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016379 + " surface=" + surface);
16380 if (token != null) {
16381 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016382 try {
Chris Tate6b391282010-10-14 15:48:59 -070016383 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016384 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016385 } finally {
16386 surface.unlockCanvasAndPost(canvas);
16387 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016388
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016389 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016390
16391 // Cache the local state object for delivery with DragEvents
16392 root.setLocalDragState(myLocalState);
16393
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016394 // repurpose 'shadowSize' for the last touch point
16395 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016396
Christopher Tatea53146c2010-09-07 11:57:52 -070016397 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016398 shadowSize.x, shadowSize.y,
16399 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016400 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016401
16402 // Off and running! Release our local surface instance; the drag
16403 // shadow surface is now managed by the system process.
16404 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016405 }
16406 } catch (Exception e) {
16407 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16408 surface.destroy();
16409 }
16410
16411 return okay;
16412 }
16413
Christopher Tatea53146c2010-09-07 11:57:52 -070016414 /**
Joe Malin32736f02011-01-19 16:14:20 -080016415 * Handles drag events sent by the system following a call to
16416 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16417 *<p>
16418 * When the system calls this method, it passes a
16419 * {@link android.view.DragEvent} object. A call to
16420 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16421 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16422 * operation.
16423 * @param event The {@link android.view.DragEvent} sent by the system.
16424 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16425 * in DragEvent, indicating the type of drag event represented by this object.
16426 * @return {@code true} if the method was successful, otherwise {@code false}.
16427 * <p>
16428 * The method should return {@code true} in response to an action type of
16429 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16430 * operation.
16431 * </p>
16432 * <p>
16433 * The method should also return {@code true} in response to an action type of
16434 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16435 * {@code false} if it didn't.
16436 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016437 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016438 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016439 return false;
16440 }
16441
16442 /**
Joe Malin32736f02011-01-19 16:14:20 -080016443 * Detects if this View is enabled and has a drag event listener.
16444 * If both are true, then it calls the drag event listener with the
16445 * {@link android.view.DragEvent} it received. If the drag event listener returns
16446 * {@code true}, then dispatchDragEvent() returns {@code true}.
16447 * <p>
16448 * For all other cases, the method calls the
16449 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16450 * method and returns its result.
16451 * </p>
16452 * <p>
16453 * This ensures that a drag event is always consumed, even if the View does not have a drag
16454 * event listener. However, if the View has a listener and the listener returns true, then
16455 * onDragEvent() is not called.
16456 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016457 */
16458 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016459 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016460 ListenerInfo li = mListenerInfo;
16461 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16462 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016463 return true;
16464 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016465 return onDragEvent(event);
16466 }
16467
Christopher Tate3d4bf172011-03-28 16:16:46 -070016468 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016469 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016470 }
16471
Christopher Tatea53146c2010-09-07 11:57:52 -070016472 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016473 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16474 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016475 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016476 */
16477 public void onCloseSystemDialogs(String reason) {
16478 }
Joe Malin32736f02011-01-19 16:14:20 -080016479
Dianne Hackbornffa42482009-09-23 22:20:11 -070016480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016481 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016482 * update a Region being computed for
16483 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016484 * that any non-transparent parts of the Drawable are removed from the
16485 * given transparent region.
16486 *
16487 * @param dr The Drawable whose transparency is to be applied to the region.
16488 * @param region A Region holding the current transparency information,
16489 * where any parts of the region that are set are considered to be
16490 * transparent. On return, this region will be modified to have the
16491 * transparency information reduced by the corresponding parts of the
16492 * Drawable that are not transparent.
16493 * {@hide}
16494 */
16495 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16496 if (DBG) {
16497 Log.i("View", "Getting transparent region for: " + this);
16498 }
16499 final Region r = dr.getTransparentRegion();
16500 final Rect db = dr.getBounds();
16501 final AttachInfo attachInfo = mAttachInfo;
16502 if (r != null && attachInfo != null) {
16503 final int w = getRight()-getLeft();
16504 final int h = getBottom()-getTop();
16505 if (db.left > 0) {
16506 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16507 r.op(0, 0, db.left, h, Region.Op.UNION);
16508 }
16509 if (db.right < w) {
16510 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16511 r.op(db.right, 0, w, h, Region.Op.UNION);
16512 }
16513 if (db.top > 0) {
16514 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16515 r.op(0, 0, w, db.top, Region.Op.UNION);
16516 }
16517 if (db.bottom < h) {
16518 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16519 r.op(0, db.bottom, w, h, Region.Op.UNION);
16520 }
16521 final int[] location = attachInfo.mTransparentLocation;
16522 getLocationInWindow(location);
16523 r.translate(location[0], location[1]);
16524 region.op(r, Region.Op.INTERSECT);
16525 } else {
16526 region.op(db, Region.Op.DIFFERENCE);
16527 }
16528 }
16529
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016530 private void checkForLongClick(int delayOffset) {
16531 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16532 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016533
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016534 if (mPendingCheckForLongPress == null) {
16535 mPendingCheckForLongPress = new CheckForLongPress();
16536 }
16537 mPendingCheckForLongPress.rememberWindowAttachCount();
16538 postDelayed(mPendingCheckForLongPress,
16539 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016541 }
16542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016543 /**
16544 * Inflate a view from an XML resource. This convenience method wraps the {@link
16545 * LayoutInflater} class, which provides a full range of options for view inflation.
16546 *
16547 * @param context The Context object for your activity or application.
16548 * @param resource The resource ID to inflate
16549 * @param root A view group that will be the parent. Used to properly inflate the
16550 * layout_* parameters.
16551 * @see LayoutInflater
16552 */
16553 public static View inflate(Context context, int resource, ViewGroup root) {
16554 LayoutInflater factory = LayoutInflater.from(context);
16555 return factory.inflate(resource, root);
16556 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016558 /**
Adam Powell637d3372010-08-25 14:37:03 -070016559 * Scroll the view with standard behavior for scrolling beyond the normal
16560 * content boundaries. Views that call this method should override
16561 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16562 * results of an over-scroll operation.
16563 *
16564 * Views can use this method to handle any touch or fling-based scrolling.
16565 *
16566 * @param deltaX Change in X in pixels
16567 * @param deltaY Change in Y in pixels
16568 * @param scrollX Current X scroll value in pixels before applying deltaX
16569 * @param scrollY Current Y scroll value in pixels before applying deltaY
16570 * @param scrollRangeX Maximum content scroll range along the X axis
16571 * @param scrollRangeY Maximum content scroll range along the Y axis
16572 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16573 * along the X axis.
16574 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16575 * along the Y axis.
16576 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16577 * @return true if scrolling was clamped to an over-scroll boundary along either
16578 * axis, false otherwise.
16579 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016580 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016581 protected boolean overScrollBy(int deltaX, int deltaY,
16582 int scrollX, int scrollY,
16583 int scrollRangeX, int scrollRangeY,
16584 int maxOverScrollX, int maxOverScrollY,
16585 boolean isTouchEvent) {
16586 final int overScrollMode = mOverScrollMode;
16587 final boolean canScrollHorizontal =
16588 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16589 final boolean canScrollVertical =
16590 computeVerticalScrollRange() > computeVerticalScrollExtent();
16591 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16592 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16593 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16594 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16595
16596 int newScrollX = scrollX + deltaX;
16597 if (!overScrollHorizontal) {
16598 maxOverScrollX = 0;
16599 }
16600
16601 int newScrollY = scrollY + deltaY;
16602 if (!overScrollVertical) {
16603 maxOverScrollY = 0;
16604 }
16605
16606 // Clamp values if at the limits and record
16607 final int left = -maxOverScrollX;
16608 final int right = maxOverScrollX + scrollRangeX;
16609 final int top = -maxOverScrollY;
16610 final int bottom = maxOverScrollY + scrollRangeY;
16611
16612 boolean clampedX = false;
16613 if (newScrollX > right) {
16614 newScrollX = right;
16615 clampedX = true;
16616 } else if (newScrollX < left) {
16617 newScrollX = left;
16618 clampedX = true;
16619 }
16620
16621 boolean clampedY = false;
16622 if (newScrollY > bottom) {
16623 newScrollY = bottom;
16624 clampedY = true;
16625 } else if (newScrollY < top) {
16626 newScrollY = top;
16627 clampedY = true;
16628 }
16629
16630 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16631
16632 return clampedX || clampedY;
16633 }
16634
16635 /**
16636 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16637 * respond to the results of an over-scroll operation.
16638 *
16639 * @param scrollX New X scroll value in pixels
16640 * @param scrollY New Y scroll value in pixels
16641 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16642 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16643 */
16644 protected void onOverScrolled(int scrollX, int scrollY,
16645 boolean clampedX, boolean clampedY) {
16646 // Intentionally empty.
16647 }
16648
16649 /**
16650 * Returns the over-scroll mode for this view. The result will be
16651 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16652 * (allow over-scrolling only if the view content is larger than the container),
16653 * or {@link #OVER_SCROLL_NEVER}.
16654 *
16655 * @return This view's over-scroll mode.
16656 */
16657 public int getOverScrollMode() {
16658 return mOverScrollMode;
16659 }
16660
16661 /**
16662 * Set the over-scroll mode for this view. Valid over-scroll modes are
16663 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16664 * (allow over-scrolling only if the view content is larger than the container),
16665 * or {@link #OVER_SCROLL_NEVER}.
16666 *
16667 * Setting the over-scroll mode of a view will have an effect only if the
16668 * view is capable of scrolling.
16669 *
16670 * @param overScrollMode The new over-scroll mode for this view.
16671 */
16672 public void setOverScrollMode(int overScrollMode) {
16673 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16674 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16675 overScrollMode != OVER_SCROLL_NEVER) {
16676 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16677 }
16678 mOverScrollMode = overScrollMode;
16679 }
16680
16681 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016682 * Gets a scale factor that determines the distance the view should scroll
16683 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16684 * @return The vertical scroll scale factor.
16685 * @hide
16686 */
16687 protected float getVerticalScrollFactor() {
16688 if (mVerticalScrollFactor == 0) {
16689 TypedValue outValue = new TypedValue();
16690 if (!mContext.getTheme().resolveAttribute(
16691 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16692 throw new IllegalStateException(
16693 "Expected theme to define listPreferredItemHeight.");
16694 }
16695 mVerticalScrollFactor = outValue.getDimension(
16696 mContext.getResources().getDisplayMetrics());
16697 }
16698 return mVerticalScrollFactor;
16699 }
16700
16701 /**
16702 * Gets a scale factor that determines the distance the view should scroll
16703 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16704 * @return The horizontal scroll scale factor.
16705 * @hide
16706 */
16707 protected float getHorizontalScrollFactor() {
16708 // TODO: Should use something else.
16709 return getVerticalScrollFactor();
16710 }
16711
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016712 /**
16713 * Return the value specifying the text direction or policy that was set with
16714 * {@link #setTextDirection(int)}.
16715 *
16716 * @return the defined text direction. It can be one of:
16717 *
16718 * {@link #TEXT_DIRECTION_INHERIT},
16719 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16720 * {@link #TEXT_DIRECTION_ANY_RTL},
16721 * {@link #TEXT_DIRECTION_LTR},
16722 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016723 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016724 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016725 * @attr ref android.R.styleable#View_textDirection
16726 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016727 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016728 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016729 @ViewDebug.ExportedProperty(category = "text", mapping = {
16730 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16731 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16732 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16733 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16734 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16735 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16736 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016737 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016738 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016739 }
16740
16741 /**
16742 * Set the text direction.
16743 *
16744 * @param textDirection the direction to set. Should be one of:
16745 *
16746 * {@link #TEXT_DIRECTION_INHERIT},
16747 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16748 * {@link #TEXT_DIRECTION_ANY_RTL},
16749 * {@link #TEXT_DIRECTION_LTR},
16750 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016751 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016752 *
16753 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16754 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16755 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016756 *
16757 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016758 */
16759 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016760 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016761 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016762 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016763 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016764 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016765 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016766 // Do resolution
16767 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016768 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016769 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016770 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016771 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016772 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016773 }
16774 }
16775
16776 /**
16777 * Return the resolved text direction.
16778 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016779 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016780 *
Doug Feltcb3791202011-07-07 11:57:48 -070016781 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16782 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016783 * {@link #TEXT_DIRECTION_LTR},
16784 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016785 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016786 *
16787 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016788 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016789 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016790 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016791 }
16792
16793 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016794 * Resolve the text direction.
16795 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016796 * @return true if resolution has been done, false otherwise.
16797 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016798 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016799 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016800 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016801 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016802 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016803
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016804 if (hasRtlSupport()) {
16805 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016806 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016807 switch(textDirection) {
16808 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016809 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016810 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016811 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016812 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016813 return false;
16814 }
16815
16816 View parent = ((View) mParent);
16817 // Parent has not yet resolved, so we still return the default
16818 if (!parent.isTextDirectionResolved()) {
16819 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16820 // Resolution will need to happen again later
16821 return false;
16822 }
16823
16824 // Set current resolved direction to the same value as the parent's one
16825 final int parentResolvedDirection = parent.getTextDirection();
16826 switch (parentResolvedDirection) {
16827 case TEXT_DIRECTION_FIRST_STRONG:
16828 case TEXT_DIRECTION_ANY_RTL:
16829 case TEXT_DIRECTION_LTR:
16830 case TEXT_DIRECTION_RTL:
16831 case TEXT_DIRECTION_LOCALE:
16832 mPrivateFlags2 |=
16833 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16834 break;
16835 default:
16836 // Default resolved direction is "first strong" heuristic
16837 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016838 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016839 break;
16840 case TEXT_DIRECTION_FIRST_STRONG:
16841 case TEXT_DIRECTION_ANY_RTL:
16842 case TEXT_DIRECTION_LTR:
16843 case TEXT_DIRECTION_RTL:
16844 case TEXT_DIRECTION_LOCALE:
16845 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016846 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016847 break;
16848 default:
16849 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016850 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016851 }
16852 } else {
16853 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016854 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016855 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016856
16857 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016858 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016859 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016860 }
16861
16862 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016863 * Check if text direction resolution can be done.
16864 *
16865 * @return true if text direction resolution can be done otherwise return false.
16866 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016867 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016868 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016869 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016870 return (mParent != null) && (mParent instanceof View) &&
16871 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016872 default:
16873 return true;
16874 }
16875 }
16876
16877 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016878 * Reset resolved text direction. Text direction will be resolved during a call to
16879 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016880 *
16881 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016882 */
16883 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016884 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016885 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016886 // Set to default value
16887 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016888 }
16889
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016890 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016891 * @return true if text direction is inherited.
16892 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016893 * @hide
16894 */
16895 public boolean isTextDirectionInherited() {
16896 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16897 }
16898
16899 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016900 * @return true if text direction is resolved.
16901 */
16902 private boolean isTextDirectionResolved() {
16903 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16904 }
16905
16906 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016907 * Return the value specifying the text alignment or policy that was set with
16908 * {@link #setTextAlignment(int)}.
16909 *
16910 * @return the defined text alignment. It can be one of:
16911 *
16912 * {@link #TEXT_ALIGNMENT_INHERIT},
16913 * {@link #TEXT_ALIGNMENT_GRAVITY},
16914 * {@link #TEXT_ALIGNMENT_CENTER},
16915 * {@link #TEXT_ALIGNMENT_TEXT_START},
16916 * {@link #TEXT_ALIGNMENT_TEXT_END},
16917 * {@link #TEXT_ALIGNMENT_VIEW_START},
16918 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016919 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016920 * @attr ref android.R.styleable#View_textAlignment
16921 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016922 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016923 */
16924 @ViewDebug.ExportedProperty(category = "text", mapping = {
16925 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16926 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16927 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16928 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16929 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16930 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16931 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16932 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016933 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016934 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016935 }
16936
16937 /**
16938 * Set the text alignment.
16939 *
16940 * @param textAlignment The text alignment to set. Should be one of
16941 *
16942 * {@link #TEXT_ALIGNMENT_INHERIT},
16943 * {@link #TEXT_ALIGNMENT_GRAVITY},
16944 * {@link #TEXT_ALIGNMENT_CENTER},
16945 * {@link #TEXT_ALIGNMENT_TEXT_START},
16946 * {@link #TEXT_ALIGNMENT_TEXT_END},
16947 * {@link #TEXT_ALIGNMENT_VIEW_START},
16948 * {@link #TEXT_ALIGNMENT_VIEW_END}
16949 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016950 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16951 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16952 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16953 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016954 * @attr ref android.R.styleable#View_textAlignment
16955 */
16956 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016957 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016958 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016959 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016960 resetResolvedTextAlignment();
16961 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016962 mPrivateFlags2 |=
16963 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16964 // Do resolution
16965 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016966 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016967 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016968 // Refresh
16969 requestLayout();
16970 invalidate(true);
16971 }
16972 }
16973
16974 /**
16975 * Return the resolved text alignment.
16976 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016977 * @return the resolved text alignment. Returns one of:
16978 *
16979 * {@link #TEXT_ALIGNMENT_GRAVITY},
16980 * {@link #TEXT_ALIGNMENT_CENTER},
16981 * {@link #TEXT_ALIGNMENT_TEXT_START},
16982 * {@link #TEXT_ALIGNMENT_TEXT_END},
16983 * {@link #TEXT_ALIGNMENT_VIEW_START},
16984 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016985 *
16986 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016987 */
16988 @ViewDebug.ExportedProperty(category = "text", mapping = {
16989 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16990 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16991 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16992 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16993 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16994 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16995 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16996 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016997 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016998 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16999 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017000 }
17001
17002 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017003 * Resolve the text alignment.
17004 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017005 * @return true if resolution has been done, false otherwise.
17006 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017008 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017009 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017010 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017011 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017012
17013 if (hasRtlSupport()) {
17014 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017015 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017016 switch (textAlignment) {
17017 case TEXT_ALIGNMENT_INHERIT:
17018 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017019 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017020 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070017021 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017022 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017023 return false;
17024 }
17025 View parent = (View) mParent;
17026
17027 // Parent has not yet resolved, so we still return the default
17028 if (!parent.isTextAlignmentResolved()) {
17029 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
17030 // Resolution will need to happen again later
17031 return false;
17032 }
17033
17034 final int parentResolvedTextAlignment = parent.getTextAlignment();
17035 switch (parentResolvedTextAlignment) {
17036 case TEXT_ALIGNMENT_GRAVITY:
17037 case TEXT_ALIGNMENT_TEXT_START:
17038 case TEXT_ALIGNMENT_TEXT_END:
17039 case TEXT_ALIGNMENT_CENTER:
17040 case TEXT_ALIGNMENT_VIEW_START:
17041 case TEXT_ALIGNMENT_VIEW_END:
17042 // Resolved text alignment is the same as the parent resolved
17043 // text alignment
17044 mPrivateFlags2 |=
17045 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17046 break;
17047 default:
17048 // Use default resolved text alignment
17049 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017050 }
17051 break;
17052 case TEXT_ALIGNMENT_GRAVITY:
17053 case TEXT_ALIGNMENT_TEXT_START:
17054 case TEXT_ALIGNMENT_TEXT_END:
17055 case TEXT_ALIGNMENT_CENTER:
17056 case TEXT_ALIGNMENT_VIEW_START:
17057 case TEXT_ALIGNMENT_VIEW_END:
17058 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017059 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017060 break;
17061 default:
17062 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017063 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017064 }
17065 } else {
17066 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017067 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017068 }
17069
17070 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017071 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017072 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017073 }
17074
17075 /**
17076 * Check if text alignment resolution can be done.
17077 *
17078 * @return true if text alignment resolution can be done otherwise return false.
17079 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017080 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017081 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017082 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017083 return (mParent != null) && (mParent instanceof View) &&
17084 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017085 default:
17086 return true;
17087 }
17088 }
17089
17090 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017091 * Reset resolved text alignment. Text alignment will be resolved during a call to
17092 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017093 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017094 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017095 */
17096 public void resetResolvedTextAlignment() {
17097 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017098 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017099 // Set to default
17100 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017101 }
17102
Adam Powella9108a22012-07-18 11:18:09 -070017103 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017104 * @return true if text alignment is inherited.
17105 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017106 * @hide
17107 */
17108 public boolean isTextAlignmentInherited() {
17109 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17110 }
17111
17112 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017113 * @return true if text alignment is resolved.
17114 */
17115 private boolean isTextAlignmentResolved() {
17116 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17117 }
17118
17119 /**
Adam Powella9108a22012-07-18 11:18:09 -070017120 * Generate a value suitable for use in {@link #setId(int)}.
17121 * This value will not collide with ID values generated at build time by aapt for R.id.
17122 *
17123 * @return a generated ID value
17124 */
17125 public static int generateViewId() {
17126 for (;;) {
17127 final int result = sNextGeneratedId.get();
17128 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17129 int newValue = result + 1;
17130 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17131 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17132 return result;
17133 }
17134 }
17135 }
17136
Chet Haaseb39f0512011-05-24 14:36:40 -070017137 //
17138 // Properties
17139 //
17140 /**
17141 * A Property wrapper around the <code>alpha</code> functionality handled by the
17142 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17143 */
Chet Haased47f1532011-12-16 11:18:52 -080017144 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017145 @Override
17146 public void setValue(View object, float value) {
17147 object.setAlpha(value);
17148 }
17149
17150 @Override
17151 public Float get(View object) {
17152 return object.getAlpha();
17153 }
17154 };
17155
17156 /**
17157 * A Property wrapper around the <code>translationX</code> functionality handled by the
17158 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17159 */
Chet Haased47f1532011-12-16 11:18:52 -080017160 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017161 @Override
17162 public void setValue(View object, float value) {
17163 object.setTranslationX(value);
17164 }
17165
17166 @Override
17167 public Float get(View object) {
17168 return object.getTranslationX();
17169 }
17170 };
17171
17172 /**
17173 * A Property wrapper around the <code>translationY</code> functionality handled by the
17174 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17175 */
Chet Haased47f1532011-12-16 11:18:52 -080017176 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017177 @Override
17178 public void setValue(View object, float value) {
17179 object.setTranslationY(value);
17180 }
17181
17182 @Override
17183 public Float get(View object) {
17184 return object.getTranslationY();
17185 }
17186 };
17187
17188 /**
17189 * A Property wrapper around the <code>x</code> functionality handled by the
17190 * {@link View#setX(float)} and {@link View#getX()} methods.
17191 */
Chet Haased47f1532011-12-16 11:18:52 -080017192 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017193 @Override
17194 public void setValue(View object, float value) {
17195 object.setX(value);
17196 }
17197
17198 @Override
17199 public Float get(View object) {
17200 return object.getX();
17201 }
17202 };
17203
17204 /**
17205 * A Property wrapper around the <code>y</code> functionality handled by the
17206 * {@link View#setY(float)} and {@link View#getY()} methods.
17207 */
Chet Haased47f1532011-12-16 11:18:52 -080017208 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017209 @Override
17210 public void setValue(View object, float value) {
17211 object.setY(value);
17212 }
17213
17214 @Override
17215 public Float get(View object) {
17216 return object.getY();
17217 }
17218 };
17219
17220 /**
17221 * A Property wrapper around the <code>rotation</code> functionality handled by the
17222 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17223 */
Chet Haased47f1532011-12-16 11:18:52 -080017224 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017225 @Override
17226 public void setValue(View object, float value) {
17227 object.setRotation(value);
17228 }
17229
17230 @Override
17231 public Float get(View object) {
17232 return object.getRotation();
17233 }
17234 };
17235
17236 /**
17237 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17238 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17239 */
Chet Haased47f1532011-12-16 11:18:52 -080017240 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017241 @Override
17242 public void setValue(View object, float value) {
17243 object.setRotationX(value);
17244 }
17245
17246 @Override
17247 public Float get(View object) {
17248 return object.getRotationX();
17249 }
17250 };
17251
17252 /**
17253 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17254 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17255 */
Chet Haased47f1532011-12-16 11:18:52 -080017256 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017257 @Override
17258 public void setValue(View object, float value) {
17259 object.setRotationY(value);
17260 }
17261
17262 @Override
17263 public Float get(View object) {
17264 return object.getRotationY();
17265 }
17266 };
17267
17268 /**
17269 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17270 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17271 */
Chet Haased47f1532011-12-16 11:18:52 -080017272 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017273 @Override
17274 public void setValue(View object, float value) {
17275 object.setScaleX(value);
17276 }
17277
17278 @Override
17279 public Float get(View object) {
17280 return object.getScaleX();
17281 }
17282 };
17283
17284 /**
17285 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17286 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17287 */
Chet Haased47f1532011-12-16 11:18:52 -080017288 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017289 @Override
17290 public void setValue(View object, float value) {
17291 object.setScaleY(value);
17292 }
17293
17294 @Override
17295 public Float get(View object) {
17296 return object.getScaleY();
17297 }
17298 };
17299
Jeff Brown33bbfd22011-02-24 20:55:35 -080017300 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017301 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17302 * Each MeasureSpec represents a requirement for either the width or the height.
17303 * A MeasureSpec is comprised of a size and a mode. There are three possible
17304 * modes:
17305 * <dl>
17306 * <dt>UNSPECIFIED</dt>
17307 * <dd>
17308 * The parent has not imposed any constraint on the child. It can be whatever size
17309 * it wants.
17310 * </dd>
17311 *
17312 * <dt>EXACTLY</dt>
17313 * <dd>
17314 * The parent has determined an exact size for the child. The child is going to be
17315 * given those bounds regardless of how big it wants to be.
17316 * </dd>
17317 *
17318 * <dt>AT_MOST</dt>
17319 * <dd>
17320 * The child can be as large as it wants up to the specified size.
17321 * </dd>
17322 * </dl>
17323 *
17324 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17325 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17326 */
17327 public static class MeasureSpec {
17328 private static final int MODE_SHIFT = 30;
17329 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17330
17331 /**
17332 * Measure specification mode: The parent has not imposed any constraint
17333 * on the child. It can be whatever size it wants.
17334 */
17335 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17336
17337 /**
17338 * Measure specification mode: The parent has determined an exact size
17339 * for the child. The child is going to be given those bounds regardless
17340 * of how big it wants to be.
17341 */
17342 public static final int EXACTLY = 1 << MODE_SHIFT;
17343
17344 /**
17345 * Measure specification mode: The child can be as large as it wants up
17346 * to the specified size.
17347 */
17348 public static final int AT_MOST = 2 << MODE_SHIFT;
17349
17350 /**
17351 * Creates a measure specification based on the supplied size and mode.
17352 *
17353 * The mode must always be one of the following:
17354 * <ul>
17355 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17356 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17357 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17358 * </ul>
17359 *
Adam Powell2c8cc972012-12-07 18:04:51 -080017360 * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
17361 * implementation was such that the order of arguments did not matter
17362 * and overflow in either value could impact the resulting MeasureSpec.
17363 * {@link android.widget.RelativeLayout} was affected by this bug.
17364 * Apps targeting API levels greater than 17 will get the fixed, more strict
17365 * behavior.</p>
17366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017367 * @param size the size of the measure specification
17368 * @param mode the mode of the measure specification
17369 * @return the measure specification based on size and mode
17370 */
17371 public static int makeMeasureSpec(int size, int mode) {
Adam Powell7da4b732012-12-07 15:28:33 -080017372 if (sUseBrokenMakeMeasureSpec) {
17373 return size + mode;
17374 } else {
17375 return (size & ~MODE_MASK) | (mode & MODE_MASK);
17376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017377 }
17378
17379 /**
17380 * Extracts the mode from the supplied measure specification.
17381 *
17382 * @param measureSpec the measure specification to extract the mode from
17383 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17384 * {@link android.view.View.MeasureSpec#AT_MOST} or
17385 * {@link android.view.View.MeasureSpec#EXACTLY}
17386 */
17387 public static int getMode(int measureSpec) {
17388 return (measureSpec & MODE_MASK);
17389 }
17390
17391 /**
17392 * Extracts the size from the supplied measure specification.
17393 *
17394 * @param measureSpec the measure specification to extract the size from
17395 * @return the size in pixels defined in the supplied measure specification
17396 */
17397 public static int getSize(int measureSpec) {
17398 return (measureSpec & ~MODE_MASK);
17399 }
17400
Philip Milne7b757812012-09-19 18:13:44 -070017401 static int adjust(int measureSpec, int delta) {
17402 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17403 }
17404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017405 /**
17406 * Returns a String representation of the specified measure
17407 * specification.
17408 *
17409 * @param measureSpec the measure specification to convert to a String
17410 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17411 */
17412 public static String toString(int measureSpec) {
17413 int mode = getMode(measureSpec);
17414 int size = getSize(measureSpec);
17415
17416 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17417
17418 if (mode == UNSPECIFIED)
17419 sb.append("UNSPECIFIED ");
17420 else if (mode == EXACTLY)
17421 sb.append("EXACTLY ");
17422 else if (mode == AT_MOST)
17423 sb.append("AT_MOST ");
17424 else
17425 sb.append(mode).append(" ");
17426
17427 sb.append(size);
17428 return sb.toString();
17429 }
17430 }
17431
17432 class CheckForLongPress implements Runnable {
17433
17434 private int mOriginalWindowAttachCount;
17435
17436 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017437 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017438 && mOriginalWindowAttachCount == mWindowAttachCount) {
17439 if (performLongClick()) {
17440 mHasPerformedLongPress = true;
17441 }
17442 }
17443 }
17444
17445 public void rememberWindowAttachCount() {
17446 mOriginalWindowAttachCount = mWindowAttachCount;
17447 }
17448 }
Joe Malin32736f02011-01-19 16:14:20 -080017449
Adam Powelle14579b2009-12-16 18:39:52 -080017450 private final class CheckForTap implements Runnable {
17451 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017452 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017453 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017454 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017455 }
17456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017457
Adam Powella35d7682010-03-12 14:48:13 -080017458 private final class PerformClick implements Runnable {
17459 public void run() {
17460 performClick();
17461 }
17462 }
17463
Dianne Hackborn63042d62011-01-26 18:56:29 -080017464 /** @hide */
17465 public void hackTurnOffWindowResizeAnim(boolean off) {
17466 mAttachInfo.mTurnOffWindowResizeAnim = off;
17467 }
Joe Malin32736f02011-01-19 16:14:20 -080017468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017469 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017470 * This method returns a ViewPropertyAnimator object, which can be used to animate
17471 * specific properties on this View.
17472 *
17473 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17474 */
17475 public ViewPropertyAnimator animate() {
17476 if (mAnimator == null) {
17477 mAnimator = new ViewPropertyAnimator(this);
17478 }
17479 return mAnimator;
17480 }
17481
17482 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017483 * Interface definition for a callback to be invoked when a hardware key event is
17484 * dispatched to this view. The callback will be invoked before the key event is
17485 * given to the view. This is only useful for hardware keyboards; a software input
17486 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017487 */
17488 public interface OnKeyListener {
17489 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017490 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017491 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017492 * <p>Key presses in software keyboards will generally NOT trigger this method,
17493 * although some may elect to do so in some situations. Do not assume a
17494 * software input method has to be key-based; even if it is, it may use key presses
17495 * in a different way than you expect, so there is no way to reliably catch soft
17496 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017497 *
17498 * @param v The view the key has been dispatched to.
17499 * @param keyCode The code for the physical key that was pressed
17500 * @param event The KeyEvent object containing full information about
17501 * the event.
17502 * @return True if the listener has consumed the event, false otherwise.
17503 */
17504 boolean onKey(View v, int keyCode, KeyEvent event);
17505 }
17506
17507 /**
17508 * Interface definition for a callback to be invoked when a touch event is
17509 * dispatched to this view. The callback will be invoked before the touch
17510 * event is given to the view.
17511 */
17512 public interface OnTouchListener {
17513 /**
17514 * Called when a touch event is dispatched to a view. This allows listeners to
17515 * get a chance to respond before the target view.
17516 *
17517 * @param v The view the touch event has been dispatched to.
17518 * @param event The MotionEvent object containing full information about
17519 * the event.
17520 * @return True if the listener has consumed the event, false otherwise.
17521 */
17522 boolean onTouch(View v, MotionEvent event);
17523 }
17524
17525 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017526 * Interface definition for a callback to be invoked when a hover event is
17527 * dispatched to this view. The callback will be invoked before the hover
17528 * event is given to the view.
17529 */
17530 public interface OnHoverListener {
17531 /**
17532 * Called when a hover event is dispatched to a view. This allows listeners to
17533 * get a chance to respond before the target view.
17534 *
17535 * @param v The view the hover event has been dispatched to.
17536 * @param event The MotionEvent object containing full information about
17537 * the event.
17538 * @return True if the listener has consumed the event, false otherwise.
17539 */
17540 boolean onHover(View v, MotionEvent event);
17541 }
17542
17543 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017544 * Interface definition for a callback to be invoked when a generic motion event is
17545 * dispatched to this view. The callback will be invoked before the generic motion
17546 * event is given to the view.
17547 */
17548 public interface OnGenericMotionListener {
17549 /**
17550 * Called when a generic motion event is dispatched to a view. This allows listeners to
17551 * get a chance to respond before the target view.
17552 *
17553 * @param v The view the generic motion event has been dispatched to.
17554 * @param event The MotionEvent object containing full information about
17555 * the event.
17556 * @return True if the listener has consumed the event, false otherwise.
17557 */
17558 boolean onGenericMotion(View v, MotionEvent event);
17559 }
17560
17561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017562 * Interface definition for a callback to be invoked when a view has been clicked and held.
17563 */
17564 public interface OnLongClickListener {
17565 /**
17566 * Called when a view has been clicked and held.
17567 *
17568 * @param v The view that was clicked and held.
17569 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017570 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017571 */
17572 boolean onLongClick(View v);
17573 }
17574
17575 /**
Chris Tate32affef2010-10-18 15:29:21 -070017576 * Interface definition for a callback to be invoked when a drag is being dispatched
17577 * to this view. The callback will be invoked before the hosting view's own
17578 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17579 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017580 *
17581 * <div class="special reference">
17582 * <h3>Developer Guides</h3>
17583 * <p>For a guide to implementing drag and drop features, read the
17584 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17585 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017586 */
17587 public interface OnDragListener {
17588 /**
17589 * Called when a drag event is dispatched to a view. This allows listeners
17590 * to get a chance to override base View behavior.
17591 *
Joe Malin32736f02011-01-19 16:14:20 -080017592 * @param v The View that received the drag event.
17593 * @param event The {@link android.view.DragEvent} object for the drag event.
17594 * @return {@code true} if the drag event was handled successfully, or {@code false}
17595 * if the drag event was not handled. Note that {@code false} will trigger the View
17596 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017597 */
17598 boolean onDrag(View v, DragEvent event);
17599 }
17600
17601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017602 * Interface definition for a callback to be invoked when the focus state of
17603 * a view changed.
17604 */
17605 public interface OnFocusChangeListener {
17606 /**
17607 * Called when the focus state of a view has changed.
17608 *
17609 * @param v The view whose state has changed.
17610 * @param hasFocus The new focus state of v.
17611 */
17612 void onFocusChange(View v, boolean hasFocus);
17613 }
17614
17615 /**
17616 * Interface definition for a callback to be invoked when a view is clicked.
17617 */
17618 public interface OnClickListener {
17619 /**
17620 * Called when a view has been clicked.
17621 *
17622 * @param v The view that was clicked.
17623 */
17624 void onClick(View v);
17625 }
17626
17627 /**
17628 * Interface definition for a callback to be invoked when the context menu
17629 * for this view is being built.
17630 */
17631 public interface OnCreateContextMenuListener {
17632 /**
17633 * Called when the context menu for this view is being built. It is not
17634 * safe to hold onto the menu after this method returns.
17635 *
17636 * @param menu The context menu that is being built
17637 * @param v The view for which the context menu is being built
17638 * @param menuInfo Extra information about the item for which the
17639 * context menu should be shown. This information will vary
17640 * depending on the class of v.
17641 */
17642 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17643 }
17644
Joe Onorato664644d2011-01-23 17:53:23 -080017645 /**
17646 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017647 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017648 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017649 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017650 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017651 */
17652 public interface OnSystemUiVisibilityChangeListener {
17653 /**
17654 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017655 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017656 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017657 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17658 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17659 * This tells you the <strong>global</strong> state of these UI visibility
17660 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017661 */
17662 public void onSystemUiVisibilityChange(int visibility);
17663 }
17664
Adam Powell4afd62b2011-02-18 15:02:18 -080017665 /**
17666 * Interface definition for a callback to be invoked when this view is attached
17667 * or detached from its window.
17668 */
17669 public interface OnAttachStateChangeListener {
17670 /**
17671 * Called when the view is attached to a window.
17672 * @param v The view that was attached
17673 */
17674 public void onViewAttachedToWindow(View v);
17675 /**
17676 * Called when the view is detached from a window.
17677 * @param v The view that was detached
17678 */
17679 public void onViewDetachedFromWindow(View v);
17680 }
17681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017682 private final class UnsetPressedState implements Runnable {
17683 public void run() {
17684 setPressed(false);
17685 }
17686 }
17687
17688 /**
17689 * Base class for derived classes that want to save and restore their own
17690 * state in {@link android.view.View#onSaveInstanceState()}.
17691 */
17692 public static class BaseSavedState extends AbsSavedState {
17693 /**
17694 * Constructor used when reading from a parcel. Reads the state of the superclass.
17695 *
17696 * @param source
17697 */
17698 public BaseSavedState(Parcel source) {
17699 super(source);
17700 }
17701
17702 /**
17703 * Constructor called by derived classes when creating their SavedState objects
17704 *
17705 * @param superState The state of the superclass of this view
17706 */
17707 public BaseSavedState(Parcelable superState) {
17708 super(superState);
17709 }
17710
17711 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17712 new Parcelable.Creator<BaseSavedState>() {
17713 public BaseSavedState createFromParcel(Parcel in) {
17714 return new BaseSavedState(in);
17715 }
17716
17717 public BaseSavedState[] newArray(int size) {
17718 return new BaseSavedState[size];
17719 }
17720 };
17721 }
17722
17723 /**
17724 * A set of information given to a view when it is attached to its parent
17725 * window.
17726 */
17727 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017728 interface Callbacks {
17729 void playSoundEffect(int effectId);
17730 boolean performHapticFeedback(int effectId, boolean always);
17731 }
17732
17733 /**
17734 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17735 * to a Handler. This class contains the target (View) to invalidate and
17736 * the coordinates of the dirty rectangle.
17737 *
17738 * For performance purposes, this class also implements a pool of up to
17739 * POOL_LIMIT objects that get reused. This reduces memory allocations
17740 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017741 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017742 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017743 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017744
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017745 private static final SynchronizedPool<InvalidateInfo> sPool =
17746 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017747
17748 View target;
17749
17750 int left;
17751 int top;
17752 int right;
17753 int bottom;
17754
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017755 public static InvalidateInfo obtain() {
17756 InvalidateInfo instance = sPool.acquire();
17757 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070017758 }
17759
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017760 public void recycle() {
17761 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017762 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017763 }
17764 }
17765
17766 final IWindowSession mSession;
17767
17768 final IWindow mWindow;
17769
17770 final IBinder mWindowToken;
17771
Jeff Brown98365d72012-08-19 20:30:52 -070017772 final Display mDisplay;
17773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017774 final Callbacks mRootCallbacks;
17775
Romain Guy59a12ca2011-06-09 17:48:21 -070017776 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017778 /**
17779 * The top view of the hierarchy.
17780 */
17781 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017783 IBinder mPanelParentWindowToken;
17784 Surface mSurface;
17785
Romain Guyb051e892010-09-28 19:09:36 -070017786 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017787 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017788 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017789
Romain Guy7e4e5612012-03-05 14:37:29 -080017790 boolean mScreenOn;
17791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017792 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017793 * Scale factor used by the compatibility mode
17794 */
17795 float mApplicationScale;
17796
17797 /**
17798 * Indicates whether the application is in compatibility mode
17799 */
17800 boolean mScalingRequired;
17801
17802 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017803 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017804 */
17805 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017806
Dianne Hackborn63042d62011-01-26 18:56:29 -080017807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017808 * Left position of this view's window
17809 */
17810 int mWindowLeft;
17811
17812 /**
17813 * Top position of this view's window
17814 */
17815 int mWindowTop;
17816
17817 /**
Adam Powell26153a32010-11-08 15:22:27 -080017818 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017819 */
Adam Powell26153a32010-11-08 15:22:27 -080017820 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017821
17822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017823 * For windows that are full-screen but using insets to layout inside
17824 * of the screen decorations, these are the current insets for the
17825 * content of the window.
17826 */
17827 final Rect mContentInsets = new Rect();
17828
17829 /**
17830 * For windows that are full-screen but using insets to layout inside
17831 * of the screen decorations, these are the current insets for the
17832 * actual visible parts of the window.
17833 */
17834 final Rect mVisibleInsets = new Rect();
17835
17836 /**
17837 * The internal insets given by this window. This value is
17838 * supplied by the client (through
17839 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17840 * be given to the window manager when changed to be used in laying
17841 * out windows behind it.
17842 */
17843 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17844 = new ViewTreeObserver.InternalInsetsInfo();
17845
17846 /**
17847 * All views in the window's hierarchy that serve as scroll containers,
17848 * used to determine if the window can be resized or must be panned
17849 * to adjust for a soft input area.
17850 */
17851 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17852
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017853 final KeyEvent.DispatcherState mKeyDispatchState
17854 = new KeyEvent.DispatcherState();
17855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017856 /**
17857 * Indicates whether the view's window currently has the focus.
17858 */
17859 boolean mHasWindowFocus;
17860
17861 /**
17862 * The current visibility of the window.
17863 */
17864 int mWindowVisibility;
17865
17866 /**
17867 * Indicates the time at which drawing started to occur.
17868 */
17869 long mDrawingTime;
17870
17871 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017872 * Indicates whether or not ignoring the DIRTY_MASK flags.
17873 */
17874 boolean mIgnoreDirtyState;
17875
17876 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017877 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17878 * to avoid clearing that flag prematurely.
17879 */
17880 boolean mSetIgnoreDirtyState = false;
17881
17882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017883 * Indicates whether the view's window is currently in touch mode.
17884 */
17885 boolean mInTouchMode;
17886
17887 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017888 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017889 * the next time it performs a traversal
17890 */
17891 boolean mRecomputeGlobalAttributes;
17892
17893 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017894 * Always report new attributes at next traversal.
17895 */
17896 boolean mForceReportNewAttributes;
17897
17898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017899 * Set during a traveral if any views want to keep the screen on.
17900 */
17901 boolean mKeepScreenOn;
17902
17903 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017904 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17905 */
17906 int mSystemUiVisibility;
17907
17908 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017909 * Hack to force certain system UI visibility flags to be cleared.
17910 */
17911 int mDisabledSystemUiVisibility;
17912
17913 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017914 * Last global system UI visibility reported by the window manager.
17915 */
17916 int mGlobalSystemUiVisibility;
17917
17918 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017919 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17920 * attached.
17921 */
17922 boolean mHasSystemUiListeners;
17923
17924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017925 * Set if the visibility of any views has changed.
17926 */
17927 boolean mViewVisibilityChanged;
17928
17929 /**
17930 * Set to true if a view has been scrolled.
17931 */
17932 boolean mViewScrollChanged;
17933
17934 /**
17935 * Global to the view hierarchy used as a temporary for dealing with
17936 * x/y points in the transparent region computations.
17937 */
17938 final int[] mTransparentLocation = new int[2];
17939
17940 /**
17941 * Global to the view hierarchy used as a temporary for dealing with
17942 * x/y points in the ViewGroup.invalidateChild implementation.
17943 */
17944 final int[] mInvalidateChildLocation = new int[2];
17945
Chet Haasec3aa3612010-06-17 08:50:37 -070017946
17947 /**
17948 * Global to the view hierarchy used as a temporary for dealing with
17949 * x/y location when view is transformed.
17950 */
17951 final float[] mTmpTransformLocation = new float[2];
17952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017953 /**
17954 * The view tree observer used to dispatch global events like
17955 * layout, pre-draw, touch mode change, etc.
17956 */
17957 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17958
17959 /**
17960 * A Canvas used by the view hierarchy to perform bitmap caching.
17961 */
17962 Canvas mCanvas;
17963
17964 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017965 * The view root impl.
17966 */
17967 final ViewRootImpl mViewRootImpl;
17968
17969 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017970 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017971 * handler can be used to pump events in the UI events queue.
17972 */
17973 final Handler mHandler;
17974
17975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017976 * Temporary for use in computing invalidate rectangles while
17977 * calling up the hierarchy.
17978 */
17979 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017980
17981 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017982 * Temporary for use in computing hit areas with transformed views
17983 */
17984 final RectF mTmpTransformRect = new RectF();
17985
17986 /**
Chet Haase599913d2012-07-23 16:22:05 -070017987 * Temporary for use in transforming invalidation rect
17988 */
17989 final Matrix mTmpMatrix = new Matrix();
17990
17991 /**
17992 * Temporary for use in transforming invalidation rect
17993 */
17994 final Transformation mTmpTransformation = new Transformation();
17995
17996 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017997 * Temporary list for use in collecting focusable descendents of a view.
17998 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017999 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070018000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018001 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070018002 * The id of the window for accessibility purposes.
18003 */
18004 int mAccessibilityWindowId = View.NO_ID;
18005
18006 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070018007 * Whether to ingore not exposed for accessibility Views when
18008 * reporting the view tree to accessibility services.
18009 */
18010 boolean mIncludeNotImportantViews;
18011
18012 /**
18013 * The drawable for highlighting accessibility focus.
18014 */
18015 Drawable mAccessibilityFocusDrawable;
18016
18017 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070018018 * Show where the margins, bounds and layout bounds are for each view.
18019 */
Dianne Hackborna53de062012-05-08 18:53:51 -070018020 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070018021
18022 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070018023 * Point used to compute visible regions.
18024 */
18025 final Point mPoint = new Point();
18026
18027 /**
Chet Haasecc699b42012-12-13 09:06:55 -080018028 * Used to track which View originated a requestLayout() call, used when
18029 * requestLayout() is called during layout.
18030 */
18031 View mViewRequestingLayout;
18032
18033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018034 * Creates a new set of attachment information with the specified
18035 * events handler and thread.
18036 *
18037 * @param handler the events handler the view must use
18038 */
Jeff Brown98365d72012-08-19 20:30:52 -070018039 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080018040 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018041 mSession = session;
18042 mWindow = window;
18043 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070018044 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080018045 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018046 mHandler = handler;
18047 mRootCallbacks = effectPlayer;
18048 }
18049 }
18050
18051 /**
18052 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18053 * is supported. This avoids keeping too many unused fields in most
18054 * instances of View.</p>
18055 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018056 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018057
Mike Cleronf116bf82009-09-27 19:14:12 -070018058 /**
18059 * Scrollbars are not visible
18060 */
18061 public static final int OFF = 0;
18062
18063 /**
18064 * Scrollbars are visible
18065 */
18066 public static final int ON = 1;
18067
18068 /**
18069 * Scrollbars are fading away
18070 */
18071 public static final int FADING = 2;
18072
18073 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018075 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018076 public int scrollBarDefaultDelayBeforeFade;
18077 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018078
18079 public int scrollBarSize;
18080 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018081 public float[] interpolatorValues;
18082 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018083
18084 public final Paint paint;
18085 public final Matrix matrix;
18086 public Shader shader;
18087
Mike Cleronf116bf82009-09-27 19:14:12 -070018088 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18089
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018090 private static final float[] OPAQUE = { 255 };
18091 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018092
Mike Cleronf116bf82009-09-27 19:14:12 -070018093 /**
18094 * When fading should start. This time moves into the future every time
18095 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18096 */
18097 public long fadeStartTime;
18098
18099
18100 /**
18101 * The current state of the scrollbars: ON, OFF, or FADING
18102 */
18103 public int state = OFF;
18104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018105 private int mLastColor;
18106
Mike Cleronf116bf82009-09-27 19:14:12 -070018107 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018108 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18109 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018110 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18111 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018112
18113 paint = new Paint();
18114 matrix = new Matrix();
18115 // use use a height of 1, and then wack the matrix each time we
18116 // actually use it.
18117 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018118 paint.setShader(shader);
18119 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018120
Mike Cleronf116bf82009-09-27 19:14:12 -070018121 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018122 }
Romain Guy8506ab42009-06-11 17:35:47 -070018123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018124 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018125 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018126 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018127
Romain Guyd679b572012-08-29 21:49:00 -070018128 if (color != 0) {
18129 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18130 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18131 paint.setShader(shader);
18132 // Restore the default transfer mode (src_over)
18133 paint.setXfermode(null);
18134 } else {
18135 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18136 paint.setShader(shader);
18137 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018139 }
18140 }
Joe Malin32736f02011-01-19 16:14:20 -080018141
Mike Cleronf116bf82009-09-27 19:14:12 -070018142 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018143 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018144 if (now >= fadeStartTime) {
18145
18146 // the animation fades the scrollbars out by changing
18147 // the opacity (alpha) from fully opaque to fully
18148 // transparent
18149 int nextFrame = (int) now;
18150 int framesCount = 0;
18151
18152 Interpolator interpolator = scrollBarInterpolator;
18153
18154 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018155 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018156
18157 // End transparent
18158 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018159 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018160
18161 state = FADING;
18162
18163 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018164 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018165 }
18166 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018167 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018168
Svetoslav Ganova0156172011-06-26 17:55:44 -070018169 /**
18170 * Resuable callback for sending
18171 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18172 */
18173 private class SendViewScrolledAccessibilityEvent implements Runnable {
18174 public volatile boolean mIsPending;
18175
18176 public void run() {
18177 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18178 mIsPending = false;
18179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018180 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018181
18182 /**
18183 * <p>
18184 * This class represents a delegate that can be registered in a {@link View}
18185 * to enhance accessibility support via composition rather via inheritance.
18186 * It is specifically targeted to widget developers that extend basic View
18187 * classes i.e. classes in package android.view, that would like their
18188 * applications to be backwards compatible.
18189 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018190 * <div class="special reference">
18191 * <h3>Developer Guides</h3>
18192 * <p>For more information about making applications accessible, read the
18193 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18194 * developer guide.</p>
18195 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018196 * <p>
18197 * A scenario in which a developer would like to use an accessibility delegate
18198 * is overriding a method introduced in a later API version then the minimal API
18199 * version supported by the application. For example, the method
18200 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18201 * in API version 4 when the accessibility APIs were first introduced. If a
18202 * developer would like his application to run on API version 4 devices (assuming
18203 * all other APIs used by the application are version 4 or lower) and take advantage
18204 * of this method, instead of overriding the method which would break the application's
18205 * backwards compatibility, he can override the corresponding method in this
18206 * delegate and register the delegate in the target View if the API version of
18207 * the system is high enough i.e. the API version is same or higher to the API
18208 * version that introduced
18209 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18210 * </p>
18211 * <p>
18212 * Here is an example implementation:
18213 * </p>
18214 * <code><pre><p>
18215 * if (Build.VERSION.SDK_INT >= 14) {
18216 * // If the API version is equal of higher than the version in
18217 * // which onInitializeAccessibilityNodeInfo was introduced we
18218 * // register a delegate with a customized implementation.
18219 * View view = findViewById(R.id.view_id);
18220 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18221 * public void onInitializeAccessibilityNodeInfo(View host,
18222 * AccessibilityNodeInfo info) {
18223 * // Let the default implementation populate the info.
18224 * super.onInitializeAccessibilityNodeInfo(host, info);
18225 * // Set some other information.
18226 * info.setEnabled(host.isEnabled());
18227 * }
18228 * });
18229 * }
18230 * </code></pre></p>
18231 * <p>
18232 * This delegate contains methods that correspond to the accessibility methods
18233 * in View. If a delegate has been specified the implementation in View hands
18234 * off handling to the corresponding method in this delegate. The default
18235 * implementation the delegate methods behaves exactly as the corresponding
18236 * method in View for the case of no accessibility delegate been set. Hence,
18237 * to customize the behavior of a View method, clients can override only the
18238 * corresponding delegate method without altering the behavior of the rest
18239 * accessibility related methods of the host view.
18240 * </p>
18241 */
18242 public static class AccessibilityDelegate {
18243
18244 /**
18245 * Sends an accessibility event of the given type. If accessibility is not
18246 * enabled this method has no effect.
18247 * <p>
18248 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18249 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18250 * been set.
18251 * </p>
18252 *
18253 * @param host The View hosting the delegate.
18254 * @param eventType The type of the event to send.
18255 *
18256 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18257 */
18258 public void sendAccessibilityEvent(View host, int eventType) {
18259 host.sendAccessibilityEventInternal(eventType);
18260 }
18261
18262 /**
alanv8eeefef2012-05-07 16:57:53 -070018263 * Performs the specified accessibility action on the view. For
18264 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18265 * <p>
18266 * The default implementation behaves as
18267 * {@link View#performAccessibilityAction(int, Bundle)
18268 * View#performAccessibilityAction(int, Bundle)} for the case of
18269 * no accessibility delegate been set.
18270 * </p>
18271 *
18272 * @param action The action to perform.
18273 * @return Whether the action was performed.
18274 *
18275 * @see View#performAccessibilityAction(int, Bundle)
18276 * View#performAccessibilityAction(int, Bundle)
18277 */
18278 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18279 return host.performAccessibilityActionInternal(action, args);
18280 }
18281
18282 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018283 * Sends an accessibility event. This method behaves exactly as
18284 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18285 * empty {@link AccessibilityEvent} and does not perform a check whether
18286 * accessibility is enabled.
18287 * <p>
18288 * The default implementation behaves as
18289 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18290 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18291 * the case of no accessibility delegate been set.
18292 * </p>
18293 *
18294 * @param host The View hosting the delegate.
18295 * @param event The event to send.
18296 *
18297 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18298 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18299 */
18300 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18301 host.sendAccessibilityEventUncheckedInternal(event);
18302 }
18303
18304 /**
18305 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18306 * to its children for adding their text content to the event.
18307 * <p>
18308 * The default implementation behaves as
18309 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18310 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18311 * the case of no accessibility delegate been set.
18312 * </p>
18313 *
18314 * @param host The View hosting the delegate.
18315 * @param event The event.
18316 * @return True if the event population was completed.
18317 *
18318 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18319 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18320 */
18321 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18322 return host.dispatchPopulateAccessibilityEventInternal(event);
18323 }
18324
18325 /**
18326 * Gives a chance to the host View to populate the accessibility event with its
18327 * text content.
18328 * <p>
18329 * The default implementation behaves as
18330 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18331 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18332 * the case of no accessibility delegate been set.
18333 * </p>
18334 *
18335 * @param host The View hosting the delegate.
18336 * @param event The accessibility event which to populate.
18337 *
18338 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18339 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18340 */
18341 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18342 host.onPopulateAccessibilityEventInternal(event);
18343 }
18344
18345 /**
18346 * Initializes an {@link AccessibilityEvent} with information about the
18347 * the host View which is the event source.
18348 * <p>
18349 * The default implementation behaves as
18350 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18351 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18352 * the case of no accessibility delegate been set.
18353 * </p>
18354 *
18355 * @param host The View hosting the delegate.
18356 * @param event The event to initialize.
18357 *
18358 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18359 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18360 */
18361 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18362 host.onInitializeAccessibilityEventInternal(event);
18363 }
18364
18365 /**
18366 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18367 * <p>
18368 * The default implementation behaves as
18369 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18370 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18371 * the case of no accessibility delegate been set.
18372 * </p>
18373 *
18374 * @param host The View hosting the delegate.
18375 * @param info The instance to initialize.
18376 *
18377 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18378 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18379 */
18380 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18381 host.onInitializeAccessibilityNodeInfoInternal(info);
18382 }
18383
18384 /**
18385 * Called when a child of the host View has requested sending an
18386 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18387 * to augment the event.
18388 * <p>
18389 * The default implementation behaves as
18390 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18391 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18392 * the case of no accessibility delegate been set.
18393 * </p>
18394 *
18395 * @param host The View hosting the delegate.
18396 * @param child The child which requests sending the event.
18397 * @param event The event to be sent.
18398 * @return True if the event should be sent
18399 *
18400 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18401 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18402 */
18403 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18404 AccessibilityEvent event) {
18405 return host.onRequestSendAccessibilityEventInternal(child, event);
18406 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018407
18408 /**
18409 * Gets the provider for managing a virtual view hierarchy rooted at this View
18410 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18411 * that explore the window content.
18412 * <p>
18413 * The default implementation behaves as
18414 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18415 * the case of no accessibility delegate been set.
18416 * </p>
18417 *
18418 * @return The provider.
18419 *
18420 * @see AccessibilityNodeProvider
18421 */
18422 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18423 return null;
18424 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018425 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018426
18427 private class MatchIdPredicate implements Predicate<View> {
18428 public int mId;
18429
18430 @Override
18431 public boolean apply(View view) {
18432 return (view.mID == mId);
18433 }
18434 }
18435
18436 private class MatchLabelForPredicate implements Predicate<View> {
18437 private int mLabeledId;
18438
18439 @Override
18440 public boolean apply(View view) {
18441 return (view.mLabelForId == mLabeledId);
18442 }
18443 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018444
18445 /**
18446 * Dump all private flags in readable format, useful for documentation and
18447 * sanity checking.
18448 */
18449 private static void dumpFlags() {
18450 final HashMap<String, String> found = Maps.newHashMap();
18451 try {
18452 for (Field field : View.class.getDeclaredFields()) {
18453 final int modifiers = field.getModifiers();
18454 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18455 if (field.getType().equals(int.class)) {
18456 final int value = field.getInt(null);
18457 dumpFlag(found, field.getName(), value);
18458 } else if (field.getType().equals(int[].class)) {
18459 final int[] values = (int[]) field.get(null);
18460 for (int i = 0; i < values.length; i++) {
18461 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18462 }
18463 }
18464 }
18465 }
18466 } catch (IllegalAccessException e) {
18467 throw new RuntimeException(e);
18468 }
18469
18470 final ArrayList<String> keys = Lists.newArrayList();
18471 keys.addAll(found.keySet());
18472 Collections.sort(keys);
18473 for (String key : keys) {
18474 Log.d(VIEW_LOG_TAG, found.get(key));
18475 }
18476 }
18477
18478 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18479 // Sort flags by prefix, then by bits, always keeping unique keys
18480 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18481 final int prefix = name.indexOf('_');
18482 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18483 final String output = bits + " " + name;
18484 found.put(key, output);
18485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018486}