blob: 11c80c26ebf19276a5ad6b4610fdb773a3a95232 [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
Svetoslav Ganov80943d82013-01-02 10:25:37 -080019import android.app.ActivityThread;
Christopher Tatea53146c2010-09-07 11:57:52 -070020import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080022import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070026import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070028import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070029import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.LinearGradient;
31import android.graphics.Matrix;
32import android.graphics.Paint;
33import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070034import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
37import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070038import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.Region;
40import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.drawable.ColorDrawable;
42import android.graphics.drawable.Drawable;
Jeff Brownbd6e1502012-08-28 03:27:37 -070043import android.hardware.display.DisplayManagerGlobal;
Adam Powell7da4b732012-12-07 15:28:33 -080044import android.os.Build;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Handler;
47import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070052import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070053import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070055import android.util.FloatProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080057import android.util.Pools.SynchronizedPool;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
Jeff Sharkey36901b62012-09-19 19:06:22 -070085import com.google.android.collect.Lists;
86import com.google.android.collect.Maps;
Doug Feltcb3791202011-07-07 11:57:48 -070087
Christopher Tatea0374192010-10-05 13:06:41 -070088import java.lang.ref.WeakReference;
Jeff Sharkey36901b62012-09-19 19:06:22 -070089import java.lang.reflect.Field;
svetoslavganov75986cf2009-05-14 22:28:01 -070090import java.lang.reflect.InvocationTargetException;
91import java.lang.reflect.Method;
Jeff Sharkey36901b62012-09-19 19:06:22 -070092import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.util.ArrayList;
94import java.util.Arrays;
Jeff Sharkey36901b62012-09-19 19:06:22 -070095import java.util.Collections;
96import java.util.HashMap;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070097import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080098import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070099import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101/**
102 * <p>
103 * This class represents the basic building block for user interface components. A View
104 * occupies a rectangular area on the screen and is responsible for drawing and
105 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700106 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
108 * are invisible containers that hold other Views (or other ViewGroups) and define
109 * their layout properties.
110 * </p>
111 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700112 * <div class="special reference">
113 * <h3>Developer Guides</h3>
114 * <p>For information about using this class to develop your application's user interface,
115 * 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 -0800116 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * <a name="Using"></a>
119 * <h3>Using Views</h3>
120 * <p>
121 * All of the views in a window are arranged in a single tree. You can add views
122 * either from code or by specifying a tree of views in one or more XML layout
123 * files. There are many specialized subclasses of views that act as controls or
124 * are capable of displaying text, images, or other content.
125 * </p>
126 * <p>
127 * Once you have created a tree of views, there are typically a few types of
128 * common operations you may wish to perform:
129 * <ul>
130 * <li><strong>Set properties:</strong> for example setting the text of a
131 * {@link android.widget.TextView}. The available properties and the methods
132 * that set them will vary among the different subclasses of views. Note that
133 * properties that are known at build time can be set in the XML layout
134 * files.</li>
135 * <li><strong>Set focus:</strong> The framework will handled moving focus in
136 * response to user input. To force focus to a specific view, call
137 * {@link #requestFocus}.</li>
138 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
139 * that will be notified when something interesting happens to the view. For
140 * example, all views will let you set a listener to be notified when the view
141 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700142 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700143 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700144 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700146 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 * </ul>
148 * </p>
149 * <p><em>
150 * Note: The Android framework is responsible for measuring, laying out and
151 * drawing views. You should not call methods that perform these actions on
152 * views yourself unless you are actually implementing a
153 * {@link android.view.ViewGroup}.
154 * </em></p>
155 *
156 * <a name="Lifecycle"></a>
157 * <h3>Implementing a Custom View</h3>
158 *
159 * <p>
160 * To implement a custom view, you will usually begin by providing overrides for
161 * some of the standard methods that the framework calls on all views. You do
162 * not need to override all of these methods. In fact, you can start by just
163 * overriding {@link #onDraw(android.graphics.Canvas)}.
164 * <table border="2" width="85%" align="center" cellpadding="5">
165 * <thead>
166 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
167 * </thead>
168 *
169 * <tbody>
170 * <tr>
171 * <td rowspan="2">Creation</td>
172 * <td>Constructors</td>
173 * <td>There is a form of the constructor that are called when the view
174 * is created from code and a form that is called when the view is
175 * inflated from a layout file. The second form should parse and apply
176 * any attributes defined in the layout file.
177 * </td>
178 * </tr>
179 * <tr>
180 * <td><code>{@link #onFinishInflate()}</code></td>
181 * <td>Called after a view and all of its children has been inflated
182 * from XML.</td>
183 * </tr>
184 *
185 * <tr>
186 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called to determine the size requirements for this view and all
189 * of its children.
190 * </td>
191 * </tr>
192 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when this view should assign a size and position to all
195 * of its children.
196 * </td>
197 * </tr>
198 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the size of this view has changed.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when the view should render its content.
208 * </td>
209 * </tr>
210 *
211 * <tr>
212 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900214 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900219 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 * </td>
221 * </tr>
222 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when a trackball motion event occurs.
225 * </td>
226 * </tr>
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when a touch screen motion event occurs.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700235 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <td>Called when the view gains or loses focus.
237 * </td>
238 * </tr>
239 *
240 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700241 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * <td>Called when the window containing the view gains or loses focus.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td rowspan="3">Attaching</td>
248 * <td><code>{@link #onAttachedToWindow()}</code></td>
249 * <td>Called when the view is attached to a window.
250 * </td>
251 * </tr>
252 *
253 * <tr>
254 * <td><code>{@link #onDetachedFromWindow}</code></td>
255 * <td>Called when the view is detached from its window.
256 * </td>
257 * </tr>
258 *
259 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700260 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 * <td>Called when the visibility of the window containing the view
262 * has changed.
263 * </td>
264 * </tr>
265 * </tbody>
266 *
267 * </table>
268 * </p>
269 *
270 * <a name="IDs"></a>
271 * <h3>IDs</h3>
272 * Views may have an integer id associated with them. These ids are typically
273 * assigned in the layout XML files, and are used to find specific views within
274 * the view tree. A common pattern is to:
275 * <ul>
276 * <li>Define a Button in the layout file and assign it a unique ID.
277 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700278 * &lt;Button
279 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 * android:layout_width="wrap_content"
281 * android:layout_height="wrap_content"
282 * android:text="@string/my_button_text"/&gt;
283 * </pre></li>
284 * <li>From the onCreate method of an Activity, find the Button
285 * <pre class="prettyprint">
286 * Button myButton = (Button) findViewById(R.id.my_button);
287 * </pre></li>
288 * </ul>
289 * <p>
290 * View IDs need not be unique throughout the tree, but it is good practice to
291 * ensure that they are at least unique within the part of the tree you are
292 * searching.
293 * </p>
294 *
295 * <a name="Position"></a>
296 * <h3>Position</h3>
297 * <p>
298 * The geometry of a view is that of a rectangle. A view has a location,
299 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
300 * two dimensions, expressed as a width and a height. The unit for location
301 * and dimensions is the pixel.
302 * </p>
303 *
304 * <p>
305 * It is possible to retrieve the location of a view by invoking the methods
306 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
307 * coordinate of the rectangle representing the view. The latter returns the
308 * top, or Y, coordinate of the rectangle representing the view. These methods
309 * both return the location of the view relative to its parent. For instance,
310 * when getLeft() returns 20, that means the view is located 20 pixels to the
311 * right of the left edge of its direct parent.
312 * </p>
313 *
314 * <p>
315 * In addition, several convenience methods are offered to avoid unnecessary
316 * computations, namely {@link #getRight()} and {@link #getBottom()}.
317 * These methods return the coordinates of the right and bottom edges of the
318 * rectangle representing the view. For instance, calling {@link #getRight()}
319 * is similar to the following computation: <code>getLeft() + getWidth()</code>
320 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
321 * </p>
322 *
323 * <a name="SizePaddingMargins"></a>
324 * <h3>Size, padding and margins</h3>
325 * <p>
326 * The size of a view is expressed with a width and a height. A view actually
327 * possess two pairs of width and height values.
328 * </p>
329 *
330 * <p>
331 * The first pair is known as <em>measured width</em> and
332 * <em>measured height</em>. These dimensions define how big a view wants to be
333 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
334 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
335 * and {@link #getMeasuredHeight()}.
336 * </p>
337 *
338 * <p>
339 * The second pair is simply known as <em>width</em> and <em>height</em>, or
340 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
341 * dimensions define the actual size of the view on screen, at drawing time and
342 * after layout. These values may, but do not have to, be different from the
343 * measured width and height. The width and height can be obtained by calling
344 * {@link #getWidth()} and {@link #getHeight()}.
345 * </p>
346 *
347 * <p>
348 * To measure its dimensions, a view takes into account its padding. The padding
349 * is expressed in pixels for the left, top, right and bottom parts of the view.
350 * Padding can be used to offset the content of the view by a specific amount of
351 * pixels. For instance, a left padding of 2 will push the view's content by
352 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700353 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
354 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
355 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
356 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * </p>
358 *
359 * <p>
360 * Even though a view can define a padding, it does not provide any support for
361 * margins. However, view groups provide such a support. Refer to
362 * {@link android.view.ViewGroup} and
363 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
364 * </p>
365 *
366 * <a name="Layout"></a>
367 * <h3>Layout</h3>
368 * <p>
369 * Layout is a two pass process: a measure pass and a layout pass. The measuring
370 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
371 * of the view tree. Each view pushes dimension specifications down the tree
372 * during the recursion. At the end of the measure pass, every view has stored
373 * its measurements. The second pass happens in
374 * {@link #layout(int,int,int,int)} and is also top-down. During
375 * this pass each parent is responsible for positioning all of its children
376 * using the sizes computed in the measure pass.
377 * </p>
378 *
379 * <p>
380 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
381 * {@link #getMeasuredHeight()} values must be set, along with those for all of
382 * that view's descendants. A view's measured width and measured height values
383 * must respect the constraints imposed by the view's parents. This guarantees
384 * that at the end of the measure pass, all parents accept all of their
385 * children's measurements. A parent view may call measure() more than once on
386 * its children. For example, the parent may measure each child once with
387 * unspecified dimensions to find out how big they want to be, then call
388 * measure() on them again with actual numbers if the sum of all the children's
389 * unconstrained sizes is too big or too small.
390 * </p>
391 *
392 * <p>
393 * The measure pass uses two classes to communicate dimensions. The
394 * {@link MeasureSpec} class is used by views to tell their parents how they
395 * want to be measured and positioned. The base LayoutParams class just
396 * describes how big the view wants to be for both width and height. For each
397 * dimension, it can specify one of:
398 * <ul>
399 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800400 * <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 -0800401 * (minus padding)
402 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
403 * enclose its content (plus padding).
404 * </ul>
405 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
406 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
407 * an X and Y value.
408 * </p>
409 *
410 * <p>
411 * MeasureSpecs are used to push requirements down the tree from parent to
412 * child. A MeasureSpec can be in one of three modes:
413 * <ul>
414 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
415 * of a child view. For example, a LinearLayout may call measure() on its child
416 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
417 * tall the child view wants to be given a width of 240 pixels.
418 * <li>EXACTLY: This is used by the parent to impose an exact size on the
419 * child. The child must use this size, and guarantee that all of its
420 * descendants will fit within this size.
421 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
422 * child. The child must gurantee that it and all of its descendants will fit
423 * within this size.
424 * </ul>
425 * </p>
426 *
427 * <p>
428 * To intiate a layout, call {@link #requestLayout}. This method is typically
429 * called by a view on itself when it believes that is can no longer fit within
430 * its current bounds.
431 * </p>
432 *
433 * <a name="Drawing"></a>
434 * <h3>Drawing</h3>
435 * <p>
436 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700437 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * this means that parents will draw before (i.e., behind) their children, with
439 * siblings drawn in the order they appear in the tree.
440 * If you set a background drawable for a View, then the View will draw it for you
441 * before calling back to its <code>onDraw()</code> method.
442 * </p>
443 *
444 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700445 * 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 -0800446 * </p>
447 *
448 * <p>
449 * To force a view to draw, call {@link #invalidate()}.
450 * </p>
451 *
452 * <a name="EventHandlingThreading"></a>
453 * <h3>Event Handling and Threading</h3>
454 * <p>
455 * The basic cycle of a view is as follows:
456 * <ol>
457 * <li>An event comes in and is dispatched to the appropriate view. The view
458 * handles the event and notifies any listeners.</li>
459 * <li>If in the course of processing the event, the view's bounds may need
460 * to be changed, the view will call {@link #requestLayout()}.</li>
461 * <li>Similarly, if in the course of processing the event the view's appearance
462 * may need to be changed, the view will call {@link #invalidate()}.</li>
463 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
464 * the framework will take care of measuring, laying out, and drawing the tree
465 * as appropriate.</li>
466 * </ol>
467 * </p>
468 *
469 * <p><em>Note: The entire view tree is single threaded. You must always be on
470 * the UI thread when calling any method on any view.</em>
471 * If you are doing work on other threads and want to update the state of a view
472 * from that thread, you should use a {@link Handler}.
473 * </p>
474 *
475 * <a name="FocusHandling"></a>
476 * <h3>Focus Handling</h3>
477 * <p>
478 * The framework will handle routine focus movement in response to user input.
479 * This includes changing the focus as views are removed or hidden, or as new
480 * views become available. Views indicate their willingness to take focus
481 * through the {@link #isFocusable} method. To change whether a view can take
482 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
483 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
484 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
485 * </p>
486 * <p>
487 * Focus movement is based on an algorithm which finds the nearest neighbor in a
488 * given direction. In rare cases, the default algorithm may not match the
489 * intended behavior of the developer. In these situations, you can provide
490 * explicit overrides by using these XML attributes in the layout file:
491 * <pre>
492 * nextFocusDown
493 * nextFocusLeft
494 * nextFocusRight
495 * nextFocusUp
496 * </pre>
497 * </p>
498 *
499 *
500 * <p>
501 * To get a particular view to take focus, call {@link #requestFocus()}.
502 * </p>
503 *
504 * <a name="TouchMode"></a>
505 * <h3>Touch Mode</h3>
506 * <p>
507 * When a user is navigating a user interface via directional keys such as a D-pad, it is
508 * necessary to give focus to actionable items such as buttons so the user can see
509 * what will take input. If the device has touch capabilities, however, and the user
510 * begins interacting with the interface by touching it, it is no longer necessary to
511 * always highlight, or give focus to, a particular view. This motivates a mode
512 * for interaction named 'touch mode'.
513 * </p>
514 * <p>
515 * For a touch capable device, once the user touches the screen, the device
516 * will enter touch mode. From this point onward, only views for which
517 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
518 * Other views that are touchable, like buttons, will not take focus when touched; they will
519 * only fire the on click listeners.
520 * </p>
521 * <p>
522 * Any time a user hits a directional key, such as a D-pad direction, the view device will
523 * exit touch mode, and find a view to take focus, so that the user may resume interacting
524 * with the user interface without touching the screen again.
525 * </p>
526 * <p>
527 * The touch mode state is maintained across {@link android.app.Activity}s. Call
528 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
529 * </p>
530 *
531 * <a name="Scrolling"></a>
532 * <h3>Scrolling</h3>
533 * <p>
534 * The framework provides basic support for views that wish to internally
535 * scroll their content. This includes keeping track of the X and Y scroll
536 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800537 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700538 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 * </p>
540 *
541 * <a name="Tags"></a>
542 * <h3>Tags</h3>
543 * <p>
544 * Unlike IDs, tags are not used to identify views. Tags are essentially an
545 * extra piece of information that can be associated with a view. They are most
546 * often used as a convenience to store data related to views in the views
547 * themselves rather than by putting them in a separate structure.
548 * </p>
549 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700550 * <a name="Properties"></a>
551 * <h3>Properties</h3>
552 * <p>
553 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
554 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
555 * available both in the {@link Property} form as well as in similarly-named setter/getter
556 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
557 * be used to set persistent state associated with these rendering-related properties on the view.
558 * The properties and methods can also be used in conjunction with
559 * {@link android.animation.Animator Animator}-based animations, described more in the
560 * <a href="#Animation">Animation</a> section.
561 * </p>
562 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * <a name="Animation"></a>
564 * <h3>Animation</h3>
565 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700566 * Starting with Android 3.0, the preferred way of animating views is to use the
567 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
568 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
569 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
570 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
571 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
572 * makes animating these View properties particularly easy and efficient.
573 * </p>
574 * <p>
575 * 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 -0800576 * You can attach an {@link Animation} object to a view using
577 * {@link #setAnimation(Animation)} or
578 * {@link #startAnimation(Animation)}. The animation can alter the scale,
579 * rotation, translation and alpha of a view over time. If the animation is
580 * attached to a view that has children, the animation will affect the entire
581 * subtree rooted by that node. When an animation is started, the framework will
582 * take care of redrawing the appropriate views until the animation completes.
583 * </p>
584 *
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * <a name="Security"></a>
586 * <h3>Security</h3>
587 * <p>
588 * Sometimes it is essential that an application be able to verify that an action
589 * is being performed with the full knowledge and consent of the user, such as
590 * granting a permission request, making a purchase or clicking on an advertisement.
591 * Unfortunately, a malicious application could try to spoof the user into
592 * performing these actions, unaware, by concealing the intended purpose of the view.
593 * As a remedy, the framework offers a touch filtering mechanism that can be used to
594 * improve the security of views that provide access to sensitive functionality.
595 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700596 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800597 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700598 * will discard touches that are received whenever the view's window is obscured by
599 * another visible window. As a result, the view will not receive touches whenever a
600 * toast, dialog or other window appears above the view's window.
601 * </p><p>
602 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700603 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
604 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700605 * </p>
606 *
Romain Guy171c5922011-01-06 10:04:23 -0800607 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_background
609 * @attr ref android.R.styleable#View_clickable
610 * @attr ref android.R.styleable#View_contentDescription
611 * @attr ref android.R.styleable#View_drawingCacheQuality
612 * @attr ref android.R.styleable#View_duplicateParentState
613 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700614 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700615 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700617 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_isScrollContainer
620 * @attr ref android.R.styleable#View_focusable
621 * @attr ref android.R.styleable#View_focusableInTouchMode
622 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
623 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800624 * @attr ref android.R.styleable#View_layerType
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800625 * @attr ref android.R.styleable#View_layoutDirection
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_longClickable
627 * @attr ref android.R.styleable#View_minHeight
628 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 * @attr ref android.R.styleable#View_nextFocusDown
630 * @attr ref android.R.styleable#View_nextFocusLeft
631 * @attr ref android.R.styleable#View_nextFocusRight
632 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_onClick
634 * @attr ref android.R.styleable#View_padding
635 * @attr ref android.R.styleable#View_paddingBottom
636 * @attr ref android.R.styleable#View_paddingLeft
637 * @attr ref android.R.styleable#View_paddingRight
638 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800639 * @attr ref android.R.styleable#View_paddingStart
640 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800642 * @attr ref android.R.styleable#View_rotation
643 * @attr ref android.R.styleable#View_rotationX
644 * @attr ref android.R.styleable#View_rotationY
645 * @attr ref android.R.styleable#View_scaleX
646 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollX
648 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_scrollbarSize
650 * @attr ref android.R.styleable#View_scrollbarStyle
651 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700652 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
653 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
655 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * @attr ref android.R.styleable#View_scrollbarThumbVertical
657 * @attr ref android.R.styleable#View_scrollbarTrackVertical
658 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
659 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_soundEffectsEnabled
661 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700662 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -0800663 * @attr ref android.R.styleable#View_textDirection
Chet Haase73066682010-11-29 15:55:32 -0800664 * @attr ref android.R.styleable#View_transformPivotX
665 * @attr ref android.R.styleable#View_transformPivotY
666 * @attr ref android.R.styleable#View_translationX
667 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700668 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 *
670 * @see android.view.ViewGroup
671 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700672public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700673 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 private static final boolean DBG = false;
675
676 /**
677 * The logging tag used by this class with android.util.Log.
678 */
679 protected static final String VIEW_LOG_TAG = "View";
680
681 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700682 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700683 *
684 * @hide
685 */
686 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
687
688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 * Used to mark a View that has no ID.
690 */
691 public static final int NO_ID = -1;
692
Adam Powell7da4b732012-12-07 15:28:33 -0800693 private static boolean sUseBrokenMakeMeasureSpec = false;
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 /**
696 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
697 * calling setFlags.
698 */
699 private static final int NOT_FOCUSABLE = 0x00000000;
700
701 /**
702 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
703 * setFlags.
704 */
705 private static final int FOCUSABLE = 0x00000001;
706
707 /**
708 * Mask for use with setFlags indicating bits used for focus.
709 */
710 private static final int FOCUSABLE_MASK = 0x00000001;
711
712 /**
713 * This view will adjust its padding to fit sytem windows (e.g. status bar)
714 */
715 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
716
717 /**
Scott Main812634c22011-07-27 13:22:35 -0700718 * This view is visible.
719 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
720 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 */
722 public static final int VISIBLE = 0x00000000;
723
724 /**
725 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700726 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
727 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 */
729 public static final int INVISIBLE = 0x00000004;
730
731 /**
732 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700733 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
734 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 */
736 public static final int GONE = 0x00000008;
737
738 /**
739 * Mask for use with setFlags indicating bits used for visibility.
740 * {@hide}
741 */
742 static final int VISIBILITY_MASK = 0x0000000C;
743
744 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
745
746 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700747 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 * Use with ENABLED_MASK when calling setFlags.
749 * {@hide}
750 */
751 static final int ENABLED = 0x00000000;
752
753 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700754 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 * Use with ENABLED_MASK when calling setFlags.
756 * {@hide}
757 */
758 static final int DISABLED = 0x00000020;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is enabled
763 * {@hide}
764 */
765 static final int ENABLED_MASK = 0x00000020;
766
767 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700768 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
769 * called and further optimizations will be performed. It is okay to have
770 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 * {@hide}
772 */
773 static final int WILL_NOT_DRAW = 0x00000080;
774
775 /**
776 * Mask for use with setFlags indicating bits used for indicating whether
777 * this view is will draw
778 * {@hide}
779 */
780 static final int DRAW_MASK = 0x00000080;
781
782 /**
783 * <p>This view doesn't show scrollbars.</p>
784 * {@hide}
785 */
786 static final int SCROLLBARS_NONE = 0x00000000;
787
788 /**
789 * <p>This view shows horizontal scrollbars.</p>
790 * {@hide}
791 */
792 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
793
794 /**
795 * <p>This view shows vertical scrollbars.</p>
796 * {@hide}
797 */
798 static final int SCROLLBARS_VERTICAL = 0x00000200;
799
800 /**
801 * <p>Mask for use with setFlags indicating bits used for indicating which
802 * scrollbars are enabled.</p>
803 * {@hide}
804 */
805 static final int SCROLLBARS_MASK = 0x00000300;
806
Jeff Brown85a31762010-09-01 17:01:00 -0700807 /**
808 * Indicates that the view should filter touches when its window is obscured.
809 * Refer to the class comments for more information about this security feature.
810 * {@hide}
811 */
812 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
813
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700814 /**
815 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
816 * that they are optional and should be skipped if the window has
817 * requested system UI flags that ignore those insets for layout.
818 */
819 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820
821 /**
822 * <p>This view doesn't show fading edges.</p>
823 * {@hide}
824 */
825 static final int FADING_EDGE_NONE = 0x00000000;
826
827 /**
828 * <p>This view shows horizontal fading edges.</p>
829 * {@hide}
830 */
831 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
832
833 /**
834 * <p>This view shows vertical fading edges.</p>
835 * {@hide}
836 */
837 static final int FADING_EDGE_VERTICAL = 0x00002000;
838
839 /**
840 * <p>Mask for use with setFlags indicating bits used for indicating which
841 * fading edges are enabled.</p>
842 * {@hide}
843 */
844 static final int FADING_EDGE_MASK = 0x00003000;
845
846 /**
847 * <p>Indicates this view can be clicked. When clickable, a View reacts
848 * to clicks by notifying the OnClickListener.<p>
849 * {@hide}
850 */
851 static final int CLICKABLE = 0x00004000;
852
853 /**
854 * <p>Indicates this view is caching its drawing into a bitmap.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_ENABLED = 0x00008000;
858
859 /**
860 * <p>Indicates that no icicle should be saved for this view.<p>
861 * {@hide}
862 */
863 static final int SAVE_DISABLED = 0x000010000;
864
865 /**
866 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
867 * property.</p>
868 * {@hide}
869 */
870 static final int SAVE_DISABLED_MASK = 0x000010000;
871
872 /**
873 * <p>Indicates that no drawing cache should ever be created for this view.<p>
874 * {@hide}
875 */
876 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
877
878 /**
879 * <p>Indicates this view can take / keep focus when int touch mode.</p>
880 * {@hide}
881 */
882 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
883
884 /**
885 * <p>Enables low quality mode for the drawing cache.</p>
886 */
887 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
888
889 /**
890 * <p>Enables high quality mode for the drawing cache.</p>
891 */
892 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
893
894 /**
895 * <p>Enables automatic quality mode for the drawing cache.</p>
896 */
897 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
898
899 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
900 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
901 };
902
903 /**
904 * <p>Mask for use with setFlags indicating bits used for the cache
905 * quality property.</p>
906 * {@hide}
907 */
908 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
909
910 /**
911 * <p>
912 * Indicates this view can be long clicked. When long clickable, a View
913 * reacts to long clicks by notifying the OnLongClickListener or showing a
914 * context menu.
915 * </p>
916 * {@hide}
917 */
918 static final int LONG_CLICKABLE = 0x00200000;
919
920 /**
921 * <p>Indicates that this view gets its drawable states from its direct parent
922 * and ignores its original internal states.</p>
923 *
924 * @hide
925 */
926 static final int DUPLICATE_PARENT_STATE = 0x00400000;
927
928 /**
929 * The scrollbar style to display the scrollbars inside the content area,
930 * without increasing the padding. The scrollbars will be overlaid with
931 * translucency on the view's content.
932 */
933 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
934
935 /**
936 * The scrollbar style to display the scrollbars inside the padded area,
937 * increasing the padding of the view. The scrollbars will not overlap the
938 * content area of the view.
939 */
940 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
941
942 /**
943 * The scrollbar style to display the scrollbars at the edge of the view,
944 * without increasing the padding. The scrollbars will be overlaid with
945 * translucency.
946 */
947 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
948
949 /**
950 * The scrollbar style to display the scrollbars at the edge of the view,
951 * increasing the padding of the view. The scrollbars will only overlap the
952 * background, if any.
953 */
954 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
955
956 /**
957 * Mask to check if the scrollbar style is overlay or inset.
958 * {@hide}
959 */
960 static final int SCROLLBARS_INSET_MASK = 0x01000000;
961
962 /**
963 * Mask to check if the scrollbar style is inside or outside.
964 * {@hide}
965 */
966 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
967
968 /**
969 * Mask for scrollbar style.
970 * {@hide}
971 */
972 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
973
974 /**
975 * View flag indicating that the screen should remain on while the
976 * window containing this view is visible to the user. This effectively
977 * takes care of automatically setting the WindowManager's
978 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
979 */
980 public static final int KEEP_SCREEN_ON = 0x04000000;
981
982 /**
983 * View flag indicating whether this view should have sound effects enabled
984 * for events such as clicking and touching.
985 */
986 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
987
988 /**
989 * View flag indicating whether this view should have haptic feedback
990 * enabled for events such as long presses.
991 */
992 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
993
994 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700995 * <p>Indicates that the view hierarchy should stop saving state when
996 * it reaches this view. If state saving is initiated immediately at
997 * the view, it will be allowed.
998 * {@hide}
999 */
1000 static final int PARENT_SAVE_DISABLED = 0x20000000;
1001
1002 /**
1003 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
1004 * {@hide}
1005 */
1006 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
1007
1008 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001009 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1010 * should add all focusable Views regardless if they are focusable in touch mode.
1011 */
1012 public static final int FOCUSABLES_ALL = 0x00000000;
1013
1014 /**
1015 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1016 * should add only Views focusable in touch mode.
1017 */
1018 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1019
1020 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001021 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * item.
1023 */
1024 public static final int FOCUS_BACKWARD = 0x00000001;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 * item.
1029 */
1030 public static final int FOCUS_FORWARD = 0x00000002;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_LEFT = 0x00000011;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_UP = 0x00000021;
1041
1042 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001043 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 */
1045 public static final int FOCUS_RIGHT = 0x00000042;
1046
1047 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001048 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 */
1050 public static final int FOCUS_DOWN = 0x00000082;
1051
Svetoslav Ganov42138042012-03-20 11:51:39 -07001052 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001053 * Bits of {@link #getMeasuredWidthAndState()} and
1054 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1055 */
1056 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1057
1058 /**
1059 * Bits of {@link #getMeasuredWidthAndState()} and
1060 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1061 */
1062 public static final int MEASURED_STATE_MASK = 0xff000000;
1063
1064 /**
1065 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1066 * for functions that combine both width and height into a single int,
1067 * such as {@link #getMeasuredState()} and the childState argument of
1068 * {@link #resolveSizeAndState(int, int, int)}.
1069 */
1070 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1071
1072 /**
1073 * Bit of {@link #getMeasuredWidthAndState()} and
1074 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1075 * is smaller that the space the view would like to have.
1076 */
1077 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1078
1079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 * Base View state sets
1081 */
1082 // Singles
1083 /**
1084 * Indicates the view has no states set. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is enabled. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is focused. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is selected. States are used with
1112 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1113 * view depending on its state.
1114 *
1115 * @see android.graphics.drawable.Drawable
1116 * @see #getDrawableState()
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is pressed. States are used with
1121 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1122 * view depending on its state.
1123 *
1124 * @see android.graphics.drawable.Drawable
1125 * @see #getDrawableState()
1126 * @hide
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 /**
1130 * Indicates the view's window has focus. States are used with
1131 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1132 * view depending on its state.
1133 *
1134 * @see android.graphics.drawable.Drawable
1135 * @see #getDrawableState()
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 // Doubles
1139 /**
1140 * Indicates the view is enabled and has the focus.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled and selected.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #SELECTED_STATE_SET
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 /**
1154 * Indicates the view is enabled and that its window has focus.
1155 *
1156 * @see #ENABLED_STATE_SET
1157 * @see #WINDOW_FOCUSED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view is focused and selected.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view has the focus and that its window has the focus.
1169 *
1170 * @see #FOCUSED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is selected and that its window has the focus.
1176 *
1177 * @see #SELECTED_STATE_SET
1178 * @see #WINDOW_FOCUSED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 // Triples
1182 /**
1183 * Indicates the view is enabled, focused and selected.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, focused and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #FOCUSED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is enabled, selected and its window has the focus.
1200 *
1201 * @see #ENABLED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is focused, selected and its window has the focus.
1208 *
1209 * @see #FOCUSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #WINDOW_FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is enabled, focused, selected and its window
1216 * has the focus.
1217 *
1218 * @see #ENABLED_STATE_SET
1219 * @see #FOCUSED_STATE_SET
1220 * @see #SELECTED_STATE_SET
1221 * @see #WINDOW_FOCUSED_STATE_SET
1222 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001223 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 /**
1225 * Indicates the view is pressed and its window has the focus.
1226 *
1227 * @see #PRESSED_STATE_SET
1228 * @see #WINDOW_FOCUSED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is pressed and selected.
1233 *
1234 * @see #PRESSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, selected and its window has the focus.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #SELECTED_STATE_SET
1243 * @see #WINDOW_FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed and focused.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and its window has the focus.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 * @see #WINDOW_FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused and selected.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, focused, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #FOCUSED_STATE_SET
1274 * @see #SELECTED_STATE_SET
1275 * @see #WINDOW_FOCUSED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed and enabled.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and its window has the focus.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled and selected.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled, selected and its window has the
1303 * focus.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #SELECTED_STATE_SET
1308 * @see #WINDOW_FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled and focused.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #FOCUSED_STATE_SET
1317 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
1320 * Indicates the view is pressed, enabled, focused and its window has the
1321 * focus.
1322 *
1323 * @see #PRESSED_STATE_SET
1324 * @see #ENABLED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 * @see #WINDOW_FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, focused and selected.
1331 *
1332 * @see #PRESSED_STATE_SET
1333 * @see #ENABLED_STATE_SET
1334 * @see #SELECTED_STATE_SET
1335 * @see #FOCUSED_STATE_SET
1336 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 /**
1339 * Indicates the view is pressed, enabled, focused, selected and its window
1340 * has the focus.
1341 *
1342 * @see #PRESSED_STATE_SET
1343 * @see #ENABLED_STATE_SET
1344 * @see #SELECTED_STATE_SET
1345 * @see #FOCUSED_STATE_SET
1346 * @see #WINDOW_FOCUSED_STATE_SET
1347 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001348 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 /**
1351 * The order here is very important to {@link #getDrawableState()}
1352 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001353 private static final int[][] VIEW_STATE_SETS;
1354
Romain Guyb051e892010-09-28 19:09:36 -07001355 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1356 static final int VIEW_STATE_SELECTED = 1 << 1;
1357 static final int VIEW_STATE_FOCUSED = 1 << 2;
1358 static final int VIEW_STATE_ENABLED = 1 << 3;
1359 static final int VIEW_STATE_PRESSED = 1 << 4;
1360 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001361 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001362 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001363 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1364 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001365
1366 static final int[] VIEW_STATE_IDS = new int[] {
1367 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1368 R.attr.state_selected, VIEW_STATE_SELECTED,
1369 R.attr.state_focused, VIEW_STATE_FOCUSED,
1370 R.attr.state_enabled, VIEW_STATE_ENABLED,
1371 R.attr.state_pressed, VIEW_STATE_PRESSED,
1372 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001373 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001374 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001375 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001376 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 };
1378
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001380 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1381 throw new IllegalStateException(
1382 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1383 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001385 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001387 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001389 orderedIds[i * 2] = viewState;
1390 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 }
1392 }
1393 }
Romain Guyb051e892010-09-28 19:09:36 -07001394 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1395 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1396 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 int numBits = Integer.bitCount(i);
1398 int[] set = new int[numBits];
1399 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int j = 0; j < orderedIds.length; j += 2) {
1401 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001402 set[pos++] = orderedIds[j];
1403 }
1404 }
1405 VIEW_STATE_SETS[i] = set;
1406 }
1407
1408 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1409 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1410 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1411 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1413 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1414 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1416 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1418 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_FOCUSED];
1421 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1422 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1424 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1426 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED];
1437 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1440
1441 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1442 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1444 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1446 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1473 | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1479 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1482 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1483 | VIEW_STATE_PRESSED];
1484 }
1485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001487 * Accessibility event types that are dispatched for text population.
1488 */
1489 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1490 AccessibilityEvent.TYPE_VIEW_CLICKED
1491 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1492 | AccessibilityEvent.TYPE_VIEW_SELECTED
1493 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1494 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1495 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001496 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001497 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001498 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001499 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1500 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001501
1502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 * Temporary Rect currently for use in setBackground(). This will probably
1504 * be extended in the future to hold our own class with more than just
1505 * a Rect. :)
1506 */
1507 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001508
1509 /**
1510 * Map used to store views' tags.
1511 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001512 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001515 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001516 */
1517 private static int sNextAccessibilityViewId;
1518
1519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 * The animation currently associated with this view.
1521 * @hide
1522 */
1523 protected Animation mCurrentAnimation = null;
1524
1525 /**
1526 * Width as measured during measure pass.
1527 * {@hide}
1528 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001529 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001530 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531
1532 /**
1533 * Height as measured during measure pass.
1534 * {@hide}
1535 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001536 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001537 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538
1539 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001540 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1541 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1542 * its display list. This flag, used only when hw accelerated, allows us to clear the
1543 * flag while retaining this information until it's needed (at getDisplayList() time and
1544 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1545 *
1546 * {@hide}
1547 */
1548 boolean mRecreateDisplayList = false;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The view's identifier.
1552 * {@hide}
1553 *
1554 * @see #setId(int)
1555 * @see #getId()
1556 */
1557 @ViewDebug.ExportedProperty(resolveId = true)
1558 int mID = NO_ID;
1559
1560 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001561 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001562 */
1563 int mAccessibilityViewId = NO_ID;
1564
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001565 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001566
1567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 * The view's tag.
1569 * {@hide}
1570 *
1571 * @see #setTag(Object)
1572 * @see #getTag()
1573 */
1574 protected Object mTag;
1575
1576 // for mPrivateFlags:
1577 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001578 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001580 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001582 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001584 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001586 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001588 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 /**
1590 * When this flag is set, this view is running an animation on behalf of its
1591 * children and should therefore not cancel invalidate requests, even if they
1592 * lie outside of this view's bounds.
1593 *
1594 * {@hide}
1595 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001596 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001598 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001600 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001602 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001604 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001606 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001608 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001609 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001610 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611
Dianne Hackborn4702a852012-08-17 15:18:29 -07001612 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
1614 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001615 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 /**
1617 * Flag used to indicate that this view should be drawn once more (and only once
1618 * more) after its animation has completed.
1619 * {@hide}
1620 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001621 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622
Dianne Hackborn4702a852012-08-17 15:18:29 -07001623 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
1625 /**
1626 * Indicates that the View returned true when onSetAlpha() was called and that
1627 * the alpha must be restored.
1628 * {@hide}
1629 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001630 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
1632 /**
1633 * Set by {@link #setScrollContainer(boolean)}.
1634 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001635 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
1637 /**
1638 * Set by {@link #setScrollContainer(boolean)}.
1639 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001640 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641
1642 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001643 * View flag indicating whether this view was invalidated (fully or partially.)
1644 *
1645 * @hide
1646 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001647 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001648
1649 /**
1650 * View flag indicating whether this view was invalidated by an opaque
1651 * invalidate request.
1652 *
1653 * @hide
1654 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001655 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001656
1657 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001658 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001659 *
1660 * @hide
1661 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001662 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001663
1664 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001665 * Indicates whether the background is opaque.
1666 *
1667 * @hide
1668 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001669 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001670
1671 /**
1672 * Indicates whether the scrollbars are opaque.
1673 *
1674 * @hide
1675 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001676 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001677
1678 /**
1679 * Indicates whether the view is opaque.
1680 *
1681 * @hide
1682 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001683 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powelle14579b2009-12-16 18:39:52 -08001685 /**
1686 * Indicates a prepressed state;
1687 * the short time between ACTION_DOWN and recognizing
1688 * a 'real' press. Prepressed is used to recognize quick taps
1689 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001690 *
Adam Powelle14579b2009-12-16 18:39:52 -08001691 * @hide
1692 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001693 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powellc9fbaab2010-02-16 17:16:19 -08001695 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001696 * Indicates whether the view is temporarily detached.
1697 *
1698 * @hide
1699 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001700 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001701
Adam Powell8568c3a2010-04-19 14:26:11 -07001702 /**
1703 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001704 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001705 * @hide
1706 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001707 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001708
1709 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001710 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1711 * @hide
1712 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001713 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001714
1715 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001716 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1717 * for transform operations
1718 *
1719 * @hide
1720 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001721 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001722
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001723 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001724 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001725
Chet Haasefd2b0022010-08-06 13:08:56 -07001726 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001727 * Indicates that this view was specifically invalidated, not just dirtied because some
1728 * child view was invalidated. The flag is used to determine when we need to recreate
1729 * a view's display list (as opposed to just returning a reference to its existing
1730 * display list).
1731 *
1732 * @hide
1733 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001734 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001735
Jeff Sharkey36901b62012-09-19 19:06:22 -07001736 /**
1737 * Masks for mPrivateFlags2, as generated by dumpFlags():
1738 *
1739 * -------|-------|-------|-------|
1740 * PFLAG2_TEXT_ALIGNMENT_FLAGS[0]
1741 * PFLAG2_TEXT_DIRECTION_FLAGS[0]
1742 * 1 PFLAG2_DRAG_CAN_ACCEPT
1743 * 1 PFLAG2_DRAG_HOVERED
1744 * 1 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
1745 * 11 PFLAG2_TEXT_DIRECTION_MASK_SHIFT
1746 * 1 1 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT
1747 * 11 PFLAG2_LAYOUT_DIRECTION_MASK
1748 * 11 1 PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
1749 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
1750 * 1 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT
1751 * 1 1 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT
1752 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED
1753 * 11 PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
1754 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[1]
1755 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[2]
1756 * 11 PFLAG2_TEXT_DIRECTION_FLAGS[3]
1757 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[4]
1758 * 1 1 PFLAG2_TEXT_DIRECTION_FLAGS[5]
1759 * 111 PFLAG2_TEXT_DIRECTION_MASK
1760 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED
1761 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT
1762 * 111 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK
1763 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[1]
1764 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[2]
1765 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[3]
1766 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[4]
1767 * 1 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[5]
1768 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[6]
1769 * 111 PFLAG2_TEXT_ALIGNMENT_MASK
1770 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED
1771 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT
1772 * 111 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK
1773 * 11 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK
1774 * 1 PFLAG2_HAS_TRANSIENT_STATE
1775 * 1 PFLAG2_ACCESSIBILITY_FOCUSED
1776 * 1 PFLAG2_ACCESSIBILITY_STATE_CHANGED
1777 * 1 PFLAG2_VIEW_QUICK_REJECTED
1778 * 1 PFLAG2_PADDING_RESOLVED
1779 * -------|-------|-------|-------|
1780 */
Christopher Tate3d4bf172011-03-28 16:16:46 -07001781
1782 /**
1783 * Indicates that this view has reported that it can accept the current drag's content.
1784 * Cleared when the drag operation concludes.
1785 * @hide
1786 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001787 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001788
1789 /**
1790 * Indicates that this view is currently directly under the drag location in a
1791 * drag-and-drop operation involving content that it can accept. Cleared when
1792 * the drag exits the view, or when the drag operation concludes.
1793 * @hide
1794 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001795 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001796
Cibu Johny86666632010-02-22 13:01:02 -08001797 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 * Horizontal layout direction of this view is from Left to Right.
1799 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001800 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001801 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001802
1803 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001804 * Horizontal layout direction of this view is from Right to Left.
1805 * Use with {@link #setLayoutDirection}.
1806 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001807 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001808
1809 /**
1810 * Horizontal layout direction of this view is inherited from its parent.
1811 * Use with {@link #setLayoutDirection}.
1812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Horizontal layout direction of this view is from deduced from the default language
1817 * script for the locale. Use with {@link #setLayoutDirection}.
1818 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001819 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001820
1821 /**
1822 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001823 * @hide
1824 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001825 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Mask for use with private flags indicating bits used for horizontal layout direction.
1829 * @hide
1830 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001831 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001832
1833 /**
1834 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1835 * right-to-left direction.
1836 * @hide
1837 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001838 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Indicates whether the view horizontal layout direction has been resolved.
1842 * @hide
1843 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001844 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /**
1847 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1848 * @hide
1849 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001850 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1851 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001852
1853 /*
1854 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1855 * flag value.
1856 * @hide
1857 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001858 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1859 LAYOUT_DIRECTION_LTR,
1860 LAYOUT_DIRECTION_RTL,
1861 LAYOUT_DIRECTION_INHERIT,
1862 LAYOUT_DIRECTION_LOCALE
1863 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001864
1865 /**
1866 * Default horizontal layout direction.
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001867 */
1868 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001869
Adam Powell539ee872012-02-03 19:00:49 -08001870 /**
1871 * Indicates that the view is tracking some sort of transient state
1872 * that the app should not need to be aware of, but that the framework
1873 * should take special care to preserve.
1874 *
1875 * @hide
1876 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001877 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001878
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001879 /**
1880 * Text direction is inherited thru {@link ViewGroup}
1881 */
1882 public static final int TEXT_DIRECTION_INHERIT = 0;
1883
1884 /**
1885 * Text direction is using "first strong algorithm". The first strong directional character
1886 * determines the paragraph direction. If there is no strong directional character, the
1887 * paragraph direction is the view's resolved layout direction.
1888 */
1889 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1890
1891 /**
1892 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1893 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1894 * If there are neither, the paragraph direction is the view's resolved layout direction.
1895 */
1896 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1897
1898 /**
1899 * Text direction is forced to LTR.
1900 */
1901 public static final int TEXT_DIRECTION_LTR = 3;
1902
1903 /**
1904 * Text direction is forced to RTL.
1905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
1910 */
1911 public static final int TEXT_DIRECTION_LOCALE = 5;
1912
1913 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001914 * Default text direction is inherited
1915 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07001916 private static final int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917
1918 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001919 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1920 * @hide
1921 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001922 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001923
1924 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001925 * Mask for use with private flags indicating bits used for text direction.
1926 * @hide
1927 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001928 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1929 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001930
1931 /**
1932 * Array of text direction flags for mapping attribute "textDirection" to correct
1933 * flag value.
1934 * @hide
1935 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001936 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1937 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1938 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001943 };
1944
1945 /**
1946 * Indicates whether the view text direction has been resolved.
1947 * @hide
1948 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001949 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1950 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001956 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001962 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1963 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001964
1965 /**
1966 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1967 * @hide
1968 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001969 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1970 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001971
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001972 /*
1973 * Default text alignment. The text alignment of this View is inherited from its parent.
1974 * Use with {@link #setTextAlignment(int)}
1975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
1983 */
1984 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1985
1986 /**
1987 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1988 *
1989 * Use with {@link #setTextAlignment(int)}
1990 */
1991 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1992
1993 /**
1994 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1995 *
1996 * Use with {@link #setTextAlignment(int)}
1997 */
1998 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1999
2000 /**
2001 * Center the paragraph, e.g. ALIGN_CENTER.
2002 *
2003 * Use with {@link #setTextAlignment(int)}
2004 */
2005 public static final int TEXT_ALIGNMENT_CENTER = 4;
2006
2007 /**
2008 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2009 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2010 *
2011 * Use with {@link #setTextAlignment(int)}
2012 */
2013 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2014
2015 /**
2016 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2017 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2018 *
2019 * Use with {@link #setTextAlignment(int)}
2020 */
2021 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2022
2023 /**
2024 * Default text alignment is inherited
2025 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07002026 private static final int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002027
2028 /**
2029 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2030 * @hide
2031 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002032 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002033
2034 /**
2035 * Mask for use with private flags indicating bits used for text alignment.
2036 * @hide
2037 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002038 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002039
2040 /**
2041 * Array of text direction flags for mapping attribute "textAlignment" to correct
2042 * flag value.
2043 * @hide
2044 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002045 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2046 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2047 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2048 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2052 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002053 };
2054
2055 /**
2056 * Indicates whether the view text alignment has been resolved.
2057 * @hide
2058 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002059 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002060
2061 /**
2062 * Bit shift to get the resolved text alignment.
2063 * @hide
2064 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002065 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002066
2067 /**
2068 * Mask for use with private flags indicating bits used for text alignment.
2069 * @hide
2070 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002071 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2072 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002073
2074 /**
2075 * Indicates whether if the view text alignment has been resolved to gravity
2076 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002077 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2078 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002079
Svetoslav Ganov42138042012-03-20 11:51:39 -07002080 // Accessiblity constants for mPrivateFlags2
2081
2082 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002083 * Shift for the bits in {@link #mPrivateFlags2} related to the
2084 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002085 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002086 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002087
2088 /**
2089 * Automatically determine whether a view is important for accessibility.
2090 */
2091 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2092
2093 /**
2094 * The view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2097
2098 /**
2099 * The view is not important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2102
2103 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002104 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002105 */
2106 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2107
2108 /**
2109 * Mask for obtainig the bits which specify how to determine
2110 * whether a view is important for accessibility.
2111 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002112 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002113 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002114 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002115
2116 /**
2117 * Flag indicating whether a view has accessibility focus.
2118 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002119 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002120
2121 /**
2122 * Flag indicating whether a view state for accessibility has changed.
2123 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002124 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2125 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002126
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002127 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002128 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2129 * is used to check whether later changes to the view's transform should invalidate the
2130 * view to force the quickReject test to run again.
2131 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002132 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002133
Adam Powell0090f202012-08-07 17:15:30 -07002134 /**
2135 * Flag indicating that start/end padding has been resolved into left/right padding
2136 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2137 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2138 * during measurement. In some special cases this is required such as when an adapter-based
2139 * view measures prospective children without attaching them to a window.
2140 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002141 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002142
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002143 /**
2144 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2145 */
2146 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2147
2148 /**
2149 * Group of bits indicating that RTL properties resolution is done.
2150 */
2151 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002152 PFLAG2_TEXT_DIRECTION_RESOLVED |
2153 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2154 PFLAG2_PADDING_RESOLVED |
2155 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002156
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002157 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002158
Christopher Tate3d4bf172011-03-28 16:16:46 -07002159 /* End of masks for mPrivateFlags2 */
2160
Chet Haase21433372012-06-05 07:54:09 -07002161 /* Masks for mPrivateFlags3 */
2162
2163 /**
2164 * Flag indicating that view has a transform animation set on it. This is used to track whether
2165 * an animation is cleared between successive frames, in order to tell the associated
2166 * DisplayList to clear its animation matrix.
2167 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002168 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002169
2170 /**
2171 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2172 * animation is cleared between successive frames, in order to tell the associated
2173 * DisplayList to restore its alpha value.
2174 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002175 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002176
2177
2178 /* End of masks for mPrivateFlags3 */
2179
Dianne Hackborn4702a852012-08-17 15:18:29 -07002180 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002181
Chet Haasedaf98e92011-01-10 14:10:36 -08002182 /**
Adam Powell637d3372010-08-25 14:37:03 -07002183 * Always allow a user to over-scroll this view, provided it is a
2184 * view that can scroll.
2185 *
2186 * @see #getOverScrollMode()
2187 * @see #setOverScrollMode(int)
2188 */
2189 public static final int OVER_SCROLL_ALWAYS = 0;
2190
2191 /**
2192 * Allow a user to over-scroll this view only if the content is large
2193 * enough to meaningfully scroll, provided it is a view that can scroll.
2194 *
2195 * @see #getOverScrollMode()
2196 * @see #setOverScrollMode(int)
2197 */
2198 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2199
2200 /**
2201 * Never allow a user to over-scroll this view.
2202 *
2203 * @see #getOverScrollMode()
2204 * @see #setOverScrollMode(int)
2205 */
2206 public static final int OVER_SCROLL_NEVER = 2;
2207
2208 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002209 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2210 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002211 *
Joe Malin32736f02011-01-19 16:14:20 -08002212 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002213 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002214 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002215
2216 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002217 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2218 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002219 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002220 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002221 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002222 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002223 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002224 *
Joe Malin32736f02011-01-19 16:14:20 -08002225 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002226 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002227 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2228
2229 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002230 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2231 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002232 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002233 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002234 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2235 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2236 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002237 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002238 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2239 * window flags) for displaying content using every last pixel on the display.
2240 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002241 * <p>There is a limitation: because navigation controls are so important, the least user
2242 * interaction will cause them to reappear immediately. When this happens, both
2243 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2244 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002245 *
2246 * @see #setSystemUiVisibility(int)
2247 */
2248 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2249
2250 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002251 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2252 * into the normal fullscreen mode so that its content can take over the screen
2253 * while still allowing the user to interact with the application.
2254 *
2255 * <p>This has the same visual effect as
2256 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2257 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2258 * meaning that non-critical screen decorations (such as the status bar) will be
2259 * hidden while the user is in the View's window, focusing the experience on
2260 * that content. Unlike the window flag, if you are using ActionBar in
2261 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2262 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2263 * hide the action bar.
2264 *
2265 * <p>This approach to going fullscreen is best used over the window flag when
2266 * it is a transient state -- that is, the application does this at certain
2267 * points in its user interaction where it wants to allow the user to focus
2268 * on content, but not as a continuous state. For situations where the application
2269 * would like to simply stay full screen the entire time (such as a game that
2270 * wants to take over the screen), the
2271 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2272 * is usually a better approach. The state set here will be removed by the system
2273 * in various situations (such as the user moving to another application) like
2274 * the other system UI states.
2275 *
2276 * <p>When using this flag, the application should provide some easy facility
2277 * for the user to go out of it. A common example would be in an e-book
2278 * reader, where tapping on the screen brings back whatever screen and UI
2279 * decorations that had been hidden while the user was immersed in reading
2280 * the book.
2281 *
2282 * @see #setSystemUiVisibility(int)
2283 */
2284 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2285
2286 /**
2287 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2288 * flags, we would like a stable view of the content insets given to
2289 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2290 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002291 * as a continuous state. In the stock Android UI this is the space for
2292 * the system bar, nav bar, and status bar, but not more transient elements
2293 * such as an input method.
2294 *
2295 * The stable layout your UI sees is based on the system UI modes you can
2296 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2297 * then you will get a stable layout for changes of the
2298 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2299 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2300 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2301 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2302 * with a stable layout. (Note that you should avoid using
2303 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2304 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002305 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002306 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2307 * then a hidden status bar will be considered a "stable" state for purposes
2308 * here. This allows your UI to continually hide the status bar, while still
2309 * using the system UI flags to hide the action bar while still retaining
2310 * a stable layout. Note that changing the window fullscreen flag will never
2311 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002312 *
2313 * <p>If you are using ActionBar in
2314 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2315 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2316 * insets it adds to those given to the application.
2317 */
2318 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2319
2320 /**
2321 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2322 * to be layed out as if it has requested
2323 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2324 * allows it to avoid artifacts when switching in and out of that mode, at
2325 * the expense that some of its user interface may be covered by screen
2326 * decorations when they are shown. You can perform layout of your inner
2327 * UI elements to account for the navagation system UI through the
2328 * {@link #fitSystemWindows(Rect)} method.
2329 */
2330 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2331
2332 /**
2333 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2334 * to be layed out as if it has requested
2335 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2336 * allows it to avoid artifacts when switching in and out of that mode, at
2337 * the expense that some of its user interface may be covered by screen
2338 * decorations when they are shown. You can perform layout of your inner
2339 * UI elements to account for non-fullscreen system UI through the
2340 * {@link #fitSystemWindows(Rect)} method.
2341 */
2342 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2343
2344 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002345 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2346 */
2347 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2348
2349 /**
2350 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2351 */
2352 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002353
2354 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002355 * @hide
2356 *
2357 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2358 * out of the public fields to keep the undefined bits out of the developer's way.
2359 *
2360 * Flag to make the status bar not expandable. Unless you also
2361 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2362 */
2363 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2364
2365 /**
2366 * @hide
2367 *
2368 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2369 * out of the public fields to keep the undefined bits out of the developer's way.
2370 *
2371 * Flag to hide notification icons and scrolling ticker text.
2372 */
2373 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2374
2375 /**
2376 * @hide
2377 *
2378 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2379 * out of the public fields to keep the undefined bits out of the developer's way.
2380 *
2381 * Flag to disable incoming notification alerts. This will not block
2382 * icons, but it will block sound, vibrating and other visual or aural notifications.
2383 */
2384 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2385
2386 /**
2387 * @hide
2388 *
2389 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2390 * out of the public fields to keep the undefined bits out of the developer's way.
2391 *
2392 * Flag to hide only the scrolling ticker. Note that
2393 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2394 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2395 */
2396 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2397
2398 /**
2399 * @hide
2400 *
2401 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2402 * out of the public fields to keep the undefined bits out of the developer's way.
2403 *
2404 * Flag to hide the center system info area.
2405 */
2406 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2407
2408 /**
2409 * @hide
2410 *
2411 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2412 * out of the public fields to keep the undefined bits out of the developer's way.
2413 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002414 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002415 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2416 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002417 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002418
2419 /**
2420 * @hide
2421 *
2422 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2423 * out of the public fields to keep the undefined bits out of the developer's way.
2424 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002425 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002426 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2427 */
2428 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2429
2430 /**
2431 * @hide
2432 *
2433 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2434 * out of the public fields to keep the undefined bits out of the developer's way.
2435 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002436 * Flag to hide only the clock. You might use this if your activity has
2437 * its own clock making the status bar's clock redundant.
2438 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002439 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2440
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002441 /**
2442 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002443 *
2444 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2445 * out of the public fields to keep the undefined bits out of the developer's way.
2446 *
2447 * Flag to hide only the recent apps button. Don't use this
2448 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2449 */
2450 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2451
2452 /**
2453 * @hide
Daniel Sandlerd5483c32012-10-19 16:44:15 -04002454 *
2455 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2456 * out of the public fields to keep the undefined bits out of the developer's way.
2457 *
2458 * Flag to disable the global search gesture. Don't use this
2459 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2460 */
2461 public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
2462
2463 /**
2464 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002465 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002466 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002467
2468 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002469 * These are the system UI flags that can be cleared by events outside
2470 * of an application. Currently this is just the ability to tap on the
2471 * screen while hiding the navigation bar to have it return.
2472 * @hide
2473 */
2474 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002475 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2476 | SYSTEM_UI_FLAG_FULLSCREEN;
2477
2478 /**
2479 * Flags that can impact the layout in relation to system UI.
2480 */
2481 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2482 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2483 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002484
2485 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002486 * Find views that render the specified text.
2487 *
2488 * @see #findViewsWithText(ArrayList, CharSequence, int)
2489 */
2490 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2491
2492 /**
2493 * Find find views that contain the specified content description.
2494 *
2495 * @see #findViewsWithText(ArrayList, CharSequence, int)
2496 */
2497 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2498
2499 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002500 * Find views that contain {@link AccessibilityNodeProvider}. Such
2501 * a View is a root of virtual view hierarchy and may contain the searched
2502 * text. If this flag is set Views with providers are automatically
2503 * added and it is a responsibility of the client to call the APIs of
2504 * the provider to determine whether the virtual tree rooted at this View
2505 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2506 * represeting the virtual views with this text.
2507 *
2508 * @see #findViewsWithText(ArrayList, CharSequence, int)
2509 *
2510 * @hide
2511 */
2512 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2513
2514 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002515 * The undefined cursor position.
Svetoslav7c512842013-01-30 23:02:08 -08002516 *
2517 * @hide
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002518 */
Svetoslav7c512842013-01-30 23:02:08 -08002519 public static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002520
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 }
Svetoslav Ganov80943d82013-01-02 10:25:37 -08005010
5011 if ((mAttachInfo.mAccessibilityFetchFlags
5012 & AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS) != 0) {
5013 String viewId = null;
5014 try {
5015 viewId = getResources().getResourceName(mID);
5016 } catch (Resources.NotFoundException nfe) {
5017 /* ignore */
5018 }
5019 if (viewId == null) {
5020 try {
5021 viewId = ((Context) ActivityThread.currentActivityThread()
5022 .getSystemContext()).getResources().getResourceName(mID);
5023 } catch (Resources.NotFoundException nfe) {
5024 /* ignore */
5025 }
5026 }
5027 info.setViewId(viewId);
5028 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005029 }
5030
5031 if (mLabelForId != View.NO_ID) {
5032 View rootView = getRootView();
5033 if (rootView == null) {
5034 rootView = this;
5035 }
5036 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
5037 if (labeled != null) {
5038 info.setLabelFor(labeled);
5039 }
5040 }
5041
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005042 info.setVisibleToUser(isVisibleToUser());
5043
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005044 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005045 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005046 info.setContentDescription(getContentDescription());
5047
5048 info.setEnabled(isEnabled());
5049 info.setClickable(isClickable());
5050 info.setFocusable(isFocusable());
5051 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005052 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005053 info.setSelected(isSelected());
5054 info.setLongClickable(isLongClickable());
5055
5056 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005057 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005058 // we should report as selectable view in an AdapterView.
5059 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5060 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5061
5062 if (isFocusable()) {
5063 if (isFocused()) {
5064 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5065 } else {
5066 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5067 }
5068 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005069
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005070 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005071 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005072 } else {
5073 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5074 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005075
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005076 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005077 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5078 }
5079
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005080 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005081 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5082 }
5083
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005084 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005085 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5086 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5087 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005088 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5089 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005090 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005091 }
5092
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005093 private View findLabelForView(View view, int labeledId) {
5094 if (mMatchLabelForPredicate == null) {
5095 mMatchLabelForPredicate = new MatchLabelForPredicate();
5096 }
5097 mMatchLabelForPredicate.mLabeledId = labeledId;
5098 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5099 }
5100
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005101 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005102 * Computes whether this view is visible to the user. Such a view is
5103 * attached, visible, all its predecessors are visible, it is not clipped
5104 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005105 *
5106 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005107 *
5108 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005109 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005110 protected boolean isVisibleToUser() {
5111 return isVisibleToUser(null);
5112 }
5113
5114 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005115 * Computes whether the given portion of this view is visible to the user.
5116 * Such a view is attached, visible, all its predecessors are visible,
5117 * has an alpha greater than zero, and the specified portion is not
5118 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005119 *
5120 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5121 * <code>null</code>, and the entire view will be tested in this case.
5122 * When <code>true</code> is returned by the function, the actual visible
5123 * region will be stored in this parameter; that is, if boundInView is fully
5124 * contained within the view, no modification will be made, otherwise regions
5125 * outside of the visible area of the view will be clipped.
5126 *
5127 * @return Whether the specified portion of the view is visible on the screen.
5128 *
5129 * @hide
5130 */
5131 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005132 if (mAttachInfo != null) {
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005133 // Attached to invisible window means this view is not visible.
5134 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
5135 return false;
5136 }
5137 // An invisible predecessor or one with alpha zero means
5138 // that this view is not visible to the user.
5139 Object current = this;
5140 while (current instanceof View) {
5141 View view = (View) current;
5142 // We have attach info so this view is attached and there is no
5143 // need to check whether we reach to ViewRootImpl on the way up.
5144 if (view.getAlpha() <= 0 || view.getVisibility() != VISIBLE) {
5145 return false;
5146 }
5147 current = view.mParent;
5148 }
5149 // Check if the view is entirely covered by its predecessors.
Romain Guyf0af1d52012-07-11 18:31:21 -07005150 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5151 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005152 if (!getGlobalVisibleRect(visibleRect, offset)) {
5153 return false;
Guang Zhu0d607fb2012-05-11 19:34:56 -07005154 }
Svetoslav Ganov11f19f12012-11-02 16:03:06 -07005155 // Check if the visible portion intersects the rectangle of interest.
5156 if (boundInView != null) {
5157 visibleRect.offset(-offset.x, -offset.y);
5158 return boundInView.intersect(visibleRect);
5159 }
5160 return true;
Romain Guyf0af1d52012-07-11 18:31:21 -07005161 }
Romain Guyf0af1d52012-07-11 18:31:21 -07005162 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005163 }
5164
5165 /**
alanvb72fe7a2012-08-27 16:44:25 -07005166 * Returns the delegate for implementing accessibility support via
5167 * composition. For more details see {@link AccessibilityDelegate}.
5168 *
5169 * @return The delegate, or null if none set.
5170 *
5171 * @hide
5172 */
5173 public AccessibilityDelegate getAccessibilityDelegate() {
5174 return mAccessibilityDelegate;
5175 }
5176
5177 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005178 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005179 * opposed to inheritance. The delegate's primary use is for implementing
5180 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5181 *
5182 * @param delegate The delegate instance.
5183 *
5184 * @see AccessibilityDelegate
5185 */
5186 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5187 mAccessibilityDelegate = delegate;
5188 }
5189
5190 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005191 * Gets the provider for managing a virtual view hierarchy rooted at this View
5192 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5193 * that explore the window content.
5194 * <p>
5195 * If this method returns an instance, this instance is responsible for managing
5196 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5197 * View including the one representing the View itself. Similarly the returned
5198 * instance is responsible for performing accessibility actions on any virtual
5199 * view or the root view itself.
5200 * </p>
5201 * <p>
5202 * If an {@link AccessibilityDelegate} has been specified via calling
5203 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5204 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5205 * is responsible for handling this call.
5206 * </p>
5207 *
5208 * @return The provider.
5209 *
5210 * @see AccessibilityNodeProvider
5211 */
5212 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5213 if (mAccessibilityDelegate != null) {
5214 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5215 } else {
5216 return null;
5217 }
5218 }
5219
5220 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005221 * Gets the unique identifier of this view on the screen for accessibility purposes.
5222 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5223 *
5224 * @return The view accessibility id.
5225 *
5226 * @hide
5227 */
5228 public int getAccessibilityViewId() {
5229 if (mAccessibilityViewId == NO_ID) {
5230 mAccessibilityViewId = sNextAccessibilityViewId++;
5231 }
5232 return mAccessibilityViewId;
5233 }
5234
5235 /**
5236 * Gets the unique identifier of the window in which this View reseides.
5237 *
5238 * @return The window accessibility id.
5239 *
5240 * @hide
5241 */
5242 public int getAccessibilityWindowId() {
5243 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5244 }
5245
5246 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005247 * Gets the {@link View} description. It briefly describes the view and is
5248 * primarily used for accessibility support. Set this property to enable
5249 * better accessibility support for your application. This is especially
5250 * true for views that do not have textual representation (For example,
5251 * ImageButton).
5252 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005253 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005254 *
5255 * @attr ref android.R.styleable#View_contentDescription
5256 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005257 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005258 public CharSequence getContentDescription() {
5259 return mContentDescription;
5260 }
5261
5262 /**
5263 * Sets the {@link View} description. It briefly describes the view and is
5264 * primarily used for accessibility support. Set this property to enable
5265 * better accessibility support for your application. This is especially
5266 * true for views that do not have textual representation (For example,
5267 * ImageButton).
5268 *
5269 * @param contentDescription The content description.
5270 *
5271 * @attr ref android.R.styleable#View_contentDescription
5272 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005273 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005274 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005275 if (mContentDescription == null) {
5276 if (contentDescription == null) {
5277 return;
5278 }
5279 } else if (mContentDescription.equals(contentDescription)) {
5280 return;
5281 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005282 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005283 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5284 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5285 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5286 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005287 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005288 }
5289
5290 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005291 * Gets the id of a view for which this view serves as a label for
5292 * accessibility purposes.
5293 *
5294 * @return The labeled view id.
5295 */
5296 @ViewDebug.ExportedProperty(category = "accessibility")
5297 public int getLabelFor() {
5298 return mLabelForId;
5299 }
5300
5301 /**
5302 * Sets the id of a view for which this view serves as a label for
5303 * accessibility purposes.
5304 *
5305 * @param id The labeled view id.
5306 */
5307 @RemotableViewMethod
5308 public void setLabelFor(int id) {
5309 mLabelForId = id;
5310 if (mLabelForId != View.NO_ID
5311 && mID == View.NO_ID) {
5312 mID = generateViewId();
5313 }
5314 }
5315
5316 /**
Romain Guya2431d02009-04-30 16:30:00 -07005317 * Invoked whenever this view loses focus, either by losing window focus or by losing
5318 * focus within its window. This method can be used to clear any state tied to the
5319 * focus. For instance, if a button is held pressed with the trackball and the window
5320 * loses focus, this method can be used to cancel the press.
5321 *
5322 * Subclasses of View overriding this method should always call super.onFocusLost().
5323 *
5324 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005325 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005326 *
5327 * @hide pending API council approval
5328 */
5329 protected void onFocusLost() {
5330 resetPressedState();
5331 }
5332
5333 private void resetPressedState() {
5334 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5335 return;
5336 }
5337
5338 if (isPressed()) {
5339 setPressed(false);
5340
5341 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005342 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005343 }
5344 }
5345 }
5346
5347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 * Returns true if this view has focus
5349 *
5350 * @return True if this view has focus, false otherwise.
5351 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005352 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005354 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 }
5356
5357 /**
5358 * Find the view in the hierarchy rooted at this view that currently has
5359 * focus.
5360 *
5361 * @return The view that currently has focus, or null if no focused view can
5362 * be found.
5363 */
5364 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005365 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 }
5367
5368 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005369 * Indicates whether this view is one of the set of scrollable containers in
5370 * its window.
5371 *
5372 * @return whether this view is one of the set of scrollable containers in
5373 * its window
5374 *
5375 * @attr ref android.R.styleable#View_isScrollContainer
5376 */
5377 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005378 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005379 }
5380
5381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 * Change whether this view is one of the set of scrollable containers in
5383 * its window. This will be used to determine whether the window can
5384 * resize or must pan when a soft input area is open -- scrollable
5385 * containers allow the window to use resize mode since the container
5386 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005387 *
5388 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 */
5390 public void setScrollContainer(boolean isScrollContainer) {
5391 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005392 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005394 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005396 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005398 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 mAttachInfo.mScrollContainers.remove(this);
5400 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005401 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 }
5403 }
5404
5405 /**
5406 * Returns the quality of the drawing cache.
5407 *
5408 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5409 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5410 *
5411 * @see #setDrawingCacheQuality(int)
5412 * @see #setDrawingCacheEnabled(boolean)
5413 * @see #isDrawingCacheEnabled()
5414 *
5415 * @attr ref android.R.styleable#View_drawingCacheQuality
5416 */
5417 public int getDrawingCacheQuality() {
5418 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5419 }
5420
5421 /**
5422 * Set the drawing cache quality of this view. This value is used only when the
5423 * drawing cache is enabled
5424 *
5425 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5426 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5427 *
5428 * @see #getDrawingCacheQuality()
5429 * @see #setDrawingCacheEnabled(boolean)
5430 * @see #isDrawingCacheEnabled()
5431 *
5432 * @attr ref android.R.styleable#View_drawingCacheQuality
5433 */
5434 public void setDrawingCacheQuality(int quality) {
5435 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5436 }
5437
5438 /**
5439 * Returns whether the screen should remain on, corresponding to the current
5440 * value of {@link #KEEP_SCREEN_ON}.
5441 *
5442 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5443 *
5444 * @see #setKeepScreenOn(boolean)
5445 *
5446 * @attr ref android.R.styleable#View_keepScreenOn
5447 */
5448 public boolean getKeepScreenOn() {
5449 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5450 }
5451
5452 /**
5453 * Controls whether the screen should remain on, modifying the
5454 * value of {@link #KEEP_SCREEN_ON}.
5455 *
5456 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5457 *
5458 * @see #getKeepScreenOn()
5459 *
5460 * @attr ref android.R.styleable#View_keepScreenOn
5461 */
5462 public void setKeepScreenOn(boolean keepScreenOn) {
5463 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5464 }
5465
5466 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005467 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5468 * @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 -08005469 *
5470 * @attr ref android.R.styleable#View_nextFocusLeft
5471 */
5472 public int getNextFocusLeftId() {
5473 return mNextFocusLeftId;
5474 }
5475
5476 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005477 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5478 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5479 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 *
5481 * @attr ref android.R.styleable#View_nextFocusLeft
5482 */
5483 public void setNextFocusLeftId(int nextFocusLeftId) {
5484 mNextFocusLeftId = nextFocusLeftId;
5485 }
5486
5487 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005488 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5489 * @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 -08005490 *
5491 * @attr ref android.R.styleable#View_nextFocusRight
5492 */
5493 public int getNextFocusRightId() {
5494 return mNextFocusRightId;
5495 }
5496
5497 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005498 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5499 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5500 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 *
5502 * @attr ref android.R.styleable#View_nextFocusRight
5503 */
5504 public void setNextFocusRightId(int nextFocusRightId) {
5505 mNextFocusRightId = nextFocusRightId;
5506 }
5507
5508 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005509 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5510 * @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 -08005511 *
5512 * @attr ref android.R.styleable#View_nextFocusUp
5513 */
5514 public int getNextFocusUpId() {
5515 return mNextFocusUpId;
5516 }
5517
5518 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005519 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5520 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5521 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 *
5523 * @attr ref android.R.styleable#View_nextFocusUp
5524 */
5525 public void setNextFocusUpId(int nextFocusUpId) {
5526 mNextFocusUpId = nextFocusUpId;
5527 }
5528
5529 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005530 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5531 * @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 -08005532 *
5533 * @attr ref android.R.styleable#View_nextFocusDown
5534 */
5535 public int getNextFocusDownId() {
5536 return mNextFocusDownId;
5537 }
5538
5539 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005540 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5541 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5542 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 *
5544 * @attr ref android.R.styleable#View_nextFocusDown
5545 */
5546 public void setNextFocusDownId(int nextFocusDownId) {
5547 mNextFocusDownId = nextFocusDownId;
5548 }
5549
5550 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005551 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5552 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5553 *
5554 * @attr ref android.R.styleable#View_nextFocusForward
5555 */
5556 public int getNextFocusForwardId() {
5557 return mNextFocusForwardId;
5558 }
5559
5560 /**
5561 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5562 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5563 * decide automatically.
5564 *
5565 * @attr ref android.R.styleable#View_nextFocusForward
5566 */
5567 public void setNextFocusForwardId(int nextFocusForwardId) {
5568 mNextFocusForwardId = nextFocusForwardId;
5569 }
5570
5571 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 * Returns the visibility of this view and all of its ancestors
5573 *
5574 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5575 */
5576 public boolean isShown() {
5577 View current = this;
5578 //noinspection ConstantConditions
5579 do {
5580 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5581 return false;
5582 }
5583 ViewParent parent = current.mParent;
5584 if (parent == null) {
5585 return false; // We are not attached to the view root
5586 }
5587 if (!(parent instanceof View)) {
5588 return true;
5589 }
5590 current = (View) parent;
5591 } while (current != null);
5592
5593 return false;
5594 }
5595
5596 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005597 * Called by the view hierarchy when the content insets for a window have
5598 * changed, to allow it to adjust its content to fit within those windows.
5599 * The content insets tell you the space that the status bar, input method,
5600 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005602 * <p>You do not normally need to deal with this function, since the default
5603 * window decoration given to applications takes care of applying it to the
5604 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5605 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5606 * and your content can be placed under those system elements. You can then
5607 * use this method within your view hierarchy if you have parts of your UI
5608 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005610 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005611 * inset's to the view's padding, consuming that content (modifying the
5612 * insets to be 0), and returning true. This behavior is off by default, but can
5613 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5614 *
5615 * <p>This function's traversal down the hierarchy is depth-first. The same content
5616 * insets object is propagated down the hierarchy, so any changes made to it will
5617 * be seen by all following views (including potentially ones above in
5618 * the hierarchy since this is a depth-first traversal). The first view
5619 * that returns true will abort the entire traversal.
5620 *
5621 * <p>The default implementation works well for a situation where it is
5622 * used with a container that covers the entire window, allowing it to
5623 * apply the appropriate insets to its content on all edges. If you need
5624 * a more complicated layout (such as two different views fitting system
5625 * windows, one on the top of the window, and one on the bottom),
5626 * you can override the method and handle the insets however you would like.
5627 * Note that the insets provided by the framework are always relative to the
5628 * far edges of the window, not accounting for the location of the called view
5629 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005630 * where the layout will place the view, as it is done before layout happens.)
5631 *
5632 * <p>Note: unlike many View methods, there is no dispatch phase to this
5633 * call. If you are overriding it in a ViewGroup and want to allow the
5634 * call to continue to your children, you must be sure to call the super
5635 * implementation.
5636 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005637 * <p>Here is a sample layout that makes use of fitting system windows
5638 * to have controls for a video view placed inside of the window decorations
5639 * that it hides and shows. This can be used with code like the second
5640 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5641 *
5642 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5643 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005644 * @param insets Current content insets of the window. Prior to
5645 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5646 * the insets or else you and Android will be unhappy.
5647 *
5648 * @return Return true if this view applied the insets and it should not
5649 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005650 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005651 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005652 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 */
5654 protected boolean fitSystemWindows(Rect insets) {
5655 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005656 mUserPaddingStart = UNDEFINED_PADDING;
5657 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005658 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5659 || mAttachInfo == null
5660 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5661 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5662 return true;
5663 } else {
5664 internalSetPadding(0, 0, 0, 0);
5665 return false;
5666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 }
5668 return false;
5669 }
5670
5671 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005672 * Sets whether or not this view should account for system screen decorations
5673 * such as the status bar and inset its content; that is, controlling whether
5674 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5675 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005676 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005677 * <p>Note that if you are providing your own implementation of
5678 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5679 * flag to true -- your implementation will be overriding the default
5680 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005681 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005682 * @param fitSystemWindows If true, then the default implementation of
5683 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005684 *
5685 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005686 * @see #getFitsSystemWindows()
5687 * @see #fitSystemWindows(Rect)
5688 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005689 */
5690 public void setFitsSystemWindows(boolean fitSystemWindows) {
5691 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5692 }
5693
5694 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005695 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005696 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5697 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005698 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005699 * @return Returns true if the default implementation of
5700 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005701 *
5702 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005703 * @see #setFitsSystemWindows()
5704 * @see #fitSystemWindows(Rect)
5705 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005706 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005707 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005708 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5709 }
5710
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005711 /** @hide */
5712 public boolean fitsSystemWindows() {
5713 return getFitsSystemWindows();
5714 }
5715
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005716 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005717 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5718 */
5719 public void requestFitSystemWindows() {
5720 if (mParent != null) {
5721 mParent.requestFitSystemWindows();
5722 }
5723 }
5724
5725 /**
5726 * For use by PhoneWindow to make its own system window fitting optional.
5727 * @hide
5728 */
5729 public void makeOptionalFitsSystemWindows() {
5730 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5731 }
5732
5733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 * Returns the visibility status for this view.
5735 *
5736 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5737 * @attr ref android.R.styleable#View_visibility
5738 */
5739 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005740 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5741 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5742 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 })
5744 public int getVisibility() {
5745 return mViewFlags & VISIBILITY_MASK;
5746 }
5747
5748 /**
5749 * Set the enabled state of this view.
5750 *
5751 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5752 * @attr ref android.R.styleable#View_visibility
5753 */
5754 @RemotableViewMethod
5755 public void setVisibility(int visibility) {
5756 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005757 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 }
5759
5760 /**
5761 * Returns the enabled status for this view. The interpretation of the
5762 * enabled state varies by subclass.
5763 *
5764 * @return True if this view is enabled, false otherwise.
5765 */
5766 @ViewDebug.ExportedProperty
5767 public boolean isEnabled() {
5768 return (mViewFlags & ENABLED_MASK) == ENABLED;
5769 }
5770
5771 /**
5772 * Set the enabled state of this view. The interpretation of the enabled
5773 * state varies by subclass.
5774 *
5775 * @param enabled True if this view is enabled, false otherwise.
5776 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005777 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005779 if (enabled == isEnabled()) return;
5780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5782
5783 /*
5784 * The View most likely has to change its appearance, so refresh
5785 * the drawable state.
5786 */
5787 refreshDrawableState();
5788
5789 // Invalidate too, since the default behavior for views is to be
5790 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005791 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 }
5793
5794 /**
5795 * Set whether this view can receive the focus.
5796 *
5797 * Setting this to false will also ensure that this view is not focusable
5798 * in touch mode.
5799 *
5800 * @param focusable If true, this view can receive the focus.
5801 *
5802 * @see #setFocusableInTouchMode(boolean)
5803 * @attr ref android.R.styleable#View_focusable
5804 */
5805 public void setFocusable(boolean focusable) {
5806 if (!focusable) {
5807 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5808 }
5809 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5810 }
5811
5812 /**
5813 * Set whether this view can receive focus while in touch mode.
5814 *
5815 * Setting this to true will also ensure that this view is focusable.
5816 *
5817 * @param focusableInTouchMode If true, this view can receive the focus while
5818 * in touch mode.
5819 *
5820 * @see #setFocusable(boolean)
5821 * @attr ref android.R.styleable#View_focusableInTouchMode
5822 */
5823 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5824 // Focusable in touch mode should always be set before the focusable flag
5825 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5826 // which, in touch mode, will not successfully request focus on this view
5827 // because the focusable in touch mode flag is not set
5828 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5829 if (focusableInTouchMode) {
5830 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5831 }
5832 }
5833
5834 /**
5835 * Set whether this view should have sound effects enabled for events such as
5836 * clicking and touching.
5837 *
5838 * <p>You may wish to disable sound effects for a view if you already play sounds,
5839 * for instance, a dial key that plays dtmf tones.
5840 *
5841 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5842 * @see #isSoundEffectsEnabled()
5843 * @see #playSoundEffect(int)
5844 * @attr ref android.R.styleable#View_soundEffectsEnabled
5845 */
5846 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5847 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5848 }
5849
5850 /**
5851 * @return whether this view should have sound effects enabled for events such as
5852 * clicking and touching.
5853 *
5854 * @see #setSoundEffectsEnabled(boolean)
5855 * @see #playSoundEffect(int)
5856 * @attr ref android.R.styleable#View_soundEffectsEnabled
5857 */
5858 @ViewDebug.ExportedProperty
5859 public boolean isSoundEffectsEnabled() {
5860 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5861 }
5862
5863 /**
5864 * Set whether this view should have haptic feedback for events such as
5865 * long presses.
5866 *
5867 * <p>You may wish to disable haptic feedback if your view already controls
5868 * its own haptic feedback.
5869 *
5870 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5871 * @see #isHapticFeedbackEnabled()
5872 * @see #performHapticFeedback(int)
5873 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5874 */
5875 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5876 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5877 }
5878
5879 /**
5880 * @return whether this view should have haptic feedback enabled for events
5881 * long presses.
5882 *
5883 * @see #setHapticFeedbackEnabled(boolean)
5884 * @see #performHapticFeedback(int)
5885 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5886 */
5887 @ViewDebug.ExportedProperty
5888 public boolean isHapticFeedbackEnabled() {
5889 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5890 }
5891
5892 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005893 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005894 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005895 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5896 * {@link #LAYOUT_DIRECTION_RTL},
5897 * {@link #LAYOUT_DIRECTION_INHERIT} or
5898 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005899 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005900 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005901 *
5902 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005903 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005904 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005905 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5906 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5907 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5908 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005909 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005910 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005911 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005912 }
5913
5914 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005915 * Set the layout direction for this view. This will propagate a reset of layout direction
5916 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005917 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005918 * @param layoutDirection the layout direction to set. Should be one of:
5919 *
5920 * {@link #LAYOUT_DIRECTION_LTR},
5921 * {@link #LAYOUT_DIRECTION_RTL},
5922 * {@link #LAYOUT_DIRECTION_INHERIT},
5923 * {@link #LAYOUT_DIRECTION_LOCALE}.
5924 *
5925 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5926 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5927 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005928 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005929 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005930 */
5931 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005932 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005933 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005934 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005935 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005936 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005937 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005938 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005939 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005940 // We need to resolve all RTL properties as they all depend on layout direction
5941 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005942 requestLayout();
5943 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005944 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005945 }
5946
5947 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005948 * Returns the resolved layout direction for this view.
5949 *
5950 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005951 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005952 *
5953 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5954 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -08005955 *
5956 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005957 */
5958 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005959 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5960 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005961 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005962 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005963 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5964 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005965 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005966 return LAYOUT_DIRECTION_LTR;
5967 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005968 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5969 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005970 }
5971
5972 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005973 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5974 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005975 *
5976 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005977 *
5978 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005979 */
5980 @ViewDebug.ExportedProperty(category = "layout")
5981 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005982 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005983 }
5984
5985 /**
Adam Powell539ee872012-02-03 19:00:49 -08005986 * Indicates whether the view is currently tracking transient state that the
5987 * app should not need to concern itself with saving and restoring, but that
5988 * the framework should take special note to preserve when possible.
5989 *
Adam Powell785c4472012-05-02 21:25:39 -07005990 * <p>A view with transient state cannot be trivially rebound from an external
5991 * data source, such as an adapter binding item views in a list. This may be
5992 * because the view is performing an animation, tracking user selection
5993 * of content, or similar.</p>
5994 *
Adam Powell539ee872012-02-03 19:00:49 -08005995 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005996 */
5997 @ViewDebug.ExportedProperty(category = "layout")
5998 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005999 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08006000 }
6001
6002 /**
6003 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07006004 * framework should attempt to preserve when possible. This flag is reference counted,
6005 * so every call to setHasTransientState(true) should be paired with a later call
6006 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08006007 *
Adam Powell785c4472012-05-02 21:25:39 -07006008 * <p>A view with transient state cannot be trivially rebound from an external
6009 * data source, such as an adapter binding item views in a list. This may be
6010 * because the view is performing an animation, tracking user selection
6011 * of content, or similar.</p>
6012 *
Adam Powell539ee872012-02-03 19:00:49 -08006013 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08006014 */
6015 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07006016 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
6017 mTransientStateCount - 1;
6018 if (mTransientStateCount < 0) {
6019 mTransientStateCount = 0;
6020 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
6021 "unmatched pair of setHasTransientState calls");
6022 }
6023 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07006024 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07006025 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07006026 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
6027 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07006028 if (mParent != null) {
6029 try {
6030 mParent.childHasTransientStateChanged(this, hasTransientState);
6031 } catch (AbstractMethodError e) {
6032 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
6033 " does not fully implement ViewParent", e);
6034 }
Adam Powell539ee872012-02-03 19:00:49 -08006035 }
6036 }
6037 }
6038
6039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006040 * If this view doesn't do any drawing on its own, set this flag to
6041 * allow further optimizations. By default, this flag is not set on
6042 * View, but could be set on some View subclasses such as ViewGroup.
6043 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006044 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
6045 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 *
6047 * @param willNotDraw whether or not this View draw on its own
6048 */
6049 public void setWillNotDraw(boolean willNotDraw) {
6050 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
6051 }
6052
6053 /**
6054 * Returns whether or not this View draws on its own.
6055 *
6056 * @return true if this view has nothing to draw, false otherwise
6057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006058 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 public boolean willNotDraw() {
6060 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6061 }
6062
6063 /**
6064 * When a View's drawing cache is enabled, drawing is redirected to an
6065 * offscreen bitmap. Some views, like an ImageView, must be able to
6066 * bypass this mechanism if they already draw a single bitmap, to avoid
6067 * unnecessary usage of the memory.
6068 *
6069 * @param willNotCacheDrawing true if this view does not cache its
6070 * drawing, false otherwise
6071 */
6072 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6073 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6074 }
6075
6076 /**
6077 * Returns whether or not this View can cache its drawing or not.
6078 *
6079 * @return true if this view does not cache its drawing, false otherwise
6080 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006081 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 public boolean willNotCacheDrawing() {
6083 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6084 }
6085
6086 /**
6087 * Indicates whether this view reacts to click events or not.
6088 *
6089 * @return true if the view is clickable, false otherwise
6090 *
6091 * @see #setClickable(boolean)
6092 * @attr ref android.R.styleable#View_clickable
6093 */
6094 @ViewDebug.ExportedProperty
6095 public boolean isClickable() {
6096 return (mViewFlags & CLICKABLE) == CLICKABLE;
6097 }
6098
6099 /**
6100 * Enables or disables click events for this view. When a view
6101 * is clickable it will change its state to "pressed" on every click.
6102 * Subclasses should set the view clickable to visually react to
6103 * user's clicks.
6104 *
6105 * @param clickable true to make the view clickable, false otherwise
6106 *
6107 * @see #isClickable()
6108 * @attr ref android.R.styleable#View_clickable
6109 */
6110 public void setClickable(boolean clickable) {
6111 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6112 }
6113
6114 /**
6115 * Indicates whether this view reacts to long click events or not.
6116 *
6117 * @return true if the view is long clickable, false otherwise
6118 *
6119 * @see #setLongClickable(boolean)
6120 * @attr ref android.R.styleable#View_longClickable
6121 */
6122 public boolean isLongClickable() {
6123 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6124 }
6125
6126 /**
6127 * Enables or disables long click events for this view. When a view is long
6128 * clickable it reacts to the user holding down the button for a longer
6129 * duration than a tap. This event can either launch the listener or a
6130 * context menu.
6131 *
6132 * @param longClickable true to make the view long clickable, false otherwise
6133 * @see #isLongClickable()
6134 * @attr ref android.R.styleable#View_longClickable
6135 */
6136 public void setLongClickable(boolean longClickable) {
6137 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6138 }
6139
6140 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006141 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 *
6143 * @see #isClickable()
6144 * @see #setClickable(boolean)
6145 *
6146 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6147 * the View's internal state from a previously set "pressed" state.
6148 */
6149 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006150 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006153 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006154 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006155 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006157
6158 if (needsRefresh) {
6159 refreshDrawableState();
6160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006161 dispatchSetPressed(pressed);
6162 }
6163
6164 /**
6165 * Dispatch setPressed to all of this View's children.
6166 *
6167 * @see #setPressed(boolean)
6168 *
6169 * @param pressed The new pressed state
6170 */
6171 protected void dispatchSetPressed(boolean pressed) {
6172 }
6173
6174 /**
6175 * Indicates whether the view is currently in pressed state. Unless
6176 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6177 * the pressed state.
6178 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006179 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 * @see #isClickable()
6181 * @see #setClickable(boolean)
6182 *
6183 * @return true if the view is currently pressed, false otherwise
6184 */
6185 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006186 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 }
6188
6189 /**
6190 * Indicates whether this view will save its state (that is,
6191 * whether its {@link #onSaveInstanceState} method will be called).
6192 *
6193 * @return Returns true if the view state saving is enabled, else false.
6194 *
6195 * @see #setSaveEnabled(boolean)
6196 * @attr ref android.R.styleable#View_saveEnabled
6197 */
6198 public boolean isSaveEnabled() {
6199 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6200 }
6201
6202 /**
6203 * Controls whether the saving of this view's state is
6204 * enabled (that is, whether its {@link #onSaveInstanceState} method
6205 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006206 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 * for its state to be saved. This flag can only disable the
6208 * saving of this view; any child views may still have their state saved.
6209 *
6210 * @param enabled Set to false to <em>disable</em> state saving, or true
6211 * (the default) to allow it.
6212 *
6213 * @see #isSaveEnabled()
6214 * @see #setId(int)
6215 * @see #onSaveInstanceState()
6216 * @attr ref android.R.styleable#View_saveEnabled
6217 */
6218 public void setSaveEnabled(boolean enabled) {
6219 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6220 }
6221
Jeff Brown85a31762010-09-01 17:01:00 -07006222 /**
6223 * Gets whether the framework should discard touches when the view's
6224 * window is obscured by another visible window.
6225 * Refer to the {@link View} security documentation for more details.
6226 *
6227 * @return True if touch filtering is enabled.
6228 *
6229 * @see #setFilterTouchesWhenObscured(boolean)
6230 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6231 */
6232 @ViewDebug.ExportedProperty
6233 public boolean getFilterTouchesWhenObscured() {
6234 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6235 }
6236
6237 /**
6238 * Sets whether the framework should discard touches when the view's
6239 * window is obscured by another visible window.
6240 * Refer to the {@link View} security documentation for more details.
6241 *
6242 * @param enabled True if touch filtering should be enabled.
6243 *
6244 * @see #getFilterTouchesWhenObscured
6245 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6246 */
6247 public void setFilterTouchesWhenObscured(boolean enabled) {
6248 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6249 FILTER_TOUCHES_WHEN_OBSCURED);
6250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251
6252 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006253 * Indicates whether the entire hierarchy under this view will save its
6254 * state when a state saving traversal occurs from its parent. The default
6255 * is true; if false, these views will not be saved unless
6256 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6257 *
6258 * @return Returns true if the view state saving from parent is enabled, else false.
6259 *
6260 * @see #setSaveFromParentEnabled(boolean)
6261 */
6262 public boolean isSaveFromParentEnabled() {
6263 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6264 }
6265
6266 /**
6267 * Controls whether the entire hierarchy under this view will save its
6268 * state when a state saving traversal occurs from its parent. The default
6269 * is true; if false, these views will not be saved unless
6270 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6271 *
6272 * @param enabled Set to false to <em>disable</em> state saving, or true
6273 * (the default) to allow it.
6274 *
6275 * @see #isSaveFromParentEnabled()
6276 * @see #setId(int)
6277 * @see #onSaveInstanceState()
6278 */
6279 public void setSaveFromParentEnabled(boolean enabled) {
6280 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6281 }
6282
6283
6284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 * Returns whether this View is able to take focus.
6286 *
6287 * @return True if this view can take focus, or false otherwise.
6288 * @attr ref android.R.styleable#View_focusable
6289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006290 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006291 public final boolean isFocusable() {
6292 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6293 }
6294
6295 /**
6296 * When a view is focusable, it may not want to take focus when in touch mode.
6297 * For example, a button would like focus when the user is navigating via a D-pad
6298 * so that the user can click on it, but once the user starts touching the screen,
6299 * the button shouldn't take focus
6300 * @return Whether the view is focusable in touch mode.
6301 * @attr ref android.R.styleable#View_focusableInTouchMode
6302 */
6303 @ViewDebug.ExportedProperty
6304 public final boolean isFocusableInTouchMode() {
6305 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6306 }
6307
6308 /**
6309 * Find the nearest view in the specified direction that can take focus.
6310 * This does not actually give focus to that view.
6311 *
6312 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6313 *
6314 * @return The nearest focusable in the specified direction, or null if none
6315 * can be found.
6316 */
6317 public View focusSearch(int direction) {
6318 if (mParent != null) {
6319 return mParent.focusSearch(this, direction);
6320 } else {
6321 return null;
6322 }
6323 }
6324
6325 /**
6326 * This method is the last chance for the focused view and its ancestors to
6327 * respond to an arrow key. This is called when the focused view did not
6328 * consume the key internally, nor could the view system find a new view in
6329 * the requested direction to give focus to.
6330 *
6331 * @param focused The currently focused view.
6332 * @param direction The direction focus wants to move. One of FOCUS_UP,
6333 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6334 * @return True if the this view consumed this unhandled move.
6335 */
6336 public boolean dispatchUnhandledMove(View focused, int direction) {
6337 return false;
6338 }
6339
6340 /**
6341 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006342 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006344 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6345 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 * @return The user specified next view, or null if there is none.
6347 */
6348 View findUserSetNextFocus(View root, int direction) {
6349 switch (direction) {
6350 case FOCUS_LEFT:
6351 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006352 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 case FOCUS_RIGHT:
6354 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006355 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 case FOCUS_UP:
6357 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006358 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 case FOCUS_DOWN:
6360 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006361 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006362 case FOCUS_FORWARD:
6363 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006364 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006365 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006366 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006367 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006368 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006369 @Override
6370 public boolean apply(View t) {
6371 return t.mNextFocusForwardId == id;
6372 }
6373 });
6374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 }
6376 return null;
6377 }
6378
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006379 private View findViewInsideOutShouldExist(View root, int id) {
6380 if (mMatchIdPredicate == null) {
6381 mMatchIdPredicate = new MatchIdPredicate();
6382 }
6383 mMatchIdPredicate.mId = id;
6384 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006386 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 }
6388 return result;
6389 }
6390
6391 /**
6392 * Find and return all focusable views that are descendants of this view,
6393 * possibly including this view if it is focusable itself.
6394 *
6395 * @param direction The direction of the focus
6396 * @return A list of focusable views
6397 */
6398 public ArrayList<View> getFocusables(int direction) {
6399 ArrayList<View> result = new ArrayList<View>(24);
6400 addFocusables(result, direction);
6401 return result;
6402 }
6403
6404 /**
6405 * Add any focusable views that are descendants of this view (possibly
6406 * including this view if it is focusable itself) to views. If we are in touch mode,
6407 * only add views that are also focusable in touch mode.
6408 *
6409 * @param views Focusable views found so far
6410 * @param direction The direction of the focus
6411 */
6412 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006413 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415
svetoslavganov75986cf2009-05-14 22:28:01 -07006416 /**
6417 * Adds any focusable views that are descendants of this view (possibly
6418 * including this view if it is focusable itself) to views. This method
6419 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006420 * only views focusable in touch mode if we are in touch mode or
6421 * only views that can take accessibility focus if accessibility is enabeld
6422 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006423 *
6424 * @param views Focusable views found so far or null if all we are interested is
6425 * the number of focusables.
6426 * @param direction The direction of the focus.
6427 * @param focusableMode The type of focusables to be added.
6428 *
6429 * @see #FOCUSABLES_ALL
6430 * @see #FOCUSABLES_TOUCH_MODE
6431 */
6432 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006433 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006434 return;
6435 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006436 if (!isFocusable()) {
6437 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006438 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006439 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6440 && isInTouchMode() && !isFocusableInTouchMode()) {
6441 return;
6442 }
6443 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 }
6445
6446 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006447 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006448 * The search is performed by either the text that the View renders or the content
6449 * description that describes the view for accessibility purposes and the view does
6450 * not render or both. Clients can specify how the search is to be performed via
6451 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6452 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006453 *
6454 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006455 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006456 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006457 * @see #FIND_VIEWS_WITH_TEXT
6458 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6459 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006460 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006461 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006462 if (getAccessibilityNodeProvider() != null) {
6463 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6464 outViews.add(this);
6465 }
6466 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006467 && (searched != null && searched.length() > 0)
6468 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006469 String searchedLowerCase = searched.toString().toLowerCase();
6470 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6471 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6472 outViews.add(this);
6473 }
6474 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006475 }
6476
6477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 * Find and return all touchable views that are descendants of this view,
6479 * possibly including this view if it is touchable itself.
6480 *
6481 * @return A list of touchable views
6482 */
6483 public ArrayList<View> getTouchables() {
6484 ArrayList<View> result = new ArrayList<View>();
6485 addTouchables(result);
6486 return result;
6487 }
6488
6489 /**
6490 * Add any touchable views that are descendants of this view (possibly
6491 * including this view if it is touchable itself) to views.
6492 *
6493 * @param views Touchable views found so far
6494 */
6495 public void addTouchables(ArrayList<View> views) {
6496 final int viewFlags = mViewFlags;
6497
6498 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6499 && (viewFlags & ENABLED_MASK) == ENABLED) {
6500 views.add(this);
6501 }
6502 }
6503
6504 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006505 * Returns whether this View is accessibility focused.
6506 *
6507 * @return True if this View is accessibility focused.
6508 */
6509 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006510 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006511 }
6512
6513 /**
6514 * Call this to try to give accessibility focus to this view.
6515 *
6516 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6517 * returns false or the view is no visible or the view already has accessibility
6518 * focus.
6519 *
6520 * See also {@link #focusSearch(int)}, which is what you call to say that you
6521 * have focus, and you want your parent to look for the next one.
6522 *
6523 * @return Whether this view actually took accessibility focus.
6524 *
6525 * @hide
6526 */
6527 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006528 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6529 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006530 return false;
6531 }
6532 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6533 return false;
6534 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006535 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6536 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006537 ViewRootImpl viewRootImpl = getViewRootImpl();
6538 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006539 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006540 }
6541 invalidate();
6542 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6543 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006544 return true;
6545 }
6546 return false;
6547 }
6548
6549 /**
6550 * Call this to try to clear accessibility focus of this view.
6551 *
6552 * See also {@link #focusSearch(int)}, which is what you call to say that you
6553 * have focus, and you want your parent to look for the next one.
6554 *
6555 * @hide
6556 */
6557 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006558 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6559 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006560 invalidate();
6561 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6562 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006563 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006564 // Clear the global reference of accessibility focus if this
6565 // view or any of its descendants had accessibility focus.
6566 ViewRootImpl viewRootImpl = getViewRootImpl();
6567 if (viewRootImpl != null) {
6568 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6569 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006570 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006571 }
6572 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006573 }
6574
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006575 private void sendAccessibilityHoverEvent(int eventType) {
6576 // Since we are not delivering to a client accessibility events from not
6577 // important views (unless the clinet request that) we need to fire the
6578 // event from the deepest view exposed to the client. As a consequence if
6579 // the user crosses a not exposed view the client will see enter and exit
6580 // of the exposed predecessor followed by and enter and exit of that same
6581 // predecessor when entering and exiting the not exposed descendant. This
6582 // is fine since the client has a clear idea which view is hovered at the
6583 // price of a couple more events being sent. This is a simple and
6584 // working solution.
6585 View source = this;
6586 while (true) {
6587 if (source.includeForAccessibility()) {
6588 source.sendAccessibilityEvent(eventType);
6589 return;
6590 }
6591 ViewParent parent = source.getParent();
6592 if (parent instanceof View) {
6593 source = (View) parent;
6594 } else {
6595 return;
6596 }
6597 }
6598 }
6599
Svetoslav Ganov42138042012-03-20 11:51:39 -07006600 /**
6601 * Clears accessibility focus without calling any callback methods
6602 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6603 * is used for clearing accessibility focus when giving this focus to
6604 * another view.
6605 */
6606 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006607 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6608 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006609 invalidate();
6610 }
6611 }
6612
6613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 * Call this to try to give focus to a specific view or to one of its
6615 * descendants.
6616 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006617 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6618 * false), or if it is focusable and it is not focusable in touch mode
6619 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006621 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 * have focus, and you want your parent to look for the next one.
6623 *
6624 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6625 * {@link #FOCUS_DOWN} and <code>null</code>.
6626 *
6627 * @return Whether this view or one of its descendants actually took focus.
6628 */
6629 public final boolean requestFocus() {
6630 return requestFocus(View.FOCUS_DOWN);
6631 }
6632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 /**
6634 * Call this to try to give focus to a specific view or to one of its
6635 * descendants and give it a hint about what direction focus is heading.
6636 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006637 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6638 * false), or if it is focusable and it is not focusable in touch mode
6639 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006641 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 * have focus, and you want your parent to look for the next one.
6643 *
6644 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6645 * <code>null</code> set for the previously focused rectangle.
6646 *
6647 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6648 * @return Whether this view or one of its descendants actually took focus.
6649 */
6650 public final boolean requestFocus(int direction) {
6651 return requestFocus(direction, null);
6652 }
6653
6654 /**
6655 * Call this to try to give focus to a specific view or to one of its descendants
6656 * and give it hints about the direction and a specific rectangle that the focus
6657 * is coming from. The rectangle can help give larger views a finer grained hint
6658 * about where focus is coming from, and therefore, where to show selection, or
6659 * forward focus change internally.
6660 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006661 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6662 * false), or if it is focusable and it is not focusable in touch mode
6663 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 *
6665 * A View will not take focus if it is not visible.
6666 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006667 * A View will not take focus if one of its parents has
6668 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6669 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006671 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 * have focus, and you want your parent to look for the next one.
6673 *
6674 * You may wish to override this method if your custom {@link View} has an internal
6675 * {@link View} that it wishes to forward the request to.
6676 *
6677 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6678 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6679 * to give a finer grained hint about where focus is coming from. May be null
6680 * if there is no hint.
6681 * @return Whether this view or one of its descendants actually took focus.
6682 */
6683 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006684 return requestFocusNoSearch(direction, previouslyFocusedRect);
6685 }
6686
6687 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 // need to be focusable
6689 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6690 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6691 return false;
6692 }
6693
6694 // need to be focusable in touch mode if in touch mode
6695 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006696 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6697 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 }
6699
6700 // need to not have any parents blocking us
6701 if (hasAncestorThatBlocksDescendantFocus()) {
6702 return false;
6703 }
6704
6705 handleFocusGainInternal(direction, previouslyFocusedRect);
6706 return true;
6707 }
6708
6709 /**
6710 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6711 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6712 * touch mode to request focus when they are touched.
6713 *
6714 * @return Whether this view or one of its descendants actually took focus.
6715 *
6716 * @see #isInTouchMode()
6717 *
6718 */
6719 public final boolean requestFocusFromTouch() {
6720 // Leave touch mode if we need to
6721 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006722 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006723 if (viewRoot != null) {
6724 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 }
6726 }
6727 return requestFocus(View.FOCUS_DOWN);
6728 }
6729
6730 /**
6731 * @return Whether any ancestor of this view blocks descendant focus.
6732 */
6733 private boolean hasAncestorThatBlocksDescendantFocus() {
6734 ViewParent ancestor = mParent;
6735 while (ancestor instanceof ViewGroup) {
6736 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6737 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6738 return true;
6739 } else {
6740 ancestor = vgAncestor.getParent();
6741 }
6742 }
6743 return false;
6744 }
6745
6746 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006747 * Gets the mode for determining whether this View is important for accessibility
6748 * which is if it fires accessibility events and if it is reported to
6749 * accessibility services that query the screen.
6750 *
6751 * @return The mode for determining whether a View is important for accessibility.
6752 *
6753 * @attr ref android.R.styleable#View_importantForAccessibility
6754 *
6755 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6756 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6757 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6758 */
6759 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006760 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6761 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6762 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006763 })
6764 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006765 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6766 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006767 }
6768
6769 /**
6770 * Sets how to determine whether this view is important for accessibility
6771 * which is if it fires accessibility events and if it is reported to
6772 * accessibility services that query the screen.
6773 *
6774 * @param mode How to determine whether this view is important for accessibility.
6775 *
6776 * @attr ref android.R.styleable#View_importantForAccessibility
6777 *
6778 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6779 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6780 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6781 */
6782 public void setImportantForAccessibility(int mode) {
6783 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006784 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6785 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6786 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006787 notifyAccessibilityStateChanged();
6788 }
6789 }
6790
6791 /**
6792 * Gets whether this view should be exposed for accessibility.
6793 *
6794 * @return Whether the view is exposed for accessibility.
6795 *
6796 * @hide
6797 */
6798 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006799 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6800 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006801 switch (mode) {
6802 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6803 return true;
6804 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6805 return false;
6806 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006807 return isActionableForAccessibility() || hasListenersForAccessibility()
6808 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006809 default:
6810 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6811 + mode);
6812 }
6813 }
6814
6815 /**
6816 * Gets the parent for accessibility purposes. Note that the parent for
6817 * accessibility is not necessary the immediate parent. It is the first
6818 * predecessor that is important for accessibility.
6819 *
6820 * @return The parent for accessibility purposes.
6821 */
6822 public ViewParent getParentForAccessibility() {
6823 if (mParent instanceof View) {
6824 View parentView = (View) mParent;
6825 if (parentView.includeForAccessibility()) {
6826 return mParent;
6827 } else {
6828 return mParent.getParentForAccessibility();
6829 }
6830 }
6831 return null;
6832 }
6833
6834 /**
6835 * Adds the children of a given View for accessibility. Since some Views are
6836 * not important for accessibility the children for accessibility are not
6837 * necessarily direct children of the riew, rather they are the first level of
6838 * descendants important for accessibility.
6839 *
6840 * @param children The list of children for accessibility.
6841 */
6842 public void addChildrenForAccessibility(ArrayList<View> children) {
6843 if (includeForAccessibility()) {
6844 children.add(this);
6845 }
6846 }
6847
6848 /**
6849 * Whether to regard this view for accessibility. A view is regarded for
6850 * accessibility if it is important for accessibility or the querying
6851 * accessibility service has explicitly requested that view not
6852 * important for accessibility are regarded.
6853 *
6854 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006855 *
6856 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006857 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006858 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006859 if (mAttachInfo != null) {
Svetoslav Ganov80943d82013-01-02 10:25:37 -08006860 return (mAttachInfo.mAccessibilityFetchFlags
6861 & AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0
6862 || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006863 }
6864 return false;
6865 }
6866
6867 /**
6868 * Returns whether the View is considered actionable from
6869 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006870 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006871 *
6872 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006873 *
6874 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006875 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006876 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006877 return (isClickable() || isLongClickable() || isFocusable());
6878 }
6879
6880 /**
6881 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006882 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006883 *
6884 * @return True if the view is actionable for accessibility.
6885 */
6886 private boolean hasListenersForAccessibility() {
6887 ListenerInfo info = getListenerInfo();
6888 return mTouchDelegate != null || info.mOnKeyListener != null
6889 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6890 || info.mOnHoverListener != null || info.mOnDragListener != null;
6891 }
6892
6893 /**
6894 * Notifies accessibility services that some view's important for
6895 * accessibility state has changed. Note that such notifications
6896 * are made at most once every
6897 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6898 * to avoid unnecessary load to the system. Also once a view has
6899 * made a notifucation this method is a NOP until the notification has
6900 * been sent to clients.
6901 *
6902 * @hide
6903 *
6904 * TODO: Makse sure this method is called for any view state change
6905 * that is interesting for accessilility purposes.
6906 */
6907 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006908 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6909 return;
6910 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006911 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6912 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006913 if (mParent != null) {
6914 mParent.childAccessibilityStateChanged(this);
6915 }
6916 }
6917 }
6918
6919 /**
6920 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006921 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006922 *
6923 * @hide
6924 */
6925 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006926 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006927 }
6928
6929 /**
6930 * Performs the specified accessibility action on the view. For
6931 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006932 * <p>
6933 * If an {@link AccessibilityDelegate} has been specified via calling
6934 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6935 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6936 * is responsible for handling this call.
6937 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006938 *
6939 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006940 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006941 * @return Whether the action was performed.
6942 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006943 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006944 if (mAccessibilityDelegate != null) {
6945 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6946 } else {
6947 return performAccessibilityActionInternal(action, arguments);
6948 }
6949 }
6950
6951 /**
6952 * @see #performAccessibilityAction(int, Bundle)
6953 *
6954 * Note: Called from the default {@link AccessibilityDelegate}.
6955 */
6956 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006957 switch (action) {
6958 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006959 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006960 performClick();
6961 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006962 }
6963 } break;
6964 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6965 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006966 performLongClick();
6967 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006968 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006969 } break;
6970 case AccessibilityNodeInfo.ACTION_FOCUS: {
6971 if (!hasFocus()) {
6972 // Get out of touch mode since accessibility
6973 // wants to move focus around.
6974 getViewRootImpl().ensureTouchMode(false);
6975 return requestFocus();
6976 }
6977 } break;
6978 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6979 if (hasFocus()) {
6980 clearFocus();
6981 return !isFocused();
6982 }
6983 } break;
6984 case AccessibilityNodeInfo.ACTION_SELECT: {
6985 if (!isSelected()) {
6986 setSelected(true);
6987 return isSelected();
6988 }
6989 } break;
6990 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6991 if (isSelected()) {
6992 setSelected(false);
6993 return !isSelected();
6994 }
6995 } break;
6996 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006997 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006998 return requestAccessibilityFocus();
6999 }
7000 } break;
7001 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
7002 if (isAccessibilityFocused()) {
7003 clearAccessibilityFocus();
7004 return true;
7005 }
7006 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007007 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
7008 if (arguments != null) {
7009 final int granularity = arguments.getInt(
7010 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
Svetoslav7c512842013-01-30 23:02:08 -08007011 final boolean extendSelection = arguments.getBoolean(
7012 AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN);
7013 return nextAtGranularity(granularity, extendSelection);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007014 }
7015 } break;
7016 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
7017 if (arguments != null) {
7018 final int granularity = arguments.getInt(
7019 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
Svetoslav7c512842013-01-30 23:02:08 -08007020 final boolean extendSelection = arguments.getBoolean(
7021 AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN);
7022 return previousAtGranularity(granularity, extendSelection);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007023 }
7024 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07007025 }
7026 return false;
7027 }
7028
Svetoslav7c512842013-01-30 23:02:08 -08007029 private boolean nextAtGranularity(int granularity, boolean extendSelection) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007030 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007031 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007032 return false;
7033 }
7034 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7035 if (iterator == null) {
7036 return false;
7037 }
Svetoslav7c512842013-01-30 23:02:08 -08007038 int current = getAccessibilitySelectionEnd();
7039 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7040 current = 0;
7041 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007042 final int[] range = iterator.following(current);
7043 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007044 return false;
7045 }
7046 final int start = range[0];
7047 final int end = range[1];
Svetoslav7c512842013-01-30 23:02:08 -08007048 if (extendSelection && isAccessibilitySelectionExtendable()) {
7049 int selectionStart = getAccessibilitySelectionStart();
7050 if (selectionStart == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7051 selectionStart = start;
7052 }
7053 setAccessibilitySelection(selectionStart, end);
7054 } else {
7055 setAccessibilitySelection(end, end);
7056 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007057 sendViewTextTraversedAtGranularityEvent(
7058 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
7059 granularity, start, end);
7060 return true;
7061 }
7062
Svetoslav7c512842013-01-30 23:02:08 -08007063 private boolean previousAtGranularity(int granularity, boolean extendSelection) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007064 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07007065 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007066 return false;
7067 }
7068 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
7069 if (iterator == null) {
7070 return false;
7071 }
Svetoslav7c512842013-01-30 23:02:08 -08007072 int current = getAccessibilitySelectionStart();
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007073 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7074 current = text.length();
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007075 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007076 final int[] range = iterator.preceding(current);
7077 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007078 return false;
7079 }
7080 final int start = range[0];
7081 final int end = range[1];
Svetoslav7c512842013-01-30 23:02:08 -08007082 if (extendSelection && isAccessibilitySelectionExtendable()) {
7083 int selectionEnd = getAccessibilitySelectionEnd();
7084 if (selectionEnd == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7085 selectionEnd = end;
7086 }
7087 setAccessibilitySelection(start, selectionEnd);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007088 } else {
Svetoslav7c512842013-01-30 23:02:08 -08007089 setAccessibilitySelection(start, start);
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007090 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007091 sendViewTextTraversedAtGranularityEvent(
7092 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7093 granularity, start, end);
7094 return true;
7095 }
7096
7097 /**
7098 * Gets the text reported for accessibility purposes.
7099 *
7100 * @return The accessibility text.
7101 *
7102 * @hide
7103 */
7104 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007105 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007106 }
7107
7108 /**
Svetoslav7c512842013-01-30 23:02:08 -08007109 * Gets whether accessibility selection can be extended.
7110 *
7111 * @return If selection is extensible.
7112 *
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007113 * @hide
7114 */
Svetoslav7c512842013-01-30 23:02:08 -08007115 public boolean isAccessibilitySelectionExtendable() {
7116 return false;
7117 }
7118
7119 /**
7120 * @hide
7121 */
7122 public int getAccessibilitySelectionStart() {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007123 return mAccessibilityCursorPosition;
7124 }
7125
7126 /**
7127 * @hide
7128 */
Svetoslav7c512842013-01-30 23:02:08 -08007129 public int getAccessibilitySelectionEnd() {
7130 return getAccessibilitySelectionStart();
7131 }
7132
7133 /**
7134 * @hide
7135 */
7136 public void setAccessibilitySelection(int start, int end) {
7137 if (start >= 0 && start == end && end <= getIterableTextForAccessibility().length()) {
7138 mAccessibilityCursorPosition = start;
7139 } else {
7140 mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
7141 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007142 }
7143
7144 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7145 int fromIndex, int toIndex) {
7146 if (mParent == null) {
7147 return;
7148 }
7149 AccessibilityEvent event = AccessibilityEvent.obtain(
7150 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7151 onInitializeAccessibilityEvent(event);
7152 onPopulateAccessibilityEvent(event);
7153 event.setFromIndex(fromIndex);
7154 event.setToIndex(toIndex);
7155 event.setAction(action);
7156 event.setMovementGranularity(granularity);
7157 mParent.requestSendAccessibilityEvent(this, event);
7158 }
7159
7160 /**
7161 * @hide
7162 */
7163 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7164 switch (granularity) {
7165 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7166 CharSequence text = getIterableTextForAccessibility();
7167 if (text != null && text.length() > 0) {
7168 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007169 CharacterTextSegmentIterator.getInstance(
7170 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007171 iterator.initialize(text.toString());
7172 return iterator;
7173 }
7174 } break;
7175 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7176 CharSequence text = getIterableTextForAccessibility();
7177 if (text != null && text.length() > 0) {
7178 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007179 WordTextSegmentIterator.getInstance(
7180 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007181 iterator.initialize(text.toString());
7182 return iterator;
7183 }
7184 } break;
7185 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7186 CharSequence text = getIterableTextForAccessibility();
7187 if (text != null && text.length() > 0) {
7188 ParagraphTextSegmentIterator iterator =
7189 ParagraphTextSegmentIterator.getInstance();
7190 iterator.initialize(text.toString());
7191 return iterator;
7192 }
7193 } break;
7194 }
7195 return null;
7196 }
7197
Svetoslav Ganov42138042012-03-20 11:51:39 -07007198 /**
Romain Guya440b002010-02-24 15:57:54 -08007199 * @hide
7200 */
7201 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007202 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007203 clearDisplayList();
7204
Romain Guya440b002010-02-24 15:57:54 -08007205 onStartTemporaryDetach();
7206 }
7207
7208 /**
7209 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7211 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007212 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 */
7214 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007215 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007216 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007217 }
7218
7219 /**
7220 * @hide
7221 */
7222 public void dispatchFinishTemporaryDetach() {
7223 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 }
Romain Guy8506ab42009-06-11 17:35:47 -07007225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 /**
7227 * Called after {@link #onStartTemporaryDetach} when the container is done
7228 * changing the view.
7229 */
7230 public void onFinishTemporaryDetach() {
7231 }
Romain Guy8506ab42009-06-11 17:35:47 -07007232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007234 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7235 * for this view's window. Returns null if the view is not currently attached
7236 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007237 * just use the standard high-level event callbacks like
7238 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007239 */
7240 public KeyEvent.DispatcherState getKeyDispatcherState() {
7241 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7242 }
Joe Malin32736f02011-01-19 16:14:20 -08007243
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 * Dispatch a key event before it is processed by any input method
7246 * associated with the view hierarchy. This can be used to intercept
7247 * key events in special situations before the IME consumes them; a
7248 * typical example would be handling the BACK key to update the application's
7249 * UI instead of allowing the IME to see it and close itself.
7250 *
7251 * @param event The key event to be dispatched.
7252 * @return True if the event was handled, false otherwise.
7253 */
7254 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7255 return onKeyPreIme(event.getKeyCode(), event);
7256 }
7257
7258 /**
7259 * Dispatch a key event to the next view on the focus path. This path runs
7260 * from the top of the view tree down to the currently focused view. If this
7261 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7262 * the next node down the focus path. This method also fires any key
7263 * listeners.
7264 *
7265 * @param event The key event to be dispatched.
7266 * @return True if the event was handled, false otherwise.
7267 */
7268 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007269 if (mInputEventConsistencyVerifier != null) {
7270 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272
Jeff Brown21bc5c92011-02-28 18:27:14 -08007273 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007274 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007275 ListenerInfo li = mListenerInfo;
7276 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7277 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 return true;
7279 }
7280
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007281 if (event.dispatch(this, mAttachInfo != null
7282 ? mAttachInfo.mKeyDispatchState : null, this)) {
7283 return true;
7284 }
7285
7286 if (mInputEventConsistencyVerifier != null) {
7287 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7288 }
7289 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 }
7291
7292 /**
7293 * Dispatches a key shortcut event.
7294 *
7295 * @param event The key event to be dispatched.
7296 * @return True if the event was handled by the view, false otherwise.
7297 */
7298 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7299 return onKeyShortcut(event.getKeyCode(), event);
7300 }
7301
7302 /**
7303 * Pass the touch screen motion event down to the target view, or this
7304 * view if it is the target.
7305 *
7306 * @param event The motion event to be dispatched.
7307 * @return True if the event was handled by the view, false otherwise.
7308 */
7309 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007310 if (mInputEventConsistencyVerifier != null) {
7311 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7312 }
7313
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007314 if (onFilterTouchEventForSecurity(event)) {
7315 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007316 ListenerInfo li = mListenerInfo;
7317 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7318 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007319 return true;
7320 }
7321
7322 if (onTouchEvent(event)) {
7323 return true;
7324 }
Jeff Brown85a31762010-09-01 17:01:00 -07007325 }
7326
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007327 if (mInputEventConsistencyVerifier != null) {
7328 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007330 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 }
7332
7333 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007334 * Filter the touch event to apply security policies.
7335 *
7336 * @param event The motion event to be filtered.
7337 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007338 *
Jeff Brown85a31762010-09-01 17:01:00 -07007339 * @see #getFilterTouchesWhenObscured
7340 */
7341 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007342 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007343 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7344 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7345 // Window is obscured, drop this touch.
7346 return false;
7347 }
7348 return true;
7349 }
7350
7351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007352 * Pass a trackball motion event down to the focused view.
7353 *
7354 * @param event The motion event to be dispatched.
7355 * @return True if the event was handled by the view, false otherwise.
7356 */
7357 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007358 if (mInputEventConsistencyVerifier != null) {
7359 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7360 }
7361
Romain Guy02ccac62011-06-24 13:20:23 -07007362 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363 }
7364
7365 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007366 * Dispatch a generic motion event.
7367 * <p>
7368 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7369 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007370 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007371 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007372 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007373 *
7374 * @param event The motion event to be dispatched.
7375 * @return True if the event was handled by the view, false otherwise.
7376 */
7377 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007378 if (mInputEventConsistencyVerifier != null) {
7379 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7380 }
7381
Jeff Browna032cc02011-03-07 16:56:21 -08007382 final int source = event.getSource();
7383 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7384 final int action = event.getAction();
7385 if (action == MotionEvent.ACTION_HOVER_ENTER
7386 || action == MotionEvent.ACTION_HOVER_MOVE
7387 || action == MotionEvent.ACTION_HOVER_EXIT) {
7388 if (dispatchHoverEvent(event)) {
7389 return true;
7390 }
7391 } else if (dispatchGenericPointerEvent(event)) {
7392 return true;
7393 }
7394 } else if (dispatchGenericFocusedEvent(event)) {
7395 return true;
7396 }
7397
Jeff Brown10b62902011-06-20 16:40:37 -07007398 if (dispatchGenericMotionEventInternal(event)) {
7399 return true;
7400 }
7401
7402 if (mInputEventConsistencyVerifier != null) {
7403 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7404 }
7405 return false;
7406 }
7407
7408 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007409 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007410 ListenerInfo li = mListenerInfo;
7411 if (li != null && li.mOnGenericMotionListener != null
7412 && (mViewFlags & ENABLED_MASK) == ENABLED
7413 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007414 return true;
7415 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007416
7417 if (onGenericMotionEvent(event)) {
7418 return true;
7419 }
7420
7421 if (mInputEventConsistencyVerifier != null) {
7422 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7423 }
7424 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007425 }
7426
7427 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007428 * Dispatch a hover event.
7429 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007430 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007431 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007432 * </p>
7433 *
7434 * @param event The motion event to be dispatched.
7435 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007436 */
7437 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007438 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007439 ListenerInfo li = mListenerInfo;
7440 if (li != null && li.mOnHoverListener != null
7441 && (mViewFlags & ENABLED_MASK) == ENABLED
7442 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007443 return true;
7444 }
7445
Jeff Browna032cc02011-03-07 16:56:21 -08007446 return onHoverEvent(event);
7447 }
7448
7449 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007450 * Returns true if the view has a child to which it has recently sent
7451 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7452 * it does not have a hovered child, then it must be the innermost hovered view.
7453 * @hide
7454 */
7455 protected boolean hasHoveredChild() {
7456 return false;
7457 }
7458
7459 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007460 * Dispatch a generic motion event to the view under the first pointer.
7461 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007462 * Do not call this method directly.
7463 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007464 * </p>
7465 *
7466 * @param event The motion event to be dispatched.
7467 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007468 */
7469 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7470 return false;
7471 }
7472
7473 /**
7474 * Dispatch a generic motion event to the currently focused view.
7475 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007476 * Do not call this method directly.
7477 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007478 * </p>
7479 *
7480 * @param event The motion event to be dispatched.
7481 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007482 */
7483 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7484 return false;
7485 }
7486
7487 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007488 * Dispatch a pointer event.
7489 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007490 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7491 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7492 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007493 * and should not be expected to handle other pointing device features.
7494 * </p>
7495 *
7496 * @param event The motion event to be dispatched.
7497 * @return True if the event was handled by the view, false otherwise.
7498 * @hide
7499 */
7500 public final boolean dispatchPointerEvent(MotionEvent event) {
7501 if (event.isTouchEvent()) {
7502 return dispatchTouchEvent(event);
7503 } else {
7504 return dispatchGenericMotionEvent(event);
7505 }
7506 }
7507
7508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 * Called when the window containing this view gains or loses window focus.
7510 * ViewGroups should override to route to their children.
7511 *
7512 * @param hasFocus True if the window containing this view now has focus,
7513 * false otherwise.
7514 */
7515 public void dispatchWindowFocusChanged(boolean hasFocus) {
7516 onWindowFocusChanged(hasFocus);
7517 }
7518
7519 /**
7520 * Called when the window containing this view gains or loses focus. Note
7521 * that this is separate from view focus: to receive key events, both
7522 * your view and its window must have focus. If a window is displayed
7523 * on top of yours that takes input focus, then your own window will lose
7524 * focus but the view focus will remain unchanged.
7525 *
7526 * @param hasWindowFocus True if the window containing this view now has
7527 * focus, false otherwise.
7528 */
7529 public void onWindowFocusChanged(boolean hasWindowFocus) {
7530 InputMethodManager imm = InputMethodManager.peekInstance();
7531 if (!hasWindowFocus) {
7532 if (isPressed()) {
7533 setPressed(false);
7534 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007535 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007536 imm.focusOut(this);
7537 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007538 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007539 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007540 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007541 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 imm.focusIn(this);
7543 }
7544 refreshDrawableState();
7545 }
7546
7547 /**
7548 * Returns true if this view is in a window that currently has window focus.
7549 * Note that this is not the same as the view itself having focus.
7550 *
7551 * @return True if this view is in a window that currently has window focus.
7552 */
7553 public boolean hasWindowFocus() {
7554 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7555 }
7556
7557 /**
Adam Powell326d8082009-12-09 15:10:07 -08007558 * Dispatch a view visibility change down the view hierarchy.
7559 * ViewGroups should override to route to their children.
7560 * @param changedView The view whose visibility changed. Could be 'this' or
7561 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007562 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7563 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007564 */
7565 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7566 onVisibilityChanged(changedView, visibility);
7567 }
7568
7569 /**
7570 * Called when the visibility of the view or an ancestor of the view is changed.
7571 * @param changedView The view whose visibility changed. Could be 'this' or
7572 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007573 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7574 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007575 */
7576 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007577 if (visibility == VISIBLE) {
7578 if (mAttachInfo != null) {
7579 initialAwakenScrollBars();
7580 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007581 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007582 }
7583 }
Adam Powell326d8082009-12-09 15:10:07 -08007584 }
7585
7586 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007587 * Dispatch a hint about whether this view is displayed. For instance, when
7588 * a View moves out of the screen, it might receives a display hint indicating
7589 * the view is not displayed. Applications should not <em>rely</em> on this hint
7590 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007591 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007592 * @param hint A hint about whether or not this view is displayed:
7593 * {@link #VISIBLE} or {@link #INVISIBLE}.
7594 */
7595 public void dispatchDisplayHint(int hint) {
7596 onDisplayHint(hint);
7597 }
7598
7599 /**
7600 * Gives this view a hint about whether is displayed or not. For instance, when
7601 * a View moves out of the screen, it might receives a display hint indicating
7602 * the view is not displayed. Applications should not <em>rely</em> on this hint
7603 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007604 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007605 * @param hint A hint about whether or not this view is displayed:
7606 * {@link #VISIBLE} or {@link #INVISIBLE}.
7607 */
7608 protected void onDisplayHint(int hint) {
7609 }
7610
7611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 * Dispatch a window visibility change down the view hierarchy.
7613 * ViewGroups should override to route to their children.
7614 *
7615 * @param visibility The new visibility of the window.
7616 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007617 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 */
7619 public void dispatchWindowVisibilityChanged(int visibility) {
7620 onWindowVisibilityChanged(visibility);
7621 }
7622
7623 /**
7624 * Called when the window containing has change its visibility
7625 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7626 * that this tells you whether or not your window is being made visible
7627 * to the window manager; this does <em>not</em> tell you whether or not
7628 * your window is obscured by other windows on the screen, even if it
7629 * is itself visible.
7630 *
7631 * @param visibility The new visibility of the window.
7632 */
7633 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007634 if (visibility == VISIBLE) {
7635 initialAwakenScrollBars();
7636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 }
7638
7639 /**
7640 * Returns the current visibility of the window this view is attached to
7641 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7642 *
7643 * @return Returns the current visibility of the view's window.
7644 */
7645 public int getWindowVisibility() {
7646 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7647 }
7648
7649 /**
7650 * Retrieve the overall visible display size in which the window this view is
7651 * attached to has been positioned in. This takes into account screen
7652 * decorations above the window, for both cases where the window itself
7653 * is being position inside of them or the window is being placed under
7654 * then and covered insets are used for the window to position its content
7655 * inside. In effect, this tells you the available area where content can
7656 * be placed and remain visible to users.
7657 *
7658 * <p>This function requires an IPC back to the window manager to retrieve
7659 * the requested information, so should not be used in performance critical
7660 * code like drawing.
7661 *
7662 * @param outRect Filled in with the visible display frame. If the view
7663 * is not attached to a window, this is simply the raw display size.
7664 */
7665 public void getWindowVisibleDisplayFrame(Rect outRect) {
7666 if (mAttachInfo != null) {
7667 try {
7668 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7669 } catch (RemoteException e) {
7670 return;
7671 }
7672 // XXX This is really broken, and probably all needs to be done
7673 // in the window manager, and we need to know more about whether
7674 // we want the area behind or in front of the IME.
7675 final Rect insets = mAttachInfo.mVisibleInsets;
7676 outRect.left += insets.left;
7677 outRect.top += insets.top;
7678 outRect.right -= insets.right;
7679 outRect.bottom -= insets.bottom;
7680 return;
7681 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007682 // The view is not attached to a display so we don't have a context.
7683 // Make a best guess about the display size.
7684 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007685 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 }
7687
7688 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007689 * Dispatch a notification about a resource configuration change down
7690 * the view hierarchy.
7691 * ViewGroups should override to route to their children.
7692 *
7693 * @param newConfig The new resource configuration.
7694 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007695 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007696 */
7697 public void dispatchConfigurationChanged(Configuration newConfig) {
7698 onConfigurationChanged(newConfig);
7699 }
7700
7701 /**
7702 * Called when the current configuration of the resources being used
7703 * by the application have changed. You can use this to decide when
7704 * to reload resources that can changed based on orientation and other
7705 * configuration characterstics. You only need to use this if you are
7706 * not relying on the normal {@link android.app.Activity} mechanism of
7707 * recreating the activity instance upon a configuration change.
7708 *
7709 * @param newConfig The new resource configuration.
7710 */
7711 protected void onConfigurationChanged(Configuration newConfig) {
7712 }
7713
7714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 * Private function to aggregate all per-view attributes in to the view
7716 * root.
7717 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007718 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7719 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 }
7721
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007722 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7723 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007724 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007725 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007726 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007727 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007728 ListenerInfo li = mListenerInfo;
7729 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007730 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 }
7733 }
7734
7735 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007736 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007737 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007738 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7739 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 ai.mRecomputeGlobalAttributes = true;
7741 }
7742 }
7743 }
7744
7745 /**
7746 * Returns whether the device is currently in touch mode. Touch mode is entered
7747 * once the user begins interacting with the device by touch, and affects various
7748 * things like whether focus is always visible to the user.
7749 *
7750 * @return Whether the device is in touch mode.
7751 */
7752 @ViewDebug.ExportedProperty
7753 public boolean isInTouchMode() {
7754 if (mAttachInfo != null) {
7755 return mAttachInfo.mInTouchMode;
7756 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007757 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 }
7759 }
7760
7761 /**
7762 * Returns the context the view is running in, through which it can
7763 * access the current theme, resources, etc.
7764 *
7765 * @return The view's Context.
7766 */
7767 @ViewDebug.CapturedViewProperty
7768 public final Context getContext() {
7769 return mContext;
7770 }
7771
7772 /**
7773 * Handle a key event before it is processed by any input method
7774 * associated with the view hierarchy. This can be used to intercept
7775 * key events in special situations before the IME consumes them; a
7776 * typical example would be handling the BACK key to update the application's
7777 * UI instead of allowing the IME to see it and close itself.
7778 *
7779 * @param keyCode The value in event.getKeyCode().
7780 * @param event Description of the key event.
7781 * @return If you handled the event, return true. If you want to allow the
7782 * event to be handled by the next receiver, return false.
7783 */
7784 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7785 return false;
7786 }
7787
7788 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007789 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7790 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7792 * is released, if the view is enabled and clickable.
7793 *
Jean Chalard405bc512012-05-29 19:12:34 +09007794 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7795 * although some may elect to do so in some situations. Do not rely on this to
7796 * catch software key presses.
7797 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 * @param keyCode A key code that represents the button pressed, from
7799 * {@link android.view.KeyEvent}.
7800 * @param event The KeyEvent object that defines the button action.
7801 */
7802 public boolean onKeyDown(int keyCode, KeyEvent event) {
7803 boolean result = false;
7804
7805 switch (keyCode) {
7806 case KeyEvent.KEYCODE_DPAD_CENTER:
7807 case KeyEvent.KEYCODE_ENTER: {
7808 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7809 return true;
7810 }
7811 // Long clickable items don't necessarily have to be clickable
7812 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7813 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7814 (event.getRepeatCount() == 0)) {
7815 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007816 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 return true;
7818 }
7819 break;
7820 }
7821 }
7822 return result;
7823 }
7824
7825 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007826 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7827 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7828 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007829 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7830 * although some may elect to do so in some situations. Do not rely on this to
7831 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007832 */
7833 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7834 return false;
7835 }
7836
7837 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007838 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7839 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7841 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007842 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7843 * although some may elect to do so in some situations. Do not rely on this to
7844 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 *
7846 * @param keyCode A key code that represents the button pressed, from
7847 * {@link android.view.KeyEvent}.
7848 * @param event The KeyEvent object that defines the button action.
7849 */
7850 public boolean onKeyUp(int keyCode, KeyEvent event) {
7851 boolean result = false;
7852
7853 switch (keyCode) {
7854 case KeyEvent.KEYCODE_DPAD_CENTER:
7855 case KeyEvent.KEYCODE_ENTER: {
7856 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7857 return true;
7858 }
7859 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7860 setPressed(false);
7861
7862 if (!mHasPerformedLongPress) {
7863 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007864 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865
7866 result = performClick();
7867 }
7868 }
7869 break;
7870 }
7871 }
7872 return result;
7873 }
7874
7875 /**
7876 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7877 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7878 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007879 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7880 * although some may elect to do so in some situations. Do not rely on this to
7881 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 *
7883 * @param keyCode A key code that represents the button pressed, from
7884 * {@link android.view.KeyEvent}.
7885 * @param repeatCount The number of times the action was made.
7886 * @param event The KeyEvent object that defines the button action.
7887 */
7888 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7889 return false;
7890 }
7891
7892 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007893 * Called on the focused view when a key shortcut event is not handled.
7894 * Override this method to implement local key shortcuts for the View.
7895 * Key shortcuts can also be implemented by setting the
7896 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 *
7898 * @param keyCode The value in event.getKeyCode().
7899 * @param event Description of the key event.
7900 * @return If you handled the event, return true. If you want to allow the
7901 * event to be handled by the next receiver, return false.
7902 */
7903 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7904 return false;
7905 }
7906
7907 /**
7908 * Check whether the called view is a text editor, in which case it
7909 * would make sense to automatically display a soft input window for
7910 * it. Subclasses should override this if they implement
7911 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007912 * a call on that method would return a non-null InputConnection, and
7913 * they are really a first-class editor that the user would normally
7914 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007915 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007916 * <p>The default implementation always returns false. This does
7917 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7918 * will not be called or the user can not otherwise perform edits on your
7919 * view; it is just a hint to the system that this is not the primary
7920 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 * @return Returns true if this view is a text editor, else false.
7923 */
7924 public boolean onCheckIsTextEditor() {
7925 return false;
7926 }
Romain Guy8506ab42009-06-11 17:35:47 -07007927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007928 /**
7929 * Create a new InputConnection for an InputMethod to interact
7930 * with the view. The default implementation returns null, since it doesn't
7931 * support input methods. You can override this to implement such support.
7932 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007934 * <p>When implementing this, you probably also want to implement
7935 * {@link #onCheckIsTextEditor()} to indicate you will return a
7936 * non-null InputConnection.
7937 *
7938 * @param outAttrs Fill in with attribute information about the connection.
7939 */
7940 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7941 return null;
7942 }
7943
7944 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007945 * Called by the {@link android.view.inputmethod.InputMethodManager}
7946 * when a view who is not the current
7947 * input connection target is trying to make a call on the manager. The
7948 * default implementation returns false; you can override this to return
7949 * true for certain views if you are performing InputConnection proxying
7950 * to them.
7951 * @param view The View that is making the InputMethodManager call.
7952 * @return Return true to allow the call, false to reject.
7953 */
7954 public boolean checkInputConnectionProxy(View view) {
7955 return false;
7956 }
Romain Guy8506ab42009-06-11 17:35:47 -07007957
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007958 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 * Show the context menu for this view. It is not safe to hold on to the
7960 * menu after returning from this method.
7961 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007962 * You should normally not overload this method. Overload
7963 * {@link #onCreateContextMenu(ContextMenu)} or define an
7964 * {@link OnCreateContextMenuListener} to add items to the context menu.
7965 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007966 * @param menu The context menu to populate
7967 */
7968 public void createContextMenu(ContextMenu menu) {
7969 ContextMenuInfo menuInfo = getContextMenuInfo();
7970
7971 // Sets the current menu info so all items added to menu will have
7972 // my extra info set.
7973 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7974
7975 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007976 ListenerInfo li = mListenerInfo;
7977 if (li != null && li.mOnCreateContextMenuListener != null) {
7978 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007979 }
7980
7981 // Clear the extra information so subsequent items that aren't mine don't
7982 // have my extra info.
7983 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7984
7985 if (mParent != null) {
7986 mParent.createContextMenu(menu);
7987 }
7988 }
7989
7990 /**
7991 * Views should implement this if they have extra information to associate
7992 * with the context menu. The return result is supplied as a parameter to
7993 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7994 * callback.
7995 *
7996 * @return Extra information about the item for which the context menu
7997 * should be shown. This information will vary across different
7998 * subclasses of View.
7999 */
8000 protected ContextMenuInfo getContextMenuInfo() {
8001 return null;
8002 }
8003
8004 /**
8005 * Views should implement this if the view itself is going to add items to
8006 * the context menu.
8007 *
8008 * @param menu the context menu to populate
8009 */
8010 protected void onCreateContextMenu(ContextMenu menu) {
8011 }
8012
8013 /**
8014 * Implement this method to handle trackball motion events. The
8015 * <em>relative</em> movement of the trackball since the last event
8016 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
8017 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
8018 * that a movement of 1 corresponds to the user pressing one DPAD key (so
8019 * they will often be fractional values, representing the more fine-grained
8020 * movement information available from a trackball).
8021 *
8022 * @param event The motion event.
8023 * @return True if the event was handled, false otherwise.
8024 */
8025 public boolean onTrackballEvent(MotionEvent event) {
8026 return false;
8027 }
8028
8029 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08008030 * Implement this method to handle generic motion events.
8031 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08008032 * Generic motion events describe joystick movements, mouse hovers, track pad
8033 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08008034 * {@link MotionEvent#getSource() source} of the motion event specifies
8035 * the class of input that was received. Implementations of this method
8036 * must examine the bits in the source before processing the event.
8037 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08008038 * </p><p>
8039 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
8040 * are delivered to the view under the pointer. All other generic motion events are
8041 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08008042 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07008043 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08008044 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08008045 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
8046 * // process the joystick movement...
8047 * return true;
8048 * }
8049 * }
8050 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
8051 * switch (event.getAction()) {
8052 * case MotionEvent.ACTION_HOVER_MOVE:
8053 * // process the mouse hover movement...
8054 * return true;
8055 * case MotionEvent.ACTION_SCROLL:
8056 * // process the scroll wheel movement...
8057 * return true;
8058 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08008059 * }
8060 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07008061 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08008062 *
8063 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08008064 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08008065 */
8066 public boolean onGenericMotionEvent(MotionEvent event) {
8067 return false;
8068 }
8069
8070 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008071 * Implement this method to handle hover events.
8072 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07008073 * This method is called whenever a pointer is hovering into, over, or out of the
8074 * bounds of a view and the view is not currently being touched.
8075 * Hover events are represented as pointer events with action
8076 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
8077 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
8078 * </p>
8079 * <ul>
8080 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
8081 * when the pointer enters the bounds of the view.</li>
8082 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
8083 * when the pointer has already entered the bounds of the view and has moved.</li>
8084 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
8085 * when the pointer has exited the bounds of the view or when the pointer is
8086 * about to go down due to a button click, tap, or similar user action that
8087 * causes the view to be touched.</li>
8088 * </ul>
8089 * <p>
8090 * The view should implement this method to return true to indicate that it is
8091 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08008092 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07008093 * The default implementation calls {@link #setHovered} to update the hovered state
8094 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008095 * is enabled and is clickable. The default implementation also sends hover
8096 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008097 * </p>
8098 *
8099 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008100 * @return True if the view handled the hover event.
8101 *
8102 * @see #isHovered
8103 * @see #setHovered
8104 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008105 */
8106 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008107 // The root view may receive hover (or touch) events that are outside the bounds of
8108 // the window. This code ensures that we only send accessibility events for
8109 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008110 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008111 if (!mSendingHoverAccessibilityEvents) {
8112 if ((action == MotionEvent.ACTION_HOVER_ENTER
8113 || action == MotionEvent.ACTION_HOVER_MOVE)
8114 && !hasHoveredChild()
8115 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008116 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008117 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008118 }
8119 } else {
8120 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008121 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008122 && !pointInView(event.getX(), event.getY()))) {
8123 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008124 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008125 // If the window does not have input focus we take away accessibility
8126 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008127 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008128 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008129 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008130 }
Jeff Browna1b24182011-07-28 13:38:24 -07008131 }
8132
Jeff Brown87b7f802011-06-21 18:35:45 -07008133 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008134 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008135 case MotionEvent.ACTION_HOVER_ENTER:
8136 setHovered(true);
8137 break;
8138 case MotionEvent.ACTION_HOVER_EXIT:
8139 setHovered(false);
8140 break;
8141 }
Jeff Browna1b24182011-07-28 13:38:24 -07008142
8143 // Dispatch the event to onGenericMotionEvent before returning true.
8144 // This is to provide compatibility with existing applications that
8145 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8146 // break because of the new default handling for hoverable views
8147 // in onHoverEvent.
8148 // Note that onGenericMotionEvent will be called by default when
8149 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8150 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008151 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008152 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008153
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008154 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008155 }
8156
8157 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008158 * Returns true if the view should handle {@link #onHoverEvent}
8159 * by calling {@link #setHovered} to change its hovered state.
8160 *
8161 * @return True if the view is hoverable.
8162 */
8163 private boolean isHoverable() {
8164 final int viewFlags = mViewFlags;
8165 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8166 return false;
8167 }
8168
8169 return (viewFlags & CLICKABLE) == CLICKABLE
8170 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8171 }
8172
8173 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008174 * Returns true if the view is currently hovered.
8175 *
8176 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008177 *
8178 * @see #setHovered
8179 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008180 */
Jeff Brown10b62902011-06-20 16:40:37 -07008181 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008182 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008183 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008184 }
8185
8186 /**
8187 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008188 * <p>
8189 * Calling this method also changes the drawable state of the view. This
8190 * enables the view to react to hover by using different drawable resources
8191 * to change its appearance.
8192 * </p><p>
8193 * The {@link #onHoverChanged} method is called when the hovered state changes.
8194 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008195 *
8196 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008197 *
8198 * @see #isHovered
8199 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008200 */
8201 public void setHovered(boolean hovered) {
8202 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008203 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8204 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008205 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008206 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008207 }
8208 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008209 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8210 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008211 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008212 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008213 }
8214 }
8215 }
8216
8217 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008218 * Implement this method to handle hover state changes.
8219 * <p>
8220 * This method is called whenever the hover state changes as a result of a
8221 * call to {@link #setHovered}.
8222 * </p>
8223 *
8224 * @param hovered The current hover state, as returned by {@link #isHovered}.
8225 *
8226 * @see #isHovered
8227 * @see #setHovered
8228 */
8229 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008230 }
8231
8232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 * Implement this method to handle touch screen motion events.
8234 *
8235 * @param event The motion event.
8236 * @return True if the event was handled, false otherwise.
8237 */
8238 public boolean onTouchEvent(MotionEvent event) {
8239 final int viewFlags = mViewFlags;
8240
8241 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008242 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008243 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 // A disabled view that is clickable still consumes the touch
8246 // events, it just doesn't respond to them.
8247 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8248 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8249 }
8250
8251 if (mTouchDelegate != null) {
8252 if (mTouchDelegate.onTouchEvent(event)) {
8253 return true;
8254 }
8255 }
8256
8257 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8258 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8259 switch (event.getAction()) {
8260 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008261 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8262 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 // take focus if we don't have it already and we should in
8264 // touch mode.
8265 boolean focusTaken = false;
8266 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8267 focusTaken = requestFocus();
8268 }
8269
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008270 if (prepressed) {
8271 // The button is being released before we actually
8272 // showed it as pressed. Make it show the pressed
8273 // state now (before scheduling the click) to ensure
8274 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008275 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008276 }
Joe Malin32736f02011-01-19 16:14:20 -08008277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 if (!mHasPerformedLongPress) {
8279 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008280 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281
8282 // Only perform take click actions if we were in the pressed state
8283 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008284 // Use a Runnable and post this rather than calling
8285 // performClick directly. This lets other visual state
8286 // of the view update before click actions start.
8287 if (mPerformClick == null) {
8288 mPerformClick = new PerformClick();
8289 }
8290 if (!post(mPerformClick)) {
8291 performClick();
8292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 }
8294 }
8295
8296 if (mUnsetPressedState == null) {
8297 mUnsetPressedState = new UnsetPressedState();
8298 }
8299
Adam Powelle14579b2009-12-16 18:39:52 -08008300 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008301 postDelayed(mUnsetPressedState,
8302 ViewConfiguration.getPressedStateDuration());
8303 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008304 // If the post failed, unpress right now
8305 mUnsetPressedState.run();
8306 }
Adam Powelle14579b2009-12-16 18:39:52 -08008307 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 }
8309 break;
8310
8311 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008312 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008313
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008314 if (performButtonActionOnTouchDown(event)) {
8315 break;
8316 }
8317
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008318 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008319 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008320
8321 // For views inside a scrolling container, delay the pressed feedback for
8322 // a short period in case this is a scroll.
8323 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008324 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008325 if (mPendingCheckForTap == null) {
8326 mPendingCheckForTap = new CheckForTap();
8327 }
8328 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8329 } else {
8330 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008331 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008332 checkForLongClick(0);
8333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334 break;
8335
8336 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008337 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008338 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008339 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 break;
8341
8342 case MotionEvent.ACTION_MOVE:
8343 final int x = (int) event.getX();
8344 final int y = (int) event.getY();
8345
8346 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008347 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008349 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008350 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008351 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008352 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353
Adam Powell4d6f0662012-02-21 15:11:11 -08008354 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008356 }
8357 break;
8358 }
8359 return true;
8360 }
8361
8362 return false;
8363 }
8364
8365 /**
Adam Powell10298662011-08-14 18:26:30 -07008366 * @hide
8367 */
8368 public boolean isInScrollingContainer() {
8369 ViewParent p = getParent();
8370 while (p != null && p instanceof ViewGroup) {
8371 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8372 return true;
8373 }
8374 p = p.getParent();
8375 }
8376 return false;
8377 }
8378
8379 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008380 * Remove the longpress detection timer.
8381 */
8382 private void removeLongPressCallback() {
8383 if (mPendingCheckForLongPress != null) {
8384 removeCallbacks(mPendingCheckForLongPress);
8385 }
8386 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008387
8388 /**
8389 * Remove the pending click action
8390 */
8391 private void removePerformClickCallback() {
8392 if (mPerformClick != null) {
8393 removeCallbacks(mPerformClick);
8394 }
8395 }
8396
Adam Powelle14579b2009-12-16 18:39:52 -08008397 /**
Romain Guya440b002010-02-24 15:57:54 -08008398 * Remove the prepress detection timer.
8399 */
8400 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008401 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008402 setPressed(false);
8403 removeCallbacks(mUnsetPressedState);
8404 }
8405 }
8406
8407 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008408 * Remove the tap detection timer.
8409 */
8410 private void removeTapCallback() {
8411 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008412 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008413 removeCallbacks(mPendingCheckForTap);
8414 }
8415 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008416
8417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 * Cancels a pending long press. Your subclass can use this if you
8419 * want the context menu to come up if the user presses and holds
8420 * at the same place, but you don't want it to come up if they press
8421 * and then move around enough to cause scrolling.
8422 */
8423 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008424 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008425
8426 /*
8427 * The prepressed state handled by the tap callback is a display
8428 * construct, but the tap callback will post a long press callback
8429 * less its own timeout. Remove it here.
8430 */
8431 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008432 }
8433
8434 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008435 * Remove the pending callback for sending a
8436 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8437 */
8438 private void removeSendViewScrolledAccessibilityEventCallback() {
8439 if (mSendViewScrolledAccessibilityEvent != null) {
8440 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008441 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008442 }
8443 }
8444
8445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 * Sets the TouchDelegate for this View.
8447 */
8448 public void setTouchDelegate(TouchDelegate delegate) {
8449 mTouchDelegate = delegate;
8450 }
8451
8452 /**
8453 * Gets the TouchDelegate for this View.
8454 */
8455 public TouchDelegate getTouchDelegate() {
8456 return mTouchDelegate;
8457 }
8458
8459 /**
8460 * Set flags controlling behavior of this view.
8461 *
8462 * @param flags Constant indicating the value which should be set
8463 * @param mask Constant indicating the bit range that should be changed
8464 */
8465 void setFlags(int flags, int mask) {
8466 int old = mViewFlags;
8467 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8468
8469 int changed = mViewFlags ^ old;
8470 if (changed == 0) {
8471 return;
8472 }
8473 int privateFlags = mPrivateFlags;
8474
8475 /* Check if the FOCUSABLE bit has changed */
8476 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008477 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008479 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 /* Give up focus if we are no longer focusable */
8481 clearFocus();
8482 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008483 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 /*
8485 * Tell the view system that we are now available to take focus
8486 * if no one else already has it.
8487 */
8488 if (mParent != null) mParent.focusableViewAvailable(this);
8489 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008490 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8491 notifyAccessibilityStateChanged();
8492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 }
8494
8495 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8496 if ((changed & VISIBILITY_MASK) != 0) {
8497 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008498 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008499 * it was not visible. Marking it drawn ensures that the invalidation will
8500 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008501 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008502 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008503 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504
8505 needGlobalAttributesUpdate(true);
8506
8507 // a view becoming visible is worth notifying the parent
8508 // about in case nothing has focus. even if this specific view
8509 // isn't focusable, it may contain something that is, so let
8510 // the root view try to give this focus if nothing else does.
8511 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8512 mParent.focusableViewAvailable(this);
8513 }
8514 }
8515 }
8516
8517 /* Check if the GONE bit has changed */
8518 if ((changed & GONE) != 0) {
8519 needGlobalAttributesUpdate(false);
8520 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521
Romain Guyecd80ee2009-12-03 17:13:02 -08008522 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8523 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008524 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008525 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008526 if (mParent instanceof View) {
8527 // GONE views noop invalidation, so invalidate the parent
8528 ((View) mParent).invalidate(true);
8529 }
8530 // Mark the view drawn to ensure that it gets invalidated properly the next
8531 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008532 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 }
8534 if (mAttachInfo != null) {
8535 mAttachInfo.mViewVisibilityChanged = true;
8536 }
8537 }
8538
8539 /* Check if the VISIBLE bit has changed */
8540 if ((changed & INVISIBLE) != 0) {
8541 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008542 /*
8543 * If this view is becoming invisible, set the DRAWN flag so that
8544 * the next invalidate() will not be skipped.
8545 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008546 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008547
8548 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008549 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 if (getRootView() != this) {
8551 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008552 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 }
8554 }
8555 if (mAttachInfo != null) {
8556 mAttachInfo.mViewVisibilityChanged = true;
8557 }
8558 }
8559
Adam Powell326d8082009-12-09 15:10:07 -08008560 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008561 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008562 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8563 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008564 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008565 } else if (mParent != null) {
8566 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008567 }
Adam Powell326d8082009-12-09 15:10:07 -08008568 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8569 }
8570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8572 destroyDrawingCache();
8573 }
8574
8575 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8576 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008577 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008578 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580
8581 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8582 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008583 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 }
8585
8586 if ((changed & DRAW_MASK) != 0) {
8587 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008588 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008589 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8590 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008591 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008592 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008593 }
8594 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008595 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 }
8597 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008598 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 }
8600
8601 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008602 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 mParent.recomputeViewAttributes(this);
8604 }
8605 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008606
8607 if (AccessibilityManager.getInstance(mContext).isEnabled()
8608 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8609 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8610 notifyAccessibilityStateChanged();
8611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 }
8613
8614 /**
8615 * Change the view's z order in the tree, so it's on top of other sibling
8616 * views
8617 */
8618 public void bringToFront() {
8619 if (mParent != null) {
8620 mParent.bringChildToFront(this);
8621 }
8622 }
8623
8624 /**
8625 * This is called in response to an internal scroll in this view (i.e., the
8626 * view scrolled its own contents). This is typically as a result of
8627 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8628 * called.
8629 *
8630 * @param l Current horizontal scroll origin.
8631 * @param t Current vertical scroll origin.
8632 * @param oldl Previous horizontal scroll origin.
8633 * @param oldt Previous vertical scroll origin.
8634 */
8635 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008636 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8637 postSendViewScrolledAccessibilityEventCallback();
8638 }
8639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 mBackgroundSizeChanged = true;
8641
8642 final AttachInfo ai = mAttachInfo;
8643 if (ai != null) {
8644 ai.mViewScrollChanged = true;
8645 }
8646 }
8647
8648 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008649 * Interface definition for a callback to be invoked when the layout bounds of a view
8650 * changes due to layout processing.
8651 */
8652 public interface OnLayoutChangeListener {
8653 /**
8654 * Called when the focus state of a view has changed.
8655 *
8656 * @param v The view whose state has changed.
8657 * @param left The new value of the view's left property.
8658 * @param top The new value of the view's top property.
8659 * @param right The new value of the view's right property.
8660 * @param bottom The new value of the view's bottom property.
8661 * @param oldLeft The previous value of the view's left property.
8662 * @param oldTop The previous value of the view's top property.
8663 * @param oldRight The previous value of the view's right property.
8664 * @param oldBottom The previous value of the view's bottom property.
8665 */
8666 void onLayoutChange(View v, int left, int top, int right, int bottom,
8667 int oldLeft, int oldTop, int oldRight, int oldBottom);
8668 }
8669
8670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 * This is called during layout when the size of this view has changed. If
8672 * you were just added to the view hierarchy, you're called with the old
8673 * values of 0.
8674 *
8675 * @param w Current width of this view.
8676 * @param h Current height of this view.
8677 * @param oldw Old width of this view.
8678 * @param oldh Old height of this view.
8679 */
8680 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8681 }
8682
8683 /**
8684 * Called by draw to draw the child views. This may be overridden
8685 * by derived classes to gain control just before its children are drawn
8686 * (but after its own view has been drawn).
8687 * @param canvas the canvas on which to draw the view
8688 */
8689 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 }
8692
8693 /**
8694 * Gets the parent of this view. Note that the parent is a
8695 * ViewParent and not necessarily a View.
8696 *
8697 * @return Parent of this view.
8698 */
8699 public final ViewParent getParent() {
8700 return mParent;
8701 }
8702
8703 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008704 * Set the horizontal scrolled position of your view. This will cause a call to
8705 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8706 * invalidated.
8707 * @param value the x position to scroll to
8708 */
8709 public void setScrollX(int value) {
8710 scrollTo(value, mScrollY);
8711 }
8712
8713 /**
8714 * Set the vertical scrolled position of your view. This will cause a call to
8715 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8716 * invalidated.
8717 * @param value the y position to scroll to
8718 */
8719 public void setScrollY(int value) {
8720 scrollTo(mScrollX, value);
8721 }
8722
8723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 * Return the scrolled left position of this view. This is the left edge of
8725 * the displayed part of your view. You do not need to draw any pixels
8726 * farther left, since those are outside of the frame of your view on
8727 * screen.
8728 *
8729 * @return The left edge of the displayed part of your view, in pixels.
8730 */
8731 public final int getScrollX() {
8732 return mScrollX;
8733 }
8734
8735 /**
8736 * Return the scrolled top position of this view. This is the top edge of
8737 * the displayed part of your view. You do not need to draw any pixels above
8738 * it, since those are outside of the frame of your view on screen.
8739 *
8740 * @return The top edge of the displayed part of your view, in pixels.
8741 */
8742 public final int getScrollY() {
8743 return mScrollY;
8744 }
8745
8746 /**
8747 * Return the width of the your view.
8748 *
8749 * @return The width of your view, in pixels.
8750 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008751 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 public final int getWidth() {
8753 return mRight - mLeft;
8754 }
8755
8756 /**
8757 * Return the height of your view.
8758 *
8759 * @return The height of your view, in pixels.
8760 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008761 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 public final int getHeight() {
8763 return mBottom - mTop;
8764 }
8765
8766 /**
8767 * Return the visible drawing bounds of your view. Fills in the output
8768 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008769 * getWidth(), and getHeight(). These bounds do not account for any
8770 * transformation properties currently set on the view, such as
8771 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 *
8773 * @param outRect The (scrolled) drawing bounds of the view.
8774 */
8775 public void getDrawingRect(Rect outRect) {
8776 outRect.left = mScrollX;
8777 outRect.top = mScrollY;
8778 outRect.right = mScrollX + (mRight - mLeft);
8779 outRect.bottom = mScrollY + (mBottom - mTop);
8780 }
8781
8782 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008783 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8784 * raw width component (that is the result is masked by
8785 * {@link #MEASURED_SIZE_MASK}).
8786 *
8787 * @return The raw measured width of this view.
8788 */
8789 public final int getMeasuredWidth() {
8790 return mMeasuredWidth & MEASURED_SIZE_MASK;
8791 }
8792
8793 /**
8794 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008795 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008796 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 * This should be used during measurement and layout calculations only. Use
8798 * {@link #getWidth()} to see how wide a view is after layout.
8799 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008800 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008802 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 return mMeasuredWidth;
8804 }
8805
8806 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008807 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8808 * raw width component (that is the result is masked by
8809 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008811 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008812 */
8813 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008814 return mMeasuredHeight & MEASURED_SIZE_MASK;
8815 }
8816
8817 /**
8818 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008819 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008820 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8821 * This should be used during measurement and layout calculations only. Use
8822 * {@link #getHeight()} to see how wide a view is after layout.
8823 *
8824 * @return The measured width of this view as a bit mask.
8825 */
8826 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 return mMeasuredHeight;
8828 }
8829
8830 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008831 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8832 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8833 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8834 * and the height component is at the shifted bits
8835 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8836 */
8837 public final int getMeasuredState() {
8838 return (mMeasuredWidth&MEASURED_STATE_MASK)
8839 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8840 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8841 }
8842
8843 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008844 * The transform matrix of this view, which is calculated based on the current
8845 * roation, scale, and pivot properties.
8846 *
8847 * @see #getRotation()
8848 * @see #getScaleX()
8849 * @see #getScaleY()
8850 * @see #getPivotX()
8851 * @see #getPivotY()
8852 * @return The current transform matrix for the view
8853 */
8854 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008855 if (mTransformationInfo != null) {
8856 updateMatrix();
8857 return mTransformationInfo.mMatrix;
8858 }
8859 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008860 }
8861
8862 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008863 * Utility function to determine if the value is far enough away from zero to be
8864 * considered non-zero.
8865 * @param value A floating point value to check for zero-ness
8866 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8867 */
8868 private static boolean nonzero(float value) {
8869 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8870 }
8871
8872 /**
Jeff Brown86671742010-09-30 20:00:15 -07008873 * Returns true if the transform matrix is the identity matrix.
8874 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008875 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008876 * @return True if the transform matrix is the identity matrix, false otherwise.
8877 */
Jeff Brown86671742010-09-30 20:00:15 -07008878 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008879 if (mTransformationInfo != null) {
8880 updateMatrix();
8881 return mTransformationInfo.mMatrixIsIdentity;
8882 }
8883 return true;
8884 }
8885
8886 void ensureTransformationInfo() {
8887 if (mTransformationInfo == null) {
8888 mTransformationInfo = new TransformationInfo();
8889 }
Jeff Brown86671742010-09-30 20:00:15 -07008890 }
8891
8892 /**
8893 * Recomputes the transform matrix if necessary.
8894 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008895 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008896 final TransformationInfo info = mTransformationInfo;
8897 if (info == null) {
8898 return;
8899 }
8900 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008901 // transform-related properties have changed since the last time someone
8902 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008903
8904 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008905 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008906 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8907 info.mPrevWidth = mRight - mLeft;
8908 info.mPrevHeight = mBottom - mTop;
8909 info.mPivotX = info.mPrevWidth / 2f;
8910 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008911 }
8912 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008913 info.mMatrix.reset();
8914 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8915 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8916 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8917 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008918 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008919 if (info.mCamera == null) {
8920 info.mCamera = new Camera();
8921 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008922 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008923 info.mCamera.save();
8924 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8925 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8926 info.mCamera.getMatrix(info.matrix3D);
8927 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8928 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8929 info.mPivotY + info.mTranslationY);
8930 info.mMatrix.postConcat(info.matrix3D);
8931 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008932 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008933 info.mMatrixDirty = false;
8934 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8935 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008936 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008937 }
8938
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008939 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008940 * Utility method to retrieve the inverse of the current mMatrix property.
8941 * We cache the matrix to avoid recalculating it when transform properties
8942 * have not changed.
8943 *
8944 * @return The inverse of the current matrix of this view.
8945 */
Jeff Brown86671742010-09-30 20:00:15 -07008946 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008947 final TransformationInfo info = mTransformationInfo;
8948 if (info != null) {
8949 updateMatrix();
8950 if (info.mInverseMatrixDirty) {
8951 if (info.mInverseMatrix == null) {
8952 info.mInverseMatrix = new Matrix();
8953 }
8954 info.mMatrix.invert(info.mInverseMatrix);
8955 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008956 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008957 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008958 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008959 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008960 }
8961
8962 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008963 * Gets the distance along the Z axis from the camera to this view.
8964 *
8965 * @see #setCameraDistance(float)
8966 *
8967 * @return The distance along the Z axis.
8968 */
8969 public float getCameraDistance() {
8970 ensureTransformationInfo();
8971 final float dpi = mResources.getDisplayMetrics().densityDpi;
8972 final TransformationInfo info = mTransformationInfo;
8973 if (info.mCamera == null) {
8974 info.mCamera = new Camera();
8975 info.matrix3D = new Matrix();
8976 }
8977 return -(info.mCamera.getLocationZ() * dpi);
8978 }
8979
8980 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008981 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8982 * views are drawn) from the camera to this view. The camera's distance
8983 * affects 3D transformations, for instance rotations around the X and Y
8984 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008985 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008986 * use a camera distance that's greater than the height (X axis rotation) or
8987 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008988 *
Romain Guya5364ee2011-02-24 14:46:04 -08008989 * <p>The distance of the camera from the view plane can have an affect on the
8990 * perspective distortion of the view when it is rotated around the x or y axis.
8991 * For example, a large distance will result in a large viewing angle, and there
8992 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008993 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008994 * also result in some drawing artifacts if the rotated view ends up partially
8995 * behind the camera (which is why the recommendation is to use a distance at
8996 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008997 *
Romain Guya5364ee2011-02-24 14:46:04 -08008998 * <p>The distance is expressed in "depth pixels." The default distance depends
8999 * on the screen density. For instance, on a medium density display, the
9000 * default distance is 1280. On a high density display, the default distance
9001 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009002 *
Romain Guya5364ee2011-02-24 14:46:04 -08009003 * <p>If you want to specify a distance that leads to visually consistent
9004 * results across various densities, use the following formula:</p>
9005 * <pre>
9006 * float scale = context.getResources().getDisplayMetrics().density;
9007 * view.setCameraDistance(distance * scale);
9008 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07009009 *
Romain Guya5364ee2011-02-24 14:46:04 -08009010 * <p>The density scale factor of a high density display is 1.5,
9011 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009012 *
Romain Guya5364ee2011-02-24 14:46:04 -08009013 * @param distance The distance in "depth pixels", if negative the opposite
9014 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07009015 *
9016 * @see #setRotationX(float)
9017 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08009018 */
9019 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009020 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08009021
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009022 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08009023 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009024 final TransformationInfo info = mTransformationInfo;
9025 if (info.mCamera == null) {
9026 info.mCamera = new Camera();
9027 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08009028 }
9029
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009030 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
9031 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08009032
Chet Haase9d1992d2012-03-13 11:03:25 -07009033 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009034 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07009035 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08009036 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009037 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009038 // View was rejected last time it was drawn by its parent; this may have changed
9039 invalidateParentIfNeeded();
9040 }
Romain Guya5364ee2011-02-24 14:46:04 -08009041 }
9042
9043 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009044 * The degrees that the view is rotated around the pivot point.
9045 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009046 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07009047 * @see #getPivotX()
9048 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009049 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009050 * @return The degrees of rotation.
9051 */
Chet Haasea5531132012-02-02 13:41:44 -08009052 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009053 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009054 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009055 }
9056
9057 /**
Chet Haase897247b2010-09-09 14:54:47 -07009058 * Sets the degrees that the view is rotated around the pivot point. Increasing values
9059 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07009060 *
9061 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009062 *
9063 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07009064 * @see #getPivotX()
9065 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009066 * @see #setRotationX(float)
9067 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08009068 *
9069 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07009070 */
9071 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009072 ensureTransformationInfo();
9073 final TransformationInfo info = mTransformationInfo;
9074 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009075 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009076 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009077 info.mRotation = rotation;
9078 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009079 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009080 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009081 mDisplayList.setRotation(rotation);
9082 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009083 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009084 // View was rejected last time it was drawn by its parent; this may have changed
9085 invalidateParentIfNeeded();
9086 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009087 }
9088 }
9089
9090 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07009091 * The degrees that the view is rotated around the vertical axis through the pivot point.
9092 *
9093 * @see #getPivotX()
9094 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009095 * @see #setRotationY(float)
9096 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009097 * @return The degrees of Y rotation.
9098 */
Chet Haasea5531132012-02-02 13:41:44 -08009099 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009100 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009101 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009102 }
9103
9104 /**
Chet Haase897247b2010-09-09 14:54:47 -07009105 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9106 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9107 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009108 *
Romain Guya5364ee2011-02-24 14:46:04 -08009109 * When rotating large views, it is recommended to adjust the camera distance
9110 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009111 *
9112 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009113 *
9114 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009115 * @see #getPivotX()
9116 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009117 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009118 * @see #setRotationX(float)
9119 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009120 *
9121 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009122 */
9123 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009124 ensureTransformationInfo();
9125 final TransformationInfo info = mTransformationInfo;
9126 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009127 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009128 info.mRotationY = rotationY;
9129 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009130 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009131 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009132 mDisplayList.setRotationY(rotationY);
9133 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009134 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009135 // View was rejected last time it was drawn by its parent; this may have changed
9136 invalidateParentIfNeeded();
9137 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009138 }
9139 }
9140
9141 /**
9142 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9143 *
9144 * @see #getPivotX()
9145 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009146 * @see #setRotationX(float)
9147 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009148 * @return The degrees of X rotation.
9149 */
Chet Haasea5531132012-02-02 13:41:44 -08009150 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009151 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009152 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009153 }
9154
9155 /**
Chet Haase897247b2010-09-09 14:54:47 -07009156 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9157 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9158 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009159 *
Romain Guya5364ee2011-02-24 14:46:04 -08009160 * When rotating large views, it is recommended to adjust the camera distance
9161 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009162 *
9163 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009164 *
9165 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009166 * @see #getPivotX()
9167 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009168 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009169 * @see #setRotationY(float)
9170 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009171 *
9172 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009173 */
9174 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009175 ensureTransformationInfo();
9176 final TransformationInfo info = mTransformationInfo;
9177 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009178 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009179 info.mRotationX = rotationX;
9180 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009181 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009182 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009183 mDisplayList.setRotationX(rotationX);
9184 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009185 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009186 // View was rejected last time it was drawn by its parent; this may have changed
9187 invalidateParentIfNeeded();
9188 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009189 }
9190 }
9191
9192 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009193 * The amount that the view is scaled in x around the pivot point, as a proportion of
9194 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9195 *
Joe Onorato93162322010-09-16 15:42:01 -04009196 * <p>By default, this is 1.0f.
9197 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009198 * @see #getPivotX()
9199 * @see #getPivotY()
9200 * @return The scaling factor.
9201 */
Chet Haasea5531132012-02-02 13:41:44 -08009202 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009203 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009204 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009205 }
9206
9207 /**
9208 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9209 * the view's unscaled width. A value of 1 means that no scaling is applied.
9210 *
9211 * @param scaleX The scaling factor.
9212 * @see #getPivotX()
9213 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009214 *
9215 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009216 */
9217 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009218 ensureTransformationInfo();
9219 final TransformationInfo info = mTransformationInfo;
9220 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009221 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009222 info.mScaleX = scaleX;
9223 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009224 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009225 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009226 mDisplayList.setScaleX(scaleX);
9227 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009228 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009229 // View was rejected last time it was drawn by its parent; this may have changed
9230 invalidateParentIfNeeded();
9231 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009232 }
9233 }
9234
9235 /**
9236 * The amount that the view is scaled in y around the pivot point, as a proportion of
9237 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9238 *
Joe Onorato93162322010-09-16 15:42:01 -04009239 * <p>By default, this is 1.0f.
9240 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009241 * @see #getPivotX()
9242 * @see #getPivotY()
9243 * @return The scaling factor.
9244 */
Chet Haasea5531132012-02-02 13:41:44 -08009245 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009246 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009247 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009248 }
9249
9250 /**
9251 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9252 * the view's unscaled width. A value of 1 means that no scaling is applied.
9253 *
9254 * @param scaleY The scaling factor.
9255 * @see #getPivotX()
9256 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009257 *
9258 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009259 */
9260 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009261 ensureTransformationInfo();
9262 final TransformationInfo info = mTransformationInfo;
9263 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009264 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009265 info.mScaleY = scaleY;
9266 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009267 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009268 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009269 mDisplayList.setScaleY(scaleY);
9270 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009271 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009272 // View was rejected last time it was drawn by its parent; this may have changed
9273 invalidateParentIfNeeded();
9274 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009275 }
9276 }
9277
9278 /**
9279 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9280 * and {@link #setScaleX(float) scaled}.
9281 *
9282 * @see #getRotation()
9283 * @see #getScaleX()
9284 * @see #getScaleY()
9285 * @see #getPivotY()
9286 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009287 *
9288 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009289 */
Chet Haasea5531132012-02-02 13:41:44 -08009290 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009291 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009292 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009293 }
9294
9295 /**
9296 * Sets the x location of the point around which the view is
9297 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009298 * By default, the pivot point is centered on the object.
9299 * Setting this property disables this behavior and causes the view to use only the
9300 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009301 *
9302 * @param pivotX The x location of the pivot point.
9303 * @see #getRotation()
9304 * @see #getScaleX()
9305 * @see #getScaleY()
9306 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009307 *
9308 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009309 */
9310 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009311 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009312 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009313 final TransformationInfo info = mTransformationInfo;
9314 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009315 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009316 info.mPivotX = pivotX;
9317 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009318 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009319 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009320 mDisplayList.setPivotX(pivotX);
9321 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009322 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009323 // View was rejected last time it was drawn by its parent; this may have changed
9324 invalidateParentIfNeeded();
9325 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009326 }
9327 }
9328
9329 /**
9330 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9331 * and {@link #setScaleY(float) scaled}.
9332 *
9333 * @see #getRotation()
9334 * @see #getScaleX()
9335 * @see #getScaleY()
9336 * @see #getPivotY()
9337 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009338 *
9339 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009340 */
Chet Haasea5531132012-02-02 13:41:44 -08009341 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009342 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009343 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009344 }
9345
9346 /**
9347 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009348 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9349 * Setting this property disables this behavior and causes the view to use only the
9350 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009351 *
9352 * @param pivotY The y location of the pivot point.
9353 * @see #getRotation()
9354 * @see #getScaleX()
9355 * @see #getScaleY()
9356 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009357 *
9358 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009359 */
9360 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009361 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009362 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009363 final TransformationInfo info = mTransformationInfo;
9364 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009365 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009366 info.mPivotY = pivotY;
9367 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009368 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009369 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009370 mDisplayList.setPivotY(pivotY);
9371 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009372 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009373 // View was rejected last time it was drawn by its parent; this may have changed
9374 invalidateParentIfNeeded();
9375 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009376 }
9377 }
9378
9379 /**
9380 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9381 * completely transparent and 1 means the view is completely opaque.
9382 *
Joe Onorato93162322010-09-16 15:42:01 -04009383 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009384 * @return The opacity of the view.
9385 */
Chet Haasea5531132012-02-02 13:41:44 -08009386 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009387 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009388 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009389 }
9390
9391 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009392 * Returns whether this View has content which overlaps. This function, intended to be
9393 * overridden by specific View types, is an optimization when alpha is set on a view. If
9394 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9395 * and then composited it into place, which can be expensive. If the view has no overlapping
9396 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9397 * An example of overlapping rendering is a TextView with a background image, such as a
9398 * Button. An example of non-overlapping rendering is a TextView with no background, or
9399 * an ImageView with only the foreground image. The default implementation returns true;
9400 * subclasses should override if they have cases which can be optimized.
9401 *
9402 * @return true if the content in this view might overlap, false otherwise.
9403 */
9404 public boolean hasOverlappingRendering() {
9405 return true;
9406 }
9407
9408 /**
Romain Guy171c5922011-01-06 10:04:23 -08009409 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9410 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009411 *
Romain Guy171c5922011-01-06 10:04:23 -08009412 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9413 * responsible for applying the opacity itself. Otherwise, calling this method is
9414 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009415 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009416 *
Chet Haasea5531132012-02-02 13:41:44 -08009417 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9418 * performance implications. It is generally best to use the alpha property sparingly and
9419 * transiently, as in the case of fading animations.</p>
9420 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009421 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009422 *
Joe Malin32736f02011-01-19 16:14:20 -08009423 * @see #setLayerType(int, android.graphics.Paint)
9424 *
Chet Haase73066682010-11-29 15:55:32 -08009425 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009426 */
9427 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009428 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009429 if (mTransformationInfo.mAlpha != alpha) {
9430 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009431 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009432 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009433 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009434 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009435 invalidate(true);
9436 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009437 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009438 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009439 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009440 mDisplayList.setAlpha(alpha);
9441 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009442 }
Chet Haaseed032702010-10-01 14:05:54 -07009443 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009444 }
9445
9446 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009447 * Faster version of setAlpha() which performs the same steps except there are
9448 * no calls to invalidate(). The caller of this function should perform proper invalidation
9449 * on the parent and this object. The return value indicates whether the subclass handles
9450 * alpha (the return value for onSetAlpha()).
9451 *
9452 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009453 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9454 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009455 */
9456 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009457 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009458 if (mTransformationInfo.mAlpha != alpha) {
9459 mTransformationInfo.mAlpha = alpha;
9460 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9461 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009462 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009463 return true;
9464 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009465 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009466 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009467 mDisplayList.setAlpha(alpha);
9468 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009469 }
Chet Haasea00f3862011-02-22 06:34:40 -08009470 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009471 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009472 }
9473
9474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 * Top position of this view relative to its parent.
9476 *
9477 * @return The top of this view, in pixels.
9478 */
9479 @ViewDebug.CapturedViewProperty
9480 public final int getTop() {
9481 return mTop;
9482 }
9483
9484 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009485 * Sets the top position of this view relative to its parent. This method is meant to be called
9486 * by the layout system and should not generally be called otherwise, because the property
9487 * may be changed at any time by the layout.
9488 *
9489 * @param top The top of this view, in pixels.
9490 */
9491 public final void setTop(int top) {
9492 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009493 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009494 final boolean matrixIsIdentity = mTransformationInfo == null
9495 || mTransformationInfo.mMatrixIsIdentity;
9496 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009497 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009498 int minTop;
9499 int yLoc;
9500 if (top < mTop) {
9501 minTop = top;
9502 yLoc = top - mTop;
9503 } else {
9504 minTop = mTop;
9505 yLoc = 0;
9506 }
Chet Haasee9140a72011-02-16 16:23:29 -08009507 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009508 }
9509 } else {
9510 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009511 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009512 }
9513
Chet Haaseed032702010-10-01 14:05:54 -07009514 int width = mRight - mLeft;
9515 int oldHeight = mBottom - mTop;
9516
Chet Haase21cd1382010-09-01 17:42:29 -07009517 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009518 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009519 mDisplayList.setTop(mTop);
9520 }
Chet Haase21cd1382010-09-01 17:42:29 -07009521
Chet Haaseed032702010-10-01 14:05:54 -07009522 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9523
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009524 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009525 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009526 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009527 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009528 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009529 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009530 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009531 }
Chet Haase55dbb652010-12-21 20:15:08 -08009532 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009533 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009534 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009535 // View was rejected last time it was drawn by its parent; this may have changed
9536 invalidateParentIfNeeded();
9537 }
Chet Haase21cd1382010-09-01 17:42:29 -07009538 }
9539 }
9540
9541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 * Bottom position of this view relative to its parent.
9543 *
9544 * @return The bottom of this view, in pixels.
9545 */
9546 @ViewDebug.CapturedViewProperty
9547 public final int getBottom() {
9548 return mBottom;
9549 }
9550
9551 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009552 * True if this view has changed since the last time being drawn.
9553 *
9554 * @return The dirty state of this view.
9555 */
9556 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009557 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009558 }
9559
9560 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009561 * Sets the bottom position of this view relative to its parent. This method is meant to be
9562 * called by the layout system and should not generally be called otherwise, because the
9563 * property may be changed at any time by the layout.
9564 *
9565 * @param bottom The bottom of this view, in pixels.
9566 */
9567 public final void setBottom(int bottom) {
9568 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009569 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009570 final boolean matrixIsIdentity = mTransformationInfo == null
9571 || mTransformationInfo.mMatrixIsIdentity;
9572 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009573 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009574 int maxBottom;
9575 if (bottom < mBottom) {
9576 maxBottom = mBottom;
9577 } else {
9578 maxBottom = bottom;
9579 }
Chet Haasee9140a72011-02-16 16:23:29 -08009580 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009581 }
9582 } else {
9583 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009584 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009585 }
9586
Chet Haaseed032702010-10-01 14:05:54 -07009587 int width = mRight - mLeft;
9588 int oldHeight = mBottom - mTop;
9589
Chet Haase21cd1382010-09-01 17:42:29 -07009590 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009591 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009592 mDisplayList.setBottom(mBottom);
9593 }
Chet Haase21cd1382010-09-01 17:42:29 -07009594
Chet Haaseed032702010-10-01 14:05:54 -07009595 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9596
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009597 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009598 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009599 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009600 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009601 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009602 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009603 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009604 }
Chet Haase55dbb652010-12-21 20:15:08 -08009605 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009606 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009607 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009608 // View was rejected last time it was drawn by its parent; this may have changed
9609 invalidateParentIfNeeded();
9610 }
Chet Haase21cd1382010-09-01 17:42:29 -07009611 }
9612 }
9613
9614 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 * Left position of this view relative to its parent.
9616 *
9617 * @return The left edge of this view, in pixels.
9618 */
9619 @ViewDebug.CapturedViewProperty
9620 public final int getLeft() {
9621 return mLeft;
9622 }
9623
9624 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009625 * Sets the left position of this view relative to its parent. This method is meant to be called
9626 * by the layout system and should not generally be called otherwise, because the property
9627 * may be changed at any time by the layout.
9628 *
9629 * @param left The bottom of this view, in pixels.
9630 */
9631 public final void setLeft(int left) {
9632 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009633 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009634 final boolean matrixIsIdentity = mTransformationInfo == null
9635 || mTransformationInfo.mMatrixIsIdentity;
9636 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009637 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009638 int minLeft;
9639 int xLoc;
9640 if (left < mLeft) {
9641 minLeft = left;
9642 xLoc = left - mLeft;
9643 } else {
9644 minLeft = mLeft;
9645 xLoc = 0;
9646 }
Chet Haasee9140a72011-02-16 16:23:29 -08009647 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009648 }
9649 } else {
9650 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009651 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009652 }
9653
Chet Haaseed032702010-10-01 14:05:54 -07009654 int oldWidth = mRight - mLeft;
9655 int height = mBottom - mTop;
9656
Chet Haase21cd1382010-09-01 17:42:29 -07009657 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009658 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009659 mDisplayList.setLeft(left);
9660 }
Chet Haase21cd1382010-09-01 17:42:29 -07009661
Chet Haaseed032702010-10-01 14:05:54 -07009662 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9663
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009664 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009665 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009666 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009667 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009668 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009669 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009670 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009671 }
Chet Haase55dbb652010-12-21 20:15:08 -08009672 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009673 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009674 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009675 // View was rejected last time it was drawn by its parent; this may have changed
9676 invalidateParentIfNeeded();
9677 }
Chet Haase21cd1382010-09-01 17:42:29 -07009678 }
9679 }
9680
9681 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009682 * Right position of this view relative to its parent.
9683 *
9684 * @return The right edge of this view, in pixels.
9685 */
9686 @ViewDebug.CapturedViewProperty
9687 public final int getRight() {
9688 return mRight;
9689 }
9690
9691 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009692 * Sets the right position of this view relative to its parent. This method is meant to be called
9693 * by the layout system and should not generally be called otherwise, because the property
9694 * may be changed at any time by the layout.
9695 *
9696 * @param right The bottom of this view, in pixels.
9697 */
9698 public final void setRight(int right) {
9699 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009700 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009701 final boolean matrixIsIdentity = mTransformationInfo == null
9702 || mTransformationInfo.mMatrixIsIdentity;
9703 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009704 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009705 int maxRight;
9706 if (right < mRight) {
9707 maxRight = mRight;
9708 } else {
9709 maxRight = right;
9710 }
Chet Haasee9140a72011-02-16 16:23:29 -08009711 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009712 }
9713 } else {
9714 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009715 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009716 }
9717
Chet Haaseed032702010-10-01 14:05:54 -07009718 int oldWidth = mRight - mLeft;
9719 int height = mBottom - mTop;
9720
Chet Haase21cd1382010-09-01 17:42:29 -07009721 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009722 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009723 mDisplayList.setRight(mRight);
9724 }
Chet Haase21cd1382010-09-01 17:42:29 -07009725
Chet Haaseed032702010-10-01 14:05:54 -07009726 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9727
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009728 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009729 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009730 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009731 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009732 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009733 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009734 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009735 }
Chet Haase55dbb652010-12-21 20:15:08 -08009736 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009737 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009738 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009739 // View was rejected last time it was drawn by its parent; this may have changed
9740 invalidateParentIfNeeded();
9741 }
Chet Haase21cd1382010-09-01 17:42:29 -07009742 }
9743 }
9744
9745 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009746 * The visual x position of this view, in pixels. This is equivalent to the
9747 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009748 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009749 *
Chet Haasedf030d22010-07-30 17:22:38 -07009750 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009751 */
Chet Haasea5531132012-02-02 13:41:44 -08009752 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009753 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009754 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009755 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009756
Chet Haasedf030d22010-07-30 17:22:38 -07009757 /**
9758 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9759 * {@link #setTranslationX(float) translationX} property to be the difference between
9760 * the x value passed in and the current {@link #getLeft() left} property.
9761 *
9762 * @param x The visual x position of this view, in pixels.
9763 */
9764 public void setX(float x) {
9765 setTranslationX(x - mLeft);
9766 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009767
Chet Haasedf030d22010-07-30 17:22:38 -07009768 /**
9769 * The visual y position of this view, in pixels. This is equivalent to the
9770 * {@link #setTranslationY(float) translationY} property plus the current
9771 * {@link #getTop() top} property.
9772 *
9773 * @return The visual y position of this view, in pixels.
9774 */
Chet Haasea5531132012-02-02 13:41:44 -08009775 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009776 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009777 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009778 }
9779
9780 /**
9781 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9782 * {@link #setTranslationY(float) translationY} property to be the difference between
9783 * the y value passed in and the current {@link #getTop() top} property.
9784 *
9785 * @param y The visual y position of this view, in pixels.
9786 */
9787 public void setY(float y) {
9788 setTranslationY(y - mTop);
9789 }
9790
9791
9792 /**
9793 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9794 * This position is post-layout, in addition to wherever the object's
9795 * layout placed it.
9796 *
9797 * @return The horizontal position of this view relative to its left position, in pixels.
9798 */
Chet Haasea5531132012-02-02 13:41:44 -08009799 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009800 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009801 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009802 }
9803
9804 /**
9805 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9806 * This effectively positions the object post-layout, in addition to wherever the object's
9807 * layout placed it.
9808 *
9809 * @param translationX The horizontal position of this view relative to its left position,
9810 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009811 *
9812 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009813 */
9814 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009815 ensureTransformationInfo();
9816 final TransformationInfo info = mTransformationInfo;
9817 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009818 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009819 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009820 info.mTranslationX = translationX;
9821 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009822 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009823 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009824 mDisplayList.setTranslationX(translationX);
9825 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009826 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009827 // View was rejected last time it was drawn by its parent; this may have changed
9828 invalidateParentIfNeeded();
9829 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009830 }
9831 }
9832
9833 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009834 * The horizontal location of this view relative to its {@link #getTop() top} position.
9835 * This position is post-layout, in addition to wherever the object's
9836 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009837 *
Chet Haasedf030d22010-07-30 17:22:38 -07009838 * @return The vertical position of this view relative to its top position,
9839 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009840 */
Chet Haasea5531132012-02-02 13:41:44 -08009841 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009842 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009843 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009844 }
9845
9846 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009847 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9848 * This effectively positions the object post-layout, in addition to wherever the object's
9849 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009850 *
Chet Haasedf030d22010-07-30 17:22:38 -07009851 * @param translationY The vertical position of this view relative to its top position,
9852 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009853 *
9854 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009855 */
Chet Haasedf030d22010-07-30 17:22:38 -07009856 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009857 ensureTransformationInfo();
9858 final TransformationInfo info = mTransformationInfo;
9859 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009860 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009861 info.mTranslationY = translationY;
9862 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009863 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009864 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009865 mDisplayList.setTranslationY(translationY);
9866 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009867 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009868 // View was rejected last time it was drawn by its parent; this may have changed
9869 invalidateParentIfNeeded();
9870 }
Chet Haasedf030d22010-07-30 17:22:38 -07009871 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009872 }
9873
9874 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 * Hit rectangle in parent's coordinates
9876 *
9877 * @param outRect The hit rectangle of the view.
9878 */
9879 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009880 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009881 final TransformationInfo info = mTransformationInfo;
9882 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009883 outRect.set(mLeft, mTop, mRight, mBottom);
9884 } else {
9885 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009886 tmpRect.set(-info.mPivotX, -info.mPivotY,
9887 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9888 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009889 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9890 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009891 }
9892 }
9893
9894 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009895 * Determines whether the given point, in local coordinates is inside the view.
9896 */
9897 /*package*/ final boolean pointInView(float localX, float localY) {
9898 return localX >= 0 && localX < (mRight - mLeft)
9899 && localY >= 0 && localY < (mBottom - mTop);
9900 }
9901
9902 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009903 * Utility method to determine whether the given point, in local coordinates,
9904 * is inside the view, where the area of the view is expanded by the slop factor.
9905 * This method is called while processing touch-move events to determine if the event
9906 * is still within the view.
9907 */
9908 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009909 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009910 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 }
9912
9913 /**
9914 * When a view has focus and the user navigates away from it, the next view is searched for
9915 * starting from the rectangle filled in by this method.
9916 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009917 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009918 * of the view. However, if your view maintains some idea of internal selection,
9919 * such as a cursor, or a selected row or column, you should override this method and
9920 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921 *
9922 * @param r The rectangle to fill in, in this view's coordinates.
9923 */
9924 public void getFocusedRect(Rect r) {
9925 getDrawingRect(r);
9926 }
9927
9928 /**
9929 * If some part of this view is not clipped by any of its parents, then
9930 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009931 * coordinates (without taking possible View rotations into account), offset
9932 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9933 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 *
9935 * @param r If true is returned, r holds the global coordinates of the
9936 * visible portion of this view.
9937 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9938 * between this view and its root. globalOffet may be null.
9939 * @return true if r is non-empty (i.e. part of the view is visible at the
9940 * root level.
9941 */
9942 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9943 int width = mRight - mLeft;
9944 int height = mBottom - mTop;
9945 if (width > 0 && height > 0) {
9946 r.set(0, 0, width, height);
9947 if (globalOffset != null) {
9948 globalOffset.set(-mScrollX, -mScrollY);
9949 }
9950 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9951 }
9952 return false;
9953 }
9954
9955 public final boolean getGlobalVisibleRect(Rect r) {
9956 return getGlobalVisibleRect(r, null);
9957 }
9958
9959 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009960 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 if (getGlobalVisibleRect(r, offset)) {
9962 r.offset(-offset.x, -offset.y); // make r local
9963 return true;
9964 }
9965 return false;
9966 }
9967
9968 /**
9969 * Offset this view's vertical location by the specified number of pixels.
9970 *
9971 * @param offset the number of pixels to offset the view by
9972 */
9973 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009974 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009975 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009976 final boolean matrixIsIdentity = mTransformationInfo == null
9977 || mTransformationInfo.mMatrixIsIdentity;
9978 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009979 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009980 invalidateViewProperty(false, false);
9981 } else {
9982 final ViewParent p = mParent;
9983 if (p != null && mAttachInfo != null) {
9984 final Rect r = mAttachInfo.mTmpInvalRect;
9985 int minTop;
9986 int maxBottom;
9987 int yLoc;
9988 if (offset < 0) {
9989 minTop = mTop + offset;
9990 maxBottom = mBottom;
9991 yLoc = offset;
9992 } else {
9993 minTop = mTop;
9994 maxBottom = mBottom + offset;
9995 yLoc = 0;
9996 }
9997 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9998 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009999 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010000 }
10001 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -070010002 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -070010003 }
Romain Guy33e72ae2010-07-17 12:40:29 -070010004
Chet Haasec3aa3612010-06-17 08:50:37 -070010005 mTop += offset;
10006 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -070010007 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080010008 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -070010009 invalidateViewProperty(false, false);
10010 } else {
10011 if (!matrixIsIdentity) {
10012 invalidateViewProperty(false, true);
10013 }
10014 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -080010015 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 }
10018
10019 /**
10020 * Offset this view's horizontal location by the specified amount of pixels.
10021 *
10022 * @param offset the numer of pixels to offset the view by
10023 */
10024 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -070010025 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -070010026 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010027 final boolean matrixIsIdentity = mTransformationInfo == null
10028 || mTransformationInfo.mMatrixIsIdentity;
10029 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010030 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010031 invalidateViewProperty(false, false);
10032 } else {
10033 final ViewParent p = mParent;
10034 if (p != null && mAttachInfo != null) {
10035 final Rect r = mAttachInfo.mTmpInvalRect;
10036 int minLeft;
10037 int maxRight;
10038 if (offset < 0) {
10039 minLeft = mLeft + offset;
10040 maxRight = mRight;
10041 } else {
10042 minLeft = mLeft;
10043 maxRight = mRight + offset;
10044 }
10045 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
10046 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -070010047 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010048 }
10049 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -070010050 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -070010051 }
Romain Guy33e72ae2010-07-17 12:40:29 -070010052
Chet Haasec3aa3612010-06-17 08:50:37 -070010053 mLeft += offset;
10054 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -070010055 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080010056 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -070010057 invalidateViewProperty(false, false);
10058 } else {
10059 if (!matrixIsIdentity) {
10060 invalidateViewProperty(false, true);
10061 }
10062 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -080010063 }
Chet Haasec3aa3612010-06-17 08:50:37 -070010064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 }
10066
10067 /**
10068 * Get the LayoutParams associated with this view. All views should have
10069 * layout parameters. These supply parameters to the <i>parent</i> of this
10070 * view specifying how it should be arranged. There are many subclasses of
10071 * ViewGroup.LayoutParams, and these correspond to the different subclasses
10072 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -080010073 *
10074 * This method may return null if this View is not attached to a parent
10075 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
10076 * was not invoked successfully. When a View is attached to a parent
10077 * ViewGroup, this method must not return null.
10078 *
10079 * @return The LayoutParams associated with this view, or null if no
10080 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -070010082 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 public ViewGroup.LayoutParams getLayoutParams() {
10084 return mLayoutParams;
10085 }
10086
10087 /**
10088 * Set the layout parameters associated with this view. These supply
10089 * parameters to the <i>parent</i> of this view specifying how it should be
10090 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
10091 * correspond to the different subclasses of ViewGroup that are responsible
10092 * for arranging their children.
10093 *
Romain Guy01c174b2011-02-22 11:51:06 -080010094 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 */
10096 public void setLayoutParams(ViewGroup.LayoutParams params) {
10097 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010098 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 }
10100 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010101 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010102 if (mParent instanceof ViewGroup) {
10103 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010105 requestLayout();
10106 }
10107
10108 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010109 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010110 *
10111 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010112 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010113 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010114 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010115 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010116 }
10117 }
10118
10119 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010120 * Set the scrolled position of your view. This will cause a call to
10121 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10122 * invalidated.
10123 * @param x the x position to scroll to
10124 * @param y the y position to scroll to
10125 */
10126 public void scrollTo(int x, int y) {
10127 if (mScrollX != x || mScrollY != y) {
10128 int oldX = mScrollX;
10129 int oldY = mScrollY;
10130 mScrollX = x;
10131 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010132 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010134 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010135 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010137 }
10138 }
10139
10140 /**
10141 * Move the scrolled position of your view. This will cause a call to
10142 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10143 * invalidated.
10144 * @param x the amount of pixels to scroll by horizontally
10145 * @param y the amount of pixels to scroll by vertically
10146 */
10147 public void scrollBy(int x, int y) {
10148 scrollTo(mScrollX + x, mScrollY + y);
10149 }
10150
10151 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010152 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10153 * animation to fade the scrollbars out after a default delay. If a subclass
10154 * provides animated scrolling, the start delay should equal the duration
10155 * of the scrolling animation.</p>
10156 *
10157 * <p>The animation starts only if at least one of the scrollbars is
10158 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10159 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10160 * this method returns true, and false otherwise. If the animation is
10161 * started, this method calls {@link #invalidate()}; in that case the
10162 * caller should not call {@link #invalidate()}.</p>
10163 *
10164 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010165 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010166 *
10167 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10168 * and {@link #scrollTo(int, int)}.</p>
10169 *
10170 * @return true if the animation is played, false otherwise
10171 *
10172 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010173 * @see #scrollBy(int, int)
10174 * @see #scrollTo(int, int)
10175 * @see #isHorizontalScrollBarEnabled()
10176 * @see #isVerticalScrollBarEnabled()
10177 * @see #setHorizontalScrollBarEnabled(boolean)
10178 * @see #setVerticalScrollBarEnabled(boolean)
10179 */
10180 protected boolean awakenScrollBars() {
10181 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010182 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010183 }
10184
10185 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010186 * Trigger the scrollbars to draw.
10187 * This method differs from awakenScrollBars() only in its default duration.
10188 * initialAwakenScrollBars() will show the scroll bars for longer than
10189 * usual to give the user more of a chance to notice them.
10190 *
10191 * @return true if the animation is played, false otherwise.
10192 */
10193 private boolean initialAwakenScrollBars() {
10194 return mScrollCache != null &&
10195 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10196 }
10197
10198 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010199 * <p>
10200 * Trigger the scrollbars to draw. When invoked this method starts an
10201 * animation to fade the scrollbars out after a fixed delay. If a subclass
10202 * provides animated scrolling, the start delay should equal the duration of
10203 * the scrolling animation.
10204 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010205 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010206 * <p>
10207 * The animation starts only if at least one of the scrollbars is enabled,
10208 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10209 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10210 * this method returns true, and false otherwise. If the animation is
10211 * started, this method calls {@link #invalidate()}; in that case the caller
10212 * should not call {@link #invalidate()}.
10213 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010214 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010215 * <p>
10216 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010217 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010218 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010219 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010220 * @param startDelay the delay, in milliseconds, after which the animation
10221 * should start; when the delay is 0, the animation starts
10222 * immediately
10223 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010224 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010225 * @see #scrollBy(int, int)
10226 * @see #scrollTo(int, int)
10227 * @see #isHorizontalScrollBarEnabled()
10228 * @see #isVerticalScrollBarEnabled()
10229 * @see #setHorizontalScrollBarEnabled(boolean)
10230 * @see #setVerticalScrollBarEnabled(boolean)
10231 */
10232 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010233 return awakenScrollBars(startDelay, true);
10234 }
Joe Malin32736f02011-01-19 16:14:20 -080010235
Mike Cleron290947b2009-09-29 18:34:32 -070010236 /**
10237 * <p>
10238 * Trigger the scrollbars to draw. When invoked this method starts an
10239 * animation to fade the scrollbars out after a fixed delay. If a subclass
10240 * provides animated scrolling, the start delay should equal the duration of
10241 * the scrolling animation.
10242 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010243 *
Mike Cleron290947b2009-09-29 18:34:32 -070010244 * <p>
10245 * The animation starts only if at least one of the scrollbars is enabled,
10246 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10247 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10248 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010249 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010250 * is set to true; in that case the caller
10251 * should not call {@link #invalidate()}.
10252 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010253 *
Mike Cleron290947b2009-09-29 18:34:32 -070010254 * <p>
10255 * This method should be invoked everytime a subclass directly updates the
10256 * scroll parameters.
10257 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010258 *
Mike Cleron290947b2009-09-29 18:34:32 -070010259 * @param startDelay the delay, in milliseconds, after which the animation
10260 * should start; when the delay is 0, the animation starts
10261 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010262 *
Mike Cleron290947b2009-09-29 18:34:32 -070010263 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010264 *
Mike Cleron290947b2009-09-29 18:34:32 -070010265 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010266 *
Mike Cleron290947b2009-09-29 18:34:32 -070010267 * @see #scrollBy(int, int)
10268 * @see #scrollTo(int, int)
10269 * @see #isHorizontalScrollBarEnabled()
10270 * @see #isVerticalScrollBarEnabled()
10271 * @see #setHorizontalScrollBarEnabled(boolean)
10272 * @see #setVerticalScrollBarEnabled(boolean)
10273 */
10274 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010275 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010276
Mike Cleronf116bf82009-09-27 19:14:12 -070010277 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10278 return false;
10279 }
10280
10281 if (scrollCache.scrollBar == null) {
10282 scrollCache.scrollBar = new ScrollBarDrawable();
10283 }
10284
10285 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10286
Mike Cleron290947b2009-09-29 18:34:32 -070010287 if (invalidate) {
10288 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010289 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010290 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010291
10292 if (scrollCache.state == ScrollabilityCache.OFF) {
10293 // FIXME: this is copied from WindowManagerService.
10294 // We should get this value from the system when it
10295 // is possible to do so.
10296 final int KEY_REPEAT_FIRST_DELAY = 750;
10297 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10298 }
10299
10300 // Tell mScrollCache when we should start fading. This may
10301 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010302 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010303 scrollCache.fadeStartTime = fadeStartTime;
10304 scrollCache.state = ScrollabilityCache.ON;
10305
10306 // Schedule our fader to run, unscheduling any old ones first
10307 if (mAttachInfo != null) {
10308 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10309 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10310 }
10311
10312 return true;
10313 }
10314
10315 return false;
10316 }
10317
10318 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010319 * Do not invalidate views which are not visible and which are not running an animation. They
10320 * will not get drawn and they should not set dirty flags as if they will be drawn
10321 */
10322 private boolean skipInvalidate() {
10323 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10324 (!(mParent instanceof ViewGroup) ||
10325 !((ViewGroup) mParent).isViewTransitioning(this));
10326 }
10327 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010328 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010329 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10330 * in the future. This must be called from a UI thread. To call from a non-UI
10331 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 *
10333 * WARNING: This method is destructive to dirty.
10334 * @param dirty the rectangle representing the bounds of the dirty region
10335 */
10336 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010337 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010338 return;
10339 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010340 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10341 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10342 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10343 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10344 mPrivateFlags |= PFLAG_INVALIDATED;
10345 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 final ViewParent p = mParent;
10347 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010348 //noinspection PointlessBooleanExpression,ConstantConditions
10349 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10350 if (p != null && ai != null && ai.mHardwareAccelerated) {
10351 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010352 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010353 p.invalidateChild(this, null);
10354 return;
10355 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 if (p != null && ai != null) {
10358 final int scrollX = mScrollX;
10359 final int scrollY = mScrollY;
10360 final Rect r = ai.mTmpInvalRect;
10361 r.set(dirty.left - scrollX, dirty.top - scrollY,
10362 dirty.right - scrollX, dirty.bottom - scrollY);
10363 mParent.invalidateChild(this, r);
10364 }
10365 }
10366 }
10367
10368 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010369 * 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 -080010370 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010371 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10372 * will be called at some point in the future. This must be called from
10373 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 * @param l the left position of the dirty region
10375 * @param t the top position of the dirty region
10376 * @param r the right position of the dirty region
10377 * @param b the bottom position of the dirty region
10378 */
10379 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010380 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010381 return;
10382 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010383 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10384 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10385 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10386 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10387 mPrivateFlags |= PFLAG_INVALIDATED;
10388 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 final ViewParent p = mParent;
10390 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010391 //noinspection PointlessBooleanExpression,ConstantConditions
10392 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10393 if (p != null && ai != null && ai.mHardwareAccelerated) {
10394 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010395 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010396 p.invalidateChild(this, null);
10397 return;
10398 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 if (p != null && ai != null && l < r && t < b) {
10401 final int scrollX = mScrollX;
10402 final int scrollY = mScrollY;
10403 final Rect tmpr = ai.mTmpInvalRect;
10404 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10405 p.invalidateChild(this, tmpr);
10406 }
10407 }
10408 }
10409
10410 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010411 * Invalidate the whole view. If the view is visible,
10412 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10413 * the future. This must be called from a UI thread. To call from a non-UI thread,
10414 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010415 */
10416 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010417 invalidate(true);
10418 }
Joe Malin32736f02011-01-19 16:14:20 -080010419
Chet Haaseed032702010-10-01 14:05:54 -070010420 /**
10421 * This is where the invalidate() work actually happens. A full invalidate()
10422 * causes the drawing cache to be invalidated, but this function can be called with
10423 * invalidateCache set to false to skip that invalidation step for cases that do not
10424 * need it (for example, a component that remains at the same dimensions with the same
10425 * content).
10426 *
10427 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10428 * well. This is usually true for a full invalidate, but may be set to false if the
10429 * View's contents or dimensions have not changed.
10430 */
Romain Guy849d0a32011-02-01 17:20:48 -080010431 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010432 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010433 return;
10434 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010435 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10436 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10437 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010438 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010439 mPrivateFlags &= ~PFLAG_DRAWN;
10440 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010441 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010442 mPrivateFlags |= PFLAG_INVALIDATED;
10443 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010446 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010447 //noinspection PointlessBooleanExpression,ConstantConditions
10448 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10449 if (p != null && ai != null && ai.mHardwareAccelerated) {
10450 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010451 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010452 p.invalidateChild(this, null);
10453 return;
10454 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010455 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 if (p != null && ai != null) {
10458 final Rect r = ai.mTmpInvalRect;
10459 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10460 // Don't call invalidate -- we don't want to internally scroll
10461 // our own bounds
10462 p.invalidateChild(this, r);
10463 }
10464 }
10465 }
10466
10467 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010468 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10469 * set any flags or handle all of the cases handled by the default invalidation methods.
10470 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10471 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10472 * walk up the hierarchy, transforming the dirty rect as necessary.
10473 *
10474 * The method also handles normal invalidation logic if display list properties are not
10475 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10476 * backup approach, to handle these cases used in the various property-setting methods.
10477 *
10478 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10479 * are not being used in this view
10480 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10481 * list properties are not being used in this view
10482 */
10483 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010484 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010485 if (invalidateParent) {
10486 invalidateParentCaches();
10487 }
10488 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010489 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010490 }
10491 invalidate(false);
10492 } else {
10493 final AttachInfo ai = mAttachInfo;
10494 final ViewParent p = mParent;
10495 if (p != null && ai != null) {
10496 final Rect r = ai.mTmpInvalRect;
10497 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10498 if (mParent instanceof ViewGroup) {
10499 ((ViewGroup) mParent).invalidateChildFast(this, r);
10500 } else {
10501 mParent.invalidateChild(this, r);
10502 }
10503 }
10504 }
10505 }
10506
10507 /**
10508 * Utility method to transform a given Rect by the current matrix of this view.
10509 */
10510 void transformRect(final Rect rect) {
10511 if (!getMatrix().isIdentity()) {
10512 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10513 boundingRect.set(rect);
10514 getMatrix().mapRect(boundingRect);
10515 rect.set((int) (boundingRect.left - 0.5f),
10516 (int) (boundingRect.top - 0.5f),
10517 (int) (boundingRect.right + 0.5f),
10518 (int) (boundingRect.bottom + 0.5f));
10519 }
10520 }
10521
10522 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010523 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010524 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10525 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010526 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10527 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010528 *
10529 * @hide
10530 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010531 protected void invalidateParentCaches() {
10532 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010533 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010534 }
10535 }
Joe Malin32736f02011-01-19 16:14:20 -080010536
Romain Guy0fd89bf2011-01-26 15:41:30 -080010537 /**
10538 * Used to indicate that the parent of this view should be invalidated. This functionality
10539 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10540 * which is necessary when various parent-managed properties of the view change, such as
10541 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10542 * an invalidation event to the parent.
10543 *
10544 * @hide
10545 */
10546 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010547 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010548 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010549 }
10550 }
10551
10552 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010553 * Indicates whether this View is opaque. An opaque View guarantees that it will
10554 * draw all the pixels overlapping its bounds using a fully opaque color.
10555 *
10556 * Subclasses of View should override this method whenever possible to indicate
10557 * whether an instance is opaque. Opaque Views are treated in a special way by
10558 * the View hierarchy, possibly allowing it to perform optimizations during
10559 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010560 *
Romain Guy24443ea2009-05-11 11:56:30 -070010561 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010562 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010563 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010564 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010565 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010566 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010567 }
10568
Adam Powell20232d02010-12-08 21:08:53 -080010569 /**
10570 * @hide
10571 */
10572 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010573 // Opaque if:
10574 // - Has a background
10575 // - Background is opaque
10576 // - Doesn't have scrollbars or scrollbars are inside overlay
10577
Philip Milne6c8ea062012-04-03 17:38:43 -070010578 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010579 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010580 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010581 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010582 }
10583
10584 final int flags = mViewFlags;
10585 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10586 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010587 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010588 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010589 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010590 }
10591 }
10592
10593 /**
10594 * @hide
10595 */
10596 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010597 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010598 }
10599
10600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 * @return A handler associated with the thread running the View. This
10602 * handler can be used to pump events in the UI events queue.
10603 */
10604 public Handler getHandler() {
10605 if (mAttachInfo != null) {
10606 return mAttachInfo.mHandler;
10607 }
10608 return null;
10609 }
10610
10611 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010612 * Gets the view root associated with the View.
10613 * @return The view root, or null if none.
10614 * @hide
10615 */
10616 public ViewRootImpl getViewRootImpl() {
10617 if (mAttachInfo != null) {
10618 return mAttachInfo.mViewRootImpl;
10619 }
10620 return null;
10621 }
10622
10623 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010624 * <p>Causes the Runnable to be added to the message queue.
10625 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010626 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 * @param action The Runnable that will be executed.
10628 *
10629 * @return Returns true if the Runnable was successfully placed in to the
10630 * message queue. Returns false on failure, usually because the
10631 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010632 *
10633 * @see #postDelayed
10634 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010635 */
10636 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010637 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010638 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010639 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010641 // Assume that post will succeed later
10642 ViewRootImpl.getRunQueue().post(action);
10643 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 }
10645
10646 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010647 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010649 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010650 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 * @param action The Runnable that will be executed.
10652 * @param delayMillis The delay (in milliseconds) until the Runnable
10653 * will be executed.
10654 *
10655 * @return true if the Runnable was successfully placed in to the
10656 * message queue. Returns false on failure, usually because the
10657 * looper processing the message queue is exiting. Note that a
10658 * result of true does not mean the Runnable will be processed --
10659 * if the looper is quit before the delivery time of the message
10660 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010661 *
10662 * @see #post
10663 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 */
10665 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010666 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010667 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010668 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010670 // Assume that post will succeed later
10671 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10672 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673 }
10674
10675 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010676 * <p>Causes the Runnable to execute on the next animation time step.
10677 * The runnable will be run on the user interface thread.</p>
10678 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010679 * @param action The Runnable that will be executed.
10680 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010681 * @see #postOnAnimationDelayed
10682 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010683 */
10684 public void postOnAnimation(Runnable action) {
10685 final AttachInfo attachInfo = mAttachInfo;
10686 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010687 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10688 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010689 } else {
10690 // Assume that post will succeed later
10691 ViewRootImpl.getRunQueue().post(action);
10692 }
10693 }
10694
10695 /**
10696 * <p>Causes the Runnable to execute on the next animation time step,
10697 * after the specified amount of time elapses.
10698 * The runnable will be run on the user interface thread.</p>
10699 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010700 * @param action The Runnable that will be executed.
10701 * @param delayMillis The delay (in milliseconds) until the Runnable
10702 * will be executed.
10703 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010704 * @see #postOnAnimation
10705 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010706 */
10707 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10708 final AttachInfo attachInfo = mAttachInfo;
10709 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010710 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10711 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010712 } else {
10713 // Assume that post will succeed later
10714 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10715 }
10716 }
10717
10718 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010719 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010720 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 * @param action The Runnable to remove from the message handling queue
10722 *
10723 * @return true if this view could ask the Handler to remove the Runnable,
10724 * false otherwise. When the returned value is true, the Runnable
10725 * may or may not have been actually removed from the message queue
10726 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010727 *
10728 * @see #post
10729 * @see #postDelayed
10730 * @see #postOnAnimation
10731 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 */
10733 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010734 if (action != null) {
10735 final AttachInfo attachInfo = mAttachInfo;
10736 if (attachInfo != null) {
10737 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010738 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10739 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010740 } else {
10741 // Assume that post will succeed later
10742 ViewRootImpl.getRunQueue().removeCallbacks(action);
10743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 return true;
10746 }
10747
10748 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010749 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10750 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 *
Romain Guye63a4f32011-08-11 11:33:31 -070010752 * <p>This method can be invoked from outside of the UI thread
10753 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010756 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 */
10758 public void postInvalidate() {
10759 postInvalidateDelayed(0);
10760 }
10761
10762 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010763 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10764 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010765 *
Romain Guye63a4f32011-08-11 11:33:31 -070010766 * <p>This method can be invoked from outside of the UI thread
10767 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 *
10769 * @param left The left coordinate of the rectangle to invalidate.
10770 * @param top The top coordinate of the rectangle to invalidate.
10771 * @param right The right coordinate of the rectangle to invalidate.
10772 * @param bottom The bottom coordinate of the rectangle to invalidate.
10773 *
10774 * @see #invalidate(int, int, int, int)
10775 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010776 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 */
10778 public void postInvalidate(int left, int top, int right, int bottom) {
10779 postInvalidateDelayed(0, left, top, right, bottom);
10780 }
10781
10782 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010783 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10784 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010785 *
Romain Guye63a4f32011-08-11 11:33:31 -070010786 * <p>This method can be invoked from outside of the UI thread
10787 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 *
10789 * @param delayMilliseconds the duration in milliseconds to delay the
10790 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010791 *
10792 * @see #invalidate()
10793 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 */
10795 public void postInvalidateDelayed(long delayMilliseconds) {
10796 // We try only with the AttachInfo because there's no point in invalidating
10797 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010798 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010799 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010800 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 }
10802 }
10803
10804 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010805 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10806 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010807 *
Romain Guye63a4f32011-08-11 11:33:31 -070010808 * <p>This method can be invoked from outside of the UI thread
10809 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 *
10811 * @param delayMilliseconds the duration in milliseconds to delay the
10812 * invalidation by
10813 * @param left The left coordinate of the rectangle to invalidate.
10814 * @param top The top coordinate of the rectangle to invalidate.
10815 * @param right The right coordinate of the rectangle to invalidate.
10816 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010817 *
10818 * @see #invalidate(int, int, int, int)
10819 * @see #invalidate(Rect)
10820 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010821 */
10822 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10823 int right, int bottom) {
10824
10825 // We try only with the AttachInfo because there's no point in invalidating
10826 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010827 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010828 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010829 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010830 info.target = this;
10831 info.left = left;
10832 info.top = top;
10833 info.right = right;
10834 info.bottom = bottom;
10835
Jeff Browna175a5b2012-02-15 19:18:31 -080010836 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010837 }
10838 }
10839
10840 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010841 * <p>Cause an invalidate to happen on the next animation time step, typically the
10842 * next display frame.</p>
10843 *
10844 * <p>This method can be invoked from outside of the UI thread
10845 * only when this View is attached to a window.</p>
10846 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010847 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010848 */
10849 public void postInvalidateOnAnimation() {
10850 // We try only with the AttachInfo because there's no point in invalidating
10851 // if we are not attached to our window
10852 final AttachInfo attachInfo = mAttachInfo;
10853 if (attachInfo != null) {
10854 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10855 }
10856 }
10857
10858 /**
10859 * <p>Cause an invalidate of the specified area to happen on the next animation
10860 * time step, typically the next display frame.</p>
10861 *
10862 * <p>This method can be invoked from outside of the UI thread
10863 * only when this View is attached to a window.</p>
10864 *
10865 * @param left The left coordinate of the rectangle to invalidate.
10866 * @param top The top coordinate of the rectangle to invalidate.
10867 * @param right The right coordinate of the rectangle to invalidate.
10868 * @param bottom The bottom coordinate of the rectangle to invalidate.
10869 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010870 * @see #invalidate(int, int, int, int)
10871 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010872 */
10873 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10874 // We try only with the AttachInfo because there's no point in invalidating
10875 // if we are not attached to our window
10876 final AttachInfo attachInfo = mAttachInfo;
10877 if (attachInfo != null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080010878 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
Jeff Brown6cb7b462012-03-05 13:21:17 -080010879 info.target = this;
10880 info.left = left;
10881 info.top = top;
10882 info.right = right;
10883 info.bottom = bottom;
10884
10885 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10886 }
10887 }
10888
10889 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010890 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10891 * This event is sent at most once every
10892 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10893 */
10894 private void postSendViewScrolledAccessibilityEventCallback() {
10895 if (mSendViewScrolledAccessibilityEvent == null) {
10896 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10897 }
10898 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10899 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10900 postDelayed(mSendViewScrolledAccessibilityEvent,
10901 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10902 }
10903 }
10904
10905 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010906 * Called by a parent to request that a child update its values for mScrollX
10907 * and mScrollY if necessary. This will typically be done if the child is
10908 * animating a scroll using a {@link android.widget.Scroller Scroller}
10909 * object.
10910 */
10911 public void computeScroll() {
10912 }
10913
10914 /**
10915 * <p>Indicate whether the horizontal edges are faded when the view is
10916 * scrolled horizontally.</p>
10917 *
10918 * @return true if the horizontal edges should are faded on scroll, false
10919 * otherwise
10920 *
10921 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010922 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010923 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 */
10925 public boolean isHorizontalFadingEdgeEnabled() {
10926 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10927 }
10928
10929 /**
10930 * <p>Define whether the horizontal edges should be faded when this view
10931 * is scrolled horizontally.</p>
10932 *
10933 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10934 * be faded when the view is scrolled
10935 * horizontally
10936 *
10937 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010938 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010939 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940 */
10941 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10942 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10943 if (horizontalFadingEdgeEnabled) {
10944 initScrollCache();
10945 }
10946
10947 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10948 }
10949 }
10950
10951 /**
10952 * <p>Indicate whether the vertical edges are faded when the view is
10953 * scrolled horizontally.</p>
10954 *
10955 * @return true if the vertical edges should are faded on scroll, false
10956 * otherwise
10957 *
10958 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010959 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010960 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961 */
10962 public boolean isVerticalFadingEdgeEnabled() {
10963 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10964 }
10965
10966 /**
10967 * <p>Define whether the vertical edges should be faded when this view
10968 * is scrolled vertically.</p>
10969 *
10970 * @param verticalFadingEdgeEnabled true if the vertical edges should
10971 * be faded when the view is scrolled
10972 * vertically
10973 *
10974 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010975 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010976 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 */
10978 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10979 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10980 if (verticalFadingEdgeEnabled) {
10981 initScrollCache();
10982 }
10983
10984 mViewFlags ^= FADING_EDGE_VERTICAL;
10985 }
10986 }
10987
10988 /**
10989 * Returns the strength, or intensity, of the top faded edge. The strength is
10990 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10991 * returns 0.0 or 1.0 but no value in between.
10992 *
10993 * Subclasses should override this method to provide a smoother fade transition
10994 * when scrolling occurs.
10995 *
10996 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10997 */
10998 protected float getTopFadingEdgeStrength() {
10999 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
11000 }
11001
11002 /**
11003 * Returns the strength, or intensity, of the bottom faded edge. The strength is
11004 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11005 * returns 0.0 or 1.0 but no value in between.
11006 *
11007 * Subclasses should override this method to provide a smoother fade transition
11008 * when scrolling occurs.
11009 *
11010 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
11011 */
11012 protected float getBottomFadingEdgeStrength() {
11013 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
11014 computeVerticalScrollRange() ? 1.0f : 0.0f;
11015 }
11016
11017 /**
11018 * Returns the strength, or intensity, of the left faded edge. The strength is
11019 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11020 * returns 0.0 or 1.0 but no value in between.
11021 *
11022 * Subclasses should override this method to provide a smoother fade transition
11023 * when scrolling occurs.
11024 *
11025 * @return the intensity of the left fade as a float between 0.0f and 1.0f
11026 */
11027 protected float getLeftFadingEdgeStrength() {
11028 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
11029 }
11030
11031 /**
11032 * Returns the strength, or intensity, of the right faded edge. The strength is
11033 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
11034 * returns 0.0 or 1.0 but no value in between.
11035 *
11036 * Subclasses should override this method to provide a smoother fade transition
11037 * when scrolling occurs.
11038 *
11039 * @return the intensity of the right fade as a float between 0.0f and 1.0f
11040 */
11041 protected float getRightFadingEdgeStrength() {
11042 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
11043 computeHorizontalScrollRange() ? 1.0f : 0.0f;
11044 }
11045
11046 /**
11047 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
11048 * scrollbar is not drawn by default.</p>
11049 *
11050 * @return true if the horizontal scrollbar should be painted, false
11051 * otherwise
11052 *
11053 * @see #setHorizontalScrollBarEnabled(boolean)
11054 */
11055 public boolean isHorizontalScrollBarEnabled() {
11056 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11057 }
11058
11059 /**
11060 * <p>Define whether the horizontal scrollbar should be drawn or not. The
11061 * scrollbar is not drawn by default.</p>
11062 *
11063 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
11064 * be painted
11065 *
11066 * @see #isHorizontalScrollBarEnabled()
11067 */
11068 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
11069 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
11070 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011071 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011072 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011073 }
11074 }
11075
11076 /**
11077 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
11078 * scrollbar is not drawn by default.</p>
11079 *
11080 * @return true if the vertical scrollbar should be painted, false
11081 * otherwise
11082 *
11083 * @see #setVerticalScrollBarEnabled(boolean)
11084 */
11085 public boolean isVerticalScrollBarEnabled() {
11086 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
11087 }
11088
11089 /**
11090 * <p>Define whether the vertical scrollbar should be drawn or not. The
11091 * scrollbar is not drawn by default.</p>
11092 *
11093 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11094 * be painted
11095 *
11096 * @see #isVerticalScrollBarEnabled()
11097 */
11098 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11099 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11100 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011101 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011102 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011103 }
11104 }
11105
Adam Powell20232d02010-12-08 21:08:53 -080011106 /**
11107 * @hide
11108 */
11109 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011110 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 }
Joe Malin32736f02011-01-19 16:14:20 -080011112
Mike Cleronfe81d382009-09-28 14:22:16 -070011113 /**
11114 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011115 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011116 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011117 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011118 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011119 */
11120 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11121 initScrollCache();
11122 final ScrollabilityCache scrollabilityCache = mScrollCache;
11123 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011124 if (fadeScrollbars) {
11125 scrollabilityCache.state = ScrollabilityCache.OFF;
11126 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011127 scrollabilityCache.state = ScrollabilityCache.ON;
11128 }
11129 }
Joe Malin32736f02011-01-19 16:14:20 -080011130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 /**
Joe Malin32736f02011-01-19 16:14:20 -080011132 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011133 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011134 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011135 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011136 *
11137 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011138 */
11139 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011140 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011141 }
Joe Malin32736f02011-01-19 16:14:20 -080011142
Mike Cleron52f0a642009-09-28 18:21:37 -070011143 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011144 *
11145 * Returns the delay before scrollbars fade.
11146 *
11147 * @return the delay before scrollbars fade
11148 *
11149 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11150 */
11151 public int getScrollBarDefaultDelayBeforeFade() {
11152 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11153 mScrollCache.scrollBarDefaultDelayBeforeFade;
11154 }
11155
11156 /**
11157 * Define the delay before scrollbars fade.
11158 *
11159 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11160 *
11161 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11162 */
11163 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11164 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11165 }
11166
11167 /**
11168 *
11169 * Returns the scrollbar fade duration.
11170 *
11171 * @return the scrollbar fade duration
11172 *
11173 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11174 */
11175 public int getScrollBarFadeDuration() {
11176 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11177 mScrollCache.scrollBarFadeDuration;
11178 }
11179
11180 /**
11181 * Define the scrollbar fade duration.
11182 *
11183 * @param scrollBarFadeDuration - the scrollbar fade duration
11184 *
11185 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11186 */
11187 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11188 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11189 }
11190
11191 /**
11192 *
11193 * Returns the scrollbar size.
11194 *
11195 * @return the scrollbar size
11196 *
11197 * @attr ref android.R.styleable#View_scrollbarSize
11198 */
11199 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011200 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011201 mScrollCache.scrollBarSize;
11202 }
11203
11204 /**
11205 * Define the scrollbar size.
11206 *
11207 * @param scrollBarSize - the scrollbar size
11208 *
11209 * @attr ref android.R.styleable#View_scrollbarSize
11210 */
11211 public void setScrollBarSize(int scrollBarSize) {
11212 getScrollCache().scrollBarSize = scrollBarSize;
11213 }
11214
11215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11217 * inset. When inset, they add to the padding of the view. And the scrollbars
11218 * can be drawn inside the padding area or on the edge of the view. For example,
11219 * if a view has a background drawable and you want to draw the scrollbars
11220 * inside the padding specified by the drawable, you can use
11221 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11222 * appear at the edge of the view, ignoring the padding, then you can use
11223 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11224 * @param style the style of the scrollbars. Should be one of
11225 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11226 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11227 * @see #SCROLLBARS_INSIDE_OVERLAY
11228 * @see #SCROLLBARS_INSIDE_INSET
11229 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11230 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011231 *
11232 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 */
11234 public void setScrollBarStyle(int style) {
11235 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11236 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011237 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011238 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011239 }
11240 }
11241
11242 /**
11243 * <p>Returns the current scrollbar style.</p>
11244 * @return the current scrollbar style
11245 * @see #SCROLLBARS_INSIDE_OVERLAY
11246 * @see #SCROLLBARS_INSIDE_INSET
11247 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11248 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011249 *
11250 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011251 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011252 @ViewDebug.ExportedProperty(mapping = {
11253 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11254 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11255 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11256 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11257 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 public int getScrollBarStyle() {
11259 return mViewFlags & SCROLLBARS_STYLE_MASK;
11260 }
11261
11262 /**
11263 * <p>Compute the horizontal range that the horizontal scrollbar
11264 * represents.</p>
11265 *
11266 * <p>The range is expressed in arbitrary units that must be the same as the
11267 * units used by {@link #computeHorizontalScrollExtent()} and
11268 * {@link #computeHorizontalScrollOffset()}.</p>
11269 *
11270 * <p>The default range is the drawing width of this view.</p>
11271 *
11272 * @return the total horizontal range represented by the horizontal
11273 * scrollbar
11274 *
11275 * @see #computeHorizontalScrollExtent()
11276 * @see #computeHorizontalScrollOffset()
11277 * @see android.widget.ScrollBarDrawable
11278 */
11279 protected int computeHorizontalScrollRange() {
11280 return getWidth();
11281 }
11282
11283 /**
11284 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11285 * within the horizontal range. This value is used to compute the position
11286 * of the thumb within the scrollbar's track.</p>
11287 *
11288 * <p>The range is expressed in arbitrary units that must be the same as the
11289 * units used by {@link #computeHorizontalScrollRange()} and
11290 * {@link #computeHorizontalScrollExtent()}.</p>
11291 *
11292 * <p>The default offset is the scroll offset of this view.</p>
11293 *
11294 * @return the horizontal offset of the scrollbar's thumb
11295 *
11296 * @see #computeHorizontalScrollRange()
11297 * @see #computeHorizontalScrollExtent()
11298 * @see android.widget.ScrollBarDrawable
11299 */
11300 protected int computeHorizontalScrollOffset() {
11301 return mScrollX;
11302 }
11303
11304 /**
11305 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11306 * within the horizontal range. This value is used to compute the length
11307 * of the thumb within the scrollbar's track.</p>
11308 *
11309 * <p>The range is expressed in arbitrary units that must be the same as the
11310 * units used by {@link #computeHorizontalScrollRange()} and
11311 * {@link #computeHorizontalScrollOffset()}.</p>
11312 *
11313 * <p>The default extent is the drawing width of this view.</p>
11314 *
11315 * @return the horizontal extent of the scrollbar's thumb
11316 *
11317 * @see #computeHorizontalScrollRange()
11318 * @see #computeHorizontalScrollOffset()
11319 * @see android.widget.ScrollBarDrawable
11320 */
11321 protected int computeHorizontalScrollExtent() {
11322 return getWidth();
11323 }
11324
11325 /**
11326 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11327 *
11328 * <p>The range is expressed in arbitrary units that must be the same as the
11329 * units used by {@link #computeVerticalScrollExtent()} and
11330 * {@link #computeVerticalScrollOffset()}.</p>
11331 *
11332 * @return the total vertical range represented by the vertical scrollbar
11333 *
11334 * <p>The default range is the drawing height of this view.</p>
11335 *
11336 * @see #computeVerticalScrollExtent()
11337 * @see #computeVerticalScrollOffset()
11338 * @see android.widget.ScrollBarDrawable
11339 */
11340 protected int computeVerticalScrollRange() {
11341 return getHeight();
11342 }
11343
11344 /**
11345 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11346 * within the horizontal range. This value is used to compute the position
11347 * of the thumb within the scrollbar's track.</p>
11348 *
11349 * <p>The range is expressed in arbitrary units that must be the same as the
11350 * units used by {@link #computeVerticalScrollRange()} and
11351 * {@link #computeVerticalScrollExtent()}.</p>
11352 *
11353 * <p>The default offset is the scroll offset of this view.</p>
11354 *
11355 * @return the vertical offset of the scrollbar's thumb
11356 *
11357 * @see #computeVerticalScrollRange()
11358 * @see #computeVerticalScrollExtent()
11359 * @see android.widget.ScrollBarDrawable
11360 */
11361 protected int computeVerticalScrollOffset() {
11362 return mScrollY;
11363 }
11364
11365 /**
11366 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11367 * within the vertical range. This value is used to compute the length
11368 * of the thumb within the scrollbar's track.</p>
11369 *
11370 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011371 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 * {@link #computeVerticalScrollOffset()}.</p>
11373 *
11374 * <p>The default extent is the drawing height of this view.</p>
11375 *
11376 * @return the vertical extent of the scrollbar's thumb
11377 *
11378 * @see #computeVerticalScrollRange()
11379 * @see #computeVerticalScrollOffset()
11380 * @see android.widget.ScrollBarDrawable
11381 */
11382 protected int computeVerticalScrollExtent() {
11383 return getHeight();
11384 }
11385
11386 /**
Adam Powell69159442011-06-13 17:53:06 -070011387 * Check if this view can be scrolled horizontally in a certain direction.
11388 *
11389 * @param direction Negative to check scrolling left, positive to check scrolling right.
11390 * @return true if this view can be scrolled in the specified direction, false otherwise.
11391 */
11392 public boolean canScrollHorizontally(int direction) {
11393 final int offset = computeHorizontalScrollOffset();
11394 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11395 if (range == 0) return false;
11396 if (direction < 0) {
11397 return offset > 0;
11398 } else {
11399 return offset < range - 1;
11400 }
11401 }
11402
11403 /**
11404 * Check if this view can be scrolled vertically in a certain direction.
11405 *
11406 * @param direction Negative to check scrolling up, positive to check scrolling down.
11407 * @return true if this view can be scrolled in the specified direction, false otherwise.
11408 */
11409 public boolean canScrollVertically(int direction) {
11410 final int offset = computeVerticalScrollOffset();
11411 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11412 if (range == 0) return false;
11413 if (direction < 0) {
11414 return offset > 0;
11415 } else {
11416 return offset < range - 1;
11417 }
11418 }
11419
11420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011421 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11422 * scrollbars are painted only if they have been awakened first.</p>
11423 *
11424 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011425 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011426 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011427 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011428 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011429 // scrollbars are drawn only when the animation is running
11430 final ScrollabilityCache cache = mScrollCache;
11431 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011432
Mike Cleronf116bf82009-09-27 19:14:12 -070011433 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011434
Mike Cleronf116bf82009-09-27 19:14:12 -070011435 if (state == ScrollabilityCache.OFF) {
11436 return;
11437 }
Joe Malin32736f02011-01-19 16:14:20 -080011438
Mike Cleronf116bf82009-09-27 19:14:12 -070011439 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011440
Mike Cleronf116bf82009-09-27 19:14:12 -070011441 if (state == ScrollabilityCache.FADING) {
11442 // We're fading -- get our fade interpolation
11443 if (cache.interpolatorValues == null) {
11444 cache.interpolatorValues = new float[1];
11445 }
Joe Malin32736f02011-01-19 16:14:20 -080011446
Mike Cleronf116bf82009-09-27 19:14:12 -070011447 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011448
Mike Cleronf116bf82009-09-27 19:14:12 -070011449 // Stops the animation if we're done
11450 if (cache.scrollBarInterpolator.timeToValues(values) ==
11451 Interpolator.Result.FREEZE_END) {
11452 cache.state = ScrollabilityCache.OFF;
11453 } else {
11454 cache.scrollBar.setAlpha(Math.round(values[0]));
11455 }
Joe Malin32736f02011-01-19 16:14:20 -080011456
11457 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011458 // drawing. We only want this when we're fading so that
11459 // we prevent excessive redraws
11460 invalidate = true;
11461 } else {
11462 // We're just on -- but we may have been fading before so
11463 // reset alpha
11464 cache.scrollBar.setAlpha(255);
11465 }
11466
Joe Malin32736f02011-01-19 16:14:20 -080011467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011468 final int viewFlags = mViewFlags;
11469
11470 final boolean drawHorizontalScrollBar =
11471 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11472 final boolean drawVerticalScrollBar =
11473 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11474 && !isVerticalScrollBarHidden();
11475
11476 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11477 final int width = mRight - mLeft;
11478 final int height = mBottom - mTop;
11479
11480 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011481
Mike Reede8853fc2009-09-04 14:01:48 -040011482 final int scrollX = mScrollX;
11483 final int scrollY = mScrollY;
11484 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11485
Mike Cleronf116bf82009-09-27 19:14:12 -070011486 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011488 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011489 int size = scrollBar.getSize(false);
11490 if (size <= 0) {
11491 size = cache.scrollBarSize;
11492 }
11493
Mike Cleronf116bf82009-09-27 19:14:12 -070011494 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011495 computeHorizontalScrollOffset(),
11496 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011497 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011498 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011499 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011500 left = scrollX + (mPaddingLeft & inside);
11501 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11502 bottom = top + size;
11503 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11504 if (invalidate) {
11505 invalidate(left, top, right, bottom);
11506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011507 }
11508
11509 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011510 int size = scrollBar.getSize(true);
11511 if (size <= 0) {
11512 size = cache.scrollBarSize;
11513 }
11514
Mike Reede8853fc2009-09-04 14:01:48 -040011515 scrollBar.setParameters(computeVerticalScrollRange(),
11516 computeVerticalScrollOffset(),
11517 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011518 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11519 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11520 verticalScrollbarPosition = isLayoutRtl() ?
11521 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11522 }
11523 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011524 default:
Adam Powell20232d02010-12-08 21:08:53 -080011525 case SCROLLBAR_POSITION_RIGHT:
11526 left = scrollX + width - size - (mUserPaddingRight & inside);
11527 break;
11528 case SCROLLBAR_POSITION_LEFT:
11529 left = scrollX + (mUserPaddingLeft & inside);
11530 break;
11531 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011532 top = scrollY + (mPaddingTop & inside);
11533 right = left + size;
11534 bottom = scrollY + height - (mUserPaddingBottom & inside);
11535 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11536 if (invalidate) {
11537 invalidate(left, top, right, bottom);
11538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011539 }
11540 }
11541 }
11542 }
Romain Guy8506ab42009-06-11 17:35:47 -070011543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011544 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011545 * 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 -080011546 * FastScroller is visible.
11547 * @return whether to temporarily hide the vertical scrollbar
11548 * @hide
11549 */
11550 protected boolean isVerticalScrollBarHidden() {
11551 return false;
11552 }
11553
11554 /**
11555 * <p>Draw the horizontal scrollbar if
11556 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011558 * @param canvas the canvas on which to draw the scrollbar
11559 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011560 *
11561 * @see #isHorizontalScrollBarEnabled()
11562 * @see #computeHorizontalScrollRange()
11563 * @see #computeHorizontalScrollExtent()
11564 * @see #computeHorizontalScrollOffset()
11565 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011566 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011567 */
Romain Guy8fb95422010-08-17 18:38:51 -070011568 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11569 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011570 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011571 scrollBar.draw(canvas);
11572 }
Mike Reede8853fc2009-09-04 14:01:48 -040011573
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11576 * returns true.</p>
11577 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 * @param canvas the canvas on which to draw the scrollbar
11579 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 *
11581 * @see #isVerticalScrollBarEnabled()
11582 * @see #computeVerticalScrollRange()
11583 * @see #computeVerticalScrollExtent()
11584 * @see #computeVerticalScrollOffset()
11585 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011586 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011587 */
Romain Guy8fb95422010-08-17 18:38:51 -070011588 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11589 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011590 scrollBar.setBounds(l, t, r, b);
11591 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 }
11593
11594 /**
11595 * Implement this to do your drawing.
11596 *
11597 * @param canvas the canvas on which the background will be drawn
11598 */
11599 protected void onDraw(Canvas canvas) {
11600 }
11601
11602 /*
11603 * Caller is responsible for calling requestLayout if necessary.
11604 * (This allows addViewInLayout to not request a new layout.)
11605 */
11606 void assignParent(ViewParent parent) {
11607 if (mParent == null) {
11608 mParent = parent;
11609 } else if (parent == null) {
11610 mParent = null;
11611 } else {
11612 throw new RuntimeException("view " + this + " being added, but"
11613 + " it already has a parent");
11614 }
11615 }
11616
11617 /**
11618 * This is called when the view is attached to a window. At this point it
11619 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011620 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11621 * however it may be called any time before the first onDraw -- including
11622 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011623 *
11624 * @see #onDetachedFromWindow()
11625 */
11626 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011627 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011628 mParent.requestTransparentRegion(this);
11629 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011630
Dianne Hackborn4702a852012-08-17 15:18:29 -070011631 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011632 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011633 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011634 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011635
Chet Haasea9b61ac2010-12-20 07:40:25 -080011636 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011637
Svetoslav Ganov42138042012-03-20 11:51:39 -070011638 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011639 if (isFocused()) {
11640 InputMethodManager imm = InputMethodManager.peekInstance();
11641 imm.focusIn(this);
11642 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011643
Romain Guyfbb93fa2012-12-03 18:50:35 -080011644 if (mDisplayList != null) {
11645 mDisplayList.setDirty(false);
Romain Guy2a0f2282012-05-08 14:43:12 -070011646 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011647 }
Cibu Johny86666632010-02-22 13:01:02 -080011648
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011649 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011650 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011651 *
11652 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011653 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011654 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011655 if (!needRtlPropertiesResolution()) return;
11656
11657 // Order is important here: LayoutDirection MUST be resolved first
11658 if (!isLayoutDirectionResolved()) {
11659 resolveLayoutDirection();
11660 resolveLayoutParams();
11661 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011662 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011663 if (!isTextDirectionResolved()) {
11664 resolveTextDirection();
11665 }
11666 if (!isTextAlignmentResolved()) {
11667 resolveTextAlignment();
11668 }
11669 if (!isPaddingResolved()) {
11670 resolvePadding();
11671 }
11672 if (!isDrawablesResolved()) {
11673 resolveDrawables();
11674 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011675 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011676 }
11677
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011678 /**
11679 * Reset resolution of all RTL related properties.
11680 *
11681 * @hide
11682 */
11683 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011684 resetResolvedLayoutDirection();
11685 resetResolvedTextDirection();
11686 resetResolvedTextAlignment();
11687 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011688 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011689 }
11690
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011691 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011692 * @see #onScreenStateChanged(int)
11693 */
11694 void dispatchScreenStateChanged(int screenState) {
11695 onScreenStateChanged(screenState);
11696 }
11697
11698 /**
11699 * This method is called whenever the state of the screen this view is
11700 * attached to changes. A state change will usually occurs when the screen
11701 * turns on or off (whether it happens automatically or the user does it
11702 * manually.)
11703 *
11704 * @param screenState The new state of the screen. Can be either
11705 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11706 */
11707 public void onScreenStateChanged(int screenState) {
11708 }
11709
11710 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011711 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11712 */
11713 private boolean hasRtlSupport() {
11714 return mContext.getApplicationInfo().hasRtlSupport();
11715 }
11716
11717 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011718 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11719 * RTL not supported)
11720 */
11721 private boolean isRtlCompatibilityMode() {
11722 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11723 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11724 }
11725
11726 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011727 * @return true if RTL properties need resolution.
11728 */
11729 private boolean needRtlPropertiesResolution() {
11730 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11731 }
11732
11733 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011734 * Called when any RTL property (layout direction or text direction or text alignment) has
11735 * been changed.
11736 *
11737 * Subclasses need to override this method to take care of cached information that depends on the
11738 * resolved layout direction, or to inform child views that inherit their layout direction.
11739 *
11740 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011741 *
11742 * @param layoutDirection the direction of the layout
11743 *
11744 * @see #LAYOUT_DIRECTION_LTR
11745 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011746 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011747 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011748 }
11749
11750 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011751 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11752 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011753 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011754 * @return true if resolution has been done, false otherwise.
11755 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011756 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011757 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011758 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011759 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011760 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011761
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011762 if (hasRtlSupport()) {
11763 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011764 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11765 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011766 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011767 // We cannot resolve yet. LTR is by default and let the resolution happen again
11768 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011769 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011770
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011771 View parent = ((View) mParent);
11772 // Parent has not yet resolved, LTR is still the default
11773 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011774
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011775 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011776 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011777 }
11778 break;
11779 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011780 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011781 break;
11782 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011783 if((LAYOUT_DIRECTION_RTL ==
11784 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011785 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011786 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011787 break;
11788 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011789 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011790 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011791 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011792
11793 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011794 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011795 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011796 }
11797
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011798 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011799 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011800 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011801 * @return true if layout direction resolution can be done otherwise return false.
11802 *
11803 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011804 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011805 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011806 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011807 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011808 return (mParent != null) && (mParent instanceof ViewGroup) &&
11809 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011810 default:
11811 return true;
11812 }
11813 }
11814
11815 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011816 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11817 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011818 *
11819 * @hide
11820 */
11821 public void resetResolvedLayoutDirection() {
11822 // Reset the current resolved bits
11823 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011824 }
11825
11826 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011827 * @return true if the layout direction is inherited.
11828 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011829 * @hide
11830 */
11831 public boolean isLayoutDirectionInherited() {
11832 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11833 }
11834
11835 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011836 * @return true if layout direction has been resolved.
11837 */
11838 private boolean isLayoutDirectionResolved() {
11839 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11840 }
11841
11842 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011843 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011844 *
11845 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011846 */
11847 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011848 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011849 }
11850
11851 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011852 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011853 *
11854 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011855 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011856 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011857 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011858 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11859 // If start / end padding are defined, they will be resolved (hence overriding) to
11860 // left / right or right / left depending on the resolved layout direction.
11861 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011862 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011863 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011864 mUserPaddingLeft = mUserPaddingLeftInitial;
11865 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011866 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011867 switch (resolvedLayoutDirection) {
11868 case LAYOUT_DIRECTION_RTL:
11869 if (mUserPaddingStart != UNDEFINED_PADDING) {
11870 mUserPaddingRight = mUserPaddingStart;
11871 }
11872 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11873 mUserPaddingLeft = mUserPaddingEnd;
11874 }
11875 break;
11876 case LAYOUT_DIRECTION_LTR:
11877 default:
11878 if (mUserPaddingStart != UNDEFINED_PADDING) {
11879 mUserPaddingLeft = mUserPaddingStart;
11880 }
11881 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11882 mUserPaddingRight = mUserPaddingEnd;
11883 }
11884 }
11885
11886 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11887
11888 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11889 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011890 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011891 }
11892
Dianne Hackborn4702a852012-08-17 15:18:29 -070011893 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011894 }
11895
11896 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011897 * Reset the resolved layout direction.
11898 *
11899 * @hide
11900 */
11901 public void resetResolvedPadding() {
11902 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11903 }
11904
11905 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011906 * This is called when the view is detached from a window. At this point it
11907 * no longer has a surface for drawing.
11908 *
11909 * @see #onAttachedToWindow()
11910 */
11911 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011912 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011913
Romain Guya440b002010-02-24 15:57:54 -080011914 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011915 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011916 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011917 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011920
Romain Guya998dff2012-03-23 18:58:36 -070011921 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011922
11923 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011924 if (mDisplayList != null) {
Romain Guyfbb93fa2012-12-03 18:50:35 -080011925 mDisplayList.setDirty(true);
Romain Guy2a0f2282012-05-08 14:43:12 -070011926 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011927 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011928 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011929 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011930 // Should never happen
11931 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011932 }
11933
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011934 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011935
Svetoslav Ganov42138042012-03-20 11:51:39 -070011936 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 }
11938
11939 /**
11940 * @return The number of times this view has been attached to a window
11941 */
11942 protected int getWindowAttachCount() {
11943 return mWindowAttachCount;
11944 }
11945
11946 /**
11947 * Retrieve a unique token identifying the window this view is attached to.
11948 * @return Return the window's token for use in
11949 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11950 */
11951 public IBinder getWindowToken() {
11952 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11953 }
11954
11955 /**
11956 * Retrieve a unique token identifying the top-level "real" window of
11957 * the window that this view is attached to. That is, this is like
11958 * {@link #getWindowToken}, except if the window this view in is a panel
11959 * window (attached to another containing window), then the token of
11960 * the containing window is returned instead.
11961 *
11962 * @return Returns the associated window token, either
11963 * {@link #getWindowToken()} or the containing window's token.
11964 */
11965 public IBinder getApplicationWindowToken() {
11966 AttachInfo ai = mAttachInfo;
11967 if (ai != null) {
11968 IBinder appWindowToken = ai.mPanelParentWindowToken;
11969 if (appWindowToken == null) {
11970 appWindowToken = ai.mWindowToken;
11971 }
11972 return appWindowToken;
11973 }
11974 return null;
11975 }
11976
11977 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011978 * Gets the logical display to which the view's window has been attached.
11979 *
11980 * @return The logical display, or null if the view is not currently attached to a window.
11981 */
11982 public Display getDisplay() {
11983 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11984 }
11985
11986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 * Retrieve private session object this view hierarchy is using to
11988 * communicate with the window manager.
11989 * @return the session object to communicate with the window manager
11990 */
11991 /*package*/ IWindowSession getWindowSession() {
11992 return mAttachInfo != null ? mAttachInfo.mSession : null;
11993 }
11994
11995 /**
11996 * @param info the {@link android.view.View.AttachInfo} to associated with
11997 * this view
11998 */
11999 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
12000 //System.out.println("Attached! " + this);
12001 mAttachInfo = info;
12002 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012003 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070012004 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012005 if (mFloatingTreeObserver != null) {
12006 info.mTreeObserver.merge(mFloatingTreeObserver);
12007 mFloatingTreeObserver = null;
12008 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070012009 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 mAttachInfo.mScrollContainers.add(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 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070012013 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080012015
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012016 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080012017 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012018 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080012019 if (listeners != null && listeners.size() > 0) {
12020 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
12021 // perform the dispatching. The iterator is a safe guard against listeners that
12022 // could mutate the list by calling the various add/remove methods. This prevents
12023 // the array from being modified while we iterate it.
12024 for (OnAttachStateChangeListener listener : listeners) {
12025 listener.onViewAttachedToWindow(this);
12026 }
12027 }
12028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012029 int vis = info.mWindowVisibility;
12030 if (vis != GONE) {
12031 onWindowVisibilityChanged(vis);
12032 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070012033 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012034 // If nobody has evaluated the drawable state yet, then do it now.
12035 refreshDrawableState();
12036 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070012037 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012038 }
12039
12040 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012041 AttachInfo info = mAttachInfo;
12042 if (info != null) {
12043 int vis = info.mWindowVisibility;
12044 if (vis != GONE) {
12045 onWindowVisibilityChanged(GONE);
12046 }
12047 }
12048
12049 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080012050
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012051 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080012052 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012053 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080012054 if (listeners != null && listeners.size() > 0) {
12055 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
12056 // perform the dispatching. The iterator is a safe guard against listeners that
12057 // could mutate the list by calling the various add/remove methods. This prevents
12058 // the array from being modified while we iterate it.
12059 for (OnAttachStateChangeListener listener : listeners) {
12060 listener.onViewDetachedFromWindow(this);
12061 }
12062 }
12063
Dianne Hackborn4702a852012-08-17 15:18:29 -070012064 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012066 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012067 }
Romain Guy01d5edc2011-01-28 11:28:53 -080012068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 mAttachInfo = null;
12070 }
12071
12072 /**
12073 * Store this view hierarchy's frozen state into the given container.
12074 *
12075 * @param container The SparseArray in which to save the view's state.
12076 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012077 * @see #restoreHierarchyState(android.util.SparseArray)
12078 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12079 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 */
12081 public void saveHierarchyState(SparseArray<Parcelable> container) {
12082 dispatchSaveInstanceState(container);
12083 }
12084
12085 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012086 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
12087 * this view and its children. May be overridden to modify how freezing happens to a
12088 * 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 -080012089 *
12090 * @param container The SparseArray in which to save the view's state.
12091 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012092 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12093 * @see #saveHierarchyState(android.util.SparseArray)
12094 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095 */
12096 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12097 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012098 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012100 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012101 throw new IllegalStateException(
12102 "Derived class did not call super.onSaveInstanceState()");
12103 }
12104 if (state != null) {
12105 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12106 // + ": " + state);
12107 container.put(mID, state);
12108 }
12109 }
12110 }
12111
12112 /**
12113 * Hook allowing a view to generate a representation of its internal state
12114 * that can later be used to create a new instance with that same state.
12115 * This state should only contain information that is not persistent or can
12116 * not be reconstructed later. For example, you will never store your
12117 * current position on screen because that will be computed again when a
12118 * new instance of the view is placed in its view hierarchy.
12119 * <p>
12120 * Some examples of things you may store here: the current cursor position
12121 * in a text view (but usually not the text itself since that is stored in a
12122 * content provider or other persistent storage), the currently selected
12123 * item in a list view.
12124 *
12125 * @return Returns a Parcelable object containing the view's current dynamic
12126 * state, or null if there is nothing interesting to save. The
12127 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012128 * @see #onRestoreInstanceState(android.os.Parcelable)
12129 * @see #saveHierarchyState(android.util.SparseArray)
12130 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012131 * @see #setSaveEnabled(boolean)
12132 */
12133 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012134 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012135 return BaseSavedState.EMPTY_STATE;
12136 }
12137
12138 /**
12139 * Restore this view hierarchy's frozen state from the given container.
12140 *
12141 * @param container The SparseArray which holds previously frozen states.
12142 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012143 * @see #saveHierarchyState(android.util.SparseArray)
12144 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12145 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012146 */
12147 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12148 dispatchRestoreInstanceState(container);
12149 }
12150
12151 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012152 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12153 * state for this view and its children. May be overridden to modify how restoring
12154 * happens to a view's children; for example, some views may want to not store state
12155 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 *
12157 * @param container The SparseArray which holds previously saved state.
12158 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012159 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12160 * @see #restoreHierarchyState(android.util.SparseArray)
12161 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 */
12163 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12164 if (mID != NO_ID) {
12165 Parcelable state = container.get(mID);
12166 if (state != null) {
12167 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12168 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012169 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012170 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012171 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 throw new IllegalStateException(
12173 "Derived class did not call super.onRestoreInstanceState()");
12174 }
12175 }
12176 }
12177 }
12178
12179 /**
12180 * Hook allowing a view to re-apply a representation of its internal state that had previously
12181 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12182 * null state.
12183 *
12184 * @param state The frozen state that had previously been returned by
12185 * {@link #onSaveInstanceState}.
12186 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012187 * @see #onSaveInstanceState()
12188 * @see #restoreHierarchyState(android.util.SparseArray)
12189 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 */
12191 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012192 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012194 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12195 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012196 + "when two views of different type have the same id in the same hierarchy. "
12197 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012198 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 }
12200 }
12201
12202 /**
12203 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12204 *
12205 * @return the drawing start time in milliseconds
12206 */
12207 public long getDrawingTime() {
12208 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12209 }
12210
12211 /**
12212 * <p>Enables or disables the duplication of the parent's state into this view. When
12213 * duplication is enabled, this view gets its drawable state from its parent rather
12214 * than from its own internal properties.</p>
12215 *
12216 * <p>Note: in the current implementation, setting this property to true after the
12217 * view was added to a ViewGroup might have no effect at all. This property should
12218 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12219 *
12220 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12221 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012222 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012223 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12224 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012225 *
12226 * @param enabled True to enable duplication of the parent's drawable state, false
12227 * to disable it.
12228 *
12229 * @see #getDrawableState()
12230 * @see #isDuplicateParentStateEnabled()
12231 */
12232 public void setDuplicateParentStateEnabled(boolean enabled) {
12233 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12234 }
12235
12236 /**
12237 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12238 *
12239 * @return True if this view's drawable state is duplicated from the parent,
12240 * false otherwise
12241 *
12242 * @see #getDrawableState()
12243 * @see #setDuplicateParentStateEnabled(boolean)
12244 */
12245 public boolean isDuplicateParentStateEnabled() {
12246 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12247 }
12248
12249 /**
Romain Guy171c5922011-01-06 10:04:23 -080012250 * <p>Specifies the type of layer backing this view. The layer can be
12251 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12252 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012253 *
Romain Guy171c5922011-01-06 10:04:23 -080012254 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12255 * instance that controls how the layer is composed on screen. The following
12256 * properties of the paint are taken into account when composing the layer:</p>
12257 * <ul>
12258 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12259 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12260 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12261 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012262 *
Romain Guy171c5922011-01-06 10:04:23 -080012263 * <p>If this view has an alpha value set to < 1.0 by calling
12264 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12265 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12266 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012267 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012268 *
Romain Guy171c5922011-01-06 10:04:23 -080012269 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12270 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12271 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012272 *
Chet Haased15ebf22012-09-05 11:40:29 -070012273 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012274 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12275 * {@link #LAYER_TYPE_HARDWARE}
12276 * @param paint The paint used to compose the layer. This argument is optional
12277 * and can be null. It is ignored when the layer type is
12278 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012279 *
12280 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012281 * @see #LAYER_TYPE_NONE
12282 * @see #LAYER_TYPE_SOFTWARE
12283 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012284 * @see #setAlpha(float)
12285 *
Romain Guy171c5922011-01-06 10:04:23 -080012286 * @attr ref android.R.styleable#View_layerType
12287 */
12288 public void setLayerType(int layerType, Paint paint) {
12289 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012290 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012291 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12292 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012293
Romain Guyd6cd5722011-01-17 14:42:41 -080012294 if (layerType == mLayerType) {
12295 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12296 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012297 invalidateParentCaches();
12298 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012299 }
12300 return;
12301 }
Romain Guy171c5922011-01-06 10:04:23 -080012302
12303 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012304 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012305 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012306 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012307 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012308 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012309 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012310 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012311 default:
12312 break;
Romain Guy171c5922011-01-06 10:04:23 -080012313 }
12314
12315 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012316 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12317 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12318 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012319
Romain Guy0fd89bf2011-01-26 15:41:30 -080012320 invalidateParentCaches();
12321 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012322 }
12323
12324 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012325 * Updates the {@link Paint} object used with the current layer (used only if the current
12326 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12327 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12328 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12329 * ensure that the view gets redrawn immediately.
12330 *
12331 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12332 * instance that controls how the layer is composed on screen. The following
12333 * properties of the paint are taken into account when composing the layer:</p>
12334 * <ul>
12335 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12336 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12337 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12338 * </ul>
12339 *
12340 * <p>If this view has an alpha value set to < 1.0 by calling
12341 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12342 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12343 * equivalent to setting a hardware layer on this view and providing a paint with
12344 * the desired alpha value.</p>
12345 *
12346 * @param paint The paint used to compose the layer. This argument is optional
12347 * and can be null. It is ignored when the layer type is
12348 * {@link #LAYER_TYPE_NONE}
12349 *
12350 * @see #setLayerType(int, android.graphics.Paint)
12351 */
12352 public void setLayerPaint(Paint paint) {
12353 int layerType = getLayerType();
12354 if (layerType != LAYER_TYPE_NONE) {
12355 mLayerPaint = paint == null ? new Paint() : paint;
12356 if (layerType == LAYER_TYPE_HARDWARE) {
12357 HardwareLayer layer = getHardwareLayer();
12358 if (layer != null) {
12359 layer.setLayerPaint(paint);
12360 }
12361 invalidateViewProperty(false, false);
12362 } else {
12363 invalidate();
12364 }
12365 }
12366 }
12367
12368 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012369 * Indicates whether this view has a static layer. A view with layer type
12370 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12371 * dynamic.
12372 */
12373 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012374 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012375 }
12376
12377 /**
Romain Guy171c5922011-01-06 10:04:23 -080012378 * Indicates what type of layer is currently associated with this view. By default
12379 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12380 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12381 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012382 *
Romain Guy171c5922011-01-06 10:04:23 -080012383 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12384 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012385 *
12386 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012387 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012388 * @see #LAYER_TYPE_NONE
12389 * @see #LAYER_TYPE_SOFTWARE
12390 * @see #LAYER_TYPE_HARDWARE
12391 */
12392 public int getLayerType() {
12393 return mLayerType;
12394 }
Joe Malin32736f02011-01-19 16:14:20 -080012395
Romain Guy6c319ca2011-01-11 14:29:25 -080012396 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012397 * Forces this view's layer to be created and this view to be rendered
12398 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12399 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012400 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012401 * This method can for instance be used to render a view into its layer before
12402 * starting an animation. If this view is complex, rendering into the layer
12403 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012404 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012405 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012406 *
12407 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012408 */
12409 public void buildLayer() {
12410 if (mLayerType == LAYER_TYPE_NONE) return;
12411
12412 if (mAttachInfo == null) {
12413 throw new IllegalStateException("This view must be attached to a window first");
12414 }
12415
12416 switch (mLayerType) {
12417 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012418 if (mAttachInfo.mHardwareRenderer != null &&
12419 mAttachInfo.mHardwareRenderer.isEnabled() &&
12420 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012421 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012422 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012423 break;
12424 case LAYER_TYPE_SOFTWARE:
12425 buildDrawingCache(true);
12426 break;
12427 }
12428 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012429
Romain Guyf1ae1062011-03-02 18:16:04 -080012430 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012431 * <p>Returns a hardware layer that can be used to draw this view again
12432 * without executing its draw method.</p>
12433 *
12434 * @return A HardwareLayer ready to render, or null if an error occurred.
12435 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012436 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012437 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12438 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012439 return null;
12440 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012441
Romain Guy9c4b79a2011-11-10 19:23:58 -080012442 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012443
12444 final int width = mRight - mLeft;
12445 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012446
Romain Guy6c319ca2011-01-11 14:29:25 -080012447 if (width == 0 || height == 0) {
12448 return null;
12449 }
12450
Dianne Hackborn4702a852012-08-17 15:18:29 -070012451 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012452 if (mHardwareLayer == null) {
12453 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12454 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012455 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012456 } else {
12457 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012458 if (mHardwareLayer.resize(width, height)) {
12459 mLocalDirtyRect.set(0, 0, width, height);
12460 }
Romain Guy846a5332012-07-11 17:44:57 -070012461 }
12462
12463 // This should not be necessary but applications that change
12464 // the parameters of their background drawable without calling
12465 // this.setBackground(Drawable) can leave the view in a bad state
12466 // (for instance isOpaque() returns true, but the background is
12467 // not opaque.)
12468 computeOpaqueFlags();
12469
12470 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012471 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012472 mHardwareLayer.setOpaque(opaque);
12473 mLocalDirtyRect.set(0, 0, width, height);
12474 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012475 }
12476
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012477 // The layer is not valid if the underlying GPU resources cannot be allocated
12478 if (!mHardwareLayer.isValid()) {
12479 return null;
12480 }
12481
Romain Guy11cb6422012-09-21 00:39:43 -070012482 mHardwareLayer.setLayerPaint(mLayerPaint);
12483 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12484 ViewRootImpl viewRoot = getViewRootImpl();
12485 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12486
Michael Jurka7e52caf2012-03-06 15:57:06 -080012487 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012488 }
12489
12490 return mHardwareLayer;
12491 }
Romain Guy171c5922011-01-06 10:04:23 -080012492
Romain Guy589b0bb2011-10-10 13:57:47 -070012493 /**
12494 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012495 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012496 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012497 *
12498 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012499 * @see #LAYER_TYPE_HARDWARE
12500 */
Romain Guya998dff2012-03-23 18:58:36 -070012501 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012502 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012503 AttachInfo info = mAttachInfo;
12504 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012505 info.mHardwareRenderer.isEnabled() &&
12506 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012507 mHardwareLayer.destroy();
12508 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012509
Chet Haase6a2d17f2012-09-30 12:14:13 -070012510 if (mDisplayList != null) {
12511 mDisplayList.reset();
12512 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012513 invalidate(true);
12514 invalidateParentCaches();
12515 }
Romain Guy65b345f2011-07-27 18:51:50 -070012516 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012517 }
Romain Guy65b345f2011-07-27 18:51:50 -070012518 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012519 }
12520
Romain Guy171c5922011-01-06 10:04:23 -080012521 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012522 * Destroys all hardware rendering resources. This method is invoked
12523 * when the system needs to reclaim resources. Upon execution of this
12524 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012525 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012526 * Note: you <strong>must</strong> call
12527 * <code>super.destroyHardwareResources()</code> when overriding
12528 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012529 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012530 * @hide
12531 */
12532 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012533 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012534 }
12535
12536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012537 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12538 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12539 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12540 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12541 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12542 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012543 *
Romain Guy171c5922011-01-06 10:04:23 -080012544 * <p>Enabling the drawing cache is similar to
12545 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012546 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12547 * drawing cache has no effect on rendering because the system uses a different mechanism
12548 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12549 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12550 * for information on how to enable software and hardware layers.</p>
12551 *
12552 * <p>This API can be used to manually generate
12553 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12554 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012555 *
12556 * @param enabled true to enable the drawing cache, false otherwise
12557 *
12558 * @see #isDrawingCacheEnabled()
12559 * @see #getDrawingCache()
12560 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012561 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012562 */
12563 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012564 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012565 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12566 }
12567
12568 /**
12569 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12570 *
12571 * @return true if the drawing cache is enabled
12572 *
12573 * @see #setDrawingCacheEnabled(boolean)
12574 * @see #getDrawingCache()
12575 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012576 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012577 public boolean isDrawingCacheEnabled() {
12578 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12579 }
12580
12581 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012582 * Debugging utility which recursively outputs the dirty state of a view and its
12583 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012584 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012585 * @hide
12586 */
Romain Guy676b1732011-02-14 14:45:33 -080012587 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012588 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012589 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12590 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12591 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12592 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012593 if (clear) {
12594 mPrivateFlags &= clearMask;
12595 }
12596 if (this instanceof ViewGroup) {
12597 ViewGroup parent = (ViewGroup) this;
12598 final int count = parent.getChildCount();
12599 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012600 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012601 child.outputDirtyFlags(indent + " ", clear, clearMask);
12602 }
12603 }
12604 }
12605
12606 /**
12607 * This method is used by ViewGroup to cause its children to restore or recreate their
12608 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12609 * to recreate its own display list, which would happen if it went through the normal
12610 * draw/dispatchDraw mechanisms.
12611 *
12612 * @hide
12613 */
12614 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012615
12616 /**
12617 * A view that is not attached or hardware accelerated cannot create a display list.
12618 * This method checks these conditions and returns the appropriate result.
12619 *
12620 * @return true if view has the ability to create a display list, false otherwise.
12621 *
12622 * @hide
12623 */
12624 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012625 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012626 }
Joe Malin32736f02011-01-19 16:14:20 -080012627
Chet Haasedaf98e92011-01-10 14:10:36 -080012628 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012629 * @return The HardwareRenderer associated with that view or null if hardware rendering
12630 * is not supported or this this has not been attached to a window.
12631 *
12632 * @hide
12633 */
12634 public HardwareRenderer getHardwareRenderer() {
12635 if (mAttachInfo != null) {
12636 return mAttachInfo.mHardwareRenderer;
12637 }
12638 return null;
12639 }
12640
12641 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012642 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12643 * Otherwise, the same display list will be returned (after having been rendered into
12644 * along the way, depending on the invalidation state of the view).
12645 *
12646 * @param displayList The previous version of this displayList, could be null.
12647 * @param isLayer Whether the requester of the display list is a layer. If so,
12648 * the view will avoid creating a layer inside the resulting display list.
12649 * @return A new or reused DisplayList object.
12650 */
12651 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12652 if (!canHaveDisplayList()) {
12653 return null;
12654 }
12655
Dianne Hackborn4702a852012-08-17 15:18:29 -070012656 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012657 displayList == null || !displayList.isValid() ||
12658 (!isLayer && mRecreateDisplayList))) {
12659 // Don't need to recreate the display list, just need to tell our
12660 // children to restore/recreate theirs
12661 if (displayList != null && displayList.isValid() &&
12662 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012663 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12664 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012665 dispatchGetDisplayList();
12666
12667 return displayList;
12668 }
12669
12670 if (!isLayer) {
12671 // If we got here, we're recreating it. Mark it as such to ensure that
12672 // we copy in child display lists into ours in drawChild()
12673 mRecreateDisplayList = true;
12674 }
12675 if (displayList == null) {
12676 final String name = getClass().getSimpleName();
12677 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12678 // If we're creating a new display list, make sure our parent gets invalidated
12679 // since they will need to recreate their display list to account for this
12680 // new child display list.
12681 invalidateParentCaches();
12682 }
12683
12684 boolean caching = false;
12685 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012686 int width = mRight - mLeft;
12687 int height = mBottom - mTop;
12688
12689 try {
12690 canvas.setViewport(width, height);
12691 // The dirty rect should always be null for a display list
12692 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012693 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012694 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012695 if (layerType == LAYER_TYPE_HARDWARE) {
12696 final HardwareLayer layer = getHardwareLayer();
12697 if (layer != null && layer.isValid()) {
12698 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12699 } else {
12700 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12701 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12702 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12703 }
12704 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012705 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012706 buildDrawingCache(true);
12707 Bitmap cache = getDrawingCache(true);
12708 if (cache != null) {
12709 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12710 caching = true;
12711 }
Chet Haasea1cff502012-02-21 13:43:44 -080012712 }
Chet Haasea1cff502012-02-21 13:43:44 -080012713 } else {
12714
12715 computeScroll();
12716
Chet Haasea1cff502012-02-21 13:43:44 -080012717 canvas.translate(-mScrollX, -mScrollY);
12718 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012719 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12720 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012721 }
12722
12723 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012724 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012725 dispatchDraw(canvas);
12726 } else {
12727 draw(canvas);
12728 }
12729 }
12730 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012731 canvas.onPostDraw();
12732
12733 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012734 displayList.setCaching(caching);
12735 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012736 displayList.setLeftTopRightBottom(0, 0, width, height);
12737 } else {
12738 setDisplayListProperties(displayList);
12739 }
12740 }
12741 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012742 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12743 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012744 }
12745
12746 return displayList;
12747 }
12748
12749 /**
12750 * Get the DisplayList for the HardwareLayer
12751 *
12752 * @param layer The HardwareLayer whose DisplayList we want
12753 * @return A DisplayList fopr the specified HardwareLayer
12754 */
12755 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12756 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12757 layer.setDisplayList(displayList);
12758 return displayList;
12759 }
12760
12761
12762 /**
Romain Guyb051e892010-09-28 19:09:36 -070012763 * <p>Returns a display list that can be used to draw this view again
12764 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012765 *
Romain Guyb051e892010-09-28 19:09:36 -070012766 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012767 *
12768 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012769 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012770 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012771 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012772 return mDisplayList;
12773 }
12774
Romain Guy38c2ece2012-05-24 14:20:56 -070012775 private void clearDisplayList() {
12776 if (mDisplayList != null) {
12777 mDisplayList.invalidate();
12778 mDisplayList.clear();
12779 }
12780 }
12781
Romain Guyb051e892010-09-28 19:09:36 -070012782 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012783 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012784 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012785 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012786 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012787 * @see #getDrawingCache(boolean)
12788 */
12789 public Bitmap getDrawingCache() {
12790 return getDrawingCache(false);
12791 }
12792
12793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012794 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12795 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12796 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12797 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12798 * request the drawing cache by calling this method and draw it on screen if the
12799 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012800 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012801 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12802 * this method will create a bitmap of the same size as this view. Because this bitmap
12803 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12804 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12805 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12806 * size than the view. This implies that your application must be able to handle this
12807 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012808 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012809 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12810 * the current density of the screen when the application is in compatibility
12811 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012813 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012814 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815 * @see #setDrawingCacheEnabled(boolean)
12816 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012817 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012818 * @see #destroyDrawingCache()
12819 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012820 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012821 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12822 return null;
12823 }
12824 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012825 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012826 }
Romain Guy02890fd2010-08-06 17:58:44 -070012827 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 }
12829
12830 /**
12831 * <p>Frees the resources used by the drawing cache. If you call
12832 * {@link #buildDrawingCache()} manually without calling
12833 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12834 * should cleanup the cache with this method afterwards.</p>
12835 *
12836 * @see #setDrawingCacheEnabled(boolean)
12837 * @see #buildDrawingCache()
12838 * @see #getDrawingCache()
12839 */
12840 public void destroyDrawingCache() {
12841 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012842 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012843 mDrawingCache = null;
12844 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012845 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012846 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012847 mUnscaledDrawingCache = null;
12848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849 }
12850
12851 /**
12852 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012853 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 * view will always be drawn on top of a solid color.
12855 *
12856 * @param color The background color to use for the drawing cache's bitmap
12857 *
12858 * @see #setDrawingCacheEnabled(boolean)
12859 * @see #buildDrawingCache()
12860 * @see #getDrawingCache()
12861 */
12862 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012863 if (color != mDrawingCacheBackgroundColor) {
12864 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012865 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012867 }
12868
12869 /**
12870 * @see #setDrawingCacheBackgroundColor(int)
12871 *
12872 * @return The background color to used for the drawing cache's bitmap
12873 */
12874 public int getDrawingCacheBackgroundColor() {
12875 return mDrawingCacheBackgroundColor;
12876 }
12877
12878 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012879 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012880 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012881 * @see #buildDrawingCache(boolean)
12882 */
12883 public void buildDrawingCache() {
12884 buildDrawingCache(false);
12885 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012886
Romain Guyfbd8f692009-06-26 14:51:58 -070012887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012888 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12889 *
12890 * <p>If you call {@link #buildDrawingCache()} manually without calling
12891 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12892 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012893 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012894 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12895 * this method will create a bitmap of the same size as this view. Because this bitmap
12896 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12897 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12898 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12899 * size than the view. This implies that your application must be able to handle this
12900 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012901 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012902 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12903 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012904 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012905 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906 *
12907 * @see #getDrawingCache()
12908 * @see #destroyDrawingCache()
12909 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012910 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012911 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012912 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012913 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012914
Romain Guy8506ab42009-06-11 17:35:47 -070012915 int width = mRight - mLeft;
12916 int height = mBottom - mTop;
12917
12918 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012919 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012920
Romain Guye1123222009-06-29 14:24:56 -070012921 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012922 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12923 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925
12926 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012927 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012928 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012929
Romain Guyaf61cc42012-09-30 10:49:15 -070012930 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12931 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012932 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12933 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012934 if (width > 0 && height > 0) {
12935 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12936 + projectedBitmapSize + " bytes, only "
12937 + drawingCacheSize + " available");
12938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012939 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012940 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012941 return;
12942 }
12943
12944 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012945 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012946
12947 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012948 Bitmap.Config quality;
12949 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012950 // Never pick ARGB_4444 because it looks awful
12951 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012952 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12953 case DRAWING_CACHE_QUALITY_AUTO:
12954 quality = Bitmap.Config.ARGB_8888;
12955 break;
12956 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012957 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012958 break;
12959 case DRAWING_CACHE_QUALITY_HIGH:
12960 quality = Bitmap.Config.ARGB_8888;
12961 break;
12962 default:
12963 quality = Bitmap.Config.ARGB_8888;
12964 break;
12965 }
12966 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012967 // Optimization for translucent windows
12968 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012969 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012970 }
12971
12972 // Try to cleanup memory
12973 if (bitmap != null) bitmap.recycle();
12974
12975 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012976 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12977 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012978 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012979 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012980 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012981 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012982 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012983 }
Adam Powell26153a32010-11-08 15:22:27 -080012984 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012985 } catch (OutOfMemoryError e) {
12986 // If there is not enough memory to create the bitmap cache, just
12987 // ignore the issue as bitmap caches are not required to draw the
12988 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012989 if (autoScale) {
12990 mDrawingCache = null;
12991 } else {
12992 mUnscaledDrawingCache = null;
12993 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012994 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012995 return;
12996 }
12997
12998 clear = drawingCacheBackgroundColor != 0;
12999 }
13000
13001 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013002 if (attachInfo != null) {
13003 canvas = attachInfo.mCanvas;
13004 if (canvas == null) {
13005 canvas = new Canvas();
13006 }
13007 canvas.setBitmap(bitmap);
13008 // Temporarily clobber the cached Canvas in case one of our children
13009 // is also using a drawing cache. Without this, the children would
13010 // steal the canvas by attaching their own bitmap to it and bad, bad
13011 // thing would happen (invisible views, corrupted drawings, etc.)
13012 attachInfo.mCanvas = null;
13013 } else {
13014 // This case should hopefully never or seldom happen
13015 canvas = new Canvas(bitmap);
13016 }
13017
13018 if (clear) {
13019 bitmap.eraseColor(drawingCacheBackgroundColor);
13020 }
13021
13022 computeScroll();
13023 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080013024
Romain Guye1123222009-06-29 14:24:56 -070013025 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070013026 final float scale = attachInfo.mApplicationScale;
13027 canvas.scale(scale, scale);
13028 }
Joe Malin32736f02011-01-19 16:14:20 -080013029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013030 canvas.translate(-mScrollX, -mScrollY);
13031
Dianne Hackborn4702a852012-08-17 15:18:29 -070013032 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080013033 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
13034 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013035 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070013036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013037
13038 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013039 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13040 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013041 dispatchDraw(canvas);
13042 } else {
13043 draw(canvas);
13044 }
13045
13046 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013047 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013048
13049 if (attachInfo != null) {
13050 // Restore the cached Canvas for our siblings
13051 attachInfo.mCanvas = canvas;
13052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013053 }
13054 }
13055
13056 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013057 * Create a snapshot of the view into a bitmap. We should probably make
13058 * some form of this public, but should think about the API.
13059 */
Romain Guy223ff5c2010-03-02 17:07:47 -080013060 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013061 int width = mRight - mLeft;
13062 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013063
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013064 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070013065 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013066 width = (int) ((width * scale) + 0.5f);
13067 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080013068
Dianne Hackborndde331c2012-08-03 14:01:57 -070013069 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
13070 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013071 if (bitmap == null) {
13072 throw new OutOfMemoryError();
13073 }
13074
Romain Guyc529d8d2011-09-06 15:01:39 -070013075 Resources resources = getResources();
13076 if (resources != null) {
13077 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
13078 }
Joe Malin32736f02011-01-19 16:14:20 -080013079
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013080 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013081 if (attachInfo != null) {
13082 canvas = attachInfo.mCanvas;
13083 if (canvas == null) {
13084 canvas = new Canvas();
13085 }
13086 canvas.setBitmap(bitmap);
13087 // Temporarily clobber the cached Canvas in case one of our children
13088 // is also using a drawing cache. Without this, the children would
13089 // steal the canvas by attaching their own bitmap to it and bad, bad
13090 // things would happen (invisible views, corrupted drawings, etc.)
13091 attachInfo.mCanvas = null;
13092 } else {
13093 // This case should hopefully never or seldom happen
13094 canvas = new Canvas(bitmap);
13095 }
13096
Romain Guy5bcdff42009-05-14 21:27:18 -070013097 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013098 bitmap.eraseColor(backgroundColor);
13099 }
13100
13101 computeScroll();
13102 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013103 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013104 canvas.translate(-mScrollX, -mScrollY);
13105
Romain Guy5bcdff42009-05-14 21:27:18 -070013106 // Temporarily remove the dirty mask
13107 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013108 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013109
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013110 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013111 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013112 dispatchDraw(canvas);
13113 } else {
13114 draw(canvas);
13115 }
13116
Romain Guy5bcdff42009-05-14 21:27:18 -070013117 mPrivateFlags = flags;
13118
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013119 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013120 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013121
13122 if (attachInfo != null) {
13123 // Restore the cached Canvas for our siblings
13124 attachInfo.mCanvas = canvas;
13125 }
Romain Guy8506ab42009-06-11 17:35:47 -070013126
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013127 return bitmap;
13128 }
13129
13130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013131 * Indicates whether this View is currently in edit mode. A View is usually
13132 * in edit mode when displayed within a developer tool. For instance, if
13133 * this View is being drawn by a visual user interface builder, this method
13134 * should return true.
13135 *
13136 * Subclasses should check the return value of this method to provide
13137 * different behaviors if their normal behavior might interfere with the
13138 * host environment. For instance: the class spawns a thread in its
13139 * constructor, the drawing code relies on device-specific features, etc.
13140 *
13141 * This method is usually checked in the drawing code of custom widgets.
13142 *
13143 * @return True if this View is in edit mode, false otherwise.
13144 */
13145 public boolean isInEditMode() {
13146 return false;
13147 }
13148
13149 /**
13150 * If the View draws content inside its padding and enables fading edges,
13151 * it needs to support padding offsets. Padding offsets are added to the
13152 * fading edges to extend the length of the fade so that it covers pixels
13153 * drawn inside the padding.
13154 *
13155 * Subclasses of this class should override this method if they need
13156 * to draw content inside the padding.
13157 *
13158 * @return True if padding offset must be applied, false otherwise.
13159 *
13160 * @see #getLeftPaddingOffset()
13161 * @see #getRightPaddingOffset()
13162 * @see #getTopPaddingOffset()
13163 * @see #getBottomPaddingOffset()
13164 *
13165 * @since CURRENT
13166 */
13167 protected boolean isPaddingOffsetRequired() {
13168 return false;
13169 }
13170
13171 /**
13172 * Amount by which to extend the left fading region. Called only when
13173 * {@link #isPaddingOffsetRequired()} returns true.
13174 *
13175 * @return The left padding offset in pixels.
13176 *
13177 * @see #isPaddingOffsetRequired()
13178 *
13179 * @since CURRENT
13180 */
13181 protected int getLeftPaddingOffset() {
13182 return 0;
13183 }
13184
13185 /**
13186 * Amount by which to extend the right fading region. Called only when
13187 * {@link #isPaddingOffsetRequired()} returns true.
13188 *
13189 * @return The right padding offset in pixels.
13190 *
13191 * @see #isPaddingOffsetRequired()
13192 *
13193 * @since CURRENT
13194 */
13195 protected int getRightPaddingOffset() {
13196 return 0;
13197 }
13198
13199 /**
13200 * Amount by which to extend the top fading region. Called only when
13201 * {@link #isPaddingOffsetRequired()} returns true.
13202 *
13203 * @return The top padding offset in pixels.
13204 *
13205 * @see #isPaddingOffsetRequired()
13206 *
13207 * @since CURRENT
13208 */
13209 protected int getTopPaddingOffset() {
13210 return 0;
13211 }
13212
13213 /**
13214 * Amount by which to extend the bottom fading region. Called only when
13215 * {@link #isPaddingOffsetRequired()} returns true.
13216 *
13217 * @return The bottom padding offset in pixels.
13218 *
13219 * @see #isPaddingOffsetRequired()
13220 *
13221 * @since CURRENT
13222 */
13223 protected int getBottomPaddingOffset() {
13224 return 0;
13225 }
13226
13227 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013228 * @hide
13229 * @param offsetRequired
13230 */
13231 protected int getFadeTop(boolean offsetRequired) {
13232 int top = mPaddingTop;
13233 if (offsetRequired) top += getTopPaddingOffset();
13234 return top;
13235 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013236
Romain Guyf2fc4602011-07-19 15:20:03 -070013237 /**
13238 * @hide
13239 * @param offsetRequired
13240 */
13241 protected int getFadeHeight(boolean offsetRequired) {
13242 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013243 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013244 return mBottom - mTop - mPaddingBottom - padding;
13245 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013246
Romain Guyf2fc4602011-07-19 15:20:03 -070013247 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013248 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013249 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013250 *
Romain Guy2bffd262010-09-12 17:40:02 -070013251 * <p>Even if this method returns true, it does not mean that every call
13252 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13253 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013254 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013255 * window is hardware accelerated,
13256 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13257 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013258 *
Romain Guy2bffd262010-09-12 17:40:02 -070013259 * @return True if the view is attached to a window and the window is
13260 * hardware accelerated; false in any other case.
13261 */
13262 public boolean isHardwareAccelerated() {
13263 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13264 }
Joe Malin32736f02011-01-19 16:14:20 -080013265
Romain Guy2bffd262010-09-12 17:40:02 -070013266 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013267 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13268 * case of an active Animation being run on the view.
13269 */
13270 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13271 Animation a, boolean scalingRequired) {
13272 Transformation invalidationTransform;
13273 final int flags = parent.mGroupFlags;
13274 final boolean initialized = a.isInitialized();
13275 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013276 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013277 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013278 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013279 onAnimationStart();
13280 }
13281
13282 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13283 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13284 if (parent.mInvalidationTransformation == null) {
13285 parent.mInvalidationTransformation = new Transformation();
13286 }
13287 invalidationTransform = parent.mInvalidationTransformation;
13288 a.getTransformation(drawingTime, invalidationTransform, 1f);
13289 } else {
13290 invalidationTransform = parent.mChildTransformation;
13291 }
Romain Guy393a52c2012-05-22 20:21:08 -070013292
Chet Haasebcca79a2012-02-14 08:45:14 -080013293 if (more) {
13294 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013295 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13296 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13297 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13298 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013299 // The child need to draw an animation, potentially offscreen, so
13300 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013301 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013302 parent.invalidate(mLeft, mTop, mRight, mBottom);
13303 }
13304 } else {
13305 if (parent.mInvalidateRegion == null) {
13306 parent.mInvalidateRegion = new RectF();
13307 }
13308 final RectF region = parent.mInvalidateRegion;
13309 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13310 invalidationTransform);
13311
13312 // The child need to draw an animation, potentially offscreen, so
13313 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013314 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013315
13316 final int left = mLeft + (int) region.left;
13317 final int top = mTop + (int) region.top;
13318 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13319 top + (int) (region.height() + .5f));
13320 }
13321 }
13322 return more;
13323 }
13324
Chet Haasea1cff502012-02-21 13:43:44 -080013325 /**
13326 * This method is called by getDisplayList() when a display list is created or re-rendered.
13327 * It sets or resets the current value of all properties on that display list (resetting is
13328 * necessary when a display list is being re-created, because we need to make sure that
13329 * previously-set transform values
13330 */
13331 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013332 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013333 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013334 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013335 if (mParent instanceof ViewGroup) {
13336 displayList.setClipChildren(
13337 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13338 }
Chet Haase9420abd2012-03-29 16:28:32 -070013339 float alpha = 1;
13340 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13341 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13342 ViewGroup parentVG = (ViewGroup) mParent;
13343 final boolean hasTransform =
13344 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13345 if (hasTransform) {
13346 Transformation transform = parentVG.mChildTransformation;
13347 final int transformType = parentVG.mChildTransformation.getTransformationType();
13348 if (transformType != Transformation.TYPE_IDENTITY) {
13349 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13350 alpha = transform.getAlpha();
13351 }
13352 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13353 displayList.setStaticMatrix(transform.getMatrix());
13354 }
13355 }
13356 }
Chet Haasea1cff502012-02-21 13:43:44 -080013357 }
13358 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013359 alpha *= mTransformationInfo.mAlpha;
13360 if (alpha < 1) {
13361 final int multipliedAlpha = (int) (255 * alpha);
13362 if (onSetAlpha(multipliedAlpha)) {
13363 alpha = 1;
13364 }
13365 }
13366 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013367 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13368 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13369 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13370 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013371 if (mTransformationInfo.mCamera == null) {
13372 mTransformationInfo.mCamera = new Camera();
13373 mTransformationInfo.matrix3D = new Matrix();
13374 }
13375 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013376 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013377 displayList.setPivotX(getPivotX());
13378 displayList.setPivotY(getPivotY());
13379 }
Chet Haase9420abd2012-03-29 16:28:32 -070013380 } else if (alpha < 1) {
13381 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013382 }
13383 }
13384 }
13385
Chet Haasebcca79a2012-02-14 08:45:14 -080013386 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013387 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13388 * This draw() method is an implementation detail and is not intended to be overridden or
13389 * to be called from anywhere else other than ViewGroup.drawChild().
13390 */
13391 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013392 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013393 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013394 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013395 final int flags = parent.mGroupFlags;
13396
Chet Haasea1cff502012-02-21 13:43:44 -080013397 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013398 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013399 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013400 }
13401
13402 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013403 boolean concatMatrix = false;
13404
13405 boolean scalingRequired = false;
13406 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013407 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013408
13409 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013410 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13411 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013412 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013413 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013414 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13415 } else {
13416 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13417 }
13418
Chet Haasebcca79a2012-02-14 08:45:14 -080013419 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013420 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013421 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013422 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013423 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013424 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013425 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013426 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013427 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013428 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013429 mDisplayList != null) {
13430 // No longer animating: clear out old animation matrix
13431 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013432 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013433 }
13434 if (!useDisplayListProperties &&
13435 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13436 final boolean hasTransform =
13437 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13438 if (hasTransform) {
13439 final int transformType = parent.mChildTransformation.getTransformationType();
13440 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13441 parent.mChildTransformation : null;
13442 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13443 }
Chet Haase64a48c12012-02-13 16:33:29 -080013444 }
13445 }
13446
13447 concatMatrix |= !childHasIdentityMatrix;
13448
13449 // Sets the flag as early as possible to allow draw() implementations
13450 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013451 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013452
Romain Guyfbb43212012-08-30 15:19:27 -070013453 if (!concatMatrix &&
13454 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13455 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013456 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013457 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13458 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013459 return more;
13460 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013461 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013462
13463 if (hardwareAccelerated) {
13464 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13465 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013466 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13467 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013468 }
13469
Chet Haase64a48c12012-02-13 16:33:29 -080013470 DisplayList displayList = null;
13471 Bitmap cache = null;
13472 boolean hasDisplayList = false;
13473 if (caching) {
13474 if (!hardwareAccelerated) {
13475 if (layerType != LAYER_TYPE_NONE) {
13476 layerType = LAYER_TYPE_SOFTWARE;
13477 buildDrawingCache(true);
13478 }
13479 cache = getDrawingCache(true);
13480 } else {
13481 switch (layerType) {
13482 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013483 if (useDisplayListProperties) {
13484 hasDisplayList = canHaveDisplayList();
13485 } else {
13486 buildDrawingCache(true);
13487 cache = getDrawingCache(true);
13488 }
Chet Haase64a48c12012-02-13 16:33:29 -080013489 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013490 case LAYER_TYPE_HARDWARE:
13491 if (useDisplayListProperties) {
13492 hasDisplayList = canHaveDisplayList();
13493 }
13494 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013495 case LAYER_TYPE_NONE:
13496 // Delay getting the display list until animation-driven alpha values are
13497 // set up and possibly passed on to the view
13498 hasDisplayList = canHaveDisplayList();
13499 break;
13500 }
13501 }
13502 }
Chet Haasea1cff502012-02-21 13:43:44 -080013503 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013504 if (useDisplayListProperties) {
13505 displayList = getDisplayList();
13506 if (!displayList.isValid()) {
13507 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13508 // to getDisplayList(), the display list will be marked invalid and we should not
13509 // try to use it again.
13510 displayList = null;
13511 hasDisplayList = false;
13512 useDisplayListProperties = false;
13513 }
13514 }
Chet Haase64a48c12012-02-13 16:33:29 -080013515
Chet Haase526057b2012-07-12 17:50:41 -070013516 int sx = 0;
13517 int sy = 0;
13518 if (!hasDisplayList) {
13519 computeScroll();
13520 sx = mScrollX;
13521 sy = mScrollY;
13522 }
13523
Chet Haase64a48c12012-02-13 16:33:29 -080013524 final boolean hasNoCache = cache == null || hasDisplayList;
13525 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13526 layerType != LAYER_TYPE_HARDWARE;
13527
Chet Haasea1cff502012-02-21 13:43:44 -080013528 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013529 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013530 restoreTo = canvas.save();
13531 }
Chet Haase64a48c12012-02-13 16:33:29 -080013532 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013533 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013534 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013535 if (!useDisplayListProperties) {
13536 canvas.translate(mLeft, mTop);
13537 }
Chet Haase64a48c12012-02-13 16:33:29 -080013538 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013539 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013540 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013541 restoreTo = canvas.save();
13542 }
Chet Haase64a48c12012-02-13 16:33:29 -080013543 // mAttachInfo cannot be null, otherwise scalingRequired == false
13544 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13545 canvas.scale(scale, scale);
13546 }
13547 }
13548
Chet Haasea1cff502012-02-21 13:43:44 -080013549 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013550 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013551 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013552 if (transformToApply != null || !childHasIdentityMatrix) {
13553 int transX = 0;
13554 int transY = 0;
13555
13556 if (offsetForScroll) {
13557 transX = -sx;
13558 transY = -sy;
13559 }
13560
13561 if (transformToApply != null) {
13562 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013563 if (useDisplayListProperties) {
13564 displayList.setAnimationMatrix(transformToApply.getMatrix());
13565 } else {
13566 // Undo the scroll translation, apply the transformation matrix,
13567 // then redo the scroll translate to get the correct result.
13568 canvas.translate(-transX, -transY);
13569 canvas.concat(transformToApply.getMatrix());
13570 canvas.translate(transX, transY);
13571 }
Chet Haasea1cff502012-02-21 13:43:44 -080013572 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013573 }
13574
13575 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013576 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013577 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013578 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013579 }
13580 }
13581
Chet Haasea1cff502012-02-21 13:43:44 -080013582 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013583 canvas.translate(-transX, -transY);
13584 canvas.concat(getMatrix());
13585 canvas.translate(transX, transY);
13586 }
13587 }
13588
Chet Haase21433372012-06-05 07:54:09 -070013589 // Deal with alpha if it is or used to be <1
13590 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013591 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013592 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013593 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013594 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013595 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013596 }
Chet Haasea1cff502012-02-21 13:43:44 -080013597 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013598 if (hasNoCache) {
13599 final int multipliedAlpha = (int) (255 * alpha);
13600 if (!onSetAlpha(multipliedAlpha)) {
13601 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013602 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013603 layerType != LAYER_TYPE_NONE) {
13604 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13605 }
Chet Haase9420abd2012-03-29 16:28:32 -070013606 if (useDisplayListProperties) {
13607 displayList.setAlpha(alpha * getAlpha());
13608 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013609 final int scrollX = hasDisplayList ? 0 : sx;
13610 final int scrollY = hasDisplayList ? 0 : sy;
13611 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13612 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013613 }
13614 } else {
13615 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013616 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013617 }
13618 }
13619 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013620 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013621 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013622 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013623 }
13624
Chet Haasea1cff502012-02-21 13:43:44 -080013625 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13626 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013627 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013628 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013629 } else {
13630 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013631 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013632 } else {
13633 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13634 }
13635 }
13636 }
13637
Chet Haase9420abd2012-03-29 16:28:32 -070013638 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013639 displayList = getDisplayList();
13640 if (!displayList.isValid()) {
13641 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13642 // to getDisplayList(), the display list will be marked invalid and we should not
13643 // try to use it again.
13644 displayList = null;
13645 hasDisplayList = false;
13646 }
13647 }
13648
13649 if (hasNoCache) {
13650 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013651 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013652 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013653 if (layer != null && layer.isValid()) {
13654 mLayerPaint.setAlpha((int) (alpha * 255));
13655 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13656 layerRendered = true;
13657 } else {
13658 final int scrollX = hasDisplayList ? 0 : sx;
13659 final int scrollY = hasDisplayList ? 0 : sy;
13660 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013661 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013662 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13663 }
13664 }
13665
13666 if (!layerRendered) {
13667 if (!hasDisplayList) {
13668 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013669 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13670 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013671 dispatchDraw(canvas);
13672 } else {
13673 draw(canvas);
13674 }
13675 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013676 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013677 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013678 }
13679 }
13680 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013681 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013682 Paint cachePaint;
13683
13684 if (layerType == LAYER_TYPE_NONE) {
13685 cachePaint = parent.mCachePaint;
13686 if (cachePaint == null) {
13687 cachePaint = new Paint();
13688 cachePaint.setDither(false);
13689 parent.mCachePaint = cachePaint;
13690 }
Chet Haase9420abd2012-03-29 16:28:32 -070013691 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013692 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013693 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13694 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013695 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013696 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013697 }
13698 } else {
13699 cachePaint = mLayerPaint;
13700 cachePaint.setAlpha((int) (alpha * 255));
13701 }
13702 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13703 }
13704
Chet Haasea1cff502012-02-21 13:43:44 -080013705 if (restoreTo >= 0) {
13706 canvas.restoreToCount(restoreTo);
13707 }
Chet Haase64a48c12012-02-13 16:33:29 -080013708
13709 if (a != null && !more) {
13710 if (!hardwareAccelerated && !a.getFillAfter()) {
13711 onSetAlpha(255);
13712 }
13713 parent.finishAnimatingView(this, a);
13714 }
13715
13716 if (more && hardwareAccelerated) {
13717 // invalidation is the trigger to recreate display lists, so if we're using
13718 // display lists to render, force an invalidate to allow the animation to
13719 // continue drawing another frame
13720 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013721 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013722 // alpha animations should cause the child to recreate its display list
13723 invalidate(true);
13724 }
13725 }
13726
13727 mRecreateDisplayList = false;
13728
13729 return more;
13730 }
13731
13732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013733 * Manually render this view (and all of its children) to the given Canvas.
13734 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013735 * called. When implementing a view, implement
13736 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13737 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738 *
13739 * @param canvas The Canvas to which the View is rendered.
13740 */
13741 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013742 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013743 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013744 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013745 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013747 /*
13748 * Draw traversal performs several drawing steps which must be executed
13749 * in the appropriate order:
13750 *
13751 * 1. Draw the background
13752 * 2. If necessary, save the canvas' layers to prepare for fading
13753 * 3. Draw view's content
13754 * 4. Draw children
13755 * 5. If necessary, draw the fading edges and restore layers
13756 * 6. Draw decorations (scrollbars for instance)
13757 */
13758
13759 // Step 1, draw the background, if needed
13760 int saveCount;
13761
Romain Guy24443ea2009-05-11 11:56:30 -070013762 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013763 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013764 if (background != null) {
13765 final int scrollX = mScrollX;
13766 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013767
Romain Guy24443ea2009-05-11 11:56:30 -070013768 if (mBackgroundSizeChanged) {
13769 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13770 mBackgroundSizeChanged = false;
13771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013772
Romain Guy24443ea2009-05-11 11:56:30 -070013773 if ((scrollX | scrollY) == 0) {
13774 background.draw(canvas);
13775 } else {
13776 canvas.translate(scrollX, scrollY);
13777 background.draw(canvas);
13778 canvas.translate(-scrollX, -scrollY);
13779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013780 }
13781 }
13782
13783 // skip step 2 & 5 if possible (common case)
13784 final int viewFlags = mViewFlags;
13785 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13786 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13787 if (!verticalEdges && !horizontalEdges) {
13788 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013789 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013790
13791 // Step 4, draw the children
13792 dispatchDraw(canvas);
13793
13794 // Step 6, draw decorations (scrollbars)
13795 onDrawScrollBars(canvas);
13796
13797 // we're done...
13798 return;
13799 }
13800
13801 /*
13802 * Here we do the full fledged routine...
13803 * (this is an uncommon case where speed matters less,
13804 * this is why we repeat some of the tests that have been
13805 * done above)
13806 */
13807
13808 boolean drawTop = false;
13809 boolean drawBottom = false;
13810 boolean drawLeft = false;
13811 boolean drawRight = false;
13812
13813 float topFadeStrength = 0.0f;
13814 float bottomFadeStrength = 0.0f;
13815 float leftFadeStrength = 0.0f;
13816 float rightFadeStrength = 0.0f;
13817
13818 // Step 2, save the canvas' layers
13819 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820
13821 final boolean offsetRequired = isPaddingOffsetRequired();
13822 if (offsetRequired) {
13823 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824 }
13825
13826 int left = mScrollX + paddingLeft;
13827 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013828 int top = mScrollY + getFadeTop(offsetRequired);
13829 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013830
13831 if (offsetRequired) {
13832 right += getRightPaddingOffset();
13833 bottom += getBottomPaddingOffset();
13834 }
13835
13836 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013837 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013838 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839
13840 // clip the fade length if top and bottom fades overlap
13841 // overlapping fades produce odd-looking artifacts
13842 if (verticalEdges && (top + length > bottom - length)) {
13843 length = (bottom - top) / 2;
13844 }
13845
13846 // also clip horizontal fades if necessary
13847 if (horizontalEdges && (left + length > right - length)) {
13848 length = (right - left) / 2;
13849 }
13850
13851 if (verticalEdges) {
13852 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013853 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013854 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013855 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013856 }
13857
13858 if (horizontalEdges) {
13859 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013860 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013861 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013862 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013863 }
13864
13865 saveCount = canvas.getSaveCount();
13866
13867 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013868 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013869 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13870
13871 if (drawTop) {
13872 canvas.saveLayer(left, top, right, top + length, null, flags);
13873 }
13874
13875 if (drawBottom) {
13876 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13877 }
13878
13879 if (drawLeft) {
13880 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13881 }
13882
13883 if (drawRight) {
13884 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13885 }
13886 } else {
13887 scrollabilityCache.setFadeColor(solidColor);
13888 }
13889
13890 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013891 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013892
13893 // Step 4, draw the children
13894 dispatchDraw(canvas);
13895
13896 // Step 5, draw the fade effect and restore layers
13897 final Paint p = scrollabilityCache.paint;
13898 final Matrix matrix = scrollabilityCache.matrix;
13899 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900
13901 if (drawTop) {
13902 matrix.setScale(1, fadeHeight * topFadeStrength);
13903 matrix.postTranslate(left, top);
13904 fade.setLocalMatrix(matrix);
13905 canvas.drawRect(left, top, right, top + length, p);
13906 }
13907
13908 if (drawBottom) {
13909 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13910 matrix.postRotate(180);
13911 matrix.postTranslate(left, bottom);
13912 fade.setLocalMatrix(matrix);
13913 canvas.drawRect(left, bottom - length, right, bottom, p);
13914 }
13915
13916 if (drawLeft) {
13917 matrix.setScale(1, fadeHeight * leftFadeStrength);
13918 matrix.postRotate(-90);
13919 matrix.postTranslate(left, top);
13920 fade.setLocalMatrix(matrix);
13921 canvas.drawRect(left, top, left + length, bottom, p);
13922 }
13923
13924 if (drawRight) {
13925 matrix.setScale(1, fadeHeight * rightFadeStrength);
13926 matrix.postRotate(90);
13927 matrix.postTranslate(right, top);
13928 fade.setLocalMatrix(matrix);
13929 canvas.drawRect(right - length, top, right, bottom, p);
13930 }
13931
13932 canvas.restoreToCount(saveCount);
13933
13934 // Step 6, draw decorations (scrollbars)
13935 onDrawScrollBars(canvas);
13936 }
13937
13938 /**
13939 * Override this if your view is known to always be drawn on top of a solid color background,
13940 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13941 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13942 * should be set to 0xFF.
13943 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013944 * @see #setVerticalFadingEdgeEnabled(boolean)
13945 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013946 *
13947 * @return The known solid color background for this view, or 0 if the color may vary
13948 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013949 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950 public int getSolidColor() {
13951 return 0;
13952 }
13953
13954 /**
13955 * Build a human readable string representation of the specified view flags.
13956 *
13957 * @param flags the view flags to convert to a string
13958 * @return a String representing the supplied flags
13959 */
13960 private static String printFlags(int flags) {
13961 String output = "";
13962 int numFlags = 0;
13963 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13964 output += "TAKES_FOCUS";
13965 numFlags++;
13966 }
13967
13968 switch (flags & VISIBILITY_MASK) {
13969 case INVISIBLE:
13970 if (numFlags > 0) {
13971 output += " ";
13972 }
13973 output += "INVISIBLE";
13974 // USELESS HERE numFlags++;
13975 break;
13976 case GONE:
13977 if (numFlags > 0) {
13978 output += " ";
13979 }
13980 output += "GONE";
13981 // USELESS HERE numFlags++;
13982 break;
13983 default:
13984 break;
13985 }
13986 return output;
13987 }
13988
13989 /**
13990 * Build a human readable string representation of the specified private
13991 * view flags.
13992 *
13993 * @param privateFlags the private view flags to convert to a string
13994 * @return a String representing the supplied flags
13995 */
13996 private static String printPrivateFlags(int privateFlags) {
13997 String output = "";
13998 int numFlags = 0;
13999
Dianne Hackborn4702a852012-08-17 15:18:29 -070014000 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014001 output += "WANTS_FOCUS";
14002 numFlags++;
14003 }
14004
Dianne Hackborn4702a852012-08-17 15:18:29 -070014005 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 if (numFlags > 0) {
14007 output += " ";
14008 }
14009 output += "FOCUSED";
14010 numFlags++;
14011 }
14012
Dianne Hackborn4702a852012-08-17 15:18:29 -070014013 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 if (numFlags > 0) {
14015 output += " ";
14016 }
14017 output += "SELECTED";
14018 numFlags++;
14019 }
14020
Dianne Hackborn4702a852012-08-17 15:18:29 -070014021 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014022 if (numFlags > 0) {
14023 output += " ";
14024 }
14025 output += "IS_ROOT_NAMESPACE";
14026 numFlags++;
14027 }
14028
Dianne Hackborn4702a852012-08-17 15:18:29 -070014029 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014030 if (numFlags > 0) {
14031 output += " ";
14032 }
14033 output += "HAS_BOUNDS";
14034 numFlags++;
14035 }
14036
Dianne Hackborn4702a852012-08-17 15:18:29 -070014037 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014038 if (numFlags > 0) {
14039 output += " ";
14040 }
14041 output += "DRAWN";
14042 // USELESS HERE numFlags++;
14043 }
14044 return output;
14045 }
14046
14047 /**
14048 * <p>Indicates whether or not this view's layout will be requested during
14049 * the next hierarchy layout pass.</p>
14050 *
14051 * @return true if the layout will be forced during next layout pass
14052 */
14053 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014054 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055 }
14056
14057 /**
Philip Milne7b757812012-09-19 18:13:44 -070014058 * Return true if o is a ViewGroup that is laying out using optical bounds.
14059 * @hide
14060 */
14061 public static boolean isLayoutModeOptical(Object o) {
14062 return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
14063 }
14064
14065 private boolean setOpticalFrame(int left, int top, int right, int bottom) {
14066 Insets parentInsets = mParent instanceof View ?
14067 ((View) mParent).getOpticalInsets() : Insets.NONE;
14068 Insets childInsets = getOpticalInsets();
14069 return setFrame(
14070 left + parentInsets.left - childInsets.left,
14071 top + parentInsets.top - childInsets.top,
14072 right + parentInsets.left + childInsets.right,
14073 bottom + parentInsets.top + childInsets.bottom);
14074 }
14075
14076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 * Assign a size and position to a view and all of its
14078 * descendants
14079 *
14080 * <p>This is the second phase of the layout mechanism.
14081 * (The first is measuring). In this phase, each parent calls
14082 * layout on all of its children to position them.
14083 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080014084 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 *
Chet Haase9c087442011-01-12 16:20:16 -080014086 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 * Derived classes with children should override
14088 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080014089 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014090 *
14091 * @param l Left position, relative to parent
14092 * @param t Top position, relative to parent
14093 * @param r Right position, relative to parent
14094 * @param b Bottom position, relative to parent
14095 */
Romain Guy5429e1d2010-09-07 12:38:00 -070014096 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080014097 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070014098 int oldL = mLeft;
14099 int oldT = mTop;
14100 int oldB = mBottom;
14101 int oldR = mRight;
Philip Milne7b757812012-09-19 18:13:44 -070014102 boolean changed = isLayoutModeOptical(mParent) ?
14103 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014104 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014106 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070014107
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014108 ListenerInfo li = mListenerInfo;
14109 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070014110 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014111 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070014112 int numListeners = listenersCopy.size();
14113 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014114 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014115 }
14116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014117 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014118 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 }
14120
14121 /**
14122 * Called from layout when this view should
14123 * assign a size and position to each of its children.
14124 *
14125 * Derived classes with children should override
14126 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014127 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 * @param changed This is a new size or position for this view
14129 * @param left Left position, relative to parent
14130 * @param top Top position, relative to parent
14131 * @param right Right position, relative to parent
14132 * @param bottom Bottom position, relative to parent
14133 */
14134 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14135 }
14136
14137 /**
14138 * Assign a size and position to this view.
14139 *
14140 * This is called from layout.
14141 *
14142 * @param left Left position, relative to parent
14143 * @param top Top position, relative to parent
14144 * @param right Right position, relative to parent
14145 * @param bottom Bottom position, relative to parent
14146 * @return true if the new size and position are different than the
14147 * previous ones
14148 * {@hide}
14149 */
14150 protected boolean setFrame(int left, int top, int right, int bottom) {
14151 boolean changed = false;
14152
14153 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014154 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014155 + right + "," + bottom + ")");
14156 }
14157
14158 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14159 changed = true;
14160
14161 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014162 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014164 int oldWidth = mRight - mLeft;
14165 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014166 int newWidth = right - left;
14167 int newHeight = bottom - top;
14168 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14169
14170 // Invalidate our old position
14171 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172
14173 mLeft = left;
14174 mTop = top;
14175 mRight = right;
14176 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014177 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014178 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014180
Dianne Hackborn4702a852012-08-17 15:18:29 -070014181 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183
Chet Haase75755e22011-07-18 17:48:25 -070014184 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014185 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014186 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014187 if (mTransformationInfo != null) {
14188 mTransformationInfo.mMatrixDirty = true;
14189 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14192 }
14193
14194 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14195 // If we are visible, force the DRAWN bit to on so that
14196 // this invalidate will go through (at least to our parent).
14197 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014198 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014199 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014200 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014201 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014202 // parent display list may need to be recreated based on a change in the bounds
14203 // of any child
14204 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014205 }
14206
14207 // Reset drawn bit to original value (invalidate turns it off)
14208 mPrivateFlags |= drawn;
14209
14210 mBackgroundSizeChanged = true;
14211 }
14212 return changed;
14213 }
14214
14215 /**
14216 * Finalize inflating a view from XML. This is called as the last phase
14217 * of inflation, after all child views have been added.
14218 *
14219 * <p>Even if the subclass overrides onFinishInflate, they should always be
14220 * sure to call the super method, so that we get called.
14221 */
14222 protected void onFinishInflate() {
14223 }
14224
14225 /**
14226 * Returns the resources associated with this view.
14227 *
14228 * @return Resources object.
14229 */
14230 public Resources getResources() {
14231 return mResources;
14232 }
14233
14234 /**
14235 * Invalidates the specified Drawable.
14236 *
14237 * @param drawable the drawable to invalidate
14238 */
14239 public void invalidateDrawable(Drawable drawable) {
14240 if (verifyDrawable(drawable)) {
14241 final Rect dirty = drawable.getBounds();
14242 final int scrollX = mScrollX;
14243 final int scrollY = mScrollY;
14244
14245 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14246 dirty.right + scrollX, dirty.bottom + scrollY);
14247 }
14248 }
14249
14250 /**
14251 * Schedules an action on a drawable to occur at a specified time.
14252 *
14253 * @param who the recipient of the action
14254 * @param what the action to run on the drawable
14255 * @param when the time at which the action must occur. Uses the
14256 * {@link SystemClock#uptimeMillis} timebase.
14257 */
14258 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014259 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014260 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014261 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014262 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14263 Choreographer.CALLBACK_ANIMATION, what, who,
14264 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014265 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014266 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 }
14269 }
14270
14271 /**
14272 * Cancels a scheduled action on a drawable.
14273 *
14274 * @param who the recipient of the action
14275 * @param what the action to cancel
14276 */
14277 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014278 if (verifyDrawable(who) && what != null) {
14279 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014280 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14281 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014282 } else {
14283 ViewRootImpl.getRunQueue().removeCallbacks(what);
14284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 }
14286 }
14287
14288 /**
14289 * Unschedule any events associated with the given Drawable. This can be
14290 * used when selecting a new Drawable into a view, so that the previous
14291 * one is completely unscheduled.
14292 *
14293 * @param who The Drawable to unschedule.
14294 *
14295 * @see #drawableStateChanged
14296 */
14297 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014298 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014299 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14300 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014301 }
14302 }
14303
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014304 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014305 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14306 * that the View directionality can and will be resolved before its Drawables.
14307 *
14308 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014309 *
14310 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014311 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014312 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014313 if (canResolveLayoutDirection()) {
14314 if (mBackground != null) {
14315 mBackground.setLayoutDirection(getLayoutDirection());
14316 }
14317 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14318 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014319 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014320 }
14321
14322 /**
14323 * Called when layout direction has been resolved.
14324 *
14325 * The default implementation does nothing.
14326 *
14327 * @param layoutDirection The resolved layout direction.
14328 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014329 * @see #LAYOUT_DIRECTION_LTR
14330 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014331 *
14332 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014333 */
14334 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014335 }
14336
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014337 /**
14338 * @hide
14339 */
14340 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014341 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14342 }
14343
14344 private boolean isDrawablesResolved() {
14345 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14346 }
14347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014348 /**
14349 * If your view subclass is displaying its own Drawable objects, it should
14350 * override this function and return true for any Drawable it is
14351 * displaying. This allows animations for those drawables to be
14352 * scheduled.
14353 *
14354 * <p>Be sure to call through to the super class when overriding this
14355 * function.
14356 *
14357 * @param who The Drawable to verify. Return true if it is one you are
14358 * displaying, else return the result of calling through to the
14359 * super class.
14360 *
14361 * @return boolean If true than the Drawable is being displayed in the
14362 * view; else false and it is not allowed to animate.
14363 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014364 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14365 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014366 */
14367 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014368 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014369 }
14370
14371 /**
14372 * This function is called whenever the state of the view changes in such
14373 * a way that it impacts the state of drawables being shown.
14374 *
14375 * <p>Be sure to call through to the superclass when overriding this
14376 * function.
14377 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014378 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 */
14380 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014381 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014382 if (d != null && d.isStateful()) {
14383 d.setState(getDrawableState());
14384 }
14385 }
14386
14387 /**
14388 * Call this to force a view to update its drawable state. This will cause
14389 * drawableStateChanged to be called on this view. Views that are interested
14390 * in the new state should call getDrawableState.
14391 *
14392 * @see #drawableStateChanged
14393 * @see #getDrawableState
14394 */
14395 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014396 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014397 drawableStateChanged();
14398
14399 ViewParent parent = mParent;
14400 if (parent != null) {
14401 parent.childDrawableStateChanged(this);
14402 }
14403 }
14404
14405 /**
14406 * Return an array of resource IDs of the drawable states representing the
14407 * current state of the view.
14408 *
14409 * @return The current drawable state
14410 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014411 * @see Drawable#setState(int[])
14412 * @see #drawableStateChanged()
14413 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014414 */
14415 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014416 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014417 return mDrawableState;
14418 } else {
14419 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014420 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 return mDrawableState;
14422 }
14423 }
14424
14425 /**
14426 * Generate the new {@link android.graphics.drawable.Drawable} state for
14427 * this view. This is called by the view
14428 * system when the cached Drawable state is determined to be invalid. To
14429 * retrieve the current state, you should use {@link #getDrawableState}.
14430 *
14431 * @param extraSpace if non-zero, this is the number of extra entries you
14432 * would like in the returned array in which you can place your own
14433 * states.
14434 *
14435 * @return Returns an array holding the current {@link Drawable} state of
14436 * the view.
14437 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014438 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014439 */
14440 protected int[] onCreateDrawableState(int extraSpace) {
14441 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14442 mParent instanceof View) {
14443 return ((View) mParent).onCreateDrawableState(extraSpace);
14444 }
14445
14446 int[] drawableState;
14447
14448 int privateFlags = mPrivateFlags;
14449
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014450 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014451 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014452 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14453 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014454 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014455 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014456 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014457 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14458 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014459 // This is set if HW acceleration is requested, even if the current
14460 // process doesn't allow it. This is just to allow app preview
14461 // windows to better match their app.
14462 viewStateIndex |= VIEW_STATE_ACCELERATED;
14463 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014464 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014465
Christopher Tate3d4bf172011-03-28 16:16:46 -070014466 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014467 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14468 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014470 drawableState = VIEW_STATE_SETS[viewStateIndex];
14471
14472 //noinspection ConstantIfStatement
14473 if (false) {
14474 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14475 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014476 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14478 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014479 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014480 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014481 + ": " + Arrays.toString(drawableState));
14482 }
14483
14484 if (extraSpace == 0) {
14485 return drawableState;
14486 }
14487
14488 final int[] fullState;
14489 if (drawableState != null) {
14490 fullState = new int[drawableState.length + extraSpace];
14491 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14492 } else {
14493 fullState = new int[extraSpace];
14494 }
14495
14496 return fullState;
14497 }
14498
14499 /**
14500 * Merge your own state values in <var>additionalState</var> into the base
14501 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014502 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014503 *
14504 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014505 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506 * own additional state values.
14507 *
14508 * @param additionalState The additional state values you would like
14509 * added to <var>baseState</var>; this array is not modified.
14510 *
14511 * @return As a convenience, the <var>baseState</var> array you originally
14512 * passed into the function is returned.
14513 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014514 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014515 */
14516 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14517 final int N = baseState.length;
14518 int i = N - 1;
14519 while (i >= 0 && baseState[i] == 0) {
14520 i--;
14521 }
14522 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14523 return baseState;
14524 }
14525
14526 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014527 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14528 * on all Drawable objects associated with this view.
14529 */
14530 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014531 if (mBackground != null) {
14532 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014533 }
14534 }
14535
14536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014537 * Sets the background color for this view.
14538 * @param color the color of the background
14539 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014540 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014541 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014542 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014543 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014544 computeOpaqueFlags();
Romain Guyf6ae4b82012-11-27 13:57:27 -080014545 mBackgroundResource = 0;
Chet Haase70d4ba12010-10-06 09:46:45 -070014546 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014547 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014549 }
14550
14551 /**
14552 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014553 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014554 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014555 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014556 * @attr ref android.R.styleable#View_background
14557 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014558 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014559 public void setBackgroundResource(int resid) {
14560 if (resid != 0 && resid == mBackgroundResource) {
14561 return;
14562 }
14563
14564 Drawable d= null;
14565 if (resid != 0) {
14566 d = mResources.getDrawable(resid);
14567 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014568 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569
14570 mBackgroundResource = resid;
14571 }
14572
14573 /**
14574 * Set the background to a given Drawable, or remove the background. If the
14575 * background has padding, this View's padding is set to the background's
14576 * padding. However, when a background is removed, this View's padding isn't
14577 * touched. If setting the padding is desired, please use
14578 * {@link #setPadding(int, int, int, int)}.
14579 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014580 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014581 * background
14582 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014583 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014584 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014585 setBackgroundDrawable(background);
14586 }
14587
14588 /**
14589 * @deprecated use {@link #setBackground(Drawable)} instead
14590 */
14591 @Deprecated
14592 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014593 computeOpaqueFlags();
14594
Philip Milne6c8ea062012-04-03 17:38:43 -070014595 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014596 return;
14597 }
14598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014599 boolean requestLayout = false;
14600
14601 mBackgroundResource = 0;
14602
14603 /*
14604 * Regardless of whether we're setting a new background or not, we want
14605 * to clear the previous drawable.
14606 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014607 if (mBackground != null) {
14608 mBackground.setCallback(null);
14609 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014610 }
14611
Philip Milne6c8ea062012-04-03 17:38:43 -070014612 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014613 Rect padding = sThreadLocal.get();
14614 if (padding == null) {
14615 padding = new Rect();
14616 sThreadLocal.set(padding);
14617 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014618 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014619 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014620 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014621 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014622 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014623 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014624 mUserPaddingLeftInitial = padding.right;
14625 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014626 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014627 break;
14628 case LAYOUT_DIRECTION_LTR:
14629 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014630 mUserPaddingLeftInitial = padding.left;
14631 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014632 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014634 }
14635
14636 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14637 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014638 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14639 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014640 requestLayout = true;
14641 }
14642
Philip Milne6c8ea062012-04-03 17:38:43 -070014643 background.setCallback(this);
14644 if (background.isStateful()) {
14645 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014646 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014647 background.setVisible(getVisibility() == VISIBLE, false);
14648 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014649
Dianne Hackborn4702a852012-08-17 15:18:29 -070014650 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14651 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14652 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014653 requestLayout = true;
14654 }
14655 } else {
14656 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014657 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014658
Dianne Hackborn4702a852012-08-17 15:18:29 -070014659 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014660 /*
14661 * This view ONLY drew the background before and we're removing
14662 * the background, so now it won't draw anything
14663 * (hence we SKIP_DRAW)
14664 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014665 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14666 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014667 }
14668
14669 /*
14670 * When the background is set, we try to apply its padding to this
14671 * View. When the background is removed, we don't touch this View's
14672 * padding. This is noted in the Javadocs. Hence, we don't need to
14673 * requestLayout(), the invalidate() below is sufficient.
14674 */
14675
14676 // The old background's minimum size could have affected this
14677 // View's layout, so let's requestLayout
14678 requestLayout = true;
14679 }
14680
Romain Guy8f1344f52009-05-15 16:03:59 -070014681 computeOpaqueFlags();
14682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014683 if (requestLayout) {
14684 requestLayout();
14685 }
14686
14687 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014688 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014689 }
14690
14691 /**
14692 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014694 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014695 *
14696 * @see #setBackground(Drawable)
14697 *
14698 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014699 */
14700 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014701 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014702 }
14703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014704 /**
14705 * Sets the padding. The view may add on the space required to display
14706 * the scrollbars, depending on the style and visibility of the scrollbars.
14707 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14708 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14709 * from the values set in this call.
14710 *
14711 * @attr ref android.R.styleable#View_padding
14712 * @attr ref android.R.styleable#View_paddingBottom
14713 * @attr ref android.R.styleable#View_paddingLeft
14714 * @attr ref android.R.styleable#View_paddingRight
14715 * @attr ref android.R.styleable#View_paddingTop
14716 * @param left the left padding in pixels
14717 * @param top the top padding in pixels
14718 * @param right the right padding in pixels
14719 * @param bottom the bottom padding in pixels
14720 */
14721 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014722 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014723
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014724 mUserPaddingStart = UNDEFINED_PADDING;
14725 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014726
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014727 mUserPaddingLeftInitial = left;
14728 mUserPaddingRightInitial = right;
14729
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014730 internalSetPadding(left, top, right, bottom);
14731 }
14732
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014733 /**
14734 * @hide
14735 */
14736 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014737 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 mUserPaddingRight = right;
14739 mUserPaddingBottom = bottom;
14740
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014741 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014742 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014743
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014744 // Common case is there are no scroll bars.
14745 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014746 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014747 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014748 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014749 switch (mVerticalScrollbarPosition) {
14750 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014751 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014752 left += offset;
14753 } else {
14754 right += offset;
14755 }
14756 break;
Adam Powell20232d02010-12-08 21:08:53 -080014757 case SCROLLBAR_POSITION_RIGHT:
14758 right += offset;
14759 break;
14760 case SCROLLBAR_POSITION_LEFT:
14761 left += offset;
14762 break;
14763 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014764 }
Adam Powell20232d02010-12-08 21:08:53 -080014765 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014766 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14767 ? 0 : getHorizontalScrollbarHeight();
14768 }
14769 }
Romain Guy8506ab42009-06-11 17:35:47 -070014770
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014771 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014772 changed = true;
14773 mPaddingLeft = left;
14774 }
14775 if (mPaddingTop != top) {
14776 changed = true;
14777 mPaddingTop = top;
14778 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014779 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014780 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014781 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014782 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014783 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014784 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014785 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 }
14787
14788 if (changed) {
14789 requestLayout();
14790 }
14791 }
14792
14793 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014794 * Sets the relative padding. The view may add on the space required to display
14795 * the scrollbars, depending on the style and visibility of the scrollbars.
14796 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14797 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14798 * from the values set in this call.
14799 *
14800 * @attr ref android.R.styleable#View_padding
14801 * @attr ref android.R.styleable#View_paddingBottom
14802 * @attr ref android.R.styleable#View_paddingStart
14803 * @attr ref android.R.styleable#View_paddingEnd
14804 * @attr ref android.R.styleable#View_paddingTop
14805 * @param start the start padding in pixels
14806 * @param top the top padding in pixels
14807 * @param end the end padding in pixels
14808 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014809 */
14810 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014811 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014812
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014813 mUserPaddingStart = start;
14814 mUserPaddingEnd = end;
14815
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014816 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014817 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014818 mUserPaddingLeftInitial = end;
14819 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014820 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014821 break;
14822 case LAYOUT_DIRECTION_LTR:
14823 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014824 mUserPaddingLeftInitial = start;
14825 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014826 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014827 }
14828 }
14829
14830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014831 * Returns the top padding of this view.
14832 *
14833 * @return the top padding in pixels
14834 */
14835 public int getPaddingTop() {
14836 return mPaddingTop;
14837 }
14838
14839 /**
14840 * Returns the bottom padding of this view. If there are inset and enabled
14841 * scrollbars, this value may include the space required to display the
14842 * scrollbars as well.
14843 *
14844 * @return the bottom padding in pixels
14845 */
14846 public int getPaddingBottom() {
14847 return mPaddingBottom;
14848 }
14849
14850 /**
14851 * Returns the left padding of this view. If there are inset and enabled
14852 * scrollbars, this value may include the space required to display the
14853 * scrollbars as well.
14854 *
14855 * @return the left padding in pixels
14856 */
14857 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014858 if (!isPaddingResolved()) {
14859 resolvePadding();
14860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014861 return mPaddingLeft;
14862 }
14863
14864 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014865 * Returns the start padding of this view depending on its resolved layout direction.
14866 * If there are inset and enabled scrollbars, this value may include the space
14867 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014868 *
14869 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014870 */
14871 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014872 if (!isPaddingResolved()) {
14873 resolvePadding();
14874 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014875 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014876 mPaddingRight : mPaddingLeft;
14877 }
14878
14879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 * Returns the right padding of this view. If there are inset and enabled
14881 * scrollbars, this value may include the space required to display the
14882 * scrollbars as well.
14883 *
14884 * @return the right padding in pixels
14885 */
14886 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014887 if (!isPaddingResolved()) {
14888 resolvePadding();
14889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014890 return mPaddingRight;
14891 }
14892
14893 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014894 * Returns the end padding of this view depending on its resolved layout direction.
14895 * If there are inset and enabled scrollbars, this value may include the space
14896 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014897 *
14898 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014899 */
14900 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014901 if (!isPaddingResolved()) {
14902 resolvePadding();
14903 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014904 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014905 mPaddingLeft : mPaddingRight;
14906 }
14907
14908 /**
14909 * Return if the padding as been set thru relative values
14910 * {@link #setPaddingRelative(int, int, int, int)} or thru
14911 * @attr ref android.R.styleable#View_paddingStart or
14912 * @attr ref android.R.styleable#View_paddingEnd
14913 *
14914 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014915 */
14916 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014917 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014918 }
14919
Philip Milne7b757812012-09-19 18:13:44 -070014920 Insets computeOpticalInsets() {
14921 return (mBackground == null) ? Insets.NONE : mBackground.getOpticalInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014922 }
14923
14924 /**
14925 * @hide
14926 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014927 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014928 if (isRtlCompatibilityMode()) {
14929 mPaddingLeft = mUserPaddingLeftInitial;
14930 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014931 return;
14932 }
14933 if (isLayoutRtl()) {
14934 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14935 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014936 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014937 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14938 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014939 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014940 }
14941
14942 /**
14943 * @hide
14944 */
Philip Milne7b757812012-09-19 18:13:44 -070014945 public Insets getOpticalInsets() {
14946 if (mLayoutInsets == null) {
14947 mLayoutInsets = computeOpticalInsets();
14948 }
14949 return mLayoutInsets;
Philip Milne1557fd72012-04-04 23:41:34 -070014950 }
14951
14952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014953 * Changes the selection state of this view. A view can be selected or not.
14954 * Note that selection is not the same as focus. Views are typically
14955 * selected in the context of an AdapterView like ListView or GridView;
14956 * the selected view is the view that is highlighted.
14957 *
14958 * @param selected true if the view must be selected, false otherwise
14959 */
14960 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014961 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14962 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014963 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014964 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014965 refreshDrawableState();
14966 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014967 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14968 notifyAccessibilityStateChanged();
14969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014970 }
14971 }
14972
14973 /**
14974 * Dispatch setSelected to all of this View's children.
14975 *
14976 * @see #setSelected(boolean)
14977 *
14978 * @param selected The new selected state
14979 */
14980 protected void dispatchSetSelected(boolean selected) {
14981 }
14982
14983 /**
14984 * Indicates the selection state of this view.
14985 *
14986 * @return true if the view is selected, false otherwise
14987 */
14988 @ViewDebug.ExportedProperty
14989 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014990 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014991 }
14992
14993 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014994 * Changes the activated state of this view. A view can be activated or not.
14995 * Note that activation is not the same as selection. Selection is
14996 * a transient property, representing the view (hierarchy) the user is
14997 * currently interacting with. Activation is a longer-term state that the
14998 * user can move views in and out of. For example, in a list view with
14999 * single or multiple selection enabled, the views in the current selection
15000 * set are activated. (Um, yeah, we are deeply sorry about the terminology
15001 * here.) The activated state is propagated down to children of the view it
15002 * is set on.
15003 *
15004 * @param activated true if the view must be activated, false otherwise
15005 */
15006 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015007 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
15008 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015009 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015010 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070015011 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015012 }
15013 }
15014
15015 /**
15016 * Dispatch setActivated to all of this View's children.
15017 *
15018 * @see #setActivated(boolean)
15019 *
15020 * @param activated The new activated state
15021 */
15022 protected void dispatchSetActivated(boolean activated) {
15023 }
15024
15025 /**
15026 * Indicates the activation state of this view.
15027 *
15028 * @return true if the view is activated, false otherwise
15029 */
15030 @ViewDebug.ExportedProperty
15031 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015032 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070015033 }
15034
15035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015036 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
15037 * observer can be used to get notifications when global events, like
15038 * layout, happen.
15039 *
15040 * The returned ViewTreeObserver observer is not guaranteed to remain
15041 * valid for the lifetime of this View. If the caller of this method keeps
15042 * a long-lived reference to ViewTreeObserver, it should always check for
15043 * the return value of {@link ViewTreeObserver#isAlive()}.
15044 *
15045 * @return The ViewTreeObserver for this view's hierarchy.
15046 */
15047 public ViewTreeObserver getViewTreeObserver() {
15048 if (mAttachInfo != null) {
15049 return mAttachInfo.mTreeObserver;
15050 }
15051 if (mFloatingTreeObserver == null) {
15052 mFloatingTreeObserver = new ViewTreeObserver();
15053 }
15054 return mFloatingTreeObserver;
15055 }
15056
15057 /**
15058 * <p>Finds the topmost view in the current view hierarchy.</p>
15059 *
15060 * @return the topmost view containing this view
15061 */
15062 public View getRootView() {
15063 if (mAttachInfo != null) {
15064 final View v = mAttachInfo.mRootView;
15065 if (v != null) {
15066 return v;
15067 }
15068 }
Romain Guy8506ab42009-06-11 17:35:47 -070015069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015070 View parent = this;
15071
15072 while (parent.mParent != null && parent.mParent instanceof View) {
15073 parent = (View) parent.mParent;
15074 }
15075
15076 return parent;
15077 }
15078
15079 /**
15080 * <p>Computes the coordinates of this view on the screen. The argument
15081 * must be an array of two integers. After the method returns, the array
15082 * contains the x and y location in that order.</p>
15083 *
15084 * @param location an array of two integers in which to hold the coordinates
15085 */
15086 public void getLocationOnScreen(int[] location) {
15087 getLocationInWindow(location);
15088
15089 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070015090 if (info != null) {
15091 location[0] += info.mWindowLeft;
15092 location[1] += info.mWindowTop;
15093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 }
15095
15096 /**
15097 * <p>Computes the coordinates of this view in its window. The argument
15098 * must be an array of two integers. After the method returns, the array
15099 * contains the x and y location in that order.</p>
15100 *
15101 * @param location an array of two integers in which to hold the coordinates
15102 */
15103 public void getLocationInWindow(int[] location) {
15104 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015105 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015106 }
15107
Gilles Debunne6583ce52011-12-06 18:09:02 -080015108 if (mAttachInfo == null) {
15109 // When the view is not attached to a window, this method does not make sense
15110 location[0] = location[1] = 0;
15111 return;
15112 }
15113
Gilles Debunnecea45132011-11-24 02:19:27 +010015114 float[] position = mAttachInfo.mTmpTransformLocation;
15115 position[0] = position[1] = 0.0f;
15116
15117 if (!hasIdentityMatrix()) {
15118 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015120
Gilles Debunnecea45132011-11-24 02:19:27 +010015121 position[0] += mLeft;
15122 position[1] += mTop;
15123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015124 ViewParent viewParent = mParent;
15125 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015126 final View view = (View) viewParent;
15127
15128 position[0] -= view.mScrollX;
15129 position[1] -= view.mScrollY;
15130
15131 if (!view.hasIdentityMatrix()) {
15132 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015133 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015134
15135 position[0] += view.mLeft;
15136 position[1] += view.mTop;
15137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015139 }
Romain Guy8506ab42009-06-11 17:35:47 -070015140
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015141 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015142 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015143 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15144 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015145 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015146
15147 location[0] = (int) (position[0] + 0.5f);
15148 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015149 }
15150
15151 /**
15152 * {@hide}
15153 * @param id the id of the view to be found
15154 * @return the view of the specified id, null if cannot be found
15155 */
15156 protected View findViewTraversal(int id) {
15157 if (id == mID) {
15158 return this;
15159 }
15160 return null;
15161 }
15162
15163 /**
15164 * {@hide}
15165 * @param tag the tag of the view to be found
15166 * @return the view of specified tag, null if cannot be found
15167 */
15168 protected View findViewWithTagTraversal(Object tag) {
15169 if (tag != null && tag.equals(mTag)) {
15170 return this;
15171 }
15172 return null;
15173 }
15174
15175 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015176 * {@hide}
15177 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015178 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015179 * @return The first view that matches the predicate or null.
15180 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015181 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015182 if (predicate.apply(this)) {
15183 return this;
15184 }
15185 return null;
15186 }
15187
15188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015189 * Look for a child view with the given id. If this view has the given
15190 * id, return this view.
15191 *
15192 * @param id The id to search for.
15193 * @return The view that has the given id in the hierarchy or null
15194 */
15195 public final View findViewById(int id) {
15196 if (id < 0) {
15197 return null;
15198 }
15199 return findViewTraversal(id);
15200 }
15201
15202 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015203 * Finds a view by its unuque and stable accessibility id.
15204 *
15205 * @param accessibilityId The searched accessibility id.
15206 * @return The found view.
15207 */
15208 final View findViewByAccessibilityId(int accessibilityId) {
15209 if (accessibilityId < 0) {
15210 return null;
15211 }
15212 return findViewByAccessibilityIdTraversal(accessibilityId);
15213 }
15214
15215 /**
15216 * Performs the traversal to find a view by its unuque and stable accessibility id.
15217 *
15218 * <strong>Note:</strong>This method does not stop at the root namespace
15219 * boundary since the user can touch the screen at an arbitrary location
15220 * potentially crossing the root namespace bounday which will send an
15221 * accessibility event to accessibility services and they should be able
15222 * to obtain the event source. Also accessibility ids are guaranteed to be
15223 * unique in the window.
15224 *
15225 * @param accessibilityId The accessibility id.
15226 * @return The found view.
15227 */
15228 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15229 if (getAccessibilityViewId() == accessibilityId) {
15230 return this;
15231 }
15232 return null;
15233 }
15234
15235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 * Look for a child view with the given tag. If this view has the given
15237 * tag, return this view.
15238 *
15239 * @param tag The tag to search for, using "tag.equals(getTag())".
15240 * @return The View that has the given tag in the hierarchy or null
15241 */
15242 public final View findViewWithTag(Object tag) {
15243 if (tag == null) {
15244 return null;
15245 }
15246 return findViewWithTagTraversal(tag);
15247 }
15248
15249 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015250 * {@hide}
15251 * Look for a child view that matches the specified predicate.
15252 * If this view matches the predicate, return this view.
15253 *
15254 * @param predicate The predicate to evaluate.
15255 * @return The first view that matches the predicate or null.
15256 */
15257 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015258 return findViewByPredicateTraversal(predicate, null);
15259 }
15260
15261 /**
15262 * {@hide}
15263 * Look for a child view that matches the specified predicate,
15264 * starting with the specified view and its descendents and then
15265 * recusively searching the ancestors and siblings of that view
15266 * until this view is reached.
15267 *
15268 * This method is useful in cases where the predicate does not match
15269 * a single unique view (perhaps multiple views use the same id)
15270 * and we are trying to find the view that is "closest" in scope to the
15271 * starting view.
15272 *
15273 * @param start The view to start from.
15274 * @param predicate The predicate to evaluate.
15275 * @return The first view that matches the predicate or null.
15276 */
15277 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15278 View childToSkip = null;
15279 for (;;) {
15280 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15281 if (view != null || start == this) {
15282 return view;
15283 }
15284
15285 ViewParent parent = start.getParent();
15286 if (parent == null || !(parent instanceof View)) {
15287 return null;
15288 }
15289
15290 childToSkip = start;
15291 start = (View) parent;
15292 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015293 }
15294
15295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015296 * Sets the identifier for this view. The identifier does not have to be
15297 * unique in this view's hierarchy. The identifier should be a positive
15298 * number.
15299 *
15300 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015301 * @see #getId()
15302 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015303 *
15304 * @param id a number used to identify the view
15305 *
15306 * @attr ref android.R.styleable#View_id
15307 */
15308 public void setId(int id) {
15309 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015310 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15311 mID = generateViewId();
15312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015313 }
15314
15315 /**
15316 * {@hide}
15317 *
15318 * @param isRoot true if the view belongs to the root namespace, false
15319 * otherwise
15320 */
15321 public void setIsRootNamespace(boolean isRoot) {
15322 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015323 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015324 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015325 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015326 }
15327 }
15328
15329 /**
15330 * {@hide}
15331 *
15332 * @return true if the view belongs to the root namespace, false otherwise
15333 */
15334 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015335 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015336 }
15337
15338 /**
15339 * Returns this view's identifier.
15340 *
15341 * @return a positive integer used to identify the view or {@link #NO_ID}
15342 * if the view has no ID
15343 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015344 * @see #setId(int)
15345 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015346 * @attr ref android.R.styleable#View_id
15347 */
15348 @ViewDebug.CapturedViewProperty
15349 public int getId() {
15350 return mID;
15351 }
15352
15353 /**
15354 * Returns this view's tag.
15355 *
15356 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015357 *
15358 * @see #setTag(Object)
15359 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015360 */
15361 @ViewDebug.ExportedProperty
15362 public Object getTag() {
15363 return mTag;
15364 }
15365
15366 /**
15367 * Sets the tag associated with this view. A tag can be used to mark
15368 * a view in its hierarchy and does not have to be unique within the
15369 * hierarchy. Tags can also be used to store data within a view without
15370 * resorting to another data structure.
15371 *
15372 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015373 *
15374 * @see #getTag()
15375 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015376 */
15377 public void setTag(final Object tag) {
15378 mTag = tag;
15379 }
15380
15381 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015382 * Returns the tag associated with this view and the specified key.
15383 *
15384 * @param key The key identifying the tag
15385 *
15386 * @return the Object stored in this view as a tag
15387 *
15388 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015389 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015390 */
15391 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015392 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015393 return null;
15394 }
15395
15396 /**
15397 * Sets a tag associated with this view and a key. A tag can be used
15398 * to mark a view in its hierarchy and does not have to be unique within
15399 * the hierarchy. Tags can also be used to store data within a view
15400 * without resorting to another data structure.
15401 *
15402 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015403 * application to ensure it is unique (see the <a
15404 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15405 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015406 * the Android framework or not associated with any package will cause
15407 * an {@link IllegalArgumentException} to be thrown.
15408 *
15409 * @param key The key identifying the tag
15410 * @param tag An Object to tag the view with
15411 *
15412 * @throws IllegalArgumentException If they specified key is not valid
15413 *
15414 * @see #setTag(Object)
15415 * @see #getTag(int)
15416 */
15417 public void setTag(int key, final Object tag) {
15418 // If the package id is 0x00 or 0x01, it's either an undefined package
15419 // or a framework id
15420 if ((key >>> 24) < 2) {
15421 throw new IllegalArgumentException("The key must be an application-specific "
15422 + "resource id.");
15423 }
15424
Adam Powell2b2f6d62011-09-23 11:15:39 -070015425 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015426 }
15427
15428 /**
15429 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15430 * framework id.
15431 *
15432 * @hide
15433 */
15434 public void setTagInternal(int key, Object tag) {
15435 if ((key >>> 24) != 0x1) {
15436 throw new IllegalArgumentException("The key must be a framework-specific "
15437 + "resource id.");
15438 }
15439
Adam Powell2b2f6d62011-09-23 11:15:39 -070015440 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015441 }
15442
Adam Powell2b2f6d62011-09-23 11:15:39 -070015443 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015444 if (mKeyedTags == null) {
15445 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015446 }
15447
Adam Powell7db82ac2011-09-22 19:44:04 -070015448 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015449 }
15450
15451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 * Prints information about this view in the log output, with the tag
15453 * {@link #VIEW_LOG_TAG}.
15454 *
15455 * @hide
15456 */
15457 public void debug() {
15458 debug(0);
15459 }
15460
15461 /**
15462 * Prints information about this view in the log output, with the tag
15463 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15464 * indentation defined by the <code>depth</code>.
15465 *
15466 * @param depth the indentation level
15467 *
15468 * @hide
15469 */
15470 protected void debug(int depth) {
15471 String output = debugIndent(depth - 1);
15472
15473 output += "+ " + this;
15474 int id = getId();
15475 if (id != -1) {
15476 output += " (id=" + id + ")";
15477 }
15478 Object tag = getTag();
15479 if (tag != null) {
15480 output += " (tag=" + tag + ")";
15481 }
15482 Log.d(VIEW_LOG_TAG, output);
15483
Dianne Hackborn4702a852012-08-17 15:18:29 -070015484 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015485 output = debugIndent(depth) + " FOCUSED";
15486 Log.d(VIEW_LOG_TAG, output);
15487 }
15488
15489 output = debugIndent(depth);
15490 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15491 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15492 + "} ";
15493 Log.d(VIEW_LOG_TAG, output);
15494
15495 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15496 || mPaddingBottom != 0) {
15497 output = debugIndent(depth);
15498 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15499 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15500 Log.d(VIEW_LOG_TAG, output);
15501 }
15502
15503 output = debugIndent(depth);
15504 output += "mMeasureWidth=" + mMeasuredWidth +
15505 " mMeasureHeight=" + mMeasuredHeight;
15506 Log.d(VIEW_LOG_TAG, output);
15507
15508 output = debugIndent(depth);
15509 if (mLayoutParams == null) {
15510 output += "BAD! no layout params";
15511 } else {
15512 output = mLayoutParams.debug(output);
15513 }
15514 Log.d(VIEW_LOG_TAG, output);
15515
15516 output = debugIndent(depth);
15517 output += "flags={";
15518 output += View.printFlags(mViewFlags);
15519 output += "}";
15520 Log.d(VIEW_LOG_TAG, output);
15521
15522 output = debugIndent(depth);
15523 output += "privateFlags={";
15524 output += View.printPrivateFlags(mPrivateFlags);
15525 output += "}";
15526 Log.d(VIEW_LOG_TAG, output);
15527 }
15528
15529 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015530 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015531 *
15532 * @param depth the indentation level
15533 * @return a String containing (depth * 2 + 3) * 2 white spaces
15534 *
15535 * @hide
15536 */
15537 protected static String debugIndent(int depth) {
15538 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15539 for (int i = 0; i < (depth * 2) + 3; i++) {
15540 spaces.append(' ').append(' ');
15541 }
15542 return spaces.toString();
15543 }
15544
15545 /**
15546 * <p>Return the offset of the widget's text baseline from the widget's top
15547 * boundary. If this widget does not support baseline alignment, this
15548 * method returns -1. </p>
15549 *
15550 * @return the offset of the baseline within the widget's bounds or -1
15551 * if baseline alignment is not supported
15552 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015553 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015554 public int getBaseline() {
15555 return -1;
15556 }
15557
15558 /**
Chet Haase97140572012-09-13 14:56:47 -070015559 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15560 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15561 * a layout pass.
15562 *
15563 * @return whether the view hierarchy is currently undergoing a layout pass
15564 */
15565 public boolean isInLayout() {
15566 ViewRootImpl viewRoot = getViewRootImpl();
15567 return (viewRoot != null && viewRoot.isInLayout());
15568 }
15569
15570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015571 * Call this when something has changed which has invalidated the
15572 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015573 * tree. This should not be called while the view hierarchy is currently in a layout
15574 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15575 * end of the current layout pass (and then layout will run again) or after the current
15576 * frame is drawn and the next layout occurs.
15577 *
15578 * <p>Subclasses which override this method should call the superclass method to
15579 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015580 */
15581 public void requestLayout() {
Chet Haasecc699b42012-12-13 09:06:55 -080015582 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
15583 // Only trigger request-during-layout logic if this is the view requesting it,
15584 // not the views in its parent hierarchy
15585 ViewRootImpl viewRoot = getViewRootImpl();
15586 if (viewRoot != null && viewRoot.isInLayout()) {
15587 if (!viewRoot.requestLayoutDuringLayout(this)) {
15588 return;
15589 }
15590 }
15591 mAttachInfo.mViewRequestingLayout = this;
Chet Haase97140572012-09-13 14:56:47 -070015592 }
Chet Haasecc699b42012-12-13 09:06:55 -080015593
Dianne Hackborn4702a852012-08-17 15:18:29 -070015594 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15595 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015596
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015597 if (mParent != null && !mParent.isLayoutRequested()) {
15598 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015599 }
Chet Haasecc699b42012-12-13 09:06:55 -080015600 if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
15601 mAttachInfo.mViewRequestingLayout = null;
15602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015603 }
15604
15605 /**
15606 * Forces this view to be laid out during the next layout pass.
15607 * This method does not call requestLayout() or forceLayout()
15608 * on the parent.
15609 */
15610 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015611 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15612 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015613 }
15614
15615 /**
15616 * <p>
15617 * This is called to find out how big a view should be. The parent
15618 * supplies constraint information in the width and height parameters.
15619 * </p>
15620 *
15621 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015622 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015623 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015624 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015625 * </p>
15626 *
15627 *
15628 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15629 * parent
15630 * @param heightMeasureSpec Vertical space requirements as imposed by the
15631 * parent
15632 *
15633 * @see #onMeasure(int, int)
15634 */
15635 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Philip Milne7b757812012-09-19 18:13:44 -070015636 boolean optical = isLayoutModeOptical(this);
15637 if (optical != isLayoutModeOptical(mParent)) {
15638 Insets insets = getOpticalInsets();
15639 int oWidth = insets.left + insets.right;
15640 int oHeight = insets.top + insets.bottom;
15641 widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
15642 heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
15643 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015644 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015645 widthMeasureSpec != mOldWidthMeasureSpec ||
15646 heightMeasureSpec != mOldHeightMeasureSpec) {
15647
15648 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015649 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015650
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015651 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015653 // measure ourselves, this should set the measured dimension flag back
15654 onMeasure(widthMeasureSpec, heightMeasureSpec);
15655
15656 // flag not set, setMeasuredDimension() was not invoked, we raise
15657 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015658 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015659 throw new IllegalStateException("onMeasure() did not set the"
15660 + " measured dimension by calling"
15661 + " setMeasuredDimension()");
15662 }
15663
Dianne Hackborn4702a852012-08-17 15:18:29 -070015664 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015665 }
15666
15667 mOldWidthMeasureSpec = widthMeasureSpec;
15668 mOldHeightMeasureSpec = heightMeasureSpec;
15669 }
15670
15671 /**
15672 * <p>
15673 * Measure the view and its content to determine the measured width and the
15674 * measured height. This method is invoked by {@link #measure(int, int)} and
15675 * should be overriden by subclasses to provide accurate and efficient
15676 * measurement of their contents.
15677 * </p>
15678 *
15679 * <p>
15680 * <strong>CONTRACT:</strong> When overriding this method, you
15681 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15682 * measured width and height of this view. Failure to do so will trigger an
15683 * <code>IllegalStateException</code>, thrown by
15684 * {@link #measure(int, int)}. Calling the superclass'
15685 * {@link #onMeasure(int, int)} is a valid use.
15686 * </p>
15687 *
15688 * <p>
15689 * The base class implementation of measure defaults to the background size,
15690 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15691 * override {@link #onMeasure(int, int)} to provide better measurements of
15692 * their content.
15693 * </p>
15694 *
15695 * <p>
15696 * If this method is overridden, it is the subclass's responsibility to make
15697 * sure the measured height and width are at least the view's minimum height
15698 * and width ({@link #getSuggestedMinimumHeight()} and
15699 * {@link #getSuggestedMinimumWidth()}).
15700 * </p>
15701 *
15702 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15703 * The requirements are encoded with
15704 * {@link android.view.View.MeasureSpec}.
15705 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15706 * The requirements are encoded with
15707 * {@link android.view.View.MeasureSpec}.
15708 *
15709 * @see #getMeasuredWidth()
15710 * @see #getMeasuredHeight()
15711 * @see #setMeasuredDimension(int, int)
15712 * @see #getSuggestedMinimumHeight()
15713 * @see #getSuggestedMinimumWidth()
15714 * @see android.view.View.MeasureSpec#getMode(int)
15715 * @see android.view.View.MeasureSpec#getSize(int)
15716 */
15717 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15718 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15719 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15720 }
15721
15722 /**
15723 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15724 * measured width and measured height. Failing to do so will trigger an
15725 * exception at measurement time.</p>
15726 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015727 * @param measuredWidth The measured width of this view. May be a complex
15728 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15729 * {@link #MEASURED_STATE_TOO_SMALL}.
15730 * @param measuredHeight The measured height of this view. May be a complex
15731 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15732 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015733 */
15734 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
Philip Milne7b757812012-09-19 18:13:44 -070015735 boolean optical = isLayoutModeOptical(this);
15736 if (optical != isLayoutModeOptical(mParent)) {
15737 Insets insets = getOpticalInsets();
15738 int opticalWidth = insets.left + insets.right;
15739 int opticalHeight = insets.top + insets.bottom;
15740
15741 measuredWidth += optical ? opticalWidth : -opticalWidth;
15742 measuredHeight += optical ? opticalHeight : -opticalHeight;
15743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015744 mMeasuredWidth = measuredWidth;
15745 mMeasuredHeight = measuredHeight;
15746
Dianne Hackborn4702a852012-08-17 15:18:29 -070015747 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015748 }
15749
15750 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015751 * Merge two states as returned by {@link #getMeasuredState()}.
15752 * @param curState The current state as returned from a view or the result
15753 * of combining multiple views.
15754 * @param newState The new view state to combine.
15755 * @return Returns a new integer reflecting the combination of the two
15756 * states.
15757 */
15758 public static int combineMeasuredStates(int curState, int newState) {
15759 return curState | newState;
15760 }
15761
15762 /**
15763 * Version of {@link #resolveSizeAndState(int, int, int)}
15764 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15765 */
15766 public static int resolveSize(int size, int measureSpec) {
15767 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15768 }
15769
15770 /**
15771 * Utility to reconcile a desired size and state, with constraints imposed
15772 * by a MeasureSpec. Will take the desired size, unless a different size
15773 * is imposed by the constraints. The returned value is a compound integer,
15774 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15775 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15776 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015777 *
15778 * @param size How big the view wants to be
15779 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015780 * @return Size information bit mask as defined by
15781 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015782 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015783 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015784 int result = size;
15785 int specMode = MeasureSpec.getMode(measureSpec);
15786 int specSize = MeasureSpec.getSize(measureSpec);
15787 switch (specMode) {
15788 case MeasureSpec.UNSPECIFIED:
15789 result = size;
15790 break;
15791 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015792 if (specSize < size) {
15793 result = specSize | MEASURED_STATE_TOO_SMALL;
15794 } else {
15795 result = size;
15796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015797 break;
15798 case MeasureSpec.EXACTLY:
15799 result = specSize;
15800 break;
15801 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015802 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015803 }
15804
15805 /**
15806 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015807 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015808 * by the MeasureSpec.
15809 *
15810 * @param size Default size for this view
15811 * @param measureSpec Constraints imposed by the parent
15812 * @return The size this view should be.
15813 */
15814 public static int getDefaultSize(int size, int measureSpec) {
15815 int result = size;
15816 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015817 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015818
15819 switch (specMode) {
15820 case MeasureSpec.UNSPECIFIED:
15821 result = size;
15822 break;
15823 case MeasureSpec.AT_MOST:
15824 case MeasureSpec.EXACTLY:
15825 result = specSize;
15826 break;
15827 }
15828 return result;
15829 }
15830
15831 /**
15832 * Returns the suggested minimum height that the view should use. This
15833 * returns the maximum of the view's minimum height
15834 * and the background's minimum height
15835 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15836 * <p>
15837 * When being used in {@link #onMeasure(int, int)}, the caller should still
15838 * ensure the returned height is within the requirements of the parent.
15839 *
15840 * @return The suggested minimum height of the view.
15841 */
15842 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015843 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015845 }
15846
15847 /**
15848 * Returns the suggested minimum width that the view should use. This
15849 * returns the maximum of the view's minimum width)
15850 * and the background's minimum width
15851 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15852 * <p>
15853 * When being used in {@link #onMeasure(int, int)}, the caller should still
15854 * ensure the returned width is within the requirements of the parent.
15855 *
15856 * @return The suggested minimum width of the view.
15857 */
15858 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015859 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015861
Philip Milne6c8ea062012-04-03 17:38:43 -070015862 /**
15863 * Returns the minimum height of the view.
15864 *
15865 * @return the minimum height the view will try to be.
15866 *
15867 * @see #setMinimumHeight(int)
15868 *
15869 * @attr ref android.R.styleable#View_minHeight
15870 */
15871 public int getMinimumHeight() {
15872 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015873 }
15874
15875 /**
15876 * Sets the minimum height of the view. It is not guaranteed the view will
15877 * be able to achieve this minimum height (for example, if its parent layout
15878 * constrains it with less available height).
15879 *
15880 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015881 *
15882 * @see #getMinimumHeight()
15883 *
15884 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015885 */
15886 public void setMinimumHeight(int minHeight) {
15887 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015888 requestLayout();
15889 }
15890
15891 /**
15892 * Returns the minimum width of the view.
15893 *
15894 * @return the minimum width the view will try to be.
15895 *
15896 * @see #setMinimumWidth(int)
15897 *
15898 * @attr ref android.R.styleable#View_minWidth
15899 */
15900 public int getMinimumWidth() {
15901 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015902 }
15903
15904 /**
15905 * Sets the minimum width of the view. It is not guaranteed the view will
15906 * be able to achieve this minimum width (for example, if its parent layout
15907 * constrains it with less available width).
15908 *
15909 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015910 *
15911 * @see #getMinimumWidth()
15912 *
15913 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015914 */
15915 public void setMinimumWidth(int minWidth) {
15916 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015917 requestLayout();
15918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015919 }
15920
15921 /**
15922 * Get the animation currently associated with this view.
15923 *
15924 * @return The animation that is currently playing or
15925 * scheduled to play for this view.
15926 */
15927 public Animation getAnimation() {
15928 return mCurrentAnimation;
15929 }
15930
15931 /**
15932 * Start the specified animation now.
15933 *
15934 * @param animation the animation to start now
15935 */
15936 public void startAnimation(Animation animation) {
15937 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15938 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015939 invalidateParentCaches();
15940 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015941 }
15942
15943 /**
15944 * Cancels any animations for this view.
15945 */
15946 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015947 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015948 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015950 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015951 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015952 }
15953
15954 /**
15955 * Sets the next animation to play for this view.
15956 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015957 * {@link #startAnimation(android.view.animation.Animation)} instead.
15958 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015959 * control over the start time and invalidation, but you
15960 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015961 * 2) the view's parent (which controls animations on its children)
15962 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015963 * start.
15964 *
15965 * @param animation The next animation, or null.
15966 */
15967 public void setAnimation(Animation animation) {
15968 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015970 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015971 // If the screen is off assume the animation start time is now instead of
15972 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15973 // would cause the animation to start when the screen turns back on
15974 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15975 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15976 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015978 animation.reset();
15979 }
15980 }
15981
15982 /**
15983 * Invoked by a parent ViewGroup to notify the start of the animation
15984 * currently associated with this view. If you override this method,
15985 * always call super.onAnimationStart();
15986 *
15987 * @see #setAnimation(android.view.animation.Animation)
15988 * @see #getAnimation()
15989 */
15990 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015991 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015992 }
15993
15994 /**
15995 * Invoked by a parent ViewGroup to notify the end of the animation
15996 * currently associated with this view. If you override this method,
15997 * always call super.onAnimationEnd();
15998 *
15999 * @see #setAnimation(android.view.animation.Animation)
16000 * @see #getAnimation()
16001 */
16002 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016003 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016004 }
16005
16006 /**
16007 * Invoked if there is a Transform that involves alpha. Subclass that can
16008 * draw themselves with the specified alpha should return true, and then
16009 * respect that alpha when their onDraw() is called. If this returns false
16010 * then the view may be redirected to draw into an offscreen buffer to
16011 * fulfill the request, which will look fine, but may be slower than if the
16012 * subclass handles it internally. The default implementation returns false.
16013 *
16014 * @param alpha The alpha (0..255) to apply to the view's drawing
16015 * @return true if the view can draw with the specified alpha.
16016 */
16017 protected boolean onSetAlpha(int alpha) {
16018 return false;
16019 }
16020
16021 /**
16022 * This is used by the RootView to perform an optimization when
16023 * the view hierarchy contains one or several SurfaceView.
16024 * SurfaceView is always considered transparent, but its children are not,
16025 * therefore all View objects remove themselves from the global transparent
16026 * region (passed as a parameter to this function).
16027 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016028 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016029 *
16030 * @return Returns true if the effective visibility of the view at this
16031 * point is opaque, regardless of the transparent region; returns false
16032 * if it is possible for underlying windows to be seen behind the view.
16033 *
16034 * {@hide}
16035 */
16036 public boolean gatherTransparentRegion(Region region) {
16037 final AttachInfo attachInfo = mAttachInfo;
16038 if (region != null && attachInfo != null) {
16039 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070016040 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016041 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
16042 // remove it from the transparent region.
16043 final int[] location = attachInfo.mTransparentLocation;
16044 getLocationInWindow(location);
16045 region.op(location[0], location[1], location[0] + mRight - mLeft,
16046 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070016047 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016048 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
16049 // exists, so we remove the background drawable's non-transparent
16050 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070016051 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016052 }
16053 }
16054 return true;
16055 }
16056
16057 /**
16058 * Play a sound effect for this view.
16059 *
16060 * <p>The framework will play sound effects for some built in actions, such as
16061 * clicking, but you may wish to play these effects in your widget,
16062 * for instance, for internal navigation.
16063 *
16064 * <p>The sound effect will only be played if sound effects are enabled by the user, and
16065 * {@link #isSoundEffectsEnabled()} is true.
16066 *
16067 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
16068 */
16069 public void playSoundEffect(int soundConstant) {
16070 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
16071 return;
16072 }
16073 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
16074 }
16075
16076 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016077 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016078 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016079 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016080 *
16081 * <p>The framework will provide haptic feedback for some built in actions,
16082 * such as long presses, but you may wish to provide feedback for your
16083 * own widget.
16084 *
16085 * <p>The feedback will only be performed if
16086 * {@link #isHapticFeedbackEnabled()} is true.
16087 *
16088 * @param feedbackConstant One of the constants defined in
16089 * {@link HapticFeedbackConstants}
16090 */
16091 public boolean performHapticFeedback(int feedbackConstant) {
16092 return performHapticFeedback(feedbackConstant, 0);
16093 }
16094
16095 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016096 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070016097 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070016098 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016099 *
16100 * @param feedbackConstant One of the constants defined in
16101 * {@link HapticFeedbackConstants}
16102 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
16103 */
16104 public boolean performHapticFeedback(int feedbackConstant, int flags) {
16105 if (mAttachInfo == null) {
16106 return false;
16107 }
Romain Guyf607bdc2010-09-10 19:20:06 -070016108 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070016109 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016110 && !isHapticFeedbackEnabled()) {
16111 return false;
16112 }
Romain Guy812ccbe2010-06-01 14:07:24 -070016113 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
16114 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016115 }
16116
16117 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016118 * Request that the visibility of the status bar or other screen/window
16119 * decorations be changed.
16120 *
16121 * <p>This method is used to put the over device UI into temporary modes
16122 * where the user's attention is focused more on the application content,
16123 * by dimming or hiding surrounding system affordances. This is typically
16124 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
16125 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
16126 * to be placed behind the action bar (and with these flags other system
16127 * affordances) so that smooth transitions between hiding and showing them
16128 * can be done.
16129 *
16130 * <p>Two representative examples of the use of system UI visibility is
16131 * implementing a content browsing application (like a magazine reader)
16132 * and a video playing application.
16133 *
16134 * <p>The first code shows a typical implementation of a View in a content
16135 * browsing application. In this implementation, the application goes
16136 * into a content-oriented mode by hiding the status bar and action bar,
16137 * and putting the navigation elements into lights out mode. The user can
16138 * then interact with content while in this mode. Such an application should
16139 * provide an easy way for the user to toggle out of the mode (such as to
16140 * check information in the status bar or access notifications). In the
16141 * implementation here, this is done simply by tapping on the content.
16142 *
16143 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
16144 * content}
16145 *
16146 * <p>This second code sample shows a typical implementation of a View
16147 * in a video playing application. In this situation, while the video is
16148 * playing the application would like to go into a complete full-screen mode,
16149 * to use as much of the display as possible for the video. When in this state
16150 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070016151 * touching on the screen to pop the UI out of full screen mode. See
16152 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070016153 *
16154 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
16155 * content}
16156 *
16157 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16158 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16159 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16160 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016161 */
16162 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016163 if (visibility != mSystemUiVisibility) {
16164 mSystemUiVisibility = visibility;
16165 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16166 mParent.recomputeViewAttributes(this);
16167 }
Joe Onorato664644d2011-01-23 17:53:23 -080016168 }
16169 }
16170
16171 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016172 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16173 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16174 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16175 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16176 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016177 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016178 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016179 return mSystemUiVisibility;
16180 }
16181
Scott Mainec6331b2011-05-24 16:55:56 -070016182 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016183 * Returns the current system UI visibility that is currently set for
16184 * the entire window. This is the combination of the
16185 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16186 * views in the window.
16187 */
16188 public int getWindowSystemUiVisibility() {
16189 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16190 }
16191
16192 /**
16193 * Override to find out when the window's requested system UI visibility
16194 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16195 * This is different from the callbacks recieved through
16196 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16197 * in that this is only telling you about the local request of the window,
16198 * not the actual values applied by the system.
16199 */
16200 public void onWindowSystemUiVisibilityChanged(int visible) {
16201 }
16202
16203 /**
16204 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16205 * the view hierarchy.
16206 */
16207 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16208 onWindowSystemUiVisibilityChanged(visible);
16209 }
16210
16211 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016212 * Set a listener to receive callbacks when the visibility of the system bar changes.
16213 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16214 */
Joe Onorato664644d2011-01-23 17:53:23 -080016215 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016216 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016217 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16218 mParent.recomputeViewAttributes(this);
16219 }
16220 }
16221
16222 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016223 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16224 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016225 */
16226 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016227 ListenerInfo li = mListenerInfo;
16228 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16229 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016230 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016231 }
16232 }
16233
Dianne Hackborncf675782012-05-10 15:07:24 -070016234 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016235 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16236 if (val != mSystemUiVisibility) {
16237 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016238 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016239 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016240 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016241 }
16242
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016243 /** @hide */
16244 public void setDisabledSystemUiVisibility(int flags) {
16245 if (mAttachInfo != null) {
16246 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16247 mAttachInfo.mDisabledSystemUiVisibility = flags;
16248 if (mParent != null) {
16249 mParent.recomputeViewAttributes(this);
16250 }
16251 }
16252 }
16253 }
16254
Joe Onorato664644d2011-01-23 17:53:23 -080016255 /**
Joe Malin32736f02011-01-19 16:14:20 -080016256 * Creates an image that the system displays during the drag and drop
16257 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16258 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16259 * appearance as the given View. The default also positions the center of the drag shadow
16260 * directly under the touch point. If no View is provided (the constructor with no parameters
16261 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16262 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16263 * default is an invisible drag shadow.
16264 * <p>
16265 * You are not required to use the View you provide to the constructor as the basis of the
16266 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16267 * anything you want as the drag shadow.
16268 * </p>
16269 * <p>
16270 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16271 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16272 * size and position of the drag shadow. It uses this data to construct a
16273 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16274 * so that your application can draw the shadow image in the Canvas.
16275 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016276 *
16277 * <div class="special reference">
16278 * <h3>Developer Guides</h3>
16279 * <p>For a guide to implementing drag and drop features, read the
16280 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16281 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016282 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016283 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016284 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016285
16286 /**
Joe Malin32736f02011-01-19 16:14:20 -080016287 * Constructs a shadow image builder based on a View. By default, the resulting drag
16288 * shadow will have the same appearance and dimensions as the View, with the touch point
16289 * over the center of the View.
16290 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016291 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016292 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016293 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016294 }
16295
Christopher Tate17ed60c2011-01-18 12:50:26 -080016296 /**
16297 * Construct a shadow builder object with no associated View. This
16298 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16299 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16300 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016301 * reference to any View object. If they are not overridden, then the result is an
16302 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016303 */
16304 public DragShadowBuilder() {
16305 mView = new WeakReference<View>(null);
16306 }
16307
16308 /**
16309 * Returns the View object that had been passed to the
16310 * {@link #View.DragShadowBuilder(View)}
16311 * constructor. If that View parameter was {@code null} or if the
16312 * {@link #View.DragShadowBuilder()}
16313 * constructor was used to instantiate the builder object, this method will return
16314 * null.
16315 *
16316 * @return The View object associate with this builder object.
16317 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016318 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016319 final public View getView() {
16320 return mView.get();
16321 }
16322
Christopher Tate2c095f32010-10-04 14:13:40 -070016323 /**
Joe Malin32736f02011-01-19 16:14:20 -080016324 * Provides the metrics for the shadow image. These include the dimensions of
16325 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016326 * be centered under the touch location while dragging.
16327 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016328 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016329 * same as the dimensions of the View itself and centers the shadow under
16330 * the touch point.
16331 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016332 *
Joe Malin32736f02011-01-19 16:14:20 -080016333 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16334 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16335 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16336 * image.
16337 *
16338 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16339 * shadow image that should be underneath the touch point during the drag and drop
16340 * operation. Your application must set {@link android.graphics.Point#x} to the
16341 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016342 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016343 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016344 final View view = mView.get();
16345 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016346 shadowSize.set(view.getWidth(), view.getHeight());
16347 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016348 } else {
16349 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16350 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016351 }
16352
16353 /**
Joe Malin32736f02011-01-19 16:14:20 -080016354 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16355 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016356 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016357 *
Joe Malin32736f02011-01-19 16:14:20 -080016358 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016359 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016360 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016361 final View view = mView.get();
16362 if (view != null) {
16363 view.draw(canvas);
16364 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016365 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016366 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016367 }
16368 }
16369
16370 /**
Joe Malin32736f02011-01-19 16:14:20 -080016371 * Starts a drag and drop operation. When your application calls this method, it passes a
16372 * {@link android.view.View.DragShadowBuilder} object to the system. The
16373 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16374 * to get metrics for the drag shadow, and then calls the object's
16375 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16376 * <p>
16377 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16378 * drag events to all the View objects in your application that are currently visible. It does
16379 * this either by calling the View object's drag listener (an implementation of
16380 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16381 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16382 * Both are passed a {@link android.view.DragEvent} object that has a
16383 * {@link android.view.DragEvent#getAction()} value of
16384 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16385 * </p>
16386 * <p>
16387 * Your application can invoke startDrag() on any attached View object. The View object does not
16388 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16389 * be related to the View the user selected for dragging.
16390 * </p>
16391 * @param data A {@link android.content.ClipData} object pointing to the data to be
16392 * transferred by the drag and drop operation.
16393 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16394 * drag shadow.
16395 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16396 * drop operation. This Object is put into every DragEvent object sent by the system during the
16397 * current drag.
16398 * <p>
16399 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16400 * to the target Views. For example, it can contain flags that differentiate between a
16401 * a copy operation and a move operation.
16402 * </p>
16403 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16404 * so the parameter should be set to 0.
16405 * @return {@code true} if the method completes successfully, or
16406 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16407 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016408 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016409 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016410 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016411 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016412 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016413 }
16414 boolean okay = false;
16415
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016416 Point shadowSize = new Point();
16417 Point shadowTouchPoint = new Point();
16418 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016419
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016420 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16421 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16422 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016423 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016424
Chris Tatea32dcf72010-10-14 12:13:50 -070016425 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016426 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16427 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016428 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016429 Surface surface = new Surface();
16430 try {
16431 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016432 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016433 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016434 + " surface=" + surface);
16435 if (token != null) {
16436 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016437 try {
Chris Tate6b391282010-10-14 15:48:59 -070016438 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016439 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016440 } finally {
16441 surface.unlockCanvasAndPost(canvas);
16442 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016443
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016444 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016445
16446 // Cache the local state object for delivery with DragEvents
16447 root.setLocalDragState(myLocalState);
16448
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016449 // repurpose 'shadowSize' for the last touch point
16450 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016451
Christopher Tatea53146c2010-09-07 11:57:52 -070016452 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016453 shadowSize.x, shadowSize.y,
16454 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016455 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016456
16457 // Off and running! Release our local surface instance; the drag
16458 // shadow surface is now managed by the system process.
16459 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016460 }
16461 } catch (Exception e) {
16462 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16463 surface.destroy();
16464 }
16465
16466 return okay;
16467 }
16468
Christopher Tatea53146c2010-09-07 11:57:52 -070016469 /**
Joe Malin32736f02011-01-19 16:14:20 -080016470 * Handles drag events sent by the system following a call to
16471 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16472 *<p>
16473 * When the system calls this method, it passes a
16474 * {@link android.view.DragEvent} object. A call to
16475 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16476 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16477 * operation.
16478 * @param event The {@link android.view.DragEvent} sent by the system.
16479 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16480 * in DragEvent, indicating the type of drag event represented by this object.
16481 * @return {@code true} if the method was successful, otherwise {@code false}.
16482 * <p>
16483 * The method should return {@code true} in response to an action type of
16484 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16485 * operation.
16486 * </p>
16487 * <p>
16488 * The method should also return {@code true} in response to an action type of
16489 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16490 * {@code false} if it didn't.
16491 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016492 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016493 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016494 return false;
16495 }
16496
16497 /**
Joe Malin32736f02011-01-19 16:14:20 -080016498 * Detects if this View is enabled and has a drag event listener.
16499 * If both are true, then it calls the drag event listener with the
16500 * {@link android.view.DragEvent} it received. If the drag event listener returns
16501 * {@code true}, then dispatchDragEvent() returns {@code true}.
16502 * <p>
16503 * For all other cases, the method calls the
16504 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16505 * method and returns its result.
16506 * </p>
16507 * <p>
16508 * This ensures that a drag event is always consumed, even if the View does not have a drag
16509 * event listener. However, if the View has a listener and the listener returns true, then
16510 * onDragEvent() is not called.
16511 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016512 */
16513 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016514 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016515 ListenerInfo li = mListenerInfo;
16516 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16517 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016518 return true;
16519 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016520 return onDragEvent(event);
16521 }
16522
Christopher Tate3d4bf172011-03-28 16:16:46 -070016523 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016524 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016525 }
16526
Christopher Tatea53146c2010-09-07 11:57:52 -070016527 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016528 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16529 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016530 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016531 */
16532 public void onCloseSystemDialogs(String reason) {
16533 }
Joe Malin32736f02011-01-19 16:14:20 -080016534
Dianne Hackbornffa42482009-09-23 22:20:11 -070016535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016536 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016537 * update a Region being computed for
16538 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016539 * that any non-transparent parts of the Drawable are removed from the
16540 * given transparent region.
16541 *
16542 * @param dr The Drawable whose transparency is to be applied to the region.
16543 * @param region A Region holding the current transparency information,
16544 * where any parts of the region that are set are considered to be
16545 * transparent. On return, this region will be modified to have the
16546 * transparency information reduced by the corresponding parts of the
16547 * Drawable that are not transparent.
16548 * {@hide}
16549 */
16550 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16551 if (DBG) {
16552 Log.i("View", "Getting transparent region for: " + this);
16553 }
16554 final Region r = dr.getTransparentRegion();
16555 final Rect db = dr.getBounds();
16556 final AttachInfo attachInfo = mAttachInfo;
16557 if (r != null && attachInfo != null) {
16558 final int w = getRight()-getLeft();
16559 final int h = getBottom()-getTop();
16560 if (db.left > 0) {
16561 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16562 r.op(0, 0, db.left, h, Region.Op.UNION);
16563 }
16564 if (db.right < w) {
16565 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16566 r.op(db.right, 0, w, h, Region.Op.UNION);
16567 }
16568 if (db.top > 0) {
16569 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16570 r.op(0, 0, w, db.top, Region.Op.UNION);
16571 }
16572 if (db.bottom < h) {
16573 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16574 r.op(0, db.bottom, w, h, Region.Op.UNION);
16575 }
16576 final int[] location = attachInfo.mTransparentLocation;
16577 getLocationInWindow(location);
16578 r.translate(location[0], location[1]);
16579 region.op(r, Region.Op.INTERSECT);
16580 } else {
16581 region.op(db, Region.Op.DIFFERENCE);
16582 }
16583 }
16584
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016585 private void checkForLongClick(int delayOffset) {
16586 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16587 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016588
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016589 if (mPendingCheckForLongPress == null) {
16590 mPendingCheckForLongPress = new CheckForLongPress();
16591 }
16592 mPendingCheckForLongPress.rememberWindowAttachCount();
16593 postDelayed(mPendingCheckForLongPress,
16594 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016596 }
16597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016598 /**
16599 * Inflate a view from an XML resource. This convenience method wraps the {@link
16600 * LayoutInflater} class, which provides a full range of options for view inflation.
16601 *
16602 * @param context The Context object for your activity or application.
16603 * @param resource The resource ID to inflate
16604 * @param root A view group that will be the parent. Used to properly inflate the
16605 * layout_* parameters.
16606 * @see LayoutInflater
16607 */
16608 public static View inflate(Context context, int resource, ViewGroup root) {
16609 LayoutInflater factory = LayoutInflater.from(context);
16610 return factory.inflate(resource, root);
16611 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016613 /**
Adam Powell637d3372010-08-25 14:37:03 -070016614 * Scroll the view with standard behavior for scrolling beyond the normal
16615 * content boundaries. Views that call this method should override
16616 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16617 * results of an over-scroll operation.
16618 *
16619 * Views can use this method to handle any touch or fling-based scrolling.
16620 *
16621 * @param deltaX Change in X in pixels
16622 * @param deltaY Change in Y in pixels
16623 * @param scrollX Current X scroll value in pixels before applying deltaX
16624 * @param scrollY Current Y scroll value in pixels before applying deltaY
16625 * @param scrollRangeX Maximum content scroll range along the X axis
16626 * @param scrollRangeY Maximum content scroll range along the Y axis
16627 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16628 * along the X axis.
16629 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16630 * along the Y axis.
16631 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16632 * @return true if scrolling was clamped to an over-scroll boundary along either
16633 * axis, false otherwise.
16634 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016635 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016636 protected boolean overScrollBy(int deltaX, int deltaY,
16637 int scrollX, int scrollY,
16638 int scrollRangeX, int scrollRangeY,
16639 int maxOverScrollX, int maxOverScrollY,
16640 boolean isTouchEvent) {
16641 final int overScrollMode = mOverScrollMode;
16642 final boolean canScrollHorizontal =
16643 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16644 final boolean canScrollVertical =
16645 computeVerticalScrollRange() > computeVerticalScrollExtent();
16646 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16647 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16648 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16649 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16650
16651 int newScrollX = scrollX + deltaX;
16652 if (!overScrollHorizontal) {
16653 maxOverScrollX = 0;
16654 }
16655
16656 int newScrollY = scrollY + deltaY;
16657 if (!overScrollVertical) {
16658 maxOverScrollY = 0;
16659 }
16660
16661 // Clamp values if at the limits and record
16662 final int left = -maxOverScrollX;
16663 final int right = maxOverScrollX + scrollRangeX;
16664 final int top = -maxOverScrollY;
16665 final int bottom = maxOverScrollY + scrollRangeY;
16666
16667 boolean clampedX = false;
16668 if (newScrollX > right) {
16669 newScrollX = right;
16670 clampedX = true;
16671 } else if (newScrollX < left) {
16672 newScrollX = left;
16673 clampedX = true;
16674 }
16675
16676 boolean clampedY = false;
16677 if (newScrollY > bottom) {
16678 newScrollY = bottom;
16679 clampedY = true;
16680 } else if (newScrollY < top) {
16681 newScrollY = top;
16682 clampedY = true;
16683 }
16684
16685 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16686
16687 return clampedX || clampedY;
16688 }
16689
16690 /**
16691 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16692 * respond to the results of an over-scroll operation.
16693 *
16694 * @param scrollX New X scroll value in pixels
16695 * @param scrollY New Y scroll value in pixels
16696 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16697 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16698 */
16699 protected void onOverScrolled(int scrollX, int scrollY,
16700 boolean clampedX, boolean clampedY) {
16701 // Intentionally empty.
16702 }
16703
16704 /**
16705 * Returns the over-scroll mode for this view. The result will be
16706 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16707 * (allow over-scrolling only if the view content is larger than the container),
16708 * or {@link #OVER_SCROLL_NEVER}.
16709 *
16710 * @return This view's over-scroll mode.
16711 */
16712 public int getOverScrollMode() {
16713 return mOverScrollMode;
16714 }
16715
16716 /**
16717 * Set the over-scroll mode for this view. Valid over-scroll modes are
16718 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16719 * (allow over-scrolling only if the view content is larger than the container),
16720 * or {@link #OVER_SCROLL_NEVER}.
16721 *
16722 * Setting the over-scroll mode of a view will have an effect only if the
16723 * view is capable of scrolling.
16724 *
16725 * @param overScrollMode The new over-scroll mode for this view.
16726 */
16727 public void setOverScrollMode(int overScrollMode) {
16728 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16729 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16730 overScrollMode != OVER_SCROLL_NEVER) {
16731 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16732 }
16733 mOverScrollMode = overScrollMode;
16734 }
16735
16736 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016737 * Gets a scale factor that determines the distance the view should scroll
16738 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16739 * @return The vertical scroll scale factor.
16740 * @hide
16741 */
16742 protected float getVerticalScrollFactor() {
16743 if (mVerticalScrollFactor == 0) {
16744 TypedValue outValue = new TypedValue();
16745 if (!mContext.getTheme().resolveAttribute(
16746 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16747 throw new IllegalStateException(
16748 "Expected theme to define listPreferredItemHeight.");
16749 }
16750 mVerticalScrollFactor = outValue.getDimension(
16751 mContext.getResources().getDisplayMetrics());
16752 }
16753 return mVerticalScrollFactor;
16754 }
16755
16756 /**
16757 * Gets a scale factor that determines the distance the view should scroll
16758 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16759 * @return The horizontal scroll scale factor.
16760 * @hide
16761 */
16762 protected float getHorizontalScrollFactor() {
16763 // TODO: Should use something else.
16764 return getVerticalScrollFactor();
16765 }
16766
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016767 /**
16768 * Return the value specifying the text direction or policy that was set with
16769 * {@link #setTextDirection(int)}.
16770 *
16771 * @return the defined text direction. It can be one of:
16772 *
16773 * {@link #TEXT_DIRECTION_INHERIT},
16774 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16775 * {@link #TEXT_DIRECTION_ANY_RTL},
16776 * {@link #TEXT_DIRECTION_LTR},
16777 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016778 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016779 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016780 * @attr ref android.R.styleable#View_textDirection
16781 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016782 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016783 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016784 @ViewDebug.ExportedProperty(category = "text", mapping = {
16785 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16786 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16787 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16788 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16789 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16790 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16791 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016792 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016793 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016794 }
16795
16796 /**
16797 * Set the text direction.
16798 *
16799 * @param textDirection the direction to set. Should be one of:
16800 *
16801 * {@link #TEXT_DIRECTION_INHERIT},
16802 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16803 * {@link #TEXT_DIRECTION_ANY_RTL},
16804 * {@link #TEXT_DIRECTION_LTR},
16805 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016806 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016807 *
16808 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16809 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16810 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016811 *
16812 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016813 */
16814 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016815 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016816 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016817 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016818 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016819 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016820 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016821 // Do resolution
16822 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016823 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016824 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016825 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016826 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016827 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016828 }
16829 }
16830
16831 /**
16832 * Return the resolved text direction.
16833 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016834 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016835 *
Doug Feltcb3791202011-07-07 11:57:48 -070016836 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16837 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016838 * {@link #TEXT_DIRECTION_LTR},
16839 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016840 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016841 *
16842 * @attr ref android.R.styleable#View_textDirection
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016843 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016844 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016845 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016846 }
16847
16848 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016849 * Resolve the text direction.
16850 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016851 * @return true if resolution has been done, false otherwise.
16852 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016853 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016854 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016855 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016856 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016857 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016858
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016859 if (hasRtlSupport()) {
16860 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016861 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016862 switch(textDirection) {
16863 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016864 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016865 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016866 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016867 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016868 return false;
16869 }
16870
16871 View parent = ((View) mParent);
16872 // Parent has not yet resolved, so we still return the default
16873 if (!parent.isTextDirectionResolved()) {
16874 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16875 // Resolution will need to happen again later
16876 return false;
16877 }
16878
16879 // Set current resolved direction to the same value as the parent's one
16880 final int parentResolvedDirection = parent.getTextDirection();
16881 switch (parentResolvedDirection) {
16882 case TEXT_DIRECTION_FIRST_STRONG:
16883 case TEXT_DIRECTION_ANY_RTL:
16884 case TEXT_DIRECTION_LTR:
16885 case TEXT_DIRECTION_RTL:
16886 case TEXT_DIRECTION_LOCALE:
16887 mPrivateFlags2 |=
16888 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16889 break;
16890 default:
16891 // Default resolved direction is "first strong" heuristic
16892 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016893 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016894 break;
16895 case TEXT_DIRECTION_FIRST_STRONG:
16896 case TEXT_DIRECTION_ANY_RTL:
16897 case TEXT_DIRECTION_LTR:
16898 case TEXT_DIRECTION_RTL:
16899 case TEXT_DIRECTION_LOCALE:
16900 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016901 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016902 break;
16903 default:
16904 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016905 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016906 }
16907 } else {
16908 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016909 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016910 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016911
16912 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016913 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016914 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016915 }
16916
16917 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016918 * Check if text direction resolution can be done.
16919 *
16920 * @return true if text direction resolution can be done otherwise return false.
16921 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016922 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016923 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016924 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016925 return (mParent != null) && (mParent instanceof View) &&
16926 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016927 default:
16928 return true;
16929 }
16930 }
16931
16932 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016933 * Reset resolved text direction. Text direction will be resolved during a call to
16934 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016935 *
16936 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016937 */
16938 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016939 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016940 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016941 // Set to default value
16942 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016943 }
16944
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016945 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016946 * @return true if text direction is inherited.
16947 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016948 * @hide
16949 */
16950 public boolean isTextDirectionInherited() {
16951 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16952 }
16953
16954 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016955 * @return true if text direction is resolved.
16956 */
16957 private boolean isTextDirectionResolved() {
16958 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16959 }
16960
16961 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016962 * Return the value specifying the text alignment or policy that was set with
16963 * {@link #setTextAlignment(int)}.
16964 *
16965 * @return the defined text alignment. It can be one of:
16966 *
16967 * {@link #TEXT_ALIGNMENT_INHERIT},
16968 * {@link #TEXT_ALIGNMENT_GRAVITY},
16969 * {@link #TEXT_ALIGNMENT_CENTER},
16970 * {@link #TEXT_ALIGNMENT_TEXT_START},
16971 * {@link #TEXT_ALIGNMENT_TEXT_END},
16972 * {@link #TEXT_ALIGNMENT_VIEW_START},
16973 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016974 *
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080016975 * @attr ref android.R.styleable#View_textAlignment
16976 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016977 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016978 */
16979 @ViewDebug.ExportedProperty(category = "text", mapping = {
16980 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16981 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16982 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16983 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16984 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16985 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16986 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16987 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016988 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016989 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016990 }
16991
16992 /**
16993 * Set the text alignment.
16994 *
16995 * @param textAlignment The text alignment to set. Should be one of
16996 *
16997 * {@link #TEXT_ALIGNMENT_INHERIT},
16998 * {@link #TEXT_ALIGNMENT_GRAVITY},
16999 * {@link #TEXT_ALIGNMENT_CENTER},
17000 * {@link #TEXT_ALIGNMENT_TEXT_START},
17001 * {@link #TEXT_ALIGNMENT_TEXT_END},
17002 * {@link #TEXT_ALIGNMENT_VIEW_START},
17003 * {@link #TEXT_ALIGNMENT_VIEW_END}
17004 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017005 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
17006 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
17007 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
17008 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017009 * @attr ref android.R.styleable#View_textAlignment
17010 */
17011 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017012 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017013 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017014 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017015 resetResolvedTextAlignment();
17016 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017017 mPrivateFlags2 |=
17018 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
17019 // Do resolution
17020 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017021 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070017022 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017023 // Refresh
17024 requestLayout();
17025 invalidate(true);
17026 }
17027 }
17028
17029 /**
17030 * Return the resolved text alignment.
17031 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017032 * @return the resolved text alignment. Returns one of:
17033 *
17034 * {@link #TEXT_ALIGNMENT_GRAVITY},
17035 * {@link #TEXT_ALIGNMENT_CENTER},
17036 * {@link #TEXT_ALIGNMENT_TEXT_START},
17037 * {@link #TEXT_ALIGNMENT_TEXT_END},
17038 * {@link #TEXT_ALIGNMENT_VIEW_START},
17039 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio7d529cd2012-11-14 17:59:06 -080017040 *
17041 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017042 */
17043 @ViewDebug.ExportedProperty(category = "text", mapping = {
17044 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
17045 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
17046 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
17047 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
17048 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
17049 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
17050 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
17051 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017052 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017053 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
17054 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017055 }
17056
17057 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017058 * Resolve the text alignment.
17059 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017060 * @return true if resolution has been done, false otherwise.
17061 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017062 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017063 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017064 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017065 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017066 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017067
17068 if (hasRtlSupport()) {
17069 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017070 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017071 switch (textAlignment) {
17072 case TEXT_ALIGNMENT_INHERIT:
17073 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017074 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017075 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070017076 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017077 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017078 return false;
17079 }
17080 View parent = (View) mParent;
17081
17082 // Parent has not yet resolved, so we still return the default
17083 if (!parent.isTextAlignmentResolved()) {
17084 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
17085 // Resolution will need to happen again later
17086 return false;
17087 }
17088
17089 final int parentResolvedTextAlignment = parent.getTextAlignment();
17090 switch (parentResolvedTextAlignment) {
17091 case TEXT_ALIGNMENT_GRAVITY:
17092 case TEXT_ALIGNMENT_TEXT_START:
17093 case TEXT_ALIGNMENT_TEXT_END:
17094 case TEXT_ALIGNMENT_CENTER:
17095 case TEXT_ALIGNMENT_VIEW_START:
17096 case TEXT_ALIGNMENT_VIEW_END:
17097 // Resolved text alignment is the same as the parent resolved
17098 // text alignment
17099 mPrivateFlags2 |=
17100 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
17101 break;
17102 default:
17103 // Use default resolved text alignment
17104 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017105 }
17106 break;
17107 case TEXT_ALIGNMENT_GRAVITY:
17108 case TEXT_ALIGNMENT_TEXT_START:
17109 case TEXT_ALIGNMENT_TEXT_END:
17110 case TEXT_ALIGNMENT_CENTER:
17111 case TEXT_ALIGNMENT_VIEW_START:
17112 case TEXT_ALIGNMENT_VIEW_END:
17113 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017114 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017115 break;
17116 default:
17117 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017118 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017119 }
17120 } else {
17121 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070017122 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017123 }
17124
17125 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070017126 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017127 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017128 }
17129
17130 /**
17131 * Check if text alignment resolution can be done.
17132 *
17133 * @return true if text alignment resolution can be done otherwise return false.
17134 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017135 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017136 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017137 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070017138 return (mParent != null) && (mParent instanceof View) &&
17139 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017140 default:
17141 return true;
17142 }
17143 }
17144
17145 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017146 * Reset resolved text alignment. Text alignment will be resolved during a call to
17147 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017148 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070017149 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017150 */
17151 public void resetResolvedTextAlignment() {
17152 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070017153 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017154 // Set to default
17155 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070017156 }
17157
Adam Powella9108a22012-07-18 11:18:09 -070017158 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017159 * @return true if text alignment is inherited.
17160 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070017161 * @hide
17162 */
17163 public boolean isTextAlignmentInherited() {
17164 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
17165 }
17166
17167 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070017168 * @return true if text alignment is resolved.
17169 */
17170 private boolean isTextAlignmentResolved() {
17171 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17172 }
17173
17174 /**
Adam Powella9108a22012-07-18 11:18:09 -070017175 * Generate a value suitable for use in {@link #setId(int)}.
17176 * This value will not collide with ID values generated at build time by aapt for R.id.
17177 *
17178 * @return a generated ID value
17179 */
17180 public static int generateViewId() {
17181 for (;;) {
17182 final int result = sNextGeneratedId.get();
17183 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17184 int newValue = result + 1;
17185 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17186 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17187 return result;
17188 }
17189 }
17190 }
17191
Chet Haaseb39f0512011-05-24 14:36:40 -070017192 //
17193 // Properties
17194 //
17195 /**
17196 * A Property wrapper around the <code>alpha</code> functionality handled by the
17197 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17198 */
Chet Haased47f1532011-12-16 11:18:52 -080017199 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017200 @Override
17201 public void setValue(View object, float value) {
17202 object.setAlpha(value);
17203 }
17204
17205 @Override
17206 public Float get(View object) {
17207 return object.getAlpha();
17208 }
17209 };
17210
17211 /**
17212 * A Property wrapper around the <code>translationX</code> functionality handled by the
17213 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17214 */
Chet Haased47f1532011-12-16 11:18:52 -080017215 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017216 @Override
17217 public void setValue(View object, float value) {
17218 object.setTranslationX(value);
17219 }
17220
17221 @Override
17222 public Float get(View object) {
17223 return object.getTranslationX();
17224 }
17225 };
17226
17227 /**
17228 * A Property wrapper around the <code>translationY</code> functionality handled by the
17229 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17230 */
Chet Haased47f1532011-12-16 11:18:52 -080017231 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017232 @Override
17233 public void setValue(View object, float value) {
17234 object.setTranslationY(value);
17235 }
17236
17237 @Override
17238 public Float get(View object) {
17239 return object.getTranslationY();
17240 }
17241 };
17242
17243 /**
17244 * A Property wrapper around the <code>x</code> functionality handled by the
17245 * {@link View#setX(float)} and {@link View#getX()} methods.
17246 */
Chet Haased47f1532011-12-16 11:18:52 -080017247 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017248 @Override
17249 public void setValue(View object, float value) {
17250 object.setX(value);
17251 }
17252
17253 @Override
17254 public Float get(View object) {
17255 return object.getX();
17256 }
17257 };
17258
17259 /**
17260 * A Property wrapper around the <code>y</code> functionality handled by the
17261 * {@link View#setY(float)} and {@link View#getY()} methods.
17262 */
Chet Haased47f1532011-12-16 11:18:52 -080017263 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017264 @Override
17265 public void setValue(View object, float value) {
17266 object.setY(value);
17267 }
17268
17269 @Override
17270 public Float get(View object) {
17271 return object.getY();
17272 }
17273 };
17274
17275 /**
17276 * A Property wrapper around the <code>rotation</code> functionality handled by the
17277 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17278 */
Chet Haased47f1532011-12-16 11:18:52 -080017279 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017280 @Override
17281 public void setValue(View object, float value) {
17282 object.setRotation(value);
17283 }
17284
17285 @Override
17286 public Float get(View object) {
17287 return object.getRotation();
17288 }
17289 };
17290
17291 /**
17292 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17293 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17294 */
Chet Haased47f1532011-12-16 11:18:52 -080017295 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017296 @Override
17297 public void setValue(View object, float value) {
17298 object.setRotationX(value);
17299 }
17300
17301 @Override
17302 public Float get(View object) {
17303 return object.getRotationX();
17304 }
17305 };
17306
17307 /**
17308 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17309 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17310 */
Chet Haased47f1532011-12-16 11:18:52 -080017311 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017312 @Override
17313 public void setValue(View object, float value) {
17314 object.setRotationY(value);
17315 }
17316
17317 @Override
17318 public Float get(View object) {
17319 return object.getRotationY();
17320 }
17321 };
17322
17323 /**
17324 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17325 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17326 */
Chet Haased47f1532011-12-16 11:18:52 -080017327 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017328 @Override
17329 public void setValue(View object, float value) {
17330 object.setScaleX(value);
17331 }
17332
17333 @Override
17334 public Float get(View object) {
17335 return object.getScaleX();
17336 }
17337 };
17338
17339 /**
17340 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17341 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17342 */
Chet Haased47f1532011-12-16 11:18:52 -080017343 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017344 @Override
17345 public void setValue(View object, float value) {
17346 object.setScaleY(value);
17347 }
17348
17349 @Override
17350 public Float get(View object) {
17351 return object.getScaleY();
17352 }
17353 };
17354
Jeff Brown33bbfd22011-02-24 20:55:35 -080017355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017356 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17357 * Each MeasureSpec represents a requirement for either the width or the height.
17358 * A MeasureSpec is comprised of a size and a mode. There are three possible
17359 * modes:
17360 * <dl>
17361 * <dt>UNSPECIFIED</dt>
17362 * <dd>
17363 * The parent has not imposed any constraint on the child. It can be whatever size
17364 * it wants.
17365 * </dd>
17366 *
17367 * <dt>EXACTLY</dt>
17368 * <dd>
17369 * The parent has determined an exact size for the child. The child is going to be
17370 * given those bounds regardless of how big it wants to be.
17371 * </dd>
17372 *
17373 * <dt>AT_MOST</dt>
17374 * <dd>
17375 * The child can be as large as it wants up to the specified size.
17376 * </dd>
17377 * </dl>
17378 *
17379 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17380 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17381 */
17382 public static class MeasureSpec {
17383 private static final int MODE_SHIFT = 30;
17384 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17385
17386 /**
17387 * Measure specification mode: The parent has not imposed any constraint
17388 * on the child. It can be whatever size it wants.
17389 */
17390 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17391
17392 /**
17393 * Measure specification mode: The parent has determined an exact size
17394 * for the child. The child is going to be given those bounds regardless
17395 * of how big it wants to be.
17396 */
17397 public static final int EXACTLY = 1 << MODE_SHIFT;
17398
17399 /**
17400 * Measure specification mode: The child can be as large as it wants up
17401 * to the specified size.
17402 */
17403 public static final int AT_MOST = 2 << MODE_SHIFT;
17404
17405 /**
17406 * Creates a measure specification based on the supplied size and mode.
17407 *
17408 * The mode must always be one of the following:
17409 * <ul>
17410 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17411 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17412 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17413 * </ul>
17414 *
Adam Powell2c8cc972012-12-07 18:04:51 -080017415 * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
17416 * implementation was such that the order of arguments did not matter
17417 * and overflow in either value could impact the resulting MeasureSpec.
17418 * {@link android.widget.RelativeLayout} was affected by this bug.
17419 * Apps targeting API levels greater than 17 will get the fixed, more strict
17420 * behavior.</p>
17421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017422 * @param size the size of the measure specification
17423 * @param mode the mode of the measure specification
17424 * @return the measure specification based on size and mode
17425 */
17426 public static int makeMeasureSpec(int size, int mode) {
Adam Powell7da4b732012-12-07 15:28:33 -080017427 if (sUseBrokenMakeMeasureSpec) {
17428 return size + mode;
17429 } else {
17430 return (size & ~MODE_MASK) | (mode & MODE_MASK);
17431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017432 }
17433
17434 /**
17435 * Extracts the mode from the supplied measure specification.
17436 *
17437 * @param measureSpec the measure specification to extract the mode from
17438 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17439 * {@link android.view.View.MeasureSpec#AT_MOST} or
17440 * {@link android.view.View.MeasureSpec#EXACTLY}
17441 */
17442 public static int getMode(int measureSpec) {
17443 return (measureSpec & MODE_MASK);
17444 }
17445
17446 /**
17447 * Extracts the size from the supplied measure specification.
17448 *
17449 * @param measureSpec the measure specification to extract the size from
17450 * @return the size in pixels defined in the supplied measure specification
17451 */
17452 public static int getSize(int measureSpec) {
17453 return (measureSpec & ~MODE_MASK);
17454 }
17455
Philip Milne7b757812012-09-19 18:13:44 -070017456 static int adjust(int measureSpec, int delta) {
17457 return makeMeasureSpec(getSize(measureSpec + delta), getMode(measureSpec));
17458 }
17459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017460 /**
17461 * Returns a String representation of the specified measure
17462 * specification.
17463 *
17464 * @param measureSpec the measure specification to convert to a String
17465 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17466 */
17467 public static String toString(int measureSpec) {
17468 int mode = getMode(measureSpec);
17469 int size = getSize(measureSpec);
17470
17471 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17472
17473 if (mode == UNSPECIFIED)
17474 sb.append("UNSPECIFIED ");
17475 else if (mode == EXACTLY)
17476 sb.append("EXACTLY ");
17477 else if (mode == AT_MOST)
17478 sb.append("AT_MOST ");
17479 else
17480 sb.append(mode).append(" ");
17481
17482 sb.append(size);
17483 return sb.toString();
17484 }
17485 }
17486
17487 class CheckForLongPress implements Runnable {
17488
17489 private int mOriginalWindowAttachCount;
17490
17491 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017492 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017493 && mOriginalWindowAttachCount == mWindowAttachCount) {
17494 if (performLongClick()) {
17495 mHasPerformedLongPress = true;
17496 }
17497 }
17498 }
17499
17500 public void rememberWindowAttachCount() {
17501 mOriginalWindowAttachCount = mWindowAttachCount;
17502 }
17503 }
Joe Malin32736f02011-01-19 16:14:20 -080017504
Adam Powelle14579b2009-12-16 18:39:52 -080017505 private final class CheckForTap implements Runnable {
17506 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017507 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017508 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017509 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017510 }
17511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017512
Adam Powella35d7682010-03-12 14:48:13 -080017513 private final class PerformClick implements Runnable {
17514 public void run() {
17515 performClick();
17516 }
17517 }
17518
Dianne Hackborn63042d62011-01-26 18:56:29 -080017519 /** @hide */
17520 public void hackTurnOffWindowResizeAnim(boolean off) {
17521 mAttachInfo.mTurnOffWindowResizeAnim = off;
17522 }
Joe Malin32736f02011-01-19 16:14:20 -080017523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017524 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017525 * This method returns a ViewPropertyAnimator object, which can be used to animate
17526 * specific properties on this View.
17527 *
17528 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17529 */
17530 public ViewPropertyAnimator animate() {
17531 if (mAnimator == null) {
17532 mAnimator = new ViewPropertyAnimator(this);
17533 }
17534 return mAnimator;
17535 }
17536
17537 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017538 * Interface definition for a callback to be invoked when a hardware key event is
17539 * dispatched to this view. The callback will be invoked before the key event is
17540 * given to the view. This is only useful for hardware keyboards; a software input
17541 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017542 */
17543 public interface OnKeyListener {
17544 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017545 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017546 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017547 * <p>Key presses in software keyboards will generally NOT trigger this method,
17548 * although some may elect to do so in some situations. Do not assume a
17549 * software input method has to be key-based; even if it is, it may use key presses
17550 * in a different way than you expect, so there is no way to reliably catch soft
17551 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017552 *
17553 * @param v The view the key has been dispatched to.
17554 * @param keyCode The code for the physical key that was pressed
17555 * @param event The KeyEvent object containing full information about
17556 * the event.
17557 * @return True if the listener has consumed the event, false otherwise.
17558 */
17559 boolean onKey(View v, int keyCode, KeyEvent event);
17560 }
17561
17562 /**
17563 * Interface definition for a callback to be invoked when a touch event is
17564 * dispatched to this view. The callback will be invoked before the touch
17565 * event is given to the view.
17566 */
17567 public interface OnTouchListener {
17568 /**
17569 * Called when a touch event is dispatched to a view. This allows listeners to
17570 * get a chance to respond before the target view.
17571 *
17572 * @param v The view the touch event has been dispatched to.
17573 * @param event The MotionEvent object containing full information about
17574 * the event.
17575 * @return True if the listener has consumed the event, false otherwise.
17576 */
17577 boolean onTouch(View v, MotionEvent event);
17578 }
17579
17580 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017581 * Interface definition for a callback to be invoked when a hover event is
17582 * dispatched to this view. The callback will be invoked before the hover
17583 * event is given to the view.
17584 */
17585 public interface OnHoverListener {
17586 /**
17587 * Called when a hover event is dispatched to a view. This allows listeners to
17588 * get a chance to respond before the target view.
17589 *
17590 * @param v The view the hover event has been dispatched to.
17591 * @param event The MotionEvent object containing full information about
17592 * the event.
17593 * @return True if the listener has consumed the event, false otherwise.
17594 */
17595 boolean onHover(View v, MotionEvent event);
17596 }
17597
17598 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017599 * Interface definition for a callback to be invoked when a generic motion event is
17600 * dispatched to this view. The callback will be invoked before the generic motion
17601 * event is given to the view.
17602 */
17603 public interface OnGenericMotionListener {
17604 /**
17605 * Called when a generic motion event is dispatched to a view. This allows listeners to
17606 * get a chance to respond before the target view.
17607 *
17608 * @param v The view the generic motion event has been dispatched to.
17609 * @param event The MotionEvent object containing full information about
17610 * the event.
17611 * @return True if the listener has consumed the event, false otherwise.
17612 */
17613 boolean onGenericMotion(View v, MotionEvent event);
17614 }
17615
17616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017617 * Interface definition for a callback to be invoked when a view has been clicked and held.
17618 */
17619 public interface OnLongClickListener {
17620 /**
17621 * Called when a view has been clicked and held.
17622 *
17623 * @param v The view that was clicked and held.
17624 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017625 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017626 */
17627 boolean onLongClick(View v);
17628 }
17629
17630 /**
Chris Tate32affef2010-10-18 15:29:21 -070017631 * Interface definition for a callback to be invoked when a drag is being dispatched
17632 * to this view. The callback will be invoked before the hosting view's own
17633 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17634 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017635 *
17636 * <div class="special reference">
17637 * <h3>Developer Guides</h3>
17638 * <p>For a guide to implementing drag and drop features, read the
17639 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17640 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017641 */
17642 public interface OnDragListener {
17643 /**
17644 * Called when a drag event is dispatched to a view. This allows listeners
17645 * to get a chance to override base View behavior.
17646 *
Joe Malin32736f02011-01-19 16:14:20 -080017647 * @param v The View that received the drag event.
17648 * @param event The {@link android.view.DragEvent} object for the drag event.
17649 * @return {@code true} if the drag event was handled successfully, or {@code false}
17650 * if the drag event was not handled. Note that {@code false} will trigger the View
17651 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017652 */
17653 boolean onDrag(View v, DragEvent event);
17654 }
17655
17656 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017657 * Interface definition for a callback to be invoked when the focus state of
17658 * a view changed.
17659 */
17660 public interface OnFocusChangeListener {
17661 /**
17662 * Called when the focus state of a view has changed.
17663 *
17664 * @param v The view whose state has changed.
17665 * @param hasFocus The new focus state of v.
17666 */
17667 void onFocusChange(View v, boolean hasFocus);
17668 }
17669
17670 /**
17671 * Interface definition for a callback to be invoked when a view is clicked.
17672 */
17673 public interface OnClickListener {
17674 /**
17675 * Called when a view has been clicked.
17676 *
17677 * @param v The view that was clicked.
17678 */
17679 void onClick(View v);
17680 }
17681
17682 /**
17683 * Interface definition for a callback to be invoked when the context menu
17684 * for this view is being built.
17685 */
17686 public interface OnCreateContextMenuListener {
17687 /**
17688 * Called when the context menu for this view is being built. It is not
17689 * safe to hold onto the menu after this method returns.
17690 *
17691 * @param menu The context menu that is being built
17692 * @param v The view for which the context menu is being built
17693 * @param menuInfo Extra information about the item for which the
17694 * context menu should be shown. This information will vary
17695 * depending on the class of v.
17696 */
17697 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17698 }
17699
Joe Onorato664644d2011-01-23 17:53:23 -080017700 /**
17701 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017702 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017703 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017704 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017705 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017706 */
17707 public interface OnSystemUiVisibilityChangeListener {
17708 /**
17709 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017710 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017711 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017712 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17713 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17714 * This tells you the <strong>global</strong> state of these UI visibility
17715 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017716 */
17717 public void onSystemUiVisibilityChange(int visibility);
17718 }
17719
Adam Powell4afd62b2011-02-18 15:02:18 -080017720 /**
17721 * Interface definition for a callback to be invoked when this view is attached
17722 * or detached from its window.
17723 */
17724 public interface OnAttachStateChangeListener {
17725 /**
17726 * Called when the view is attached to a window.
17727 * @param v The view that was attached
17728 */
17729 public void onViewAttachedToWindow(View v);
17730 /**
17731 * Called when the view is detached from a window.
17732 * @param v The view that was detached
17733 */
17734 public void onViewDetachedFromWindow(View v);
17735 }
17736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017737 private final class UnsetPressedState implements Runnable {
17738 public void run() {
17739 setPressed(false);
17740 }
17741 }
17742
17743 /**
17744 * Base class for derived classes that want to save and restore their own
17745 * state in {@link android.view.View#onSaveInstanceState()}.
17746 */
17747 public static class BaseSavedState extends AbsSavedState {
17748 /**
17749 * Constructor used when reading from a parcel. Reads the state of the superclass.
17750 *
17751 * @param source
17752 */
17753 public BaseSavedState(Parcel source) {
17754 super(source);
17755 }
17756
17757 /**
17758 * Constructor called by derived classes when creating their SavedState objects
17759 *
17760 * @param superState The state of the superclass of this view
17761 */
17762 public BaseSavedState(Parcelable superState) {
17763 super(superState);
17764 }
17765
17766 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17767 new Parcelable.Creator<BaseSavedState>() {
17768 public BaseSavedState createFromParcel(Parcel in) {
17769 return new BaseSavedState(in);
17770 }
17771
17772 public BaseSavedState[] newArray(int size) {
17773 return new BaseSavedState[size];
17774 }
17775 };
17776 }
17777
17778 /**
17779 * A set of information given to a view when it is attached to its parent
17780 * window.
17781 */
17782 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017783 interface Callbacks {
17784 void playSoundEffect(int effectId);
17785 boolean performHapticFeedback(int effectId, boolean always);
17786 }
17787
17788 /**
17789 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17790 * to a Handler. This class contains the target (View) to invalidate and
17791 * the coordinates of the dirty rectangle.
17792 *
17793 * For performance purposes, this class also implements a pool of up to
17794 * POOL_LIMIT objects that get reused. This reduces memory allocations
17795 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017796 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017797 static class InvalidateInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017798 private static final int POOL_LIMIT = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017799
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017800 private static final SynchronizedPool<InvalidateInfo> sPool =
17801 new SynchronizedPool<InvalidateInfo>(POOL_LIMIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017802
17803 View target;
17804
17805 int left;
17806 int top;
17807 int right;
17808 int bottom;
17809
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017810 public static InvalidateInfo obtain() {
17811 InvalidateInfo instance = sPool.acquire();
17812 return (instance != null) ? instance : new InvalidateInfo();
Romain Guyd928d682009-03-31 17:52:16 -070017813 }
17814
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080017815 public void recycle() {
17816 target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017817 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017818 }
17819 }
17820
17821 final IWindowSession mSession;
17822
17823 final IWindow mWindow;
17824
17825 final IBinder mWindowToken;
17826
Jeff Brown98365d72012-08-19 20:30:52 -070017827 final Display mDisplay;
17828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017829 final Callbacks mRootCallbacks;
17830
Romain Guy59a12ca2011-06-09 17:48:21 -070017831 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017833 /**
17834 * The top view of the hierarchy.
17835 */
17836 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017838 IBinder mPanelParentWindowToken;
17839 Surface mSurface;
17840
Romain Guyb051e892010-09-28 19:09:36 -070017841 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017842 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017843 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017844
Romain Guy7e4e5612012-03-05 14:37:29 -080017845 boolean mScreenOn;
17846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017847 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017848 * Scale factor used by the compatibility mode
17849 */
17850 float mApplicationScale;
17851
17852 /**
17853 * Indicates whether the application is in compatibility mode
17854 */
17855 boolean mScalingRequired;
17856
17857 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017858 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017859 */
17860 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017861
Dianne Hackborn63042d62011-01-26 18:56:29 -080017862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017863 * Left position of this view's window
17864 */
17865 int mWindowLeft;
17866
17867 /**
17868 * Top position of this view's window
17869 */
17870 int mWindowTop;
17871
17872 /**
Adam Powell26153a32010-11-08 15:22:27 -080017873 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017874 */
Adam Powell26153a32010-11-08 15:22:27 -080017875 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017876
17877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017878 * For windows that are full-screen but using insets to layout inside
17879 * of the screen decorations, these are the current insets for the
17880 * content of the window.
17881 */
17882 final Rect mContentInsets = new Rect();
17883
17884 /**
17885 * For windows that are full-screen but using insets to layout inside
17886 * of the screen decorations, these are the current insets for the
17887 * actual visible parts of the window.
17888 */
17889 final Rect mVisibleInsets = new Rect();
17890
17891 /**
17892 * The internal insets given by this window. This value is
17893 * supplied by the client (through
17894 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17895 * be given to the window manager when changed to be used in laying
17896 * out windows behind it.
17897 */
17898 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17899 = new ViewTreeObserver.InternalInsetsInfo();
17900
17901 /**
17902 * All views in the window's hierarchy that serve as scroll containers,
17903 * used to determine if the window can be resized or must be panned
17904 * to adjust for a soft input area.
17905 */
17906 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17907
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017908 final KeyEvent.DispatcherState mKeyDispatchState
17909 = new KeyEvent.DispatcherState();
17910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017911 /**
17912 * Indicates whether the view's window currently has the focus.
17913 */
17914 boolean mHasWindowFocus;
17915
17916 /**
17917 * The current visibility of the window.
17918 */
17919 int mWindowVisibility;
17920
17921 /**
17922 * Indicates the time at which drawing started to occur.
17923 */
17924 long mDrawingTime;
17925
17926 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017927 * Indicates whether or not ignoring the DIRTY_MASK flags.
17928 */
17929 boolean mIgnoreDirtyState;
17930
17931 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017932 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17933 * to avoid clearing that flag prematurely.
17934 */
17935 boolean mSetIgnoreDirtyState = false;
17936
17937 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017938 * Indicates whether the view's window is currently in touch mode.
17939 */
17940 boolean mInTouchMode;
17941
17942 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017943 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017944 * the next time it performs a traversal
17945 */
17946 boolean mRecomputeGlobalAttributes;
17947
17948 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017949 * Always report new attributes at next traversal.
17950 */
17951 boolean mForceReportNewAttributes;
17952
17953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017954 * Set during a traveral if any views want to keep the screen on.
17955 */
17956 boolean mKeepScreenOn;
17957
17958 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017959 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17960 */
17961 int mSystemUiVisibility;
17962
17963 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017964 * Hack to force certain system UI visibility flags to be cleared.
17965 */
17966 int mDisabledSystemUiVisibility;
17967
17968 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017969 * Last global system UI visibility reported by the window manager.
17970 */
17971 int mGlobalSystemUiVisibility;
17972
17973 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017974 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17975 * attached.
17976 */
17977 boolean mHasSystemUiListeners;
17978
17979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017980 * Set if the visibility of any views has changed.
17981 */
17982 boolean mViewVisibilityChanged;
17983
17984 /**
17985 * Set to true if a view has been scrolled.
17986 */
17987 boolean mViewScrollChanged;
17988
17989 /**
17990 * Global to the view hierarchy used as a temporary for dealing with
17991 * x/y points in the transparent region computations.
17992 */
17993 final int[] mTransparentLocation = new int[2];
17994
17995 /**
17996 * Global to the view hierarchy used as a temporary for dealing with
17997 * x/y points in the ViewGroup.invalidateChild implementation.
17998 */
17999 final int[] mInvalidateChildLocation = new int[2];
18000
Chet Haasec3aa3612010-06-17 08:50:37 -070018001
18002 /**
18003 * Global to the view hierarchy used as a temporary for dealing with
18004 * x/y location when view is transformed.
18005 */
18006 final float[] mTmpTransformLocation = new float[2];
18007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018008 /**
18009 * The view tree observer used to dispatch global events like
18010 * layout, pre-draw, touch mode change, etc.
18011 */
18012 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
18013
18014 /**
18015 * A Canvas used by the view hierarchy to perform bitmap caching.
18016 */
18017 Canvas mCanvas;
18018
18019 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080018020 * The view root impl.
18021 */
18022 final ViewRootImpl mViewRootImpl;
18023
18024 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070018025 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018026 * handler can be used to pump events in the UI events queue.
18027 */
18028 final Handler mHandler;
18029
18030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018031 * Temporary for use in computing invalidate rectangles while
18032 * calling up the hierarchy.
18033 */
18034 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070018035
18036 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070018037 * Temporary for use in computing hit areas with transformed views
18038 */
18039 final RectF mTmpTransformRect = new RectF();
18040
18041 /**
Chet Haase599913d2012-07-23 16:22:05 -070018042 * Temporary for use in transforming invalidation rect
18043 */
18044 final Matrix mTmpMatrix = new Matrix();
18045
18046 /**
18047 * Temporary for use in transforming invalidation rect
18048 */
18049 final Transformation mTmpTransformation = new Transformation();
18050
18051 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070018052 * Temporary list for use in collecting focusable descendents of a view.
18053 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070018054 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070018055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018056 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070018057 * The id of the window for accessibility purposes.
18058 */
18059 int mAccessibilityWindowId = View.NO_ID;
18060
18061 /**
Svetoslav Ganov80943d82013-01-02 10:25:37 -080018062 * Flags related to accessibility processing.
18063 *
18064 * @see AccessibilityNodeInfo#FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
18065 * @see AccessibilityNodeInfo#FLAG_REPORT_VIEW_IDS
Svetoslav Ganov42138042012-03-20 11:51:39 -070018066 */
Svetoslav Ganov80943d82013-01-02 10:25:37 -080018067 int mAccessibilityFetchFlags;
Svetoslav Ganov42138042012-03-20 11:51:39 -070018068
18069 /**
18070 * The drawable for highlighting accessibility focus.
18071 */
18072 Drawable mAccessibilityFocusDrawable;
18073
18074 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070018075 * Show where the margins, bounds and layout bounds are for each view.
18076 */
Dianne Hackborna53de062012-05-08 18:53:51 -070018077 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070018078
18079 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070018080 * Point used to compute visible regions.
18081 */
18082 final Point mPoint = new Point();
18083
18084 /**
Chet Haasecc699b42012-12-13 09:06:55 -080018085 * Used to track which View originated a requestLayout() call, used when
18086 * requestLayout() is called during layout.
18087 */
18088 View mViewRequestingLayout;
18089
18090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018091 * Creates a new set of attachment information with the specified
18092 * events handler and thread.
18093 *
18094 * @param handler the events handler the view must use
18095 */
Jeff Brown98365d72012-08-19 20:30:52 -070018096 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080018097 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018098 mSession = session;
18099 mWindow = window;
18100 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070018101 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080018102 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018103 mHandler = handler;
18104 mRootCallbacks = effectPlayer;
18105 }
18106 }
18107
18108 /**
18109 * <p>ScrollabilityCache holds various fields used by a View when scrolling
18110 * is supported. This avoids keeping too many unused fields in most
18111 * instances of View.</p>
18112 */
Mike Cleronf116bf82009-09-27 19:14:12 -070018113 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080018114
Mike Cleronf116bf82009-09-27 19:14:12 -070018115 /**
18116 * Scrollbars are not visible
18117 */
18118 public static final int OFF = 0;
18119
18120 /**
18121 * Scrollbars are visible
18122 */
18123 public static final int ON = 1;
18124
18125 /**
18126 * Scrollbars are fading away
18127 */
18128 public static final int FADING = 2;
18129
18130 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080018131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018132 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070018133 public int scrollBarDefaultDelayBeforeFade;
18134 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018135
18136 public int scrollBarSize;
18137 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070018138 public float[] interpolatorValues;
18139 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018140
18141 public final Paint paint;
18142 public final Matrix matrix;
18143 public Shader shader;
18144
Mike Cleronf116bf82009-09-27 19:14:12 -070018145 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
18146
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018147 private static final float[] OPAQUE = { 255 };
18148 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080018149
Mike Cleronf116bf82009-09-27 19:14:12 -070018150 /**
18151 * When fading should start. This time moves into the future every time
18152 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
18153 */
18154 public long fadeStartTime;
18155
18156
18157 /**
18158 * The current state of the scrollbars: ON, OFF, or FADING
18159 */
18160 public int state = OFF;
18161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018162 private int mLastColor;
18163
Mike Cleronf116bf82009-09-27 19:14:12 -070018164 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018165 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18166 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018167 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18168 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018169
18170 paint = new Paint();
18171 matrix = new Matrix();
18172 // use use a height of 1, and then wack the matrix each time we
18173 // actually use it.
18174 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018175 paint.setShader(shader);
18176 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018177
Mike Cleronf116bf82009-09-27 19:14:12 -070018178 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018179 }
Romain Guy8506ab42009-06-11 17:35:47 -070018180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018181 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018182 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018183 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018184
Romain Guyd679b572012-08-29 21:49:00 -070018185 if (color != 0) {
18186 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18187 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18188 paint.setShader(shader);
18189 // Restore the default transfer mode (src_over)
18190 paint.setXfermode(null);
18191 } else {
18192 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18193 paint.setShader(shader);
18194 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018196 }
18197 }
Joe Malin32736f02011-01-19 16:14:20 -080018198
Mike Cleronf116bf82009-09-27 19:14:12 -070018199 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018200 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018201 if (now >= fadeStartTime) {
18202
18203 // the animation fades the scrollbars out by changing
18204 // the opacity (alpha) from fully opaque to fully
18205 // transparent
18206 int nextFrame = (int) now;
18207 int framesCount = 0;
18208
18209 Interpolator interpolator = scrollBarInterpolator;
18210
18211 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018212 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018213
18214 // End transparent
18215 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018216 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018217
18218 state = FADING;
18219
18220 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018221 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018222 }
18223 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018224 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018225
Svetoslav Ganova0156172011-06-26 17:55:44 -070018226 /**
18227 * Resuable callback for sending
18228 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18229 */
18230 private class SendViewScrolledAccessibilityEvent implements Runnable {
18231 public volatile boolean mIsPending;
18232
18233 public void run() {
18234 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18235 mIsPending = false;
18236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018237 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018238
18239 /**
18240 * <p>
18241 * This class represents a delegate that can be registered in a {@link View}
18242 * to enhance accessibility support via composition rather via inheritance.
18243 * It is specifically targeted to widget developers that extend basic View
18244 * classes i.e. classes in package android.view, that would like their
18245 * applications to be backwards compatible.
18246 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018247 * <div class="special reference">
18248 * <h3>Developer Guides</h3>
18249 * <p>For more information about making applications accessible, read the
18250 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18251 * developer guide.</p>
18252 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018253 * <p>
18254 * A scenario in which a developer would like to use an accessibility delegate
18255 * is overriding a method introduced in a later API version then the minimal API
18256 * version supported by the application. For example, the method
18257 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18258 * in API version 4 when the accessibility APIs were first introduced. If a
18259 * developer would like his application to run on API version 4 devices (assuming
18260 * all other APIs used by the application are version 4 or lower) and take advantage
18261 * of this method, instead of overriding the method which would break the application's
18262 * backwards compatibility, he can override the corresponding method in this
18263 * delegate and register the delegate in the target View if the API version of
18264 * the system is high enough i.e. the API version is same or higher to the API
18265 * version that introduced
18266 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18267 * </p>
18268 * <p>
18269 * Here is an example implementation:
18270 * </p>
18271 * <code><pre><p>
18272 * if (Build.VERSION.SDK_INT >= 14) {
18273 * // If the API version is equal of higher than the version in
18274 * // which onInitializeAccessibilityNodeInfo was introduced we
18275 * // register a delegate with a customized implementation.
18276 * View view = findViewById(R.id.view_id);
18277 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18278 * public void onInitializeAccessibilityNodeInfo(View host,
18279 * AccessibilityNodeInfo info) {
18280 * // Let the default implementation populate the info.
18281 * super.onInitializeAccessibilityNodeInfo(host, info);
18282 * // Set some other information.
18283 * info.setEnabled(host.isEnabled());
18284 * }
18285 * });
18286 * }
18287 * </code></pre></p>
18288 * <p>
18289 * This delegate contains methods that correspond to the accessibility methods
18290 * in View. If a delegate has been specified the implementation in View hands
18291 * off handling to the corresponding method in this delegate. The default
18292 * implementation the delegate methods behaves exactly as the corresponding
18293 * method in View for the case of no accessibility delegate been set. Hence,
18294 * to customize the behavior of a View method, clients can override only the
18295 * corresponding delegate method without altering the behavior of the rest
18296 * accessibility related methods of the host view.
18297 * </p>
18298 */
18299 public static class AccessibilityDelegate {
18300
18301 /**
18302 * Sends an accessibility event of the given type. If accessibility is not
18303 * enabled this method has no effect.
18304 * <p>
18305 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18306 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18307 * been set.
18308 * </p>
18309 *
18310 * @param host The View hosting the delegate.
18311 * @param eventType The type of the event to send.
18312 *
18313 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18314 */
18315 public void sendAccessibilityEvent(View host, int eventType) {
18316 host.sendAccessibilityEventInternal(eventType);
18317 }
18318
18319 /**
alanv8eeefef2012-05-07 16:57:53 -070018320 * Performs the specified accessibility action on the view. For
18321 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18322 * <p>
18323 * The default implementation behaves as
18324 * {@link View#performAccessibilityAction(int, Bundle)
18325 * View#performAccessibilityAction(int, Bundle)} for the case of
18326 * no accessibility delegate been set.
18327 * </p>
18328 *
18329 * @param action The action to perform.
18330 * @return Whether the action was performed.
18331 *
18332 * @see View#performAccessibilityAction(int, Bundle)
18333 * View#performAccessibilityAction(int, Bundle)
18334 */
18335 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18336 return host.performAccessibilityActionInternal(action, args);
18337 }
18338
18339 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018340 * Sends an accessibility event. This method behaves exactly as
18341 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18342 * empty {@link AccessibilityEvent} and does not perform a check whether
18343 * accessibility is enabled.
18344 * <p>
18345 * The default implementation behaves as
18346 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18347 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18348 * the case of no accessibility delegate been set.
18349 * </p>
18350 *
18351 * @param host The View hosting the delegate.
18352 * @param event The event to send.
18353 *
18354 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18355 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18356 */
18357 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18358 host.sendAccessibilityEventUncheckedInternal(event);
18359 }
18360
18361 /**
18362 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18363 * to its children for adding their text content to the event.
18364 * <p>
18365 * The default implementation behaves as
18366 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18367 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18368 * the case of no accessibility delegate been set.
18369 * </p>
18370 *
18371 * @param host The View hosting the delegate.
18372 * @param event The event.
18373 * @return True if the event population was completed.
18374 *
18375 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18376 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18377 */
18378 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18379 return host.dispatchPopulateAccessibilityEventInternal(event);
18380 }
18381
18382 /**
18383 * Gives a chance to the host View to populate the accessibility event with its
18384 * text content.
18385 * <p>
18386 * The default implementation behaves as
18387 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18388 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18389 * the case of no accessibility delegate been set.
18390 * </p>
18391 *
18392 * @param host The View hosting the delegate.
18393 * @param event The accessibility event which to populate.
18394 *
18395 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18396 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18397 */
18398 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18399 host.onPopulateAccessibilityEventInternal(event);
18400 }
18401
18402 /**
18403 * Initializes an {@link AccessibilityEvent} with information about the
18404 * the host View which is the event source.
18405 * <p>
18406 * The default implementation behaves as
18407 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18408 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18409 * the case of no accessibility delegate been set.
18410 * </p>
18411 *
18412 * @param host The View hosting the delegate.
18413 * @param event The event to initialize.
18414 *
18415 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18416 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18417 */
18418 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18419 host.onInitializeAccessibilityEventInternal(event);
18420 }
18421
18422 /**
18423 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18424 * <p>
18425 * The default implementation behaves as
18426 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18427 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18428 * the case of no accessibility delegate been set.
18429 * </p>
18430 *
18431 * @param host The View hosting the delegate.
18432 * @param info The instance to initialize.
18433 *
18434 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18435 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18436 */
18437 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18438 host.onInitializeAccessibilityNodeInfoInternal(info);
18439 }
18440
18441 /**
18442 * Called when a child of the host View has requested sending an
18443 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18444 * to augment the event.
18445 * <p>
18446 * The default implementation behaves as
18447 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18448 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18449 * the case of no accessibility delegate been set.
18450 * </p>
18451 *
18452 * @param host The View hosting the delegate.
18453 * @param child The child which requests sending the event.
18454 * @param event The event to be sent.
18455 * @return True if the event should be sent
18456 *
18457 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18458 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18459 */
18460 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18461 AccessibilityEvent event) {
18462 return host.onRequestSendAccessibilityEventInternal(child, event);
18463 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018464
18465 /**
18466 * Gets the provider for managing a virtual view hierarchy rooted at this View
18467 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18468 * that explore the window content.
18469 * <p>
18470 * The default implementation behaves as
18471 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18472 * the case of no accessibility delegate been set.
18473 * </p>
18474 *
18475 * @return The provider.
18476 *
18477 * @see AccessibilityNodeProvider
18478 */
18479 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18480 return null;
18481 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018482 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018483
18484 private class MatchIdPredicate implements Predicate<View> {
18485 public int mId;
18486
18487 @Override
18488 public boolean apply(View view) {
18489 return (view.mID == mId);
18490 }
18491 }
18492
18493 private class MatchLabelForPredicate implements Predicate<View> {
18494 private int mLabeledId;
18495
18496 @Override
18497 public boolean apply(View view) {
18498 return (view.mLabelForId == mLabeledId);
18499 }
18500 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018501
18502 /**
18503 * Dump all private flags in readable format, useful for documentation and
18504 * sanity checking.
18505 */
18506 private static void dumpFlags() {
18507 final HashMap<String, String> found = Maps.newHashMap();
18508 try {
18509 for (Field field : View.class.getDeclaredFields()) {
18510 final int modifiers = field.getModifiers();
18511 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18512 if (field.getType().equals(int.class)) {
18513 final int value = field.getInt(null);
18514 dumpFlag(found, field.getName(), value);
18515 } else if (field.getType().equals(int[].class)) {
18516 final int[] values = (int[]) field.get(null);
18517 for (int i = 0; i < values.length; i++) {
18518 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18519 }
18520 }
18521 }
18522 }
18523 } catch (IllegalAccessException e) {
18524 throw new RuntimeException(e);
18525 }
18526
18527 final ArrayList<String> keys = Lists.newArrayList();
18528 keys.addAll(found.keySet());
18529 Collections.sort(keys);
18530 for (String key : keys) {
18531 Log.d(VIEW_LOG_TAG, found.get(key));
18532 }
18533 }
18534
18535 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18536 // Sort flags by prefix, then by bits, always keeping unique keys
18537 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18538 final int prefix = name.indexOf('_');
18539 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18540 final String output = bits + " " + name;
18541 found.put(key, output);
18542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018543}